Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,6 @@ from openai import OpenAI
|
|
4 |
import gradio as gr
|
5 |
import requests
|
6 |
import pandas as pd
|
7 |
-
|
8 |
from smolagents import CodeAgent, DuckDuckGoSearchTool, tool
|
9 |
|
10 |
# --- Constants ---
|
@@ -15,22 +14,20 @@ openai_api_key = os.getenv("OPENAI_API_KEY")
|
|
15 |
if not openai_api_key:
|
16 |
raise RuntimeError("Please set OPENAI_API_KEY in your Space secrets or env!")
|
17 |
openai.api_key = openai_api_key
|
18 |
-
client = OpenAI()
|
19 |
-
|
20 |
-
# Optional override for model id
|
21 |
OPENAI_MODEL_ID = os.getenv("OPENAI_MODEL_ID", "gpt-4.1")
|
22 |
|
23 |
-
# ---
|
24 |
class OpenAIModelWrapper:
|
25 |
"""
|
26 |
-
|
|
|
27 |
"""
|
28 |
def __init__(self, model_id: str, client: OpenAI):
|
29 |
self.model_id = model_id
|
30 |
self.client = client
|
31 |
|
32 |
-
def
|
33 |
-
# call the new OpenAI SDK endpoint
|
34 |
resp = self.client.responses.create(
|
35 |
model=self.model_id,
|
36 |
input=prompt
|
@@ -96,7 +93,7 @@ Rules:
|
|
96 |
- Strings: no filler words.
|
97 |
"""
|
98 |
|
99 |
-
# --- Build the CodeAgent with the
|
100 |
|
101 |
llm_wrapper = OpenAIModelWrapper(model_id=OPENAI_MODEL_ID, client=client)
|
102 |
|
|
|
4 |
import gradio as gr
|
5 |
import requests
|
6 |
import pandas as pd
|
|
|
7 |
from smolagents import CodeAgent, DuckDuckGoSearchTool, tool
|
8 |
|
9 |
# --- Constants ---
|
|
|
14 |
if not openai_api_key:
|
15 |
raise RuntimeError("Please set OPENAI_API_KEY in your Space secrets or env!")
|
16 |
openai.api_key = openai_api_key
|
17 |
+
client = OpenAI() # official OpenAI client
|
|
|
|
|
18 |
OPENAI_MODEL_ID = os.getenv("OPENAI_MODEL_ID", "gpt-4.1")
|
19 |
|
20 |
+
# --- Model Wrapper with __call__ ---
|
21 |
class OpenAIModelWrapper:
|
22 |
"""
|
23 |
+
Wraps the new OpenAI client.responses.create API so that
|
24 |
+
CodeAgent can call it directly.
|
25 |
"""
|
26 |
def __init__(self, model_id: str, client: OpenAI):
|
27 |
self.model_id = model_id
|
28 |
self.client = client
|
29 |
|
30 |
+
def __call__(self, prompt: str) -> str:
|
|
|
31 |
resp = self.client.responses.create(
|
32 |
model=self.model_id,
|
33 |
input=prompt
|
|
|
93 |
- Strings: no filler words.
|
94 |
"""
|
95 |
|
96 |
+
# --- Build the CodeAgent with the callable wrapper ---
|
97 |
|
98 |
llm_wrapper = OpenAIModelWrapper(model_id=OPENAI_MODEL_ID, client=client)
|
99 |
|