dsfa
Browse files
app.py
CHANGED
|
@@ -7,52 +7,50 @@ from smolagents import CodeAgent
|
|
| 7 |
from smolagents.models import InferenceClientModel
|
| 8 |
from smolagents.tools import (
|
| 9 |
DuckDuckGoSearchTool,
|
| 10 |
-
PythonREPLTool,
|
| 11 |
RequestsTool,
|
|
|
|
| 12 |
)
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
# --- Constants ---
|
| 16 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 17 |
|
| 18 |
# --- Basic Agent Definition ---
|
| 19 |
-
# ----- THIS IS WHERE YOU CAN BUILD WHAT YOU WANT ------
|
| 20 |
class BasicAgent:
|
| 21 |
def __init__(self):
|
| 22 |
-
# Cargamos el modelo
|
| 23 |
model = InferenceClientModel(model_id="google/flan-t5-large")
|
| 24 |
-
# Usamos búsqueda, requests y REPL para todo tipo de tareas
|
| 25 |
tools = [
|
| 26 |
-
DuckDuckGoSearchTool(),
|
| 27 |
-
RequestsTool(),
|
| 28 |
-
PythonREPLTool(),
|
| 29 |
]
|
| 30 |
-
#
|
| 31 |
self.agent = CodeAgent(
|
| 32 |
model=model,
|
| 33 |
tools=tools,
|
| 34 |
add_base_tools=True,
|
| 35 |
max_steps=10,
|
| 36 |
)
|
| 37 |
-
print("✅ CodeAgent
|
| 38 |
|
| 39 |
def __call__(self, question: str) -> str:
|
| 40 |
-
#
|
| 41 |
prompt = (
|
| 42 |
-
"Eres un asistente que SOLO
|
| 43 |
-
"sin explicaciones ni
|
| 44 |
f"Pregunta: {question}\n"
|
| 45 |
"Respuesta:"
|
| 46 |
)
|
| 47 |
raw = self.agent.run(prompt)
|
| 48 |
-
#
|
|
|
|
|
|
|
| 49 |
for line in raw.strip().splitlines()[::-1]:
|
| 50 |
if line.strip():
|
| 51 |
return line.strip()
|
| 52 |
return raw.strip()
|
| 53 |
|
| 54 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 55 |
-
space_id = os.getenv("SPACE_ID","")
|
| 56 |
if not profile:
|
| 57 |
return "🔒 Por favor, inicia sesión con Hugging Face.", None
|
| 58 |
username = profile.username
|
|
@@ -60,13 +58,12 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 60 |
# 1) Instanciar agente
|
| 61 |
agent = BasicAgent()
|
| 62 |
|
| 63 |
-
# 2)
|
| 64 |
-
|
| 65 |
-
resp = requests.get(questions_url, timeout=20)
|
| 66 |
resp.raise_for_status()
|
| 67 |
questions = resp.json()
|
| 68 |
|
| 69 |
-
# 3) Ejecutar en
|
| 70 |
answers, log = [], []
|
| 71 |
for q in questions:
|
| 72 |
tid, txt = q["task_id"], q["question"]
|
|
@@ -74,16 +71,16 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 74 |
answers.append({"task_id": tid, "submitted_answer": ans})
|
| 75 |
log.append({"Task ID": tid, "Question": txt, "Submitted Answer": ans})
|
| 76 |
|
| 77 |
-
# 4) Enviar
|
| 78 |
payload = {
|
| 79 |
"username": username,
|
| 80 |
"agent_code": f"https://huggingface.co/spaces/{space_id}/tree/main",
|
| 81 |
-
"answers": answers
|
| 82 |
}
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
status = (
|
| 88 |
f"✅ Submission Successful!\n"
|
| 89 |
f"User: {result['username']}\n"
|
|
@@ -93,24 +90,15 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 93 |
)
|
| 94 |
return status, pd.DataFrame(log)
|
| 95 |
|
| 96 |
-
# ---
|
| 97 |
with gr.Blocks() as demo:
|
| 98 |
-
gr.Markdown("#
|
| 99 |
-
gr.Markdown(
|
| 100 |
-
"""
|
| 101 |
-
**Instructions:**
|
| 102 |
-
|
| 103 |
-
1. Clone this space, modifica solo la clase `BasicAgent`.
|
| 104 |
-
2. Haz login con Hugging Face.
|
| 105 |
-
3. Click ‘Run Evaluation & Submit All Answers’.
|
| 106 |
-
"""
|
| 107 |
-
)
|
| 108 |
gr.LoginButton()
|
| 109 |
run_btn = gr.Button("Run Evaluation & Submit All Answers")
|
| 110 |
-
|
| 111 |
-
table = gr.
|
| 112 |
-
|
| 113 |
-
run_btn.click(fn=run_and_submit_all, outputs=[out, table])
|
| 114 |
|
| 115 |
if __name__ == "__main__":
|
| 116 |
demo.launch(debug=True)
|
|
|
|
| 7 |
from smolagents.models import InferenceClientModel
|
| 8 |
from smolagents.tools import (
|
| 9 |
DuckDuckGoSearchTool,
|
|
|
|
| 10 |
RequestsTool,
|
| 11 |
+
PythonREPLTool,
|
| 12 |
)
|
| 13 |
|
| 14 |
+
# --- Constants (no toques) ---
|
|
|
|
| 15 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 16 |
|
| 17 |
# --- Basic Agent Definition ---
|
|
|
|
| 18 |
class BasicAgent:
|
| 19 |
def __init__(self):
|
|
|
|
| 20 |
model = InferenceClientModel(model_id="google/flan-t5-large")
|
|
|
|
| 21 |
tools = [
|
| 22 |
+
DuckDuckGoSearchTool(), # buscar texto en web
|
| 23 |
+
RequestsTool(), # descargar archivos adjuntos
|
| 24 |
+
PythonREPLTool(), # procesar Excel, cálculos, imágenes…
|
| 25 |
]
|
| 26 |
+
# add_base_tools=True inyecta más utilidades (filesystem, JSON, etc.)
|
| 27 |
self.agent = CodeAgent(
|
| 28 |
model=model,
|
| 29 |
tools=tools,
|
| 30 |
add_base_tools=True,
|
| 31 |
max_steps=10,
|
| 32 |
)
|
| 33 |
+
print("✅ CodeAgent con todas las herramientas listo.")
|
| 34 |
|
| 35 |
def __call__(self, question: str) -> str:
|
| 36 |
+
# Prompt que obliga a devolver solo la respuesta pura
|
| 37 |
prompt = (
|
| 38 |
+
"Eres un asistente que SOLO debe devolver la respuesta final, "
|
| 39 |
+
"sin explicaciones, listas ni texto adicional.\n"
|
| 40 |
f"Pregunta: {question}\n"
|
| 41 |
"Respuesta:"
|
| 42 |
)
|
| 43 |
raw = self.agent.run(prompt)
|
| 44 |
+
print("RAW OUTPUT:", raw) # ayuda a depurar si algo falla
|
| 45 |
+
|
| 46 |
+
# Extraer únicamente la última línea no vacía
|
| 47 |
for line in raw.strip().splitlines()[::-1]:
|
| 48 |
if line.strip():
|
| 49 |
return line.strip()
|
| 50 |
return raw.strip()
|
| 51 |
|
| 52 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 53 |
+
space_id = os.getenv("SPACE_ID", "")
|
| 54 |
if not profile:
|
| 55 |
return "🔒 Por favor, inicia sesión con Hugging Face.", None
|
| 56 |
username = profile.username
|
|
|
|
| 58 |
# 1) Instanciar agente
|
| 59 |
agent = BasicAgent()
|
| 60 |
|
| 61 |
+
# 2) Descargar preguntas
|
| 62 |
+
resp = requests.get(f"{DEFAULT_API_URL}/questions", timeout=20)
|
|
|
|
| 63 |
resp.raise_for_status()
|
| 64 |
questions = resp.json()
|
| 65 |
|
| 66 |
+
# 3) Ejecutar agente en cada pregunta
|
| 67 |
answers, log = [], []
|
| 68 |
for q in questions:
|
| 69 |
tid, txt = q["task_id"], q["question"]
|
|
|
|
| 71 |
answers.append({"task_id": tid, "submitted_answer": ans})
|
| 72 |
log.append({"Task ID": tid, "Question": txt, "Submitted Answer": ans})
|
| 73 |
|
| 74 |
+
# 4) Enviar resultados
|
| 75 |
payload = {
|
| 76 |
"username": username,
|
| 77 |
"agent_code": f"https://huggingface.co/spaces/{space_id}/tree/main",
|
| 78 |
+
"answers": answers,
|
| 79 |
}
|
| 80 |
+
submit_resp = requests.post(f"{DEFAULT_API_URL}/submit", json=payload, timeout=60)
|
| 81 |
+
submit_resp.raise_for_status()
|
| 82 |
+
result = submit_resp.json()
|
| 83 |
+
|
| 84 |
status = (
|
| 85 |
f"✅ Submission Successful!\n"
|
| 86 |
f"User: {result['username']}\n"
|
|
|
|
| 90 |
)
|
| 91 |
return status, pd.DataFrame(log)
|
| 92 |
|
| 93 |
+
# --- Gradio UI ---
|
| 94 |
with gr.Blocks() as demo:
|
| 95 |
+
gr.Markdown("# 🧠 GAIA Final Agent Runner")
|
| 96 |
+
gr.Markdown("Haz login y pulsa el botón para evaluar tu agente en el benchmark GAIA.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
gr.LoginButton()
|
| 98 |
run_btn = gr.Button("Run Evaluation & Submit All Answers")
|
| 99 |
+
status = gr.Textbox(label="Run Status / Submission Result", lines=6)
|
| 100 |
+
table = gr.Dataframe(label="Questions and Agent Answers", wrap=True)
|
| 101 |
+
run_btn.click(fn=run_and_submit_all, outputs=[status, table])
|
|
|
|
| 102 |
|
| 103 |
if __name__ == "__main__":
|
| 104 |
demo.launch(debug=True)
|