thecodemaiden commited on
Commit
9f64264
·
2 Parent(s): 9b50f40355b538

Merge branch 'feat/experimental'

Browse files
Files changed (2) hide show
  1. app.py +158 -68
  2. my_agent.py +39 -4
app.py CHANGED
@@ -5,11 +5,13 @@ import pandas as pd
5
  from my_agent import GeminiAgentContainer
6
  from markdownify import markdownify as to_markdown
7
  import time
 
8
 
9
  # (Keep Constants as is)
10
  # --- Constants ---
11
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
12
 
 
13
 
14
 
15
  def run_and_submit_all( profile: gr.OAuthProfile | None):
@@ -42,71 +44,86 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
42
  print(agent_code)
43
 
44
  # 2. Fetch Questions
 
 
 
 
 
 
 
45
  print(f"Fetching questions from: {questions_url}")
46
  try:
47
  response = requests.get(questions_url, timeout=15)
48
  response.raise_for_status()
49
  questions_data = response.json()
50
  if not questions_data:
51
- print("Fetched questions list is empty.")
52
- return "Fetched questions list is empty or invalid format.", None
53
  print(f"Fetched {len(questions_data)} questions.")
54
  except requests.exceptions.RequestException as e:
55
  print(f"Error fetching questions: {e}")
56
- return f"Error fetching questions: {e}", None
57
  except requests.exceptions.JSONDecodeError as e:
58
  print(f"Error decoding JSON response from questions endpoint: {e}")
59
  print(f"Response text: {response.text[:500]}")
60
- return f"Error decoding server response for questions: {e}", None
61
  except Exception as e:
62
  print(f"An unexpected error occurred fetching questions: {e}")
63
- return f"An unexpected error occurred fetching questions: {e}", None
64
-
65
- def answer_question(question_data):
66
- task_id = question_data.get("task_id")
67
- question_text = question_data.get("question")
68
- payload = None
69
- submitted_answer = None
70
- agent_error = None
71
-
72
- try:
73
- if not task_id or question_text is None:
74
- raise ValueError(f"Missing task_id or question in item: {question_data}")
75
- submitted_answer = agent(question_text)
76
- payload = {"task_id": task_id, "submitted_answer": submitted_answer}
77
- except Exception as e:
78
- print(f"Error running agent on task {task_id}: {e}")
79
- agent_error = f"AGENT ERROR: {e}"
80
- finally:
81
- log_entry = {
82
- "Task ID": task_id,
83
- "Question": question_text,
84
- "Submitted Answer": submitted_answer or agent_error,
85
- }
86
- return payload, log_entry
87
-
88
 
 
 
 
 
 
 
 
 
 
 
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
- payload_data, log_entry = answer_question(item)
96
- if payload_data:
97
- answers_payload.append(payload_data)
98
- results_log.append(log_entry)
99
- time.sleep(1)
100
- if not answers_payload:
101
- print("Agent did not produce any answers to submit.")
102
- return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
 
 
 
 
 
 
103
 
104
- # 4. Prepare Submission
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
  submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
106
- status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
107
  print(status_update)
108
 
109
- # 5. Submit
110
  print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
111
  try:
112
  response = requests.post(submit_url, json=submission_data, timeout=60)
@@ -119,9 +136,8 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
119
  f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
120
  f"Message: {result_data.get('message', 'No message received.')}"
121
  )
122
- print("Submission successful.")
123
- results_df = pd.DataFrame(results_log)
124
- return final_status, results_df
125
  except requests.exceptions.HTTPError as e:
126
  error_detail = f"Server responded with status {e.response.status_code}."
127
  try:
@@ -131,23 +147,96 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
131
  error_detail += f" Response: {e.response.text[:500]}"
132
  status_message = f"Submission Failed: {error_detail}"
133
  print(status_message)
134
- results_df = pd.DataFrame(results_log)
135
- return status_message, results_df
136
  except requests.exceptions.Timeout:
137
  status_message = "Submission Failed: The request timed out."
138
  print(status_message)
139
- results_df = pd.DataFrame(results_log)
140
- return status_message, results_df
141
  except requests.exceptions.RequestException as e:
142
  status_message = f"Submission Failed: Network error - {e}"
143
  print(status_message)
144
- results_df = pd.DataFrame(results_log)
145
- return status_message, results_df
146
  except Exception as e:
147
  status_message = f"An unexpected error occurred during submission: {e}"
148
  print(status_message)
