Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,103 +1,278 @@
|
|
| 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 |
-
|
| 12 |
-
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 13 |
-
class BasicAgent:
|
| 14 |
def __init__(self):
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 42 |
try:
|
| 43 |
-
agent =
|
| 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:
|
| 58 |
-
|
| 59 |
-
return "Fetched questions list is empty or invalid format.", None
|
| 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 |
-
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 |
-
|
| 70 |
-
|
| 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")
|
| 79 |
if not task_id or question_text is None:
|
| 80 |
-
|
| 81 |
-
continue
|
| 82 |
try:
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 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 = {"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()
|
|
@@ -109,88 +284,32 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 109 |
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
| 110 |
f"Message: {result_data.get('message', 'No message received.')}"
|
| 111 |
)
|
| 112 |
-
|
| 113 |
-
results_df = pd.DataFrame(results_log)
|
| 114 |
-
return final_status, results_df
|
| 115 |
except requests.exceptions.HTTPError as e:
|
| 116 |
-
|
| 117 |
-
|
| 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)
|
| 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 |
-
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 |
-
|
| 138 |
-
print(status_message)
|
| 139 |
-
results_df = pd.DataFrame(results_log)
|
| 140 |
-
return status_message, results_df
|
| 141 |
|
| 142 |
|
| 143 |
-
# ---
|
| 144 |
with gr.Blocks() as demo:
|
| 145 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
| 146 |
gr.Markdown(
|
| 147 |
"""
|
| 148 |
**Instructions:**
|
| 149 |
|
| 150 |
-
1.
|
| 151 |
-
2.
|
| 152 |
-
3.
|
| 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 seperate action or even to answer the questions in async.
|
| 158 |
"""
|
| 159 |
)
|
| 160 |
-
|
| 161 |
gr.LoginButton()
|
| 162 |
-
|
| 163 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 164 |
-
|
| 165 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 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("
|
| 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") # Get SPACE_ID at startup
|
| 179 |
-
|
| 180 |
-
if space_host_startup:
|
| 181 |
-
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
| 182 |
-
print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
|
| 183 |
-
else:
|
| 184 |
-
print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
|
| 185 |
-
|
| 186 |
-
if space_id_startup: # Print repo URLs if SPACE_ID is found
|
| 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 |
import os
|
| 2 |
+
import re
|
| 3 |
+
import sys
|
| 4 |
+
import base64
|
| 5 |
+
import tempfile
|
| 6 |
+
import subprocess
|
| 7 |
+
import threading
|
| 8 |
+
import concurrent.futures as cf
|
| 9 |
+
|
| 10 |
import gradio as gr
|
| 11 |
import requests
|
|
|
|
| 12 |
import pandas as pd
|
| 13 |
+
import anthropic
|
| 14 |
+
from bs4 import BeautifulSoup
|
| 15 |
|
|
|
|
| 16 |
# --- Constants ---
|
| 17 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 18 |
+
MODEL = os.getenv("CLAUDE_MODEL", "claude-sonnet-5-5")
|
| 19 |
+
MAX_STEPS = 14 # tool-use rounds per question
|
| 20 |
+
MAX_WORKERS = 3 # questions answered in parallel (lower it if you hit rate limits)
|
| 21 |
+
CHUNK = 12000 # characters returned per page fetch
|
| 22 |
+
|
| 23 |
+
IMAGE_TYPES = {".png": "image/png", ".jpg": "image/jpeg", ".jpeg": "image/jpeg",
|
| 24 |
+
".gif": "image/gif", ".webp": "image/webp"}
|
| 25 |
+
AUDIO_EXT = (".mp3", ".wav", ".m4a", ".flac", ".ogg")
|
| 26 |
+
|
| 27 |
+
SYSTEM = """You are a careful research agent solving questions from the GAIA benchmark.
|
| 28 |
+
Answers are graded by exact match, so the format matters as much as the content.
|
| 29 |
+
|
| 30 |
+
Method:
|
| 31 |
+
- Think step by step and use the tools whenever a fact is not certain. Do not guess from memory
|
| 32 |
+
when a lookup is possible. For counting, sorting and arithmetic, use run_python.
|
| 33 |
+
- Some questions come with an attached file; its local path is given in the message.
|
| 34 |
+
Use read_file (text, Excel, audio transcription) or run_python on it. Images are attached directly.
|
| 35 |
+
- Wikipedia pages: fetch_url works well. For tables, run_python with pandas.read_html is more reliable.
|
| 36 |
+
- Respect every detail of the question (date ranges, "inclusive", "only first name", alphabetical order, etc.).
|
| 37 |
+
- If a question is a puzzle (reversed text, logic table), solve it directly and then apply the instruction it contains.
|
| 38 |
+
|
| 39 |
+
Output rules:
|
| 40 |
+
- End your reply with one line: FINAL ANSWER: <answer>
|
| 41 |
+
- The answer must be only the requested value: a number, a short phrase, or a comma-separated list.
|
| 42 |
+
- No units, currency symbols or thousands separators unless the question asks for them.
|
| 43 |
+
- No articles or abbreviations in strings unless asked. Write numbers as digits.
|
| 44 |
+
- If you truly cannot find the answer after trying, still give your best single guess."""
|
| 45 |
+
|
| 46 |
+
# --- Tools ---------------------------------------------------------------
|
| 47 |
+
|
| 48 |
+
TOOLS = [
|
| 49 |
+
{"type": "web_search_20250305", "name": "web_search", "max_uses": 6},
|
| 50 |
+
{
|
| 51 |
+
"name": "fetch_url",
|
| 52 |
+
"description": "Download a web page and return its text content (up to ~12000 characters per call). "
|
| 53 |
+
"Use `start` to read further into long pages.",
|
| 54 |
+
"input_schema": {
|
| 55 |
+
"type": "object",
|
| 56 |
+
"properties": {"url": {"type": "string"}, "start": {"type": "integer", "description": "Character offset, default 0"}},
|
| 57 |
+
"required": ["url"],
|
| 58 |
+
},
|
| 59 |
+
},
|
| 60 |
+
{
|
| 61 |
+
"name": "run_python",
|
| 62 |
+
"description": "Run Python 3 code (pandas, requests, bs4 available) and return stdout/stderr. "
|
| 63 |
+
"Always print() what you need. 60 second limit.",
|
| 64 |
+
"input_schema": {"type": "object", "properties": {"code": {"type": "string"}}, "required": ["code"]},
|
| 65 |
+
},
|
| 66 |
+
{
|
| 67 |
+
"name": "read_file",
|
| 68 |
+
"description": "Read a local file: text/code/csv/json, Excel (all sheets as CSV), or audio (transcribed to text).",
|
| 69 |
+
"input_schema": {"type": "object", "properties": {"path": {"type": "string"}}, "required": ["path"]},
|
| 70 |
+
},
|
| 71 |
+
{
|
| 72 |
+
"name": "youtube_transcript",
|
| 73 |
+
"description": "Get the transcript (subtitles) of a YouTube video. Cannot see the images of the video.",
|
| 74 |
+
"input_schema": {"type": "object", "properties": {"url": {"type": "string"}}, "required": ["url"]},
|
| 75 |
+
},
|
| 76 |
+
]
|
| 77 |
+
|
| 78 |
+
_whisper = None
|
| 79 |
+
_whisper_lock = threading.Lock()
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
def transcribe(path: str) -> str:
|
| 83 |
+
global _whisper
|
| 84 |
+
with _whisper_lock:
|
| 85 |
+
if _whisper is None:
|
| 86 |
+
from faster_whisper import WhisperModel
|
| 87 |
+
_whisper = WhisperModel("base", device="cpu", compute_type="int8")
|
| 88 |
+
segments, _ = _whisper.transcribe(path)
|
| 89 |
+
return " ".join(s.text.strip() for s in segments)
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
def tool_fetch_url(url: str, start: int = 0) -> str:
|
| 93 |
+
r = requests.get(url, headers={"User-Agent": "Mozilla/5.0 (compatible; AgentCourseBot/1.0)"}, timeout=25)
|
| 94 |
+
r.raise_for_status()
|
| 95 |
+
if "html" in r.headers.get("content-type", ""):
|
| 96 |
+
soup = BeautifulSoup(r.text, "html.parser")
|
| 97 |
+
for t in soup(["script", "style", "nav", "footer"]):
|
| 98 |
+
t.decompose()
|
| 99 |
+
text = re.sub(r"\n{2,}", "\n", soup.get_text("\n"))
|
| 100 |
+
else:
|
| 101 |
+
text = r.text
|
| 102 |
+
return text[start:start + CHUNK] or "(no more content)"
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
def tool_run_python(code: str) -> str:
|
| 106 |
+
with tempfile.NamedTemporaryFile("w", suffix=".py", delete=False) as f:
|
| 107 |
+
f.write(code)
|
| 108 |
+
env = {k: v for k, v in os.environ.items() if k not in ("ANTHROPIC_API_KEY", "HF_TOKEN")}
|
| 109 |
+
try:
|
| 110 |
+
p = subprocess.run([sys.executable, f.name], capture_output=True, text=True, timeout=60, env=env)
|
| 111 |
+
except subprocess.TimeoutExpired:
|
| 112 |
+
return "Timed out after 60 seconds."
|
| 113 |
+
out = p.stdout + (f"\nSTDERR:\n{p.stderr}" if p.stderr else "")
|
| 114 |
+
return out[:8000] or "(no output)"
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
def tool_read_file(path: str) -> str:
|
| 118 |
+
ext = os.path.splitext(path)[1].lower()
|
| 119 |
+
if ext in AUDIO_EXT:
|
| 120 |
+
return transcribe(path)
|
| 121 |
+
if ext in (".xlsx", ".xls"):
|
| 122 |
+
sheets = pd.read_excel(path, sheet_name=None)
|
| 123 |
+
return "\n\n".join(f"## Sheet: {n}\n{df.to_csv(index=False)}" for n, df in sheets.items())[:15000]
|
| 124 |
+
with open(path, "r", errors="replace") as f:
|
| 125 |
+
return f.read()[:15000]
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
def tool_youtube_transcript(url: str) -> str:
|
| 129 |
+
from youtube_transcript_api import YouTubeTranscriptApi
|
| 130 |
+
m = re.search(r"(?:v=|youtu\.be/)([\w-]{11})", url)
|
| 131 |
+
if not m:
|
| 132 |
+
return "Could not find a video id in that URL."
|
| 133 |
+
data = YouTubeTranscriptApi().fetch(m.group(1))
|
| 134 |
+
return "\n".join(f"[{int(s.start)}s] {s.text}" for s in data)[:15000]
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
def run_tool(name: str, args: dict) -> str:
|
| 138 |
+
try:
|
| 139 |
+
if name == "fetch_url":
|
| 140 |
+
return tool_fetch_url(**args)
|
| 141 |
+
if name == "run_python":
|
| 142 |
+
return tool_run_python(**args)
|
| 143 |
+
if name == "read_file":
|
| 144 |
+
return tool_read_file(**args)
|
| 145 |
+
if name == "youtube_transcript":
|
| 146 |
+
return tool_youtube_transcript(**args)
|
| 147 |
+
return f"Unknown tool {name}"
|
| 148 |
+
except Exception as e:
|
| 149 |
+
return f"Tool error: {type(e).__name__}: {e}"
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
# --- Agent ---------------------------------------------------------------
|
| 153 |
+
|
| 154 |
+
def extract_final(resp) -> str:
|
| 155 |
+
text = "".join(b.text for b in resp.content if b.type == "text").strip()
|
| 156 |
+
matches = re.findall(r"FINAL ANSWER:\s*(.+)", text)
|
| 157 |
+
answer = matches[-1].strip() if matches else text.splitlines()[-1].strip() if text else ""
|
| 158 |
+
return answer.strip().strip("*").rstrip(".") if answer else ""
|
| 159 |
+
|
| 160 |
|
| 161 |
+
class ClaudeAgent:
|
|
|
|
|
|
|
| 162 |
def __init__(self):
|
| 163 |
+
self.client = anthropic.Anthropic(max_retries=6)
|
| 164 |
+
print(f"ClaudeAgent initialized with model {MODEL}")
|
| 165 |
+
|
| 166 |
+
def __call__(self, question: str, file_path: str | None = None) -> str:
|
| 167 |
+
content = []
|
| 168 |
+
text = question
|
| 169 |
+
if file_path:
|
| 170 |
+
ext = os.path.splitext(file_path)[1].lower()
|
| 171 |
+
if ext in IMAGE_TYPES:
|
| 172 |
+
with open(file_path, "rb") as f:
|
| 173 |
+
data = base64.standard_b64encode(f.read()).decode()
|
| 174 |
+
content.append({"type": "image", "source": {"type": "base64", "media_type": IMAGE_TYPES[ext], "data": data}})
|
| 175 |
+
text += "\n\n(The attached image is shown above.)"
|
| 176 |
+
else:
|
| 177 |
+
text += f"\n\n[Attached file saved locally at: {file_path}]"
|
| 178 |
+
content.append({"type": "text", "text": text})
|
| 179 |
+
messages = [{"role": "user", "content": content}]
|
| 180 |
+
|
| 181 |
+
resp = None
|
| 182 |
+
for _ in range(MAX_STEPS):
|
| 183 |
+
resp = self.client.messages.create(
|
| 184 |
+
model=MODEL, max_tokens=3000, system=SYSTEM, tools=TOOLS, messages=messages
|
| 185 |
+
)
|
| 186 |
+
messages.append({"role": "assistant", "content": resp.content})
|
| 187 |
+
if resp.stop_reason == "tool_use":
|
| 188 |
+
results = [
|
| 189 |
+
{"type": "tool_result", "tool_use_id": b.id, "content": run_tool(b.name, b.input)}
|
| 190 |
+
for b in resp.content if b.type == "tool_use"
|
| 191 |
+
]
|
| 192 |
+
messages.append({"role": "user", "content": results})
|
| 193 |
+
elif resp.stop_reason == "pause_turn":
|
| 194 |
+
continue
|
| 195 |
+
else:
|
| 196 |
+
break
|
| 197 |
+
|
| 198 |
+
# If we ran out of steps without a final answer, force one.
|
| 199 |
+
full = "".join(b.text for b in resp.content if b.type == "text") if resp else ""
|
| 200 |
+
if "FINAL ANSWER:" not in full:
|
| 201 |
+
messages.append({"role": "user", "content": "Stop using tools. Give your best final answer now, ending with 'FINAL ANSWER: <answer>'."})
|
| 202 |
+
resp = self.client.messages.create(model=MODEL, max_tokens=500, system=SYSTEM, messages=messages)
|
| 203 |
+
return extract_final(resp)
|
| 204 |
+
|
| 205 |
+
|
| 206 |
+
def download_task_file(task_id: str, file_name: str) -> str | None:
|
| 207 |
+
if not file_name:
|
| 208 |
+
return None
|
| 209 |
+
try:
|
| 210 |
+
r = requests.get(f"{DEFAULT_API_URL}/files/{task_id}", timeout=30)
|
| 211 |
+
if r.status_code != 200 or not r.content:
|
| 212 |
+
return None
|
| 213 |
+
path = os.path.join(tempfile.gettempdir(), f"{task_id}_{file_name}")
|
| 214 |
+
with open(path, "wb") as f:
|
| 215 |
+
f.write(r.content)
|
| 216 |
+
return path
|
| 217 |
+
except Exception as e:
|
| 218 |
+
print(f"Could not download file for {task_id}: {e}")
|
| 219 |
+
return None
|
| 220 |
+
|
| 221 |
+
|
| 222 |
+
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 223 |
+
space_id = os.getenv("SPACE_ID")
|
| 224 |
|
| 225 |
if profile:
|
| 226 |
+
username = f"{profile.username}"
|
| 227 |
print(f"User logged in: {username}")
|
| 228 |
else:
|
|
|
|
| 229 |
return "Please Login to Hugging Face with the button.", None
|
| 230 |
|
| 231 |
+
if not os.getenv("ANTHROPIC_API_KEY"):
|
| 232 |
+
return "Missing ANTHROPIC_API_KEY. Add it as a secret in the Space settings.", None
|
| 233 |
+
|
| 234 |
+
questions_url = f"{DEFAULT_API_URL}/questions"
|
| 235 |
+
submit_url = f"{DEFAULT_API_URL}/submit"
|
| 236 |
|
|
|
|
| 237 |
try:
|
| 238 |
+
agent = ClaudeAgent()
|
| 239 |
except Exception as e:
|
|
|
|
| 240 |
return f"Error initializing agent: {e}", None
|
|
|
|
| 241 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
|
|
|
| 242 |
|
|
|
|
|
|
|
| 243 |
try:
|
| 244 |
response = requests.get(questions_url, timeout=15)
|
| 245 |
response.raise_for_status()
|
| 246 |
questions_data = response.json()
|
| 247 |
if not questions_data:
|
| 248 |
+
return "Fetched questions list is empty or invalid format.", None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 249 |
except Exception as e:
|
| 250 |
+
return f"Error fetching questions: {e}", None
|
| 251 |
+
|
| 252 |
+
def solve(item):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 253 |
task_id = item.get("task_id")
|
| 254 |
question_text = item.get("question")
|
| 255 |
if not task_id or question_text is None:
|
| 256 |
+
return None
|
|
|
|
| 257 |
try:
|
| 258 |
+
path = download_task_file(task_id, item.get("file_name"))
|
| 259 |
+
answer = agent(question_text, path)
|
|
|
|
| 260 |
except Exception as e:
|
| 261 |
+
print(f"Error on task {task_id}: {e}")
|
| 262 |
+
answer = f"AGENT ERROR: {e}"
|
| 263 |
+
print(f"[{task_id}] -> {answer}")
|
| 264 |
+
return task_id, question_text, answer
|
| 265 |
+
|
| 266 |
+
with cf.ThreadPoolExecutor(max_workers=MAX_WORKERS) as pool:
|
| 267 |
+
outputs = [o for o in pool.map(solve, questions_data) if o]
|
| 268 |
+
|
| 269 |
+
results_log = [{"Task ID": t, "Question": q, "Submitted Answer": a} for t, q, a in outputs]
|
| 270 |
+
answers_payload = [{"task_id": t, "submitted_answer": a} for t, q, a in outputs if not a.startswith("AGENT ERROR")]
|
| 271 |
|
| 272 |
if not answers_payload:
|
|
|
|
| 273 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 274 |
|
|
|
|
| 275 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 276 |
try:
|
| 277 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
| 278 |
response.raise_for_status()
|
|
|
|
| 284 |
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
| 285 |
f"Message: {result_data.get('message', 'No message received.')}"
|
| 286 |
)
|
| 287 |
+
return final_status, pd.DataFrame(results_log)
|
|
|
|
|
|
|
| 288 |
except requests.exceptions.HTTPError as e:
|
| 289 |
+
detail = f"Server responded with status {e.response.status_code}. Response: {e.response.text[:500]}"
|
| 290 |
+
return f"Submission Failed: {detail}", pd.DataFrame(results_log)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 291 |
except Exception as e:
|
| 292 |
+
return f"Submission Failed: {e}", pd.DataFrame(results_log)
|
|
|
|
|
|
|
|
|
|
| 293 |
|
| 294 |
|
| 295 |
+
# --- Gradio interface ---
|
| 296 |
with gr.Blocks() as demo:
|
| 297 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
| 298 |
gr.Markdown(
|
| 299 |
"""
|
| 300 |
**Instructions:**
|
| 301 |
|
| 302 |
+
1. Add your `ANTHROPIC_API_KEY` as a secret in the Space settings.
|
| 303 |
+
2. Log in to your Hugging Face account using the button below.
|
| 304 |
+
3. Click 'Run Evaluation & Submit All Answers'. It can take 10 to 20 minutes.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 305 |
"""
|
| 306 |
)
|
|
|
|
| 307 |
gr.LoginButton()
|
|
|
|
| 308 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
|
|
|
| 309 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
|
|
|
| 310 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 311 |
+
run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 312 |
|
| 313 |
if __name__ == "__main__":
|
| 314 |
+
print("Launching Gradio Interface for Agent Evaluation...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 315 |
demo.launch(debug=True, share=False)
|