thecodemaiden commited on
Commit
1fdd9d3
·
2 Parent(s): f1609cc0d0a5fc

Merge branch 'feat/experimental'

Browse files
Files changed (2) hide show
  1. app.py +40 -15
  2. my_agent.py +160 -123
app.py CHANGED
@@ -68,8 +68,7 @@ def answer_one(agent, question_data):
68
  "Question": question_text,
69
  "Submitted Answer": submitted_answer or agent_error,
70
  }
71
- return payload, log_entry
72
-
73
 
74
  def _submit_all(username, agent_code, answers_payload, submit_url):
75
  # Prepare Submission
@@ -119,7 +118,7 @@ def _submit_all(username, agent_code, answers_payload, submit_url):
119
  def prepare_agent(api_key=None):
120
  # 1. Instantiate Agent ( modify this part to create your agent)
121
  try:
122
- agent = GeminiAgentContainer()
123
  print(agent.system_prompt)
124
  except Exception as e:
125
  print(f"Error instantiating agent: {e}")
@@ -127,7 +126,27 @@ def prepare_agent(api_key=None):
127
 
128
  return agent
129
 
130
- def _run_all(api_key: str | None = None):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
131
  """
132
  Fetches all questions, runs the BasicAgent on them,
133
  """
@@ -154,38 +173,29 @@ def _run_all(api_key: str | None = None):
154
  print(final_status)
155
  return final_status, pd.DataFrame(results_log)
156
 
157
-
158
- def run_all():
159
- run_status, results_df = _run_all(os.environ.get("GOOGLE_API_KEY"))
160
- return run_status, results_df
161
-
162
  def submit_all( profile: gr.OAuthProfile | None):
163
  """
164
  Submits all answers and displays the results.
165
  """
166
  submit_url = f"{DEFAULT_API_URL}/submit"
167
 
168
-
169
  if profile:
170
  username= f"{profile.username}"
171
  print(f"User logged in: {username}")
172
  else:
173
  print("User not logged in.")
174
  return "Please Login to Hugging Face with the button."
175
-
176
  # --- Determine HF Space Runtime URL and Repo URL ---
177
  space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
178
 
179
  if not answers_by_task:
180
  submit_status = "No answers to submit."
181
  else:
182
-
183
  # 4. Submit all answers
184
- answers_payload = list(answers_by_task.values())
185
  # 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)
186
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
187
-
188
- submit_status = _submit_all(username, agent_code, answers_payload, submit_url)
189
 
190
  return submit_status
191
 
@@ -205,7 +215,16 @@ with gr.Blocks() as demo:
205
 
206
  gr.LoginButton()
207
 
 
 
 
 
 
 
 
 
208
  run_button = gr.Button("Run Evaluation")
 
209
  submit_button = gr.Button("Submit All Answers")
210
 
211
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
@@ -213,9 +232,15 @@ with gr.Blocks() as demo:
213
 
214
  run_button.click(
215
  fn=run_all,
 
216
  outputs=[status_output, results_table]
217
  )
218
 
 
 
 
 
 
219
  submit_button.click(
220
  fn=submit_all,
221
  outputs=[status_output]
 
68
  "Question": question_text,
69
  "Submitted Answer": submitted_answer or agent_error,
70
  }
71
+ return payload, log_entry
 
72
 
73
  def _submit_all(username, agent_code, answers_payload, submit_url):
74
  # Prepare Submission
 
118
  def prepare_agent(api_key=None):
119
  # 1. Instantiate Agent ( modify this part to create your agent)
120
  try:
121
+ agent = GeminiAgentContainer(api_key=api_key)
122
  print(agent.system_prompt)
123
  except Exception as e:
124
  print(f"Error instantiating agent: {e}")
 
126
 
127
  return agent
128
 
129
+ def save_answers_to_file():
130
+ """
131
+ Submits the answers to a local file named with the current epoch time.
132
+ """
133
+ if not answers_by_task:
134
+ return ("Nothing to save, no answers found.")
135
+ answers_payload = list(answers_by_task.values())
136
+
137
+ file_path = f"answers-{int(time.time())}.json"
138
+ print(f"Saving answers to file: {file_path}")
139
+ try:
140
+ with open(file_path, "w") as file:
141
+ json.dump(answers_payload, file, indent=4)
142
+ submit_status = (f"Answers successfully written to {file_path}")
143
+ except Exception as e:
144
+ submit_status = (f"Error writing answers to file: {e}")
145
+ print(submit_status)
146
+ return submit_status
147
+
148
+
149
+ def run_all(api_key: str | None = None):
150
  """
151
  Fetches all questions, runs the BasicAgent on them,
152
  """
 