149
- results_df = pd.DataFrame(results_log)
150
- return status_message, results_df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
151
 
152
 
153
  # --- Build Gradio Interface using Blocks ---
@@ -157,30 +246,30 @@ with gr.Blocks() as demo:
157
  """
158
  **Instructions:**
159
 
160
- 1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
161
- 2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
162
- 3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
163
-
164
- ---
165
- **Disclaimers:**
166
- 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).
167
- 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.
168
- """
169
  )
170
 
171
  gr.LoginButton()
172
 
173
- run_button = gr.Button("Run Evaluation & Submit All Answers")
 
174
 
175
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
176
- # Removed max_rows=10 from DataFrame constructor
177
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
178
 
179
  run_button.click(
180
- fn=run_and_submit_all,
181
  outputs=[status_output, results_table]
182
  )
183
 
 
 
 
 
 
184
  if __name__ == "__main__":
185
  print("\n" + "-"*30 + " App Starting " + "-"*30)
186
  # Check for SPACE_HOST and SPACE_ID at startup for information
@@ -199,6 +288,7 @@ if __name__ == "__main__":
199
  print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
200
  else:
201
  print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
 
202
 
203
  print("-"*(60 + len(" App Starting ")) + "\n")
204
 
 
5
  from my_agent import GeminiAgentContainer
6
  from markdownify import markdownify as to_markdown
7
  import time
8
+ import json
9
 
10
  # (Keep Constants as is)
11
  # --- Constants ---
12
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
13
 
14
+ <<<<<<< HEAD
15
 
16
 
17
  def run_and_submit_all( profile: gr.OAuthProfile | None):
 
44
  print(agent_code)
45
 
46
  # 2. Fetch Questions
47
+ =======
48
+ questions = None
49
+ results_log = []
50
+ answers_by_task = {}
51
+
52
+ def load_questions(questions_url):
53
+ >>>>>>> feat/experimental
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 or invalid.")
61
+ return 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 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 None
70
  except Exception as e:
71
  print(f"An unexpected error occurred fetching questions: {e}")
72
+ return None
73
+ return questions_data
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
 
75
+ def answer_one(agent, question_data):
76
+ """
77
+ Runs the agent on a single question and returns the result.
78
+ """
79
+ task_id = question_data.get("task_id")
80
+ question_text = question_data.get("question")
81
+ filename = question_data.get("file_name")
82
+ payload = None
83
+ submitted_answer = None
84
+ agent_error = None
85
 
86
+ try:
87
+ if not task_id or question_text is None:
88
+ raise ValueError(f"Missing task_id or question in item: {question_data}")
89
+ if filename:
90
+ file_prompt = f"\nThere is an attached file with task id `{task_id}` available.\n"
91
+ question_text = file_prompt + question_text
92
+ submitted_answer = agent(question_text)
93
+ payload = {"task_id": task_id, "submitted_answer": submitted_answer}
94
+ except Exception as e:
95
+ print(agent)
96
+ print(f"Error running agent on task {task_id}: {e}")
97
+ agent_error = f"AGENT ERROR: {e}"
98
+ finally:
99
+ log_entry = {
100
+ "Task ID": task_id,
101
+ "Question": question_text,
102
+ "Submitted Answer": submitted_answer or agent_error,
103
+ }
104
+ return payload, log_entry
105
 
106
+ def _submit_answers_to_file(answers_payload, file_path):
107
+ """
108
+ Submits the answers to a local file.
109
+ """
110
+
111
+ try:
112
+ with open(file_path, "w") as file:
113
+ json.dump(answers_payload, file, indent=4)
114
+ submit_status = (f"Answers successfully written to {file_path}")
115
+ except Exception as e:
116
+ submit_status = (f"Error writing answers to file: {e}")
117
+ print(submit_status)
118
+ return submit_status
119
+
120
+ def _submit_all(username, agent_code, answers_payload, submit_url):
121
+ # Prepare Submission
122
  submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
123
+ status_update = f"Submitting {len(answers_payload)} answers for user '{username}'..."
124
  print(status_update)
125
 
126
+ # Submit Answers
127
  print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
128
  try:
129
  response = requests.post(submit_url, json=submission_data, timeout=60)
 
136
  f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
137
  f"Message: {result_data.get('message', 'No message received.')}"
138
  )
139
+ print(final_status)
140
+ return final_status
 
141
  except requests.exceptions.HTTPError as e:
142
  error_detail = f"Server responded with status {e.response.status_code}."
