| |
| import os |
| import time |
| import requests |
| import pandas as pd |
| import gradio as gr |
| from dotenv import load_dotenv |
| from smolagents import CodeAgent, LiteLLMModel, tool |
|
|
| |
| DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space" |
|
|
| class BasicAgent: |
| def __init__(self): |
| |
| load_dotenv() |
|
|
| |
| try: |
| from langfuse import get_client |
| from openinference.instrumentation.smolagents import SmolagentsInstrumentor |
|
|
| langfuse_client = get_client() |
| if langfuse_client.auth_check(): |
| print("📡 Langfuse autenticado com sucesso!") |
| SmolagentsInstrumentor().instrument() |
| else: |
| print("⚠️ Langfuse ignorado (chaves ausentes).") |
| except Exception: |
| print("⚠️ Monitoramento do Langfuse desativado.") |
|
|
| |
| gemini_key = os.getenv("GEMINI_API_KEY") |
| if not gemini_key: |
| print("❌ ERRO: A variável 'GEMINI_API_KEY' não foi encontrada nos Secrets.") |
|
|
| |
| self.model = LiteLLMModel( |
| model_id="gemini/gemini-2.0-flash", |
| api_key=gemini_key, |
| num_retries=3 |
| ) |
|
|
| |
| @tool |
| def busca_web(query: str) -> str: |
| """Useful to search the web for up-to-date facts, Wikipedia articles, or general information. |
| Args: |
| query: The exact search query to look up on the internet. |
| """ |
| try: |
| headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/120.0.0.0 Safari/537.36'} |
| url = f"https://html.duckduckgo.com/html/?q={requests.utils.quote(query)}" |
| res = requests.get(url, headers=headers, timeout=15) |
| res.raise_for_status() |
|
|
| from bs4 import BeautifulSoup |
| soup = BeautifulSoup(res.text, 'html.parser') |
| snippets = [span.get_text() for span in soup.find_all('span', class_='result__snippet')] |
| |
| if not snippets: |
| return "No clear results found on the web for this query." |
| return "\n\n".join(snippets[:4]) |
| except Exception as e: |
| return f"Search failed due to network error: {e}" |
|
|
| |
| @tool |
| def transcribe_audio(file_path: str) -> str: |
| """Useful to transcribe any audio file (like MP3, WAV, M4A) into text. |
| Always use this tool first when a question involves understanding audio. |
| Args: |
| file_path: The local path to the audio file (e.g., 'audio.mp3'). |
| """ |
| openai_key = os.getenv("OPENAI_API_KEY") |
| if not openai_key: |
| return "Error: OPENAI_API_KEY not found in secrets. Cannot transcribe audio." |
| |
| try: |
| import openai |
| client = openai.OpenAI(api_key=openai_key) |
| with open(file_path, "rb") as audio_file: |
| transcript = client.audio.transcriptions.create( |
| model="whisper-1", |
| file=audio_file |
| ) |
| return f"Audio Transcription Content:\n{transcript.text}" |
| except Exception as e: |
| return f"Error transcribing audio: {e}." |
|
|
| |
| self.agent = CodeAgent( |
| tools=[busca_web, transcribe_audio], |
| model=self.model, |
| add_base_tools=False, |
| max_steps=10, |
| additional_authorized_imports=[ |
| "requests", "pydub", "wave", "openai", |
| "PIL", "pdfplumber", "pypdf", |
| "json", "csv", "openpyxl", "pandas", |
| "os", "pathlib", "zipfile", |
| "math", "datetime", "re", "itertools", "bs4" |
| ] |
| ) |
|
|
| def __call__(self, question: str) -> str: |
| """Permite chamar o agente diretamente passando a pergunta.""" |
| print(f"Agent received question (first 50 chars): {question[:50]}...") |
|
|
| |
| prompt_ajustado = ( |
| f"TASK TO SOLVE: {question}\n\n" |
| "EXECUTION RULES:\n" |
| "1. You MUST solve this task step-by-step using Python code.\n" |
| "2. Every single response you generate MUST strictly follow this exact grammar:\n" |
| "Thoughts: <your reasoning here>\n" |
| "<code>\n" |
| "# your python code here using available tools\n" |
| "</code>\n" |
| "3. NEVER write conversational text or explanations outside of the 'Thoughts' or '<code>' sections.\n" |
| "4. To finish the task and deliver the answer, you MUST call the `final_answer` tool inside a code block.\n" |
| "5. CRITICAL FOR GAIA BENCHMARK (EXACT MATCH STRICT RULE):\n" |
| "Inside the `final_answer()` tool, pass ONLY the raw string or number value matching the exact required format. Do NOT add labels or conversational prefixes.\n\n" |
| "FEW-SHOT EXAMPLES OF EXPECTED FINAL ANSWERS:\n" |
| "- Question: What was the actual enrollment count of the clinical trial on H. pylori in acne vulgaris patients from Jan-May 2018 as listed on the NIH website?\n" |
| " Correct Call: final_answer(90) or final_answer('90')\n\n" |
| "- Question: If this whole pint is made up of ice cream, how many percent above or below the US federal standards for butterfat content is it when using the standards as reported by Wikipedia in 2020? Answer as + or - a number rounded to one decimal place.\n" |
| " Correct Call: final_answer('+4.6')\n\n" |
| "- Question: In NASA's Astronomy Picture of the Day on 2006 January 21, two astronauts are visible... Give the last name of the astronaut, separated from the number of minutes by a semicolon.\n" |
| " Correct Call: final_answer('White; 5876')\n\n" |
| "6. WEB REQUESTS: Always provide a User-Agent header when using `requests.get()` to avoid 403 Forbidden errors." |
| ) |
|
|
| max_tentativas = 2 |
| segundos_de_espera = 15 |
|
|
| for tentativa in range(max_tentativas): |
| try: |
| resposta_final = self.agent.run(prompt_ajustado) |
| texto_resposta = str(resposta_final).strip() |
|
|
| |
| prefixos_para_remover = [ |
| "final answer:", "final answer", |
| "the final answer is:", "the final answer is", |
| "answer:", "the answer is:" |
| ] |
| texto_lower = texto_resposta.lower() |
| for prefixo in prefixos_para_remover: |
| if texto_lower.startswith(prefixo): |
| texto_resposta = texto_resposta[len(prefixo):].strip() |
| texto_lower = texto_resposta.lower() |
|
|
| texto_resposta = texto_resposta.strip(" \t\n\r:.\"'") |
| return texto_resposta |
|
|
| except Exception as e: |
| print(f"⚠️ Falha na tentativa {tentativa + 1}/{max_tentativas}: {e}") |
| if tentativa < max_tentativas - 1: |
| time.sleep(segundos_de_espera) |
| else: |
| return f"Erro definitivo da API: {e}" |
|
|
| def run_and_submit_all(profile: gr.OAuthProfile | None): |
| |
| username = os.getenv("SPACE_AUTHOR_NAME", "marantmir") |
| space_id = os.getenv("SPACE_ID", f"{username}/Final_Assignment_Template") |
| agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" |
|
|
| questions_url = f"{DEFAULT_API_URL}/questions" |
| submit_url = f"{DEFAULT_API_URL}/submit" |
|
|
| try: |
| agent = BasicAgent() |
| except Exception as e: |
| return f"Error initializing agent: {e}", None |
|
|
| try: |
| response = requests.get(questions_url, timeout=15) |
| response.raise_for_status() |
| questions_data = response.json() |
| if not questions_data: |
| return "Fetched questions list is empty.", None |
| except Exception as e: |
| return f"Error fetching questions: {e}", None |
|
|
| results_log = [] |
| answers_payload = [] |
| for item in questions_data: |
| task_id = item.get("task_id") |
| question_text = item.get("question") |
| if not task_id or question_text is None: |
| continue |
| try: |
| submitted_answer = agent(question_text) |
| answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer}) |
| results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer}) |
| except Exception as e: |
| results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"ERROR: {e}"}) |
|
|
| if not answers_payload: |
| return "Agent did not produce any answers.", pd.DataFrame(results_log) |
|
|
| submission_data = {"username": username, "agent_code": agent_code, "answers": answers_payload} |
|
|
| try: |
| response = requests.post(submit_url, json=submission_data, timeout=60) |
| response.raise_for_status() |
| result_data = response.json() |
| final_status = ( |
| f"Submission Successful!\n" |
| f"User: {result_data.get('username')}\n" |
| f"Overall Score: {result_data.get('score', 'N/A')}%\n" |
| f"Message: {result_data.get('message', '')}" |
| ) |
| return final_status, pd.DataFrame(results_log) |
| except Exception as e: |
| return f"Submission Failed: {e}", pd.DataFrame(results_log) |
|
|
| |
| with gr.Blocks() as demo: |
| gr.Markdown("# GAIA Benchmark - SmolAgents Runner") |
| run_button = gr.Button("Run Evaluation & Submit All Answers") |
| status_output = gr.Textbox(label="Run Status", interactive=False) |
| results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True) |
| run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table]) |
|
|
| if __name__ == "__main__": |
| demo.launch(debug=True, share=False) |