alfulanny commited on
Commit
0a11885
·
verified ·
1 Parent(s): 0106d0b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -223
app.py CHANGED
@@ -1,232 +1,33 @@
1
- import os
 
 
 
 
 
 
 
 
2
  import gradio as gr
3
- import requests
4
- import inspect
5
- import pandas as pd
6
- from smoleagents_agent import OptimizedSmolagentsGAIAgent as SmolagentsGAIAgent
7
 
8
- # --- Constants ---
9
 
10
- DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
11
-
12
- def run_and_submit_all(profile: gr.OAuthProfile | None):
13
- """
14
- Fetches all questions, runs the Enhanced DirectToolAgent on them, submits all answers,
15
- and displays the results.
16
- """
17
- # --- Determine HF Space Runtime URL and Repo URL ---
18
- space_id = os.getenv("SPACE_ID")
19
-
20
- if profile:
21
- username = f"{profile.username}"
22
- print(f"User logged in: {username}")
23
- else:
24
- print("User not logged in.")
25
- return "Please Login to Hugging Face with the button.", None
26
-
27
- api_url = DEFAULT_API_URL
28
- questions_url = f"{api_url}/questions"
29
- submit_url = f"{api_url}/submit"
30
-
31
- # 1. Instantiate Enhanced Direct Tool Agent
32
- try:
33
- agent = SmolagentsGAIAgent()
34
- print("Enhanced Direct Tool Agent initialized successfully!")
35
- except Exception as e:
36
- print(f"Error instantiating enhanced direct tool agent: {e}")
37
- return f"Error initializing enhanced direct tool agent: {e}", None
38
-
39
- agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
40
- print(f"Agent code link: {agent_code}")
41
-
42
- # 2. Fetch Questions
43
- print(f"Fetching questions from: {questions_url}")
44
  try:
45
- response = requests.get(questions_url, timeout=15)
46
- response.raise_for_status()
47
- questions_data = response.json()
48
- if not questions_data:
49
- print("Fetched questions list is empty.")
50
- return "Fetched questions list is empty or invalid format.", None
51
- print(f"Fetched {len(questions_data)} questions.")
52
- except requests.exceptions.RequestException as e:
53
- print(f"Error fetching questions: {e}")
54
- return f"Error fetching questions: {e}", None
55
- except requests.exceptions.JSONDecodeError as e:
56
- print(f"Error decoding JSON response from questions endpoint: {e}")
57
- print(f"Response text: {response.text[:500]}")
58
- return f"Error decoding server response for questions: {e}", None
59
  except Exception as e:
