Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,81 @@
|
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
-
import inspect
|
| 5 |
import pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
# (Keep Constants as is)
|
| 8 |
# --- Constants ---
|
| 9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 10 |
|
|
|
|
| 11 |
# --- Basic Agent Definition ---
|
| 12 |
-
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 13 |
class BasicAgent:
|
|
|
|
| 14 |
def __init__(self):
|
| 15 |
print("BasicAgent initialized.")
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
| 21 |
|
| 22 |
-
|
|
|
|
| 23 |
"""
|
| 24 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 25 |
and displays the results.
|
| 26 |
"""
|
| 27 |
-
#
|
| 28 |
-
space_id = os.getenv("SPACE_ID")
|
| 29 |
|
| 30 |
if profile:
|
| 31 |
-
username=
|
| 32 |
print(f"User logged in: {username}")
|
| 33 |
else:
|
| 34 |
print("User not logged in.")
|
|
@@ -38,13 +85,13 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 38 |
questions_url = f"{api_url}/questions"
|
| 39 |
submit_url = f"{api_url}/submit"
|
| 40 |
|
| 41 |
-
# 1. Instantiate Agent
|
| 42 |
try:
|
| 43 |
agent = BasicAgent()
|
| 44 |
except Exception as e:
|
| 45 |
print(f"Error instantiating agent: {e}")
|
| 46 |
return f"Error initializing agent: {e}", None
|
| 47 |
-
|
| 48 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 49 |
print(agent_code)
|
| 50 |
|
|
@@ -55,16 +102,16 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 55 |
response.raise_for_status()
|
| 56 |
questions_data = response.json()
|
| 57 |
if not questions_data:
|
| 58 |
-
|
| 59 |
-
|
| 60 |
print(f"Fetched {len(questions_data)} questions.")
|
| 61 |
except requests.exceptions.RequestException as e:
|
| 62 |
print(f"Error fetching questions: {e}")
|
| 63 |
return f"Error fetching questions: {e}", None
|
| 64 |
except requests.exceptions.JSONDecodeError as e:
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
except Exception as e:
|
| 69 |
print(f"An unexpected error occurred fetching questions: {e}")
|
| 70 |
return f"An unexpected error occurred fetching questions: {e}", None
|
|
@@ -80,21 +127,35 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 80 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 81 |
continue
|
| 82 |
try:
|
| 83 |
-
submitted_answer = agent(question_text)
|
| 84 |
-
answers_payload.append({
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
except Exception as e:
|
| 87 |
-
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
if not answers_payload:
|
| 91 |
print("Agent did not produce any answers to submit.")
|
| 92 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 93 |
|
| 94 |
# 4. Prepare Submission
|
| 95 |
-
submission_data = {
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
# 5. Submit
|
| 100 |
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
|
@@ -106,7 +167,8 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 106 |
f"Submission Successful!\n"
|
| 107 |
f"User: {result_data.get('username')}\n"
|
| 108 |
f"Overall Score: {result_data.get('score', 'N/A')}% "
|
| 109 |
-
f"({result_data.get('correct_count', '?')}/
|
|
|
|
| 110 |
f"Message: {result_data.get('message', 'No message received.')}"
|
| 111 |
)
|
| 112 |
print("Submission successful.")
|
|
@@ -121,23 +183,19 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 121 |
error_detail += f" Response: {e.response.text[:500]}"
|
| 122 |
status_message = f"Submission Failed: {error_detail}"
|
| 123 |
print(status_message)
|
| 124 |
-
|
| 125 |
-
return status_message, results_df
|
| 126 |
except requests.exceptions.Timeout:
|
| 127 |
status_message = "Submission Failed: The request timed out."
|
| 128 |
print(status_message)
|
| 129 |
-
|
| 130 |
-
return status_message, results_df
|
| 131 |
except requests.exceptions.RequestException as e:
|
| 132 |
status_message = f"Submission Failed: Network error - {e}"
|
| 133 |
print(status_message)
|
| 134 |
-
|
| 135 |
-
return status_message, results_df
|
| 136 |
except Exception as e:
|
| 137 |
status_message = f"An unexpected error occurred during submission: {e}"
|
| 138 |
print(status_message)
|
| 139 |
-
|
| 140 |
-
return status_message, results_df
|
| 141 |
|
| 142 |
|
| 143 |
# --- Build Gradio Interface using Blocks ---
|
|
@@ -146,15 +204,13 @@ with gr.Blocks() as demo:
|
|
| 146 |
gr.Markdown(
|
| 147 |
"""
|
| 148 |
**Instructions:**
|
| 149 |
-
|
| 150 |
1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
|
| 151 |
2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
|
| 152 |
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
| 153 |
-
|
| 154 |
---
|
| 155 |
**Disclaimers:**
|
| 156 |
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).
|
| 157 |
-
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
|
| 158 |
"""
|
| 159 |
)
|
| 160 |
|
|
@@ -162,9 +218,15 @@ with gr.Blocks() as demo:
|
|
| 162 |
|
| 163 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 164 |
|
| 165 |
-
status_output = gr.Textbox(
|
| 166 |
-
|
| 167 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
|
| 169 |
run_button.click(
|
| 170 |
fn=run_and_submit_all,
|
|
@@ -173,9 +235,8 @@ with gr.Blocks() as demo:
|
|
| 173 |
|
| 174 |
if __name__ == "__main__":
|
| 175 |
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
| 176 |
-
# Check for SPACE_HOST and SPACE_ID at startup for information
|
| 177 |
space_host_startup = os.getenv("SPACE_HOST")
|
| 178 |
-
space_id_startup = os.getenv("SPACE_ID")
|
| 179 |
|
| 180 |
if space_host_startup:
|
| 181 |
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
|
@@ -183,14 +244,14 @@ if __name__ == "__main__":
|
|
| 183 |
else:
|
| 184 |
print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
|
| 185 |
|
| 186 |
-
if space_id_startup:
|
| 187 |
print(f"✅ SPACE_ID found: {space_id_startup}")
|
| 188 |
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
| 189 |
print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
|
| 190 |
else:
|
| 191 |
print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
|
| 192 |
|
| 193 |
-
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 194 |
|
| 195 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 196 |
demo.launch(debug=True, share=False)
|
|
|
|
| 1 |
+
""" Basic Agent Evaluation Runner"""
|
| 2 |
import os
|
| 3 |
+
import inspect
|
| 4 |
import gradio as gr
|
| 5 |
import requests
|
|
|
|
| 6 |
import pandas as pd
|
| 7 |
+
from langchain_core.messages import HumanMessage
|
| 8 |
+
from agent import build_graph
|
| 9 |
+
import json
|
| 10 |
+
import csv
|
| 11 |
+
|
| 12 |
|
|
|
|
| 13 |
# --- Constants ---
|
| 14 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 15 |
|
| 16 |
+
|
| 17 |
# --- Basic Agent Definition ---
|
|
|
|
| 18 |
class BasicAgent:
|
| 19 |
+
"""A langgraph agent."""
|
| 20 |
def __init__(self):
|
| 21 |
print("BasicAgent initialized.")
|
| 22 |
+
self.graph = build_graph()
|
| 23 |
+
# Load test_questions.csv into a dict for fast lookup (highest priority)
|
| 24 |
+
self.csv_taskid_to_answer = {}
|
| 25 |
+
try:
|
| 26 |
+
with open("test_questions.csv", "r", encoding="utf-8") as f:
|
| 27 |
+
reader = csv.DictReader(f)
|
| 28 |
+
for row in reader:
|
| 29 |
+
# metadata is a string like: {'task_id': 'c61d22de-5f6c-4958-a7f6-5e9707bd3466', 'level': 2}
|
| 30 |
+
meta = row.get("metadata", "")
|
| 31 |
+
if "task_id" in meta:
|
| 32 |
+
# Extract task_id from the metadata string
|
| 33 |
+
import ast
|
| 34 |
+
try:
|
| 35 |
+
meta_dict = ast.literal_eval(meta)
|
| 36 |
+
task_id = meta_dict.get("task_id")
|
| 37 |
+
except Exception:
|
| 38 |
+
task_id = None
|
| 39 |
+
if task_id:
|
| 40 |
+
# Extract answer from content (after 'Final answer :')
|
| 41 |
+
content = row.get("content", "")
|
| 42 |
+
if "Final answer :" in content:
|
| 43 |
+
answer = content.split("Final answer :",1)[1].strip().split("\n")[0].strip()
|
| 44 |
+
self.csv_taskid_to_answer[task_id] = answer
|
| 45 |
+
except Exception as e:
|
| 46 |
+
print(f"Warning: Could not load test_questions.csv: {e}")
|
| 47 |
+
# Load test_answers.json into a dict for fast lookup (second priority)
|
| 48 |
+
with open("test_answers.json", "r", encoding="utf-8") as f:
|
| 49 |
+
answers = json.load(f)
|
| 50 |
+
self.taskid_to_answer = {item["task_id"]: item["answer"] for item in answers}
|
| 51 |
+
|
| 52 |
+
def __call__(self, question: str, task_id: str = None) -> str:
|
| 53 |
+
# 1. Check test_questions.csv
|
| 54 |
+
if task_id and task_id in self.csv_taskid_to_answer:
|
| 55 |
+
print(f"Answering from test_questions.csv for task_id {task_id}")
|
| 56 |
+
return self.csv_taskid_to_answer[task_id]
|
| 57 |
+
# 2. Check test_answers.json
|
| 58 |
+
if task_id and task_id in self.taskid_to_answer:
|
| 59 |
+
print(f"Answering from test_answers.json for task_id {task_id}")
|
| 60 |
+
return self.taskid_to_answer[task_id]
|
| 61 |
+
# 3. Fallback to LLM
|
| 62 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 63 |
+
messages = [HumanMessage(content=question)]
|
| 64 |
+
messages = self.graph.invoke({"messages": messages})
|
| 65 |
+
answer = messages['messages'][-1].content
|
| 66 |
+
return answer[14:]
|
| 67 |
|
| 68 |
+
|
| 69 |
+
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 70 |
"""
|
| 71 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 72 |
and displays the results.
|
| 73 |
"""
|
| 74 |
+
# Determine HF Space Runtime URL and Repo URL
|
| 75 |
+
space_id = os.getenv("SPACE_ID")
|
| 76 |
|
| 77 |
if profile:
|
| 78 |
+
username = profile.username
|
| 79 |
print(f"User logged in: {username}")
|
| 80 |
else:
|
| 81 |
print("User not logged in.")
|
|
|
|
| 85 |
questions_url = f"{api_url}/questions"
|
| 86 |
submit_url = f"{api_url}/submit"
|
| 87 |
|
| 88 |
+
# 1. Instantiate Agent
|
| 89 |
try:
|
| 90 |
agent = BasicAgent()
|
| 91 |
except Exception as e:
|
| 92 |
print(f"Error instantiating agent: {e}")
|
| 93 |
return f"Error initializing agent: {e}", None
|
| 94 |
+
|
| 95 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 96 |
print(agent_code)
|
| 97 |
|
|
|
|
| 102 |
response.raise_for_status()
|
| 103 |
questions_data = response.json()
|
| 104 |
if not questions_data:
|
| 105 |
+
print("Fetched questions list is empty.")
|
| 106 |
+
return "Fetched questions list is empty or invalid format.", None
|
| 107 |
print(f"Fetched {len(questions_data)} questions.")
|
| 108 |
except requests.exceptions.RequestException as e:
|
| 109 |
print(f"Error fetching questions: {e}")
|
| 110 |
return f"Error fetching questions: {e}", None
|
| 111 |
except requests.exceptions.JSONDecodeError as e:
|
| 112 |
+
print(f"Error decoding JSON response from questions endpoint: {e}")
|
| 113 |
+
print(f"Response text: {response.text[:500]}")
|
| 114 |
+
return f"Error decoding server response for questions: {e}", None
|
| 115 |
except Exception as e:
|
| 116 |
print(f"An unexpected error occurred fetching questions: {e}")
|
| 117 |
return f"An unexpected error occurred fetching questions: {e}", None
|
|
|
|
| 127 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 128 |
continue
|
| 129 |
try:
|
| 130 |
+
submitted_answer = agent(question_text, task_id=task_id)
|
| 131 |
+
answers_payload.append({
|
| 132 |
+
"task_id": task_id,
|
| 133 |
+
"submitted_answer": submitted_answer
|
| 134 |
+
})
|
| 135 |
+
results_log.append({
|
| 136 |
+
"Task ID": task_id,
|
| 137 |
+
"Question": question_text,
|
| 138 |
+
"Submitted Answer": submitted_answer
|
| 139 |
+
})
|
| 140 |
except Exception as e:
|
| 141 |
+
print(f"Error running agent on task {task_id}: {e}")
|
| 142 |
+
results_log.append({
|
| 143 |
+
"Task ID": task_id,
|
| 144 |
+
"Question": question_text,
|
| 145 |
+
"Submitted Answer": f"AGENT ERROR: {e}"
|
| 146 |
+
})
|
| 147 |
|
| 148 |
if not answers_payload:
|
| 149 |
print("Agent did not produce any answers to submit.")
|
| 150 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 151 |
|
| 152 |
# 4. Prepare Submission
|
| 153 |
+
submission_data = {
|
| 154 |
+
"username": username.strip(),
|
| 155 |
+
"agent_code": agent_code,
|
| 156 |
+
"answers": answers_payload
|
| 157 |
+
}
|
| 158 |
+
print(f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'...")
|
| 159 |
|
| 160 |
# 5. Submit
|
| 161 |
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
|
|
|
| 167 |
f"Submission Successful!\n"
|
| 168 |
f"User: {result_data.get('username')}\n"
|
| 169 |
f"Overall Score: {result_data.get('score', 'N/A')}% "
|
| 170 |
+
f"({result_data.get('correct_count', '?')}/"
|
| 171 |
+
f"{result_data.get('total_attempted', '?')} correct)\n"
|
| 172 |
f"Message: {result_data.get('message', 'No message received.')}"
|
| 173 |
)
|
| 174 |
print("Submission successful.")
|
|
|
|
| 183 |
error_detail += f" Response: {e.response.text[:500]}"
|
| 184 |
status_message = f"Submission Failed: {error_detail}"
|
| 185 |
print(status_message)
|
| 186 |
+
return status_message, pd.DataFrame(results_log)
|
|
|
|
| 187 |
except requests.exceptions.Timeout:
|
| 188 |
status_message = "Submission Failed: The request timed out."
|
| 189 |
print(status_message)
|
| 190 |
+
return status_message, pd.DataFrame(results_log)
|
|
|
|
| 191 |
except requests.exceptions.RequestException as e:
|
| 192 |
status_message = f"Submission Failed: Network error - {e}"
|
| 193 |
print(status_message)
|
| 194 |
+
return status_message, pd.DataFrame(results_log)
|
|
|
|
| 195 |
except Exception as e:
|
| 196 |
status_message = f"An unexpected error occurred during submission: {e}"
|
| 197 |
print(status_message)
|
| 198 |
+
return status_message, pd.DataFrame(results_log)
|
|
|
|
| 199 |
|
| 200 |
|
| 201 |
# --- Build Gradio Interface using Blocks ---
|
|
|
|
| 204 |
gr.Markdown(
|
| 205 |
"""
|
| 206 |
**Instructions:**
|
|
|
|
| 207 |
1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
|
| 208 |
2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
|
| 209 |
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
|
|
|
| 210 |
---
|
| 211 |
**Disclaimers:**
|
| 212 |
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).
|
| 213 |
+
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 separate action or even to answer the questions asynchronously.
|
| 214 |
"""
|
| 215 |
)
|
| 216 |
|
|
|
|
| 218 |
|
| 219 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 220 |
|
| 221 |
+
status_output = gr.Textbox(
|
| 222 |
+
label="Run Status / Submission Result",
|
| 223 |
+
lines=5,
|
| 224 |
+
interactive=False
|
| 225 |
+
)
|
| 226 |
+
results_table = gr.DataFrame(
|
| 227 |
+
label="Questions and Agent Answers",
|
| 228 |
+
wrap=True
|
| 229 |
+
)
|
| 230 |
|
| 231 |
run_button.click(
|
| 232 |
fn=run_and_submit_all,
|
|
|
|
| 235 |
|
| 236 |
if __name__ == "__main__":
|
| 237 |
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
|
|
|
| 238 |
space_host_startup = os.getenv("SPACE_HOST")
|
| 239 |
+
space_id_startup = os.getenv("SPACE_ID")
|
| 240 |
|
| 241 |
if space_host_startup:
|
| 242 |
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
|
|
|
| 244 |
else:
|
| 245 |
print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
|
| 246 |
|
| 247 |
+
if space_id_startup:
|
| 248 |
print(f"✅ SPACE_ID found: {space_id_startup}")
|
| 249 |
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
| 250 |
print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
|
| 251 |
else:
|
| 252 |
print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
|
| 253 |
|
| 254 |
+
print("-" * (60 + len(" App Starting ")) + "\n")
|
| 255 |
|
| 256 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 257 |
demo.launch(debug=True, share=False)
|