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

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

# --- Simple Working Agent ---
class BasicAgent:
    def __init__(self):
        print("Agent ready")
    
    def __call__(self, question: str) -> str:
        # Return a simple answer - you'll replace this with your logic
        return "This is a placeholder answer. Replace with your actual answer logic."

def run_and_submit_all(profile: gr.OAuthProfile | None):
    """Run agent on all questions and submit answers"""
    
    if not profile:
        return "Please login first", None
    
    username = profile.username
    space_id = os.getenv("SPACE_ID", "local")
    api_url = DEFAULT_API_URL
    
    # Create agent
    agent = BasicAgent()
    
    # Get questions
    try:
        response = requests.get(f"{api_url}/questions", timeout=10)
        questions = response.json()
    except Exception as e:
        return f"Failed to get questions: {e}", None
    
    # Answer questions
    answers = []
    results = []
    
    for q in questions:
        task_id = q.get("task_id")
        question_text = q.get("question", "")
        
        # Get answer from agent
        answer = agent(question_text)
        
        answers.append({
            "task_id": task_id,
            "submitted_answer": answer
        })
        
        results.append({
            "Task ID": task_id,
            "Question": question_text[:50] + "...",
            "Answer": answer[:50] + "..."
        })
    
    # Submit answers
    try:
        response = requests.post(
            f"{api_url}/submit",
            json={
                "username": username,
                "agent_code": f"https://huggingface.co/spaces/{space_id}",
                "answers": answers
            },
            timeout=30
        )
        
        result = response.json()
        status = f"Score: {result.get('score', 0)}% ({result.get('correct_count', 0)}/20 correct)"
        
        return status, pd.DataFrame(results)
        
    except Exception as e:
        return f"Submission failed: {e}", pd.DataFrame(results)

# --- Gradio Interface ---
with gr.Blocks() as demo:
    gr.Markdown("# GIAIA 20 Questions Evaluation")
    
    gr.LoginButton()
    btn = gr.Button("Run on 20 Questions")
    status = gr.Textbox(label="Result")
    table = gr.DataFrame(label="Answers Preview")
    
    btn.click(run_and_submit_all, outputs=[status, table])

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