Freddolin commited on
Commit
fb741d6
·
verified ·
1 Parent(s): e09e2df

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -149
app.py CHANGED
@@ -1,52 +1,17 @@
1
  import os
2
  import gradio as gr
3
  import requests
4
- import inspect
5
  import pandas as pd
6
- from agent.agent import build_graph
7
- from langchain_core.messages import HumanMessage
8
 
9
- # (Keep Constants as is)
10
- # --- Constants ---
11
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
12
 
13
- # --- Basic Agent Definition ---
14
- # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
15
-
16
- # --- GAIA Agent Definition ---
17
- class GAIAAgent:
18
- def __init__(self):
19
- print("Initializing GAIAAgent using build_graph()...")
20
- self.graph = build_graph()
21
-
22
- def __call__(self, question: str) -> str:
23
- print(f"Received question: {question[:60]}...")
24
- try:
25
- messages = [HumanMessage(content=question)]
26
- result = self.graph.invoke({"messages": messages})
27
- raw_output = result["messages"][-1].content
28
- print(f"Raw output: {raw_output}")
29
-
30
- # Extract FINAL ANSWER
31
- if "FINAL ANSWER:" in raw_output:
32
- return raw_output.split("FINAL ANSWER:")[-1].strip()
33
- else:
34
- print("⚠️ 'FINAL ANSWER:' not found — returning full output")
35
- return raw_output.strip()
36
- except Exception as e:
37
- print(f"Agent error: {e}")
38
- return f"AGENT ERROR: {e}"
39
-
40
- def run_and_submit_all( profile: gr.OAuthProfile | None):
41
- """
42
- Fetches all questions, runs the BasicAgent on them, submits all answers,
43
- and displays the results.
44
- """
45
- # --- Determine HF Space Runtime URL and Repo URL ---
46
- space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
47
 
48
  if profile:
49
- username= f"{profile.username}"
50
  print(f"User logged in: {username}")
51
  else:
52
  print("User not logged in.")
@@ -56,71 +21,58 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
56
  questions_url = f"{api_url}/questions"
57
  submit_url = f"{api_url}/submit"
58
 
59
- # 1. Instantiate Agent ( modify this part to create your agent)
60
  try:
61
- agent = GAIAAgent()
62
  except Exception as e:
63
  return f"Error initializing agent: {e}", None
64
 
65
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
66
- print(f"Agent code URL: {agent_code}")
67
-
68
 
69
- # 2. Fetch Questions
70
- print(f"Fetching questions from: {questions_url}")
71
  try:
72
  response = requests.get(questions_url, timeout=15)
73
  response.raise_for_status()
74
  questions_data = response.json()
75
- if not questions_data:
76
- print("Fetched questions list is empty.")
77
- return "Fetched questions list is empty or invalid format.", None
78
- print(f"Fetched {len(questions_data)} questions.")
79
- except requests.exceptions.RequestException as e:
80
- print(f"Error fetching questions: {e}")
81
- return f"Error fetching questions: {e}", None
82
- except requests.exceptions.JSONDecodeError as e:
83
- print(f"Error decoding JSON response from questions endpoint: {e}")
84
- print(f"Response text: {response.text[:500]}")
85
- return f"Error decoding server response for questions: {e}", None
86
  except Exception as e:
87
- print(f"An unexpected error occurred fetching questions: {e}")
88
- return f"An unexpected error occurred fetching questions: {e}", None
89
 
90
- # 3. Run your Agent
91
  results_log = []
92
  answers_payload = []
93
- print(f"Running agent on {len(questions_data)} questions...")
 
94
  for item in questions_data:
95
  task_id = item.get("task_id")
96
  question_text = item.get("question")
97
  if not task_id or question_text is None:
98
- print(f"Skipping item with missing task_id or question: {item}")
99
  continue
100
  try:
101
- submitted_answer = agent(question_text)
102
- answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
103
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
 
 
 
 
 
 
 
 
 
 
 
 
 
104
  except Exception as e:
105
- print(f"Error running agent on task {task_id}: {e}")
106
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
107
 
108
- # Save answers to file
109
- save_answers_to_jsonl(answers_payload, filename="gaia_submission.jsonl")
110
-
111
  if not answers_payload:
