RayNene commited on
Commit
c0cdd2e
·
1 Parent(s): 28e25a4

Fix ZeroGPU startup

Browse files
Files changed (2) hide show
  1. app.py +166 -66
  2. requirements.txt +1 -0
app.py CHANGED
@@ -1,36 +1,35 @@
 
1
  import os
2
  import gradio as gr
3
  import requests
4
- import inspect
5
  import pandas as pd
 
6
  from langchain_google_genai import ChatGoogleGenerativeAI
7
 
8
- # (Keep Constants as is)
9
- # --- Constants ---
10
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
11
 
12
- # --- Basic Agent Definition ---
13
- # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
14
  class BasicAgent:
15
  def __init__(self):
16
  print("BasicAgent initialized.")
 
17
  def __call__(self, question: str) -> str:
18
  print(f"Agent received question (first 50 chars): {question[:50]}...")
19
- llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash", temperature=1)
 
 
 
20
  fixed_answer = llm.invoke(question).content
21
  print(f"Agent returning fixed answer: {fixed_answer}")
22
  return fixed_answer
23
 
24
- def run_and_submit_all( profile: gr.OAuthProfile | None):
25
- """
26
- Fetches all questions, runs the BasicAgent on them, submits all answers,
27
- and displays the results.
28
- """
29
- # --- Determine HF Space Runtime URL and Repo URL ---
30
- space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
31
 
32
  if profile:
33
- username= f"{profile.username}"
34
  print(f"User logged in: {username}")
35
  else:
36
  print("User not logged in.")
@@ -40,159 +39,260 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
40
  questions_url = f"{api_url}/questions"
41
  submit_url = f"{api_url}/submit"
42
 
43
- # 1. Instantiate Agent ( modify this part to create your agent)
44
  try:
45
  agent = BasicAgent()
46
  except Exception as e:
47
  print(f"Error instantiating agent: {e}")
48
  return f"Error initializing agent: {e}", None
49
- # 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)
50
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
51
  print(agent_code)
52
 
53
- # 2. Fetch Questions
54
  print(f"Fetching questions from: {questions_url}")
 
55
  try:
56
  response = requests.get(questions_url, timeout=15)
57
  response.raise_for_status()
58
  questions_data = response.json()
 
59
  if not questions_data:
60
- print("Fetched questions list is empty.")
61
- return "Fetched questions list is empty or invalid format.", None
 
62
  print(f"Fetched {len(questions_data)} questions.")
 
63
  except requests.exceptions.RequestException as e:
64
  print(f"Error fetching questions: {e}")
65
  return f"Error fetching questions: {e}", None
 
66
  except requests.exceptions.JSONDecodeError as e:
67
- print(f"Error decoding JSON response from questions endpoint: {e}")
68
- print(f"Response text: {response.text[:500]}")
69
- return f"Error decoding server response for questions: {e}", None
 
70
  except Exception as e:
71
  print(f"An unexpected error occurred fetching questions: {e}")
72
  return f"An unexpected error occurred fetching questions: {e}", None
73
 
74
- # 3. Run your Agent
75
  results_log = []
76
  answers_payload = []
 
77
  print(f"Running agent on {len(questions_data)} questions...")
 
78
  for item in questions_data:
79
  task_id = item.get("task_id")
80
  question_text = item.get("question")
 
81
  if not task_id or question_text is None:
82
  print(f"Skipping item with missing task_id or question: {item}")
83
  continue
 
84
  try:
85
  submitted_answer = agent(question_text)
86
- answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
87
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
 
 
 
 
 
 
 
 
 
 
88
  except Exception as e:
89
- print(f"Error running agent on task {task_id}: {e}")
90
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
 
 
 
 
 
91
 
92
  if not answers_payload:
93
  print("Agent did not produce any answers to submit.")
94
- return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
 
 
 
95
 
96
- # 4. Prepare Submission
97
- submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
98
- status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
99
- print(status_update)
 
100
 
101
- # 5. Submit
 
 
 
 
 
102
  print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
 
103
  try:
104
- response = requests.post(submit_url, json=submission_data, timeout=60)
 
 
 
 
 
105
  response.raise_for_status()
106
  result_data = response.json()
 
