bitcloud2/Final_Assignment_Template
1
1import os2import gradio as gr3import requests4import inspect5import pandas as pd6 7from my_agent import SmolAgent # Import the new agent8 9# (Keep Constants as is)10# --- Constants ---11DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"12 13 14# --- Basic Agent Definition ---15# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------16class BasicAgent:17 def __init__(self):18 print("BasicAgent initialized.")19 20 def __call__(self, question: str) -> str:21 print(f"Agent received question (first 50 chars): {question[:50]}...")22 fixed_answer = "This is a default answer."23 print(f"Agent returning fixed answer: {fixed_answer}")24 return fixed_answer25 26 27def instantiate_agent():28 """Instantiates the agent."""29 try:30 # agent = BasicAgent()31 agent = SmolAgent() # Use the new agent32 return agent, None # Return agent and no error33 except Exception as e:34 print(f"Error instantiating agent: {e}")35 return None, f"Error initializing agent: {e}" # Return None and error message36 37 38def fetch_questions(questions_url: str):39 """Fetches questions from the specified URL."""40 print(f"Fetching questions from: {questions_url}")41 try:42 response = requests.get(questions_url, timeout=15)43 response.raise_for_status()44 questions_data = response.json()45 if not questions_data:46 print("Fetched questions list is empty.")47 return None, "Fetched questions list is empty or invalid format."48 print(f"Fetched {len(questions_data)} questions.")49 return questions_data, None # Return data and no error50 except requests.exceptions.RequestException as e:51 print(f"Error fetching questions: {e}")52 return None, f"Error fetching questions: {e}"53 except requests.exceptions.JSONDecodeError as e:54 print(f"Error decoding JSON response from questions endpoint: {e}")55 print(f"Response text: {response.text[:500]}")56 return None, f"Error decoding server response for questions: {e}"57 except Exception as e:58 print(f"An unexpected error occurred fetching questions: {e}")59 return None, f"An unexpected error occurred fetching questions: {e}"60 61 62def run_agent_on_questions(agent, questions_data):63 """Runs the agent on each question and collects results."""64 results_log = []65 answers_payload = []66 print(f"Running agent on {len(questions_data)} questions...")67 for item in questions_data:68 task_id = item.get("task_id")69 question_text = item.get("question")70 if not task_id or question_text is None:71 print(f"Skipping item with missing task_id or question: {item}")72 continue73 try:74 submitted_answer = agent(question_text)75 answers_payload.append(76 {"task_id": task_id, "submitted_answer": submitted_answer}77 )78 results_log.append(79 {80 "Task ID": task_id,81 "Question": question_text,82 "Submitted Answer": submitted_answer,83 }84 )85 except Exception as e:86 print(f"Error running agent on task {task_id}: {e}")87 results_log.append(88 {89 "Task ID": task_id,90 "Question": question_text,91 "Submitted Answer": f"AGENT ERROR: {e}",92 }93 )94 return answers_payload, results_log95 96 97def dev_run():98 """99 Fetches all questions, runs the BasicAgent on them,100 and displays the results.101 """102 api_url = DEFAULT_API_URL103 questions_url = f"{api_url}/questions"104 105 agent, error_message = instantiate_agent()106 if error_message:107 return error_message, None # Return error message and None for results_df108 109 # 2. Fetch Questions110 questions_data, error_message = fetch_questions(questions_url)111 if error_message:112 # Return the error message from fetch_questions and None for the results DataFrame113 return error_message, None114 115 # 3. Run your Agent116 answers_payload, results_log = run_agent_on_questions(agent, questions_data)117 118 if not answers_payload:119 print("Agent did not produce any answers to submit.")120 return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)121 122 return answers_payload, pd.DataFrame(results_log)123 124 125def run_and_submit_all(profile: gr.OAuthProfile | None):126 """127 Fetches all questions, runs the BasicAgent on them, submits all answers,128 and displays the results.129 """130 # --- Determine HF Space Runtime URL and Repo URL ---131 space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code132 133 if profile:134 username = f"{profile.username}"135 print(f"User logged in: {username}")136 else:137 print("User not logged in.")138 return "Please Login to Hugging Face with the button.", None139 140 api_url = DEFAULT_API_URL141 questions_url = f"{api_url}/questions"142 submit_url = f"{api_url}/submit"143 144 # 1. Instantiate Agent ( modify this part to create your agent)145 agent, error_message = instantiate_agent()146 if error_message:147 return error_message, None # Return error message and None for results_df148 149 # 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)150 agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"151 print(agent_code)152 153 # 2. Fetch Questions154 questions_data, error_message = fetch_questions(questions_url)155 if error_message:156 # Return the error message from fetch_questions and None for the results DataFrame157 return error_message, None158 159 # 3. Run your Agent160 answers_payload, results_log = run_agent_on_questions(agent, questions_data)161 162 if not answers_payload:163 print("Agent did not produce any answers to submit.")164 return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)165 166 # 4. Prepare Submission167 submission_data = {168 "username": username.strip(),169 "agent_code": agent_code,170 "answers": answers_payload,171 }172 status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."173 print(status_update)174 175 # 5. Submit176 print(f"Submitting {len(answers_payload)} answers to: {submit_url}")177 try:178 response = requests.post(submit_url, json=submission_data, timeout=60)179 response.raise_for_status()180 result_data = response.json()181 final_status = (182 f"Submission Successful!\n"183 f"User: {result_data.get('username')}\n"184 f"Overall Score: {result_data.get('score', 'N/A')}% "185 f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"186 f"Message: {result_data.get('message', 'No message received.')}"187 )188 print("Submission successful.")189 results_df = pd.DataFrame(results_log)190 return final_status, results_df191 except requests.exceptions.HTTPError as e:192 error_detail = f"Server responded with status {e.response.status_code}."193 try:194 error_json = e.response.json()195 error_detail += f" Detail: {error_json.get('detail', e.response.text)}"196 except requests.exceptions.JSONDecodeError:197 error_detail += f" Response: {e.response.text[:500]}"198 status_message = f"Submission Failed: {error_detail}"199 print(status_message)200 results_df = pd.DataFrame(results_log)201 return status_message, results_df202 except requests.exceptions.Timeout:203 status_message = "Submission Failed: The request timed out."204 print(status_message)205 results_df = pd.DataFrame(results_log)206 return status_message, results_df207 except requests.exceptions.RequestException as e:208 status_message = f"Submission Failed: Network error - {e}"209 print(status_message)210 results_df = pd.DataFrame(results_log)211 return status_message, results_df212 except Exception as e:213 status_message = f"An unexpected error occurred during submission: {e}"214 print(status_message)215 results_df = pd.DataFrame(results_log)216 return status_message, results_df217 218 219# # --- Build Gradio Interface using Blocks ---220# with gr.Blocks() as demo:221# gr.Markdown("# Basic Agent Evaluation Runner")222# gr.Markdown(223# """224# **Instructions:**225 226# 1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...227# 2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.228# 3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.229 230# ---231# **Disclaimers:**232# 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).233# 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.234# """235# )236 237# gr.LoginButton()238 239# run_button = gr.Button("Run Evaluation & Submit All Answers")240 241# status_output = gr.Textbox(242# label="Run Status / Submission Result", lines=5, interactive=False243# )244# # Removed max_rows=10 from DataFrame constructor245# results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)246 247# run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])248 249# if __name__ == "__main__":250# print("\n" + "-" * 30 + " App Starting " + "-" * 30)251# # Check for SPACE_HOST and SPACE_ID at startup for information252# space_host_startup = os.getenv("SPACE_HOST")253# space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup254 255# if space_host_startup:256# print(f"✅ SPACE_HOST found: {space_host_startup}")257# print(f" Runtime URL should be: https://{space_host_startup}.hf.space")258# else:259# print("ℹ️ SPACE_HOST environment variable not found (running locally?).")260 261# if space_id_startup: # Print repo URLs if SPACE_ID is found262# print(f"✅ SPACE_ID found: {space_id_startup}")263# print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")264# print(265# f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main"266# )267# else:268# print(269# "ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined."270# )271 272# print("-" * (60 + len(" App Starting ")) + "\n")273 274# print("Launching Gradio Interface for Basic Agent Evaluation...")275# demo.launch(debug=True, share=False)276 277if __name__ == "__main__":278 answers_payload, results_log = dev_run()279 print(answers_payload)280 print(results_log)281 