File size: 10,748 Bytes
d8d83b8 10e9b7d d8d83b8 eccf8e4 3c4371f d8d83b8 10e9b7d d8d83b8 3db6293 e80aab9 31243f4 d8d83b8 31243f4 d8d83b8 31243f4 d8d83b8 31243f4 3c4371f eccf8e4 31243f4 7d65c66 31243f4 d8d83b8 7d65c66 d8d83b8 e80aab9 7d65c66 31243f4 7d65c66 31243f4 d8d83b8 31243f4 d8d83b8 31243f4 d8d83b8 e80aab9 7d65c66 e80aab9 31243f4 e80aab9 d8d83b8 e80aab9 d8d83b8 7d65c66 d8d83b8 e80aab9 d8d83b8 e80aab9 d8d83b8 31243f4 d8d83b8 7d65c66 d8d83b8 e80aab9 3c4371f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 | # -*- coding: utf-8 -*-
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
# URL da API responsável por fornecer as perguntas e receber o envio do benchmark.
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
class BasicAgent:
def __init__(self):
# 1. Carrega variáveis de ambiente (útil para testes locais, ignorado no HF Spaces)
load_dotenv()
# 2. Configuração do Langfuse (Opcional - não vai quebrar se falhar)
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.")
# Valida a presença da chave do Gemini (Obrigatório para o cérebro)
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.")
# 3. Inicialização do modelo LLM usando LiteLLM (Corrigido para Gemini 2.0 Flash)
self.model = LiteLLMModel(
model_id="gemini/gemini-2.0-flash",
api_key=gemini_key,
num_retries=3 # Resiliência contra o erro 429
)
# 4. Ferramenta de busca Web
@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}"
# 5. Ferramenta de transcrição de áudio via OpenAI Whisper
@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}."
# 6. Instanciação do CodeAgent
self.agent = CodeAgent(
tools=[busca_web, transcribe_audio],
model=self.model,
add_base_tools=False,
max_steps=10, # Adicionado limite de passos para evitar loops infinitos
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 com regras rígidas para o GAIA
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 # Reduzido de 3 para 2 para evitar estourar cota de tempo atoa
segundos_de_espera = 15
for tentativa in range(max_tentativas):
try:
resposta_final = self.agent.run(prompt_ajustado)
texto_resposta = str(resposta_final).strip()
# Sanitização
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):
# Resgata automaticamente os dados do Space atual
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)
# Interface Gradio
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) |