112
- print("Agent did not produce any answers to submit.")
113
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
114
 
 
 
 
 
 
115
 
116
-
117
- # 4. Prepare Submission
118
- submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
119
- status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
120
- print(status_update)
121
-
122
- # 5. Submit
123
- print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
124
  try:
125
  response = requests.post(submit_url, json=submission_data, timeout=60)
126
  response.raise_for_status()
@@ -132,88 +84,30 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
132
  f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
133
  f"Message: {result_data.get('message', 'No message received.')}"
134
  )
135
- print("Submission successful.")
136
  results_df = pd.DataFrame(results_log)
137
  return final_status, results_df
138
- except requests.exceptions.HTTPError as e:
139
- error_detail = f"Server responded with status {e.response.status_code}."
140
- try:
141
- error_json = e.response.json()
142
- error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
143
- except requests.exceptions.JSONDecodeError:
144
- error_detail += f" Response: {e.response.text[:500]}"
145
- status_message = f"Submission Failed: {error_detail}"
146
- print(status_message)
147
- results_df = pd.DataFrame(results_log)
148
- return status_message, results_df
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
- except requests.exceptions.RequestException as e:
155
- status_message = f"Submission Failed: Network error - {e}"
156
- print(status_message)
157
- results_df = pd.DataFrame(results_log)
158
- return status_message, results_df
159
  except Exception as e:
160
- status_message = f"An unexpected error occurred during submission: {e}"
161
- print(status_message)
162
- results_df = pd.DataFrame(results_log)
163
- return status_message, results_df
164
-
165
- def save_answers_to_jsonl(answers_payload, filename="gaia_submission.jsonl"):
166
- """
167
- Save the agent's answers to a .jsonl file for manual submission.
168
- """
169
- try:
170
- with open(filename, "w", encoding="utf-8") as f:
171
- for answer in answers_payload:
172
- f.write(json.dumps({
173
- "task_id": answer["task_id"],
174
- "model_answer": answer["submitted_answer"]
175
- }) + "\n")
176
- print(f" Saved {len(answers_payload)} answers to {filename}")
177
- except Exception as e:
178
- print(f" Error saving answers to file: {e}")
179
 
180
 
181
- # --- Build Gradio Interface using Blocks ---
182
  with gr.Blocks() as demo:
183
- gr.Markdown("# Basic Agent Evaluation Runner")
184
- gr.Markdown(
185
- """
186
- **Instructions:**
187
-
188
- 1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
189
- 2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
190
- 3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
191
-
192
- ---
193
- **Disclaimers:**
194
- 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).
195
- 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.
196
- """
197
- )
198
-
199
  gr.LoginButton()
200
 
201
  run_button = gr.Button("Run Evaluation & Submit All Answers")
 
 
202
 
203
- status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
204
- # Removed max_rows=10 from DataFrame constructor
205
- results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
206
-
207
- run_button.click(
208
- fn=run_and_submit_all,
209
- outputs=[status_output, results_table]
210
- )
211
 
212
  if __name__ == "__main__":
213
  print("\n" + "-"*30 + " App Starting " + "-"*30)
214
- # Check for SPACE_HOST and SPACE_ID at startup for information
215
  space_host_startup = os.getenv("SPACE_HOST")
216
- space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
217
 
218
  if space_host_startup:
219
  print(f"✅ SPACE_HOST found: {space_host_startup}")
@@ -221,7 +115,7 @@ if __name__ == "__main__":
221
  else:
222
  print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
223
 
224
- if space_id_startup: # Print repo URLs if SPACE_ID is found
225
  print(f"✅ SPACE_ID found: {space_id_startup}")
226
  print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
227
  print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
@@ -231,4 +125,5 @@ if __name__ == "__main__":
231
  print("-"*(60 + len(" App Starting ")) + "\n")
232
 
233
  print("Launching Gradio Interface for Basic Agent Evaluation...")
234
- demo.launch(debug=True, share=False)
 
 
1
  import os
2
  import gradio as gr
3
  import requests
 
4
  import pandas as pd
5
+ from agent import GaiaAgent
 
6
 
 
 
