m-bendik/agents-course-final-assignment
0
1import os2import gradio as gr3import requests4import pandas as pd5 6from smolagents import (7 CodeAgent,8 OpenAIServerModel,9)10from smolagents import CodeAgent, DuckDuckGoSearchTool, VisitWebpageTool11from dotenv import load_dotenv12import os13 14load_dotenv()15 16system_prompt = """17YOUR 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.18"""19 20# model = OpenAIServerModel(model_id="GPT 4o mini",21# api_base=os.environ["LITE_LLM_ENDPOINT"], api_key=os.environ["LITE_LLM_KEY"])22# model = OpenAIServerModel(model_id="o3")23model = OpenAIServerModel(model_id="gpt-4o")24agent = CodeAgent(tools=[DuckDuckGoSearchTool(), VisitWebpageTool()], model=model, add_base_tools=True, max_steps=30)25 26# (Keep Constants as is)27# --- Constants ---28DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"29 30# --- Basic Agent Definition ---31# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------32class BasicAgent:33 def __init__(self):34 print("BasicAgent initialized.")35 def __call__(self, question: str) -> str:36 print(f"Agent received question (first 50 chars): {question[:50]}...")37 answer = agent.run(question + "\n\n" + system_prompt)38 print(f"Agent returning fixed answer: {answer}")39 return answer40 41def run_and_submit_all( profile: gr.OAuthProfile | None):42 """43 Fetches all questions, runs the BasicAgent on them, submits all answers,44 and displays the results.45 """46 # --- Determine HF Space Runtime URL and Repo URL ---47 space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code48 49 if profile:50 username= f"{profile.username}"51 print(f"User logged in: {username}")52 else:53 print("User not logged in.")54 return "Please Login to Hugging Face with the button.", None55 56 api_url = DEFAULT_API_URL57 questions_url = f"{api_url}/questions"58 submit_url = f"{api_url}/submit"59 60 # 1. Instantiate Agent ( modify this part to create your agent)61 try:62 agent = BasicAgent()63 except Exception as e:64 print(f"Error instantiating agent: {e}")65 return f"Error initializing agent: {e}", None66 # In the case of an app running as a hugging Face space, this link points toward your codebase ( usefull for others so please keep it public)67 agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"68 print(agent_code)69 70 # 2. Fetch Questions71 print(f"Fetching questions from: {questions_url}")72 try:73 response = requests.get(questions_url, timeout=15)74 response.raise_for_status()75 questions_data = response.json()76 if not questions_data:77 print("Fetched questions list is empty.")78 return "Fetched questions list is empty or invalid format.", None79 print(f"Fetched {len(questions_data)} questions.")80 except requests.exceptions.RequestException as e:81 print(f"Error fetching questions: {e}")82 return f"Error fetching questions: {e}", None83 except requests.exceptions.JSONDecodeError as e:84 print(f"Error decoding JSON response from questions endpoint: {e}")85 print(f"Response text: {response.text[:500]}")86 return f"Error decoding server response for questions: {e}", None87 except Exception as e:88 print(f"An unexpected error occurred fetching questions: {e}")89 return f"An unexpected error occurred fetching questions: {e}", None90 91 # 3. Run your Agent92 results_log = []93 answers_payload = []94 print(f"Running agent on {len(questions_data)} questions...")95 for item in questions_data:96 task_id = item.get("task_id")97 question_text = item.get("question")98 if not task_id or question_text is None:99 print(f"Skipping item with missing task_id or question: {item}")100 continue101 try:102 submitted_answer = agent(question_text)103 answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})104 results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})105 except Exception as e:106 print(f"Error running agent on task {task_id}: {e}")107 results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})108 109 if not answers_payload:110 print("Agent did not produce any answers to submit.")111 return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)112 113 # 4. Prepare Submission 114 submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}115 status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."116 print(status_update)117 118 # 5. Submit119 print(f"Submitting {len(answers_payload)} answers to: {submit_url}")120 try:121 response = requests.post(submit_url, json=submission_data, timeout=60)122 response.raise_for_status()123 result_data = response.json()124 final_status = (125 f"Submission Successful!\n"126 f"User: {result_data.get('username')}\n"127 f"Overall Score: {result_data.get('score', 'N/A')}% "128 f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"129 f"Message: {result_data.get('message', 'No message received.')}"130 )131 print("Submission successful.")132 results_df = pd.DataFrame(results_log)133 return final_status, results_df134 except requests.exceptions.HTTPError as e:135 error_detail = f"Server responded with status {e.response.status_code}."136 try:137 error_json = e.response.json()138 error_detail += f" Detail: {error_json.get('detail', e.response.text)}"139 except requests.exceptions.JSONDecodeError:140 error_detail += f" Response: {e.response.text[:500]}"141 status_message = f"Submission Failed: {error_detail}"142 print(status_message)143 results_df = pd.DataFrame(results_log)144 return status_message, results_df145 except requests.exceptions.Timeout:146 status_message = "Submission Failed: The request timed out."147 print(status_message)148 results_df = pd.DataFrame(results_log)149 return status_message, results_df150 except requests.exceptions.RequestException as e:151 status_message = f"Submission Failed: Network error - {e}"152 print(status_message)153 results_df = pd.DataFrame(results_log)154 return status_message, results_df155 except Exception as e:156 status_message = f"An unexpected error occurred during submission: {e}"157 print(status_message)158 results_df = pd.DataFrame(results_log)159 return status_message, results_df160 161 162# --- Build Gradio Interface using Blocks ---163with gr.Blocks() as demo:164 gr.Markdown("# Basic Agent Evaluation Runner")165 gr.Markdown(166 """167 **Instructions:**168 1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...169 2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.170 3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.171 ---172 **Disclaimers:**173 Once clicking on the "submit button, it can take quite some time ( this is the time for the agent to go through all the questions).174 This space provides a basic setup and is intentionally sub-optimal to encourage you to develop your own, more robust solution. For instance for the delay process of the submit button, a solution could be to cache the answers and submit in a seperate action or even to answer the questions in async.175 """176 )177 178 gr.LoginButton()179 180 run_button = gr.Button("Run Evaluation & Submit All Answers")181 182 status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)183 # Removed max_rows=10 from DataFrame constructor184 results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)185 186 run_button.click(187 fn=run_and_submit_all,188 outputs=[status_output, results_table]189 )190 191if __name__ == "__main__":192 print("\n" + "-"*30 + " App Starting " + "-"*30)193 # Check for SPACE_HOST and SPACE_ID at startup for information194 space_host_startup = os.getenv("SPACE_HOST")195 space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup196 197 if space_host_startup:198 print(f"✅ SPACE_HOST found: {space_host_startup}")199 print(f" Runtime URL should be: https://{space_host_startup}.hf.space")200 else:201 print("ℹ️ SPACE_HOST environment variable not found (running locally?).")202 203 if space_id_startup: # Print repo URLs if SPACE_ID is found204 print(f"✅ SPACE_ID found: {space_id_startup}")205 print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")206 print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")207 else:208 print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")209 210 print("-"*(60 + len(" App Starting ")) + "\n")211 212 print("Launching Gradio Interface for Basic Agent Evaluation...")213 demo.launch(debug=True, share=False)214 