143
  try:
 
147
  error_detail += f" Response: {e.response.text[:500]}"
148
  status_message = f"Submission Failed: {error_detail}"
149
  print(status_message)
150
+ return status_message
 
151
  except requests.exceptions.Timeout:
152
  status_message = "Submission Failed: The request timed out."
153
  print(status_message)
154
+ return status_message
 
155
  except requests.exceptions.RequestException as e:
156
  status_message = f"Submission Failed: Network error - {e}"
157
  print(status_message)
158
+ return status_message
 
159
  except Exception as e:
160
  status_message = f"An unexpected error occurred during submission: {e}"
161
  print(status_message)
162
+ return status_message
163
+
164
+
165
+ def prepare_agent(api_key=None):
166
+ # 1. Instantiate Agent ( modify this part to create your agent)
167
+ try:
168
+ agent = GeminiAgentContainer()
169
+ print(agent.system_prompt)
170
+ except Exception as e:
171
+ print(f"Error instantiating agent: {e}")
172
+ return None
173
+
174
+ return agent
175
+
176
+ def _run_all(api_key: str | None = None):
177
+ """
178
+ Fetches all questions, runs the BasicAgent on them,
179
+ """
180
+
181
+ questions_url = f"{DEFAULT_API_URL}/questions"
182
+
183
+ agent = prepare_agent(api_key)
184
+
185
+ questions_data = load_questions(questions_url)
186
+ # 3. Run your Agent
187
+
188
+ print(f"Running agent on {len(questions_data)} questions...")
189
+ for item in questions_data:
190
+ payload_data, log_entry = answer_one(agent, item)
191
+ if payload_data:
192
+ task_id = payload_data.get("task_id")
193
+ answers_by_task[task_id] = payload_data
194
+ results_log.append(log_entry)
195
+ time.sleep(1)
196
+ if not answers_by_task:
197
+ final_status = "Agent did not produce any answers to submit."
198
+ else:
199
+ final_status = f"Agent finished, {len(answers_by_task)} answers produced."
200
+ print(final_status)
201
+ return final_status, pd.DataFrame(results_log)
202
+
203
+
204
+ def run_all():
205
+ run_status, results_df = _run_all(os.environ.get("GOOGLE_API_KEY"))
206
+ return run_status, results_df
207
+
208
+ def submit_all( profile: gr.OAuthProfile | None, to_file = True):
209
+ """
210
+ Submits all answers and displays the results.
211
+ """
212
+ submit_url = f"{DEFAULT_API_URL}/submit"
213
+
214
+ if not to_file:
215
+ if profile:
216
+ username= f"{profile.username}"
217
+ print(f"User logged in: {username}")
218
+ else:
219
+ print("User not logged in.")
220
+ return "Please Login to Hugging Face with the button."
221
+
222
+ # --- Determine HF Space Runtime URL and Repo URL ---
223
+ space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
224
+
225
+ if not answers_by_task:
226
+ submit_status = "No answers to submit."
227
+ else:
228
+
229
+ # 4. Submit all answers
230
+ answers_payload = list(answers_by_task.values())
231
+ # 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)
232
+ agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
233
+
234
+ if to_file:
235
+ submit_status = _submit_answers_to_file(answers_payload, f"run-{int(time.time())}.json")
236
+ else:
237
+ submit_status = _submit_all(username, agent_code, answers_payload, submit_url)
238
+
239
+ return submit_status
240
 
241
 
242
  # --- Build Gradio Interface using Blocks ---
 
246
  """
247
  **Instructions:**
248
 
249
+ 1. Please use your own Gemini API key to run the agent. You can find your API key in your [Gemini account settings](https://gemini.com/account/settings).
250
+ 2. Click 'Run Evaluation' to fetch questions, run the agent, and see the answers.
251
+ 3. Click 'Submit All Answers' to submit the answers to the server.
252
+ """
 
 
 
 
 
253
  )
254
 
255
  gr.LoginButton()
256
 
257
+ run_button = gr.Button("Run Evaluation")
258
+ submit_button = gr.Button("Submit All Answers")
259
 
260
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
 
261
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
262
 
263
  run_button.click(
264
+ fn=run_all,
265
  outputs=[status_output, results_table]
266
  )
267
 
268
+ submit_button.click(
269
+ fn=submit_all,
270
+ outputs=[status_output]
271
+ )
272
+
273
  if __name__ == "__main__":
