shubham7080 commited on
Commit
fee8740
·
verified ·
1 Parent(s): 81917a3

Update app.py

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