Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,5 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
import requests
|
| 4 |
-
import pandas as pd
|
| 5 |
|
| 6 |
from smolagents import (
|
| 7 |
ToolCallingAgent,
|
|
@@ -10,18 +8,16 @@ from smolagents import (
|
|
| 10 |
VisitWebpageTool
|
| 11 |
)
|
| 12 |
|
| 13 |
-
# =========================================================
|
| 14 |
-
# CONSTANTS
|
| 15 |
-
# =========================================================
|
| 16 |
-
|
| 17 |
-
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 18 |
|
| 19 |
# =========================================================
|
| 20 |
-
# TEST
|
| 21 |
# =========================================================
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
|
| 27 |
# =========================================================
|
|
@@ -29,9 +25,26 @@ TEST_QUESTION_COUNT = 1
|
|
| 29 |
# =========================================================
|
| 30 |
|
| 31 |
class BasicAgent:
|
|
|
|
| 32 |
def __init__(self):
|
| 33 |
|
| 34 |
-
print("\n
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
# -------------------------------------------------
|
| 37 |
# MODEL
|
|
@@ -40,10 +53,13 @@ class BasicAgent:
|
|
| 40 |
self.model = OpenAIModel(
|
| 41 |
model_id="openai/gpt-oss-20b",
|
| 42 |
api_base="https://api.groq.com/openai/v1",
|
| 43 |
-
api_key=
|
| 44 |
)
|
| 45 |
|
| 46 |
-
print(
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
# -------------------------------------------------
|
| 49 |
# TOOLS
|
|
@@ -54,7 +70,15 @@ class BasicAgent:
|
|
| 54 |
VisitWebpageTool()
|
| 55 |
]
|
| 56 |
|
| 57 |
-
print(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
# -------------------------------------------------
|
| 60 |
# AGENT
|
|
@@ -67,653 +91,282 @@ class BasicAgent:
|
|
| 67 |
verbosity_level=2
|
| 68 |
)
|
| 69 |
|
| 70 |
-
print("[INIT] ToolCallingAgent initialized.", flush=True)
|
| 71 |
-
|
| 72 |
-
def __call__(self, question: str) -> str:
|
| 73 |
-
|
| 74 |
-
print("\n" + "=" * 60, flush=True)
|
| 75 |
-
print("AGENT START", flush=True)
|
| 76 |
-
print("=" * 60, flush=True)
|
| 77 |
-
|
| 78 |
-
print(f"[QUESTION]\n{question}", flush=True)
|
| 79 |
-
|
| 80 |
-
try:
|
| 81 |
-
answer = self.agent.run(question)
|
| 82 |
-
|
| 83 |
-
print("\n[ANSWER]", flush=True)
|
| 84 |
-
print(answer, flush=True)
|
| 85 |
-
|
| 86 |
-
print("=" * 60, flush=True)
|
| 87 |
-
print("AGENT END", flush=True)
|
| 88 |
-
print("=" * 60 + "\n", flush=True)
|
| 89 |
-
|
| 90 |
-
return str(answer)
|
| 91 |
-
|
| 92 |
-
except Exception as e:
|
| 93 |
-
|
| 94 |
-
print("\n[AGENT ERROR]", flush=True)
|
| 95 |
-
print(f"{type(e).__name__}: {e}", flush=True)
|
| 96 |
-
|
| 97 |
-
print("=" * 60, flush=True)
|
| 98 |
-
print("AGENT FAILED", flush=True)
|
| 99 |
-
print("=" * 60 + "\n", flush=True)
|
| 100 |
-
|
| 101 |
-
raise
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
# =========================================================
|
| 105 |
-
# MAIN EVALUATION FUNCTION
|
| 106 |
-
# =========================================================
|
| 107 |
-
|
| 108 |
-
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 109 |
-
|
| 110 |
-
"""
|
| 111 |
-
In TEST_MODE:
|
| 112 |
-
- Fetch all GAIA questions
|
| 113 |
-
- Run only TEST_QUESTION_COUNT questions
|
| 114 |
-
- Do NOT submit answers
|
| 115 |
-
|
| 116 |
-
In normal mode:
|
| 117 |
-
- Run all questions
|
| 118 |
-
- Submit answers
|
| 119 |
-
"""
|
| 120 |
-
|
| 121 |
-
print("\n" + "=" * 70, flush=True)
|
| 122 |
-
print("RUN STARTED", flush=True)
|
| 123 |
-
print("=" * 70, flush=True)
|
| 124 |
-
|
| 125 |
-
# -----------------------------------------------------
|
| 126 |
-
# CHECK LOGIN
|
| 127 |
-
# -----------------------------------------------------
|
| 128 |
-
|
| 129 |
-
if profile:
|
| 130 |
-
username = f"{profile.username}"
|
| 131 |
-
print(f"[LOGIN] User logged in: {username}", flush=True)
|
| 132 |
-
|
| 133 |
-
else:
|
| 134 |
-
print("[LOGIN] User not logged in.", flush=True)
|
| 135 |
-
|
| 136 |
-
return (
|
| 137 |
-
"Please Login to Hugging Face with the button.",
|
| 138 |
-
None
|
| 139 |
-
)
|
| 140 |
-
|
| 141 |
-
# -----------------------------------------------------
|
| 142 |
-
# SPACE INFORMATION
|
| 143 |
-
# -----------------------------------------------------
|
| 144 |
-
|
| 145 |
-
space_id = os.getenv("SPACE_ID")
|
| 146 |
-
|
| 147 |
-
print(f"[SPACE_ID] {space_id}", flush=True)
|
| 148 |
-
|
| 149 |
-
if space_id:
|
| 150 |
-
agent_code = (
|
| 151 |
-
f"https://huggingface.co/spaces/"
|
| 152 |
-
f"{space_id}/tree/main"
|
| 153 |
-
)
|
| 154 |
-
else:
|
| 155 |
-
agent_code = "UNKNOWN"
|
| 156 |
-
|
| 157 |
-
print(f"[AGENT CODE] {agent_code}", flush=True)
|
| 158 |
-
|
| 159 |
-
# -----------------------------------------------------
|
| 160 |
-
# API ENDPOINTS
|
| 161 |
-
# -----------------------------------------------------
|
| 162 |
-
|
| 163 |
-
api_url = DEFAULT_API_URL
|
| 164 |
-
|
| 165 |
-
questions_url = f"{api_url}/questions"
|
| 166 |
-
submit_url = f"{api_url}/submit"
|
| 167 |
-
|
| 168 |
-
print(f"[QUESTIONS API] {questions_url}", flush=True)
|
| 169 |
-
|
| 170 |
-
# -----------------------------------------------------
|
| 171 |
-
# INITIALIZE AGENT
|
| 172 |
-
# -----------------------------------------------------
|
| 173 |
-
|
| 174 |
-
try:
|
| 175 |
-
|
| 176 |
-
agent = BasicAgent()
|
| 177 |
-
|
| 178 |
-
except Exception as e:
|
| 179 |
-
|
| 180 |
print(
|
| 181 |
-
|
| 182 |
-
f"{type(e).__name__}: {e}",
|
| 183 |
flush=True
|
| 184 |
)
|
| 185 |
|
| 186 |
-
|
| 187 |
-
f"Error initializing agent: {e}",
|
| 188 |
-
None
|
| 189 |
-
)
|
| 190 |
-
|
| 191 |
-
# -----------------------------------------------------
|
| 192 |
-
# FETCH QUESTIONS
|
| 193 |
-
# -----------------------------------------------------
|
| 194 |
|
| 195 |
-
print("\n[API] Fetching questions...", flush=True)
|
| 196 |
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
response = requests.get(
|
| 200 |
-
questions_url,
|
| 201 |
-
timeout=15
|
| 202 |
-
)
|
| 203 |
-
|
| 204 |
-
response.raise_for_status()
|
| 205 |
-
|
| 206 |
-
questions_data = response.json()
|
| 207 |
-
|
| 208 |
-
if not questions_data:
|
| 209 |
-
|
| 210 |
-
print(
|
| 211 |
-
"[API] Fetched questions list is empty.",
|
| 212 |
-
flush=True
|
| 213 |
-
)
|
| 214 |
-
|
| 215 |
-
return (
|
| 216 |
-
"Fetched questions list is empty.",
|
| 217 |
-
None
|
| 218 |
-
)
|
| 219 |
-
|
| 220 |
-
print(
|
| 221 |
-
f"[API] Fetched "
|
| 222 |
-
f"{len(questions_data)} questions.",
|
| 223 |
-
flush=True
|
| 224 |
-
)
|
| 225 |
-
|
| 226 |
-
except requests.exceptions.RequestException as e:
|
| 227 |
-
|
| 228 |
-
print(
|
| 229 |
-
f"[API ERROR] {e}",
|
| 230 |
-
flush=True
|
| 231 |
-
)
|
| 232 |
-
|
| 233 |
-
return (
|
| 234 |
-
f"Error fetching questions: {e}",
|
| 235 |
-
None
|
| 236 |
-
)
|
| 237 |
-
|
| 238 |
-
except Exception as e:
|
| 239 |
-
|
| 240 |
-
print(
|
| 241 |
-
f"[API ERROR] "
|
| 242 |
-
f"{type(e).__name__}: {e}",
|
| 243 |
-
flush=True
|
| 244 |
-
)
|
| 245 |
-
|
| 246 |
-
return (
|
| 247 |
-
f"Unexpected error fetching questions: {e}",
|
| 248 |
-
None
|
| 249 |
-
)
|
| 250 |
-
|
| 251 |
-
# -----------------------------------------------------
|
| 252 |
-
# SELECT QUESTIONS
|
| 253 |
-
# -----------------------------------------------------
|
| 254 |
-
|
| 255 |
-
if TEST_MODE:
|
| 256 |
-
|
| 257 |
-
questions_to_run = (
|
| 258 |
-
questions_data[:TEST_QUESTION_COUNT]
|
| 259 |
-
)
|
| 260 |
|
| 261 |
print("\n" + "=" * 70, flush=True)
|
| 262 |
-
print("
|
| 263 |
print("=" * 70, flush=True)
|
| 264 |
|
| 265 |
print(
|
| 266 |
-
f"[
|
| 267 |
-
f"{len(questions_data)}",
|
| 268 |
-
flush=True
|
| 269 |
-
)
|
| 270 |
-
|
| 271 |
-
print(
|
| 272 |
-
f"[TEST] Questions to run: "
|
| 273 |
-
f"{len(questions_to_run)}",
|
| 274 |
-
flush=True
|
| 275 |
-
)
|
| 276 |
-
|
| 277 |
-
print(
|
| 278 |
-
"[TEST] Submission is DISABLED.",
|
| 279 |
-
flush=True
|
| 280 |
-
)
|
| 281 |
-
|
| 282 |
-
else:
|
| 283 |
-
|
| 284 |
-
questions_to_run = questions_data
|
| 285 |
-
|
| 286 |
-
print(
|
| 287 |
-
f"[NORMAL MODE] Running "
|
| 288 |
-
f"{len(questions_to_run)} questions.",
|
| 289 |
flush=True
|
| 290 |
)
|
| 291 |
|
| 292 |
-
|
| 293 |
-
# RUN AGENT
|
| 294 |
-
# -----------------------------------------------------
|
| 295 |
-
|
| 296 |
-
results_log = []
|
| 297 |
-
answers_payload = []
|
| 298 |
-
|
| 299 |
-
for index, item in enumerate(
|
| 300 |
-
questions_to_run,
|
| 301 |
-
start=1
|
| 302 |
-
):
|
| 303 |
|
| 304 |
-
|
| 305 |
-
question_text = item.get("question")
|
| 306 |
|
| 307 |
-
|
| 308 |
|
| 309 |
print(
|
| 310 |
-
|
| 311 |
-
f"{item}",
|
| 312 |
flush=True
|
| 313 |
)
|
| 314 |
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
print(
|
| 320 |
-
f"[TASK] "
|
| 321 |
-
f"{index}/{len(questions_to_run)}",
|
| 322 |
-
flush=True
|
| 323 |
-
)
|
| 324 |
-
|
| 325 |
-
print(
|
| 326 |
-
f"[TASK ID] {task_id}",
|
| 327 |
-
flush=True
|
| 328 |
-
)
|
| 329 |
|
| 330 |
-
|
| 331 |
-
f"[QUESTION]\n{question_text}",
|
| 332 |
-
flush=True
|
| 333 |
-
)
|
| 334 |
|
| 335 |
-
|
|
|
|
|
|
|
|
|
|
| 336 |
|
| 337 |
-
|
| 338 |
|
| 339 |
-
|
| 340 |
-
question_text
|
| 341 |
-
)
|
| 342 |
|
| 343 |
-
|
| 344 |
-
{
|
| 345 |
-
"task_id": task_id,
|
| 346 |
-
"submitted_answer": submitted_answer
|
| 347 |
-
}
|
| 348 |
-
)
|
| 349 |
|
| 350 |
-
|
| 351 |
-
{
|
| 352 |
-
"Task ID": task_id,
|
| 353 |
-
"Question": question_text,
|
| 354 |
-
"Submitted Answer": submitted_answer
|
| 355 |
-
}
|
| 356 |
-
)
|
| 357 |
|
| 358 |
print(
|
| 359 |
-
|
| 360 |
flush=True
|
| 361 |
)
|
| 362 |
|
| 363 |
-
except Exception as e:
|
| 364 |
-
|
| 365 |
print(
|
| 366 |
-
f"
|
| 367 |
-
f"{type(e).__name__}: {e}",
|
| 368 |
flush=True
|
| 369 |
)
|
| 370 |
|
| 371 |
-
|
| 372 |
-
{
|
| 373 |
-
|
| 374 |
-
"Question": question_text,
|
| 375 |
-
"Submitted Answer":
|
| 376 |
-
f"AGENT ERROR: {e}"
|
| 377 |
-
}
|
| 378 |
)
|
| 379 |
|
| 380 |
-
|
| 381 |
-
# CONVERT RESULTS TO DATAFRAME
|
| 382 |
-
# -----------------------------------------------------
|
| 383 |
-
|
| 384 |
-
results_df = pd.DataFrame(
|
| 385 |
-
results_log
|
| 386 |
-
)
|
| 387 |
-
|
| 388 |
-
# -----------------------------------------------------
|
| 389 |
-
# STOP HERE IN TEST MODE
|
| 390 |
-
# -----------------------------------------------------
|
| 391 |
-
|
| 392 |
-
if TEST_MODE:
|
| 393 |
-
|
| 394 |
-
print("\n" + "=" * 70, flush=True)
|
| 395 |
-
print("TEST FINISHED", flush=True)
|
| 396 |
-
print("=" * 70, flush=True)
|
| 397 |
-
|
| 398 |
-
print(
|
| 399 |
-
"[TEST] No answers submitted.",
|
| 400 |
-
flush=True
|
| 401 |
-
)
|
| 402 |
-
|
| 403 |
-
success_count = len(
|
| 404 |
-
answers_payload
|
| 405 |
-
)
|
| 406 |
-
|
| 407 |
-
failed_count = (
|
| 408 |
-
len(questions_to_run)
|
| 409 |
-
- success_count
|
| 410 |
-
)
|
| 411 |
-
|
| 412 |
-
status_message = (
|
| 413 |
-
"TEST COMPLETED\n\n"
|
| 414 |
-
f"Questions fetched: "
|
| 415 |
-
f"{len(questions_data)}\n"
|
| 416 |
-
f"Questions tested: "
|
| 417 |
-
f"{len(questions_to_run)}\n"
|
| 418 |
-
f"Successful runs: "
|
| 419 |
-
f"{success_count}\n"
|
| 420 |
-
f"Failed runs: "
|
| 421 |
-
f"{failed_count}\n\n"
|
| 422 |
-
"No answers were submitted.\n"
|
| 423 |
-
"Check Container Logs for details."
|
| 424 |
-
)
|
| 425 |
-
|
| 426 |
-
return (
|
| 427 |
-
status_message,
|
| 428 |
-
results_df
|
| 429 |
-
)
|
| 430 |
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
|
| 436 |
-
if not answers_payload:
|
| 437 |
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
)
|
| 442 |
|
| 443 |
-
|
| 444 |
-
"Agent did not produce any answers.",
|
| 445 |
-
results_df
|
| 446 |
-
)
|
| 447 |
|
| 448 |
-
#
|
| 449 |
-
|
| 450 |
-
#
|
| 451 |
|
| 452 |
-
|
| 453 |
-
"
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
}
|
| 457 |
|
| 458 |
print(
|
| 459 |
-
|
| 460 |
-
f"{len(answers_payload)} answers.",
|
| 461 |
flush=True
|
| 462 |
)
|
| 463 |
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
|
|
|
| 467 |
|
| 468 |
try:
|
| 469 |
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
flush=True
|
| 473 |
-
)
|
| 474 |
-
|
| 475 |
-
response = requests.post(
|
| 476 |
-
submit_url,
|
| 477 |
-
json=submission_data,
|
| 478 |
-
timeout=60
|
| 479 |
-
)
|
| 480 |
-
|
| 481 |
-
response.raise_for_status()
|
| 482 |
-
|
| 483 |
-
result_data = response.json()
|
| 484 |
-
|
| 485 |
-
final_status = (
|
| 486 |
-
"Submission Successful!\n\n"
|
| 487 |
-
f"User: "
|
| 488 |
-
f"{result_data.get('username')}\n"
|
| 489 |
-
f"Overall Score: "
|
| 490 |
-
f"{result_data.get('score', 'N/A')}%\n"
|
| 491 |
-
f"Correct: "
|
| 492 |
-
f"{result_data.get('correct_count', '?')}/"
|
| 493 |
-
f"{result_data.get('total_attempted', '?')}\n"
|
| 494 |
-
f"Message: "
|
| 495 |
-
f"{result_data.get('message', '')}"
|
| 496 |
-
)
|
| 497 |
-
|
| 498 |
-
print(
|
| 499 |
-
"[SUBMIT] Submission successful.",
|
| 500 |
-
flush=True
|
| 501 |
-
)
|
| 502 |
-
|
| 503 |
-
return (
|
| 504 |
-
final_status,
|
| 505 |
-
results_df
|
| 506 |
-
)
|
| 507 |
-
|
| 508 |
-
except requests.exceptions.HTTPError as e:
|
| 509 |
-
|
| 510 |
-
error_detail = (
|
| 511 |
-
f"Server responded with status "
|
| 512 |
-
f"{e.response.status_code}."
|
| 513 |
-
)
|
| 514 |
-
|
| 515 |
-
try:
|
| 516 |
-
|
| 517 |
-
error_json = e.response.json()
|
| 518 |
-
|
| 519 |
-
error_detail += (
|
| 520 |
-
f" Detail: "
|
| 521 |
-
f"{error_json.get('detail', e.response.text)}"
|
| 522 |
-
)
|
| 523 |
-
|
| 524 |
-
except Exception:
|
| 525 |
-
|
| 526 |
-
error_detail += (
|
| 527 |
-
f" Response: "
|
| 528 |
-
f"{e.response.text[:500]}"
|
| 529 |
-
)
|
| 530 |
-
|
| 531 |
-
print(
|
| 532 |
-
f"[SUBMIT ERROR] {error_detail}",
|
| 533 |
-
flush=True
|
| 534 |
-
)
|
| 535 |
-
|
| 536 |
-
return (
|
| 537 |
-
f"Submission Failed: {error_detail}",
|
| 538 |
-
results_df
|
| 539 |
-
)
|
| 540 |
-
|
| 541 |
-
except requests.exceptions.Timeout:
|
| 542 |
-
|
| 543 |
-
print(
|
| 544 |
-
"[SUBMIT ERROR] Request timed out.",
|
| 545 |
-
flush=True
|
| 546 |
-
)
|
| 547 |
-
|
| 548 |
-
return (
|
| 549 |
-
"Submission Failed: "
|
| 550 |
-
"The request timed out.",
|
| 551 |
-
results_df
|
| 552 |
-
)
|
| 553 |
|
| 554 |
-
|
|
|
|
| 555 |
|
| 556 |
-
print(
|
| 557 |
-
|
| 558 |
-
|
| 559 |
-
)
|
| 560 |
|
| 561 |
-
return
|
| 562 |
-
f"Submission Failed: "
|
| 563 |
-
f"Network error - {e}",
|
| 564 |
-
results_df
|
| 565 |
-
)
|
| 566 |
|
| 567 |
except Exception as e:
|
| 568 |
|
| 569 |
print(
|
| 570 |
-
f"[
|
| 571 |
f"{type(e).__name__}: {e}",
|
| 572 |
flush=True
|
| 573 |
)
|
| 574 |
|
| 575 |
return (
|
| 576 |
-
f"
|
| 577 |
-
|
| 578 |
)
|
| 579 |
|
| 580 |
|
| 581 |
# =========================================================
|
| 582 |
-
# GRADIO
|
| 583 |
# =========================================================
|
| 584 |
|
| 585 |
with gr.Blocks() as demo:
|
| 586 |
|
| 587 |
-
gr.Markdown(
|
| 588 |
-
"# Basic Agent Evaluation Runner"
|
| 589 |
-
)
|
| 590 |
-
|
| 591 |
gr.Markdown(
|
| 592 |
"""
|
| 593 |
-
#
|
| 594 |
-
|
| 595 |
-
|
| 596 |
-
|
| 597 |
-
-
|
| 598 |
-
-
|
| 599 |
-
|
| 600 |
-
|
| 601 |
-
-
|
| 602 |
-
-
|
| 603 |
-
-
|
| 604 |
-
-
|
|
|
|
|
|
|
|
|
|
| 605 |
"""
|
| 606 |
)
|
| 607 |
|
| 608 |
-
|
|
|
|
|
|
|
| 609 |
|
| 610 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 611 |
|
| 612 |
-
|
| 613 |
-
|
| 614 |
-
|
| 615 |
|
| 616 |
-
|
|
|
|
|
|
|
| 617 |
|
| 618 |
-
|
| 619 |
-
|
| 620 |
-
|
| 621 |
|
| 622 |
-
|
| 623 |
-
label="
|
| 624 |
lines=8,
|
| 625 |
interactive=False
|
| 626 |
)
|
| 627 |
|
| 628 |
-
|
| 629 |
-
|
| 630 |
-
|
| 631 |
-
)
|
| 632 |
|
| 633 |
-
|
| 634 |
-
fn=
|
| 635 |
-
|
| 636 |
-
|
| 637 |
-
results_table
|
| 638 |
-
]
|
| 639 |
)
|
| 640 |
|
| 641 |
|
| 642 |
# =========================================================
|
| 643 |
-
#
|
| 644 |
# =========================================================
|
| 645 |
|
| 646 |
if __name__ == "__main__":
|
| 647 |
|
| 648 |
print(
|
| 649 |
"\n"
|
| 650 |
-
+ "
|
| 651 |
-
+ " App Starting "
|
| 652 |
-
+ "-" * 30,
|
| 653 |
flush=True
|
| 654 |
)
|
| 655 |
|
| 656 |
-
|
| 657 |
-
"
|
|
|
|
| 658 |
)
|
| 659 |
|
| 660 |
-
|
| 661 |
-
"
|
|
|
|
| 662 |
)
|
| 663 |
|
| 664 |
-
|
|
|
|
|
|
|
| 665 |
|
| 666 |
-
|
| 667 |
-
|
| 668 |
-
|
| 669 |
-
flush=True
|
| 670 |
-
)
|
| 671 |
|
| 672 |
print(
|
| 673 |
-
f"
|
| 674 |
-
f"https://{space_host_startup}",
|
| 675 |
flush=True
|
| 676 |
)
|
| 677 |
|
| 678 |
else:
|
| 679 |
|
| 680 |
print(
|
| 681 |
-
"
|
| 682 |
flush=True
|
| 683 |
)
|
| 684 |
|
| 685 |
-
|
|
|
|
|
|
|
| 686 |
|
| 687 |
-
|
| 688 |
-
f"✅ SPACE_ID found: "
|
| 689 |
-
f"{space_id_startup}",
|
| 690 |
-
flush=True
|
| 691 |
-
)
|
| 692 |
|
| 693 |
print(
|
| 694 |
-
|
| 695 |
-
f"https://huggingface.co/spaces/"
|
| 696 |
-
f"{space_id_startup}",
|
| 697 |
flush=True
|
| 698 |
)
|
| 699 |
|
| 700 |
else:
|
| 701 |
|
| 702 |
print(
|
| 703 |
-
"
|
| 704 |
flush=True
|
| 705 |
)
|
| 706 |
|
| 707 |
print(
|
| 708 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 709 |
flush=True
|
| 710 |
)
|
| 711 |
|
| 712 |
print(
|
| 713 |
-
"
|
| 714 |
flush=True
|
| 715 |
)
|
| 716 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 717 |
demo.launch(
|
| 718 |
debug=True,
|
| 719 |
share=False
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
|
|
|
|
|
|
| 3 |
|
| 4 |
from smolagents import (
|
| 5 |
ToolCallingAgent,
|
|
|
|
| 8 |
VisitWebpageTool
|
| 9 |
)
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# =========================================================
|
| 13 |
+
# TEST QUESTION
|
| 14 |
# =========================================================
|
| 15 |
|
| 16 |
+
TEST_QUESTION = (
|
| 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 |
# =========================================================
|
|
|
|
| 25 |
# =========================================================
|
| 26 |
|
| 27 |
class BasicAgent:
|
| 28 |
+
|
| 29 |
def __init__(self):
|
| 30 |
|
| 31 |
+
print("\n" + "=" * 60, flush=True)
|
| 32 |
+
print("[INIT] Initializing BasicAgent...", flush=True)
|
| 33 |
+
print("=" * 60, flush=True)
|
| 34 |
+
|
| 35 |
+
# -------------------------------------------------
|
| 36 |
+
# CHECK GROQ API KEY
|
| 37 |
+
# -------------------------------------------------
|
| 38 |
+
|
| 39 |
+
groq_api_key = os.getenv("GROQ_API_KEY")
|
| 40 |
+
|
| 41 |
+
if not groq_api_key:
|
| 42 |
+
raise ValueError(
|
| 43 |
+
"GROQ_API_KEY was not found. "
|
| 44 |
+
"Please add it to Hugging Face Space Secrets."
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
print("[INIT] GROQ_API_KEY found.", flush=True)
|
| 48 |
|
| 49 |
# -------------------------------------------------
|
| 50 |
# MODEL
|
|
|
|
| 53 |
self.model = OpenAIModel(
|
| 54 |
model_id="openai/gpt-oss-20b",
|
| 55 |
api_base="https://api.groq.com/openai/v1",
|
| 56 |
+
api_key=groq_api_key
|
| 57 |
)
|
| 58 |
|
| 59 |
+
print(
|
| 60 |
+
"[INIT] Groq GPT-OSS-20B model initialized.",
|
| 61 |
+
flush=True
|
| 62 |
+
)
|
| 63 |
|
| 64 |
# -------------------------------------------------
|
| 65 |
# TOOLS
|
|
|
|
| 70 |
VisitWebpageTool()
|
| 71 |
]
|
| 72 |
|
| 73 |
+
print(
|
| 74 |
+
"[INIT] WebSearchTool initialized.",
|
| 75 |
+
flush=True
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
print(
|
| 79 |
+
"[INIT] VisitWebpageTool initialized.",
|
| 80 |
+
flush=True
|
| 81 |
+
)
|
| 82 |
|
| 83 |
# -------------------------------------------------
|
| 84 |
# AGENT
|
|
|
|
| 91 |
verbosity_level=2
|
| 92 |
)
|
| 93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
print(
|
| 95 |
+
"[INIT] ToolCallingAgent initialized.",
|
|
|
|
| 96 |
flush=True
|
| 97 |
)
|
| 98 |
|
| 99 |
+
print("=" * 60 + "\n", flush=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
|
|
|
| 101 |
|
| 102 |
+
def __call__(self, question: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
print("\n" + "=" * 70, flush=True)
|
| 105 |
+
print("AGENT START", flush=True)
|
| 106 |
print("=" * 70, flush=True)
|
| 107 |
|
| 108 |
print(
|
| 109 |
+
f"\n[QUESTION]\n{question}\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
flush=True
|
| 111 |
)
|
| 112 |
|
| 113 |
+
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
+
answer = self.agent.run(question)
|
|
|
|
| 116 |
|
| 117 |
+
print("\n" + "-" * 70, flush=True)
|
| 118 |
|
| 119 |
print(
|
| 120 |
+
"[FINAL ANSWER]",
|
|
|
|
| 121 |
flush=True
|
| 122 |
)
|
| 123 |
|
| 124 |
+
print(
|
| 125 |
+
answer,
|
| 126 |
+
flush=True
|
| 127 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
+
print("-" * 70, flush=True)
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
+
print(
|
| 132 |
+
"AGENT FINISHED SUCCESSFULLY",
|
| 133 |
+
flush=True
|
| 134 |
+
)
|
| 135 |
|
| 136 |
+
print("=" * 70 + "\n", flush=True)
|
| 137 |
|
| 138 |
+
return str(answer)
|
|
|
|
|
|
|
| 139 |
|
| 140 |
+
except Exception as e:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
|
| 142 |
+
print("\n" + "!" * 70, flush=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
|
| 144 |
print(
|
| 145 |
+
"[AGENT ERROR]",
|
| 146 |
flush=True
|
| 147 |
)
|
| 148 |
|
|
|
|
|
|
|
| 149 |
print(
|
| 150 |
+
f"Type: {type(e).__name__}",
|
|
|
|
| 151 |
flush=True
|
| 152 |
)
|
| 153 |
|
| 154 |
+
print(
|
| 155 |
+
f"Message: {e}",
|
| 156 |
+
flush=True
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
)
|
| 158 |
|
| 159 |
+
print("!" * 70 + "\n", flush=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
|
| 161 |
+
return (
|
| 162 |
+
f"AGENT ERROR\n\n"
|
| 163 |
+
f"{type(e).__name__}: {e}"
|
| 164 |
+
)
|
| 165 |
|
|
|
|
| 166 |
|
| 167 |
+
# =========================================================
|
| 168 |
+
# TEST FUNCTION
|
| 169 |
+
# =========================================================
|
|
|
|
| 170 |
|
| 171 |
+
def test_agent():
|
|
|
|
|
|
|
|
|
|
| 172 |
|
| 173 |
+
print("\n" + "#" * 70, flush=True)
|
| 174 |
+
print("STARTING SINGLE QUESTION TEST", flush=True)
|
| 175 |
+
print("#" * 70, flush=True)
|
| 176 |
|
| 177 |
+
print(
|
| 178 |
+
"[TEST] No /questions API will be called.",
|
| 179 |
+
flush=True
|
| 180 |
+
)
|
|
|
|
| 181 |
|
| 182 |
print(
|
| 183 |
+
"[TEST] No /submit API will be called.",
|
|
|
|
| 184 |
flush=True
|
| 185 |
)
|
| 186 |
|
| 187 |
+
print(
|
| 188 |
+
f"\n[TEST QUESTION]\n{TEST_QUESTION}\n",
|
| 189 |
+
flush=True
|
| 190 |
+
)
|
| 191 |
|
| 192 |
try:
|
| 193 |
|
| 194 |
+
# Create agent
|
| 195 |
+
agent = BasicAgent()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
|
| 197 |
+
# Run exactly ONE question
|
| 198 |
+
answer = agent(TEST_QUESTION)
|
| 199 |
|
| 200 |
+
print("\n" + "#" * 70, flush=True)
|
| 201 |
+
print("TEST COMPLETED", flush=True)
|
| 202 |
+
print("#" * 70, flush=True)
|
|
|
|
| 203 |
|
| 204 |
+
return answer
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
|
| 206 |
except Exception as e:
|
| 207 |
|
| 208 |
print(
|
| 209 |
+
f"[TEST ERROR] "
|
| 210 |
f"{type(e).__name__}: {e}",
|
| 211 |
flush=True
|
| 212 |
)
|
| 213 |
|
| 214 |
return (
|
| 215 |
+
f"TEST ERROR\n\n"
|
| 216 |
+
f"{type(e).__name__}: {e}"
|
| 217 |
)
|
| 218 |
|
| 219 |
|
| 220 |
# =========================================================
|
| 221 |
+
# GRADIO INTERFACE
|
| 222 |
# =========================================================
|
| 223 |
|
| 224 |
with gr.Blocks() as demo:
|
| 225 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 226 |
gr.Markdown(
|
| 227 |
"""
|
| 228 |
+
# GAIA Agent - Single Question Test
|
| 229 |
+
|
| 230 |
+
### Configuration
|
| 231 |
+
|
| 232 |
+
- **Model:** `openai/gpt-oss-20b`
|
| 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 |
+
This page is only for testing the agent.
|
| 243 |
"""
|
| 244 |
)
|
| 245 |
|
| 246 |
+
# -----------------------------------------------------
|
| 247 |
+
# SHOW TEST QUESTION
|
| 248 |
+
# -----------------------------------------------------
|
| 249 |
|
| 250 |
+
question_box = gr.Textbox(
|
| 251 |
+
label="Test Question",
|
| 252 |
+
value=TEST_QUESTION,
|
| 253 |
+
lines=4,
|
| 254 |
+
interactive=False
|
| 255 |
+
)
|
| 256 |
|
| 257 |
+
# -----------------------------------------------------
|
| 258 |
+
# TEST BUTTON
|
| 259 |
+
# -----------------------------------------------------
|
| 260 |
|
| 261 |
+
test_button = gr.Button(
|
| 262 |
+
"Test Agent"
|
| 263 |
+
)
|
| 264 |
|
| 265 |
+
# -----------------------------------------------------
|
| 266 |
+
# OUTPUT
|
| 267 |
+
# -----------------------------------------------------
|
| 268 |
|
| 269 |
+
answer_box = gr.Textbox(
|
| 270 |
+
label="Agent Answer",
|
| 271 |
lines=8,
|
| 272 |
interactive=False
|
| 273 |
)
|
| 274 |
|
| 275 |
+
# -----------------------------------------------------
|
| 276 |
+
# BUTTON ACTION
|
| 277 |
+
# -----------------------------------------------------
|
|
|
|
| 278 |
|
| 279 |
+
test_button.click(
|
| 280 |
+
fn=test_agent,
|
| 281 |
+
inputs=[],
|
| 282 |
+
outputs=answer_box
|
|
|
|
|
|
|
| 283 |
)
|
| 284 |
|
| 285 |
|
| 286 |
# =========================================================
|
| 287 |
+
# START APPLICATION
|
| 288 |
# =========================================================
|
| 289 |
|
| 290 |
if __name__ == "__main__":
|
| 291 |
|
| 292 |
print(
|
| 293 |
"\n"
|
| 294 |
+
+ "=" * 70,
|
|
|
|
|
|
|
| 295 |
flush=True
|
| 296 |
)
|
| 297 |
|
| 298 |
+
print(
|
| 299 |
+
"GAIA SINGLE QUESTION TEST",
|
| 300 |
+
flush=True
|
| 301 |
)
|
| 302 |
|
| 303 |
+
print(
|
| 304 |
+
"=" * 70,
|
| 305 |
+
flush=True
|
| 306 |
)
|
| 307 |
|
| 308 |
+
# -----------------------------------------------------
|
| 309 |
+
# CHECK ENVIRONMENT
|
| 310 |
+
# -----------------------------------------------------
|
| 311 |
|
| 312 |
+
space_id = os.getenv("SPACE_ID")
|
| 313 |
+
|
| 314 |
+
if space_id:
|
|
|
|
|
|
|
| 315 |
|
| 316 |
print(
|
| 317 |
+
f"[SPACE] {space_id}",
|
|
|
|
| 318 |
flush=True
|
| 319 |
)
|
| 320 |
|
| 321 |
else:
|
| 322 |
|
| 323 |
print(
|
| 324 |
+
"[SPACE] SPACE_ID not found.",
|
| 325 |
flush=True
|
| 326 |
)
|
| 327 |
|
| 328 |
+
# -----------------------------------------------------
|
| 329 |
+
# CHECK API KEY
|
| 330 |
+
# -----------------------------------------------------
|
| 331 |
|
| 332 |
+
if os.getenv("GROQ_API_KEY"):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 333 |
|
| 334 |
print(
|
| 335 |
+
"[GROQ] GROQ_API_KEY found.",
|
|
|
|
|
|
|
| 336 |
flush=True
|
| 337 |
)
|
| 338 |
|
| 339 |
else:
|
| 340 |
|
| 341 |
print(
|
| 342 |
+
"[GROQ] WARNING: GROQ_API_KEY not found!",
|
| 343 |
flush=True
|
| 344 |
)
|
| 345 |
|
| 346 |
print(
|
| 347 |
+
"\n[MODE] SINGLE QUESTION TEST",
|
| 348 |
+
flush=True
|
| 349 |
+
)
|
| 350 |
+
|
| 351 |
+
print(
|
| 352 |
+
"[MODE] /questions disabled",
|
| 353 |
+
flush=True
|
| 354 |
+
)
|
| 355 |
+
|
| 356 |
+
print(
|
| 357 |
+
"[MODE] /submit disabled",
|
| 358 |
flush=True
|
| 359 |
)
|
| 360 |
|
| 361 |
print(
|
| 362 |
+
"=" * 70 + "\n",
|
| 363 |
flush=True
|
| 364 |
)
|
| 365 |
|
| 366 |
+
# -----------------------------------------------------
|
| 367 |
+
# LAUNCH
|
| 368 |
+
# -----------------------------------------------------
|
| 369 |
+
|
| 370 |
demo.launch(
|
| 371 |
debug=True,
|
| 372 |
share=False
|