Frazer2810 commited on
Commit
e9f897a
·
verified ·
1 Parent(s): 71f9f04

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -65
app.py CHANGED
@@ -1,64 +1,65 @@
1
  import os
2
  import gradio as gr
3
  import requests
 
4
  import pandas as pd
5
- from agent import BasicAgent # ← importa il vero agente smolagents
6
 
7
- # --- Constants -------------------------------------------------
8
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
9
 
10
- # ---------------------------------------------------------------
11
  def run_and_submit_all(profile: gr.OAuthProfile | None):
12
  """
13
- Fetches all questions, runs the BasicAgent on them, submits all answers,
14
  and displays the results.
15
  """
16
- # --- Determine HF Space Runtime URL and Repo URL ------------
17
- space_id = os.getenv("SPACE_ID") # for the agent_code link
18
 
19
  if profile:
20
- username = f"{profile.username}"
21
  print(f"User logged in: {username}")
22
  else:
23
  print("User not logged in.")
24
- return "Please login to Hugging Face with the button.", None
25
 
26
  api_url = DEFAULT_API_URL
27
  questions_url = f"{api_url}/questions"
28
  submit_url = f"{api_url}/submit"
29
 
30
- # 1. Instantiate the agent -----------------------------------
31
  try:
32
- agent = BasicAgent()
33
  except Exception as e:
34
  print(f"Error instantiating agent: {e}")
35
  return f"Error initializing agent: {e}", None
36
-
37
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
38
  print(agent_code)
39
 
40
- # 2. Fetch questions -----------------------------------------
41
  print(f"Fetching questions from: {questions_url}")
42
  try:
43
  response = requests.get(questions_url, timeout=15)
44
  response.raise_for_status()
45
  questions_data = response.json()
46
  if not questions_data:
47
- print("Fetched questions list is empty.")
48
- return "Fetched questions list is empty or invalid format.", None
49
  print(f"Fetched {len(questions_data)} questions.")
50
  except requests.exceptions.RequestException as e:
51
  print(f"Error fetching questions: {e}")
52
  return f"Error fetching questions: {e}", None
53
  except requests.exceptions.JSONDecodeError as e:
54
- print(f"Error decoding JSON response from questions endpoint: {e}")
55
- print(f"Response text: {response.text[:500]}")
56
- return f"Error decoding server response for questions: {e}", None
57
  except Exception as e:
58
  print(f"An unexpected error occurred fetching questions: {e}")
59
  return f"An unexpected error occurred fetching questions: {e}", None
60
 
61
- # 3. Run the agent -------------------------------------------
62
  results_log = []
63
  answers_payload = []
64
  print(f"Running agent on {len(questions_data)} questions...")
@@ -70,42 +71,22 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
70
  continue
71
  try:
72
  submitted_answer = agent(question_text)
73
- answers_payload.append(
74
- {"task_id": task_id, "submitted_answer": submitted_answer}
75
- )
76
- results_log.append(
77
- {
78
- "Task ID": task_id,
79
- "Question": question_text,
80
- "Submitted Answer": submitted_answer,
81
- }
82
- )
83
  except Exception as e:
84
- print(f"Error running agent on task {task_id}: {e}")
85
- results_log.append(
86
- {
87
- "Task ID": task_id,
88
- "Question": question_text,
89
- "Submitted Answer": f"AGENT ERROR: {e}",
90
- }
91
- )
92
 
93
  if not answers_payload:
94
  print("Agent did not produce any answers to submit.")
95
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
96
 
97
- # 4. Prepare submission --------------------------------------
98
- submission_data = {
99
- "username": username.strip(),
100
- "agent_code": agent_code,
101
- "answers": answers_payload,
102
- }
103
- status_update = (
104
- f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
105
- )
106
  print(status_update)
107
 
108
- # 5. Submit ---------------------------------------------------
109
  print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
110
  try:
111
  response = requests.post(submit_url, json=submission_data, timeout=60)
@@ -149,21 +130,21 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
149
  return status_message, results_df
150
 
151
 
152
- # --- Build Gradio Interface using Blocks -----------------------
153
  with gr.Blocks() as demo:
154
- gr.Markdown("# Basic Agent Evaluation Runner")
155
  gr.Markdown(
156
  """
157
  **Instructions:**
158
 
159
- 1. Clone this space, then modify the code to define your agent logic, tools, etc.
160
- 2. Log in to your Hugging Face account with the button below. Your HF username is used for submission.
161
- 3. Click 'Run Evaluation & Submit All Answers' to fetch the questions, run your agent, submit answers, and see your score.
162
 
163
  ---
164
  **Disclaimers:**
165
- Running can take some time because the agent must answer all questions.
166
- This setup is intentionally minimal; feel free to optimise (e.g. async, caching).
167
  """
168
  )
169
 
@@ -171,32 +152,34 @@ with gr.Blocks() as demo:
171
 
172
  run_button = gr.Button("Run Evaluation & Submit All Answers")
173
 
174
- status_output = gr.Textbox(
175
- label="Run Status / Submission Result", lines=5, interactive=False
176
- )
177
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
178
 
179
- run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
 
 
 
180
 
181
- # --- Launch -----------------------------------------------------
182
  if __name__ == "__main__":
183
- print("\n" + "-" * 30 + " App Starting " + "-" * 30)
 
184
  space_host_startup = os.getenv("SPACE_HOST")
