ashishja commited on
Commit
b37e524
·
verified ·
1 Parent(s): 39ca98b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +103 -153
app.py CHANGED
@@ -8,60 +8,30 @@ import google.genai.types as types
8
  import requests
9
  from google.adk.events import Event, EventActions
10
  from google.adk.agents.invocation_context import InvocationContext
11
- from typing import AsyncGenerator
12
  from google.genai import types as genai_types
13
  from google.adk.tools import ToolContext, FunctionTool
14
  import logging
15
- #from google.adk.tools import built_in_code_execution
16
  from google.adk.tools import agent_tool
17
-
18
-
19
-
20
-
21
-
22
-
23
-
24
 
25
  logging.basicConfig(level=logging.ERROR)
26
- #from google.adk.tools import agent_tool
27
  url = 'https://agents-course-unit4-scoring.hf.space/questions'
28
  headers = {'accept': 'application/json'}
29
  response = requests.get(url, headers=headers)
30
 
31
- # class responses_api(BaseAgent):
32
- # async def _run_async_impl(self, ctx: InvocationContext)-> AsyncGenerator[Event, None]:
33
- # # This method is called when the agent is run
34
- # # You can implement your logic here
35
- # # For example, you can call an external API or perform some calculations
36
- # # and return the result
37
- # url = 'https://agents-course-unit4-scoring.hf.space/questions'
38
- # headers = {'accept': 'application/json'}
39
- # response = requests.get(url, headers=headers)
40
- # for i in response.json():
41
- # if i['file_name'] != '':
42
- # url_file = f"https://agents-course-unit4-scoring.hf.space/files/{i['task_id']}"
43
- # question = i['question']
44
- # prompt = f"{question} and the file is {url_file}, give the final answer only"
45
- # else:
46
- # question = i['question']
47
- # prompt = f"{question} give the final answer only"
48
- # existing_responses = ctx.session.state.get("user:responses", [])
49
- # existing_responses.append(prompt)
50
- # ctx.session_state["user:responses"] = existing_responses
51
-
52
- # # Optionally, yield a single event to indicate completion or provide some output
53
- # yield Event(author=self.name, content=types.Content(parts=[types.Part(text=f"Fetched {len(questions_data)} questions.")]))
54
-
55
-
56
-
57
  def answer_questions():
 
58
  url = 'https://agents-course-unit4-scoring.hf.space/questions'
59
  headers = {'accept': 'application/json'}
60
  response = requests.get(url, headers=headers)
 
61
  prompts = []
62
  for i in response.json():
63
  task_id = i['task_id']
64
- if i['file_name'] != '':
65
  url_file = f"https://agents-course-unit4-scoring.hf.space/files/{i['task_id']}"
66
  question = i['question']
67
  prompt = f"{task_id}:{question} and the file is {url_file}, give the final answer only"
@@ -70,27 +40,28 @@ def answer_questions():
70
  prompt = f"{task_id}:{question} give the final answer only"
71
  prompts.append(prompt)
72
  return prompts
73
- #responses_api = responses_api(name= 'responses_api_1')
74
- from typing import Dict, Any
75
- def submit_questions(answers: list[str]) -> Dict[str, Any]:
76
  url = 'https://agents-course-unit4-scoring.hf.space/submit'
77
  payload = {
78
- "username": "ashishja",
79
- "agent_code": "https://huggingface.co/spaces/ashishja/Agents_Course_Final_Assignment_Ashish",
80
- "answers": answers}
 
81
  headers = {'accept': 'application/json', "Content-Type": "application/json"}
82
- response = requests.post(url, headers=headers, json =payload)
83
  import json
 
84
  print(json.dumps(payload, indent=2))
85
  if response.status_code == 200:
 
86
  return response.json()
87
  else:
 
88
  response.raise_for_status()
89
 
90
-
91
-
92
-
93
- responses_api = FunctionTool(func= answer_questions)
94
  submit_api = FunctionTool(func=submit_questions)
95
 
96
  # class QuestionAnswerer(LlmAgent):
@@ -99,147 +70,126 @@ submit_api = FunctionTool(func=submit_questions)
99
  # for q in questions_to_answer:
100
  # answer = await self._llm(messages=[types.ChatMessage(role="user", parts=[types.Part(text=q)])])
101
  # yield Event(author=self.name, content=answer.content)
 
 
102
 
103
- # qa = QuestionAnswerer(name = 'qa_1', model="gemini-2.0-flash", description="Question Answerer")
104
-
105
-
106
-
107
-
108
-
109
-
110
-
111
-
112
- APP_NAME="weather_sentiment_agent"
113
  USER_ID="user1234"
114
- SESSION_ID="1234"
115
-
116
 
