abdulaziz744 commited on
Commit
d95582d
·
verified ·
1 Parent(s): db8c88a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -79
app.py CHANGED
@@ -4,24 +4,20 @@ import requests
4
  import inspect
5
  import pandas as pd
6
  from huggingface_hub import InferenceClient
 
 
 
 
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
- import os
15
- from huggingface_hub import InferenceClient
16
- from langchain_core.prompts import PromptTemplate
17
- from ddgs import DDGS
18
- import re
19
-
20
  class BasicAgent:
21
  def __init__(self):
22
- print("BasicAgent initialized with Qwen/Qwen3-Next-80B-A3B-Instruct")
23
  self.client = InferenceClient(
24
- model="Qwen/Qwen3-Next-80B-A3B-Instruct",
25
  token=os.getenv("HF_TOKEN")
26
  )
27
 
@@ -60,108 +56,96 @@ Question: {question}
60
 
61
  Thought:
62
  """)
63
-
64
  full_prompt_start = prompt_template.format(question=question)
65
-
66
  max_steps = 6
67
  history = ""
68
-
69
  for step in range(max_steps):
70
  current_prompt = full_prompt_start + history
71
-
72
- try:
73
- generation = self.client.text_generation(
74
- current_prompt,
75
- max_new_tokens=320,
76
- temperature=0.12,
77
- stop_sequences=["Final Answer:", "Observation:"]
78
- )
79
- except Exception as e:
80
- print(f"Generation failed: {e}")
 
 
 
 
 
 
 
 
 
81
  return "Generation error"
82
-
83
- new_text = generation.strip()
84
  history += new_text + "\n"
85
-
86
  # Look for tool call
87
  action_match = re.search(
88
  r"Action:\s*web_search\s*\[([^\]]+)\]",
89
  history,
90
  re.IGNORECASE | re.DOTALL
91
  )
92
-
93
  if action_match:
94
  query = action_match.group(1).strip()
95
  print(f"Tool call → {query}")
96
  observation = self.search_web(query)
97
  obs_short = observation[:1400] + "..." if len(observation) > 1400 else observation
98
  history += f"\nObservation: {obs_short}\nThought: "
99
-
100
  # Look for final answer
101
  if "Final Answer:" in history:
102
  parts = history.split("Final Answer:")
103
  answer_part = parts[-1].strip()
104
- # Take first clean line, remove trailing punctuation if not needed
105
  answer = answer_part.split("\n")[0].strip()
106
  answer = answer.replace(".", "").replace(",", "").strip()
107
  print(f"→ Final answer: {answer}")
108
  return answer
109
-
110
  print("Max steps reached - no final answer")
111
  return "Unable to answer"
112
 
113
-
114
- def run_and_submit_all( profile: gr.OAuthProfile | None):
115
  """
116
  Fetches all questions, runs the BasicAgent on them, submits all answers,
117
  and displays the results.
118
  """
119
- # --- Determine HF Space Runtime URL and Repo URL ---
120
- space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
121
-
122
  if profile:
123
- username= f"{profile.username}"
124
  print(f"User logged in: {username}")
125
  else:
126
  print("User not logged in.")
127
  return "Please Login to Hugging Face with the button.", None
128
-
129
  api_url = DEFAULT_API_URL
130
  questions_url = f"{api_url}/questions"
131
  submit_url = f"{api_url}/submit"
132
-
133
- # 1. Instantiate Agent ( modify this part to create your agent)
134
  try:
135
  agent = BasicAgent()
136
  except Exception as e:
137
  print(f"Error instantiating agent: {e}")
138
  return f"Error initializing agent: {e}", None
139
- # 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)
140
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
141
  print(agent_code)
142
-
143
- # 2. Fetch Questions
144
  print(f"Fetching questions from: {questions_url}")
145
  try:
146
  response = requests.get(questions_url, timeout=15)
147
  response.raise_for_status()
148
  questions_data = response.json()
149
  if not questions_data:
150
- print("Fetched questions list is empty.")
151
- return "Fetched questions list is empty or invalid format.", None
152
  print(f"Fetched {len(questions_data)} questions.")
153
  except requests.exceptions.RequestException as e:
154
  print(f"Error fetching questions: {e}")
155
  return f"Error fetching questions: {e}", None
156
  except requests.exceptions.JSONDecodeError as e:
157
- print(f"Error decoding JSON response from questions endpoint: {e}")
158
- print(f"Response text: {response.text[:500]}")
159
- return f"Error decoding server response for questions: {e}", None
160
  except Exception as e:
161
  print(f"An unexpected error occurred fetching questions: {e}")
162
  return f"An unexpected error occurred fetching questions: {e}", None
163
-
164
- # 3. Run your Agent
165
  results_log = []
166
  answers_payload = []
167
  print(f"Running agent on {len(questions_data)} questions...")
@@ -176,19 +160,14 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
176
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
177
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
178
  except Exception as e:
179
- print(f"Error running agent on task {task_id}: {e}")
180
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
181
-
182
  if not answers_payload:
183
  print("Agent did not produce any answers to submit.")
184
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
185
-
186
- # 4. Prepare Submission
187
  submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
188
  status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
189
  print(status_update)
190
-
191
- # 5. Submit
192
  print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
193
  try:
194
  response = requests.post(submit_url, json=submission_data, timeout=60)
@@ -231,33 +210,25 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
231
  results_df = pd.DataFrame(results_log)
232
  return status_message, results_df
233
 
234
-
235
  # --- Build Gradio Interface using Blocks ---
236
  with gr.Blocks() as demo:
237
  gr.Markdown("# Basic Agent Evaluation Runner")
238
  gr.Markdown(
239
  """
