File size: 2,623 Bytes
3bce169
cfc7eb3
10e9b7d
3bce169
188585a
3bce169
188585a
3bce169
cfc7eb3
188585a
 
3bce169
188585a
3bce169
188585a
 
3bce169
 
188585a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3bce169
188585a
 
 
 
 
3bce169
188585a
 
 
 
 
3bce169
 
 
 
 
188585a
 
 
 
 
3bce169
 
188585a
 
 
 
 
3bce169
188585a
 
 
 
3bce169
188585a
 
 
 
 
 
 
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
# app.py

import os
import asyncio
import pandas as pd
import requests
import gradio as gr
from agent import answer_question

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

class GAIALlamaAgent:
    def __init__(self):
        print("Initialized LlamaIndex Agent")

    def __call__(self, question: str) -> str:
        print(f"Received question: {question[:60]}...")
        return asyncio.run(answer_question(question))

def run_and_submit_all(profile: gr.OAuthProfile | None):
    space_id = os.getenv("SPACE_ID")
    username = profile.username if profile else None
    if not username:
        return "Please login to Hugging Face.", None

    agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else ""
    api_url = DEFAULT_API_URL

    try:
        response = requests.get(f"{api_url}/questions", timeout=15)
        response.raise_for_status()
        questions_data = response.json()
    except Exception as e:
        return f"Error fetching questions: {e}", None

    answers_payload = []
    results_log = []
    agent = GAIALlamaAgent()

    for item in questions_data:
        q = item.get("question")
        task_id = item.get("task_id")
        try:
            a = agent(q)
        except Exception as e:
            a = f"ERROR: {e}"
        answers_payload.append({"task_id": task_id, "submitted_answer": a})
        results_log.append({"Task ID": task_id, "Question": q, "Submitted Answer": a})

    submission_data = {
        "username": username,
        "agent_code": agent_code,
        "answers": answers_payload
    }

    try:
        response = requests.post(f"{api_url}/submit", json=submission_data, timeout=60)
        response.raise_for_status()
        result_data = response.json()
        status = (
            f"✅ Submission Successful!\n"
            f"User: {result_data.get('username')}\n"
            f"Score: {result_data.get('score')}%\n"
            f"Correct: {result_data.get('correct_count')}/{result_data.get('total_attempted')}\n"
            f"Message: {result_data.get('message')}"
        )
        return status, pd.DataFrame(results_log)
    except Exception as e:
        return f"Submission failed: {e}", pd.DataFrame(results_log)

with gr.Blocks() as demo:
    gr.Markdown("# LlamaIndex GAIA Agent – Evaluation Portal")
    gr.LoginButton()
    run_btn = gr.Button("Run Evaluation & Submit All Answers")
    status_box = gr.Textbox(label="Status", lines=5)
    result_table = gr.DataFrame(label="Agent Answers")
    run_btn.click(fn=run_and_submit_all, outputs=[status_box, result_table])

demo.launch(debug=True)