117
  code_agent = LlmAgent(
118
  name='codegaiaAgent',
119
- model="gemini-2.5-pro-preview-05-06",
120
  description=(
121
- "You are a smart agent that can write and execute code and answer any questions provided access the given files and answer"
122
  ),
123
- instruction = (
124
- "if the question contains a file with .py ,Get the code file and depending on the question and the file provided, execute the code and provide the final answer. "
125
- "If the question contains a spreadsheet file like .xlsx and .csv among others, get the file and depending on the question and the file provided, execute the code and provide the final answer. "
126
- "use code like import pandas as pd , file = pd.read_csv('file.csv') and then use the file to answer the question. "
127
- "if the question contains a file with .txt ,Get the code file and depending on the question and the file provided, execute the code and provide the final answer. "
128
- "if the question contains a file with .json ,Get the code file and depending on the question and the file provided, execute the code and provide the final answer. "
129
- "If you are writing code or if you get a code file, use the code execution tool to run the code and provide the final answer. "
130
- )
131
-
132
- ,
133
- # tools=[built_in_code_execution],
134
- # Add the responses_api agent as a tool
135
- # sub_agents=[responses_api]
136
  )
137
 
138
-
139
  search_agent = LlmAgent(
140
  name='searchgaiaAgent',
141
- model="gemini-2.5-pro-preview-05-06",
142
  description=(
143
- "You are a smart agent that can search the web and answer any questions provided access the given files and answer"
144
  ),
145
- instruction = (
146
- "Get the url associated perform a search and consolidate the information provided and answer the provided question "
147
- )
148
-
149
- ,
150
- tools=[google_search],
151
- # Add the responses_api agent as a tool
152
- # sub_agents=[responses_api]
153
  )
154
 
155
  image_agent = LlmAgent(
156
  name='imagegaiaAgent',
157
- model="gemini-2.5-pro-preview-05-06",
158
  description=(
159
- "You are a smart agent that can when given a image file and answer any questions related to it"
 
 
 
160
  ),
161
- instruction = (
162
- "Get the image file from the link associated in the prompt use Gemini to watch the video and answer the provided question ")
163
-
164
- ,
165
- # tools=[google_search],
166
- # # Add the responses_api agent as a tool
167
- # sub_agents=[responses_api]
168
  )
169
 
170
-
171
  youtube_agent = LlmAgent(
172
  name='youtubegaiaAgent',
173
- model="gemini-2.5-pro-preview-05-06",
174
  description=(
175
- "You are a smart agent that can when given a youtube link watch it and answer any questions related to it"
 
 
 
176
  ),
177
- instruction = (
178
- "Get the youtube link associated use Gemini to watch the video and answer the provided question ")
179
-
180
- ,
181
- tools=[google_search],
182
- # Add the responses_api agent as a tool
183
- # sub_agents=[responses_api]
184
  )
185
 
186
  root_agent = LlmAgent(
187
  name='basegaiaAgent',
188
- model="gemini-2.5-pro-preview-05-06",
189
  description=(
190
- "You are a smart agent that can answer any questions provided access the given files and answer"
191
  ),
192
- instruction = (
193
- "You are a helpful agent. When the user asks to get the questions or makes a similar request, "
194
- "invoke your tool 'responses_api' to retrieve the questions. "
195
- "Once you receive the list of questions, loop over each question and provide a concise answer for each based on the question and any provided file. "
196
- "For every answer, return a dictionary with the keys task_id and submitted_answer, for example: "
197
- "{'task_id': 'the-task-id', 'submitted_answer': 'your answer'}. "
198
- "Collect all such dictionaries in a list (do not include any backslashes), and pass this list to the 'submit_api' tool to submit the answers."
199
- )
200
-
201
- ,
202
- tools=[responses_api,submit_api,agent_tool.AgentTool(agent = code_agent),\
203
- agent_tool.AgentTool(agent = search_agent), agent_tool.AgentTool(youtube_agent), agent_tool.AgentTool(image_agent)],
204
- # Add the responses_api agent as a tool
205
- # sub_agents=[responses_api]
 
 
206
  )
207
 
208
- # root_agent = LlmAgent(
209
- # name='gaiaAgent',
210
- # model="gemini-2.5-pro-preview-05-06",
211
- # description=(
212
- # "You are a smart agent that can answer any questions provided access the given files and answer"
213
- # ),
214
- # instruction = (
215
- # "You are a helpful agent. When the user asks to get the questions or makes a similar request, "
216
- # "invoke base agent. "
217
- # "Once you the answers check if are in correct format. "
218
- # #"Collect all such dictionaries in a list (do not include any backslashes), and pass this list to the 'submit_api' tool to submit the answers."
219
- # )
220
-
221
- # ,
222
- # #tools=[submit_api],
223
- # # Add the responses_api agent as a tool
224
- # sub_agents=[base_agent]
225
- # )
226
-
227
  session_service = InMemorySessionService()
