import os import gradio as gr import requests import pandas as pd from smolagents import CodeAgent, DuckDuckGoSearchTool from smolagents.models import OpenAIServerModel # Constants DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space" # === Define the Smol Agent === class MyAgent: def __init__(self): self.model = OpenAIServerModel(model_id="gpt-4") # or "gpt-3.5-turbo" self.agent = CodeAgent( tools=[DuckDuckGoSearchTool()], model=self.model, system_message="""You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.""" ) def __call__(self, question: str) -> str: return self.agent.run(question) # === Submission Logic === 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 to Hugging Face with the button.", None 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" try: agent = MyAgent() except Exception as e: return f"Error initializing agent: {e}", None # Fetch Questions try: res = requests.get(questions_url, timeout=15) res.raise_for_status() questions_data = res.json() 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 = item.get("question") if not task_id or question is None: continue try: answer = agent(question) results_log.append({"Task ID": task_id, "Question": question, "Submitted Answer": answer}) answers_payload.append({"task_id": task_id, "submitted_answer": answer}) except Exception as e: results_log.append({"Task ID": task_id, "Question": question, "Submitted Answer": f"ERROR: {e}"}) if not answers_payload: return "No answers generated.", pd.DataFrame(results_log) submission_data = { "username": username, "agent_code": agent_code, "answers": answers_payload } try: res = requests.post(submit_url, json=submission_data, timeout=60) res.raise_for_status() result_data = res.json() summary = ( f"Submission Successful!\n" f"User: {result_data.get('username')}\n" f"Score: {result_data.get('score', '?')}% " f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')})\n" f"Message: {result_data.get('message', '')}" ) return summary, pd.DataFrame(results_log) except Exception as e: return f"Submission failed: {e}", pd.DataFrame(results_log) # === Gradio UI === with gr.Blocks() as demo: gr.Markdown("# Agent Evaluation Runner (SmolAgents)") gr.Markdown(""" **Instructions:** 1. Clone this space and customize your agent. 2. Log in with Hugging Face. 3. Click 'Run Evaluation' to answer and submit. """) gr.LoginButton() run_button = gr.Button("Run Evaluation & Submit All Answers") status_output = gr.Textbox(label="Status", lines=4, interactive=False) results_table = gr.DataFrame(label="Results", wrap=True) run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table]) if __name__ == "__main__": print("Launching...") demo.launch(debug=True)