173
  print(final_status)
174
  return final_status, pd.DataFrame(results_log)
175
 
 
 
 
 
 
176
  def submit_all( profile: gr.OAuthProfile | None):
177
  """
178
  Submits all answers and displays the results.
179
  """
180
  submit_url = f"{DEFAULT_API_URL}/submit"
181
 
 
182
  if profile:
183
  username= f"{profile.username}"
184
  print(f"User logged in: {username}")
185
  else:
186
  print("User not logged in.")
187
  return "Please Login to Hugging Face with the button."
 
188
  # --- Determine HF Space Runtime URL and Repo URL ---
189
  space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
190
 
191
  if not answers_by_task:
192
  submit_status = "No answers to submit."
193
  else:
 
194
  # 4. Submit all answers
 
195
  # 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)
196
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
197
+
198
+ submit_status = _submit_all(username, agent_code, list(answers_by_task.values()), submit_url)
199
 
200
  return submit_status
201
 
 
215
 
216
  gr.LoginButton()
217
 
218
+ api_key_input = gr.Textbox(
219
+ label="Gemini API Key",
220
+ placeholder="Enter your Gemini API key here",
221
+ type="password",
222
+ lines=1,
223
+ visible=True
224
+ )
225
+
226
  run_button = gr.Button("Run Evaluation")
227
+ save_button = gr.Button("Save Answers to File")
228
  submit_button = gr.Button("Submit All Answers")
229
 
230
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
 
232
 
233
  run_button.click(
234
  fn=run_all,
235
+ inputs=[api_key_input],
236
  outputs=[status_output, results_table]
237
  )
238
 
