Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,8 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
from smolagents import (
|
| 5 |
ToolCallingAgent,
|
|
@@ -10,210 +13,588 @@ from smolagents import (
|
|
| 10 |
|
| 11 |
|
| 12 |
# =========================================================
|
| 13 |
-
#
|
| 14 |
# =========================================================
|
| 15 |
|
| 16 |
-
|
| 17 |
-
"How many studio albums were published by Mercedes Sosa "
|
| 18 |
-
"between 2000 and 2009 (included)? "
|
| 19 |
-
"You can use the latest 2022 version of English Wikipedia."
|
| 20 |
-
)
|
| 21 |
|
| 22 |
|
| 23 |
# =========================================================
|
| 24 |
# BASIC AGENT
|
| 25 |
# =========================================================
|
|
|
|
| 26 |
|
| 27 |
class BasicAgent:
|
| 28 |
-
|
| 29 |
def __init__(self):
|
| 30 |
|
| 31 |
-
print("
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
"Please add it to Hugging Face Space Secrets."
|
| 45 |
)
|
| 46 |
|
| 47 |
-
|
| 48 |
|
| 49 |
-
# -------------------------------------------------
|
| 50 |
-
# MODEL
|
| 51 |
-
# -------------------------------------------------
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
)
|
| 58 |
|
|
|
|
|
|
|
| 59 |
print(
|
| 60 |
-
"
|
| 61 |
flush=True
|
| 62 |
)
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
print(
|
| 74 |
-
"
|
| 75 |
flush=True
|
| 76 |
)
|
| 77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
print(
|
| 79 |
-
"
|
| 80 |
flush=True
|
| 81 |
)
|
| 82 |
|
| 83 |
-
# -------------------------------------------------
|
| 84 |
-
# AGENT
|
| 85 |
-
# -------------------------------------------------
|
| 86 |
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
)
|
| 93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
print(
|
| 95 |
-
"
|
|
|
|
| 96 |
flush=True
|
| 97 |
)
|
| 98 |
|
| 99 |
-
print(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
-
def __call__(self, question: str) -> str:
|
| 103 |
|
| 104 |
-
|
| 105 |
-
print("AGENT START", flush=True)
|
| 106 |
-
print("=" * 70, flush=True)
|
| 107 |
|
| 108 |
print(
|
| 109 |
-
f"
|
|
|
|
| 110 |
flush=True
|
| 111 |
)
|
| 112 |
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
-
answer = self.agent.run(question)
|
| 116 |
|
| 117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
-
|
| 120 |
-
"[FINAL ANSWER]",
|
| 121 |
-
flush=True
|
| 122 |
-
)
|
| 123 |
|
| 124 |
-
print(
|
| 125 |
-
answer,
|
| 126 |
-
flush=True
|
| 127 |
-
)
|
| 128 |
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
print(
|
| 132 |
-
"
|
|
|
|
| 133 |
flush=True
|
| 134 |
)
|
| 135 |
|
| 136 |
-
|
| 137 |
|
| 138 |
-
return str(answer)
|
| 139 |
|
| 140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
|
| 142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
)
|
| 148 |
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
)
|
| 153 |
|
|
|
|
|
|
|
|
|
|
| 154 |
print(
|
| 155 |
-
f"
|
|
|
|
| 156 |
flush=True
|
| 157 |
)
|
| 158 |
|
| 159 |
-
print("!" * 70 + "\n", flush=True)
|
| 160 |
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
)
|
| 165 |
|
| 166 |
|
| 167 |
-
#
|
| 168 |
-
#
|
| 169 |
-
#
|
| 170 |
|
| 171 |
-
|
| 172 |
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
|
|
|
|
|
|
| 176 |
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
)
|
| 181 |
|
|
|
|
| 182 |
print(
|
| 183 |
-
|
| 184 |
flush=True
|
| 185 |
)
|
| 186 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
print(
|
| 188 |
-
f"
|
|
|
|
|
|
|
| 189 |
flush=True
|
| 190 |
)
|
| 191 |
|
|
|
|
| 192 |
try:
|
| 193 |
|
| 194 |
-
|
| 195 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
|
| 197 |
-
# Run exactly ONE question
|
| 198 |
-
answer = agent(TEST_QUESTION)
|
| 199 |
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
|
| 204 |
-
return answer
|
| 205 |
|
| 206 |
except Exception as e:
|
| 207 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
print(
|
| 209 |
-
|
| 210 |
-
f"{type(e).__name__}: {e}",
|
| 211 |
flush=True
|
| 212 |
)
|
| 213 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
return (
|
| 215 |
-
|
| 216 |
-
|
| 217 |
)
|
| 218 |
|
| 219 |
|
|
@@ -223,63 +604,58 @@ def test_agent():
|
|
| 223 |
|
| 224 |
with gr.Blocks() as demo:
|
| 225 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 226 |
gr.Markdown(
|
| 227 |
"""
|
| 228 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
|
| 230 |
-
|
| 231 |
|
| 232 |
-
|
| 233 |
-
- **Provider:** Groq
|
| 234 |
-
- **Agent:** `ToolCallingAgent`
|
| 235 |
-
- **Web Search:** Enabled
|
| 236 |
-
- **Visit Webpage:** Enabled
|
| 237 |
-
- **Max Steps:** 3
|
| 238 |
-
- **Questions:** 1
|
| 239 |
-
- **GAIA `/questions` API:** Disabled
|
| 240 |
-
- **GAIA `/submit` API:** Disabled
|
| 241 |
|
| 242 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
"""
|
| 244 |
)
|
| 245 |
|
| 246 |
-
# -----------------------------------------------------
|
| 247 |
-
# SHOW TEST QUESTION
|
| 248 |
-
# -----------------------------------------------------
|
| 249 |
|
| 250 |
-
|
| 251 |
-
label="Test Question",
|
| 252 |
-
value=TEST_QUESTION,
|
| 253 |
-
lines=4,
|
| 254 |
-
interactive=False
|
| 255 |
-
)
|
| 256 |
|
| 257 |
-
# -----------------------------------------------------
|
| 258 |
-
# TEST BUTTON
|
| 259 |
-
# -----------------------------------------------------
|
| 260 |
|
| 261 |
-
|
| 262 |
-
"
|
| 263 |
)
|
| 264 |
|
| 265 |
-
# -----------------------------------------------------
|
| 266 |
-
# OUTPUT
|
| 267 |
-
# -----------------------------------------------------
|
| 268 |
|
| 269 |
-
|
| 270 |
-
label="
|
| 271 |
-
lines=
|
| 272 |
interactive=False
|
| 273 |
)
|
| 274 |
|
| 275 |
-
# -----------------------------------------------------
|
| 276 |
-
# BUTTON ACTION
|
| 277 |
-
# -----------------------------------------------------
|
| 278 |
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 283 |
)
|
| 284 |
|
| 285 |
|
|
@@ -291,81 +667,88 @@ if __name__ == "__main__":
|
|
| 291 |
|
| 292 |
print(
|
| 293 |
"\n"
|
| 294 |
-
+ "
|
|
|
|
|
|
|
| 295 |
flush=True
|
| 296 |
)
|
| 297 |
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
)
|
| 302 |
|
| 303 |
-
|
| 304 |
-
"
|
| 305 |
-
flush=True
|
| 306 |
)
|
| 307 |
|
| 308 |
-
# -----------------------------------------------------
|
| 309 |
-
# CHECK ENVIRONMENT
|
| 310 |
-
# -----------------------------------------------------
|
| 311 |
|
| 312 |
-
|
| 313 |
|
| 314 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 315 |
|
| 316 |
print(
|
| 317 |
-
f"
|
|
|
|
| 318 |
flush=True
|
| 319 |
)
|
| 320 |
|
| 321 |
else:
|
| 322 |
|
| 323 |
print(
|
| 324 |
-
"
|
|
|
|
| 325 |
flush=True
|
| 326 |
)
|
| 327 |
|
| 328 |
-
# -----------------------------------------------------
|
| 329 |
-
# CHECK API KEY
|
| 330 |
-
# -----------------------------------------------------
|
| 331 |
|
| 332 |
-
if
|
| 333 |
|
| 334 |
print(
|
| 335 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 336 |
flush=True
|
| 337 |
)
|
| 338 |
|
| 339 |
else:
|
| 340 |
|
| 341 |
print(
|
| 342 |
-
"
|
|
|
|
| 343 |
flush=True
|
| 344 |
)
|
| 345 |
|
| 346 |
-
print(
|
| 347 |
-
"\n[MODE] SINGLE QUESTION TEST",
|
| 348 |
-
flush=True
|
| 349 |
-
)
|
| 350 |
|
| 351 |
print(
|
| 352 |
-
"
|
| 353 |
flush=True
|
| 354 |
)
|
| 355 |
|
| 356 |
-
print(
|
| 357 |
-
"[MODE] /submit disabled",
|
| 358 |
-
flush=True
|
| 359 |
-
)
|
| 360 |
|
| 361 |
print(
|
| 362 |
-
"
|
|
|
|
| 363 |
flush=True
|
| 364 |
)
|
| 365 |
|
| 366 |
-
# -----------------------------------------------------
|
| 367 |
-
# LAUNCH
|
| 368 |
-
# -----------------------------------------------------
|
| 369 |
|
| 370 |
demo.launch(
|
| 371 |
debug=True,
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
import requests
|
| 4 |
+
import inspect
|
| 5 |
+
import pandas as pd
|
| 6 |
|
| 7 |
from smolagents import (
|
| 8 |
ToolCallingAgent,
|
|
|
|
| 13 |
|
| 14 |
|
| 15 |
# =========================================================
|
| 16 |
+
# CONSTANTS
|
| 17 |
# =========================================================
|
| 18 |
|
| 19 |
+
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
|
| 22 |
# =========================================================
|
| 23 |
# BASIC AGENT
|
| 24 |
# =========================================================
|
| 25 |
+
# ----- THIS IS WHERE YOU CAN BUILD WHAT YOU WANT -----
|
| 26 |
|
| 27 |
class BasicAgent:
|
|
|
|
| 28 |
def __init__(self):
|
| 29 |
|
| 30 |
+
print("[INIT] Initializing Agent...", flush=True)
|
| 31 |
+
|
| 32 |
+
# Groq model
|
| 33 |
+
self.model = OpenAIModel(
|
| 34 |
+
model_id="openai/gpt-oss-20b",
|
| 35 |
+
api_base="https://api.groq.com/openai/v1",
|
| 36 |
+
api_key=os.environ["GROQ_API_KEY"]
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
print("[INIT] Groq model initialized.", flush=True)
|
| 40 |
+
|
| 41 |
+
# Tools
|
| 42 |
+
self.tools = [
|
| 43 |
+
WebSearchTool(),
|
| 44 |
+
VisitWebpageTool()
|
| 45 |
+
]
|
| 46 |
+
|
| 47 |
+
print("[INIT] Tools initialized.", flush=True)
|
| 48 |
+
|
| 49 |
+
# IMPORTANT:
|
| 50 |
+
# Changed from CodeAgent -> ToolCallingAgent
|
| 51 |
+
self.agent = ToolCallingAgent(
|
| 52 |
+
model=self.model,
|
| 53 |
+
tools=self.tools,
|
| 54 |
+
max_steps=5,
|
| 55 |
+
verbosity_level=2
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
print("BasicAgent initialized.", flush=True)
|
| 59 |
+
|
| 60 |
|
| 61 |
+
def __call__(self, question: str) -> str:
|
| 62 |
+
|
| 63 |
+
print(
|
| 64 |
+
f"Agent received question "
|
| 65 |
+
f"(first 100 chars): {question[:100]}...",
|
| 66 |
+
flush=True
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
try:
|
| 70 |
+
|
| 71 |
+
answer = self.agent.run(question)
|
| 72 |
+
|
| 73 |
+
print(
|
| 74 |
+
f"Agent answer: {answer}",
|
| 75 |
+
flush=True
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
return str(answer)
|
| 79 |
|
| 80 |
+
except Exception as e:
|
| 81 |
|
| 82 |
+
print(
|
| 83 |
+
f"Agent error: {type(e).__name__}: {e}",
|
| 84 |
+
flush=True
|
|
|
|
| 85 |
)
|
| 86 |
|
| 87 |
+
raise
|
| 88 |
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
+
# =========================================================
|
| 91 |
+
# RUN AND SUBMIT ALL 20 QUESTIONS
|
| 92 |
+
# =========================================================
|
| 93 |
+
|
| 94 |
+
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 95 |
+
|
| 96 |
+
"""
|
| 97 |
+
Fetches all questions,
|
| 98 |
+
runs the BasicAgent on them,
|
| 99 |
+
submits all answers,
|
| 100 |
+
and displays the results.
|
| 101 |
+
"""
|
| 102 |
+
|
| 103 |
+
# -----------------------------------------------------
|
| 104 |
+
# Determine HF Space Runtime URL and Repo URL
|
| 105 |
+
# -----------------------------------------------------
|
| 106 |
+
|
| 107 |
+
space_id = os.getenv("SPACE_ID")
|
| 108 |
+
|
| 109 |
+
if profile:
|
| 110 |
+
|
| 111 |
+
username = f"{profile.username}"
|
| 112 |
+
|
| 113 |
+
print(
|
| 114 |
+
f"User logged in: {username}",
|
| 115 |
+
flush=True
|
| 116 |
)
|
| 117 |
|
| 118 |
+
else:
|
| 119 |
+
|
| 120 |
print(
|
| 121 |
+
"User not logged in.",
|
| 122 |
flush=True
|
| 123 |
)
|
| 124 |
|
| 125 |
+
return (
|
| 126 |
+
"Please Login to Hugging Face with the button.",
|
| 127 |
+
None
|
| 128 |
+
)
|
| 129 |
|
| 130 |
+
|
| 131 |
+
# -----------------------------------------------------
|
| 132 |
+
# API URLs
|
| 133 |
+
# -----------------------------------------------------
|
| 134 |
+
|
| 135 |
+
api_url = DEFAULT_API_URL
|
| 136 |
+
|
| 137 |
+
questions_url = f"{api_url}/questions"
|
| 138 |
+
|
| 139 |
+
submit_url = f"{api_url}/submit"
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
# -----------------------------------------------------
|
| 143 |
+
# 1. Instantiate Agent
|
| 144 |
+
# -----------------------------------------------------
|
| 145 |
+
|
| 146 |
+
try:
|
| 147 |
+
|
| 148 |
+
agent = BasicAgent()
|
| 149 |
+
|
| 150 |
+
except Exception as e:
|
| 151 |
|
| 152 |
print(
|
| 153 |
+
f"Error instantiating agent: {e}",
|
| 154 |
flush=True
|
| 155 |
)
|
| 156 |
|
| 157 |
+
return (
|
| 158 |
+
f"Error initializing agent: {e}",
|
| 159 |
+
None
|
| 160 |
+
)
|
| 161 |
+
|
| 162 |
+
|
| 163 |
+
# -----------------------------------------------------
|
| 164 |
+
# Agent code URL
|
| 165 |
+
# -----------------------------------------------------
|
| 166 |
+
|
| 167 |
+
agent_code = (
|
| 168 |
+
f"https://huggingface.co/spaces/"
|
| 169 |
+
f"{space_id}/tree/main"
|
| 170 |
+
)
|
| 171 |
+
|
| 172 |
+
print(
|
| 173 |
+
f"Agent code: {agent_code}",
|
| 174 |
+
flush=True
|
| 175 |
+
)
|
| 176 |
+
|
| 177 |
+
|
| 178 |
+
# -----------------------------------------------------
|
| 179 |
+
# 2. Fetch Questions
|
| 180 |
+
# -----------------------------------------------------
|
| 181 |
+
|
| 182 |
+
print(
|
| 183 |
+
f"Fetching questions from: {questions_url}",
|
| 184 |
+
flush=True
|
| 185 |
+
)
|
| 186 |
+
|
| 187 |
+
try:
|
| 188 |
+
|
| 189 |
+
response = requests.get(
|
| 190 |
+
questions_url,
|
| 191 |
+
timeout=15
|
| 192 |
+
)
|
| 193 |
+
|
| 194 |
+
response.raise_for_status()
|
| 195 |
+
|
| 196 |
+
questions_data = response.json()
|
| 197 |
+
|
| 198 |
+
|
| 199 |
+
if not questions_data:
|
| 200 |
+
|
| 201 |
+
print(
|
| 202 |
+
"Fetched questions list is empty.",
|
| 203 |
+
flush=True
|
| 204 |
+
)
|
| 205 |
+
|
| 206 |
+
return (
|
| 207 |
+
"Fetched questions list is empty "
|
| 208 |
+
"or invalid format.",
|
| 209 |
+
None
|
| 210 |
+
)
|
| 211 |
+
|
| 212 |
+
|
| 213 |
print(
|
| 214 |
+
f"Fetched {len(questions_data)} questions.",
|
| 215 |
flush=True
|
| 216 |
)
|
| 217 |
|
|
|
|
|
|
|
|
|
|
| 218 |
|
| 219 |
+
except requests.exceptions.RequestException as e:
|
| 220 |
+
|
| 221 |
+
print(
|
| 222 |
+
f"Error fetching questions: {e}",
|
| 223 |
+
flush=True
|
| 224 |
)
|
| 225 |
|
| 226 |
+
return (
|
| 227 |
+
f"Error fetching questions: {e}",
|
| 228 |
+
None
|
| 229 |
+
)
|
| 230 |
+
|
| 231 |
+
|
| 232 |
+
except requests.exceptions.JSONDecodeError as e:
|
| 233 |
+
|
| 234 |
print(
|
| 235 |
+
f"Error decoding JSON response "
|
| 236 |
+
f"from questions endpoint: {e}",
|
| 237 |
flush=True
|
| 238 |
)
|
| 239 |
|
| 240 |
+
print(
|
| 241 |
+
f"Response text: "
|
| 242 |
+
f"{response.text[:500]}",
|
| 243 |
+
flush=True
|
| 244 |
+
)
|
| 245 |
|
| 246 |
+
return (
|
| 247 |
+
f"Error decoding server response "
|
| 248 |
+
f"for questions: {e}",
|
| 249 |
+
None
|
| 250 |
+
)
|
| 251 |
|
|
|
|
| 252 |
|
| 253 |
+
except Exception as e:
|
|
|
|
|
|
|
| 254 |
|
| 255 |
print(
|
| 256 |
+
f"An unexpected error occurred "
|
| 257 |
+
f"fetching questions: {e}",
|
| 258 |
flush=True
|
| 259 |
)
|
| 260 |
|
| 261 |
+
return (
|
| 262 |
+
f"An unexpected error occurred "
|
| 263 |
+
f"fetching questions: {e}",
|
| 264 |
+
None
|
| 265 |
+
)
|
| 266 |
|
|
|
|
| 267 |
|
| 268 |
+
# -----------------------------------------------------
|
| 269 |
+
# 3. Run Agent on ALL Questions
|
| 270 |
+
# -----------------------------------------------------
|
| 271 |
+
|
| 272 |
+
results_log = []
|
| 273 |
|
| 274 |
+
answers_payload = []
|
|
|
|
|
|
|
|
|
|
| 275 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 276 |
|
| 277 |
+
print(
|
| 278 |
+
f"Running agent on "
|
| 279 |
+
f"{len(questions_data)} questions...",
|
| 280 |
+
flush=True
|
| 281 |
+
)
|
| 282 |
+
|
| 283 |
+
|
| 284 |
+
for index, item in enumerate(
|
| 285 |
+
questions_data,
|
| 286 |
+
start=1
|
| 287 |
+
):
|
| 288 |
+
|
| 289 |
+
task_id = item.get("task_id")
|
| 290 |
+
|
| 291 |
+
question_text = item.get("question")
|
| 292 |
+
|
| 293 |
+
|
| 294 |
+
if not task_id or question_text is None:
|
| 295 |
|
| 296 |
print(
|
| 297 |
+
f"Skipping item with missing "
|
| 298 |
+
f"task_id or question: {item}",
|
| 299 |
flush=True
|
| 300 |
)
|
| 301 |
|
| 302 |
+
continue
|
| 303 |
|
|
|
|
| 304 |
|
| 305 |
+
# Show progress in logs
|
| 306 |
+
print(
|
| 307 |
+
"\n"
|
| 308 |
+
+ "=" * 70,
|
| 309 |
+
flush=True
|
| 310 |
+
)
|
| 311 |
|
| 312 |
+
print(
|
| 313 |
+
f"QUESTION {index}/"
|
| 314 |
+
f"{len(questions_data)}",
|
| 315 |
+
flush=True
|
| 316 |
+
)
|
| 317 |
|
| 318 |
+
print(
|
| 319 |
+
f"Task ID: {task_id}",
|
| 320 |
+
flush=True
|
| 321 |
+
)
|
| 322 |
+
|
| 323 |
+
print(
|
| 324 |
+
f"Question: {question_text}",
|
| 325 |
+
flush=True
|
| 326 |
+
)
|
| 327 |
+
|
| 328 |
+
print(
|
| 329 |
+
"=" * 70,
|
| 330 |
+
flush=True
|
| 331 |
+
)
|
| 332 |
+
|
| 333 |
+
|
| 334 |
+
try:
|
| 335 |
+
|
| 336 |
+
submitted_answer = agent(
|
| 337 |
+
question_text
|
| 338 |
)
|
| 339 |
|
| 340 |
+
|
| 341 |
+
answers_payload.append(
|
| 342 |
+
{
|
| 343 |
+
"task_id": task_id,
|
| 344 |
+
"submitted_answer": submitted_answer
|
| 345 |
+
}
|
| 346 |
+
)
|
| 347 |
+
|
| 348 |
+
|
| 349 |
+
results_log.append(
|
| 350 |
+
{
|
| 351 |
+
"Task ID": task_id,
|
| 352 |
+
"Question": question_text,
|
| 353 |
+
"Submitted Answer": submitted_answer
|
| 354 |
+
}
|
| 355 |
)
|
| 356 |
|
| 357 |
+
|
| 358 |
+
except Exception as e:
|
| 359 |
+
|
| 360 |
print(
|
| 361 |
+
f"Error running agent on "
|
| 362 |
+
f"task {task_id}: {e}",
|
| 363 |
flush=True
|
| 364 |
)
|
| 365 |
|
|
|
|
| 366 |
|
| 367 |
+
results_log.append(
|
| 368 |
+
{
|
| 369 |
+
"Task ID": task_id,
|
| 370 |
+
"Question": question_text,
|
| 371 |
+
"Submitted Answer":
|
| 372 |
+
f"AGENT ERROR: {e}"
|
| 373 |
+
}
|
| 374 |
)
|
| 375 |
|
| 376 |
|
| 377 |
+
# -----------------------------------------------------
|
| 378 |
+
# Check answers
|
| 379 |
+
# -----------------------------------------------------
|
| 380 |
|
| 381 |
+
if not answers_payload:
|
| 382 |
|
| 383 |
+
print(
|
| 384 |
+
"Agent did not produce "
|
| 385 |
+
"any answers to submit.",
|
| 386 |
+
flush=True
|
| 387 |
+
)
|
| 388 |
|
| 389 |
+
return (
|
| 390 |
+
"Agent did not produce "
|
| 391 |
+
"any answers to submit.",
|
| 392 |
+
pd.DataFrame(results_log)
|
| 393 |
+
)
|
| 394 |
+
|
| 395 |
+
|
| 396 |
+
# -----------------------------------------------------
|
| 397 |
+
# 4. Prepare Submission
|
| 398 |
+
# -----------------------------------------------------
|
| 399 |
+
|
| 400 |
+
submission_data = {
|
| 401 |
+
"username": username.strip(),
|
| 402 |
+
"agent_code": agent_code,
|
| 403 |
+
"answers": answers_payload
|
| 404 |
+
}
|
| 405 |
+
|
| 406 |
+
|
| 407 |
+
status_update = (
|
| 408 |
+
f"Agent finished. "
|
| 409 |
+
f"Submitting {len(answers_payload)} answers "
|
| 410 |
+
f"for user '{username}'..."
|
| 411 |
)
|
| 412 |
|
| 413 |
+
|
| 414 |
print(
|
| 415 |
+
status_update,
|
| 416 |
flush=True
|
| 417 |
)
|
| 418 |
|
| 419 |
+
|
| 420 |
+
# -----------------------------------------------------
|
| 421 |
+
# 5. Submit
|
| 422 |
+
# -----------------------------------------------------
|
| 423 |
+
|
| 424 |
print(
|
| 425 |
+
f"Submitting "
|
| 426 |
+
f"{len(answers_payload)} answers "
|
| 427 |
+
f"to: {submit_url}",
|
| 428 |
flush=True
|
| 429 |
)
|
| 430 |
|
| 431 |
+
|
| 432 |
try:
|
| 433 |
|
| 434 |
+
response = requests.post(
|
| 435 |
+
submit_url,
|
| 436 |
+
json=submission_data,
|
| 437 |
+
timeout=60
|
| 438 |
+
)
|
| 439 |
+
|
| 440 |
+
response.raise_for_status()
|
| 441 |
+
|
| 442 |
+
result_data = response.json()
|
| 443 |
+
|
| 444 |
+
|
| 445 |
+
final_status = (
|
| 446 |
+
f"Submission Successful!\n"
|
| 447 |
+
f"User: "
|
| 448 |
+
f"{result_data.get('username')}\n"
|
| 449 |
+
f"Overall Score: "
|
| 450 |
+
f"{result_data.get('score', 'N/A')}% "
|
| 451 |
+
f"("
|
| 452 |
+
f"{result_data.get('correct_count', '?')}/"
|
| 453 |
+
f"{result_data.get('total_attempted', '?')} "
|
| 454 |
+
f"correct"
|
| 455 |
+
f")\n"
|
| 456 |
+
f"Message: "
|
| 457 |
+
f"{result_data.get('message', 'No message received.')}"
|
| 458 |
+
)
|
| 459 |
+
|
| 460 |
+
|
| 461 |
+
print(
|
| 462 |
+
"Submission successful.",
|
| 463 |
+
flush=True
|
| 464 |
+
)
|
| 465 |
+
|
| 466 |
+
|
| 467 |
+
results_df = pd.DataFrame(
|
| 468 |
+
results_log
|
| 469 |
+
)
|
| 470 |
+
|
| 471 |
+
|
| 472 |
+
return (
|
| 473 |
+
final_status,
|
| 474 |
+
results_df
|
| 475 |
+
)
|
| 476 |
+
|
| 477 |
+
|
| 478 |
+
except requests.exceptions.HTTPError as e:
|
| 479 |
+
|
| 480 |
+
error_detail = (
|
| 481 |
+
f"Server responded with status "
|
| 482 |
+
f"{e.response.status_code}."
|
| 483 |
+
)
|
| 484 |
|
|
|
|
|
|
|
| 485 |
|
| 486 |
+
try:
|
| 487 |
+
|
| 488 |
+
error_json = e.response.json()
|
| 489 |
+
|
| 490 |
+
error_detail += (
|
| 491 |
+
f" Detail: "
|
| 492 |
+
f"{error_json.get('detail', e.response.text)}"
|
| 493 |
+
)
|
| 494 |
+
|
| 495 |
+
|
| 496 |
+
except requests.exceptions.JSONDecodeError:
|
| 497 |
+
|
| 498 |
+
error_detail += (
|
| 499 |
+
f" Response: "
|
| 500 |
+
f"{e.response.text[:500]}"
|
| 501 |
+
)
|
| 502 |
+
|
| 503 |
+
|
| 504 |
+
status_message = (
|
| 505 |
+
f"Submission Failed: {error_detail}"
|
| 506 |
+
)
|
| 507 |
+
|
| 508 |
+
|
| 509 |
+
print(
|
| 510 |
+
status_message,
|
| 511 |
+
flush=True
|
| 512 |
+
)
|
| 513 |
+
|
| 514 |
+
|
| 515 |
+
results_df = pd.DataFrame(
|
| 516 |
+
results_log
|
| 517 |
+
)
|
| 518 |
+
|
| 519 |
+
|
| 520 |
+
return (
|
| 521 |
+
status_message,
|
| 522 |
+
results_df
|
| 523 |
+
)
|
| 524 |
+
|
| 525 |
+
|
| 526 |
+
except requests.exceptions.Timeout:
|
| 527 |
+
|
| 528 |
+
status_message = (
|
| 529 |
+
"Submission Failed: "
|
| 530 |
+
"The request timed out."
|
| 531 |
+
)
|
| 532 |
+
|
| 533 |
+
|
| 534 |
+
print(
|
| 535 |
+
status_message,
|
| 536 |
+
flush=True
|
| 537 |
+
)
|
| 538 |
+
|
| 539 |
+
|
| 540 |
+
results_df = pd.DataFrame(
|
| 541 |
+
results_log
|
| 542 |
+
)
|
| 543 |
+
|
| 544 |
+
|
| 545 |
+
return (
|
| 546 |
+
status_message,
|
| 547 |
+
results_df
|
| 548 |
+
)
|
| 549 |
+
|
| 550 |
+
|
| 551 |
+
except requests.exceptions.RequestException as e:
|
| 552 |
+
|
| 553 |
+
status_message = (
|
| 554 |
+
f"Submission Failed: "
|
| 555 |
+
f"Network error - {e}"
|
| 556 |
+
)
|
| 557 |
+
|
| 558 |
+
|
| 559 |
+
print(
|
| 560 |
+
status_message,
|
| 561 |
+
flush=True
|
| 562 |
+
)
|
| 563 |
+
|
| 564 |
+
|
| 565 |
+
results_df = pd.DataFrame(
|
| 566 |
+
results_log
|
| 567 |
+
)
|
| 568 |
+
|
| 569 |
+
|
| 570 |
+
return (
|
| 571 |
+
status_message,
|
| 572 |
+
results_df
|
| 573 |
+
)
|
| 574 |
|
|
|
|
| 575 |
|
| 576 |
except Exception as e:
|
| 577 |
|
| 578 |
+
status_message = (
|
| 579 |
+
f"An unexpected error occurred "
|
| 580 |
+
f"during submission: {e}"
|
| 581 |
+
)
|
| 582 |
+
|
| 583 |
+
|
| 584 |
print(
|
| 585 |
+
status_message,
|
|
|
|
| 586 |
flush=True
|
| 587 |
)
|
| 588 |
|
| 589 |
+
|
| 590 |
+
results_df = pd.DataFrame(
|
| 591 |
+
results_log
|
| 592 |
+
)
|
| 593 |
+
|
| 594 |
+
|
| 595 |
return (
|
| 596 |
+
status_message,
|
| 597 |
+
results_df
|
| 598 |
)
|
| 599 |
|
| 600 |
|
|
|
|
| 604 |
|
| 605 |
with gr.Blocks() as demo:
|
| 606 |
|
| 607 |
+
gr.Markdown(
|
| 608 |
+
"# Basic Agent Evaluation Runner"
|
| 609 |
+
)
|
| 610 |
+
|
| 611 |
gr.Markdown(
|
| 612 |
"""
|
| 613 |
+
**Instructions:**
|
| 614 |
+
|
| 615 |
+
1. Please clone this space, then modify the code to define your agent's logic, tools, and necessary packages.
|
| 616 |
+
2. Log in to your Hugging Face account using the button below.
|
| 617 |
+
3. Click **Run Evaluation & Submit All Answers** to fetch questions, run your agent, submit answers, and see the score.
|
| 618 |
|
| 619 |
+
---
|
| 620 |
|
| 621 |
+
**Current Agent Configuration**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 622 |
|
| 623 |
+
- Provider: Groq
|
| 624 |
+
- Model: `openai/gpt-oss-20b`
|
| 625 |
+
- Agent: `ToolCallingAgent`
|
| 626 |
+
- Tools: `WebSearchTool`, `VisitWebpageTool`
|
| 627 |
+
- Max Steps: 5
|
| 628 |
"""
|
| 629 |
)
|
| 630 |
|
|
|
|
|
|
|
|
|
|
| 631 |
|
| 632 |
+
gr.LoginButton()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 633 |
|
|
|
|
|
|
|
|
|
|
| 634 |
|
| 635 |
+
run_button = gr.Button(
|
| 636 |
+
"Run Evaluation & Submit All Answers"
|
| 637 |
)
|
| 638 |
|
|
|
|
|
|
|
|
|
|
| 639 |
|
| 640 |
+
status_output = gr.Textbox(
|
| 641 |
+
label="Run Status / Submission Result",
|
| 642 |
+
lines=5,
|
| 643 |
interactive=False
|
| 644 |
)
|
| 645 |
|
|
|
|
|
|
|
|
|
|
| 646 |
|
| 647 |
+
results_table = gr.DataFrame(
|
| 648 |
+
label="Questions and Agent Answers",
|
| 649 |
+
wrap=True
|
| 650 |
+
)
|
| 651 |
+
|
| 652 |
+
|
| 653 |
+
run_button.click(
|
| 654 |
+
fn=run_and_submit_all,
|
| 655 |
+
outputs=[
|
| 656 |
+
status_output,
|
| 657 |
+
results_table
|
| 658 |
+
]
|
| 659 |
)
|
| 660 |
|
| 661 |
|
|
|
|
| 667 |
|
| 668 |
print(
|
| 669 |
"\n"
|
| 670 |
+
+ "-" * 30
|
| 671 |
+
+ " App Starting "
|
| 672 |
+
+ "-" * 30,
|
| 673 |
flush=True
|
| 674 |
)
|
| 675 |
|
| 676 |
+
|
| 677 |
+
space_host_startup = os.getenv(
|
| 678 |
+
"SPACE_HOST"
|
| 679 |
)
|
| 680 |
|
| 681 |
+
space_id_startup = os.getenv(
|
| 682 |
+
"SPACE_ID"
|
|
|
|
| 683 |
)
|
| 684 |
|
|
|
|
|
|
|
|
|
|
| 685 |
|
| 686 |
+
if space_host_startup:
|
| 687 |
|
| 688 |
+
print(
|
| 689 |
+
f"✅ SPACE_HOST found: "
|
| 690 |
+
f"{space_host_startup}",
|
| 691 |
+
flush=True
|
| 692 |
+
)
|
| 693 |
|
| 694 |
print(
|
| 695 |
+
f"Runtime URL: "
|
| 696 |
+
f"https://{space_host_startup}",
|
| 697 |
flush=True
|
| 698 |
)
|
| 699 |
|
| 700 |
else:
|
| 701 |
|
| 702 |
print(
|
| 703 |
+
"ℹ️ SPACE_HOST environment "
|
| 704 |
+
"variable not found.",
|
| 705 |
flush=True
|
| 706 |
)
|
| 707 |
|
|
|
|
|
|
|
|
|
|
| 708 |
|
| 709 |
+
if space_id_startup:
|
| 710 |
|
| 711 |
print(
|
| 712 |
+
f"✅ SPACE_ID found: "
|
| 713 |
+
f"{space_id_startup}",
|
| 714 |
+
flush=True
|
| 715 |
+
)
|
| 716 |
+
|
| 717 |
+
print(
|
| 718 |
+
f"Repo URL: "
|
| 719 |
+
f"https://huggingface.co/spaces/"
|
| 720 |
+
f"{space_id_startup}",
|
| 721 |
+
flush=True
|
| 722 |
+
)
|
| 723 |
+
|
| 724 |
+
print(
|
| 725 |
+
f"Repo Tree URL: "
|
| 726 |
+
f"https://huggingface.co/spaces/"
|
| 727 |
+
f"{space_id_startup}/tree/main",
|
| 728 |
flush=True
|
| 729 |
)
|
| 730 |
|
| 731 |
else:
|
| 732 |
|
| 733 |
print(
|
| 734 |
+
"ℹ️ SPACE_ID environment "
|
| 735 |
+
"variable not found.",
|
| 736 |
flush=True
|
| 737 |
)
|
| 738 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 739 |
|
| 740 |
print(
|
| 741 |
+
"-" * 74 + "\n",
|
| 742 |
flush=True
|
| 743 |
)
|
| 744 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 745 |
|
| 746 |
print(
|
| 747 |
+
"Launching Gradio Interface "
|
| 748 |
+
"for Basic Agent Evaluation...",
|
| 749 |
flush=True
|
| 750 |
)
|
| 751 |
|
|
|
|
|
|
|
|
|
|
| 752 |
|
| 753 |
demo.launch(
|
| 754 |
debug=True,
|