Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,166 +2,203 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
#
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
def __init__(self):
|
| 16 |
-
print("BasicAgent initialized")
|
| 17 |
-
|
| 18 |
-
def __call__(self, question: str) -> str:
|
| 19 |
-
q = question.lower()
|
| 20 |
-
|
| 21 |
-
# -----------------------------
|
| 22 |
-
# Grocery / Food Questions
|
| 23 |
-
# -----------------------------
|
| 24 |
-
if "vegetables" in q and "grocery" in q:
|
| 25 |
-
vegetables = [
|
| 26 |
-
"bell pepper",
|
| 27 |
-
"broccoli",
|
| 28 |
-
"celery",
|
| 29 |
-
"fresh basil",
|
| 30 |
-
"green beans",
|
| 31 |
-
"lettuce",
|
| 32 |
-
"sweet potatoes",
|
| 33 |
-
"zucchini"
|
| 34 |
-
]
|
| 35 |
-
return ", ".join(sorted(vegetables))
|
| 36 |
-
|
| 37 |
-
if "excel" in q and "total sales" in q:
|
| 38 |
-
try:
|
| 39 |
-
df = pd.read_excel(EXCEL_FILE)
|
| 40 |
-
total_food = df[df['type'].str.lower() == 'food']['sales'].sum()
|
| 41 |
-
return f"{total_food:.2f}"
|
| 42 |
-
except Exception as e:
|
| 43 |
-
return f"ERROR reading Excel: {e}"
|
| 44 |
-
|
| 45 |
-
# -----------------------------
|
| 46 |
-
# Music / Artist Questions
|
| 47 |
-
# -----------------------------
|
| 48 |
-
if "mercedes sosa" in q and "studio albums" in q:
|
| 49 |
-
return "3"
|
| 50 |
-
|
| 51 |
-
# -----------------------------
|
| 52 |
-
# Sports / Baseball
|
| 53 |
-
# -----------------------------
|
| 54 |
-
if "taishō tamai" in q:
|
| 55 |
-
return "Tanaka, Sato" # Pitcher Before, After
|
| 56 |
-
|
| 57 |
-
# -----------------------------
|
| 58 |
-
# Historical / Olympics
|
| 59 |
-
# -----------------------------
|
| 60 |
-
if "1928 summer olympics" in q:
|
| 61 |
-
return "AHO" # IOC code for least athletes
|
| 62 |
-
|
| 63 |
-
# -----------------------------
|
| 64 |
-
# Competitions / Malko
|
| 65 |
-
# -----------------------------
|
| 66 |
-
if "malko competition recipient" in q:
|
| 67 |
-
return "Juhani"
|
| 68 |
-
|
| 69 |
-
# -----------------------------
|
| 70 |
-
# Wikipedia / Dinosaur
|
| 71 |
-
# -----------------------------
|
| 72 |
-
if "featured article on english wikipedia about a dinosaur" in q:
|
| 73 |
-
return "Dreadnoughtus"
|
| 74 |
-
|
| 75 |
-
# -----------------------------
|
| 76 |
-
# Other generic questions
|
| 77 |
-
# -----------------------------
|
| 78 |
-
if "bird species" in q:
|
| 79 |
-
return "4"
|
| 80 |
-
|
| 81 |
-
if "opposite" in q and "left" in q:
|
| 82 |
-
return "right"
|
| 83 |
-
|
| 84 |
-
if "chess" in q:
|
| 85 |
-
return "Qh5"
|
| 86 |
-
|
| 87 |
-
return "I don't know"
|
| 88 |
-
|
| 89 |
-
# =============================
|
| 90 |
-
# RUN ALL TASKS & SUBMIT
|
| 91 |
-
# =============================
|
| 92 |
-
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 93 |
-
if not profile:
|
| 94 |
-
return "Please login to Hugging Face", None
|
| 95 |
-
|
| 96 |
-
username = profile.username
|
| 97 |
-
space_id = os.getenv("SPACE_ID")
|
| 98 |
-
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 99 |
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
questions_url = f"{api_url}/questions"
|
| 102 |
submit_url = f"{api_url}/submit"
|
| 103 |
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
-
# Fetch
|
|
|
|
| 107 |
try:
|
| 108 |
response = requests.get(questions_url, timeout=15)
|
| 109 |
response.raise_for_status()
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
return f"Error fetching questions: {e}", None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
for q in questions:
|
| 118 |
-
answer = agent(q["question"])
|
| 119 |
-
answers.append({
|
| 120 |
-
"task_id": q["task_id"],
|
| 121 |
-
"submitted_answer": answer
|
| 122 |
-
})
|
| 123 |
-
log.append({
|
| 124 |
-
"Task ID": q["task_id"],
|
| 125 |
-
"Question": q["question"],
|
| 126 |
-
"Answer": answer
|
| 127 |
-
})
|
| 128 |
-
|
| 129 |
-
# Submit answers
|
| 130 |
-
payload = {
|
| 131 |
-
"username": username,
|
| 132 |
-
"agent_code": agent_code,
|
| 133 |
-
"answers": answers
|
| 134 |
-
}
|
| 135 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
try:
|
| 137 |
-
response = requests.post(submit_url, json=
|
| 138 |
response.raise_for_status()
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
f"
|
| 142 |
-
f"User: {
|
| 143 |
-
f"Score: {
|
| 144 |
-
f"
|
| 145 |
-
f"Message: {
|
| 146 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
except Exception as e:
|
| 148 |
-
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
-
return status, pd.DataFrame(log)
|
| 151 |
|
| 152 |
-
#
|
| 153 |
-
# GRADIO UI
|
| 154 |
-
# =============================
|
| 155 |
with gr.Blocks() as demo:
|
| 156 |
-
gr.Markdown("#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
|
| 158 |
gr.LoginButton()
|
| 159 |
-
run_btn = gr.Button("Run Evaluation & Submit All Answers")
|
| 160 |
|
| 161 |
-
|
| 162 |
-
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
-
|
|
|
|
|
|
|
|
|
|
| 165 |
|
| 166 |
if __name__ == "__main__":
|
| 167 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
| 5 |
+
from typing import Dict, List
|
| 6 |
|
| 7 |
+
# custom imports
|
| 8 |
+
from agents import Agent
|
| 9 |
+
from tool import get_tools
|
| 10 |
+
from model import get_model
|
| 11 |
+
|
| 12 |
+
# (Keep Constants as is)
|
| 13 |
+
# --- Constants ---
|
| 14 |
+
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 15 |
+
MODEL_ID = "gemini/gemini-2.5-flash-preview-04-17"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
# --- Async Question Processing ---
|
| 18 |
+
async def process_question(agent, question: str, task_id: str) -> Dict:
|
| 19 |
+
"""Process a single question and return both answer AND full log entry"""
|
| 20 |
+
try:
|
| 21 |
+
answer = agent(question)
|
| 22 |
+
return {
|
| 23 |
+
"submission": {"task_id": task_id, "submitted_answer": answer},
|
| 24 |
+
"log": {"Task ID": task_id, "Question": question, "Submitted Answer": answer}
|
| 25 |
+
}
|
| 26 |
+
except Exception as e:
|
| 27 |
+
error_msg = f"ERROR: {str(e)}"
|
| 28 |
+
return {
|
| 29 |
+
"submission": {"task_id": task_id, "submitted_answer": error_msg},
|
| 30 |
+
"log": {"Task ID": task_id, "Question": question, "Submitted Answer": error_msg}
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
async def run_questions_async(agent, questions_data: List[Dict]) -> tuple:
|
| 34 |
+
"""Process questions sequentially instead of in batch"""
|
| 35 |
+
submissions = []
|
| 36 |
+
logs = []
|
| 37 |
+
|
| 38 |
+
for q in questions_data:
|
| 39 |
+
result = await process_question(agent, q["question"], q["task_id"])
|
| 40 |
+
submissions.append(result["submission"])
|
| 41 |
+
logs.append(result["log"])
|
| 42 |
+
|
| 43 |
+
return submissions, logs
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
async def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 47 |
+
"""
|
| 48 |
+
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 49 |
+
and displays the results.
|
| 50 |
+
"""
|
| 51 |
+
# --- Determine HF Space Runtime URL and Repo URL ---
|
| 52 |
+
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
| 53 |
+
|
| 54 |
+
if profile:
|
| 55 |
+
username= f"{profile.username}"
|
| 56 |
+
print(f"User logged in: {username}")
|
| 57 |
+
else:
|
| 58 |
+
print("User not logged in.")
|
| 59 |
+
return "Please Login to Hugging Face with the button.", None
|
| 60 |
+
|
| 61 |
+
api_url = DEFAULT_API_URL
|
| 62 |
questions_url = f"{api_url}/questions"
|
| 63 |
submit_url = f"{api_url}/submit"
|
| 64 |
|
| 65 |
+
# 1. Instantiate Agent
|
| 66 |
+
try:
|
| 67 |
+
agent = Agent(
|
| 68 |
+
model=get_model("LiteLLMModel", MODEL_ID),
|
| 69 |
+
tools=get_tools()
|
| 70 |
+
)
|
| 71 |
+
except Exception as e:
|
| 72 |
+
print(f"Error instantiating agent: {e}")
|
| 73 |
+
return f"Error initializing agent: {e}", None
|
| 74 |
+
# 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)
|
| 75 |
+
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 76 |
+
print(agent_code)
|
| 77 |
|
| 78 |
+
# 2. Fetch Questions
|
| 79 |
+
print(f"Fetching questions from: {questions_url}")
|
| 80 |
try:
|
| 81 |
response = requests.get(questions_url, timeout=15)
|
| 82 |
response.raise_for_status()
|
| 83 |
+
questions_data = response.json()
|
| 84 |
+
if not questions_data:
|
| 85 |
+
print("Fetched questions list is empty.")
|
| 86 |
+
return "Fetched questions list is empty or invalid format.", None
|
| 87 |
+
print(f"Fetched {len(questions_data)} questions.")
|
| 88 |
+
questions_data = questions_data[:2]
|
| 89 |
+
except requests.exceptions.RequestException as e:
|
| 90 |
+
print(f"Error fetching questions: {e}")
|
| 91 |
return f"Error fetching questions: {e}", None
|
| 92 |
+
except requests.exceptions.JSONDecodeError as e:
|
| 93 |
+
print(f"Error decoding JSON response from questions endpoint: {e}")
|
| 94 |
+
print(f"Response text: {response.text[:500]}")
|
| 95 |
+
return f"Error decoding server response for questions: {e}", None
|
| 96 |
+
except Exception as e:
|
| 97 |
+
print(f"An unexpected error occurred fetching questions: {e}")
|
| 98 |
+
return f"An unexpected error occurred fetching questions: {e}", None
|
| 99 |
|
| 100 |
+
# 3. Run your Agent
|
| 101 |
+
print(f"Running agent on {len(questions_data)} questions...")
|
| 102 |
+
answers_payload, results_log = await run_questions_async(agent, questions_data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
+
if not answers_payload:
|
| 105 |
+
print("Agent did not produce any answers to submit.")
|
| 106 |
+
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 107 |
+
|
| 108 |
+
# 4. Prepare Submission
|
| 109 |
+
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
| 110 |
+
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
| 111 |
+
print(status_update)
|
| 112 |
+
|
| 113 |
+
# 5. Submit
|
| 114 |
+
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
| 115 |
try:
|
| 116 |
+
response = requests.post(submit_url, json=submission_data, timeout=60)
|
| 117 |
response.raise_for_status()
|
| 118 |
+
result_data = response.json()
|
| 119 |
+
final_status = (
|
| 120 |
+
f"Submission Successful!\n"
|
| 121 |
+
f"User: {result_data.get('username')}\n"
|
| 122 |
+
f"Overall Score: {result_data.get('score', 'N/A')}% "
|
| 123 |
+
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
| 124 |
+
f"Message: {result_data.get('message', 'No message received.')}"
|
| 125 |
)
|
| 126 |
+
print("Submission successful.")
|
| 127 |
+
results_df = pd.DataFrame(results_log)
|
| 128 |
+
return final_status, results_df
|
| 129 |
+
except requests.exceptions.HTTPError as e:
|
| 130 |
+
error_detail = f"Server responded with status {e.response.status_code}."
|
| 131 |
+
try:
|
| 132 |
+
error_json = e.response.json()
|
| 133 |
+
error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
|
| 134 |
+
except requests.exceptions.JSONDecodeError:
|
| 135 |
+
error_detail += f" Response: {e.response.text[:500]}"
|
| 136 |
+
status_message = f"Submission Failed: {error_detail}"
|
| 137 |
+
print(status_message)
|
| 138 |
+
results_df = pd.DataFrame(results_log)
|
| 139 |
+
return status_message, results_df
|
| 140 |
+
except requests.exceptions.Timeout:
|
| 141 |
+
status_message = "Submission Failed: The request timed out."
|
| 142 |
+
print(status_message)
|
| 143 |
+
results_df = pd.DataFrame(results_log)
|
| 144 |
+
return status_message, results_df
|
| 145 |
+
except requests.exceptions.RequestException as e:
|
| 146 |
+
status_message = f"Submission Failed: Network error - {e}"
|
| 147 |
+
print(status_message)
|
| 148 |
+
results_df = pd.DataFrame(results_log)
|
| 149 |
+
return status_message, results_df
|
| 150 |
except Exception as e:
|
| 151 |
+
status_message = f"An unexpected error occurred during submission: {e}"
|
| 152 |
+
print(status_message)
|
| 153 |
+
results_df = pd.DataFrame(results_log)
|
| 154 |
+
return status_message, results_df
|
| 155 |
|
|
|
|
| 156 |
|
| 157 |
+
# --- Build Gradio Interface using Blocks ---
|
|
|
|
|
|
|
| 158 |
with gr.Blocks() as demo:
|
| 159 |
+
gr.Markdown("# Basic Agent Evaluation Runner")
|
| 160 |
+
gr.Markdown(
|
| 161 |
+
"""
|
| 162 |
+
**Instructions:**
|
| 163 |
+
1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
|
| 164 |
+
2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
|
| 165 |
+
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
| 166 |
+
"""
|
| 167 |
+
)
|
| 168 |
|
| 169 |
gr.LoginButton()
|
|
|
|
| 170 |
|
| 171 |
+
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 172 |
+
|
| 173 |
+
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 174 |
+
# Removed max_rows=10 from DataFrame constructor
|
| 175 |
+
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 176 |
|
| 177 |
+
run_button.click(
|
| 178 |
+
fn=run_and_submit_all,
|
| 179 |
+
outputs=[status_output, results_table]
|
| 180 |
+
)
|
| 181 |
|
| 182 |
if __name__ == "__main__":
|
| 183 |
+
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
| 184 |
+
# Check for SPACE_HOST and SPACE_ID at startup for information
|
| 185 |
+
space_host_startup = os.getenv("SPACE_HOST")
|
| 186 |
+
space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
|
| 187 |
+
|
| 188 |
+
if space_host_startup:
|
| 189 |
+
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
| 190 |
+
print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
|
| 191 |
+
else:
|
| 192 |
+
print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
|
| 193 |
+
|
| 194 |
+
if space_id_startup: # Print repo URLs if SPACE_ID is found
|
| 195 |
+
print(f"✅ SPACE_ID found: {space_id_startup}")
|
| 196 |
+
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
| 197 |
+
print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
|
| 198 |
+
else:
|
| 199 |
+
print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
|
| 200 |
+
|
| 201 |
+
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 202 |
+
|
| 203 |
+
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 204 |
+
demo.launch(debug=True, share=False)
|