File size: 2,222 Bytes
10e9b7d
 
eccf8e4
3c4371f
28f65b4
10e9b7d
3db6293
e80aab9
28f65b4
 
 
 
 
4021bf3
28f65b4
36ed51a
3c4371f
eccf8e4
28f65b4
7d65c66
31243f4
7d65c66
28f65b4
e80aab9
28f65b4
31243f4
 
28f65b4
 
31243f4
 
28f65b4
31243f4
28f65b4
 
 
31243f4
28f65b4
 
 
 
 
e80aab9
 
28f65b4
 
 
 
 
7d65c66
28f65b4
e80aab9
 
28f65b4
 
7e4a06b
31243f4
28f65b4
 
e80aab9
28f65b4
e80aab9
 
28f65b4
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
import os
import gradio as gr
import requests
import pandas as pd
from rag_agent import BasicAgent

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

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 log in to Hugging Face.", None

    agent = BasicAgent()
    agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"

    try:
        response = requests.get(f"{DEFAULT_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 = [], []
    for item in questions_data:
        task_id = item.get("task_id")
        question = item.get("question")
        if not task_id or not question:
            continue
        try:
            answer = agent(question)
        except Exception as e:
            answer = f"ERROR: {e}"
        answers_payload.append({"task_id": task_id, "submitted_answer": answer})
        results_log.append({"Task ID": task_id, "Question": question, "Submitted Answer": answer})

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

    try:
        res = requests.post(f"{DEFAULT_API_URL}/submit", json=submission_data, timeout=60)
        res.raise_for_status()
        result = res.json()
        status = f"✅ Submitted! Score: {result.get('score')}% - {result.get('message')}"
        return status, pd.DataFrame(results_log)
    except Exception as e:
        return f"Submission error: {e}", pd.DataFrame(results_log)

with gr.Blocks() as demo:
    gr.Markdown("# 🧠 RAG Agent: Wikipedia + arXiv")
    gr.Markdown("Login with Hugging Face and click below to evaluate your agent.")
    gr.LoginButton()
    run_button = gr.Button("Run Evaluation & Submit All Answers")
    status = gr.Textbox(label="Status", lines=4)
    table = gr.DataFrame(label="Results")

    run_button.click(fn=run_and_submit_all, outputs=[status, table])

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