Update app.py
Browse files
app.py
CHANGED
|
@@ -1,247 +1,117 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
|
|
|
| 4 |
import pandas as pd
|
| 5 |
-
import re
|
| 6 |
-
from typing import Optional, Dict, Any
|
| 7 |
-
import json
|
| 8 |
-
import logging
|
| 9 |
-
|
| 10 |
-
# --- Setup Logging ---
|
| 11 |
-
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 12 |
-
logger = logging.getLogger(__name__)
|
| 13 |
|
|
|
|
| 14 |
# --- Constants ---
|
| 15 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 16 |
|
| 17 |
-
# ---
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
self.tools = {
|
| 21 |
-
"web_search": self.web_search_tool,
|
| 22 |
-
"file_processor": self.file_processor_tool
|
| 23 |
-
}
|
| 24 |
-
self.answer_cache = {}
|
| 25 |
-
logger.info("SmolAgent initialized.")
|
| 26 |
-
|
| 27 |
-
def web_search_tool(self, query: str) -> Dict[str, str]:
|
| 28 |
-
"""Simulates a web search tool (e.g., SerpAPI, Wikipedia)."""
|
| 29 |
-
logger.info(f"Web search tool called with query: {query}")
|
| 30 |
-
mock_results = {
|
| 31 |
-
"1928 summer olympics least athletes": {"result": "Malta (MLT) had the fewest athletes (1)."},
|
| 32 |
-
"taishō tamai pitcher numbers july 2023": {"result": "Pitchers before and after Taishō Tamai (18): Tanaka (17), Yamamoto (19)."},
|
| 33 |
-
"malko competition winners after 1977 defunct country": {"result": "Igor Lassov, USSR, won in 1986."},
|
| 34 |
-
"mercedes sosa studio albums 2000-2009": {"result": "3 albums: Misa Criolla (2000), Corazón Libre (2005), Cantora (2009)."},
|
| 35 |
-
"opposite of left": {"result": "right"},
|
| 36 |
-
"youtube video camera count": {"result": "3 cameras used simultaneously."}, # Hypothetical
|
| 37 |
-
"pasta shapes starting with c": {"result": "Campanelle, Cavatappi, Conchiglie"},
|
| 38 |
-
"highest mountain southern hemisphere": {"result": "Aconcagua"},
|
| 39 |
-
"elements atomic number less than 10": {"result": "Hydrogen, Helium, Lithium, Beryllium, Boron, Carbon, Nitrogen, Oxygen, Fluorine"},
|
| 40 |
-
"nobel peace prize 2009": {"result": "Barack Obama"},
|
| 41 |
-
"first human in space": {"result": "Yuri Gagarin"},
|
| 42 |
-
"capital of bhutan": {"result": "Thimphu"},
|
| 43 |
-
"longest river south america": {"result": "Amazon River"},
|
| 44 |
-
"oscar best picture 2010": {"result": "The Hurt Locker"},
|
| 45 |
-
"noble gases": {"result": "Helium, Neon, Argon, Krypton, Xenon, Radon"},
|
| 46 |
-
"largest desert": {"result": "Antarctic Desert"},
|
| 47 |
-
"world cup 2014 winner": {"result": "Germany"},
|
| 48 |
-
"shakespeare othello": {"result": "Othello"},
|
| 49 |
-
"currency japan": {"result": "Yen"},
|
| 50 |
-
"smallest country land area": {"result": "Vatican City"}
|
| 51 |
-
}
|
| 52 |
-
for key, value in mock_results.items():
|
| 53 |
-
if key.lower() in query.lower():
|
| 54 |
-
return value
|
| 55 |
-
return {"result": "No data found."}
|
| 56 |
-
|
| 57 |
-
def file_processor_tool(self, file_path: str, query: str) -> Dict[str, str]:
|
| 58 |
-
"""Simulates processing of files (e.g., Excel for sales)."""
|
| 59 |
-
logger.info(f"File processor tool called with file: {file_path}, query: {query}")
|
| 60 |
-
if "fast-food chain" in query.lower() and "excel" in query.lower():
|
| 61 |
-
return {"result": "10423.75"} # Hardcoded from submitted answer
|
| 62 |
-
return {"result": "Unable to process file."}
|
| 63 |
-
|
| 64 |
-
def run(self, question: str, files: Optional[list] = None) -> str:
|
| 65 |
-
"""Processes a question using tools and mock LLM logic."""
|
| 66 |
-
logger.info(f"Processing question (first 50 chars): {question[:50]}...")
|
| 67 |
-
question_lower = question.lower().strip()
|
| 68 |
-
|
| 69 |
-
# Check cache
|
| 70 |
-
if question in self.answer_cache:
|
| 71 |
-
logger.info(f"Returning cached answer: {self.answer_cache[question]}")
|
| 72 |
-
return self.answer_cache[question]
|
| 73 |
-
|
| 74 |
-
# Question 1: Grocery list vegetable categorization
|
| 75 |
-
if "grocery list" in question_lower and "botany" in question_lower:
|
| 76 |
-
vegetables = ["acorns", "basil", "broccoli", "celery", "lettuce", "sweet potatoes"]
|
| 77 |
-
answer = ", ".join(sorted(vegetables)).strip()
|
| 78 |
-
self.answer_cache[question] = answer
|
| 79 |
-
logger.info(f"Returning vegetable list: {answer}")
|
| 80 |
-
return answer
|
| 81 |
-
|
| 82 |
-
# Question 2: 1928 Summer Olympics
|
| 83 |
-
elif "1928 summer olympics" in question_lower:
|
| 84 |
-
result = self.tools["web_search"]("1928 summer olympics least athletes")
|
| 85 |
-
answer = "MLT" if result["result"] != "No data found." else "MLT"
|
| 86 |
-
self.answer_cache[question] = answer
|
| 87 |
-
logger.info(f"Returning IOC code: {answer}")
|
| 88 |
-
return answer
|
| 89 |
-
|
| 90 |
-
# Question 3: Taishō Tamai pitchers
|
| 91 |
-
elif "taishō tamai" in question_lower:
|
| 92 |
-
result = self.tools["web_search"]("taishō tamai pitcher numbers july 2023")
|
| 93 |
-
answer = "Tanaka, Yamamoto" if result["result"] != "No data found." else "Tanaka, Yamamoto"
|
| 94 |
-
self.answer_cache[question] = answer
|
| 95 |
-
logger.info(f"Returning pitchers: {answer}")
|
| 96 |
-
return answer
|
| 97 |
-
|
| 98 |
-
# Question 4: Fast-food sales (Excel)
|
| 99 |
-
elif "fast-food chain" in question_lower and "excel file" in question_lower:
|
| 100 |
-
if files:
|
| 101 |
-
result = self.tools["file_processor"](files[0], question)
|
| 102 |
-
answer = result["result"]
|
| 103 |
-
else:
|
| 104 |
-
answer = "10423.75" # Fallback
|
| 105 |
-
self.answer_cache[question] = answer
|
| 106 |
-
logger.info(f"Returning total sales: {answer}")
|
| 107 |
-
return answer
|
| 108 |
-
|
| 109 |
-
# Question 5: Malko Competition
|
| 110 |
-
elif "malko competition" in question_lower:
|
| 111 |
-
result = self.tools["web_search"]("malko competition winners after 1977 defunct country")
|
| 112 |
-
if result["result"] != "No data found.":
|
| 113 |
-
match = re.search(r"(\w+)\s+\w+,", result["result"])
|
| 114 |
-
answer = match.group(1) if match else "Igor"
|
| 115 |
-
else:
|
| 116 |
-
answer = "Igor"
|
| 117 |
-
self.answer_cache[question] = answer
|
| 118 |
-
logger.info(f"Returning Malko recipient: {answer}")
|
| 119 |
-
return answer
|
| 120 |
-
|
| 121 |
-
# Additional GAIA Questions
|
| 122 |
-
elif "mercedes sosa" in question_lower and "studio albums" in question_lower:
|
| 123 |
-
result = self.tools["web_search"]("mercedes sosa studio albums 2000-2009")
|
| 124 |
-
answer = "3" if result["result"] != "No data found." else "3"
|
| 125 |
-
self.answer_cache[question] = answer
|
| 126 |
-
logger.info(f"Returning album count: {answer}")
|
| 127 |
-
return answer
|
| 128 |
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
|
|
|
|
|
|
| 135 |
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
self.
|
| 140 |
-
|
|
|
|
| 141 |
return answer
|
|
|
|
|
|
|
|
|
|
| 142 |
|
| 143 |
-
# Generic GAIA Tasks
|
| 144 |
-
for query_key in [
|
| 145 |
-
"pasta shapes starting with c",
|
| 146 |
-
"highest mountain southern hemisphere",
|
| 147 |
-
"elements atomic number less than 10",
|
| 148 |
-
"nobel peace prize 2009",
|
| 149 |
-
"first human in space",
|
| 150 |
-
"capital of bhutan",
|
| 151 |
-
"longest river south america",
|
| 152 |
-
"oscar best picture 2010",
|
| 153 |
-
"noble gases",
|
| 154 |
-
"largest desert",
|
| 155 |
-
"world cup 2014 winner",
|
| 156 |
-
"shakespeare othello",
|
| 157 |
-
"currency japan",
|
| 158 |
-
"smallest country land area"
|
| 159 |
-
]:
|
| 160 |
-
if query_key in question_lower:
|
| 161 |
-
result = self.tools["web_search"](query_key)
|
| 162 |
-
answer = result["result"] if result["result"] != "No data found." else "Unable to process question."
|
| 163 |
-
self.answer_cache[question] = answer
|
| 164 |
-
logger.info(f"Returning answer for {query_key}: {answer}")
|
| 165 |
-
return answer
|
| 166 |
-
|
| 167 |
-
# Default fallback
|
| 168 |
-
logger.info("Question not recognized. Using web search...")
|
| 169 |
-
result = self.tools["web_search"](question[:100])
|
| 170 |
-
answer = result["result"] if result["result"] != "No data found." else "Unable to process question."
|
| 171 |
-
self.answer_cache[question] = answer
|
| 172 |
-
logger.info(f"Returning default answer: {answer}")
|
| 173 |
-
return answer
|
| 174 |
|
| 175 |
-
|
| 176 |
-
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 177 |
"""
|
| 178 |
-
Fetches all questions, runs the
|
| 179 |
and displays the results.
|
| 180 |
"""
|
| 181 |
-
|
|
|
|
|
|
|
| 182 |
if profile:
|
| 183 |
-
username
|
| 184 |
-
|
| 185 |
else:
|
| 186 |
-
|
| 187 |
return "Please Login to Hugging Face with the button.", None
|
| 188 |
|
| 189 |
api_url = DEFAULT_API_URL
|
| 190 |
questions_url = f"{api_url}/questions"
|
| 191 |
submit_url = f"{api_url}/submit"
|
| 192 |
-
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 193 |
|
|
|
|
| 194 |
try:
|
| 195 |
-
agent =
|
| 196 |
except Exception as e:
|
| 197 |
-
|
| 198 |
return f"Error initializing agent: {e}", None
|
|
|
|
|
|
|
|
|
|
| 199 |
|
| 200 |
-
|
| 201 |
-
|
| 202 |
try:
|
| 203 |
response = requests.get(questions_url, timeout=15)
|
| 204 |
response.raise_for_status()
|
| 205 |
questions_data = response.json()
|
| 206 |
if not questions_data:
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
except requests.exceptions.RequestException as e:
|
| 211 |
-
|
| 212 |
return f"Error fetching questions: {e}", None
|
| 213 |
except requests.exceptions.JSONDecodeError as e:
|
| 214 |
-
|
| 215 |
-
|
|
|
|
| 216 |
except Exception as e:
|
| 217 |
-
|
| 218 |
return f"An unexpected error occurred fetching questions: {e}", None
|
| 219 |
|
|
|
|
| 220 |
results_log = []
|
| 221 |
answers_payload = []
|
| 222 |
-
|
| 223 |
for item in questions_data:
|
| 224 |
task_id = item.get("task_id")
|
| 225 |
question_text = item.get("question")
|
| 226 |
-
files = item.get("files", [])
|
| 227 |
if not task_id or question_text is None:
|
| 228 |
-
|
| 229 |
continue
|
| 230 |
try:
|
| 231 |
-
submitted_answer = agent
|
| 232 |
-
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer
|
| 233 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 234 |
-
logger.info(f"Task {task_id} answer: {submitted_answer}")
|
| 235 |
except Exception as e:
|
| 236 |
-
|
| 237 |
-
|
| 238 |
|
| 239 |
if not answers_payload:
|
| 240 |
-
|
| 241 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 242 |
|
|
|
|
| 243 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
| 244 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
try:
|
| 246 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
| 247 |
response.raise_for_status()
|
|
@@ -253,7 +123,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 253 |
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
| 254 |
f"Message: {result_data.get('message', 'No message received.')}"
|
| 255 |
)
|
| 256 |
-
|
| 257 |
results_df = pd.DataFrame(results_log)
|
| 258 |
return final_status, results_df
|
| 259 |
except requests.exceptions.HTTPError as e:
|
|
@@ -263,36 +133,42 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 263 |
error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
|
| 264 |
except requests.exceptions.JSONDecodeError:
|
| 265 |
error_detail += f" Response: {e.response.text[:500]}"
|
| 266 |
-
|
|
|
|
| 267 |
results_df = pd.DataFrame(results_log)
|
| 268 |
-
return
|
| 269 |
except requests.exceptions.Timeout:
|
| 270 |
-
|
|
|
|
| 271 |
results_df = pd.DataFrame(results_log)
|
| 272 |
-
return
|
| 273 |
except requests.exceptions.RequestException as e:
|
| 274 |
-
|
|
|
|
| 275 |
results_df = pd.DataFrame(results_log)
|
| 276 |
-
return
|
| 277 |
except Exception as e:
|
| 278 |
-
|
|
|
|
| 279 |
results_df = pd.DataFrame(results_log)
|
| 280 |
-
return
|
|
|
|
| 281 |
|
| 282 |
-
# --- Build Gradio Interface ---
|
| 283 |
with gr.Blocks() as demo:
|
| 284 |
-
gr.Markdown("#
|
| 285 |
gr.Markdown(
|
| 286 |
"""
|
| 287 |
**Instructions:**
|
| 288 |
|
| 289 |
-
1.
|
| 290 |
-
2.
|
| 291 |
-
3.
|
| 292 |
|
| 293 |
---
|
| 294 |
**Disclaimers:**
|
| 295 |
-
|
|
|
|
| 296 |
"""
|
| 297 |
)
|
| 298 |
|
|
@@ -301,6 +177,7 @@ with gr.Blocks() as demo:
|
|
| 301 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 302 |
|
| 303 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
|
|
|
| 304 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 305 |
|
| 306 |
run_button.click(
|
|
@@ -309,22 +186,25 @@ with gr.Blocks() as demo:
|
|
| 309 |
)
|
| 310 |
|
| 311 |
if __name__ == "__main__":
|
| 312 |
-
|
|
|
|
| 313 |
space_host_startup = os.getenv("SPACE_HOST")
|
| 314 |
-
space_id_startup = os.getenv("SPACE_ID")
|
| 315 |
|
| 316 |
if space_host_startup:
|
| 317 |
-
|
| 318 |
-
|
| 319 |
else:
|
| 320 |
-
|
| 321 |
|
| 322 |
-
if space_id_startup:
|
| 323 |
-
|
| 324 |
-
|
|
|
|
| 325 |
else:
|
| 326 |
-
|
|
|
|
|
|
|
| 327 |
|
| 328 |
-
|
| 329 |
-
logger.info("Launching Gradio Interface...")
|
| 330 |
demo.launch(debug=True, share=False)
|
|
|
|
| 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 |
+
from transformers import pipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
class BasicAgent:
|
| 16 |
+
def __init__(self):
|
| 17 |
+
print("✅ AI Agent with transformers + PyTorch initialized")
|
| 18 |
+
self.generator = pipeline(
|
| 19 |
+
"text-generation",
|
| 20 |
+
model="sshleifer/tiny-gpt2", # or use "distilgpt2"
|
| 21 |
+
framework="pt" # Explicitly use PyTorch
|
| 22 |
+
)
|
| 23 |
|
| 24 |
+
def __call__(self, question: str) -> str:
|
| 25 |
+
print(f"⚙️ Generating answer for: {question[:60]}...")
|
| 26 |
+
try:
|
| 27 |
+
response = self.generator(question, max_length=50, num_return_sequences=1)
|
| 28 |
+
answer = response[0]['generated_text'].strip()
|
| 29 |
+
print(f"🧠 Answer: {answer}")
|
| 30 |
return answer
|
| 31 |
+
except Exception as e:
|
| 32 |
+
print(f"❌ Error in model inference: {e}")
|
| 33 |
+
return "Model error"
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
|
|
| 37 |
"""
|
| 38 |
+
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 39 |
and displays the results.
|
| 40 |
"""
|
| 41 |
+
# --- Determine HF Space Runtime URL and Repo URL ---
|
| 42 |
+
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
| 43 |
+
|
| 44 |
if profile:
|
| 45 |
+
username= f"{profile.username}"
|
| 46 |
+
print(f"User logged in: {username}")
|
| 47 |
else:
|
| 48 |
+
print("User not logged in.")
|
| 49 |
return "Please Login to Hugging Face with the button.", None
|
| 50 |
|
| 51 |
api_url = DEFAULT_API_URL
|
| 52 |
questions_url = f"{api_url}/questions"
|
| 53 |
submit_url = f"{api_url}/submit"
|
|
|
|
| 54 |
|
| 55 |
+
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 56 |
try:
|
| 57 |
+
agent = BasicAgent()
|
| 58 |
except Exception as e:
|
| 59 |
+
print(f"Error instantiating agent: {e}")
|
| 60 |
return f"Error initializing agent: {e}", None
|
| 61 |
+
# 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)
|
| 62 |
+
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 63 |
+
print(agent_code)
|
| 64 |
|
| 65 |
+
# 2. Fetch Questions
|
| 66 |
+
print(f"Fetching questions from: {questions_url}")
|
| 67 |
try:
|
| 68 |
response = requests.get(questions_url, timeout=15)
|
| 69 |
response.raise_for_status()
|
| 70 |
questions_data = response.json()
|
| 71 |
if not questions_data:
|
| 72 |
+
print("Fetched questions list is empty.")
|
| 73 |
+
return "Fetched questions list is empty or invalid format.", None
|
| 74 |
+
print(f"Fetched {len(questions_data)} questions.")
|
| 75 |
except requests.exceptions.RequestException as e:
|
| 76 |
+
print(f"Error fetching questions: {e}")
|
| 77 |
return f"Error fetching questions: {e}", None
|
| 78 |
except requests.exceptions.JSONDecodeError as e:
|
| 79 |
+
print(f"Error decoding JSON response from questions endpoint: {e}")
|
| 80 |
+
print(f"Response text: {response.text[:500]}")
|
| 81 |
+
return f"Error decoding server response for questions: {e}", None
|
| 82 |
except Exception as e:
|
| 83 |
+
print(f"An unexpected error occurred fetching questions: {e}")
|
| 84 |
return f"An unexpected error occurred fetching questions: {e}", None
|
| 85 |
|
| 86 |
+
# 3. Run your Agent
|
| 87 |
results_log = []
|
| 88 |
answers_payload = []
|
| 89 |
+
print(f"Running agent on {len(questions_data)} questions...")
|
| 90 |
for item in questions_data:
|
| 91 |
task_id = item.get("task_id")
|
| 92 |
question_text = item.get("question")
|
|
|
|
| 93 |
if not task_id or question_text is None:
|
| 94 |
+
print(f"Skipping item with missing task_id or question: {item}")
|
| 95 |
continue
|
| 96 |
try:
|
| 97 |
+
submitted_answer = agent(question_text)
|
| 98 |
+
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 99 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
|
|
|
| 100 |
except Exception as e:
|
| 101 |
+
print(f"Error running agent on task {task_id}: {e}")
|
| 102 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
| 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()
|
|
|
|
| 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:
|
|
|
|
| 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 |
|
| 164 |
+
1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
|
| 165 |
+
2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
|
| 166 |
+
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
| 167 |
|
| 168 |
---
|
| 169 |
**Disclaimers:**
|
| 170 |
+
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).
|
| 171 |
+
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.
|
| 172 |
"""
|
| 173 |
)
|
| 174 |
|
|
|
|
| 177 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 178 |
|
| 179 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 180 |
+
# Removed max_rows=10 from DataFrame constructor
|
| 181 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 182 |
|
| 183 |
run_button.click(
|
|
|
|
| 186 |
)
|
| 187 |
|
| 188 |
if __name__ == "__main__":
|
| 189 |
+
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
| 190 |
+
# Check for SPACE_HOST and SPACE_ID at startup for information
|
| 191 |
space_host_startup = os.getenv("SPACE_HOST")
|
| 192 |
+
space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
|
| 193 |
|
| 194 |
if space_host_startup:
|
| 195 |
+
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
| 196 |
+
print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
|
| 197 |
else:
|
| 198 |
+
print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
|
| 199 |
|
| 200 |
+
if space_id_startup: # Print repo URLs if SPACE_ID is found
|
| 201 |
+
print(f"✅ SPACE_ID found: {space_id_startup}")
|
| 202 |
+
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
| 203 |
+
print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
|
| 204 |
else:
|
| 205 |
+
print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
|
| 206 |
+
|
| 207 |
+
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 208 |
|
| 209 |
+
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
|
|
|
| 210 |
demo.launch(debug=True, share=False)
|