Spaces:
Runtime error
Runtime error
Download agent.py from InacioPina/Final_Assignment_Template: direct link, hf CLI and curl.
- Browser
- Download file 3.55 kB
-
https://huggingface.co/spaces/InacioPina/Final_Assignment_Template/resolve/main/agent.py
- Command line
-
hf download hf://spaces/InacioPina/Final_Assignment_Template/agent.py
-
curl -L -o agent.py https://huggingface.co/spaces/InacioPina/Final_Assignment_Template/resolve/main/agent.py
3.55 kB
| import mimetypes | |
| import os | |
| import re | |
| import requests | |
| from google import genai | |
| from google.genai import types | |
| SCORING_API = os.getenv("GAIA_API_URL", "https://agents-course-unit4-scoring.hf.space") | |
| MODEL_NAME = os.getenv("GEMINI_MODEL", "gemini-2.5-flash") | |
| SYSTEM_INSTRUCTION = """ | |
| You are a research agent solving GAIA benchmark questions. | |
| Use Google Search grounding to verify factual claims and prefer primary sources. | |
| Reason carefully through multi-step questions, dates, units, calculations, and requested output formatting. | |
| The user may refer to an attached file associated with the task. Inspect it if available. | |
| Return ONLY the answer requested by the question: no explanation, no citations, no markdown, | |
| no prefix such as 'Final answer'. If a number is requested, output the number only unless | |
| the question explicitly asks for units or symbols. If a list is requested, use the exact | |
| separator and order requested. Do not invent facts. If you cannot determine the answer, | |
| give the most defensible concise answer rather than a fabricated explanation. | |
| """ | |
| def clean_answer(value): | |
| text = str(value or "").strip() | |
| text = re.sub(r"^\s*(final\s+answer|answer)\s*:\s*", "", text, flags=re.I) | |
| text = text.strip().strip("`").strip() | |
| text = re.sub(r"\s+", " ", text) | |
| # Remove wrapping quotes only; preserve punctuation that may be part of the expected answer. | |
| if len(text) >= 2 and text[0] == text[-1] and text[0] in ("'", '"'): | |
| text = text[1:-1].strip() | |
| return text | |
| class GaiaGeminiAgent: | |
| def __init__(self): | |
| api_key = os.getenv("GEMINI_API_KEY") | |
| if not api_key: | |
| raise RuntimeError("GEMINI_API_KEY não está configurada.") | |
| self.client = genai.Client(api_key=api_key) | |
| def _attachment_parts(self, task_id): | |
| """Download task attachment using the course API; return Gemini-compatible parts.""" | |
| if not task_id: | |
| return [] | |
| url = f"{SCORING_API}/files/{task_id}" | |
| try: | |
| response = requests.get(url, timeout=45) | |
| if response.status_code != 200 or not response.content: | |
| return [] | |
| content_type = response.headers.get("content-type", "").split(";")[0].strip() | |
| if not content_type or content_type == "application/octet-stream": | |
| guessed, _ = mimetypes.guess_type(response.url) | |
| content_type = guessed or "application/octet-stream" | |
| # Gemini inline data has size limits; skip unexpectedly large attachments rather than crashing. | |
| if len(response.content) > 18 * 1024 * 1024: | |
| return [types.Part.from_text(text="[Attachment was larger than 18 MB and could not be attached inline.]")] | |
| return [types.Part.from_bytes(data=response.content, mime_type=content_type)] | |
| except Exception: | |
| return [] | |
| def solve(self, question, task_id=""): | |
| contents = [ | |
| "Question:\n" + question, | |
| ] | |
| contents.extend(self._attachment_parts(task_id)) | |
| response = self.client.models.generate_content( | |
| model=MODEL_NAME, | |
| contents=contents, | |
| config=types.GenerateContentConfig( | |
| system_instruction=SYSTEM_INSTRUCTION, | |
| temperature=0.1, | |
| tools=[types.Tool(google_search=types.GoogleSearch())], | |
| ), | |
| ) | |
| answer = clean_answer(response.text) | |
| if not answer: | |
| raise RuntimeError("Gemini retornou uma resposta vazia.") | |
| return answer | |