Spaces:
No application file
No application file
Upload resolver_benchmark.py
Browse files- resolver_benchmark.py +112 -0
resolver_benchmark.py
ADDED
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import json
|
3 |
+
import requests
|
4 |
+
from smolagents import CodeAgent, HfApiModel
|
5 |
+
from smolagents.tools.web_search import DuckDuckGoSearchTool
|
6 |
+
from smolagents.tools.python_interpreter import PythonInterpreterTool
|
7 |
+
|
8 |
+
# ==============================================================================
|
9 |
+
# --- CONFIGURACI脫N ACTUALIZADA CON TUS DATOS ---
|
10 |
+
# ==============================================================================
|
11 |
+
# URL de la API del benchmark (reemplaza si es diferente)
|
12 |
+
API_URL = "https://huggingface.co/spaces/leondurb/gaia_leaderboard"
|
13 |
+
|
14 |
+
# Tu nombre de usuario de Hugging Face
|
15 |
+
TU_USERNAME_DE_HF = "Diego2106"
|
16 |
+
|
17 |
+
# El enlace al c贸digo de tu Space en Hugging Face
|
18 |
+
TU_LINK_AL_CODIGO = "https://huggingface.co/spaces/Diego2106/MyAgentGAIA/tree/main"
|
19 |
+
# ==============================================================================
|
20 |
+
|
21 |
+
|
22 |
+
def crear_agente_gaia():
|
23 |
+
"""Crea y configura el agente que resolver谩 las preguntas."""
|
24 |
+
modelo = HfApiModel(model_id="mistralai/Mistral-7B-Instruct-v0.2")
|
25 |
+
herramientas = [
|
26 |
+
DuckDuckGoSearchTool(),
|
27 |
+
PythonInterpreterTool()
|
28 |
+
]
|
29 |
+
agente = CodeAgent(
|
30 |
+
model=modelo,
|
31 |
+
tools=herramientas,
|
32 |
+
# Prompt para asegurar que la respuesta sea directa y sin texto extra
|
33 |
+
system_prompt_template="Responde 煤nicamente con la respuesta final, sin explicaciones ni texto adicional."
|
34 |
+
)
|
35 |
+
return agente
|
36 |
+
|
37 |
+
def resolver_benchmark():
|
38 |
+
"""Funci贸n principal que ejecuta todo el proceso."""
|
39 |
+
|
40 |
+
# --- 1. Obtener las preguntas de la API ---
|
41 |
+
print("Paso 1: Obteniendo preguntas de la API...")
|
42 |
+
try:
|
43 |
+
response = requests.get(f"{API_URL}/questions")
|
44 |
+
response.raise_for_status()
|
45 |
+
preguntas = response.json()
|
46 |
+
print(f"Se obtuvieron {len(preguntas)} preguntas.")
|
47 |
+
except requests.exceptions.RequestException as e:
|
48 |
+
print(f"Error al obtener las preguntas: {e}")
|
49 |
+
return
|
50 |
+
|
51 |
+
# --- 2. Crear el agente y la lista de respuestas ---
|
52 |
+
agente = crear_agente_gaia()
|
53 |
+
lista_de_respuestas = []
|
54 |
+
|
55 |
+
# Directorio para guardar archivos de las preguntas
|
56 |
+
if not os.path.exists("temp_files"):
|
57 |
+
os.makedirs("temp_files")
|
58 |
+
|
59 |
+
# --- 3. Procesar cada pregunta ---
|
60 |
+
print("\nPaso 2: Procesando cada pregunta con el agente (esto puede tardar mucho)...")
|
61 |
+
for i, tarea in enumerate(preguntas):
|
62 |
+
task_id = tarea['task_id']
|
63 |
+
pregunta_texto = tarea['Question']
|
64 |
+
file_name = tarea.get('file_name')
|
65 |
+
|
66 |
+
prompt_para_agente = pregunta_texto
|
67 |
+
|
68 |
+
# Si la pregunta necesita un archivo, lo descargamos
|
69 |
+
if file_name:
|
70 |
+
print(f" - La pregunta {task_id} necesita el archivo: {file_name}. Descargando...")
|
71 |
+
try:
|
72 |
+
file_response = requests.get(f"{API_URL}/files/{file_name}")
|
73 |
+
file_response.raise_for_status()
|
74 |
+
|
75 |
+
ruta_archivo_local = os.path.join("temp_files", file_name)
|
76 |
+
with open(ruta_archivo_local, 'wb') as f:
|
77 |
+
f.write(file_response.content)
|
78 |
+
|
79 |
+
prompt_para_agente += f"\n\nContexto adicional: Usa el archivo que se encuentra en la ruta '{ruta_archivo_local}'."
|
80 |
+
print(f" - Archivo descargado en: {ruta_archivo_local}")
|
81 |
+
|
82 |
+
except requests.exceptions.RequestException as e:
|
83 |
+
print(f" - !! Error al descargar el archivo {file_name}: {e}")
|
84 |
+
continue
|
85 |
+
|
86 |
+
# Ejecutar el agente
|
87 |
+
print(f" - Resolviendo pregunta {i+1}/{len(preguntas)} (ID: {task_id})...")
|
88 |
+
try:
|
89 |
+
respuesta_agente = agente.run(prompt_para_agente)
|
90 |
+
print(f" - Respuesta obtenida: {respuesta_agente}")
|
91 |
+
|
92 |
+
lista_de_respuestas.append({
|
93 |
+
"task_id": task_id,
|
94 |
+
"submitted_answer": str(respuesta_agente)
|
95 |
+
})
|
96 |
+
except Exception as e:
|
97 |
+
print(f" - !! Error al ejecutar el agente en la tarea {task_id}: {e}")
|
98 |
+
lista_de_respuestas.append({
|
99 |
+
"task_id": task_id,
|
100 |
+
"submitted_answer": "ERROR"
|
101 |
+
})
|
102 |
+
|
103 |
+
# --- 4. Guardar las respuestas en un archivo local (como respaldo) ---
|
104 |
+
print("\nPaso 3: Guardando las respuestas en 'submission.jsonl' como respaldo...")
|
105 |
+
with open("submission.jsonl", 'w') as f:
|
106 |
+
for respuesta in lista_de_respuestas:
|
107 |
+
f.write(json.dumps(respuesta) + '\n')
|
108 |
+
print("Respaldo guardado.")
|
109 |
+
|
110 |
+
# --- 5. Enviar los resultados a la API ---
|
111 |
+
print("\nPaso 4: Enviando los resultados para calificaci贸n...")
|
112 |
+
payload = {
|