Update app.py
Browse files
app.py
CHANGED
|
@@ -1,217 +1,57 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
|
|
|
| 4 |
import pandas as pd
|
| 5 |
-
import re # For CalculatorTool validation
|
| 6 |
-
|
| 7 |
-
# --- Smol Agents and Hugging Face Hub Imports ---
|
| 8 |
-
from smol_agents import ReactAgent # The core agent from smol_agents (module name uses underscore)
|
| 9 |
-
from huggingface_hub import load_tool as hub_load_tool # To load tools like search
|
| 10 |
-
from huggingface_hub import login
|
| 11 |
|
|
|
|
| 12 |
# --- Constants ---
|
| 13 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 14 |
-
# LLM for the Agent (Mixtral is a strong choice for reasoning tasks)
|
| 15 |
-
LLM_ENDPOINT = "mistralai/Mixtral-8x7B-Instruct-v0.1"
|
| 16 |
-
|
| 17 |
-
# --- Hugging Face Authentication ---
|
| 18 |
-
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 19 |
-
|
| 20 |
-
# --- Tool Definitions for Smol Agent ---
|
| 21 |
-
|
| 22 |
-
# 1. Calculator Tool (as a simple class with a 'run' method for smol-agents)
|
| 23 |
-
class SmolCalculatorTool:
|
| 24 |
-
name = "calculator"
|
| 25 |
-
description = (
|
| 26 |
-
"This tool is a calculator. Use it to compute mathematical expressions. "
|
| 27 |
-
"Input should be a valid mathematical expression string (e.g., '2+2', '100/5*2', '(3.14+2.71)*4'). "
|
| 28 |
-
"Only use standard arithmetic operators (+, -, *, /) and parentheses."
|
| 29 |
-
)
|
| 30 |
-
# smol-agents typically infers arguments from the run method's signature
|
| 31 |
-
|
| 32 |
-
def run(self, expression: str) -> str:
|
| 33 |
-
"""
|
| 34 |
-
Computes a mathematical expression.
|
| 35 |
-
Args:
|
| 36 |
-
expression (str): The mathematical expression to evaluate.
|
| 37 |
-
Returns:
|
| 38 |
-
str: The result of the calculation or an error message.
|
| 39 |
-
"""
|
| 40 |
-
print(f"CalculatorTool received expression: {expression}")
|
| 41 |
-
try:
|
| 42 |
-
if not isinstance(expression, str):
|
| 43 |
-
return "Error: Input expression must be a string."
|
| 44 |
-
|
| 45 |
-
if not re.match(r"^[0-9\.\+\-\*\/\(\)\s_]+$", expression): # Allow underscore for e.g. large numbers
|
| 46 |
-
return "Error: Expression contains invalid characters. Only use numbers, operators (+, -, *, /), parentheses, and underscores."
|
| 47 |
-
|
| 48 |
-
# Replace underscores if they are used as separators (e.g., 1_000_000)
|
| 49 |
-
expression_to_eval = expression.replace("_", "")
|
| 50 |
-
|
| 51 |
-
# Safely evaluate the expression
|
| 52 |
-
result = eval(expression_to_eval)
|
| 53 |
-
return str(result)
|
| 54 |
-
except Exception as e:
|
| 55 |
-
return f"Error during calculation: {str(e)}. Please ensure the expression is valid."
|
| 56 |
-
|
| 57 |
-
# --- GAIA Smol Agent Definition ---
|
| 58 |
-
# This system prompt guides the ReactAgent's behavior.
|
| 59 |
-
# smol-agents' ReactAgent will automatically include tool descriptions.
|
| 60 |
-
SMOL_AGENT_SYSTEM_PROMPT = """
|
| 61 |
-
You are a highly capable and meticulous AI assistant. Your task is to answer user questions accurately and comprehensively.
|
| 62 |
-
You must operate in a loop of Thought, Action, Observation.
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
{{
|
| 68 |
-
"tool_name": "your_tool_name",
|
| 69 |
-
"tool_input": "input for the tool"
|
| 70 |
-
}}
|
| 71 |
-
```
|
| 72 |
-
If you believe you can answer the question without a tool, or after using tools you have the final answer, output your answer directly without the JSON blob.
|
| 73 |
-
3. **Observation:** (This will be filled by the system with the tool's output if an action was taken.)
|
| 74 |
-
4. Repeat the Thought, Action, Observation loop until you have the final answer.
|
| 75 |
-
|
| 76 |
-
Important guidelines:
|
| 77 |
-
- **Accuracy is key:** Prioritize correctness. If you cannot find the information or are unsure, state that. Do not invent facts.
|
| 78 |
-
- **Tool Use:** Use tools only when necessary. For factual queries requiring up-to-date information or calculations, use your tools.
|
| 79 |
-
- **Search Effectively:** When using the search tool, formulate concise and targeted search queries.
|
| 80 |
-
- **Calculations:** For any numerical calculations, use the calculator tool to ensure accuracy.
|
| 81 |
-
- **Multi-step Reasoning:** Break down complex questions into smaller, manageable steps.
|
| 82 |
-
- **Final Answer:** Once you have all the necessary information and are confident in your answer, provide it directly.
|
| 83 |
-
"""
|
| 84 |
-
|
| 85 |
-
class GaiaSmolAgent:
|
| 86 |
def __init__(self):
|
| 87 |
-
print("
|
| 88 |
-
self.agent = None
|
| 89 |
-
|
| 90 |
-
if not HF_TOKEN:
|
| 91 |
-
print("ERROR: HF_TOKEN environment variable not found. GaiaSmolAgent cannot be initialized.")
|
| 92 |
-
raise ValueError("HF_TOKEN is not set. Please set it as a secret in your Hugging Face Space.")
|
| 93 |
-
|
| 94 |
-
try:
|
| 95 |
-
login(token=HF_TOKEN, add_to_git_credential=False)
|
| 96 |
-
print("Successfully logged in to Hugging Face Hub for GaiaSmolAgent.")
|
| 97 |
-
except Exception as e:
|
| 98 |
-
print(f"Error during Hugging Face login for GaiaSmolAgent: {e}")
|
| 99 |
-
raise ConnectionError(f"Hugging Face login failed: {e}")
|
| 100 |
-
|
| 101 |
-
# Prepare tools
|
| 102 |
-
tools_list = []
|
| 103 |
-
try:
|
| 104 |
-
print("Loading DuckDuckGo search tool via hub_load_tool...")
|
| 105 |
-
# hub_load_tool is often used with smol-agents
|
| 106 |
-
search_tool = hub_load_tool("HuggingFaceH4/duckduckgo_search", hf_token=HF_TOKEN)
|
| 107 |
-
tools_list.append(search_tool)
|
| 108 |
-
print(f"DuckDuckGo search tool loaded: {search_tool.name if hasattr(search_tool, 'name') else 'Unknown name'}")
|
| 109 |
-
except Exception as e:
|
| 110 |
-
print(f"Error loading DuckDuckGo search tool: {e}. Search functionality will be unavailable.")
|
| 111 |
-
# Decide if this is a fatal error
|
| 112 |
-
# raise RuntimeError(f"Failed to load search tool: {e}") from e
|
| 113 |
-
|
| 114 |
-
calculator = SmolCalculatorTool()
|
| 115 |
-
tools_list.append(calculator)
|
| 116 |
-
print(f"Calculator tool prepared: {calculator.name}")
|
| 117 |
-
|
| 118 |
-
if not tools_list:
|
| 119 |
-
print("CRITICAL WARNING: No tools were successfully loaded for GaiaSmolAgent.")
|
| 120 |
-
|
| 121 |
-
try:
|
| 122 |
-
print(f"Initializing ReactAgent with LLM: {LLM_ENDPOINT}")
|
| 123 |
-
self.agent = ReactAgent(
|
| 124 |
-
tools=tools_list,
|
| 125 |
-
llm_engine=LLM_ENDPOINT, # smol-agents can take model ID string
|
| 126 |
-
system_prompt=SMOL_AGENT_SYSTEM_PROMPT,
|
| 127 |
-
hf_token=HF_TOKEN, # Pass token for LLM access via model ID
|
| 128 |
-
max_iterations=10, # Limit iterations to prevent infinite loops
|
| 129 |
-
# You can adjust temperature and other LLM params via llm_kwargs
|
| 130 |
-
# llm_kwargs={"temperature": 0.1, "max_new_tokens": 1024}
|
| 131 |
-
)
|
| 132 |
-
print("GaiaSmolAgent ReactAgent component initialized successfully.")
|
| 133 |
-
except Exception as e:
|
| 134 |
-
print(f"CRITICAL Error initializing ReactAgent component: {e}")
|
| 135 |
-
raise RuntimeError(f"Failed to initialize ReactAgent: {e}") from e
|
| 136 |
-
|
| 137 |
-
print("GaiaSmolAgent fully initialized.")
|
| 138 |
-
|
| 139 |
def __call__(self, question: str) -> str:
|
| 140 |
-
print(f"
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
try:
|
| 146 |
-
print("Running ReactAgent to get the answer...")
|
| 147 |
-
# smol-agents' run method takes a 'task' argument
|
| 148 |
-
# The output of agent.run() is typically the final answer string.
|
| 149 |
-
final_answer = self.agent.run(task=question)
|
| 150 |
-
|
| 151 |
-
if not isinstance(final_answer, str):
|
| 152 |
-
print(f"Warning: ReactAgent returned a non-string type: {type(final_answer)}. Converting to string.")
|
| 153 |
-
final_answer = str(final_answer)
|
| 154 |
-
|
| 155 |
-
print(f"GaiaSmolAgent returning answer (first 100 chars): {final_answer[:100]}...")
|
| 156 |
-
return final_answer
|
| 157 |
-
except Exception as e:
|
| 158 |
-
print(f"Error during GaiaSmolAgent execution for question '{question[:50]}...': {e}")
|
| 159 |
-
# Include traceback for better debugging if possible, or detailed error
|
| 160 |
-
import traceback
|
| 161 |
-
traceback_str = traceback.format_exc()
|
| 162 |
-
print(traceback_str)
|
| 163 |
-
return f"AGENT EXECUTION ERROR: {str(e)}"
|
| 164 |
-
|
| 165 |
|
| 166 |
-
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 167 |
"""
|
| 168 |
-
Fetches all questions, runs the
|
| 169 |
and displays the results.
|
| 170 |
"""
|
| 171 |
-
|
|
|
|
|
|
|
| 172 |
if profile:
|
| 173 |
-
username
|
| 174 |
print(f"User logged in: {username}")
|
| 175 |
else:
|
| 176 |
print("User not logged in.")
|
| 177 |
return "Please Login to Hugging Face with the button.", None
|
| 178 |
|
| 179 |
-
if not HF_TOKEN:
|
| 180 |
-
no_token_message = "ERROR: HF_TOKEN secret is not set in this Space. The agent cannot operate. Please ask the Space owner to set it."
|
| 181 |
-
print(no_token_message)
|
| 182 |
-
return no_token_message, None
|
| 183 |
-
|
| 184 |
api_url = DEFAULT_API_URL
|
| 185 |
questions_url = f"{api_url}/questions"
|
| 186 |
submit_url = f"{api_url}/submit"
|
| 187 |
|
| 188 |
-
# 1. Instantiate Agent (
|
| 189 |
try:
|
| 190 |
-
|
| 191 |
-
agent = GaiaSmolAgent() # <<<< MODIFIED HERE
|
| 192 |
-
print("GaiaSmolAgent instantiated successfully.")
|
| 193 |
except Exception as e:
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
if "HF_TOKEN is not set" in str(e):
|
| 200 |
-
error_msg = "Fatal Error: The HF_TOKEN secret is missing or not accessible. The agent cannot start. Please ensure it's set in the Space settings."
|
| 201 |
-
elif "login failed" in str(e) or "authentication" in str(e).lower():
|
| 202 |
-
error_msg = "Fatal Error: Hugging Face login failed. Check if the HF_TOKEN is valid and has 'read' permissions. The agent cannot start."
|
| 203 |
-
elif "Failed to initialize ReactAgent" in str(e):
|
| 204 |
-
error_msg = f"Fatal Error: Core ReactAgent component failed to initialize: {e}. This could be due to issues with the LLM endpoint ({LLM_ENDPOINT}) or tool setup."
|
| 205 |
-
return error_msg, None
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else "Code link not available (SPACE_ID not set)"
|
| 209 |
-
print(f"Agent code link: {agent_code}")
|
| 210 |
|
| 211 |
# 2. Fetch Questions
|
| 212 |
print(f"Fetching questions from: {questions_url}")
|
| 213 |
try:
|
| 214 |
-
response = requests.get(questions_url, timeout=
|
| 215 |
response.raise_for_status()
|
| 216 |
questions_data = response.json()
|
| 217 |
if not questions_data:
|
|
@@ -225,14 +65,14 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 225 |
print(f"Error decoding JSON response from questions endpoint: {e}")
|
| 226 |
print(f"Response text: {response.text[:500]}")
|
| 227 |
return f"Error decoding server response for questions: {e}", None
|
| 228 |
-
except Exception as e:
|
| 229 |
print(f"An unexpected error occurred fetching questions: {e}")
|
| 230 |
return f"An unexpected error occurred fetching questions: {e}", None
|
| 231 |
|
| 232 |
# 3. Run your Agent
|
| 233 |
results_log = []
|
| 234 |
answers_payload = []
|
| 235 |
-
print(f"Running
|
| 236 |
for item in questions_data:
|
| 237 |
task_id = item.get("task_id")
|
| 238 |
question_text = item.get("question")
|
|
@@ -240,30 +80,27 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 240 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 241 |
continue
|
| 242 |
try:
|
| 243 |
-
|
| 244 |
-
submitted_answer = agent(question_text)
|
| 245 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 246 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 247 |
-
print(f"Task ID {task_id} processed. Answer (first 100): {submitted_answer[:100]}")
|
| 248 |
except Exception as e:
|
| 249 |
-
print(f"Error running agent on task {task_id}
|
| 250 |
-
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT
|
| 251 |
|
| 252 |
-
if not answers_payload:
|
| 253 |
-
print("Agent did not produce any
|
| 254 |
-
return "Agent did not produce any
|
| 255 |
|
| 256 |
-
|
| 257 |
-
# 4. Prepare Submission
|
| 258 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
| 259 |
-
status_update = f"
|
| 260 |
print(status_update)
|
| 261 |
|
| 262 |
# 5. Submit
|
| 263 |
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
| 264 |
try:
|
| 265 |
-
response = requests.post(submit_url, json=submission_data, timeout=
|
| 266 |
-
response.raise_for_status()
|
| 267 |
result_data = response.json()
|
| 268 |
final_status = (
|
| 269 |
f"Submission Successful!\n"
|
|
@@ -278,10 +115,10 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 278 |
except requests.exceptions.HTTPError as e:
|
| 279 |
error_detail = f"Server responded with status {e.response.status_code}."
|
| 280 |
try:
|
| 281 |
-
error_json = e.response.json()
|
| 282 |
error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
|
| 283 |
-
except requests.exceptions.JSONDecodeError:
|
| 284 |
-
error_detail += f" Response: {e.response.text[:500]}"
|
| 285 |
status_message = f"Submission Failed: {error_detail}"
|
| 286 |
print(status_message)
|
| 287 |
results_df = pd.DataFrame(results_log)
|
|
@@ -291,12 +128,12 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 291 |
print(status_message)
|
| 292 |
results_df = pd.DataFrame(results_log)
|
| 293 |
return status_message, results_df
|
| 294 |
-
except requests.exceptions.RequestException as e:
|
| 295 |
status_message = f"Submission Failed: Network error - {e}"
|
| 296 |
print(status_message)
|
| 297 |
results_df = pd.DataFrame(results_log)
|
| 298 |
return status_message, results_df
|
| 299 |
-
except Exception as e:
|
| 300 |
status_message = f"An unexpected error occurred during submission: {e}"
|
| 301 |
print(status_message)
|
| 302 |
results_df = pd.DataFrame(results_log)
|
|
@@ -304,51 +141,54 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 304 |
|
| 305 |
|
| 306 |
# --- Build Gradio Interface using Blocks ---
|
| 307 |
-
with gr.Blocks(
|
| 308 |
-
gr.Markdown("#
|
| 309 |
gr.Markdown(
|
| 310 |
-
|
| 311 |
**Instructions:**
|
| 312 |
-
1.
|
| 313 |
-
2.
|
| 314 |
-
3.
|
| 315 |
-
4. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
|
| 316 |
-
5. Click 'Run Evaluation & Submit All Answers' to fetch questions, run the agent, submit answers, and see the score.
|
| 317 |
---
|
| 318 |
**Disclaimers:**
|
| 319 |
-
|
| 320 |
-
-
|
| 321 |
-
- The agent's performance depends on the LLM, prompt, and tool effectiveness.
|
| 322 |
-
- This version uses `smolagents` (the PyPI package name). Ensure `requirements.txt` is updated accordingly.
|
| 323 |
"""
|
| 324 |
)
|
|
|
|
| 325 |
gr.LoginButton()
|
|
|
|
| 326 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 327 |
-
|
| 328 |
-
|
|
|
|
|
|
|
| 329 |
|
| 330 |
run_button.click(
|
| 331 |
fn=run_and_submit_all,
|
| 332 |
-
|
| 333 |
-
outputs=[status_output, results_table],
|
| 334 |
)
|
| 335 |
|
| 336 |
if __name__ == "__main__":
|
| 337 |
-
print("\n" + "-"*30 + "
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
|
|
|
|
|
|
|
|
|
| 342 |
else:
|
| 343 |
-
print(
|
| 344 |
|
| 345 |
-
space_id_startup
|
| 346 |
-
if space_id_startup:
|
| 347 |
print(f"✅ SPACE_ID found: {space_id_startup}")
|
| 348 |
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
|
|
|
| 349 |
else:
|
| 350 |
print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
|
| 351 |
-
|
| 352 |
-
print("-"*(60 + len("
|
| 353 |
-
|
| 354 |
-
|
|
|
|
|
|
| 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 |
def __call__(self, question: str) -> str:
|
| 17 |
+
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 18 |
+
fixed_answer = "This is a default answer."
|
| 19 |
+
print(f"Agent returning fixed answer: {fixed_answer}")
|
| 20 |
+
return fixed_answer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 23 |
"""
|
| 24 |
+
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 25 |
and displays the results.
|
| 26 |
"""
|
| 27 |
+
# --- Determine HF Space Runtime URL and Repo URL ---
|
| 28 |
+
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
| 29 |
+
|
| 30 |
if profile:
|
| 31 |
+
username= f"{profile.username}"
|
| 32 |
print(f"User logged in: {username}")
|
| 33 |
else:
|
| 34 |
print("User not logged in.")
|
| 35 |
return "Please Login to Hugging Face with the button.", None
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
api_url = DEFAULT_API_URL
|
| 38 |
questions_url = f"{api_url}/questions"
|
| 39 |
submit_url = f"{api_url}/submit"
|
| 40 |
|
| 41 |
+
# 1. Instantiate Agent ( modify this part to create your 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 |
+
# 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)
|
| 48 |
+
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 49 |
+
print(agent_code)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
# 2. Fetch Questions
|
| 52 |
print(f"Fetching questions from: {questions_url}")
|
| 53 |
try:
|
| 54 |
+
response = requests.get(questions_url, timeout=15)
|
| 55 |
response.raise_for_status()
|
| 56 |
questions_data = response.json()
|
| 57 |
if not questions_data:
|
|
|
|
| 65 |
print(f"Error decoding JSON response from questions endpoint: {e}")
|
| 66 |
print(f"Response text: {response.text[:500]}")
|
| 67 |
return f"Error decoding server response for questions: {e}", None
|
| 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
|
| 71 |
|
| 72 |
# 3. Run your Agent
|
| 73 |
results_log = []
|
| 74 |
answers_payload = []
|
| 75 |
+
print(f"Running agent on {len(questions_data)} questions...")
|
| 76 |
for item in questions_data:
|
| 77 |
task_id = item.get("task_id")
|
| 78 |
question_text = item.get("question")
|
|
|
|
| 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({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 85 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
|
|
|
| 86 |
except Exception as e:
|
| 87 |
+
print(f"Error running agent on task {task_id}: {e}")
|
| 88 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
| 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 = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
| 96 |
+
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
| 97 |
print(status_update)
|
| 98 |
|
| 99 |
# 5. Submit
|
| 100 |
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
| 101 |
try:
|
| 102 |
+
response = requests.post(submit_url, json=submission_data, timeout=60)
|
| 103 |
+
response.raise_for_status()
|
| 104 |
result_data = response.json()
|
| 105 |
final_status = (
|
| 106 |
f"Submission Successful!\n"
|
|
|
|
| 115 |
except requests.exceptions.HTTPError as e:
|
| 116 |
error_detail = f"Server responded with status {e.response.status_code}."
|
| 117 |
try:
|
| 118 |
+
error_json = e.response.json()
|
| 119 |
error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
|
| 120 |
+
except requests.exceptions.JSONDecodeError:
|
| 121 |
+
error_detail += f" Response: {e.response.text[:500]}"
|
| 122 |
status_message = f"Submission Failed: {error_detail}"
|
| 123 |
print(status_message)
|
| 124 |
results_df = pd.DataFrame(results_log)
|
|
|
|
| 128 |
print(status_message)
|
| 129 |
results_df = pd.DataFrame(results_log)
|
| 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 |
results_df = pd.DataFrame(results_log)
|
| 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 |
results_df = pd.DataFrame(results_log)
|
|
|
|
| 141 |
|
| 142 |
|
| 143 |
# --- Build Gradio Interface using Blocks ---
|
| 144 |
+
with gr.Blocks() as demo:
|
| 145 |
+
gr.Markdown("# Basic Agent Evaluation Runner")
|
| 146 |
gr.Markdown(
|
| 147 |
+
"""
|
| 148 |
**Instructions:**
|
| 149 |
+
1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
|
| 150 |
+
2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
|
| 151 |
+
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
|
|
|
|
|
|
| 152 |
---
|
| 153 |
**Disclaimers:**
|
| 154 |
+
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).
|
| 155 |
+
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.
|
|
|
|
|
|
|
| 156 |
"""
|
| 157 |
)
|
| 158 |
+
|
| 159 |
gr.LoginButton()
|
| 160 |
+
|
| 161 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 162 |
+
|
| 163 |
+
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 164 |
+
# Removed max_rows=10 from DataFrame constructor
|
| 165 |
+
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 166 |
|
| 167 |
run_button.click(
|
| 168 |
fn=run_and_submit_all,
|
| 169 |
+
outputs=[status_output, results_table]
|
|
|
|
| 170 |
)
|
| 171 |
|
| 172 |
if __name__ == "__main__":
|
| 173 |
+
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
| 174 |
+
# Check for SPACE_HOST and SPACE_ID at startup for information
|
| 175 |
+
space_host_startup = os.getenv("SPACE_HOST")
|
| 176 |
+
space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
|
| 177 |
+
|
| 178 |
+
if space_host_startup:
|
| 179 |
+
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
| 180 |
+
print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
|
| 181 |
else:
|
| 182 |
+
print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
|
| 183 |
|
| 184 |
+
if space_id_startup: # Print repo URLs if SPACE_ID is found
|
|
|
|
| 185 |
print(f"✅ SPACE_ID found: {space_id_startup}")
|
| 186 |
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
| 187 |
+
print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
|
| 188 |
else:
|
| 189 |
print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
|
| 190 |
+
|
| 191 |
+
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 192 |
+
|
| 193 |
+
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 194 |
+
demo.launch(debug=True, share=False)
|