7
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
8
 
9
+ def run_and_submit_all(profile: gr.OAuthProfile | None):
10
+ space_id = os.getenv("SPACE_ID")
11
+ api_key = os.getenv("OPENAI_API_KEY")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
  if profile:
14
+ username = f"{profile.username}"
15
  print(f"User logged in: {username}")
16
  else:
17
  print("User not logged in.")
 
21
  questions_url = f"{api_url}/questions"
22
  submit_url = f"{api_url}/submit"
23
 
 
24
  try:
25
+ agent = GaiaAgent(api_key)
26
  except Exception as e:
27
  return f"Error initializing agent: {e}", None
28
 
29
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
 
 
30
 
 
 
31
  try:
32
  response = requests.get(questions_url, timeout=15)
33
  response.raise_for_status()
34
  questions_data = response.json()
 
 
 
 
 
 
 
 
 
 
 
35
  except Exception as e:
36
+ return f"Error fetching questions: {e}", None
 
37
 
 
38
  results_log = []
39
  answers_payload = []
40
+
41
+ print("\n--- STARTING AGENT RUN ---")
42
  for item in questions_data:
43
  task_id = item.get("task_id")
44
  question_text = item.get("question")
45
  if not task_id or question_text is None:
 
46
  continue
47
  try:
48
+ final_answer, trace = agent(question_text)
49
+
50
+ print("\n--- QUESTION ---")
51
+ print(f"Task ID: {task_id}")
52
+ print(f"Question: {question_text}")
53
+ print("\n--- REASONING TRACE ---")
54
+ print(trace)
55
+ print("\n--- FINAL ANSWER (SUBMITTED) ---")
56
+ print(final_answer)
57
+
58
+ answers_payload.append({
59
+ "task_id": task_id,
60
+ "submitted_answer": final_answer,
61
+ "reasoning_trace": trace
62
+ })
63
+ results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": final_answer})
64
  except Exception as e:
65
+ results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"ERROR: {e}"})
 
66
 
 
 
 
67
  if not answers_payload:
 
68
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
69
 
70
+ submission_data = {
71
+ "username": username.strip(),
72
+ "agent_code": agent_code,
73
+ "answers": answers_payload
74
+ }
75
 
 
 
 
 
 
 
 
 
76
  try:
77
  response = requests.post(submit_url, json=submission_data, timeout=60)
78
  response.raise_for_status()
 
84
  f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
85
  f"Message: {result_data.get('message', 'No message received.')}"
86
  )
 
87
  results_df = pd.DataFrame(results_log)
88
  return final_status, results_df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
  except Exception as e:
90
+ return f"Submission Failed: {e}", pd.DataFrame(results_log)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
 
92
 
 
93
  with gr.Blocks() as demo:
94
+ gr.Markdown("# GAIA Agent Submission Interface")
95
+ gr.Markdown("""
96
+ Logga in och kör agenten.\n
97
+ Du behöver ha din OpenAI API-nyckel i miljövariabeln `OPENAI_API_KEY`.
98
+ """)
 
 
 
 
 
 
 
 
 
 
 
99
  gr.LoginButton()
100
 
101
  run_button = gr.Button("Run Evaluation & Submit All Answers")
102
+ status_output = gr.Textbox(label="Submission Result")
103
+ results_table = gr.DataFrame(label="Answers")
104
 
105
+ run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
 
 
 
 
 
 
 
106
 
107
  if __name__ == "__main__":
108
  print("\n" + "-"*30 + " App Starting " + "-"*30)
 
109
  space_host_startup = os.getenv("SPACE_HOST")
110
+ space_id_startup = os.getenv("SPACE_ID")
111
 
112
  if space_host_startup:
113
  print(f"✅ SPACE_HOST found: {space_host_startup}")
 
115
  else:
116
  print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
117
 
118
+ if space_id_startup:
119
  print(f"✅ SPACE_ID found: {space_id_startup}")
120
  print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
121
  print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
 
125
  print("-"*(60 + len(" App Starting ")) + "\n")
126
 
127
  print("Launching Gradio Interface for Basic Agent Evaluation...")
128
+ demo.launch(debug=True, share=False)
129
+