File size: 2,451 Bytes
10e9b7d
 
eccf8e4
3c4371f
10e9b7d
3db6293
e80aab9
31243f4
 
9ff6421
4021bf3
e4cb4da
9ff6421
 
3c4371f
e4cb4da
7e4a06b
9ff6421
7e4a06b
9ff6421
3c4371f
9ff6421
 
 
e80aab9
e4cb4da
9ff6421
 
3c4371f
eccf8e4
9ff6421
 
7d65c66
e4cb4da
e80aab9
9ff6421
 
 
 
 
 
 
 
 
31243f4
e4cb4da
9ff6421
 
 
e4cb4da
e80aab9
 
9ff6421
 
 
 
 
 
 
 
e80aab9
9ff6421
7d65c66
9ff6421
e80aab9
 
9ff6421
7e4a06b
9ff6421
 
 
e80aab9
9ff6421
e80aab9
 
e4cb4da
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
import os
import gradio as gr
import requests
import pandas as pd

DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"

class BasicAgent:
    def __init__(self):
        print("Agente básico cargado.")

    def __call__(self, question: str) -> str:
        print(f"Pregunta recibida: {question}")
        return "This is a default answer."  # Puedes modificar esta línea luego

def run_and_submit_all(profile: gr.OAuthProfile | None):
    if profile:
        username = profile.username
    else:
        return "🔒 Por favor, inicia sesión con Hugging Face.", None

    space_id = os.getenv("SPACE_ID", "")
    if not space_id:
        return "❌ Falta la variable SPACE_ID", None

    agent = BasicAgent()
    questions_url = f"{DEFAULT_API_URL}/questions"
    submit_url = f"{DEFAULT_API_URL}/submit"

    try:
        response = requests.get(questions_url, timeout=10)
        questions = response.json()
    except Exception as e:
        return f"❌ Error al obtener preguntas: {e}", None

    results = []
    answers = []
    for q in questions:
        task_id = q.get("task_id")
        question = q.get("question")
        if task_id and question:
            answer = agent(question)
            results.append({"Task ID": task_id, "Question": question, "Submitted Answer": answer})
            answers.append({"task_id": task_id, "submitted_answer": answer})

    payload = {
        "username": username,
        "agent_code": f"https://huggingface.co/spaces/{space_id}/tree/main",
        "answers": answers
    }

    try:
        res = requests.post(submit_url, json=payload)
        res.raise_for_status()
        result_data = res.json()
        msg = (
            f"✅ Envío correcto\n"
            f"Usuario: {result_data.get('username')}\n"
            f"Puntuación: {result_data.get('score')}% "
            f"({result_data.get('correct_count')}/{result_data.get('total_attempted')})"
        )
        return msg, pd.DataFrame(results)
    except Exception as e:
        return f"❌ Error al enviar resultados: {e}", pd.DataFrame(results)

with gr.Blocks() as demo:
    gr.Markdown("# Evaluación del Agente GAIA")
    gr.LoginButton()
    btn = gr.Button("Enviar respuestas y evaluar")
    output = gr.Textbox(label="Resultado", lines=4)
    tabla = gr.DataFrame(label="Respuestas")

    btn.click(fn=run_and_submit_all, outputs=[output, tabla])

if __name__ == "__main__":
    demo.launch(debug=True)