107
  final_status = (
108
  f"Submission Successful!\n"
109
  f"User: {result_data.get('username')}\n"
110
  f"Overall Score: {result_data.get('score', 'N/A')}% "
111
- f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
 
112
  f"Message: {result_data.get('message', 'No message received.')}"
113
  )
 
114
  print("Submission successful.")
 
115
  results_df = pd.DataFrame(results_log)
 
116
  return final_status, results_df
 
117
  except requests.exceptions.HTTPError as e:
118
- error_detail = f"Server responded with status {e.response.status_code}."
 
 
 
 
119
  try:
120
  error_json = e.response.json()
121
- error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
 
 
 
 
122
  except requests.exceptions.JSONDecodeError:
123
- error_detail += f" Response: {e.response.text[:500]}"
 
 
 
124
  status_message = f"Submission Failed: {error_detail}"
 
125
  print(status_message)
 
126
  results_df = pd.DataFrame(results_log)
 
127
  return status_message, results_df
 
128
  except requests.exceptions.Timeout:
129
- status_message = "Submission Failed: The request timed out."
 
 
 
130
  print(status_message)
 
131
  results_df = pd.DataFrame(results_log)
 
132
  return status_message, results_df
 
133
  except requests.exceptions.RequestException as e:
134
- status_message = f"Submission Failed: Network error - {e}"
 
 
 
135
  print(status_message)
 
136
  results_df = pd.DataFrame(results_log)
 
137
  return status_message, results_df
 
138
  except Exception as e:
139
- status_message = f"An unexpected error occurred during submission: {e}"
 
 
 
140
  print(status_message)
 
141
  results_df = pd.DataFrame(results_log)
 
142
  return status_message, results_df
143
 
144
 
145
- # --- Build Gradio Interface using Blocks ---
146
  with gr.Blocks() as demo:
147
  gr.Markdown("# Basic Agent Evaluation Runner")
 
148
  gr.Markdown(
149
  """
150
  **Instructions:**
151
 
152
- 1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
153
- 2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
154
- 3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
155
 
156
- ---
157
  **Disclaimers:**
158
- 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).
159
- 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.
160
  """
161
  )
162
 
163
  gr.LoginButton()
164
 
165
- run_button = gr.Button("Run Evaluation & Submit All Answers")
 
 
166
 
167
- status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
168
- # Removed max_rows=10 from DataFrame constructor
169
- results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
 
 
 
 
 
 
 
170
 
171
  run_button.click(
172
  fn=run_and_submit_all,
173
  outputs=[status_output, results_table]
174
  )
175
 
 
176
  if __name__ == "__main__":
177
- print("\n" + "-"*30 + " App Starting " + "-"*30)
178
- # Check for SPACE_HOST and SPACE_ID at startup for information
179
  space_host_startup = os.getenv("SPACE_HOST")
180
- space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
181
 
182
  if space_host_startup:
183
- print(f"✅ SPACE_HOST found: {space_host_startup}")
184
- print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
 
 
185
  else:
186
- print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
 
 
187
 
188
- if space_id_startup: # Print repo URLs if SPACE_ID is found
189
- print(f"✅ SPACE_ID found: {space_id_startup}")
190
- print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
191
- print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
 
 
 
 
 
 
 
192
  else:
193
- print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
194
-
195
- print("-"*(60 + len(" App Starting ")) + "\n")
196
 
 
197
  print("Launching Gradio Interface for Basic Agent Evaluation...")
198
- demo.launch(debug=True, share=False)
 
 
 
 
 
 
1
+
2
  import os
3
  import gradio as gr
4
  import requests
 
5
  import pandas as pd
6
+ import spaces
7
  from langchain_google_genai import ChatGoogleGenerativeAI
8
 
 
 
9
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
10
 
11
+
 
12
  class BasicAgent:
13
  def __init__(self):
14
  print("BasicAgent initialized.")
15
+
16
  def __call__(self, question: str) -> str:
17
  print(f"Agent received question (first 50 chars): {question[:50]}...")
18
+ llm = ChatGoogleGenerativeAI(
19
+ model="gemini-2.0-flash",
20
+ temperature=1
21
+ )
22
  fixed_answer = llm.invoke(question).content
23
  print(f"Agent returning fixed answer: {fixed_answer}")
