smiquensi commited on
Commit
41ba21f
·
verified ·
1 Parent(s): 0305bdf
Files changed (1) hide show
  1. app.py +31 -24
app.py CHANGED
@@ -1,53 +1,57 @@
1
  # app.py
 
2
  import os
3
  import gradio as gr
4
  import requests
5
  import pandas as pd
 
6
  from agent import make_agent
7
 
8
- API = "https://agents-course-unit4-scoring.hf.space"
9
 
10
  def run_and_submit_all(profile: gr.OAuthProfile | None):
11
- # 1) Login check
12
  if not profile:
13
- return "🔒 Por favor inicia sesión.", None
14
- user = profile.username
15
  space_id = os.getenv("SPACE_ID", "")
16
 
17
- # 2) Instanciar agente
18
  agent = make_agent()
19
 
20
- # 3) Descargar preguntas
21
- resp = requests.get(f"{API}/questions", timeout=20)
22
  resp.raise_for_status()
23
- qs = resp.json()
24
 
 
25
  answers, log = [], []
26
- for q in qs:
27
- tid, txt = q["task_id"], q["question"]
28
- # Construimos un prompt que fuerce solo la respuesta final
29
  prompt = (
30
  "Responde SÓLO con la respuesta final, sin explicaciones.\n"
31
- f"{txt}\n"
32
  "Respuesta:"
33
  )
34
  raw = agent.run(prompt)
35
- # Extraemos la última línea no vacía
36
- ans = next((line for line in raw.splitlines()[::-1] if line.strip()), raw).strip()
37
  answers.append({"task_id": tid, "submitted_answer": ans})
38
- log.append({"Task ID": tid, "Question": txt, "Answer": ans})
39
 
40
- # 4) Envío
41
  payload = {
42
- "username": user,
43
  "agent_code": f"https://huggingface.co/spaces/{space_id}/tree/main",
44
  "answers": answers,
45
  }
46
- sub = requests.post(f"{API}/submit", json=payload, timeout=60)
47
  sub.raise_for_status()
48
  result = sub.json()
 
 
49
  status = (
50
- f"✅ ¡Éxito!\n"
51
  f"User: {result['username']}\n"
52
  f"Score: {result['score']}% "
53
  f"({result.get('correct_count')}/{result.get('total_attempted')})\n"
@@ -55,13 +59,16 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
55
  )
56
  return status, pd.DataFrame(log)
57
 
 
58
  with gr.Blocks() as demo:
59
- gr.Markdown("# GAIA Final Agent Mini")
 
60
  gr.LoginButton()
61
- btn = gr.Button("Run & Submit")
62
- out = gr.Textbox(label="Resultado", lines=6)
63
- tbl = gr.DataFrame(label="Q & A", wrap=True)
64
- btn.click(fn=run_and_submit_all, outputs=[out, tbl])
 
65
 
66
  if __name__ == "__main__":
67
  demo.launch(debug=True)
 
1
  # app.py
2
+
3
  import os
4
  import gradio as gr
5
  import requests
6
  import pandas as pd
7
+
8
  from agent import make_agent
9
 
10
+ API_URL = "https://agents-course-unit4-scoring.hf.space"
11
 
12
  def run_and_submit_all(profile: gr.OAuthProfile | None):
13
+ # 0) Comprueba login
14
  if not profile:
15
+ return "🔒 Por favor, haz login con Hugging Face.", None
16
+ username = profile.username
17
  space_id = os.getenv("SPACE_ID", "")
18
 
19
+ # 1) Instancia tu agente
20
  agent = make_agent()
21
 
22
+ # 2) Descarga las 20 preguntas de GAIA
23
+ resp = requests.get(f"{API_URL}/questions", timeout=20)
24
  resp.raise_for_status()
25
+ questions = resp.json()
26
 
27
+ # 3) Para cada pregunta: construye prompt, ejecuta y extrae la última línea
28
  answers, log = [], []
29
+ for q in questions:
30
+ tid, text = q["task_id"], q["question"]
 
31
  prompt = (
32
  "Responde SÓLO con la respuesta final, sin explicaciones.\n"
33
+ f"{text}\n"
34
  "Respuesta:"
35
  )
36
  raw = agent.run(prompt)
37
+ # toma la última línea no vacía
38
+ ans = next((l for l in raw.splitlines()[::-1] if l.strip()), raw).strip()
39
  answers.append({"task_id": tid, "submitted_answer": ans})
40
+ log.append({"Task ID": tid, "Question": text, "Answer": ans})
41
 
42
+ # 4) Envía el payload
43
  payload = {
44
+ "username": username,
45
  "agent_code": f"https://huggingface.co/spaces/{space_id}/tree/main",
46
  "answers": answers,
47
  }
48
+ sub = requests.post(f"{API_URL}/submit", json=payload, timeout=60)
49
  sub.raise_for_status()
50
  result = sub.json()
51
+
52
+ # 5) Muestra resultado y tabla
53
  status = (
54
+ f"✅ Submission Successful!\n"
55
  f"User: {result['username']}\n"
56
  f"Score: {result['score']}% "
57
  f"({result.get('correct_count')}/{result.get('total_attempted')})\n"
 
59
  )
60
  return status, pd.DataFrame(log)
61
 
62
+ # --- Interfaz Gradio ---
63
  with gr.Blocks() as demo:
64
+ gr.Markdown("# 🧠 GAIA Final Agent Mini")
65
+ gr.Markdown("Haz login y pulsa el botón para evaluar tu agente en GAIA.")
66
  gr.LoginButton()
67
+ run_btn = gr.Button("Run Evaluation & Submit All Answers")
68
+ status = gr.Textbox(label="Resultado", lines=6, interactive=False)
69
+ table = gr.Dataframe(label="Preguntas y Respuestas", wrap=True)
70
+
71
+ run_btn.click(fn=run_and_submit_all, outputs=[status, table])
72
 
73
  if __name__ == "__main__":
74
  demo.launch(debug=True)