239
+ save_button.click(
240
+ fn=save_answers_to_file,
241
+ outputs=[status_output]
242
+ )
243
+
244
  submit_button.click(
245
  fn=submit_all,
246
  outputs=[status_output]
my_agent.py CHANGED
@@ -1,30 +1,38 @@
1
  import os
2
  import requests
3
- from smolagents import LiteLLMModel, ToolCallingAgent, tool
4
  from typing import Optional
5
  from google import genai
6
  from google.genai import types
7
  import wikipedia as wiki
8
  from markdownify import markdownify as to_markdown
9
 
10
- # --- Constants --
11
- client = genai.Client(api_key=os.getenv("GOOGLE_API_KEY"))
12
- model_name = "gemini-2.0-flash"
13
-
14
-
15
  # --- Tools ---
16
- @tool
17
- def watch_video(video_url: str, user_query:str) -> str:
18
- """
19
- Watches a video and answers a question about it.
20
- Args:
21
- video_url (str): The URL of the video to watch.
22
- user_query (str): The question to answer about the video.
23
- Returns:
24
- str: The answer to the question.
25
  """
26
- request_json = {
27
- 'model': f'models/{model_name}',
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  'contents': [{
29
  "parts": [
30
  {
@@ -38,128 +46,157 @@ def watch_video(video_url: str, user_query:str) -> str:
38
  ]
39
  }]
40
  }
41
- api_url = f'https://generativelanguage.googleapis.com/v1beta/models/{model_name}:generateContent?key={os.getenv("GOOGLE_API_KEY")}'
42
- response = requests.post(
43
- api_url,
44
- json=request_json,
45
- headers={
46
- 'Content-Type': 'application/json',
47
- }
48
- )
49
- if response.status_code != 200:
50
- return f"Error: {response.status_code} - {response.text}"
51
- response_json = response.json()
52
- result_parts = response_json['candidates'][0]['content']['parts']
53
- result = "".join([_.get('text', '') for _ in result_parts])
54
- return result
55
-
56
- @tool
57
- def google_search(query: str) -> str:
58
- """
59
  Performs a Google search and returns the results.
60
- Args:
61
- query (str): The search query.
62
- Returns:
63
- str: The search results.
64
  """
65
- # This is a placeholder for the actual Google search logic
66
- # In a real implementation, you would use the Google Search API
67
- # to get the results.
68
- # For now, we will just return a dummy response.
69
- google_search_tool = types.Tool(
70
- google_search=types.GoogleSearch()
71
- )
72
- response = client.models.generate_content(
73
- model=model_name,
 
 
 
 
 
 
 
 
 
 
74
  contents=f"Please search the internet for: {query}",
75
  config=types.GenerateContentConfig(
76
  tools=[google_search_tool],
77
  response_modalities=['TEXT'],
 
78
  )
79
- )
80
- result_parts = response.candidates[0].content.parts
81
- result = "".join([_.text for _ in result_parts])
 
 
 
 
 
 
 
 
 
 
 
82
 
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:
90
- query (str): The search query.
91
- Returns:
92
- str: A comma separated list of canonical Wikipedia page titles that match the query.
93
- """
94
- response = wiki.search(query)
95
- if len(response) > 0:
96
- result = ", ".join(response)
97
- else:
98
- result = "No results found."
99
- return result
 
 
 
 
100
 
101
- @tool
102
- def get_wikipedia_page(page_title: str) -> str:
103
- """
104
- Gets the content of a we
105
- Args:
106
- page_title (str): The canonical title of the Wikipedia page.
107
- Returns:
108
- str: Markdown-formatted content of the Wikipedia page.
109
- """
110
- # This is a placeholder for the actual Wikipedia search logic
111
- # In a real implementation, you would use the Wikipedia API
112
- # to get the results.
113
- # For now, we will just return a dummy response.
114
- try:
115
- page = wiki.page(page_title)
116
- except wiki.exceptions.PageError:
117
- return f"Page '{page_title}' not found."
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:
157
  """
158
  A container for the Gemini agent.
159
  """
160
- def __init__(self):
161
- # TODO: make it easier to chnge the model
162
- self.model = LiteLLMModel(model_id=f"gemini/{model_name}", api_key=os.getenv("GOOGLE_API_KEY"))
 
 
 
 
163
  system_prompt = """
164
  You are a general AI assistant. I will ask you a question.
165
  YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings.
@@ -177,12 +214,12 @@ class GeminiAgentContainer:
177
  self.agent = ToolCallingAgent(
178
  model=self.model,
179
  tools = [
180
- watch_video,
181
- google_search,
182
- run_query_with_file,
183
- check_wikipedia_page_titles,
184
- get_wikipedia_page,
185
- ],
186
  max_steps=14,
187
  planning_interval=2,
188
  )
 
1
  import os
2
  import requests
3
+ from smolagents import LiteLLMModel, ToolCallingAgent, Tool
4
  from typing import Optional
5
  from google import genai
6
  from google.genai import types
7
  import wikipedia as wiki
8
  from markdownify import markdownify as to_markdown
9
 
 
 
 
 
 
10
  # --- Tools ---
11
+
12
+ class VideoWatchingTool(Tool):
13
+ name = "watch_video"
14
+ description ="""
15
+ A tool for watching videos and answering questions about them.
 
 
 
 
16
  """
17
+ inputs = {
18
+ "video_url": {
19
+ "type": "string",
20
+ "description": "The URL of the video to watch."
21
+ },
22
+ "user_query": {
23
+ "type": "string",
24
+ "description": "The question to answer about the video."
25
+ }
26
+ }
27
+ output_type = "string"
28
+
29
+ def __init__(self, model_name, *args, **kwargs):
30
+ super().__init__(*args, **kwargs)
31
+ self.model_name = model_name
32
+
33
+ def forward(self, video_url: str, user_query: str) -> str:
34
+ request_json = {
35
+ 'model': f'models/{self.model_name}',
36
  'contents': [{
37
  "parts": [
38
  {
 
46
  ]
47
  }]
48
  }
49
+ api_url = f'https://generativelanguage.googleapis.com/v1beta/models/{self.model_name}:generateContent?key={os.getenv("GOOGLE_API_KEY")}'
50
+ response = requests.post(
51
+ api_url,
52
+ json=request_json,
53
+ headers={
54
+ 'Content-Type': 'application/json',
55
+ }
56
+ )
57
+ if response.status_code != 200:
58
+ return f"Error: {response.status_code} - {response.text}"
59
+ response_json = response.json()
60
+ result_parts = response_json['candidates'][0]['content']['parts']
61
+ result = "".join([_.get('text', '') for _ in result_parts])
62
+ return result
63
+
64
+ class GoogleSearchTool(Tool):
65
+ name = "google_search"
66
+ description = """
67
  Performs a Google search and returns the results.
 
 
 
 
68
  """
69
+ inputs = {
70
+ "query": {
71
+ "type": "string",
72
+ "description": "The search query."
73
+ }
74
+ }
75
+ output_type = "string"
76
+
77
+ def __init__(self, client, model_name, *args, **kwargs):
78
+ super().__init__(*args, **kwargs)
79
+ self.client = client
80
+ self.model_name = model_name
81
+
82
+ def forward(self, query: str) -> str:
83
+ google_search_tool = types.Tool(
84
+ google_search=types.GoogleSearch()
85
+ )
86
+ response = self.client.models.generate_content(
87
+ model=self.model_name,
88
  contents=f"Please search the internet for: {query}",
89
  config=types.GenerateContentConfig(
90
  tools=[google_search_tool],
91
  response_modalities=['TEXT'],
92
+ )
93
  )
94
+ return response.text
95
+
96
+ class WikipediaTitleSearchTool(Tool):
97
+ name = "check_wikipedia_page_titles"
98
+ description = """
99
+ Searches for Wikipedia pages related to the query and returns the canonical titles of the related pages.
100
+ """
101
+ inputs = {
102
+ "query": {
103
+ "type": "string",
104
+ "description": "The search query."
105
+ }
106
+ }
107
+ output_type = "string"
108
 
109
+ def forward(self, query: str) -> str:
110
+ response = wiki.search(query)
111
+ if len(response) > 0:
112
+ result = ", ".join(response)
113
+ else:
114
+ result = "No results found."
115
+ return result
116
 
117
+ class WikipediaPageTool(Tool):
118
+ name = "get_wikipedia_page"
119
+ description = """
120
+ Gets the content of a Wikipedia page.
121
  """
122
+ inputs = {
123
+ "page_title": {
124
+ "type": "string",
125
+ "description": "The canonical title of the Wikipedia page."
126
+ }
127
+ }
128
+ output_type = "string"
129
+
130
+ def forward(self, page_title: str) -> str:
131
+ # TODO: may need to do caching of the HTML ourselves?
132
+ try:
133
+ page = wiki.page(page_title)
134
+ except wiki.exceptions.PageError:
135
+ return f"Page '{page_title}' not found."
136
+ md_content = to_markdown(page.html())
137
+ return md_content
138
 
139
+ class FileAttachmentQueryTool(Tool):
140
+ name = "run_query_with_file"
141
+ description = """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
142
  Downloads a file mentioned in a user prompt, adds it to the context, and runs a query on it.
143
  This assumes the file is 20MB or less.
 
 
 
 
 
 
144
  """
145
+ inputs = {
146
+ "task_id": {
147
+ "type": "string",
148
+ "description": "A unique identifier for the task related to this file, used to download it."
149
+ },
150
+ "mime_type": {
151
+ "type": "string",
152
+ "nullable": True,
153
+ "description": "The MIME type of the file, or the best guess if unknown."
154
+ },
155
+ "user_query": {
156
+ "type": "string",
157
+ "description": "The question to answer about the file."
158
+ }
159
+ }
160
+ output_type = "string"
161
+ def __init__(self, client, model_name, *args, **kwargs):
162
+ super().__init__(*args, **kwargs)
163
+ self.client = client
164
+ self.model_name = model_name
165
 
166
+ def forward(self, task_id: str, mime_type: str | None, user_query: str) -> str:
167
+ # Download the file
168
+ file_url = f"https://agents-course-unit4-scoring.hf.space/files/{task_id}"
169
+ file_response = requests.get(file_url)
170
+ if file_response.status_code != 200:
171
+ raise Exception(f"Failed to download file: {file_response.status_code} - {file_response.text}")
172
+ file_data = file_response.content
173
+ mime_type = mime_type or file_response.headers.get('Content-Type', 'application/octet-stream')
174
+ response = self.client.models.generate_content(
175
+ model=self.model_name,
176
+ contents=[
177
+ types.Part.from_bytes(
178
+ data=file_data,
179
+ mime_type=mime_type,
180
+ ),
181
+ user_query,
182
+ ]
183
+ )
184
 
185
+ return response.text
186
+
187
  # --- Agent Management ---
188
 
189
  class GeminiAgentContainer:
190
  """
191
  A container for the Gemini agent.
192
  """
193
+ # TODO: make it easier to chnge the model
194
+ MODEL_NAME = "gemini-2.0-flash"
195
+
196
+ def __init__(self, api_key: Optional[str] = None):
197
+ api_key = api_key or os.getenv("GOOGLE_API_KEY")
198
+ self.model = LiteLLMModel(model_id=f"gemini/{self.MODEL_NAME}", api_key=api_key)
199
+ self.client = genai.Client(api_key=os.getenv("GOOGLE_API_KEY"))
200
  system_prompt = """
201
  You are a general AI assistant. I will ask you a question.
202
  YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings.
 
214
  self.agent = ToolCallingAgent(
215
  model=self.model,
216
  tools = [
217
+ VideoWatchingTool(model_name=self.MODEL_NAME),
218
+ GoogleSearchTool(client=self.client, model_name=self.MODEL_NAME),
219
+ WikipediaTitleSearchTool(),
220
+ WikipediaPageTool(),
221
+ FileAttachmentQueryTool(client=self.client, model_name=self.MODEL_NAME),
222
+ ],
223
  max_steps=14,
224
  planning_interval=2,
225
  )