Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,217 @@
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import requests
|
| 4 |
-
import
|
| 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 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 =
|
| 19 |
-
print(f"Agent returning
|
| 20 |
return fixed_answer
|
| 21 |
|
| 22 |
-
|
|
|
|
| 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 =
|
| 29 |
|
| 30 |
if profile:
|
| 31 |
-
username= f"{profile.username}"
|
| 32 |
print(f"User logged in: {username}")
|
| 33 |
else:
|
| 34 |
print("User not logged in.")
|
|
@@ -38,10 +221,9 @@ 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 ( 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)
|
|
@@ -55,17 +237,17 @@ 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
|
| 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 |
|
|
@@ -76,23 +258,62 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 76 |
for item in questions_data:
|
| 77 |
task_id = item.get("task_id")
|
| 78 |
question_text = item.get("question")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
if not task_id or question_text is None:
|
| 80 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 81 |
continue
|
| 82 |
try:
|
| 83 |
-
submitted_answer = agent(
|
| 84 |
-
answers_payload.append(
|
| 85 |
-
|
| 86 |
-
|
| 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 |
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
| 97 |
print(status_update)
|
| 98 |
|
|
@@ -133,7 +354,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 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)
|
|
@@ -146,11 +367,9 @@ 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).
|
|
@@ -162,20 +381,19 @@ with gr.Blocks() as demo:
|
|
| 162 |
|
| 163 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 164 |
|
| 165 |
-
status_output = gr.Textbox(
|
|
|
|
|
|
|
| 166 |
# Removed max_rows=10 from DataFrame constructor
|
| 167 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 168 |
|
| 169 |
-
run_button.click(
|
| 170 |
-
fn=run_and_submit_all,
|
| 171 |
-
outputs=[status_output, results_table]
|
| 172 |
-
)
|
| 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 +401,18 @@ 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(
|
|
|
|
|
|
|
| 190 |
else:
|
| 191 |
-
print(
|
|
|
|
|
|
|
| 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 |
+
"""app.py"""
|
| 2 |
+
|
| 3 |
import os
|
| 4 |
+
import re
|
| 5 |
+
import pathlib
|
| 6 |
+
import tempfile
|
| 7 |
+
from pathlib import Path
|
| 8 |
+
from typing import Union, Optional
|
| 9 |
+
|
| 10 |
+
import openai
|
| 11 |
import requests
|
| 12 |
+
import gradio as gr
|
| 13 |
import pandas as pd
|
| 14 |
+
from tabulate import tabulate
|
| 15 |
+
from smolagents import (
|
| 16 |
+
OpenAIServerModel,
|
| 17 |
+
DuckDuckGoSearchTool,
|
| 18 |
+
CodeAgent,
|
| 19 |
+
WikipediaSearchTool,
|
| 20 |
+
)
|
| 21 |
+
from smolagents.tools import PipelineTool, Tool
|
| 22 |
+
|
| 23 |
|
| 24 |
# (Keep Constants as is)
|
| 25 |
# --- Constants ---
|
| 26 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 27 |
|
| 28 |
+
|
| 29 |
+
class SpeechToTextTool(PipelineTool):
|
| 30 |
+
"""
|
| 31 |
+
Transcribes an audio file to text using the OpenAI Whisper API.
|
| 32 |
+
Only local file paths are supported.
|
| 33 |
+
"""
|
| 34 |
+
|
| 35 |
+
default_checkpoint = "openai/whisper-1" # purely informational here
|
| 36 |
+
description = (
|
| 37 |
+
"This tool sends an audio file to OpenAI Whisper and returns the "
|
| 38 |
+
"transcribed text."
|
| 39 |
+
)
|
| 40 |
+
name = "transcriber"
|
| 41 |
+
inputs = {
|
| 42 |
+
"audio": {
|
| 43 |
+
"type": "string",
|
| 44 |
+
"description": "Absolute or relative path to a local audio file.",
|
| 45 |
+
}
|
| 46 |
+
}
|
| 47 |
+
output_type = "string"
|
| 48 |
+
|
| 49 |
+
# ──────────────────────────────────────────────────────────────────────────
|
| 50 |
+
# Public interface
|
| 51 |
+
# ──────────────────────────────────────────────────────────────────────────
|
| 52 |
+
def __call__(self, audio: str) -> str:
|
| 53 |
+
"""
|
| 54 |
+
Convenience wrapper so the tool can be used like a regular function:
|
| 55 |
+
text = SpeechToTextTool()(path_to_audio)
|
| 56 |
+
"""
|
| 57 |
+
return self._transcribe(audio)
|
| 58 |
+
|
| 59 |
+
# ──────────────────────────────────────────────────────────────────────────
|
| 60 |
+
# Internal helpers
|
| 61 |
+
# ──────────────────────────────────────────────────────────────────────────
|
| 62 |
+
@staticmethod
|
| 63 |
+
def _transcribe(audio_path: str) -> str:
|
| 64 |
+
# ----- validation ----------------------------------------------------
|
| 65 |
+
if not isinstance(audio_path, str):
|
| 66 |
+
raise TypeError(
|
| 67 |
+
"Parameter 'audio' must be a string containing the file path."
|
| 68 |
+
)
|
| 69 |
+
path = Path(audio_path).expanduser().resolve()
|
| 70 |
+
if not path.is_file():
|
| 71 |
+
raise FileNotFoundError(f"No such audio file: {path}")
|
| 72 |
+
|
| 73 |
+
# ----- API call ------------------------------------------------------
|
| 74 |
+
with path.open("rb") as fp:
|
| 75 |
+
response = openai.audio.transcriptions.create(
|
| 76 |
+
file=fp,
|
| 77 |
+
model="whisper-1", # currently the only Whisper model
|
| 78 |
+
response_format="text", # returns plain text instead of JSON
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
# For response_format="text", `response` is already the raw transcript
|
| 82 |
+
return response
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
class ExcelToTextTool(Tool):
|
| 86 |
+
"""Render an Excel worksheet as Markdown text."""
|
| 87 |
+
|
| 88 |
+
# ------------------------------------------------------------------
|
| 89 |
+
# Required smol‑agents metadata
|
| 90 |
+
# ------------------------------------------------------------------
|
| 91 |
+
name = "excel_to_text"
|
| 92 |
+
description = (
|
| 93 |
+
"Read an Excel file and return a Markdown table of the requested sheet. "
|
| 94 |
+
"Accepts either the sheet name or the zero-based index."
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
inputs = {
|
| 98 |
+
"excel_path": {
|
| 99 |
+
"type": "string",
|
| 100 |
+
"description": "Path to the Excel file (.xlsx / .xls).",
|
| 101 |
+
},
|
| 102 |
+
"sheet_name": {
|
| 103 |
+
"type": "string",
|
| 104 |
+
"description": (
|
| 105 |
+
"Worksheet name or zero-based index *as a string* (optional; default first sheet)."
|
| 106 |
+
),
|
| 107 |
+
"nullable": True,
|
| 108 |
+
},
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
output_type = "string"
|
| 112 |
+
|
| 113 |
+
def forward(
|
| 114 |
+
self,
|
| 115 |
+
excel_path: str,
|
| 116 |
+
sheet_name: Optional[str] = None,
|
| 117 |
+
) -> str:
|
| 118 |
+
"""Load *excel_path* and return the sheet as a Markdown table."""
|
| 119 |
+
|
| 120 |
+
path = pathlib.Path(excel_path).expanduser().resolve()
|
| 121 |
+
if not path.exists():
|
| 122 |
+
return f"Error: Excel file not found at {path}"
|
| 123 |
+
|
| 124 |
+
try:
|
| 125 |
+
# Interpret sheet identifier -----------------------------------
|
| 126 |
+
sheet: Union[str, int]
|
| 127 |
+
if sheet_name is None or sheet_name == "":
|
| 128 |
+
sheet = 0 # first sheet
|
| 129 |
+
else:
|
| 130 |
+
# If the user passed a numeric string (e.g. "1"), cast to int
|
| 131 |
+
sheet = int(sheet_name) if sheet_name.isdigit() else sheet_name
|
| 132 |
+
|
| 133 |
+
# Load worksheet ----------------------------------------------
|
| 134 |
+
df = pd.read_excel(path, sheet_name=sheet)
|
| 135 |
+
|
| 136 |
+
# Render to Markdown; fall back to tabulate if needed ---------
|
| 137 |
+
if hasattr(pd.DataFrame, "to_markdown"):
|
| 138 |
+
return df.to_markdown(index=False)
|
| 139 |
+
|
| 140 |
+
return tabulate(df, headers="keys", tablefmt="github", showindex=False)
|
| 141 |
+
|
| 142 |
+
except Exception as exc: # pylint: disable=broad-except
|
| 143 |
+
return f"Error reading Excel file: {exc}"
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
def download_file_if_any(base_api_url: str, task_id: str) -> str | None:
|
| 147 |
+
"""
|
| 148 |
+
Try GET /files/{task_id}.
|
| 149 |
+
• On HTTP 200 → save to a temp dir and return local path.
|
| 150 |
+
• On 404 → return None.
|
| 151 |
+
• On other errors → raise so caller can log / handle.
|
| 152 |
+
"""
|
| 153 |
+
url = f"{base_api_url}/files/{task_id}"
|
| 154 |
+
try:
|
| 155 |
+
resp = requests.get(url, timeout=30)
|
| 156 |
+
if resp.status_code == 404:
|
| 157 |
+
return None # no file
|
| 158 |
+
resp.raise_for_status() # raise on 4xx/5xx ≠ 404
|
| 159 |
+
except requests.exceptions.HTTPError as e:
|
| 160 |
+
# propagate non-404 errors (403, 500, …)
|
| 161 |
+
raise e
|
| 162 |
+
|
| 163 |
+
# ▸ Save bytes to a named file inside the system temp dir
|
| 164 |
+
# Try to keep original extension from Content-Disposition if present.
|
| 165 |
+
cdisp = resp.headers.get("content-disposition", "")
|
| 166 |
+
filename = task_id # default base name
|
| 167 |
+
if "filename=" in cdisp:
|
| 168 |
+
m = re.search(r'filename="([^"]+)"', cdisp)
|
| 169 |
+
if m:
|
| 170 |
+
filename = m.group(1) # keep provided name
|
| 171 |
+
|
| 172 |
+
tmp_dir = Path(tempfile.gettempdir()) / "gaia_files"
|
| 173 |
+
tmp_dir.mkdir(exist_ok=True)
|
| 174 |
+
file_path = tmp_dir / filename
|
| 175 |
+
with open(file_path, "wb") as f:
|
| 176 |
+
f.write(resp.content)
|
| 177 |
+
return str(file_path)
|
| 178 |
+
|
| 179 |
+
|
| 180 |
class BasicAgent:
|
| 181 |
+
"""Basic Agent for the evaluation task."""
|
| 182 |
+
|
| 183 |
def __init__(self):
|
| 184 |
+
self.agent = CodeAgent(
|
| 185 |
+
model=OpenAIServerModel(model_id="gpt-4o"),
|
| 186 |
+
tools=[
|
| 187 |
+
DuckDuckGoSearchTool(),
|
| 188 |
+
WikipediaSearchTool(),
|
| 189 |
+
SpeechToTextTool(),
|
| 190 |
+
ExcelToTextTool(),
|
| 191 |
+
],
|
| 192 |
+
add_base_tools=True,
|
| 193 |
+
additional_authorized_imports=["pandas", "numpy", "csv", "subprocess"],
|
| 194 |
+
)
|
| 195 |
+
|
| 196 |
print("BasicAgent initialized.")
|
| 197 |
+
|
| 198 |
def __call__(self, question: str) -> str:
|
| 199 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 200 |
+
fixed_answer = self.agent.run(question)
|
| 201 |
+
print(f"Agent returning answer: {fixed_answer}")
|
| 202 |
return fixed_answer
|
| 203 |
|
| 204 |
+
|
| 205 |
+
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 206 |
"""
|
| 207 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 208 |
and displays the results.
|
| 209 |
"""
|
| 210 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
| 211 |
+
space_id = "l3xv/Final_Assignment_Template"
|
| 212 |
|
| 213 |
if profile:
|
| 214 |
+
username = f"{profile.username}"
|
| 215 |
print(f"User logged in: {username}")
|
| 216 |
else:
|
| 217 |
print("User not logged in.")
|
|
|
|
| 221 |
questions_url = f"{api_url}/questions"
|
| 222 |
submit_url = f"{api_url}/submit"
|
| 223 |
|
|
|
|
| 224 |
try:
|
| 225 |
agent = BasicAgent()
|
| 226 |
+
except Exception as e: # pylint: disable=broad-except
|
| 227 |
print(f"Error instantiating agent: {e}")
|
| 228 |
return f"Error initializing agent: {e}", None
|
| 229 |
# 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)
|
|
|
|
| 237 |
response.raise_for_status()
|
| 238 |
questions_data = response.json()
|
| 239 |
if not questions_data:
|
| 240 |
+
print("Fetched questions list is empty.")
|
| 241 |
+
return "Fetched questions list is empty or invalid format.", None
|
| 242 |
print(f"Fetched {len(questions_data)} questions.")
|
| 243 |
+
except requests.exceptions.JSONDecodeError as e:
|
| 244 |
+
print(f"Error decoding JSON response from questions endpoint: {e}")
|
| 245 |
+
print(f"Response text: {response.text[:500]}")
|
| 246 |
+
return f"Error decoding server response for questions: {e}", None
|
| 247 |
except requests.exceptions.RequestException as e:
|
| 248 |
print(f"Error fetching questions: {e}")
|
| 249 |
return f"Error fetching questions: {e}", None
|
| 250 |
+
except Exception as e: # pylint: disable=broad-except
|
|
|
|
|
|
|
|
|
|
|
|
|
| 251 |
print(f"An unexpected error occurred fetching questions: {e}")
|
| 252 |
return f"An unexpected error occurred fetching questions: {e}", None
|
| 253 |
|
|
|
|
| 258 |
for item in questions_data:
|
| 259 |
task_id = item.get("task_id")
|
| 260 |
question_text = item.get("question")
|
| 261 |
+
|
| 262 |
+
# ----------fetch any attached file ----------
|
| 263 |
+
try:
|
| 264 |
+
file_path = download_file_if_any(api_url, task_id)
|
| 265 |
+
except Exception as e: # pylint: disable=broad-except
|
| 266 |
+
file_path = None
|
| 267 |
+
print(f"[file fetch error] {task_id}: {e}")
|
| 268 |
+
|
| 269 |
+
# ---------- Build the prompt sent to the agent ----------
|
| 270 |
+
if file_path:
|
| 271 |
+
q_for_agent = (
|
| 272 |
+
f"{question_text}\n\n"
|
| 273 |
+
f"---\n"
|
| 274 |
+
f"A file was downloaded for this task and saved locally at:\n"
|
| 275 |
+
f"{file_path}\n"
|
| 276 |
+
f"---\n\n"
|
| 277 |
+
)
|
| 278 |
+
else:
|
| 279 |
+
q_for_agent = question_text
|
| 280 |
+
|
| 281 |
if not task_id or question_text is None:
|
| 282 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 283 |
continue
|
| 284 |
try:
|
| 285 |
+
submitted_answer = agent(q_for_agent)
|
| 286 |
+
answers_payload.append(
|
| 287 |
+
{"task_id": task_id, "submitted_answer": submitted_answer}
|
| 288 |
+
)
|
| 289 |
+
results_log.append(
|
| 290 |
+
{
|
| 291 |
+
"Task ID": task_id,
|
| 292 |
+
"Question": question_text,
|
| 293 |
+
"Submitted Answer": submitted_answer,
|
| 294 |
+
}
|
| 295 |
+
)
|
| 296 |
+
except Exception as e: # pylint: disable=broad-except
|
| 297 |
+
print(f"Error running agent on task {task_id}: {e}")
|
| 298 |
+
results_log.append(
|
| 299 |
+
{
|
| 300 |
+
"Task ID": task_id,
|
| 301 |
+
"Question": question_text,
|
| 302 |
+
"Submitted Answer": f"AGENT ERROR: {e}",
|
| 303 |
+
}
|
| 304 |
+
)
|
| 305 |
|
| 306 |
if not answers_payload:
|
| 307 |
print("Agent did not produce any answers to submit.")
|
| 308 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 309 |
|
| 310 |
+
# 4. Prepare Submission
|
| 311 |
+
submission_data = {
|
| 312 |
+
"username": username.strip(),
|
| 313 |
+
"agent_code": agent_code,
|
| 314 |
+
"answers": answers_payload,
|
| 315 |
+
}
|
| 316 |
+
|
| 317 |
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
| 318 |
print(status_update)
|
| 319 |
|
|
|
|
| 354 |
print(status_message)
|
| 355 |
results_df = pd.DataFrame(results_log)
|
| 356 |
return status_message, results_df
|
| 357 |
+
except Exception as e: # pylint: disable=broad-except
|
| 358 |
status_message = f"An unexpected error occurred during submission: {e}"
|
| 359 |
print(status_message)
|
| 360 |
results_df = pd.DataFrame(results_log)
|
|
|
|
| 367 |
gr.Markdown(
|
| 368 |
"""
|
| 369 |
**Instructions:**
|
|
|
|
| 370 |
1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
|
| 371 |
2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
|
| 372 |
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
|
|
|
| 373 |
---
|
| 374 |
**Disclaimers:**
|
| 375 |
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).
|
|
|
|
| 381 |
|
| 382 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 383 |
|
| 384 |
+
status_output = gr.Textbox(
|
| 385 |
+
label="Run Status / Submission Result", lines=5, interactive=False
|
| 386 |
+
)
|
| 387 |
# Removed max_rows=10 from DataFrame constructor
|
| 388 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 389 |
|
| 390 |
+
run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
|
|
|
|
|
|
|
|
|
|
| 391 |
|
| 392 |
if __name__ == "__main__":
|
| 393 |
+
print("\n" + "-" * 30 + " App Starting " + "-" * 30)
|
| 394 |
# Check for SPACE_HOST and SPACE_ID at startup for information
|
| 395 |
space_host_startup = os.getenv("SPACE_HOST")
|
| 396 |
+
space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
|
| 397 |
|
| 398 |
if space_host_startup:
|
| 399 |
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
|
|
|
| 401 |
else:
|
| 402 |
print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
|
| 403 |
|
| 404 |
+
if space_id_startup: # Print repo URLs if SPACE_ID is found
|
| 405 |
print(f"✅ SPACE_ID found: {space_id_startup}")
|
| 406 |
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
| 407 |
+
print(
|
| 408 |
+
f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main"
|
| 409 |
+
)
|
| 410 |
else:
|
| 411 |
+
print(
|
| 412 |
+
"ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined."
|
| 413 |
+
)
|
| 414 |
|
| 415 |
+
print("-" * (60 + len(" App Starting ")) + "\n")
|
| 416 |
|
| 417 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 418 |
+
demo.launch(debug=True, share=False)
|