274
  print("\n" + "-"*30 + " App Starting " + "-"*30)
275
  # Check for SPACE_HOST and SPACE_ID at startup for information
 
288
  print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
289
  else:
290
  print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
291
+ print(f"API KEY: {os.getenv('GOOGLE_API_KEY')}")
292
 
293
  print("-"*(60 + len(" App Starting ")) + "\n")
294
 
my_agent.py CHANGED
@@ -83,7 +83,7 @@ def google_search(query: str) -> str:
83
  return result
84
 
85
  @tool
86
- def search_wikipedia_pages(query: str) -> str:
87
  """
88
  Searches for Wikipedia pages related to the query and returns the canonical titles of the related pages.
89
  Args:
@@ -118,6 +118,39 @@ def get_wikipedia_page(page_title: str) -> str:
118
  md_content = to_markdown(page.html())
119
  return md_content
120
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121
  # --- Agent Management ---
122
 
123
  class GeminiAgentContainer:
@@ -130,7 +163,7 @@ class GeminiAgentContainer:
130
  # model = HfApiModel(model_id=model_id, provider=provider)
131
  self.model = LiteLLMModel(model_id=f"gemini/{model_name}", api_key=os.getenv("GOOGLE_API_KEY"))
132
  system_prompt = """
133
- You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer only your final answer on a line by itself.
134
  YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings.
135
  If your answer is a number and you are not explicitly asked for a string, write it in numerals instead of words, and don't use comma to write your number nor use units such as $ or percent sign unless specified otherwise.
136
  If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise.
@@ -148,10 +181,12 @@ class GeminiAgentContainer:
148
  tools = [
149
  watch_video,
150
  google_search,
151
- search_wikipedia_pages,
 
152
  get_wikipedia_page,
153
  ],
154
- max_steps=5,
 
155
  )
156
  self.system_prompt = system_prompt
157
 
 
83
  return result
84
 
85
  @tool
86
+ def check_wikipedia_page_titles(query: str) -> str:
87
  """
88
  Searches for Wikipedia pages related to the query and returns the canonical titles of the related pages.
89
  Args:
 
118
  md_content = to_markdown(page.html())
119
  return md_content
120
 
121
+ @tool
122
+ def run_query_with_file(task_id: str, mime_type: str | None, user_query: str) -> str:
123
+ """
124
+ Downloads a file mentioned in a user prompt, adds it to the context, and runs a query on it.
125
+ This assumes the file is 20MB or less.
126
+ Args:
127
+ task_id (str): A unique identifier for the task related to this file, used to download it.
128
+ mime_type (str): The MIME type of the file, or the best guess if unknown
129
+ user_query (str): The question to answer about the file.
130
+ Returns:
131
+ str: The answer to the question.
132
+ """
133
+ # Download the file
134
+ file_url = f"https://agents-course-unit4-scoring.hf.space/files/{task_id}"
135
+ file_response = requests.get(file_url)
136
+ if file_response.status_code != 200:
137
+ raise Exception(f"Failed to download file: {file_response.status_code} - {file_response.text}")
138
+ file_data = file_response.content
139
+ mime_type = mime_type or file_response.headers.get('Content-Type', 'application/octet-stream')
140
+ response = client.models.generate_content(
141
+ model=model_name,
142
+ contents=[
143
+ types.Part.from_bytes(
144
+ data=file_data,
145
+ mime_type=mime_type,
146
+ ),
147
+ user_query,
148
+ ]
149
+ )
150
+
151
+ return response.text
152
+
153
+
154
  # --- Agent Management ---
155
 
156
  class GeminiAgentContainer:
 
163
  # model = HfApiModel(model_id=model_id, provider=provider)
164
  self.model = LiteLLMModel(model_id=f"gemini/{model_name}", api_key=os.getenv("GOOGLE_API_KEY"))
165
  system_prompt = """
166
+ You are a general AI assistant. I will ask you a question.
167
  YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings.
168
  If your answer is a number and you are not explicitly asked for a string, write it in numerals instead of words, and don't use comma to write your number nor use units such as $ or percent sign unless specified otherwise.
169
  If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise.
 
181
  tools = [
182
  watch_video,
183
  google_search,
184
+ run_query_with_file,
185
+ check_wikipedia_page_titles,
186
  get_wikipedia_page,
187
  ],
188
+ max_steps=6,
189
+ planning_interval=2,
190
  )
191
  self.system_prompt = system_prompt
192