228
- session = session_service.create_session(app_name=APP_NAME, \
229
- user_id=USER_ID,\
230
- session_id=SESSION_ID)
231
- import asyncio
232
- runner = Runner(agent=root_agent, app_name=APP_NAME, session_service= session_service)
233
- def send_query_to_agent(root_agent, query, session):
234
- session = session
235
- content = types.Content(role='user', parts=[types.Part(text=query)])
236
-
237
-
238
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
239
 
240
  async def main():
 
241
  await process_questions_and_answer()
242
 
243
  if __name__ == "__main__":
244
- import asyncio
245
- asyncio.run(main())
 
8
  import requests
9
  from google.adk.events import Event, EventActions
10
  from google.adk.agents.invocation_context import InvocationContext
11
+ from typing import AsyncGenerator, Dict, Any
12
  from google.genai import types as genai_types
13
  from google.adk.tools import ToolContext, FunctionTool
14
  import logging
15
+ from google.adk.tools import built_in_code_execution
16
  from google.adk.tools import agent_tool
17
+ import asyncio
 
 
 
 
 
 
18
 
19
  logging.basicConfig(level=logging.ERROR)
20
+
21
  url = 'https://agents-course-unit4-scoring.hf.space/questions'
22
  headers = {'accept': 'application/json'}
23
  response = requests.get(url, headers=headers)
24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  def answer_questions():
26
+ """Fetches questions from the scoring API."""
27
  url = 'https://agents-course-unit4-scoring.hf.space/questions'
28
  headers = {'accept': 'application/json'}
29
  response = requests.get(url, headers=headers)
30
+ response.raise_for_status() # Raise an exception for bad status codes
31
  prompts = []
32
  for i in response.json():
33
  task_id = i['task_id']
34
+ if i['file_name']:
35
  url_file = f"https://agents-course-unit4-scoring.hf.space/files/{i['task_id']}"
36
  question = i['question']
37
  prompt = f"{task_id}:{question} and the file is {url_file}, give the final answer only"
 
40
  prompt = f"{task_id}:{question} give the final answer only"
41
  prompts.append(prompt)
42
  return prompts
43
+
44
+ def submit_questions(answers: list[Dict[str, Any]]) -> Dict[str, Any]:
45
+ """Submits the collected answers to the scoring API."""
46
  url = 'https://agents-course-unit4-scoring.hf.space/submit'
47
  payload = {
48
+ "username": "ashishja",
49
+ "agent_code": "https://huggingface.co/spaces/ashishja/Agents_Course_Final_Assignment_Ashish",
50
+ "answers": answers
51
+ }
52
  headers = {'accept': 'application/json', "Content-Type": "application/json"}
53
+ response = requests.post(url, headers=headers, json=payload)
54
  import json
55
+ print("Submitting the following payload:")
56
  print(json.dumps(payload, indent=2))
57
  if response.status_code == 200:
58
+ print("Submission successful!")
59
  return response.json()
60
  else:
61
+ print(f"Submission failed with status {response.status_code}: {response.text}")
62
  response.raise_for_status()
63
 
64
+ responses_api = FunctionTool(func=answer_questions)
 
 
 
65
  submit_api = FunctionTool(func=submit_questions)
66
 
67
  # class QuestionAnswerer(LlmAgent):
 
70
  # for q in questions_to_answer:
71
  # answer = await self._llm(messages=[types.ChatMessage(role="user", parts=[types.Part(text=q)])])
72
  # yield Event(author=self.name, content=answer.content)
73
+ #
74
+ # qa = QuestionAnswerer(name = 'qa_1', model="gemini-1.5-flash-latest", description="Question Answerer")
75
 
76
+ APP_NAME="final_assignment_agent"
 
 
 
 
 
 
 
 
 
77
  USER_ID="user1234"
78
+ SESSION_ID="5678"
 
79
 
80
  code_agent = LlmAgent(
81
  name='codegaiaAgent',
82
+ model="gemini-2.0-flash",
83
  description=(
84
+ "You are a smart agent that can write and execute code to answer questions. Use this for questions involving code files (.py) or data files (.csv, .xlsx, .json, .txt)."
85
  ),
86
+ instruction=(
87
+ "If the question contains a file with .py, get the code file and, depending on the question and the file provided, execute the code and provide the final answer. "
88
+ "If the question contains a spreadsheet file like .xlsx or .csv, get the file, use pandas to analyze it, and provide the final answer. "
89
+ "If the question contains a file with .txt or .json, get the file and use code to parse it and answer the question. "
90
+ "Always use the code execution tool to run your code and provide only the final answer."
91
+ ),
92
+ tools=[built_in_code_execution],
 
 
 
 
 
 
93
  )
