Update app.py
Browse files
app.py
CHANGED
|
@@ -1,279 +1,359 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import pandas as pd
|
| 3 |
-
import requests
|
| 4 |
-
import smolagents
|
| 5 |
-
print("SmolAgents version:", smolagents.__version__)
|
| 6 |
-
from smolagents import Tool, CodeAgent, InferenceClientModel, load_tool
|
| 7 |
-
from smolagents import DuckDuckGoSearchTool, WikipediaSearchTool, PythonInterpreterTool, UserInputTool
|
| 8 |
import gradio as gr
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
model = InferenceClientModel(
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
|
|
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
# fixed_answer = "This is a default answer."
|
| 25 |
-
# print(f"Agent returning fixed answer: {fixed_answer}")
|
| 26 |
-
# return fixed_answer
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
# class ImageCaptioningTool(Tool):
|
| 30 |
-
# name = "image_captioner"
|
| 31 |
-
# description = "Generate a caption for an image."
|
| 32 |
-
# inputs = {"image": "image"}
|
| 33 |
-
# output_type = "text"
|
| 34 |
-
|
| 35 |
-
# def run(self, inputs: dict) -> str:
|
| 36 |
-
# image = inputs.get("image")
|
| 37 |
-
# if not image:
|
| 38 |
-
# return "No image provided."
|
| 39 |
-
# # You could run your model here instead
|
| 40 |
-
# return "This is a placeholder caption for the uploaded image."
|
| 41 |
-
|
| 42 |
-
class BasicAgent:
|
| 43 |
-
def __init__(self):
|
| 44 |
-
model = InferenceClientModel(
|
| 45 |
-
"qwen/Qwen2.5-0.5B-Instruct",
|
| 46 |
-
max_tokens=512,
|
| 47 |
-
system_message="""
|
| 48 |
-
You are a highly capable AI assistant designed to solve real-world, multi-step reasoning tasks in the GAIA benchmark.
|
| 49 |
-
Your job is to:
|
| 50 |
-
- Search the web or Wikipedia if needed
|
| 51 |
-
- Perform Python calculations or date arithmetic
|
| 52 |
-
|
| 53 |
-
Instructions:
|
| 54 |
-
1. Think step-by-step and use tools wisely.
|
| 55 |
-
2. Always return a short, direct answer — no explanation or formatting.
|
| 56 |
-
|
| 57 |
-
Examples:
|
| 58 |
-
- Q: What is the capital of France?
|
| 59 |
-
- A: Paris
|
| 60 |
-
|
| 61 |
-
Your output must be: a single clean answer string only.
|
| 62 |
-
|
| 63 |
-
"""
|
| 64 |
-
)
|
| 65 |
-
self.agent = CodeAgent(
|
| 66 |
-
tools=[
|
| 67 |
-
DuckDuckGoSearchTool(max_results=5, rate_limit=2.0),
|
| 68 |
-
WikipediaSearchTool(user_agent="my-agent", language="en"),
|
| 69 |
-
PythonInterpreterTool(),
|
| 70 |
-
UserInputTool(),
|
| 71 |
-
# ImageCaptioningTool(),
|
| 72 |
-
],
|
| 73 |
-
model=model
|
| 74 |
-
|
| 75 |
-
)
|
| 76 |
-
print("BasicAgent initialized.")
|
| 77 |
-
# print("Available tools:", [tool.name for tool in self.agent.tools])
|
| 78 |
-
def __call__(self, question):
|
| 79 |
-
if isinstance(question, dict):
|
| 80 |
-
text = question.get("question", "")
|
| 81 |
-
# ignoring image context for now since agent.run doesn't support it
|
| 82 |
-
else:
|
| 83 |
-
text = question
|
| 84 |
-
|
| 85 |
-
print(f"Agent received question (first 50 chars): {text[:50]}...")
|
| 86 |
-
answer = self.agent.run(text)
|
| 87 |
-
return answer.strip()
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
| 98 |
-
|
| 99 |
-
if profile:
|
| 100 |
-
username= f"{profile.username}"
|
| 101 |
-
print(f"User logged in: {username}")
|
| 102 |
-
else:
|
| 103 |
-
print("User not logged in.")
|
| 104 |
-
return "Please Login to Hugging Face with the button.", None
|
| 105 |
-
|
| 106 |
-
api_url = DEFAULT_API_URL
|
| 107 |
-
questions_url = f"{api_url}/questions"
|
| 108 |
-
submit_url = f"{api_url}/submit"
|
| 109 |
-
|
| 110 |
-
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 111 |
-
try:
|
| 112 |
-
agent = BasicAgent()
|
| 113 |
-
except Exception as e:
|
| 114 |
-
print(f"Error instantiating agent: {e}")
|
| 115 |
-
return f"Error initializing agent: {e}", None
|
| 116 |
-
# 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)
|
| 117 |
-
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 118 |
-
print(agent_code)
|
| 119 |
-
|
| 120 |
-
# 2. Fetch Questions
|
| 121 |
-
print(f"Fetching questions from: {questions_url}")
|
| 122 |
try:
|
| 123 |
-
|
| 124 |
-
response.raise_for_status()
|
| 125 |
-
questions_data = response.json()
|
| 126 |
-
if not questions_data:
|
| 127 |
-
print("Fetched questions list is empty.")
|
| 128 |
-
return "Fetched questions list is empty or invalid format.", None
|
| 129 |
-
print(f"Fetched {len(questions_data)} questions.")
|
| 130 |
-
except requests.exceptions.RequestException as e:
|
| 131 |
-
print(f"Error fetching questions: {e}")
|
| 132 |
-
return f"Error fetching questions: {e}", None
|
| 133 |
-
except requests.exceptions.JSONDecodeError as e:
|
| 134 |
-
print(f"Error decoding JSON response from questions endpoint: {e}")
|
| 135 |
-
print(f"Response text: {response.text[:500]}")
|
| 136 |
-
return f"Error decoding server response for questions: {e}", None
|
| 137 |
except Exception as e:
|
| 138 |
-
|
| 139 |
-
return f"An unexpected error occurred fetching questions: {e}", None
|
| 140 |
-
|
| 141 |
|
| 142 |
-
# question_text = item.get("question")
|
| 143 |
-
# question_input = {"question": question_text}
|
| 144 |
-
# if "image" in item:
|
| 145 |
-
# question_input["image"] = item["image"]
|
| 146 |
-
# submitted_answer = agent(question_input)
|
| 147 |
-
# 3. Run your Agent
|
| 148 |
results_log = []
|
| 149 |
answers_payload = []
|
| 150 |
-
|
| 151 |
for item in questions_data:
|
| 152 |
task_id = item.get("task_id")
|
| 153 |
question_text = item.get("question")
|
| 154 |
-
|
| 155 |
-
|
| 156 |
if not task_id or question_text is None:
|
| 157 |
-
print(f"Skipping item with missing task_id or question: {item}")
|
| 158 |
continue
|
| 159 |
-
|
| 160 |
try:
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
submitted_answer = agent(question_input)
|
| 165 |
-
|
| 166 |
-
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 167 |
-
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 168 |
-
|
| 169 |
except Exception as e:
|
| 170 |
-
|
| 171 |
-
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer":
|
| 172 |
|
| 173 |
if not answers_payload:
|
| 174 |
-
print("Agent did not produce any answers to submit.")
|
| 175 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 176 |
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
| 180 |
-
print(status_update)
|
| 181 |
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
results_df = pd.DataFrame(results_log)
|
| 197 |
-
return final_status, results_df
|
| 198 |
-
except requests.exceptions.HTTPError as e:
|
| 199 |
-
error_detail = f"Server responded with status {e.response.status_code}."
|
| 200 |
-
try:
|
| 201 |
-
error_json = e.response.json()
|
| 202 |
-
error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
|
| 203 |
-
except requests.exceptions.JSONDecodeError:
|
| 204 |
-
error_detail += f" Response: {e.response.text[:500]}"
|
| 205 |
-
status_message = f"Submission Failed: {error_detail}"
|
| 206 |
-
print(status_message)
|
| 207 |
-
results_df = pd.DataFrame(results_log)
|
| 208 |
-
return status_message, results_df
|
| 209 |
-
except requests.exceptions.Timeout:
|
| 210 |
-
status_message = "Submission Failed: The request timed out."
|
| 211 |
-
print(status_message)
|
| 212 |
-
results_df = pd.DataFrame(results_log)
|
| 213 |
-
return status_message, results_df
|
| 214 |
-
except requests.exceptions.RequestException as e:
|
| 215 |
-
status_message = f"Submission Failed: Network error - {e}"
|
| 216 |
-
print(status_message)
|
| 217 |
-
results_df = pd.DataFrame(results_log)
|
| 218 |
-
return status_message, results_df
|
| 219 |
-
except Exception as e:
|
| 220 |
-
status_message = f"An unexpected error occurred during submission: {e}"
|
| 221 |
-
print(status_message)
|
| 222 |
-
results_df = pd.DataFrame(results_log)
|
| 223 |
-
return status_message, results_df
|
| 224 |
|
| 225 |
|
| 226 |
-
# --- Build Gradio Interface using Blocks ---
|
| 227 |
-
with gr.Blocks() as demo:
|
| 228 |
-
gr.Markdown("# Basic Agent Evaluation Runner")
|
| 229 |
-
gr.Markdown(
|
| 230 |
-
"""
|
| 231 |
-
**Instructions:**
|
| 232 |
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 236 |
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
)
|
| 243 |
|
| 244 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
|
| 246 |
-
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 247 |
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 251 |
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
)
|
| 256 |
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import json
|
| 4 |
+
from smolagents import CodeAgent, InferenceClientModel
|
| 5 |
+
from smolagents.tools import (
|
| 6 |
+
DuckDuckGoSearchTool,
|
| 7 |
+
WikipediaSearchTool,
|
| 8 |
+
PythonInterpreterTool,
|
| 9 |
+
UserInputTool
|
| 10 |
+
)
|
| 11 |
|
| 12 |
+
# -------------------------- MODEL SETUP -------------------------- #
|
| 13 |
+
model = InferenceClientModel(
|
| 14 |
+
model_id="Qwen/Qwen2-7B-Instruct", # You can change this to another Hugging Face Inference-compatible model
|
| 15 |
+
system_message="You are a helpful AI assistant for answering academic questions in a concise and accurate way.",
|
| 16 |
+
max_tokens=512,
|
| 17 |
+
)
|
| 18 |
|
| 19 |
+
# -------------------------- TOOLS SETUP -------------------------- #
|
| 20 |
+
tools = [
|
| 21 |
+
DuckDuckGoSearchTool(max_results=5, rate_limit=2.0),
|
| 22 |
+
WikipediaSearchTool(language="en", user_agent="my-eval-agent"),
|
| 23 |
+
PythonInterpreterTool(),
|
| 24 |
+
UserInputTool()
|
| 25 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
# -------------------------- AGENT SETUP -------------------------- #
|
| 28 |
+
agent = CodeAgent(
|
| 29 |
+
model=model,
|
| 30 |
+
tools=tools
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
# -------------------------- EVALUATION FUNCTION -------------------------- #
|
| 34 |
+
def run_agent(file):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
try:
|
| 36 |
+
questions_data = json.load(file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
except Exception as e:
|
| 38 |
+
return f"Invalid JSON: {e}", pd.DataFrame()
|
|
|
|
|
|
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
results_log = []
|
| 41 |
answers_payload = []
|
| 42 |
+
|
| 43 |
for item in questions_data:
|
| 44 |
task_id = item.get("task_id")
|
| 45 |
question_text = item.get("question")
|
| 46 |
+
|
|
|
|
| 47 |
if not task_id or question_text is None:
|
|
|
|
| 48 |
continue
|
| 49 |
+
|
| 50 |
try:
|
| 51 |
+
answer = agent.run(question_text)
|
| 52 |
+
answers_payload.append({"task_id": task_id, "submitted_answer": answer})
|
| 53 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": answer})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
except Exception as e:
|
| 55 |
+
error_msg = f"AGENT ERROR: {e}"
|
| 56 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": error_msg})
|
| 57 |
|
| 58 |
if not answers_payload:
|
|
|
|
| 59 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 60 |
|
| 61 |
+
results_df = pd.DataFrame(results_log)
|
| 62 |
+
return "Agent run completed!", results_df
|
|
|
|
|
|
|
| 63 |
|
| 64 |
+
# -------------------------- GRADIO UI -------------------------- #
|
| 65 |
+
demo = gr.Interface(
|
| 66 |
+
fn=run_agent,
|
| 67 |
+
inputs=gr.File(label="Upload questions.json"),
|
| 68 |
+
outputs=[
|
| 69 |
+
gr.Textbox(label="Status"),
|
| 70 |
+
gr.Dataframe(label="Results", wrap=True),
|
| 71 |
+
],
|
| 72 |
+
title="SmolAgent Question Evaluator",
|
| 73 |
+
description="Upload a JSON file with `task_id` and `question` fields to evaluate the agent's performance.",
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
if __name__ == "__main__":
|
| 77 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
+
# import os
|
| 82 |
+
# import pandas as pd
|
| 83 |
+
# import requests
|
| 84 |
+
# import smolagents
|
| 85 |
+
# print("SmolAgents version:", smolagents.__version__)
|
| 86 |
+
# from smolagents import Tool, CodeAgent, InferenceClientModel, load_tool
|
| 87 |
+
# from smolagents import DuckDuckGoSearchTool, WikipediaSearchTool, PythonInterpreterTool, UserInputTool
|
| 88 |
+
# import gradio as gr
|
| 89 |
+
# from PIL import Image
|
| 90 |
|
| 91 |
+
# # 🧠 Inference model
|
| 92 |
+
# model = InferenceClientModel("qwen/Qwen2.5-0.5B-Instruct", max_tokens=512)
|
| 93 |
+
# # (Keep Constants as is)
|
| 94 |
+
# # --- Constants ---
|
| 95 |
+
# DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
|
| 96 |
|
| 97 |
+
# # --- Basic Agent Definition ---
|
| 98 |
+
# # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 99 |
+
# # class BasicAgent:
|
| 100 |
+
# # def __init__(self):
|
| 101 |
+
# # print("BasicAgent initialized.")
|
| 102 |
+
# # def __call__(self, question: str) -> str:
|
| 103 |
+
# # print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 104 |
+
# # fixed_answer = "This is a default answer."
|
| 105 |
+
# # print(f"Agent returning fixed answer: {fixed_answer}")
|
| 106 |
+
# # return fixed_answer
|
| 107 |
|
|
|
|
| 108 |
|
| 109 |
+
# # class ImageCaptioningTool(Tool):
|
| 110 |
+
# # name = "image_captioner"
|
| 111 |
+
# # description = "Generate a caption for an image."
|
| 112 |
+
# # inputs = {"image": "image"}
|
| 113 |
+
# # output_type = "text"
|
| 114 |
+
|
| 115 |
+
# # def run(self, inputs: dict) -> str:
|
| 116 |
+
# # image = inputs.get("image")
|
| 117 |
+
# # if not image:
|
| 118 |
+
# # return "No image provided."
|
| 119 |
+
# # # You could run your model here instead
|
| 120 |
+
# # return "This is a placeholder caption for the uploaded image."
|
| 121 |
+
|
| 122 |
+
# class BasicAgent:
|
| 123 |
+
# def __init__(self):
|
| 124 |
+
# model = InferenceClientModel(
|
| 125 |
+
# "qwen/Qwen2.5-0.5B-Instruct",
|
| 126 |
+
# max_tokens=512,
|
| 127 |
+
# system_message="""
|
| 128 |
+
# You are a highly capable AI assistant designed to solve real-world, multi-step reasoning tasks in the GAIA benchmark.
|
| 129 |
+
# Your job is to:
|
| 130 |
+
# - Search the web or Wikipedia if needed
|
| 131 |
+
# - Perform Python calculations or date arithmetic
|
| 132 |
+
|
| 133 |
+
# Instructions:
|
| 134 |
+
# 1. Think step-by-step and use tools wisely.
|
| 135 |
+
# 2. Always return a short, direct answer — no explanation or formatting.
|
| 136 |
|
| 137 |
+
# Examples:
|
| 138 |
+
# - Q: What is the capital of France?
|
| 139 |
+
# - A: Paris
|
|
|
|
| 140 |
|
| 141 |
+
# Your output must be: a single clean answer string only.
|
| 142 |
+
|
| 143 |
+
# """
|
| 144 |
+
# )
|
| 145 |
+
# self.agent = CodeAgent(
|
| 146 |
+
# tools=[
|
| 147 |
+
# DuckDuckGoSearchTool(max_results=5, rate_limit=2.0),
|
| 148 |
+
# WikipediaSearchTool(user_agent="my-agent", language="en"),
|
| 149 |
+
# PythonInterpreterTool(),
|
| 150 |
+
# UserInputTool(),
|
| 151 |
+
# # ImageCaptioningTool(),
|
| 152 |
+
# ],
|
| 153 |
+
# model=model
|
| 154 |
+
|
| 155 |
+
# )
|
| 156 |
+
# print("BasicAgent initialized.")
|
| 157 |
+
# # print("Available tools:", [tool.name for tool in self.agent.tools])
|
| 158 |
+
# def __call__(self, question):
|
| 159 |
+
# if isinstance(question, dict):
|
| 160 |
+
# text = question.get("question", "")
|
| 161 |
+
# # ignoring image context for now since agent.run doesn't support it
|
| 162 |
+
# else:
|
| 163 |
+
# text = question
|
| 164 |
+
|
| 165 |
+
# print(f"Agent received question (first 50 chars): {text[:50]}...")
|
| 166 |
+
# answer = self.agent.run(text)
|
| 167 |
+
# return answer.strip()
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
# def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 172 |
+
# """
|
| 173 |
+
# Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 174 |
+
# and displays the results.
|
| 175 |
+
# """
|
| 176 |
+
# # --- Determine HF Space Runtime URL and Repo URL ---
|
| 177 |
+
# space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
| 178 |
+
|
| 179 |
+
# if profile:
|
| 180 |
+
# username= f"{profile.username}"
|
| 181 |
+
# print(f"User logged in: {username}")
|
| 182 |
+
# else:
|
| 183 |
+
# print("User not logged in.")
|
| 184 |
+
# return "Please Login to Hugging Face with the button.", None
|
| 185 |
+
|
| 186 |
+
# api_url = DEFAULT_API_URL
|
| 187 |
+
# questions_url = f"{api_url}/questions"
|
| 188 |
+
# submit_url = f"{api_url}/submit"
|
| 189 |
+
|
| 190 |
+
# # 1. Instantiate Agent ( modify this part to create your agent)
|
| 191 |
+
# try:
|
| 192 |
+
# agent = BasicAgent()
|
| 193 |
+
# except Exception as e:
|
| 194 |
+
# print(f"Error instantiating agent: {e}")
|
| 195 |
+
# return f"Error initializing agent: {e}", None
|
| 196 |
+
# # 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)
|
| 197 |
+
# agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 198 |
+
# print(agent_code)
|
| 199 |
+
|
| 200 |
+
# # 2. Fetch Questions
|
| 201 |
+
# print(f"Fetching questions from: {questions_url}")
|
| 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 |
+
# print("Fetched questions list is empty.")
|
| 208 |
+
# return "Fetched questions list is empty or invalid format.", None
|
| 209 |
+
# print(f"Fetched {len(questions_data)} questions.")
|
| 210 |
+
# except requests.exceptions.RequestException as e:
|
| 211 |
+
# print(f"Error fetching questions: {e}")
|
| 212 |
+
# return f"Error fetching questions: {e}", None
|
| 213 |
+
# except requests.exceptions.JSONDecodeError as e:
|
| 214 |
+
# print(f"Error decoding JSON response from questions endpoint: {e}")
|
| 215 |
+
# print(f"Response text: {response.text[:500]}")
|
| 216 |
+
# return f"Error decoding server response for questions: {e}", None
|
| 217 |
+
# except Exception as e:
|
| 218 |
+
# print(f"An unexpected error occurred fetching questions: {e}")
|
| 219 |
+
# return f"An unexpected error occurred fetching questions: {e}", None
|
| 220 |
+
|
| 221 |
+
|
| 222 |
+
# # question_text = item.get("question")
|
| 223 |
+
# # question_input = {"question": question_text}
|
| 224 |
+
# # if "image" in item:
|
| 225 |
+
# # question_input["image"] = item["image"]
|
| 226 |
+
# # submitted_answer = agent(question_input)
|
| 227 |
+
# # 3. Run your Agent
|
| 228 |
+
# results_log = []
|
| 229 |
+
# answers_payload = []
|
| 230 |
+
# print(f"Running agent on {len(questions_data)} questions...")
|
| 231 |
+
# for item in questions_data:
|
| 232 |
+
# task_id = item.get("task_id")
|
| 233 |
+
# question_text = item.get("question")
|
| 234 |
+
# image = item.get("image", None)
|
| 235 |
+
|
| 236 |
+
# if not task_id or question_text is None:
|
| 237 |
+
# print(f"Skipping item with missing task_id or question: {item}")
|
| 238 |
+
# continue
|
| 239 |
+
|
| 240 |
+
# try:
|
| 241 |
+
# question_input = {"question": question_text}
|
| 242 |
+
# if image:
|
| 243 |
+
# question_input["image"] = {"type": "image", "data": image}
|
| 244 |
+
# submitted_answer = agent(question_input)
|
| 245 |
+
|
| 246 |
+
# answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 247 |
+
# results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 248 |
+
|
| 249 |
+
# except Exception as e:
|
| 250 |
+
# print(f"Error running agent on task {task_id}: {e}")
|
| 251 |
+
# results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
| 252 |
+
|
| 253 |
+
# if not answers_payload:
|
| 254 |
+
# print("Agent did not produce any answers to submit.")
|
| 255 |
+
# return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 256 |
+
|
| 257 |
+
# # 4. Prepare Submission
|
| 258 |
+
# submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
| 259 |
+
# status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
| 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=60)
|
| 266 |
+
# response.raise_for_status()
|
| 267 |
+
# result_data = response.json()
|
| 268 |
+
# final_status = (
|
| 269 |
+
# f"Submission Successful!\n"
|
| 270 |
+
# f"User: {result_data.get('username')}\n"
|
| 271 |
+
# f"Overall Score: {result_data.get('score', 'N/A')}% "
|
| 272 |
+
# f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
| 273 |
+
# f"Message: {result_data.get('message', 'No message received.')}"
|
| 274 |
+
# )
|
| 275 |
+
# print("Submission successful.")
|
| 276 |
+
# results_df = pd.DataFrame(results_log)
|
| 277 |
+
# return final_status, results_df
|
| 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)
|
| 288 |
+
# return status_message, results_df
|
| 289 |
+
# except requests.exceptions.Timeout:
|
| 290 |
+
# status_message = "Submission Failed: The request timed out."
|
| 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)
|
| 303 |
+
# return status_message, results_df
|
| 304 |
+
|
| 305 |
+
|
| 306 |
+
# # --- Build Gradio Interface using Blocks ---
|
| 307 |
+
# with gr.Blocks() as demo:
|
| 308 |
+
# gr.Markdown("# Basic Agent Evaluation Runner")
|
| 309 |
+
# gr.Markdown(
|
| 310 |
+
# """
|
| 311 |
+
# **Instructions:**
|
| 312 |
+
|
| 313 |
+
# 1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
|
| 314 |
+
# 2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
|
| 315 |
+
# 3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
| 316 |
+
|
| 317 |
+
# ---
|
| 318 |
+
# **Disclaimers:**
|
| 319 |
+
# 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).
|
| 320 |
+
# 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.
|
| 321 |
+
# """
|
| 322 |
+
# )
|
| 323 |
+
|
| 324 |
+
# gr.LoginButton()
|
| 325 |
+
|
| 326 |
+
# run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 327 |
+
|
| 328 |
+
# status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 329 |
+
# # Removed max_rows=10 from DataFrame constructor
|
| 330 |
+
# results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 331 |
+
|
| 332 |
+
# run_button.click(
|
| 333 |
+
# fn=run_and_submit_all,
|
| 334 |
+
# outputs=[status_output, results_table]
|
| 335 |
+
# )
|
| 336 |
+
|
| 337 |
+
# if __name__ == "__main__":
|
| 338 |
+
# print("\n" + "-"*30 + " App Starting " + "-"*30)
|
| 339 |
+
# # Check for SPACE_HOST and SPACE_ID at startup for information
|
| 340 |
+
# space_host_startup = os.getenv("SPACE_HOST")
|
| 341 |
+
# space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
|
| 342 |
+
|
| 343 |
+
# if space_host_startup:
|
| 344 |
+
# print(f"�� SPACE_HOST found: {space_host_startup}")
|
| 345 |
+
# print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
|
| 346 |
+
# else:
|
| 347 |
+
# print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
|
| 348 |
+
|
| 349 |
+
# if space_id_startup: # Print repo URLs if SPACE_ID is found
|
| 350 |
+
# print(f"✅ SPACE_ID found: {space_id_startup}")
|
| 351 |
+
# print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
| 352 |
+
# print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
|
| 353 |
+
# else:
|
| 354 |
+
# print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
|
| 355 |
+
|
| 356 |
+
# print("-"*(60 + len(" App Starting ")) + "\n")
|
| 357 |
+
|
| 358 |
+
# print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 359 |
+
# demo.launch(debug=True, share=False)
|