File size: 3,634 Bytes
10e9b7d
2707bf9
fffffb0
2794b4c
904e0bd
ae8739e
2707bf9
 
ae8739e
 
1803c5e
ae8739e
fffffb0
1803c5e
ae8739e
 
177be6f
ae8739e
 
 
 
 
 
 
 
 
 
 
 
3fd9d2e
177be6f
ae8739e
 
177be6f
ae8739e
177be6f
ae8739e
 
 
 
177be6f
ae8739e
177be6f
ae8739e
393a8af
ae8739e
 
3fd9d2e
ae8739e
 
177be6f
 
ae8739e
177be6f
ae8739e
d8dafef
ae8739e
 
 
177be6f
ae8739e
3fd9d2e
ae8739e
3fd9d2e
ae8739e
177be6f
 
 
ae8739e
3fd9d2e
 
ae8739e
 
 
 
 
 
177be6f
d8dafef
ae8739e
177be6f
ae8739e
3628aaf
ae8739e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
177be6f
 
ae8739e
 
 
 
 
 
 
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
import os
import gradio as gr
import requests
import pandas as pd

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

# --- Agent Definition (replace with your own logic) ---
class BasicAgent:
    def __init__(self):
        print("βœ… BasicAgent initialized.")

    def __call__(self, question: str) -> str:
        print(f"πŸ“₯ Question received: {question[:60]}...")
        return "Paris" if "capital of France" in question else "42"

# --- Evaluation Function ---
def run_and_submit_all(profile: gr.OAuthProfile | None):
    space_id = os.getenv("SPACE_ID") or "your-username/your-space"  # fallback

    if not profile:
        return "⚠️ Please log in to Hugging Face to submit.", None

    username = profile.username
    print(f"πŸ” Logged in as: {username}")

    questions_url = f"{DEFAULT_API_URL}/questions"
    submit_url = f"{DEFAULT_API_URL}/submit"
    agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"

    # Instantiate your agent
    agent = BasicAgent()

    # Fetch questions
    try:
        response = requests.get(questions_url, timeout=15)
        response.raise_for_status()
        questions = response.json()
        print(f"πŸ“¦ {len(questions)} questions fetched.")
    except Exception as e:
        return f"❌ Failed to fetch questions: {e}", None

    # Process answers
    answers_payload = []
    logs = []
    for q in questions:
        task_id = q.get("task_id")
        question_text = q.get("question")
        if not task_id or not question_text:
            continue
        try:
            answer = agent(question_text)
        except Exception as e:
            answer = f"AGENT ERROR: {e}"

        answers_payload.append({"task_id": task_id, "submitted_answer": answer})
        logs.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": answer})
        print(f"βœ… Task ID: {task_id} | Answer: {answer}")

    # Submit answers
    submission = {
        "username": username,
        "agent_code": agent_code,
        "answers": answers_payload,
    }

    try:
        res = requests.post(submit_url, json=submission, timeout=60)
        res.raise_for_status()
        result = res.json()
        status = (
            f"πŸŽ‰ Submission Successful!\n"
            f"User: {result.get('username', 'N/A')}\n"
            f"Score: {result.get('score', 0)}%\n"
            f"Correct: {result.get('correct_count', 0)}/{result.get('total_attempted', 0)}\n"
            f"Message: {result.get('message', '')}"
        )
    except Exception as e:
        status = f"❌ Submission failed: {e}"

    return status, pd.DataFrame(logs)

# --- Gradio UI ---
with gr.Blocks() as demo:
    gr.Markdown("# πŸ€– GAIA Evaluation Agent")
    gr.Markdown("""
    1. Log in with your Hugging Face account below.
    2. Click the button to evaluate and submit answers.
    3. Your score and submission details will appear below.
    """)

    profile_input = gr.OAuthProfile()
    run_button = gr.Button("πŸš€ Run Evaluation & Submit All Answers")
    status_output = gr.Textbox(label="Submission Result", lines=6, interactive=False)
    results_table = gr.DataFrame(label="Answer Log")

    run_button.click(
        fn=run_and_submit_all,
        inputs=[profile_input],
        outputs=[status_output, results_table]
    )

if __name__ == "__main__":
    print("\n================== GAIA Agent App Starting ==================")
    if os.getenv("SPACE_ID"):
        print(f"πŸ“¦ SPACE_ID = {os.getenv('SPACE_ID')}")
    else:
        print("ℹ️ No SPACE_ID found. Using fallback link.")

    demo.launch(debug=True)