| import os |
| import gradio as gr |
| import requests |
| import pandas as pd |
|
|
| from smolagents import CodeAgent, HfApiModel, DuckDuckGoSearchTool |
|
|
| |
| DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space" |
|
|
| |
| 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 "unknown" |
|
|
|
|
| |
| def run_and_submit_all(profile: gr.OAuthProfile | None): |
|
|
| if profile: |
| username = profile.username |
| else: |
| return "Please login first", pd.DataFrame([]) |
|
|
| space_id = os.getenv("SPACE_ID", "unknown") |
| agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" |
|
|
| questions_url = f"{DEFAULT_API_URL}/questions" |
| submit_url = f"{DEFAULT_API_URL}/submit" |
|
|
| agent = BasicAgent() |
|
|
| |
| try: |
| response = requests.get(questions_url, timeout=30) |
| response.raise_for_status() |
| questions_data = response.json() |
| except Exception as e: |
| return f"Failed to fetch questions: {e}", pd.DataFrame([]) |
|
|
| results_log = [] |
| answers_payload = [] |
|
|
| |
| for item in questions_data: |
| task_id = item.get("task_id") |
| question = item.get("question") |
|
|
| if not task_id or not question: |
| continue |
|
|
| 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 |
| } |
|
|
| try: |
| response = requests.post(submit_url, json=submission_data, timeout=60) |
| response.raise_for_status() |
| result = response.json() |
|
|
| return ( |
| f"Score: {result.get('score', 0)}%", |
| pd.DataFrame(results_log) |
| ) |
|
|
| except Exception as e: |
| return f"Submission failed: {e}", 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(label="Score") |
| out2 = gr.DataFrame(label="Results") |
|
|
| btn.click(run_and_submit_all, outputs=[out1, out2]) |
|
|
|
|
| if __name__ == "__main__": |
| demo.launch() |