24
  return fixed_answer
25
 
26
+
27
+ @spaces.GPU(duration=120)
28
+ def run_and_submit_all(profile: gr.OAuthProfile | None):
29
+ space_id = os.getenv("SPACE_ID")
 
 
 
30
 
31
  if profile:
32
+ username = f"{profile.username}"
33
  print(f"User logged in: {username}")
34
  else:
35
  print("User not logged in.")
 
39
  questions_url = f"{api_url}/questions"
40
  submit_url = f"{api_url}/submit"
41
 
 
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
+
48
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
49
  print(agent_code)
50
 
 
51
  print(f"Fetching questions from: {questions_url}")
52
+
53
  try:
54
  response = requests.get(questions_url, timeout=15)
55
  response.raise_for_status()
56
  questions_data = response.json()
57
+
58
  if not questions_data:
59
+ print("Fetched questions list is empty.")
60
+ return "Fetched questions list is empty or invalid format.", None
61
+
62
  print(f"Fetched {len(questions_data)} questions.")
63
+
64
  except requests.exceptions.RequestException as e:
65
  print(f"Error fetching questions: {e}")
66
  return f"Error fetching questions: {e}", None
67
+
68
  except requests.exceptions.JSONDecodeError as e:
69
+ print(f"Error decoding JSON response from questions endpoint: {e}")
70
+ print(f"Response text: {response.text[:500]}")
71
+ return f"Error decoding server response for questions: {e}", None
72
+
73
  except Exception as e:
74
  print(f"An unexpected error occurred fetching questions: {e}")
75
  return f"An unexpected error occurred fetching questions: {e}", None
76
 
 
77
  results_log = []
78
  answers_payload = []
79
+
80
  print(f"Running agent on {len(questions_data)} questions...")
81
+
82
  for item in questions_data:
83
  task_id = item.get("task_id")
84
  question_text = item.get("question")
85
+
86
  if not task_id or question_text is None:
87
  print(f"Skipping item with missing task_id or question: {item}")
88
  continue
89
+
90
  try:
91
  submitted_answer = agent(question_text)
92
+
93
+ answers_payload.append({
94
+ "task_id": task_id,
95
+ "submitted_answer": submitted_answer
96
+ })
97
+
98
+ results_log.append({
99
+ "Task ID": task_id,
100
+ "Question": question_text,
101
+ "Submitted Answer": submitted_answer
102
+ })
103
+
104
  except Exception as e:
105
+ print(f"Error running agent on task {task_id}: {e}")
106
+
107
+ results_log.append({
108
+ "Task ID": task_id,
109
+ "Question": question_text,
110
+ "Submitted Answer": f"AGENT ERROR: {e}"
111
+ })
112
 
113
  if not answers_payload:
114
  print("Agent did not produce any answers to submit.")
115
+ return (
116
+ "Agent did not produce any answers to submit.",
117
+ pd.DataFrame(results_log)
118
+ )
119
 
120
+ submission_data = {
121
+ "username": username.strip(),
122
+ "agent_code": agent_code,
123
+ "answers": answers_payload
124
+ }
125
 
126
+ status_update = (
127
+ f"Agent finished. Submitting "
128
+ f"{len(answers_payload)} answers for user '{username}'..."
129
+ )
130
+
131
+ print(status_update)
132
  print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
133
+
134
  try:
135
+ response = requests.post(
136
+ submit_url,
137
+ json=submission_data,
138
+ timeout=60
139
+ )
140
+
141
  response.raise_for_status()
142
  result_data = response.json()
143
+
144
  final_status = (
145
  f"Submission Successful!\n"
146
  f"User: {result_data.get('username')}\n"
147
  f"Overall Score: {result_data.get('score', 'N/A')}% "
148
+ f"({result_data.get('correct_count', '?')}/"
149
+ f"{result_data.get('total_attempted', '?')} correct)\n"
150
  f"Message: {result_data.get('message', 'No message received.')}"
151
  )
152
+
153
  print("Submission successful.")
154
+
155
  results_df = pd.DataFrame(results_log)
156
+
157
  return final_status, results_df
158
+
159
  except requests.exceptions.HTTPError as e:
160
+ error_detail = (
161
+ f"Server responded with status "
162
+ f"{e.response.status_code}."
163
+ )
164
+
165
  try:
166
  error_json = e.response.json()
167
+ error_detail += (
168
+ f" Detail: "
169
+ f"{error_json.get('detail', e.response.text)}"
170
+ )
171
+
172
  except requests.exceptions.JSONDecodeError:
173
+ error_detail += (
174
+ f" Response: {e.response.text[:500]}"
175
+ )
176
+
177
  status_message = f"Submission Failed: {error_detail}"
178
+
179
  print(status_message)
180
+
181
  results_df = pd.DataFrame(results_log)
182
+
183
  return status_message, results_df
184
+
185
  except requests.exceptions.Timeout:
186
+ status_message = (
187
+ "Submission Failed: The request timed out."
188
+ )
189
+
190
  print(status_message)
191
+
192
  results_df = pd.DataFrame(results_log)
193
+
194
  return status_message, results_df
195
+
196
  except requests.exceptions.RequestException as e:
197
+ status_message = (
198
+ f"Submission Failed: Network error - {e}"
199
+ )
200
+
201
  print(status_message)
202
+
203
  results_df = pd.DataFrame(results_log)
204
+
205
  return status_message, results_df
206
+
207
  except Exception as e:
208
+ status_message = (
209
+ f"An unexpected error occurred during submission: {e}"
210
+ )
211
+
212
  print(status_message)
213
+
214
  results_df = pd.DataFrame(results_log)
215
+
216
  return status_message, results_df
217
 
218
 
 
219
  with gr.Blocks() as demo:
220
  gr.Markdown("# Basic Agent Evaluation Runner")
221
+
222
  gr.Markdown(
223
  """
224
  **Instructions:**
225
 
226
+ 1. Clone this Space and modify the code to define your agent's logic.
227
+ 2. Log in to your Hugging Face account using the button below.
228
+ 3. Click 'Run Evaluation & Submit All Answers' to run the evaluation.
229
 
 
230
  **Disclaimers:**
231
+
232
+ The evaluation may take some time because the agent processes all questions.
233
  """
234
  )
235
 
236
  gr.LoginButton()
237
 
238
+ run_button = gr.Button(
239
+ "Run Evaluation & Submit All Answers"
240
+ )
241
 
242
+ status_output = gr.Textbox(
243
+ label="Run Status / Submission Result",
244
+ lines=5,
245
+ interactive=False
246
+ )
247
+
248
+ results_table = gr.DataFrame(
249
+ label="Questions and Agent Answers",
250
+ wrap=True
251
+ )
252
 
253
  run_button.click(
254
  fn=run_and_submit_all,
255
  outputs=[status_output, results_table]
256
  )
257
 
258
+
259
  if __name__ == "__main__":
260
+ print("\n" + "-" * 30 + " App Starting " + "-" * 30)
261
+
262
  space_host_startup = os.getenv("SPACE_HOST")
263
+ space_id_startup = os.getenv("SPACE_ID")
264
 
265
  if space_host_startup:
266
+ print(f"SPACE_HOST found: {space_host_startup}")
267
+ print(
268
+ f"Runtime URL: https://{space_host_startup}"
269
+ )
270
  else:
271
+ print(
272
+ "SPACE_HOST environment variable not found."
273
+ )
274
 
275
+ if space_id_startup:
276
+ print(f"SPACE_ID found: {space_id_startup}")
277
+ print(
278
+ f"Repo URL: "
279
+ f"https://huggingface.co/spaces/{space_id_startup}"
280
+ )
281
+ print(
282
+ f"Repo Tree URL: "
283
+ f"https://huggingface.co/spaces/"
284
+ f"{space_id_startup}/tree/main"
285
+ )
286
  else:
287
+ print(
288
+ "SPACE_ID environment variable not found."
289
+ )
290
 
291
+ print("-" * 60)
292
  print("Launching Gradio Interface for Basic Agent Evaluation...")
293
+
294
+ demo.launch(
295
+ debug=True,
296
+ share=False
297
+ )
298
+
requirements.txt CHANGED
@@ -1,3 +1,4 @@
 
1
  gradio
2
  requests
3
  langchain_google_genai
 
1
+ spaces
2
  gradio
3
  requests
4
  langchain_google_genai