240
  **Instructions:**
241
-
242
- 1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
243
- 2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
244
- 3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
245
-
246
  ---
247
  **Disclaimers:**
248
  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).
249
  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.
250
  """
251
  )
252
-
253
  gr.LoginButton()
254
-
255
  run_button = gr.Button("Run Evaluation & Submit All Answers")
256
-
257
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
258
- # Removed max_rows=10 from DataFrame constructor
259
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
260
-
261
  run_button.click(
262
  fn=run_and_submit_all,
263
  outputs=[status_output, results_table]
@@ -265,24 +236,19 @@ with gr.Blocks() as demo:
265
 
266
  if __name__ == "__main__":
267
  print("\n" + "-"*30 + " App Starting " + "-"*30)
268
- # Check for SPACE_HOST and SPACE_ID at startup for information
269
  space_host_startup = os.getenv("SPACE_HOST")
270
- space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
271
-
272
  if space_host_startup:
273
  print(f"✅ SPACE_HOST found: {space_host_startup}")
274
- print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
275
  else:
276
- print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
277
-
278
- if space_id_startup: # Print repo URLs if SPACE_ID is found
279
  print(f"✅ SPACE_ID found: {space_id_startup}")
280
- print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
281
- print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
282
  else:
283
- print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
284
-
285
  print("-"*(60 + len(" App Starting ")) + "\n")
286
-
287
  print("Launching Gradio Interface for Basic Agent Evaluation...")
288
  demo.launch(debug=True, share=False)
 
4
  import inspect
5
  import pandas as pd
6
  from huggingface_hub import InferenceClient
7
+ import time
8
+ from langchain_core.prompts import PromptTemplate
9
+ from ddgs import DDGS
10
+ import re
11
 
 
12
  # --- Constants ---
13
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
14
 
15
  # --- Basic Agent Definition ---
 
 
 
 
 
 
 
16
  class BasicAgent:
17
  def __init__(self):
18
+ print("BasicAgent initialized with Qwen/Qwen2.5-72B-Instruct")
19
  self.client = InferenceClient(
20
+ model="Qwen/Qwen2.5-72B-Instruct",
21
  token=os.getenv("HF_TOKEN")
22
  )
23
 
 
56
 
57
  Thought:
58
  """)
 
59
  full_prompt_start = prompt_template.format(question=question)
 
60
  max_steps = 6
61
  history = ""
 
62
  for step in range(max_steps):
63
  current_prompt = full_prompt_start + history
64
+ new_text = None
65
+ for attempt in range(3): # Retry logic
66
+ try:
67
+ messages = [{"role": "user", "content": current_prompt}]
68
+ response = self.client.chat_completion(
69
+ messages=messages,
70
+ max_tokens=320,
71
+ temperature=0.12,
72
+ stop=["Final Answer:", "Observation:"]
73
+ )
74
+ new_text = response.choices[0].message.content.strip()
75
+ break
76
+ except Exception as e:
77
+ print(f"Attempt {attempt+1} failed: {str(e)}")
78
+ if attempt < 2:
79
+ time.sleep(5 * (attempt + 1)) # Backoff
80
+ else:
81
+ return "Generation error after retries"
82
+ if new_text is None:
83
  return "Generation error"
 
 
84
  history += new_text + "\n"
 
85
  # Look for tool call
86
  action_match = re.search(
87
  r"Action:\s*web_search\s*\[([^\]]+)\]",
88
  history,
89
  re.IGNORECASE | re.DOTALL
90
  )
 
91
  if action_match:
92
  query = action_match.group(1).strip()
93
  print(f"Tool call → {query}")
