Update app.py
Browse files
app.py
CHANGED
@@ -25,7 +25,7 @@ def summarize_query(query: str) -> str:
|
|
25 |
|
26 |
search_tool = DuckDuckGoSearchTool()
|
27 |
|
28 |
-
# --- System
|
29 |
|
30 |
system_message = """
|
31 |
You are a ReACT agent with scratchpad memory and a retry mechanism.
|
@@ -50,26 +50,14 @@ Formatting Rules:
|
|
50 |
- Numbers: plain (no commas unless list)
|
51 |
- Strings: no articles unless inside proper names
|
52 |
- Lists: comma-separated without extra punctuation
|
53 |
-
|
54 |
-
Example scratchpad flow:
|
55 |
-
Thought: Need fruits from painting.
|
56 |
-
Action: DuckDuckGoSearchTool('fruits in Embroidery from Uzbekistan painting')
|
57 |
-
Observation: (empty)
|
58 |
-
Thought: Unclear result, retry.
|
59 |
-
Action: summarize_query('fruits in Embroidery painting Uzbekistan')
|
60 |
-
Observation: pomegranate, apple, grape
|
61 |
-
Thought: Find breakfast fruits.
|
62 |
-
Action: DuckDuckGoSearchTool('breakfast menu October 1949 SS Ile de France')
|
63 |
-
Observation: grapes, apples, oranges
|
64 |
-
Thought: Overlap is grapes and apples.
|
65 |
-
FINAL ANSWER: grapes, apples
|
66 |
"""
|
67 |
|
68 |
# --- Build the Smart Agent ---
|
69 |
|
70 |
smart_agent = CodeAgent(
|
71 |
tools=[search_tool, summarize_query],
|
72 |
-
model=HfApiModel(
|
|
|
73 |
)
|
74 |
|
75 |
# --- Integrate into Gradio App ---
|
@@ -80,121 +68,71 @@ class BasicAgent:
|
|
80 |
|
81 |
def __call__(self, question: str) -> str:
|
82 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
83 |
-
|
84 |
-
print(f"Agent returning answer: {answer}")
|
85 |
-
return answer
|
86 |
|
87 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
88 |
space_id = os.getenv("SPACE_ID")
|
89 |
-
|
90 |
if profile:
|
91 |
username = profile.username
|
92 |
-
print(f"User logged in: {username}")
|
93 |
else:
|
94 |
-
print("User not logged in.")
|
95 |
return "Please log in to Hugging Face using the button above.", None
|
96 |
|
97 |
-
|
98 |
-
questions_url = f"{api_url}/questions"
|
99 |
-
submit_url = f"{api_url}/submit"
|
100 |
-
|
101 |
-
try:
|
102 |
-
agent = BasicAgent()
|
103 |
-
except Exception as e:
|
104 |
-
return f"Error initializing agent: {e}", None
|
105 |
-
|
106 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
107 |
|
108 |
# Fetch questions
|
109 |
try:
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
if not
|
114 |
-
return "
|
115 |
except Exception as e:
|
116 |
return f"Error fetching questions: {e}", None
|
117 |
|
118 |
-
# Run agent
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
if not task_id or question_text is None:
|
125 |
continue
|
126 |
try:
|
127 |
-
|
128 |
-
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
129 |
-
results_log.append({
|
130 |
-
"Task ID": task_id,
|
131 |
-
"Question": question_text,
|
132 |
-
"Submitted Answer": submitted_answer
|
133 |
-
})
|
134 |
except Exception as e:
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
# Submit answers
|
145 |
-
submission_data = {
|
146 |
-
"username": username,
|
147 |
-
"agent_code": agent_code,
|
148 |
-
"answers": answers_payload
|
149 |
-
}
|
150 |
try:
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
f"Submission Successful!\n"
|
156 |
-
f"User: {
|
157 |
-
f"
|
158 |
-
f"({
|
159 |
-
f"{result_data.get('total_attempted', '?')} correct)\n"
|
160 |
-
f"Message: {result_data.get('message', '')}"
|
161 |
)
|
162 |
-
|
163 |
-
return final_status, results_df
|
164 |
except Exception as e:
|
165 |
-
|
166 |
-
return f"Submission Failed: {e}", results_df
|
167 |
|
168 |
-
# --- Gradio
|
169 |
|
170 |
with gr.Blocks() as demo:
|
171 |
gr.Markdown("# SmolAgent GAIA Evaluation Runner 🚀")
|
172 |
-
gr.Markdown(
|
173 |
-
"""
|
174 |
-
**Instructions:**
|
175 |
-
1. Clone this space and modify if needed.
|
176 |
-
2. Log in to Hugging Face.
|
177 |
-
3. Click 'Run Evaluation & Submit All Answers'.
|
178 |
-
**Note:** Evaluation can take a few minutes.
|
179 |
-
"""
|
180 |
-
)
|
181 |
gr.LoginButton()
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
run_button.click(
|
187 |
-
fn=run_and_submit_all,
|
188 |
-
outputs=[status_output, results_table]
|
189 |
-
)
|
190 |
|
191 |
if __name__ == "__main__":
|
192 |
-
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
193 |
-
space_host = os.getenv("SPACE_HOST")
|
194 |
-
space_id = os.getenv("SPACE_ID")
|
195 |
-
if space_host:
|
196 |
-
print(f"SPACE_HOST: {space_host}")
|
197 |
-
if space_id:
|
198 |
-
print(f"SPACE_ID: {space_id}")
|
199 |
-
print("Launching Gradio Interface...")
|
200 |
demo.launch(debug=True, share=False)
|
|
|
25 |
|
26 |
search_tool = DuckDuckGoSearchTool()
|
27 |
|
28 |
+
# --- System Message for ReACT + Scratchpad + Auto-Retry ---
|
29 |
|
30 |
system_message = """
|
31 |
You are a ReACT agent with scratchpad memory and a retry mechanism.
|
|
|
50 |
- Numbers: plain (no commas unless list)
|
51 |
- Strings: no articles unless inside proper names
|
52 |
- Lists: comma-separated without extra punctuation
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
"""
|
54 |
|
55 |
# --- Build the Smart Agent ---
|
56 |
|
57 |
smart_agent = CodeAgent(
|
58 |
tools=[search_tool, summarize_query],
|
59 |
+
model=HfApiModel(), # no prompt here
|
60 |
+
system_message=system_message # moved into CodeAgent
|
61 |
)
|
62 |
|
63 |
# --- Integrate into Gradio App ---
|
|
|
68 |
|
69 |
def __call__(self, question: str) -> str:
|
70 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
71 |
+
return smart_agent.run(question)
|
|
|
|
|
72 |
|
73 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
74 |
space_id = os.getenv("SPACE_ID")
|
|
|
75 |
if profile:
|
76 |
username = profile.username
|
|
|
77 |
else:
|
|
|
78 |
return "Please log in to Hugging Face using the button above.", None
|
79 |
|
80 |
+
agent = BasicAgent()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
82 |
|
83 |
# Fetch questions
|
84 |
try:
|
85 |
+
resp = requests.get(f"{DEFAULT_API_URL}/questions", timeout=15)
|
86 |
+
resp.raise_for_status()
|
87 |
+
questions = resp.json()
|
88 |
+
if not questions:
|
89 |
+
return "No questions fetched.", None
|
90 |
except Exception as e:
|
91 |
return f"Error fetching questions: {e}", None
|
92 |
|
93 |
+
# Run agent
|
94 |
+
logs, payload = [], []
|
95 |
+
for item in questions:
|
96 |
+
tid = item.get("task_id")
|
97 |
+
q = item.get("question")
|
98 |
+
if not tid or q is None:
|
|
|
99 |
continue
|
100 |
try:
|
101 |
+
ans = agent(q)
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
except Exception as e:
|
103 |
+
ans = f"AGENT ERROR: {e}"
|
104 |
+
logs.append({"Task ID": tid, "Question": q, "Submitted Answer": ans})
|
105 |
+
payload.append({"task_id": tid, "submitted_answer": ans})
|
106 |
+
|
107 |
+
if not payload:
|
108 |
+
return "Agent did not produce any answers to submit.", pd.DataFrame(logs)
|
109 |
+
|
110 |
+
# Submit
|
111 |
+
sub = {"username": username, "agent_code": agent_code, "answers": payload}
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
try:
|
113 |
+
r = requests.post(f"{DEFAULT_API_URL}/submit", json=sub, timeout=60)
|
114 |
+
r.raise_for_status()
|
115 |
+
res = r.json()
|
116 |
+
status = (
|
117 |
f"Submission Successful!\n"
|
118 |
+
f"User: {res.get('username')}\n"
|
119 |
+
f"Score: {res.get('score', 'N/A')}% "
|
120 |
+
f"({res.get('correct_count', '?')}/{res.get('total_attempted', '?')})"
|
|
|
|
|
121 |
)
|
122 |
+
return status, pd.DataFrame(logs)
|
|
|
123 |
except Exception as e:
|
124 |
+
return f"Submission Failed: {e}", pd.DataFrame(logs)
|
|
|
125 |
|
126 |
+
# --- Gradio UI ---
|
127 |
|
128 |
with gr.Blocks() as demo:
|
129 |
gr.Markdown("# SmolAgent GAIA Evaluation Runner 🚀")
|
130 |
+
gr.Markdown("1. Clone this space 2. Log in 3. Click **Run Evaluation & Submit All Answers**")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
131 |
gr.LoginButton()
|
132 |
+
btn = gr.Button("Run Evaluation & Submit All Answers")
|
133 |
+
out_status = gr.Textbox(label="Status", lines=5, interactive=False)
|
134 |
+
out_table = gr.DataFrame(label="Results")
|
135 |
+
btn.click(fn=run_and_submit_all, outputs=[out_status, out_table])
|
|
|
|
|
|
|
|
|
136 |
|
137 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
138 |
demo.launch(debug=True, share=False)
|