94
 
 
95
  search_agent = LlmAgent(
96
  name='searchgaiaAgent',
97
+ model="gemini-2.0-flash",
98
  description=(
99
+ "You are a smart agent that can search the web to answer questions."
100
  ),
101
+ instruction=(
102
+ "Get the URL associated with the question, perform a web search, consolidate the information, and answer the provided question."
103
+ ),
104
+ tools=[google_search],
 
 
 
 
105
  )
106
 
107
  image_agent = LlmAgent(
108
  name='imagegaiaAgent',
109
+ model="gemini-2.0-flash",
110
  description=(
111
+ "You are a smart agent that can analyze an image file and answer any questions related to it."
112
+ ),
113
+ instruction=(
114
+ "Get the image file from the link provided in the prompt. Use your multimodal capabilities to understand the image and answer the question."
115
  ),
 
 
 
 
 
 
 
116
  )
117
 
 
118
  youtube_agent = LlmAgent(
119
  name='youtubegaiaAgent',
120
+ model="gemini-2.0-flash",
121
  description=(
122
+ "You are a smart agent that can watch a YouTube video and answer any questions related to it."
123
+ ),
124
+ instruction=(
125
+ "Get the YouTube link from the prompt. Use your multimodal capabilities to watch the video and answer the provided question."
126
  ),
 
 
 
 
 
 
 
127
  )
128
 
129
  root_agent = LlmAgent(
130
  name='basegaiaAgent',
131
+ model="gemini-2.0-flash",
132
  description=(
133
+ "You are a master agent that orchestrates sub-agents to answer various types of questions."
134
  ),
135
+ instruction=(
136
+ "You are a helpful orchestrator agent. Your primary goal is to answer a series of questions and submit them. "
137
+ "First, invoke your tool 'answer_questions' to retrieve the list of questions. "
138
+ "Once you receive the list, iterate through each question. For each one, delegate to the most appropriate sub-agent (code, search, youtube, image) based on its description and the question's content (e.g., file type). "
139
+ "After getting the answer from the sub-agent, format it into a dictionary with 'task_id' and 'submitted_answer' keys. The task_id is at the beginning of each question string, separated by a colon. "
140
+ "Collect all these answer dictionaries into a single list. "
141
+ "Finally, pass this complete list of dictionaries to the 'submit_questions' tool to submit all answers at once."
142
+ ),
143
+ tools=[
144
+ responses_api,
145
+ submit_api,
146
+ agent_tool.AgentTool(agent=code_agent),
147
+ agent_tool.AgentTool(agent=search_agent),
148
+ agent_tool.AgentTool(agent=youtube_agent),
149
+ agent_tool.AgentTool(agent=image_agent)
150
+ ],
151
  )
152
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
153
  session_service = InMemorySessionService()
154
+ runner = Runner(agent=root_agent, app_name=APP_NAME, session_service=session_service)
155
+
156
+ async def process_questions_and_answer():
157
+ """
158
+ Orchestrates the entire process of fetching questions, answering them
159
+ using the agent, and submitting the final answers.
160
+ """
161
+ session = await session_service.create_session(
162
+ app_name=APP_NAME, user_id=USER_ID, session_id=SESSION_ID
163
+ )
164
+ print(f"===== Application Startup at {session.create_time} =====")
165
+ print(f"Session created: {session.session_id}")
166
+
167
+ # Initial prompt to kick off the agent's task
168
+ initial_prompt = "Please get all the questions, answer each one by delegating to the correct tool or sub-agent, format the answers, and then submit the final list."
169
+
170
+ print("\nSending initial prompt to the agent...")
171
+ print(f"Prompt: '{initial_prompt}'")
172
+
173
+ # Run the agent and stream events
174
+ async for event in runner.run_async(
175
+ session_id=session.session_id,
176
+ content=types.Content(role="user", parts=[types.Part(text=initial_prompt)]),
177
+ ):
178
+ if event.action == EventActions.AGENT_RESPONSE and event.author == root_agent.name:
179
+ if event.content and event.content.parts:
180
+ print(f"\nFinal Agent Response: {event.content.parts[0].text}")
181
+ elif event.action == EventActions.TOOL_OUTPUT:
182
+ if event.content and event.content.parts and event.content.parts[0].tool_output:
183
+ tool_output = event.content.parts[0].tool_output
184
+ print(f"\n<-- Tool Output from {tool_output.tool_name}:")
185
+ for key, value in tool_output.data.items():
186
+ print(f" {key}: {value}")
187
+
188
+ print("\n===== Task Complete =====")
189
 
190
  async def main():
191
+ """Main entry point for the application."""
192
  await process_questions_and_answer()
193
 
194
  if __name__ == "__main__":
195
+ asyncio.run(main())