DrekFretson commited on
Commit
67e704c
·
verified ·
1 Parent(s): 083cf21

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +100 -56
app.py CHANGED
@@ -1,47 +1,40 @@
1
- """ Basic Agent Evaluation Runner"""
2
- import os
3
  import inspect
 
 
 
 
4
  import gradio as gr
5
- import requests
6
  import pandas as pd
7
- from langchain_core.messages import HumanMessage
8
- from agent import build_graph
9
-
10
 
 
 
 
11
 
12
  # (Keep Constants as is)
13
  # --- Constants ---
14
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
15
 
 
16
  # --- Basic Agent Definition ---
17
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
18
 
19
 
20
- class BasicAgent:
21
- """A langgraph agent."""
22
- def __init__(self):
23
- print("BasicAgent initialized.")
24
- self.graph = build_graph()
25
-
26
- def __call__(self, question: str) -> str:
27
- print(f"Agent received question (first 50 chars): {question[:50]}...")
28
- # Wrap the question in a HumanMessage from langchain_core
29
- messages = [HumanMessage(content=question)]
30
- messages = self.graph.invoke({"messages": messages})
31
- answer = messages['messages'][-1].content
32
- return answer[14:]
33
-
34
-
35
- def run_and_submit_all( profile: gr.OAuthProfile | None):
36
  """
37
- Fetches all questions, runs the BasicAgent on them, submits all answers,
38
- and displays the results.
 
 
 
39
  """
40
  # --- Determine HF Space Runtime URL and Repo URL ---
41
  space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
42
 
43
  if profile:
44
- username= f"{profile.username}"
45
  print(f"User logged in: {username}")
46
  else:
47
  print("User not logged in.")
@@ -50,14 +43,17 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
50
  api_url = DEFAULT_API_URL
51
  questions_url = f"{api_url}/questions"
52
  submit_url = f"{api_url}/submit"
 
53
 
54
  # 1. Instantiate Agent ( modify this part to create your agent)
55
  try:
56
- agent = BasicAgent()
 
 
57
  except Exception as e:
58
  print(f"Error instantiating agent: {e}")
59
  return f"Error initializing agent: {e}", None
60
- # In the case of an app running as a hugging Face space, this link points toward your codebase ( usefull for others so please keep it public)
61
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
62
  print(agent_code)
63
 
@@ -68,16 +64,16 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
68
  response.raise_for_status()
69
  questions_data = response.json()
70
  if not questions_data:
71
- print("Fetched questions list is empty.")
72
- return "Fetched questions list is empty or invalid format.", None
73
  print(f"Fetched {len(questions_data)} questions.")
74
  except requests.exceptions.RequestException as e:
75
  print(f"Error fetching questions: {e}")
76
  return f"Error fetching questions: {e}", None
77
  except requests.exceptions.JSONDecodeError as e:
78
- print(f"Error decoding JSON response from questions endpoint: {e}")
79
- print(f"Response text: {response.text[:500]}")
80
- return f"Error decoding server response for questions: {e}", None
81
  except Exception as e:
82
  print(f"An unexpected error occurred fetching questions: {e}")
83
  return f"An unexpected error occurred fetching questions: {e}", None
@@ -93,19 +89,67 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
93
  print(f"Skipping item with missing task_id or question: {item}")
94
  continue
95
  try:
96
- submitted_answer = agent(question_text)
97
- answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
98
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
  except Exception as e:
100
- print(f"Error running agent on task {task_id}: {e}")
101
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
 
 
 
 
 
 
102
 
103
  if not answers_payload:
104
  print("Agent did not produce any answers to submit.")
105
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
106
 
107
- # 4. Prepare Submission
108
- submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
 
 
 
 
109
  status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
110
  print(status_update)
111
 
@@ -159,13 +203,11 @@ with gr.Blocks() as demo:
159
  gr.Markdown(
160
  """
161
  **Instructions:**
162
- 1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
163
- 2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
164
- 3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
165
  ---
166
  **Disclaimers:**
167
- 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).
168
- 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.
169
  """
170
  )
171
 
