Update app.py
Browse filesAdded a function to download task file
app.py
CHANGED
|
@@ -9,6 +9,7 @@ from google import genai
|
|
| 9 |
# from google.genai.types import Tool, GoogleSearch
|
| 10 |
from google.genai import types
|
| 11 |
import asyncio
|
|
|
|
| 12 |
|
| 13 |
# (Keep Constants as is)
|
| 14 |
# --- Constants ---
|
|
@@ -34,7 +35,7 @@ class BasicAgent:
|
|
| 34 |
|
| 35 |
print("BasicAgent initialized.")
|
| 36 |
|
| 37 |
-
async def __call__(self, question: str) -> str:
|
| 38 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 39 |
fixed_answer = "This is a default answer."
|
| 40 |
# print(f"Agent returning fixed answer: {fixed_answer}")
|
|
@@ -42,17 +43,26 @@ class BasicAgent:
|
|
| 42 |
# return self.agent.run(question)
|
| 43 |
# Use the Google GenAI client to run the question
|
| 44 |
await asyncio.sleep(10)
|
| 45 |
-
|
| 46 |
model='gemini-2.0-flash',
|
| 47 |
contents=f'''Answer the following question in the format as requested. If the format not specified, then provide a single word/number/name.
|
| 48 |
-
|
| 49 |
config=types.GenerateContentConfig(
|
| 50 |
-
tools=[types.Tool(
|
| 51 |
-
|
| 52 |
-
)]
|
| 53 |
)
|
| 54 |
)
|
| 55 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 58 |
"""
|
|
@@ -115,7 +125,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 115 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 116 |
continue
|
| 117 |
try:
|
| 118 |
-
submitted_answer = asyncio.run(agent(question_text))
|
| 119 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 120 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 121 |
except Exception as e:
|
|
|
|
| 9 |
# from google.genai.types import Tool, GoogleSearch
|
| 10 |
from google.genai import types
|
| 11 |
import asyncio
|
| 12 |
+
import requests
|
| 13 |
|
| 14 |
# (Keep Constants as is)
|
| 15 |
# --- Constants ---
|
|
|
|
| 35 |
|
| 36 |
print("BasicAgent initialized.")
|
| 37 |
|
| 38 |
+
async def __call__(self, question: str, task_id: str) -> str:
|
| 39 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 40 |
fixed_answer = "This is a default answer."
|
| 41 |
# print(f"Agent returning fixed answer: {fixed_answer}")
|
|
|
|
| 43 |
# return self.agent.run(question)
|
| 44 |
# Use the Google GenAI client to run the question
|
| 45 |
await asyncio.sleep(10)
|
| 46 |
+
answer = self.client.models.generate_content(
|
| 47 |
model='gemini-2.0-flash',
|
| 48 |
contents=f'''Answer the following question in the format as requested. If the format not specified, then provide a single word/number/name.
|
| 49 |
+
In your response, do not include anything other than your answer. {question}''',
|
| 50 |
config=types.GenerateContentConfig(
|
| 51 |
+
tools=[types.Tool(google_search=types.GoogleSearch()),
|
| 52 |
+
types.Tool(code_execution=self.get_file)]
|
|
|
|
| 53 |
)
|
| 54 |
)
|
| 55 |
+
return answer.text
|
| 56 |
+
|
| 57 |
+
def get_file(self, task_id):
|
| 58 |
+
'''
|
| 59 |
+
Downloads the file associated with the given task_id, if one exists and is mapped.
|
| 60 |
+
If the question mentions an attachment, use this function.
|
| 61 |
+
'''
|
| 62 |
+
file_request = requests.get(url=f'https://agents-course-unit4-scoring.hf.space/files/{task_id}')
|
| 63 |
+
file_request.raise_for_status()
|
| 64 |
+
|
| 65 |
+
return file_request
|
| 66 |
|
| 67 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 68 |
"""
|
|
|
|
| 125 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 126 |
continue
|
| 127 |
try:
|
| 128 |
+
submitted_answer = asyncio.run(agent(question_text, task_id))
|
| 129 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 130 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 131 |
except Exception as e:
|