File size: 3,715 Bytes
10e9b7d
 
eccf8e4
3c4371f
819d6f9
10e9b7d
3db6293
e80aab9
31243f4
 
819d6f9
 
e875e25
 
 
 
 
 
 
 
 
 
819d6f9
4d14618
31243f4
819d6f9
4d14618
819d6f9
 
 
 
 
 
 
4021bf3
3c4371f
819d6f9
 
 
3c4371f
819d6f9
 
7e4a06b
819d6f9
e80aab9
31243f4
 
 
819d6f9
3c4371f
eccf8e4
819d6f9
 
 
31243f4
7d65c66
819d6f9
e80aab9
7d65c66
 
819d6f9
31243f4
 
 
 
 
 
819d6f9
31243f4
819d6f9
 
 
31243f4
 
819d6f9
31243f4
819d6f9
 
 
 
 
e80aab9
 
819d6f9
 
 
 
 
 
 
 
 
e80aab9
819d6f9
7d65c66
819d6f9
e80aab9
 
 
819d6f9
 
 
 
 
 
7e4a06b
819d6f9
 
 
 
e80aab9
 
819d6f9
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
113
import os
import gradio as gr
import requests
import pandas as pd
from smolagents import CodeAgent, DuckDuckGoSearchTool, OpenAIServerModel

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

class BasicAgent:
    def __init__(self):
        print("Initializing agent via OpenRouter...")
        # Uses OpenRouter — free $1 credit, no HF credit consumption
 self.agent = CodeAgent(
    tools=[DuckDuckGoSearchTool()],
    model=OpenAIServerModel(
        model_id="llama-3.3-70b-versatile",
        api_base="https://api.groq.com/openai/v1",
        api_key=os.environ["GROQ_API_KEY"],
    ),
    max_steps=4,
    verbosity_level=0,
)
        print("Agent ready.")

    def __call__(self, question: str) -> str:
        print(f"Q: {question[:80]}...")
        try:
            result = self.agent.run(question)
            answer = str(result).strip()
            print(f"A: {answer[:100]}")
            return answer
        except Exception as e:
            print(f"Error: {e}")
            return "I don't know."


def run_and_submit_all(profile: gr.OAuthProfile | None):
    if not profile:
        return "Please login with your Hugging Face account first.", None

    username = profile.username
    space_id = os.getenv("SPACE_ID", "unknown/space")
    api_url = DEFAULT_API_URL
    agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"

    try:
        agent = BasicAgent()
    except Exception as e:
        return f"Agent init failed: {e}", None

    try:
        resp = requests.get(f"{api_url}/questions", timeout=15)
        resp.raise_for_status()
        questions_data = resp.json()
        print(f"Fetched {len(questions_data)} questions.")
    except Exception as e:
        return f"Failed to fetch questions: {e}", None

    results_log = []
    answers_payload = []

    for item in questions_data:
        task_id = item.get("task_id")
        question_text = item.get("question")
        if not task_id or question_text is None:
            continue
        try:
            answer = agent(question_text)
        except Exception as e:
            answer = "I don't know."
        answers_payload.append({"task_id": task_id, "submitted_answer": answer})
        results_log.append({"Task ID": task_id, "Question": question_text, "Answer": answer})

    if not answers_payload:
        return "No answers produced.", pd.DataFrame(results_log)

    submission = {
        "username": username.strip(),
        "agent_code": agent_code,
        "answers": answers_payload
    }

    try:
        resp = requests.post(f"{api_url}/submit", json=submission, timeout=120)
        resp.raise_for_status()
        r = resp.json()
        status = (
            f"✅ Submitted!\n"
            f"User: {r.get('username')}\n"
            f"Score: {r.get('score', 'N/A')}% "
            f"({r.get('correct_count', '?')}/{r.get('total_attempted', '?')} correct)\n"
            f"Message: {r.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("# 🤖 HF Agents Course — Final Assignment")
    gr.Markdown("""
1. Login with your HF account below
2. Click **Run Evaluation** and wait ~3–5 min
3. Results and score appear below
    """)
    gr.LoginButton()
    run_btn = gr.Button("🚀 Run Evaluation & Submit All Answers")
    status_out = gr.Textbox(label="Result", lines=6, interactive=False)
    results_table = gr.DataFrame(label="Answers", wrap=True)
    run_btn.click(fn=run_and_submit_all, outputs=[status_out, results_table])

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