185
- space_id_startup = os.getenv("SPACE_ID")
186
 
187
  if space_host_startup:
188
  print(f"✅ SPACE_HOST found: {space_host_startup}")
189
  print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
190
  else:
191
- print("ℹ️ SPACE_HOST env variable not found (running locally?).")
192
 
193
- if space_id_startup:
194
  print(f"✅ SPACE_ID found: {space_id_startup}")
195
  print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
196
  print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
197
  else:
198
- print("ℹ️ SPACE_ID env variable not found (running locally?).")
 
 
199
 
200
- print("-" * (60 + len(" App Starting ")) + "\n")
201
- print("Launching Gradio Interface for Basic Agent Evaluation...")
202
  demo.launch(debug=True, share=False)
 
1
  import os
2
  import gradio as gr
3
  import requests
4
+ import inspect
5
  import pandas as pd
6
+ from agent import GaiaAgent
7
 
8
+ # --- Constants ---
9
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
10
 
11
+ # --- Build Agent ---
12
  def run_and_submit_all(profile: gr.OAuthProfile | None):
13
  """
14
+ Fetches all questions, runs the GaiaAgent 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") # Get the SPACE_ID for sending link to the code
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 Agent
32
  try:
33
+ agent = GaiaAgent()
34
  except Exception as e:
35
  print(f"Error instantiating agent: {e}")
36
  return f"Error initializing agent: {e}", None
37
+ # In the case of an app running as a hugging Face space, this link points toward your codebase
38
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
39
  print(agent_code)
40
 
41
+ # 2. Fetch Questions
42
  print(f"Fetching questions from: {questions_url}")
43
  try:
44
  response = requests.get(questions_url, timeout=15)
45
  response.raise_for_status()
46
  questions_data = response.json()
47
  if not questions_data:
48
+ print("Fetched questions list is empty.")
49
+ return "Fetched questions list is empty or invalid format.", None
50
  print(f"Fetched {len(questions_data)} questions.")
51
  except requests.exceptions.RequestException as e:
52
  print(f"Error fetching questions: {e}")
53
  return f"Error fetching questions: {e}", None
54
  except requests.exceptions.JSONDecodeError as e:
55
+ print(f"Error decoding JSON response from questions endpoint: {e}")
56
+ print(f"Response text: {response.text[:500]}")
57
+ return f"Error decoding server response for questions: {e}", None
58
  except Exception as e:
59
  print(f"An unexpected error occurred fetching questions: {e}")
60
  return f"An unexpected error occurred fetching questions: {e}", None
61
 
62
+ # 3. Run your Agent
63
  results_log = []
64
  answers_payload = []
65
  print(f"Running agent on {len(questions_data)} questions...")
 
71
  continue
72
  try:
73
  submitted_answer = agent(question_text)
74
+ answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
75
+ results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
 
 
 
 
 
 
 
 
76
  except Exception as e:
77
+ print(f"Error running agent on task {task_id}: {e}")
78
+ results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
 
 
 
 
 
 
79
 
80
  if not answers_payload:
81
  print("Agent did not produce any answers to submit.")
82
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
83
 
84
+ # 4. Prepare Submission
85
+ submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
86
+ status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
 
 
 
 
 
 
87
  print(status_update)
88
 
89
+ # 5. Submit
90
  print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
91
  try:
92
  response = requests.post(submit_url, json=submission_data, timeout=60)
 
130
  return status_message, results_df
131
 
132
 
133
+ # --- Build Gradio Interface using Blocks ---
134
  with gr.Blocks() as demo:
135
+ gr.Markdown("# GAIA Test Level 1 Agent Evaluation")
136
  gr.Markdown(
137
  """
138
  **Instructions:**
139
 
140
+ 1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
141
+ 2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
142
+ 3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
143
 
144
  ---
145
  **Disclaimers:**
146
+ Once clicking on the "submit button, it can take quite some time ( this is the time for the agent to go through all the questions).
147
+ This space provides a basic setup and is intentionally sub-optimal to encourage you to develop your own, more robust solution. For instance for the delay process of the submit button, a solution could be to cache the answers and submit in a seperate action or even to answer the questions in async.
148
  """
149
  )
150
 
 
152
 
153
  run_button = gr.Button("Run Evaluation & Submit All Answers")
154
 
155
+ status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
 
 
156
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
157
 
158
+ run_button.click(
159
+ fn=run_and_submit_all,
160
+ outputs=[status_output, results_table]
161
+ )
162
 
 
163
  if __name__ == "__main__":
164
+ print("\n" + "-"*30 + " App Starting " + "-"*30)
165
+ # Check for SPACE_HOST and SPACE_ID at startup for information
166
  space_host_startup = os.getenv("SPACE_HOST")
167
+ space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
168
 
169
  if space_host_startup:
170
  print(f"✅ SPACE_HOST found: {space_host_startup}")
171
  print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
172
  else:
173
+ print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
174
 
175
+ if space_id_startup: # Print repo URLs if SPACE_ID is found
176
  print(f"✅ SPACE_ID found: {space_id_startup}")
177
  print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
178
  print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
179
  else:
180
+ print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
181
+
182
+ print("-"*(60 + len(" App Starting ")) + "\n")
183
 
184
+ print("Launching Gradio Interface for GAIA Test Level 1 Agent...")
 
185
  demo.launch(debug=True, share=False)