thecodemaiden commited on
Commit
5c654d9
·
1 Parent(s): 81917a3

feat: use Gemini-2.0-Flash, add tools for Google, Wikipedia, YouTube

Browse files
Files changed (2) hide show
  1. app.py +187 -23
  2. requirements.txt +8 -1
app.py CHANGED
@@ -3,21 +3,168 @@ 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
  """
@@ -40,7 +187,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
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
@@ -69,24 +216,41 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
69
  print(f"An unexpected error occurred fetching questions: {e}")
70
  return f"An unexpected error occurred fetching questions: {e}", None
71
 
72
- # 3. Run your Agent
73
- results_log = []
74
- answers_payload = []
75
- print(f"Running agent on {len(questions_data)} questions...")
76
- for item in questions_data:
77
- task_id = item.get("task_id")
78
- question_text = item.get("question")
79
- if not task_id or question_text is 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)
 
3
  import requests
4
  import inspect
5
  import pandas as pd
6
+ from smolagents import tool, LiteLLMModel, ToolCallingAgent
7
+ from typing import Optional
8
+ from google import genai
9
+ from google.genai import types
10
+ import wikipedia as wiki
11
+ from markdownify import markdownify as to_markdown
12
+ import time
13
 
14
  # (Keep Constants as is)
15
  # --- Constants ---
16
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
17
 
18
+ client = genai.Client(api_key=os.getenv("GOOGLE_API_KEY"))
19
+ model_name = "gemini-2.0-flash"
20
+
21
+ # --- Tools ---
22
+ @tool
23
+ def watch_video(video_url: str, user_query:str) -> str:
24
+ """
25
+ Watches a video and answers a question about it.
26
+ Args:
27
+ video_url (str): The URL of the video to watch.
28
+ user_query (str): The question to answer about the video.
29
+ Returns:
30
+ str: The answer to the question.
31
+ """
32
+ request_json = {
33
+ 'model': f'models/{model_name}',
34
+ 'contents': [{
35
+ "parts": [
36
+ {
37
+ 'fileData': {
38
+ 'fileUri': video_url
39
+ }
40
+ },
41
+ {
42
+ 'text': f"Please watch the video and answer the following question: {user_query}"
43
+ }
44
+ ]
45
+ }]
46
+ }
47
+ api_url = f'https://generativelanguage.googleapis.com/v1beta/models/{model_name}:generateContent?key={os.getenv("GOOGLE_API_KEY")}'
48
+ response = requests.post(
49
+ api_url,
50
+ json=request_json,
51
+ headers={
52
+ 'Content-Type': 'application/json',
53
+ }
54
+ )
55
+ if response.status_code != 200:
56
+ return f"Error: {response.status_code} - {response.text}"
57
+ response_json = response.json()
58
+ result_parts = response_json['candidates'][0]['content']['parts']
59
+ result = "".join([_.get('text', '') for _ in result_parts])
60
+ return result
61
+
62
+ @tool
63
+ def google_search(query: str) -> str:
64
+ """
65
+ Performs a Google search and returns the results.
66
+ Args:
67
+ query (str): The search query.
68
+ Returns:
69
+ str: The search results.
70
+ """
71
+ # This is a placeholder for the actual Google search logic
72
+ # In a real implementation, you would use the Google Search API
73
+ # to get the results.
74
+ # For now, we will just return a dummy response.
75
+ google_search_tool = types.Tool(
76
+ google_search=types.GoogleSearch()
77
+ )
78
+ response = client.models.generate_content(
79
+ model=model_name,
80
+ contents=f"Please search the internet for: {query}",
81
+ config=types.GenerateContentConfig(
82
+ tools=[google_search_tool],
83
+ response_modalities=['TEXT'],
84
+ )
85
+ )
86
+ result_parts = response.candidates[0].content.parts
87
+ result = "".join([_.text for _ in result_parts])
88
+
89
+ return result
90
+
91
+ @tool
92
+ def search_wikipedia_pages(query: str) -> str:
93
+ """
94
+ Searches for Wikipedia pages related to the query and returns the canonical titles of the related pages.
95
+ Args:
96
+ query (str): The search query.
97
+ Returns:
98
+ str: A comma separated list of canonical Wikipedia page titles that match the query.
99
+ """
100
+ response = wiki.search(query)
101
+ if len(response) > 0:
102
+ result = ", ".join(response)
103
+ else:
104
+ result = "No results found."
105
+ return result
106
+
107
+ @tool
108
+ def get_wikipedia_page(page_title: str) -> str:
109
+ """
110
+ Gets the content of a we
111
+ Args:
112
+ page_title (str): The canonical title of the Wikipedia page.
113
+ Returns:
114
+ str: Markdown-formatted content of the Wikipedia page.
115
+ """
116
+ # This is a placeholder for the actual Wikipedia search logic
117
+ # In a real implementation, you would use the Wikipedia API
118
+ # to get the results.
119
+ # For now, we will just return a dummy response.
120
+ try:
121
+ page = wiki.page(page_title)
122
+ except wiki.exceptions.PageError:
123
+ return f"Page '{page_title}' not found."
124
+ md_content = to_markdown(page.html())
125
+ return md_content
126
+
127
+ # --- Agent Management ---
128
+
129
+ class GeminiAgentContainer:
130
+ """
131
+ A container for the Gemini agent.
132
+ """
133
+ def __init__(self, model_id: Optional[str] = None, provider: Optional[str] = None):
134
+ # model_id = model_id or "Qwen/Qwen2.5-Coder-32B-Instruct"
135
+ # provider = provider or "together"
136
+ # model = HfApiModel(model_id=model_id, provider=provider)
137
+ self.model = LiteLLMModel(model_id=f"gemini/{model_name}", api_key=os.getenv("GOOGLE_API_KEY"))
138
+ system_prompt = """
139
+ You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template on a separate line: FINAL ANSWER: [YOUR FINAL ANSWER].
140
+ YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings.
141
+ 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.
142
+ 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.
143
+ If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.
144
+
145
+ Answer questions as literally as you can, making as few assumptions as possible. Restrict the answer to the narrowest definition that still satifies the question.
146
+ If you are provied with a video, please watch and summarize the entire video before answering the question. The correct answer may be present only in a few frames of the video.
147
+ If you have difficulty finding an answer on Wikipedia, you may search the internet using Google Search.
148
+ If you are asked to prove something, first state your assumptions and think step by step before giving your final answer.
149
+
150
+ """
151
+
152
+ self.agent = ToolCallingAgent(
153
+ model=self.model,
154
+ tools = [
155
+ watch_video,
156
+ google_search,
157
+ search_wikipedia_pages,
158
+ get_wikipedia_page,
159
+ ],
160
+ max_steps=5,
161
+ )
162
+ self.system_prompt = system_prompt
163
+
164
+
165
  def __call__(self, question: str) -> str:
166
+ response = self.agent.run(f"{self.system_prompt}\n{question}")
167
+ return response
 
 
168
 
169
  def run_and_submit_all( profile: gr.OAuthProfile | None):
170
  """
 