@@ -173,20 +215,18 @@ with gr.Blocks() as demo:
173
 
174
  run_button = gr.Button("Run Evaluation & Submit All Answers")
175
 
176
- status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
177
- # Removed max_rows=10 from DataFrame constructor
 
178
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
179
 
180
- run_button.click(
181
- fn=run_and_submit_all,
182
- outputs=[status_output, results_table]
183
- )
184
 
185
  if __name__ == "__main__":
186
- print("\n" + "-"*30 + " App Starting " + "-"*30)
187
  # Check for SPACE_HOST and SPACE_ID at startup for information
188
  space_host_startup = os.getenv("SPACE_HOST")
189
- space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
190
 
191
  if space_host_startup:
192
  print(f"✅ SPACE_HOST found: {space_host_startup}")
@@ -194,14 +234,18 @@ if __name__ == "__main__":
194
  else:
195
  print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
196
 
197
- if space_id_startup: # Print repo URLs if SPACE_ID is found
198
  print(f"✅ SPACE_ID found: {space_id_startup}")
199
  print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
200
- print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
 
 
201
  else:
202
- print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
 
 
203
 
204
- print("-"*(60 + len(" App Starting ")) + "\n")
205
 
206
  print("Launching Gradio Interface for Basic Agent Evaluation...")
207
  demo.launch(debug=True, share=False)
 
 
 
1
  import inspect
2
+ import os
3
+ import tempfile
4
+ from typing import Any, Optional, Tuple
5
+
6
  import gradio as gr
 
7
  import pandas as pd
8
+ import requests
 
 
9
 
10
+ from agent import Agent
11
+ from model import get_model
12
+ from tools.tools import get_tools
13
 
14
  # (Keep Constants as is)
15
  # --- Constants ---
16
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
17
 
18
+
19
  # --- Basic Agent Definition ---
20
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
21
 
22
 
23
+ def run_and_submit_all(
24
+ profile: Optional[gr.OAuthProfile],
25
+ ) -> Tuple[str, Optional[pd.DataFrame]]:
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  """
27
+ Fetches all questions, runs the Agent on them, submits all answers, and displays the results.
28
+ Args:
29
+ profile (Optional[gr.OAuthProfile]): The OAuth profile of the user.
30
+ Returns:
31
+ Tuple[str, Optional[pd.DataFrame]]: Status message and DataFrame of results.
32
  """
33
  # --- Determine HF Space Runtime URL and Repo URL ---
34
  space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
35
 
36
  if profile:
37
+ username = f"{profile.username}"
38
  print(f"User logged in: {username}")
39
  else:
40
  print("User not logged in.")
 
43
  api_url = DEFAULT_API_URL
44
  questions_url = f"{api_url}/questions"
45
  submit_url = f"{api_url}/submit"
46
+ files_url = f"{api_url}/files"
47
 
48
  # 1. Instantiate Agent ( modify this part to create your agent)
49
  try:
50
+ agent = Agent(
51
+ model=get_model("OpenAIServerModel", "gpt-4.1"), tools=get_tools()
52
+ )
53
  except Exception as e:
54
  print(f"Error instantiating agent: {e}")
55
  return f"Error initializing agent: {e}", None
56
+ # In the case of an app running as a hugging Face space, this link points toward your codebase (usefull for others so please keep it public)
57
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
58
  print(agent_code)
59
 
 
64
  response.raise_for_status()
65
  questions_data = response.json()
66
  if not questions_data:
67
+ print("Fetched questions list is empty.")
68
+ return "Fetched questions list is empty or invalid format.", None
69
  print(f"Fetched {len(questions_data)} questions.")
70
  except requests.exceptions.RequestException as e:
71
  print(f"Error fetching questions: {e}")
72
  return f"Error fetching questions: {e}", None
73
  except requests.exceptions.JSONDecodeError as e:
74
+ print(f"Error decoding JSON response from questions endpoint: {e}")
75
+ print(f"Response text: {response.text[:500]}")
76
+ return f"Error decoding server response for questions: {e}", None
77
  except Exception as e:
