Goodguygregory93/ai-agents-certification-code
0
1import os2import gradio as gr3import requests4import inspect5import pandas as pd6from basic_agent import BasicAgent7 8 9# (Keep Constants as is)10# --- Constants ---11DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"12 13async def run_and_submit_all( profile: gr.OAuthProfile | None):14 """15 Fetches all questions, runs the BasicAgent on them, submits all answers,16 and displays the results.17 """18 # --- Determine HF Space Runtime URL and Repo URL ---19 space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code20 21 if profile:22 username= f"{profile.username}"23 print(f"User logged in: {username}")24 else:25 print("User not logged in.")26 return "Please Login to Hugging Face with the button.", None27 28 api_url = DEFAULT_API_URL29 questions_url = f"{api_url}/questions"30 submit_url = f"{api_url}/submit"31 32 # 1. Instantiate Agent ( modify this part to create your agent)33 try:34 agent = BasicAgent()35 except Exception as e:36 print(f"Error instantiating agent: {e}")37 return f"Error initializing agent: {e}", None38 # 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)39 agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"40 print(agent_code)41 42 # 2. Fetch Questions43 print(f"Fetching questions from: {questions_url}")44 try:45 response = requests.get(questions_url, timeout=15)46 response.raise_for_status()47 questions_data = response.json()48 if not questions_data:49 print("Fetched questions list is empty.")50 return "Fetched questions list is empty or invalid format.", None51 print(f"Fetched {len(questions_data)} questions.")52 except requests.exceptions.RequestException as e:53 print(f"Error fetching questions: {e}")54 return f"Error fetching questions: {e}", None55 except requests.exceptions.JSONDecodeError as e:56 print(f"Error decoding JSON response from questions endpoint: {e}")57 print(f"Response text: {response.text[:500]}")58 return f"Error decoding server response for questions: {e}", None59 except Exception as e:60 print(f"An unexpected error occurred fetching questions: {e}")61 return f"An unexpected error occurred fetching questions: {e}", None62 63 # 3. Run your Agent64 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 = await agent(question_text)75 if 'FINAL ANSWER' in submitted_answer:76 stripped_answer = submitted_answer.split('FINAL ANSWER:')[1].strip()77 78 answers_payload.append({"task_id": task_id, "submitted_answer": stripped_answer})79 80 answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})81 results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})82 except Exception as e:83 print(f"Error running agent on task {task_id}: {e}")84 results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})85 86 if not answers_payload:87 print("Agent did not produce any answers to submit.")88 return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)89 90 # 4. Prepare Submission 91 submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}92 status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."93 print(status_update)94 95 # 5. Submit96 print(f"Submitting {len(answers_payload)} answers to: {submit_url}")97 try:98 response = requests.post(submit_url, json=submission_data, timeout=60)99 response.raise_for_status()100 result_data = response.json()101 final_status = (102 f"Submission Successful!\n"103 f"User: {result_data.get('username')}\n"104 f"Overall Score: {result_data.get('score', 'N/A')}% "105 f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"106 f"Message: {result_data.get('message', 'No message received.')}"107 )108 print("Submission successful.")109 results_df = pd.DataFrame(results_log)110 return final_status, results_df111 except requests.exceptions.HTTPError as e:112 error_detail = f"Server responded with status {e.response.status_code}."113 try:114 error_json = e.response.json()115 error_detail += f" Detail: {error_json.get('detail', e.response.text)}"116 except requests.exceptions.JSONDecodeError:117 error_detail += f" Response: {e.response.text[:500]}"118 status_message = f"Submission Failed: {error_detail}"119 print(status_message)120 results_df = pd.DataFrame(results_log)121 return status_message, results_df122 except requests.exceptions.Timeout:123 status_message = "Submission Failed: The request timed out."124 print(status_message)125 results_df = pd.DataFrame(results_log)126 return status_message, results_df127 except requests.exceptions.RequestException as e:128 status_message = f"Submission Failed: Network error - {e}"129 print(status_message)130 results_df = pd.DataFrame(results_log)131 return status_message, results_df132 except Exception as e:133 status_message = f"An unexpected error occurred during submission: {e}"134 print(status_message)135 results_df = pd.DataFrame(results_log)136 return status_message, results_df137 138 139# --- Build Gradio Interface using Blocks ---140with gr.Blocks() as demo:141 gr.Markdown("# Basic Agent Evaluation Runner")142 gr.Markdown(143 """144 **Instructions:**145 1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...146 2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.147 3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.148 ---149 **Disclaimers:**150 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).151 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.152 """153 )154 155 gr.LoginButton()156 157 run_button = gr.Button("Run Evaluation & Submit All Answers")158 159 status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)160 # Removed max_rows=10 from DataFrame constructor161 results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)162 163 run_button.click(164 fn=run_and_submit_all,165 outputs=[status_output, results_table]166 )167 168if __name__ == "__main__":169 print("\n" + "-"*30 + " App Starting " + "-"*30)170 # Check for SPACE_HOST and SPACE_ID at startup for information171 space_host_startup = os.getenv("SPACE_HOST")172 space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup173 174 if space_host_startup:175 print(f"✅ SPACE_HOST found: {space_host_startup}")176 print(f" Runtime URL should be: https://{space_host_startup}.hf.space")177 else:178 print("ℹ️ SPACE_HOST environment variable not found (running locally?).")179 180 if space_id_startup: # Print repo URLs if SPACE_ID is found181 print(f"✅ SPACE_ID found: {space_id_startup}")182 print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")183 print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")184 else:185 print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")186 187 print("-"*(60 + len(" App Starting ")) + "\n")188 189 print("Launching Gradio Interface for Basic Agent Evaluation...")190 demo.launch(debug=True, share=False)