Update app.py
Browse files
app.py
CHANGED
@@ -1,19 +1,41 @@
|
|
1 |
import os
|
2 |
import openai
|
|
|
3 |
import gradio as gr
|
4 |
import requests
|
5 |
import pandas as pd
|
6 |
-
|
|
|
7 |
|
8 |
# --- Constants ---
|
9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
10 |
|
11 |
-
# --- Configure OpenAI SDK ---
|
12 |
openai_api_key = os.getenv("OPENAI_API_KEY")
|
13 |
if not openai_api_key:
|
14 |
-
raise RuntimeError("Please set OPENAI_API_KEY in your Space secrets
|
15 |
openai.api_key = openai_api_key
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
# --- Tool Definitions ---
|
19 |
|
@@ -51,7 +73,7 @@ summarize_tool = summarize_query
|
|
51 |
# --- ReACT + Scratchpad + Auto-Retry Prompt ---
|
52 |
|
53 |
instruction_prompt = """
|
54 |
-
You are a ReACT agent with three tools:
|
55 |
• DuckDuckGoSearchTool(query: str)
|
56 |
• wikipedia_search(page: str)
|
57 |
• summarize_query(query: str)
|
@@ -65,21 +87,22 @@ Internally, for each question:
|
|
65 |
Record new Observation.
|
66 |
5. Thought: integrate observations.
|
67 |
|
68 |
-
Finally, output
|
69 |
-
|
70 |
|
|
|
|
|
|
|
|
|
71 |
"""
|
72 |
|
73 |
-
# --- Build the CodeAgent with the
|
74 |
|
75 |
-
|
76 |
-
model_id=openai_model_id,
|
77 |
-
api_key=openai_api_key
|
78 |
-
)
|
79 |
|
80 |
smart_agent = CodeAgent(
|
81 |
tools=[search_tool, wiki_tool, summarize_tool],
|
82 |
-
model=
|
83 |
)
|
84 |
|
85 |
# --- Wrap in BasicAgent for Gradio ---
|
@@ -106,6 +129,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
106 |
agent = BasicAgent()
|
107 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
108 |
|
|
|
109 |
try:
|
110 |
resp = requests.get(f"{DEFAULT_API_URL}/questions", timeout=15)
|
111 |
resp.raise_for_status()
|
@@ -113,6 +137,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
113 |
except Exception as e:
|
114 |
return f"Error fetching questions: {e}", None
|
115 |
|
|
|
116 |
logs, payload = [], []
|
117 |
for item in questions:
|
118 |
tid = item.get("task_id")
|
@@ -126,6 +151,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
126 |
if not payload:
|
127 |
return "Agent did not produce any answers.", pd.DataFrame(logs)
|
128 |
|
|
|
129 |
submission = {"username": username, "agent_code": agent_code, "answers": payload}
|
130 |
try:
|
131 |
post = requests.post(f"{DEFAULT_API_URL}/submit", json=submission, timeout=60)
|
@@ -142,6 +168,8 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
142 |
except Exception as e:
|
143 |
return f"Submission Failed: {e}", pd.DataFrame(logs)
|
144 |
|
|
|
|
|
145 |
with gr.Blocks() as demo:
|
146 |
gr.Markdown("# SmolAgent GAIA Runner 🚀")
|
147 |
gr.Markdown("""
|
@@ -152,7 +180,7 @@ with gr.Blocks() as demo:
|
|
152 |
4. Click **Run Evaluation & Submit All Answers**.
|
153 |
""")
|
154 |
gr.LoginButton()
|
155 |
-
run_btn
|
156 |
status_out = gr.Textbox(label="Status", lines=5, interactive=False)
|
157 |
table_out = gr.DataFrame(label="Questions & Answers", wrap=True)
|
158 |
|
|
|
1 |
import os
|
2 |
import openai
|
3 |
+
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 ---
|
11 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
12 |
|
13 |
+
# --- Configure OpenAI SDK & Client ---
|
14 |
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() # official OpenAI client
|
19 |
+
|
20 |
+
# Optional override for model id
|
21 |
+
OPENAI_MODEL_ID = os.getenv("OPENAI_MODEL_ID", "gpt-4.1")
|
22 |
+
|
23 |
+
# --- Custom smolagents Model Wrapper around OpenAI client ---
|
24 |
+
class OpenAIModelWrapper:
|
25 |
+
"""
|
26 |
+
Adapts the OpenAI client.responses.create API to smolagents BaseModel interface.
|
27 |
+
"""
|
28 |
+
def __init__(self, model_id: str, client: OpenAI):
|
29 |
+
self.model_id = model_id
|
30 |
+
self.client = client
|
31 |
+
|
32 |
+
def run(self, prompt: str) -> str:
|
33 |
+
# call the new OpenAI SDK endpoint
|
34 |
+
resp = self.client.responses.create(
|
35 |
+
model=self.model_id,
|
36 |
+
input=prompt
|
37 |
+
)
|
38 |
+
return resp.output_text
|
39 |
|
40 |
# --- Tool Definitions ---
|
41 |
|
|
|
73 |
# --- ReACT + Scratchpad + Auto-Retry Prompt ---
|
74 |
|
75 |
instruction_prompt = """
|
76 |
+
You are a ReACT agent with three tools:
|
77 |
• DuckDuckGoSearchTool(query: str)
|
78 |
• wikipedia_search(page: str)
|
79 |
• summarize_query(query: str)
|
|
|
87 |
Record new Observation.
|
88 |
5. Thought: integrate observations.
|
89 |
|
90 |
+
Finally, output exactly one line:
|
91 |
+
FINAL ANSWER: [your concise answer]
|
92 |
|
93 |
+
Rules:
|
94 |
+
- Numbers: digits only.
|
95 |
+
- Lists: comma-separated, no extra punctuation.
|
96 |
+
- Strings: no filler words.
|
97 |
"""
|
98 |
|
99 |
+
# --- Build the CodeAgent with the OpenAIModelWrapper ---
|
100 |
|
101 |
+
llm_wrapper = OpenAIModelWrapper(model_id=OPENAI_MODEL_ID, client=client)
|
|
|
|
|
|
|
102 |
|
103 |
smart_agent = CodeAgent(
|
104 |
tools=[search_tool, wiki_tool, summarize_tool],
|
105 |
+
model=llm_wrapper
|
106 |
)
|
107 |
|
108 |
# --- Wrap in BasicAgent for Gradio ---
|
|
|
129 |
agent = BasicAgent()
|
130 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
131 |
|
132 |
+
# 1. Fetch questions
|
133 |
try:
|
134 |
resp = requests.get(f"{DEFAULT_API_URL}/questions", timeout=15)
|
135 |
resp.raise_for_status()
|
|
|
137 |
except Exception as e:
|
138 |
return f"Error fetching questions: {e}", None
|
139 |
|
140 |
+
# 2. Run agent on each question
|
141 |
logs, payload = [], []
|
142 |
for item in questions:
|
143 |
tid = item.get("task_id")
|
|
|
151 |
if not payload:
|
152 |
return "Agent did not produce any answers.", pd.DataFrame(logs)
|
153 |
|
154 |
+
# 3. Submit answers
|
155 |
submission = {"username": username, "agent_code": agent_code, "answers": payload}
|
156 |
try:
|
157 |
post = requests.post(f"{DEFAULT_API_URL}/submit", json=submission, timeout=60)
|
|
|
168 |
except Exception as e:
|
169 |
return f"Submission Failed: {e}", pd.DataFrame(logs)
|
170 |
|
171 |
+
# --- Gradio Interface ---
|
172 |
+
|
173 |
with gr.Blocks() as demo:
|
174 |
gr.Markdown("# SmolAgent GAIA Runner 🚀")
|
175 |
gr.Markdown("""
|
|
|
180 |
4. Click **Run Evaluation & Submit All Answers**.
|
181 |
""")
|
182 |
gr.LoginButton()
|
183 |
+
run_btn = gr.Button("Run Evaluation & Submit All Answers")
|
184 |
status_out = gr.Textbox(label="Status", lines=5, interactive=False)
|
185 |
table_out = gr.DataFrame(label="Questions & Answers", wrap=True)
|
186 |
|