78
  print(f"An unexpected error occurred fetching questions: {e}")
79
  return f"An unexpected error occurred fetching questions: {e}", None
 
89
  print(f"Skipping item with missing task_id or question: {item}")
90
  continue
91
  try:
92
+ file_path: Optional[str] = None
93
+ try:
94
+ file_response = requests.get(f"{files_url}/{task_id}", timeout=15)
95
+ if file_response.status_code == 200 and file_response.content:
96
+ # Get filename from Content-Disposition header or URL
97
+ filename = None
98
+ content_disposition = file_response.headers.get(
99
+ "Content-Disposition"
100
+ )
101
+ if content_disposition and "filename=" in content_disposition:
102
+ filename = content_disposition.split("filename=")[-1].strip('"')
103
+ else:
104
+ # Try to get filename from URL
105
+ url = file_response.url
106
+ filename = url.split("/")[-1]
107
+ if not filename or filename == str(task_id):
108
+ filename = f"file_{task_id}"
109
+
110
+ # Create temp directory and save file with original name
111
+ temp_dir = tempfile.mkdtemp()
112
+ file_path = os.path.join(temp_dir, filename)
113
+ with open(file_path, "wb") as f:
114
+ f.write(file_response.content)
115
+ print(f"Downloaded file for task {task_id} to {file_path}")
116
+ else:
117
+ print(f"No file for task {task_id} or file is empty.")
118
+ except Exception as e:
119
+ print(f"Error downloading file for task {task_id}: {e}")
120
+ file_path = None
121
+
122
+ submitted_answer = agent(question_text, file_path)
123
+ answers_payload.append(
124
+ {"task_id": task_id, "submitted_answer": submitted_answer}
125
+ )
126
+ results_log.append(
127
+ {
128
+ "Task ID": task_id,
129
+ "Question": question_text,
130
+ "Submitted Answer": submitted_answer,
131
+ }
132
+ )
133
  except Exception as e:
134
+ print(f"Error running agent on task {task_id}: {e}")
135
+ results_log.append(
136
+ {
137
+ "Task ID": task_id,
138
+ "Question": question_text,
139
+ "Submitted Answer": f"AGENT ERROR: {e}",
140
+ }
141
+ )
142
 
143
  if not answers_payload:
144
  print("Agent did not produce any answers to submit.")
145
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
146
 
147
+ # 4. Prepare Submission
148
+ submission_data = {
149
+ "username": username.strip(),
150
+ "agent_code": agent_code,
151
+ "answers": answers_payload,
152
+ }
153
  status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
154
  print(status_update)
155
 
 
203
  gr.Markdown(
204
  """
205
  **Instructions:**
206
+ 1. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
207
+ 2. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
 
208
  ---
209
  **Disclaimers:**
210
+ 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).
 
211
  """
212
  )
213
 
 
215
 
216
  run_button = gr.Button("Run Evaluation & Submit All Answers")
217
 
218
+ status_output = gr.Textbox(
219
+ label="Run Status / Submission Result", lines=5, interactive=False
220
+ )
221
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
222
 
223
+ run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
 
 
 
224
 
225
  if __name__ == "__main__":
226
+ print("\n" + "-" * 30 + " App Starting " + "-" * 30)
227
  # Check for SPACE_HOST and SPACE_ID at startup for information
228
  space_host_startup = os.getenv("SPACE_HOST")
229
+ space_id_startup = os.getenv("SPACE_ID")
230
 
231
  if space_host_startup:
232
  print(f"✅ SPACE_HOST found: {space_host_startup}")
 
234
  else:
235
  print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
236
 
237
+ if space_id_startup: # Print repo URLs if SPACE_ID is found
238
  print(f"✅ SPACE_ID found: {space_id_startup}")
239
  print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
240
+ print(
241
+ f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main"
242
+ )
243
  else:
244
+ print(
245
+ "ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined."
246
+ )
247
 
248
+ print("-" * (60 + len(" App Starting ")) + "\n")
249
 
250
  print("Launching Gradio Interface for Basic Agent Evaluation...")
251
  demo.launch(debug=True, share=False)