| import os |
| import gradio as gr |
| import requests |
| import pandas as pd |
|
|
| |
| DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space" |
|
|
| |
| from smolagents import CodeAgent, HfApiModel, DuckDuckGoSearchTool |
|
|
| model = HfApiModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct") |
|
|
| real_agent = CodeAgent( |
| model=model, |
| tools=[DuckDuckGoSearchTool()] |
| ) |
|
|
| class BasicAgent: |
| def __init__(self): |
| print("Agent initialized") |
|
|
| def __call__(self, question: str) -> str: |
| try: |
| result = real_agent.run(question) |
| return str(result).strip() |
| except Exception as e: |
| print(f"Agent error: {e}") |
| return "ERROR" |
|
|
|
|
| |
| def run_and_submit_all(profile: gr.OAuthProfile | None): |
|
|
| space_id = os.getenv("SPACE_ID") |
|
|
| if profile: |
| username = profile.username |
| print(f"User logged in: {username}") |
| else: |
| return "Please login first", None |
|
|
| api_url = DEFAULT_API_URL |
| questions_url = f"{api_url}/questions" |
| submit_url = f"{api_url}/submit" |
|
|
| agent = BasicAgent() |
|
|
| agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" |
|
|
| |
| response = requests.get(questions_url, timeout=30) |
| questions_data = response.json() |
|
|
| results_log = [] |
| answers_payload = [] |
|
|
| for item in questions_data: |
| task_id = item["task_id"] |
| question = item["question"] |
|
|
| answer = agent(question) |
|
|
| 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 |
| } |
|
|
| response = requests.post(submit_url, json=submission_data, timeout=60) |
| result = response.json() |
|
|
| return ( |
| f"Score: {result.get('score', 0)}%", |
| pd.DataFrame(results_log) |
| ) |
|
|
|
|
| |
| with gr.Blocks() as demo: |
| gr.Markdown("# Basic Agent Evaluation Runner") |
|
|
| gr.LoginButton() |
|
|
| btn = gr.Button("Run Evaluation & Submit All Answers") |
|
|
| out1 = gr.Textbox() |
| out2 = gr.DataFrame() |
|
|
| btn.click(run_and_submit_all, outputs=[out1, out2]) |
|
|
|
|
| if __name__ == "__main__": |
| demo.launch() |