94
  observation = self.search_web(query)
95
  obs_short = observation[:1400] + "..." if len(observation) > 1400 else observation
96
  history += f"\nObservation: {obs_short}\nThought: "
 
97
  # Look for final answer
98
  if "Final Answer:" in history:
99
  parts = history.split("Final Answer:")
100
  answer_part = parts[-1].strip()
 
101
  answer = answer_part.split("\n")[0].strip()
102
  answer = answer.replace(".", "").replace(",", "").strip()
103
  print(f"→ Final answer: {answer}")
104
  return answer
 
105
  print("Max steps reached - no final answer")
106
  return "Unable to answer"
107
 
108
+ def run_and_submit_all(profile: gr.OAuthProfile | None):
 
109
  """
110
  Fetches all questions, runs the BasicAgent on them, submits all answers,
111
  and displays the results.
112
  """
113
+ space_id = os.getenv("SPACE_ID")
 
 
114
  if profile:
115
+ username = f"{profile.username}"
116
  print(f"User logged in: {username}")
117
  else:
118
  print("User not logged in.")
119
  return "Please Login to Hugging Face with the button.", None
 
120
  api_url = DEFAULT_API_URL
121
  questions_url = f"{api_url}/questions"
122
  submit_url = f"{api_url}/submit"
 
 
123
  try:
124
  agent = BasicAgent()
125
  except Exception as e:
126
  print(f"Error instantiating agent: {e}")
127
  return f"Error initializing agent: {e}", None
 
128
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
129
  print(agent_code)
 
 
130
  print(f"Fetching questions from: {questions_url}")
131
  try:
132
  response = requests.get(questions_url, timeout=15)
133
  response.raise_for_status()
134
  questions_data = response.json()
135
  if not questions_data:
136
+ print("Fetched questions list is empty.")
137
+ return "Fetched questions list is empty or invalid format.", None
138
  print(f"Fetched {len(questions_data)} questions.")
139
  except requests.exceptions.RequestException as e:
140
  print(f"Error fetching questions: {e}")
141
  return f"Error fetching questions: {e}", None
142
  except requests.exceptions.JSONDecodeError as e:
143
+ print(f"Error decoding JSON response from questions endpoint: {e}")
144
+ print(f"Response text: {response.text[:500]}")
145
+ return f"Error decoding server response for questions: {e}", None
146
  except Exception as e:
147
  print(f"An unexpected error occurred fetching questions: {e}")
148
  return f"An unexpected error occurred fetching questions: {e}", None
 
 
149
  results_log = []
150
  answers_payload = []
151
  print(f"Running agent on {len(questions_data)} questions...")
 
160
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
161
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
162
  except Exception as e:
163
+ print(f"Error running agent on task {task_id}: {e}")
164
+ results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
 
165
  if not answers_payload:
166
  print("Agent did not produce any answers to submit.")
167
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
 
 
168
  submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
169
  status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
170
  print(status_update)
 
 
171
  print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
172
  try:
173
  response = requests.post(submit_url, json=submission_data, timeout=60)
 
210
  results_df = pd.DataFrame(results_log)
211
  return status_message, results_df
212
 
 
213
  # --- Build Gradio Interface using Blocks ---
214
  with gr.Blocks() as demo:
215
  gr.Markdown("# Basic Agent Evaluation Runner")
216
  gr.Markdown(
217
  """
218
  **Instructions:**
219
+ 1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
220
+ 2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
221
+ 3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
 
 
222
  ---
223
  **Disclaimers:**
224
  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).
225
  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.
226
  """
227
  )
 
228
  gr.LoginButton()
 
229
  run_button = gr.Button("Run Evaluation & Submit All Answers")
 
230
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
 
231
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
 
232
  run_button.click(
233
  fn=run_and_submit_all,
234
  outputs=[status_output, results_table]
 
236
 
237
  if __name__ == "__main__":
238
  print("\n" + "-"*30 + " App Starting " + "-"*30)
 
239
  space_host_startup = os.getenv("SPACE_HOST")
240
+ space_id_startup = os.getenv("SPACE_ID")
 
241
  if space_host_startup:
242
  print(f"✅ SPACE_HOST found: {space_host_startup}")
243
+ print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
244
  else:
245
+ print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
246
+ if space_id_startup:
 
247
  print(f"✅ SPACE_ID found: {space_id_startup}")
248
+ print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
249
+ print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
250
  else:
251
+ print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
 
252
  print("-"*(60 + len(" App Starting ")) + "\n")
 
253
  print("Launching Gradio Interface for Basic Agent Evaluation...")
254
  demo.launch(debug=True, share=False)