187
 
188
  # 1. Instantiate Agent ( modify this part to create your agent)
189
  try:
190
+ agent = GeminiAgentContainer()
191
  except Exception as e:
192
  print(f"Error instantiating agent: {e}")
193
  return f"Error initializing agent: {e}", None
 
216
  print(f"An unexpected error occurred fetching questions: {e}")
217
  return f"An unexpected error occurred fetching questions: {e}", None
218
 
219
+ def answer_question(question_data):
220
+ task_id = question_data.get("task_id")
221
+ question_text = question_data.get("question")
222
+ payload = None
223
+ submitted_answer = None
224
+ agent_error = None
225
+
 
 
 
226
  try:
227
+ if not task_id or question_text is None:
228
+ raise ValueError(f"Missing task_id or question in item: {question_data}")
229
  submitted_answer = agent(question_text)
230
+ payload = {"task_id": task_id, "submitted_answer": submitted_answer}
 
231
  except Exception as e:
232
  print(f"Error running agent on task {task_id}: {e}")
233
+ agent_error = f"AGENT ERROR: {e}"
234
+ finally:
235
+ log_entry = {
236
+ "Task ID": task_id,
237
+ "Question": question_text,
238
+ "Submitted Answer": submitted_answer or agent_error,
239
+ }
240
+ return payload, log_entry
241
+
242
+
243
 
244
+ # 3. Run your Agent
245
+ results_log = []
246
+ answers_payload = []
247
+ print(f"Running agent on {len(questions_data)} questions...")
248
+ for item in questions_data:
249
+ payload_data, log_entry = answer_question(item)
250
+ if payload_data:
251
+ answers_payload.append(payload_data)
252
+ results_log.append(log_entry)
253
+ time.sleep(1)
254
  if not answers_payload:
255
  print("Agent did not produce any answers to submit.")
256
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
requirements.txt CHANGED
@@ -1,2 +1,9 @@
1
  gradio
2
- requests
 
 
 
 
 
 
 
 
1
  gradio
2
+ requests
3
+ smolagents[litellm]
4
+ gradio[oauth]
5
+ google-api-python-client
6
+ google-genai
7
+ wikipedia
8
+ markdownify
9
+