60
- print(f"An unexpected error occurred fetching questions: {e}")
61
- return f"An unexpected error occurred fetching questions: {e}", None
62
-
63
- # 3. Run Enhanced DirectToolAgent
64
- results_log = []
65
- answers_payload = []
66
- print(f"Running enhanced direct tool agent on {len(questions_data)} questions...")
67
-
68
- for i, item in enumerate(questions_data):
69
- task_id = item.get("task_id")
70
- question_text = item.get("question")
71
-
72
- if not task_id or question_text is None:
73
- print(f"Skipping item with missing task_id or question: {item}")
74
- continue
75
-
76
- try:
77
- print(f"\n--- Processing Question {i+1}/{len(questions_data)} ---")
78
- print(f"Task ID: {task_id}")
79
- print(f"Question: {question_text[:100]}...")
80
-
81
- # Run the enhanced smolagents agent
82
- submitted_answer = agent.process_question(question_text)
83
-
84
- answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
85
- results_log.append({
86
- "Task ID": task_id,
87
- "Question": question_text,
88
- "Submitted Answer": submitted_answer
89
- })
90
-
91
- print(f"Answer: {submitted_answer[:100]}...")
92
-
93
- except Exception as e:
94
- print(f"Error running agent on task {task_id}: {e}")
95
- error_answer = f"AGENT ERROR: {str(e)[:100]}"
96
- answers_payload.append({"task_id": task_id, "submitted_answer": error_answer})
97
- results_log.append({
98
- "Task ID": task_id,
99
- "Question": question_text,
100
- "Submitted Answer": error_answer
101
- })
102
-
103
- if not answers_payload:
104
- print("Enhanced DirectToolAgent did not produce any answers to submit.")
105
- return "Enhanced DirectToolAgent did not produce any answers to submit.", pd.DataFrame(results_log)
106
-
107
- # 4. Prepare Submission
108
- submission_data = {
109
- "username": username.strip(),
110
- "agent_code": agent_code,
111
- "answers": answers_payload
112
- }
113
-
114
- status_update = f"Enhanced DirectToolAgent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
115
- print(status_update)
116
-
117
- # 5. Submit
118
- print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
119
- try:
120
- response = requests.post(submit_url, json=submission_data, timeout=60)
121
- response.raise_for_status()
122
- result_data = response.json()
123
-
124
- final_status = (
125
- f"Submission Successful!\n"
126
- f"User: {result_data.get('username')}\n"
127
- f"Overall Score: {result_data.get('score', 'N/A')}% "
128
- f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
129
- f"Message: {result_data.get('message', 'No message received.')}"
130
- )
131
-
132
- print("Submission successful!")
133
- results_df = pd.DataFrame(results_log)
134
- return final_status, results_df
135
-
136
- except requests.exceptions.HTTPError as e:
137
- error_detail = f"Server responded with status {e.response.status_code}."
138
- try:
139
- error_json = e.response.json()
140
- error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
141
- except requests.exceptions.JSONDecodeError:
142
- error_detail += f" Response: {e.response.text[:500]}"
143
-
144
- status_message = f"Submission Failed: {error_detail}"
145
- print(status_message)
146
- results_df = pd.DataFrame(results_log)
147
- return status_message, results_df
148
-
149
- except requests.exceptions.Timeout:
150
- status_message = "Submission Failed: The request timed out."
151
- print(status_message)
152
- results_df = pd.DataFrame(results_log)
153
- return status_message, results_df
154
-
155
- except requests.exceptions.RequestException as e:
156
- status_message = f"Submission Failed: Network error - {e}"
157
- print(status_message)
158
- results_df = pd.DataFrame(results_log)
159
- return status_message, results_df
160
-
161
- except Exception as e:
162
- status_message = f"An unexpected error occurred during submission: {e}"
163
- print(status_message)
164
- results_df = pd.DataFrame(results_log)
165
- return status_message, results_df
166
-
167
-
168
- # --- Build Gradio Interface using Blocks ---
169
- with gr.Blocks(title="Enhanced Smolagents GAIA Agent") as demo:
170
- gr.Markdown("# Enhanced Smolagents GAIA Agent Evaluation")
171
- gr.Markdown(
172
- """
173
- ## Instructions:
174
- 1. **Login** to Hugging Face using the button below
175
- 2. **Click** 'Run Enhanced Evaluation & Submit All Answers'
176
- 3. **Wait** for your enhanced smolagents agent to process all questions
177
- 4. **View** your score and detailed results
178
-
179
- ## How the Enhanced Agent Works:
180
- - **Smart Capability Detection**: Automatically detects available tools
181
- - **Fallback Strategy**: Uses basic responses when advanced features unavailable
182
- - **Enhanced Responses**: Leverages Wikipedia, web search, and data tools when possible
183
- - **Zero-Error Operation**: Guaranteed to complete all questions regardless of available features
184
-
185
- **Note**: This enhanced version maximizes capabilities while maintaining complete reliability.
186
- """
187
- )
188
-
189
- gr.LoginButton()
190
-
191
- run_button = gr.Button("Run Enhanced Smolagents GAIA Evaluation & Submit All Answers", variant="primary")
192
-
193
- status_output = gr.Textbox(
194
- label="📊 Run Status / Submission Result",
195
- lines=10,
196
- interactive=False
197
- )
198
-
199
- results_table = gr.DataFrame(
200
- label="📋 Questions and Agent Answers",
201
- wrap=True
202
- )
203
-
204
- run_button.click(
205
- fn=run_and_submit_all,
206
- outputs=[status_output, results_table]
207
- )
208
-
209
- if __name__ == "__app__":
210
- print("\n" + "="*60 + " Enhanced Smolagents GAIA Agent Starting " + "="*60)
211
-
212
- # Check environment variables
213
- space_host_startup = os.getenv("SPACE_HOST")
214
- space_id_startup = os.getenv("SPACE_ID")
215
 
216
- if space_host_startup:
217
- print(f"[OK] SPACE_HOST found: {space_host_startup}")
218
- print(f" Runtime URL: https://{space_host_startup}.hf.space")
219
- else:
220
- print("[INFO] SPACE_HOST not found (running locally)")
221
 
222
- if space_id_startup:
223
- print(f"[OK] SPACE_ID found: {space_id_startup}")
224
- print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
225
- print(f" Code URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
226
- else:
227
- print("[INFO] SPACE_ID not found (running locally)")
 
228
 
229
- print("="*(120 + len(" Enhanced Smolagents GAIA Agent Starting ")) + "\n")
230
 
231
- print("Launching Enhanced Smolagents GAIA Agent Evaluation Interface...")
232
- demo.launch(debug=True, share=False)
 
1
+ """
2
+ Simple Gradio demo to interact with the baseline agent locally.
3
+
4
+ Run with:
5
+ ```powershell
6
+ python app.py
7
+ ```
8
+ Then open the printed local URL or the Gradio link.
9
+ """
10
  import gradio as gr
11
+ from code_agent import run_agent
 
 
 
12
 
 
13
 
14
+ def respond(prompt: str):
15
+ # Run the real smolagents CodeAgent (or raise if not available)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  try:
17
+ ans = run_agent(prompt)
18
+ return ans
 
 
 
 
 
 
 
 
 
 
 
 
19
  except Exception as e:
20
+ return f"(agent error) {e}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
 
 
 
 
 
22
 
23
+ demo = gr.Interface(
24
+ fn=respond,
25
+ inputs=gr.Textbox(lines=5, label="User Prompt"),
26
+ outputs=gr.Textbox(lines=10, label="Agent Response"),
27
+ title="Agents Course — Final Agent Demo",
28
+ description="smolagents CodeAgent demo for the course final project.",
29
+ )
30
 
 
31
 
32
+ if __name__ == "__main__":
33
+ demo.launch()