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 ---
|
@@ -16,20 +15,27 @@ 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 |
OPENAI_MODEL_ID = os.getenv("OPENAI_MODEL_ID", "gpt-4.1")
|
21 |
|
22 |
-
# ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
|
|
24 |
@tool
|
25 |
def summarize_query(query: str) -> str:
|
26 |
"""
|
27 |
-
Reframes an unclear query into a
|
28 |
-
|
29 |
-
Args:
|
30 |
-
query (str): The search query to refine.
|
31 |
-
Returns:
|
32 |
-
str: A concise, improved query.
|
33 |
"""
|
34 |
return f"Summarize and reframe: {query}"
|
35 |
|
@@ -37,25 +43,19 @@ def summarize_query(query: str) -> str:
|
|
37 |
def wikipedia_search(page: str) -> str:
|
38 |
"""
|
39 |
Fetches the summary extract of an English Wikipedia page.
|
40 |
-
|
41 |
-
Args:
|
42 |
-
page (str): The page title (e.g. 'Mercedes_Sosa_discography').
|
43 |
-
Returns:
|
44 |
-
str: The extract section text.
|
45 |
"""
|
46 |
url = f"https://en.wikipedia.org/api/rest_v1/page/summary/{page}"
|
47 |
-
|
48 |
-
|
49 |
-
return
|
50 |
|
51 |
search_tool = DuckDuckGoSearchTool()
|
52 |
wiki_tool = wikipedia_search
|
53 |
summarize_tool = summarize_query
|
54 |
|
55 |
# --- ReACT + Scratchpad + Auto-Retry Instruction Prompt ---
|
56 |
-
|
57 |
instruction_prompt = """
|
58 |
-
You are a ReACT agent with three tools:
|
59 |
• DuckDuckGoSearchTool(query: str)
|
60 |
• wikipedia_search(page: str)
|
61 |
• summarize_query(query: str)
|
@@ -69,8 +69,8 @@ Internally, for each question:
|
|
69 |
Record new Observation.
|
70 |
5. Thought: integrate observations.
|
71 |
|
72 |
-
Finally, output
|
73 |
-
|
74 |
|
75 |
Rules:
|
76 |
- Numbers: digits only.
|
@@ -78,63 +78,43 @@ Rules:
|
|
78 |
- Strings: no filler words.
|
79 |
"""
|
80 |
|
81 |
-
# --- Model wrapper to satisfy CodeAgent's expected interface ---
|
82 |
-
|
83 |
-
class OpenAIModelWrapper:
|
84 |
-
def __init__(self, model_id: str, client: OpenAI):
|
85 |
-
self.model_id = model_id
|
86 |
-
self.client = client
|
87 |
-
|
88 |
-
def __call__(self, prompt: str, **kwargs) -> str:
|
89 |
-
# kwargs catches stop_sequences, temperature, etc.
|
90 |
-
resp = self.client.responses.create(
|
91 |
-
model=self.model_id,
|
92 |
-
input_text=prompt # <-- use input_text here
|
93 |
-
)
|
94 |
-
return resp.output_text
|
95 |
-
|
96 |
-
llm_wrapper = OpenAIModelWrapper(model_id=OPENAI_MODEL_ID, client=client)
|
97 |
-
|
98 |
# --- Build the CodeAgent ---
|
99 |
-
|
100 |
smart_agent = CodeAgent(
|
101 |
tools=[search_tool, wiki_tool, summarize_tool],
|
102 |
model=llm_wrapper
|
103 |
)
|
104 |
|
105 |
# --- BasicAgent wrapper for Gradio ---
|
106 |
-
|
107 |
class BasicAgent:
|
108 |
def __init__(self):
|
109 |
print("SmolAgent (GPT-4.1) with ReACT & tools initialized.")
|
110 |
|
111 |
def __call__(self, question: str) -> str:
|
112 |
prompt = instruction_prompt.strip() + "\n\nQUESTION: " + question.strip()
|
113 |
-
print(f"Agent prompt: {prompt[:120]}…")
|
114 |
try:
|
115 |
return smart_agent.run(prompt)
|
116 |
except Exception as e:
|
117 |
return f"AGENT ERROR: {e}"
|
118 |
|
119 |
# --- Gradio / Submission Logic ---
|
120 |
-
|
121 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
122 |
if not profile:
|
123 |
return "Please log in to Hugging Face.", None
|
124 |
username = profile.username
|
125 |
space_id = os.getenv("SPACE_ID", "")
|
126 |
-
agent = BasicAgent()
|
127 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
|
|
128 |
|
129 |
-
#
|
130 |
try:
|
131 |
-
resp
|
132 |
resp.raise_for_status()
|
133 |
questions = resp.json() or []
|
134 |
except Exception as e:
|
135 |
return f"Error fetching questions: {e}", None
|
136 |
|
137 |
-
#
|
138 |
logs, payload = [], []
|
139 |
for item in questions:
|
140 |
tid = item.get("task_id")
|
@@ -148,7 +128,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
148 |
if not payload:
|
149 |
return "Agent did not produce any answers.", pd.DataFrame(logs)
|
150 |
|
151 |
-
#
|
152 |
submission = {"username": username, "agent_code": agent_code, "answers": payload}
|
153 |
try:
|
154 |
post = requests.post(f"{DEFAULT_API_URL}/submit", json=submission, timeout=60)
|
@@ -157,8 +137,8 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
157 |
status = (
|
158 |
f"Submission Successful!\n"
|
159 |
f"User: {res.get('username')}\n"
|
160 |
-
f"Overall Score: {res.get('score',
|
161 |
-
f"({res.get('correct_count',
|
162 |
f"Message: {res.get('message','')}"
|
163 |
)
|
164 |
return status, pd.DataFrame(logs)
|
@@ -166,7 +146,6 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
166 |
return f"Submission Failed: {e}", pd.DataFrame(logs)
|
167 |
|
168 |
# --- Gradio Interface ---
|
169 |
-
|
170 |
with gr.Blocks() as demo:
|
171 |
gr.Markdown("# SmolAgent GAIA Runner 🚀")
|
172 |
gr.Markdown("""
|
@@ -180,7 +159,6 @@ with gr.Blocks() as demo:
|
|
180 |
run_btn = gr.Button("Run Evaluation & Submit All Answers")
|
181 |
status_out = gr.Textbox(label="Status", lines=5, interactive=False)
|
182 |
table_out = gr.DataFrame(label="Questions & Answers", wrap=True)
|
183 |
-
|
184 |
run_btn.click(fn=run_and_submit_all, outputs=[status_out, table_out])
|
185 |
|
186 |
if __name__ == "__main__":
|
|
|
4 |
import gradio as gr
|
5 |
import requests
|
6 |
import pandas as pd
|
|
|
7 |
from smolagents import CodeAgent, DuckDuckGoSearchTool, tool
|
8 |
|
9 |
# --- Constants ---
|
|
|
15 |
raise RuntimeError("Please set OPENAI_API_KEY in your Space secrets or env!")
|
16 |
openai.api_key = openai_api_key
|
17 |
client = OpenAI()
|
|
|
18 |
OPENAI_MODEL_ID = os.getenv("OPENAI_MODEL_ID", "gpt-4.1")
|
19 |
|
20 |
+
# --- Model Wrapper for CodeAgent compatibility ---
|
21 |
+
class OpenAIModelWrapper:
|
22 |
+
def __init__(self, model_id: str, client: OpenAI):
|
23 |
+
self.model_id = model_id
|
24 |
+
self.client = client
|
25 |
+
|
26 |
+
def __call__(self, prompt: str, **kwargs) -> str:
|
27 |
+
# ignore extra kwargs (stop_sequences, temperature, etc.)
|
28 |
+
resp = self.client.responses.create(
|
29 |
+
model=self.model_id,
|
30 |
+
input=prompt
|
31 |
+
)
|
32 |
+
return resp.output_text
|
33 |
|
34 |
+
# --- Tool Definitions ---
|
35 |
@tool
|
36 |
def summarize_query(query: str) -> str:
|
37 |
"""
|
38 |
+
Reframes an unclear query into a concise one.
|
|
|
|
|
|
|
|
|
|
|
39 |
"""
|
40 |
return f"Summarize and reframe: {query}"
|
41 |
|
|
|
43 |
def wikipedia_search(page: str) -> str:
|
44 |
"""
|
45 |
Fetches the summary extract of an English Wikipedia page.
|
|
|
|
|
|
|
|
|
|
|
46 |
"""
|
47 |
url = f"https://en.wikipedia.org/api/rest_v1/page/summary/{page}"
|
48 |
+
r = requests.get(url, timeout=10)
|
49 |
+
r.raise_for_status()
|
50 |
+
return r.json().get("extract", "")
|
51 |
|
52 |
search_tool = DuckDuckGoSearchTool()
|
53 |
wiki_tool = wikipedia_search
|
54 |
summarize_tool = summarize_query
|
55 |
|
56 |
# --- ReACT + Scratchpad + Auto-Retry Instruction Prompt ---
|
|
|
57 |
instruction_prompt = """
|
58 |
+
You are a ReACT agent with three tools:
|
59 |
• DuckDuckGoSearchTool(query: str)
|
60 |
• wikipedia_search(page: str)
|
61 |
• summarize_query(query: str)
|
|
|
69 |
Record new Observation.
|
70 |
5. Thought: integrate observations.
|
71 |
|
72 |
+
Finally, output your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.
|
73 |
+
|
74 |
|
75 |
Rules:
|
76 |
- Numbers: digits only.
|
|
|
78 |
- Strings: no filler words.
|
79 |
"""
|
80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
# --- Build the CodeAgent ---
|
82 |
+
llm_wrapper = OpenAIModelWrapper(model_id=OPENAI_MODEL_ID, client=client)
|
83 |
smart_agent = CodeAgent(
|
84 |
tools=[search_tool, wiki_tool, summarize_tool],
|
85 |
model=llm_wrapper
|
86 |
)
|
87 |
|
88 |
# --- BasicAgent wrapper for Gradio ---
|
|
|
89 |
class BasicAgent:
|
90 |
def __init__(self):
|
91 |
print("SmolAgent (GPT-4.1) with ReACT & tools initialized.")
|
92 |
|
93 |
def __call__(self, question: str) -> str:
|
94 |
prompt = instruction_prompt.strip() + "\n\nQUESTION: " + question.strip()
|
|
|
95 |
try:
|
96 |
return smart_agent.run(prompt)
|
97 |
except Exception as e:
|
98 |
return f"AGENT ERROR: {e}"
|
99 |
|
100 |
# --- Gradio / Submission Logic ---
|
|
|
101 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
102 |
if not profile:
|
103 |
return "Please log in to Hugging Face.", None
|
104 |
username = profile.username
|
105 |
space_id = os.getenv("SPACE_ID", "")
|
|
|
106 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
107 |
+
agent = BasicAgent()
|
108 |
|
109 |
+
# Fetch questions
|
110 |
try:
|
111 |
+
resp = requests.get(f"{DEFAULT_API_URL}/questions", timeout=15)
|
112 |
resp.raise_for_status()
|
113 |
questions = resp.json() or []
|
114 |
except Exception as e:
|
115 |
return f"Error fetching questions: {e}", None
|
116 |
|
117 |
+
# Run agent
|
118 |
logs, payload = [], []
|
119 |
for item in questions:
|
120 |
tid = item.get("task_id")
|
|
|
128 |
if not payload:
|
129 |
return "Agent did not produce any answers.", pd.DataFrame(logs)
|
130 |
|
131 |
+
# Submit
|
132 |
submission = {"username": username, "agent_code": agent_code, "answers": payload}
|
133 |
try:
|
134 |
post = requests.post(f"{DEFAULT_API_URL}/submit", json=submission, timeout=60)
|
|
|
137 |
status = (
|
138 |
f"Submission Successful!\n"
|
139 |
f"User: {res.get('username')}\n"
|
140 |
+
f"Overall Score: {res.get('score','N/A')}% "
|
141 |
+
f"({res.get('correct_count','?')}/{res.get('total_attempted','?')})\n"
|
142 |
f"Message: {res.get('message','')}"
|
143 |
)
|
144 |
return status, pd.DataFrame(logs)
|
|
|
146 |
return f"Submission Failed: {e}", pd.DataFrame(logs)
|
147 |
|
148 |
# --- Gradio Interface ---
|
|
|
149 |
with gr.Blocks() as demo:
|
150 |
gr.Markdown("# SmolAgent GAIA Runner 🚀")
|
151 |
gr.Markdown("""
|
|
|
159 |
run_btn = gr.Button("Run Evaluation & Submit All Answers")
|
160 |
status_out = gr.Textbox(label="Status", lines=5, interactive=False)
|
161 |
table_out = gr.DataFrame(label="Questions & Answers", wrap=True)
|
|
|
162 |
run_btn.click(fn=run_and_submit_all, outputs=[status_out, table_out])
|
163 |
|
164 |
if __name__ == "__main__":
|