Spaces:
Sleeping
Sleeping
| import os | |
| import gradio as gr | |
| import requests | |
| import pandas as pd | |
| from smolagents import ( | |
| CodeAgent, | |
| DuckDuckGoSearchTool, | |
| VisitWebpageTool, | |
| InferenceClientModel | |
| ) | |
| # --- Constants --- | |
| DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space" | |
| # ----------------------------- | |
| # REAL AGENT (FIXED VERSION) | |
| # ----------------------------- | |
| class BasicAgent: | |
| def __init__(self): | |
| print("Initializing real agent...") | |
| # Model (Hugging Face hosted inference) | |
| self.model = InferenceClientModel() | |
| # Tools | |
| tools = [ | |
| DuckDuckGoSearchTool(), | |
| VisitWebpageTool() | |
| ] | |
| # CodeAgent (actual reasoning agent) | |
| self.agent = CodeAgent( | |
| tools=tools, | |
| model=self.model | |
| ) | |
| def __call__(self, question: str) -> str: | |
| print(f"Question: {question}") | |
| try: | |
| result = self.agent.run(question) | |
| return str(result) | |
| except Exception as e: | |
| print(f"Agent error: {e}") | |
| return f"ERROR: {e}" | |
| # ----------------------------- | |
| # RUN + SUBMIT FUNCTION | |
| # ----------------------------- | |
| 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 using Hugging Face button.", None | |
| api_url = DEFAULT_API_URL | |
| questions_url = f"{api_url}/questions" | |
| submit_url = f"{api_url}/submit" | |
| # Create agent | |
| agent = BasicAgent() | |
| agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" | |
| # Fetch questions | |
| response = requests.get(questions_url, timeout=30) | |
| response.raise_for_status() | |
| questions_data = response.json() | |
| results_log = [] | |
| answers_payload = [] | |
| # Run agent | |
| 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, | |
| "Answer": answer | |
| }) | |
| # Submit results | |
| submission_data = { | |
| "username": username, | |
| "agent_code": agent_code, | |
| "answers": answers_payload | |
| } | |
| submit_response = requests.post(submit_url, json=submission_data, timeout=60) | |
| submit_response.raise_for_status() | |
| result = submit_response.json() | |
| status = ( | |
| f"Submission Successful!\n" | |
| f"Score: {result.get('score')}%\n" | |
| f"Correct: {result.get('correct_count')}/{result.get('total_attempted')}\n" | |
| f"Message: {result.get('message')}" | |
| ) | |
| return status, pd.DataFrame(results_log) | |
| # ----------------------------- | |
| # GRADIO UI | |
| # ----------------------------- | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# Unit 4 Agent Evaluation (Fixed Version)") | |
| gr.Markdown( | |
| "1. Login with Hugging Face\n" | |
| "2. Click Run Evaluation\n" | |
| "3. Wait for scoring" | |
| ) | |
| gr.LoginButton() | |
| btn = gr.Button("Run Evaluation") | |
| status = gr.Textbox(label="Status") | |
| table = gr.DataFrame(label="Results") | |
| btn.click( | |
| fn=run_and_submit_all, | |
| outputs=[status, table] | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() |