File size: 27,440 Bytes
10e9b7d
 
eccf8e4
3c4371f
9ed74de
 
 
 
 
 
 
 
6213116
 
 
00a50e0
9ed74de
87a780b
9ed74de
10e9b7d
e80aab9
9ed74de
 
 
 
 
 
ad49360
9ed74de
 
 
 
 
 
 
 
 
 
 
 
ad49360
9ed74de
 
 
 
 
ad49360
9ed74de
 
 
 
 
 
 
00a50e0
4906175
9ed74de
 
 
 
 
 
4906175
9ed74de
 
 
 
6213116
9ed74de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4906175
 
9ed74de
 
 
4906175
9ed74de
00a50e0
9ed74de
 
 
 
 
6213116
 
 
 
 
 
 
 
 
9ed74de
4906175
 
9ed74de
 
4906175
9ed74de
 
 
 
6213116
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9ed74de
 
 
 
 
 
 
 
 
 
 
 
ad49360
6213116
 
 
 
 
9ed74de
 
ad49360
9ed74de
 
ad49360
9ed74de
 
 
 
 
 
 
 
 
 
 
 
 
 
6213116
 
 
 
 
 
 
 
 
9ed74de
6213116
 
 
 
 
9ed74de
 
6213116
9ed74de
4906175
6213116
9ed74de
 
6213116
9ed74de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6213116
9ed74de
 
6213116
9ed74de
 
 
 
 
 
 
 
 
 
 
6213116
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9ed74de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6213116
 
 
 
9ed74de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6213116
 
 
 
 
87a780b
9ed74de
6213116
4906175
9ed74de
 
 
 
 
6213116
 
9ed74de
 
 
 
 
 
 
 
 
 
 
6213116
9ed74de
4906175
32c95ee
 
6213116
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32c95ee
9ed74de
 
 
 
 
 
 
 
3c4371f
7e4a06b
9ed74de
3c4371f
7e4a06b
3c4371f
7d65c66
3c4371f
7e4a06b
31243f4
 
e80aab9
9ed74de
31243f4
9ed74de
31243f4
3c4371f
31243f4
4906175
9ed74de
36ed51a
c1fd3d2
3c4371f
9ed74de
31243f4
eccf8e4
31243f4
7d65c66
31243f4
 
9ed74de
 
31243f4
e80aab9
9ed74de
31243f4
9ed74de
 
 
 
7d65c66
9ed74de
31243f4
e80aab9
9ed74de
7d65c66
 
3c4371f
31243f4
 
9ed74de
 
 
 
 
31243f4
 
9ed74de
 
00a50e0
9ed74de
 
 
 
 
 
 
31243f4
6213116
9ed74de
7d65c66
 
31243f4
9ed74de
 
31243f4
 
9ed74de
31243f4
 
9ed74de
7d65c66
9ed74de
 
 
 
31243f4
e80aab9
7d65c66
e80aab9
 
31243f4
e80aab9
 
3c4371f
 
 
e80aab9
 
9ed74de
 
e80aab9
3c4371f
e80aab9
 
3c4371f
9ed74de
7d65c66
9ed74de
 
 
 
3c4371f
9ed74de
 
 
 
e80aab9
9ed74de
 
 
 
7d65c66
9ed74de
 
 
 
e80aab9
 
9ed74de
e80aab9
31243f4
0ee0419
e514fd7
 
 
81917a3
e514fd7
 
 
 
 
 
9ed74de
e514fd7
e80aab9
 
7e4a06b
e80aab9
31243f4
9ed74de
9088b99
9ed74de
7d65c66
e80aab9
31243f4
 
 
e80aab9
 
 
9ed74de
 
3c4371f
9ed74de
7d65c66
3c4371f
 
7d65c66
3c4371f
7d65c66
 
9ed74de
7d65c66
 
 
 
9ed74de
 
 
3c4371f
31243f4
9ed74de
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
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
624
625
626
627
628
629
630
import os
import gradio as gr
import requests
import pandas as pd
import math
import statistics
import ast
import pathlib
import io
import tempfile
import base64
import urllib.request
import time
import re
import json

from huggingface_hub import InferenceClient
from smolagents import CodeAgent, InferenceClientModel, tool
from smolagents import DuckDuckGoSearchTool, VisitWebpageTool


# --- Custom tool: safe arithmetic calculator ---
@tool
def calculator(expression: str) -> str:
    """
    Evaluate a safe arithmetic or mathematical expression.
    Use this for numeric computations: arithmetic, trig, sqrt, logarithms, etc.

    Args:
        expression: A Python-style math expression, e.g. "sqrt(144) + 2**10" or "mean([3,5,7])"
    """
    _ALLOWED_NODES = {
        ast.Expression, ast.BinOp, ast.UnaryOp, ast.Num, ast.Constant,
        ast.Add, ast.Sub, ast.Mult, ast.Div, ast.Pow, ast.Mod, ast.USub, ast.UAdd,
        ast.FloorDiv, ast.Load, ast.Compare, ast.Eq, ast.NotEq, ast.Lt, ast.LtE, ast.Gt, ast.GtE,
        ast.Call, ast.Name, ast.Tuple, ast.List,
    }
    _math_funcs = {k: getattr(math, k) for k in dir(math) if not k.startswith("_")}
    _math_funcs.update({"mean": statistics.mean, "median": statistics.median,
                        "sum": sum, "min": min, "max": max, "round": round, "abs": abs})

    def _check(n):
        if type(n) not in _ALLOWED_NODES:
            raise ValueError(f"Disallowed expression: {type(n).__name__}")
        for child in ast.iter_child_nodes(n):
            _check(child)

    try:
        node = ast.parse(expression, mode="eval")
        _check(node)
        val = eval(compile(node, "<calc>", "eval"), {"__builtins__": {}}, _math_funcs)
        return str(val)
    except Exception as e:
        return f"ERROR: calculator failed: {e}"


# --- Multimodal tool: image OCR via FireRed-OCR ---
@tool
def ocr_image(image_source: str) -> str:
    """
    Extract all text visible in an image using FireRed-OCR (a VLM-based OCR model).
    Accepts an HTTP/HTTPS image URL or a local file path.

    Args:
        image_source: HTTP URL or absolute local file path of the image to process.
    """
    try:
        client = InferenceClient("FireRedTeam/FireRed-OCR", token=os.getenv("HF_TOKEN") or os.getenv("HF_API_TOKEN"))
        if image_source.startswith("http"):
            image_content = {"type": "image_url", "image_url": {"url": image_source}}
        else:
            with open(image_source, "rb") as f:
                b64 = base64.b64encode(f.read()).decode()
            ext = pathlib.Path(image_source).suffix.lstrip(".") or "png"
            image_content = {
                "type": "image_url",
                "image_url": {"url": f"data:image/{ext};base64,{b64}"},
            }
        messages = [{
            "role": "user",
            "content": [
                image_content,
                {"type": "text", "text": "Extract and return ALL text visible in this image. Output only the extracted text, and a full description of the image."},
            ],
        }]
        resp = client.chat_completion(messages=messages, max_tokens=1024)
        return resp.choices[0].message.content.strip() or "(no text detected)"
    except Exception as e:
        return f"ERROR: ocr_image failed: {e}"


# --- Multimodal tool: video understanding via LLaVA-Video-7B-Qwen2 ---
@tool
def analyze_video(video_url: str, question: str = "Describe this video in detail.") -> str:
    """
    Analyze a video and answer a question about it using LLaVA-Video-7B-Qwen2.

    Args:
        video_url: Direct HTTP/HTTPS URL to the video file (mp4, avi, webm, mov, etc.).
        question: The question to ask about the video content.
    """
    try:
        client = InferenceClient("lmms-lab/LLaVA-Video-7B-Qwen2", token=os.getenv("HF_TOKEN") or os.getenv("HF_API_TOKEN"))
        # LLaVA-Video does not accept YouTube watch URLs β€” only direct video file URLs.
        # If caller passed a YouTube URL, surface a clear error so the agent can fall back.
        if "youtube.com/watch" in video_url or "youtu.be/" in video_url:
            return (
                "ERROR: analyze_video does not support YouTube watch URLs. "
                "Call get_youtube_transcript(url) instead to get the spoken content, "
                "or use DuckDuckGoSearchTool to search for information about the video."
            )
        messages = [{
            "role": "user",
            "content": [
                {"type": "video_url", "video_url": {"url": video_url}},
                {"type": "text", "text": question},
            ],
        }]
        resp = client.chat_completion(messages=messages, max_tokens=768)
        return resp.choices[0].message.content.strip()
    except Exception as e:
        return f"ERROR: analyze_video failed: {e if e else 'model returned empty response'}"


# --- YouTube transcript via youtube-transcript-api ---
@tool
def get_youtube_transcript(url: str) -> str:
    """
    Retrieve the spoken transcript of a YouTube video.
    Works with standard youtube.com/watch?v=... and youtu.be/... URLs.
    Use this for any question about what is said or shown in a YouTube video.

    Args:
        url: Full YouTube video URL, e.g. 'https://www.youtube.com/watch?v=abcd1234'
    """
    try:
        from youtube_transcript_api import YouTubeTranscriptApi
        # Extract video ID
        match = re.search(r"(?:v=|youtu\.be/)([A-Za-z0-9_-]{11})", url)
        if not match:
            return f"ERROR: could not extract YouTube video ID from URL: {url}"
        video_id = match.group(1)
        # Try to get English transcript first, fall back to any available
        try:
            transcript_list = YouTubeTranscriptApi.get_transcript(video_id, languages=["en", "en-US", "en-GB"])
        except Exception:
            # Fetch whatever language is available
            transcripts = YouTubeTranscriptApi.list_transcripts(video_id)
            transcript_list = transcripts.find_transcript(
                [t.language_code for t in transcripts]
            ).fetch()
        text = " ".join(entry["text"] for entry in transcript_list)
        return text[:8000] if len(text) > 8000 else text
    except Exception as e:
        return f"ERROR: get_youtube_transcript failed: {e}"


# --- Audio transcription via Whisper ---
@tool
def transcribe_audio(audio_source: str) -> str:
    """
    Transcribe speech in an audio file to text using openai/whisper-large-v3.
    Accepts an HTTP/HTTPS URL or a local file path.

    Args:
        audio_source: HTTP URL or local path to an audio file (mp3, wav, flac, ogg, m4a).
    """
    try:
        client = InferenceClient(
            "openai/whisper-large-v3",
            token=os.getenv("HF_TOKEN") or os.getenv("HF_API_TOKEN"),
            provider="hf-inference",  # avoids paid fal-ai routing
        )
        result = client.automatic_speech_recognition(audio_source)
        return result.text if hasattr(result, "text") else str(result)
    except Exception as e:
        return f"ERROR: transcribe_audio failed: {e}"


# --- File interpretation: PDF, CSV, Excel, text, image, audio, video ---
@tool
def read_task_file(task_id: str, file_name: str, file_path: str = "") -> str:
    """
    Download and parse the file attached to a GAIA task question.
    Automatically handles: PDF (text extraction), CSV/Excel (table as text),
    plain text/JSON/HTML, images (OCR), audio (transcription), video (analysis).

    Args:
        task_id: The GAIA task ID whose attached file should be read.
        file_name: The original file name including extension (e.g. 'data.csv', 'chart.png').
        file_path: Optional relative file path from the task metadata (e.g. '2023/test/uuid.jpg').
                   When provided this is tried first as the download URL.
    """
    hf_token = os.getenv("HF_TOKEN") or os.getenv("HF_API_TOKEN")
    hf_headers = {"User-Agent": "HF-AgentsCourse/1.0"}
    if hf_token:
        hf_headers["Authorization"] = f"Bearer {hf_token}"

    # Build candidate URLs in priority order:
    # 1. HuggingFace dataset repo (actual storage location, needs auth)
    # 2. GAIA scoring API /files/{task_id} (fallback)
    candidates = []
    if file_path:
        candidates.append(
            f"https://huggingface.co/datasets/gaia-benchmark/GAIA/resolve/main/{file_path}"
        )
    candidates.append(f"https://agents-course-unit4-scoring.hf.space/files/{task_id}")

    data = None
    last_err = ""
    successful_url = candidates[0]  # default
    for url in candidates:
        try:
            req = urllib.request.Request(url, headers=hf_headers)
            with urllib.request.urlopen(req, timeout=30) as resp:
                data = resp.read()
            successful_url = url
            break  # success
        except Exception as e:
            last_err = str(e)
    if data is None:
        return f"ERROR: could not download file for task '{task_id}': {last_err}"

    ext = pathlib.Path(file_name).suffix.lower()
    try:
        if ext == ".pdf":
            import pypdf
            reader = pypdf.PdfReader(io.BytesIO(data))
            pages = [p.extract_text() or "" for p in reader.pages]
            text = "\n\n--- Page Break ---\n\n".join(pages).strip()
            return text[:8000] if text else "(no text extracted from PDF)"

        elif ext == ".csv":
            df = pd.read_csv(io.BytesIO(data))
            return df.to_string(max_rows=200, index=False)

        elif ext in (".xlsx", ".xls"):
            df = pd.read_excel(io.BytesIO(data))
            return df.to_string(max_rows=200, index=False)

        elif ext in (".txt", ".md", ".json", ".xml", ".html", ".htm", ".py", ".tsv"):
            return data.decode("utf-8", errors="replace")[:8000]

        elif ext in (".jpg", ".jpeg", ".png", ".gif", ".webp", ".bmp", ".tiff"):
            suffix = ext or ".png"
            with tempfile.NamedTemporaryFile(suffix=suffix, delete=False) as tmp:
                tmp.write(data)
                tmp_path = tmp.name
            try:
                return ocr_image(tmp_path)
            finally:
                os.unlink(tmp_path)

        elif ext in (".mp3", ".wav", ".flac", ".ogg", ".m4a"):
            return transcribe_audio(successful_url)

        elif ext in (".mp4", ".avi", ".mov", ".mkv", ".webm"):
            return analyze_video(successful_url)

        else:
            # Try decoding as UTF-8 text, fall back to size info
            try:
                return data.decode("utf-8", errors="replace")[:4000]
            except Exception:
                return f"[binary file, {len(data)} bytes, extension='{ext}']"
    except Exception as e:
        return f"ERROR: read_task_file parsing failed (ext='{ext}'): {e}"


# --- Wikipedia search via official API (avoids 403 from VisitWebpageTool) ---
@tool
def wikipedia_search(query: str, sentences: int = 10) -> str:
    """
    Search Wikipedia and return a plain-text summary of the most relevant article.
    Preferred over VisitWebpageTool for Wikipedia questions β€” never returns 403.
    For full article sections (e.g. discography, revisions history), call with a
    precise article title and use the 'sections' parameter via VisitWebpageTool as
    fallback if you need more than the summary.

    Args:
        query: Search term or exact Wikipedia article title.
        sentences: How many sentences of summary to return (default 10).
    """
    try:
        # Step 1: search for the best matching title
        search_url = (
            "https://en.wikipedia.org/w/api.php"
            f"?action=opensearch&search={urllib.request.quote(query)}&limit=1&format=json"
        )
        req = urllib.request.Request(search_url, headers={"User-Agent": "HF-AgentsCourse/1.0 (research bot)"})
        with urllib.request.urlopen(req, timeout=15) as r:
            results = json.loads(r.read())
        if not results[1]:
            return f"No Wikipedia article found for query: '{query}'"
        title = results[1][0]

        # Step 2: fetch plain-text extract
        extract_url = (
            "https://en.wikipedia.org/w/api.php"
            f"?action=query&titles={urllib.request.quote(title)}"
            f"&prop=extracts&explaintext=true&exsentences={sentences}&format=json"
        )
        req2 = urllib.request.Request(extract_url, headers={"User-Agent": "HF-AgentsCourse/1.0 (research bot)"})
        with urllib.request.urlopen(req2, timeout=15) as r2:
            data = json.loads(r2.read())
        pages = data.get("query", {}).get("pages", {})
        page = next(iter(pages.values()))
        extract = page.get("extract", "").strip()
        return extract[:6000] if extract else f"No content found for article: '{title}'"
    except Exception as e:
        return f"ERROR: wikipedia_search failed: {e}"


# --- Constants ---
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"


# ReAct-style instructions appended to each task.  CodeAgent implements the
# Thought β†’ Code β†’ Observation β†’ … β†’ final_answer() ReAct loop natively.
# The final_answer() value must follow the GAIA submission format below.
REACT_INSTRUCTIONS = (
    "\n\nYou are a general AI assistant. I will ask you a question. "
    "Report your thoughts, and finish your answer with the following template: "
    "FINAL ANSWER: [YOUR FINAL ANSWER].\n"
    "YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma "
    "separated list of numbers and/or strings.\n"
    "If you are asked for a number, don't use comma to write your number neither use "
    "units such as $ or percent sign unless specified otherwise.\n"
    "If you are asked for a string, don't use articles, neither abbreviations "
    "(e.g. for cities), and write the digits in plain text unless specified otherwise.\n"
    "If you are asked for a comma separated list, apply the above rules depending of "
    "whether the element to be put in the list is a number or a string.\n\n"
    "Additional execution rules:\n"
    "- Reason step-by-step in code comments before calling tools.\n"
    "- Use DuckDuckGoSearchTool / VisitWebpageTool to look up facts.\n"
    "- Use calculator for any arithmetic; never compute in your head.\n"
    "- If the question mentions an attached file, call read_task_file first.\n"
    "- For images call ocr_image, for audio call transcribe_audio, "
    "for video call analyze_video.\n"
    "- For YouTube video questions, call get_youtube_transcript(url) β€” "
    "analyze_video does NOT work with YouTube URLs.\n"
    "- For Wikipedia questions, prefer wikipedia_search over VisitWebpageTool "
    "(it uses the API and never gets 403).\n"
    "- When you are confident, call final_answer() with ONLY the bare answer value "
    "(no 'FINAL ANSWER:' prefix β€” the prefix is for your reasoning trace only)."
)


def _extract_final_answer(raw: str) -> str:
    """
    Pull the answer out of the agent's output.
    Handles both:
      - CodeAgent returning a plain string from final_answer()
      - A string containing 'FINAL ANSWER: ...' anywhere in it
    """
    if not isinstance(raw, str):
        raw = str(raw)
    # Look for the canonical submission marker
    marker = "FINAL ANSWER:"
    idx = raw.upper().rfind(marker)  # rfind β†’ take the last occurrence
    if idx != -1:
        answer = raw[idx + len(marker):].strip()
        # Strip trailing punctuation that may have been added
        answer = answer.rstrip(".")
        return answer
    # No marker found β€” the CodeAgent returned the bare value directly
    return raw.strip()


def build_agent() -> CodeAgent:
    """
    Build a ReAct CodeAgent (Thought β†’ Code β†’ Observation loop) powered by
    Qwen2.5-72B-Instruct with the following tools:
      - DuckDuckGoSearchTool   : web search
      - VisitWebpageTool       : fetch and read a web page
      - calculator             : safe AST-based arithmetic / math
      - ocr_image              : image text extraction (FireRedTeam/FireRed-OCR)
      - analyze_video          : video understanding (lmms-lab/LLaVA-Video-7B-Qwen2)
      - transcribe_audio       : speech-to-text (openai/whisper-large-v3)
      - read_task_file         : download & parse task attachments
                                 (PDF, CSV, Excel, text, image, audio, video)
    """
    # HF Spaces exposes the token as HF_TOKEN; fall back to HF_API_TOKEN for
    # HF Spaces exposes the token as HF_TOKEN; fall back to HF_API_TOKEN for
    # local / custom secret names.  provider="novita" is required because
    # Qwen2.5-72B-Instruct is not hosted on hf-inference (causes 404).
    hf_token = os.getenv("HF_TOKEN") or os.getenv("HF_API_TOKEN")
    model = InferenceClientModel(
        model_id="Qwen/Qwen2.5-72B-Instruct",
        token=hf_token,
    )
    return CodeAgent(
        tools=[
            DuckDuckGoSearchTool(max_results=5),
            VisitWebpageTool(),
            calculator,
            wikipedia_search,
            get_youtube_transcript,
            ocr_image,
            analyze_video,
            transcribe_audio,
            read_task_file,
        ],
        model=model,
        max_steps=10,
        additional_authorized_imports=[
            "math", "statistics", "json", "re",
            "datetime", "collections", "itertools",
            "pandas", "io", "base64", "pathlib",
            "urllib", "urllib.request", "urllib.parse",
        ],
    )


def _run_with_retry(agent: CodeAgent, task_input: str, max_retries: int = 2) -> str:
    """
    Run the agent with automatic retry on transient server errors (504, 503, 502).
    Returns the raw answer string, or raises on non-transient errors.
    """
    transient_codes = ("504", "503", "502", "timeout", "Timeout", "timed out")
    for attempt in range(max_retries + 1):
        try:
            result = agent.run(task_input)
            # Guard against None / truly empty results
            if result is None or (isinstance(result, str) and not result.strip()):
                return "I could not determine the answer."
            return result
        except Exception as e:
            err = str(e)
            is_transient = any(code in err for code in transient_codes)
            if is_transient and attempt < max_retries:
                wait = 15 * (attempt + 1)  # 15 s, 30 s
                print(f"Transient error on attempt {attempt + 1}, retrying in {wait}s: {err[:120]}")
                time.sleep(wait)
                continue
            raise  # re-raise non-transient or exhausted retries




def run_and_submit_all( profile: gr.OAuthProfile | None):
    """
    Fetches all questions, runs the BasicAgent on them, submits all answers,
    and displays the results.
    """
    # --- Determine HF Space Runtime URL and Repo URL ---
    space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code

    if profile:
        username= f"{profile.username}"
        print(f"User logged in: {username}")
    else:
        print("User not logged in.")
        return "Please Login to Hugging Face with the button.", None

    api_url = DEFAULT_API_URL
    questions_url = f"{api_url}/questions"
    submit_url = f"{api_url}/submit"

    # 1. Instantiate Agent
    try:
        agent = build_agent()
    except Exception as e:
        print(f"Error instantiating agent: {e}")
        return f"Error initializing agent: {e}", None

    # 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)
    agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
    print(agent_code)

    # 2. Fetch Questions
    print(f"Fetching questions from: {questions_url}")
    try:
        response = requests.get(questions_url, timeout=15)
        response.raise_for_status()
        questions_data = response.json()
        if not questions_data:
             print("Fetched questions list is empty.")
             return "Fetched questions list is empty or invalid format.", None
        print(f"Fetched {len(questions_data)} questions.")
    except requests.exceptions.RequestException as e:
        print(f"Error fetching questions: {e}")
        return f"Error fetching questions: {e}", None
    except requests.exceptions.JSONDecodeError as e:
         print(f"Error decoding JSON response from questions endpoint: {e}")
         print(f"Response text: {response.text[:500]}")
         return f"Error decoding server response for questions: {e}", None
    except Exception as e:
        print(f"An unexpected error occurred fetching questions: {e}")
        return f"An unexpected error occurred fetching questions: {e}", None

    # 3. Run your Agent
    results_log = []
    answers_payload = []
    print(f"Running agent on {len(questions_data)} questions...")
    for item in questions_data:
        task_id = item.get("task_id")
        # API returns 'Question' (capital Q); guard against both casings
        question_text = item.get("Question") or item.get("question")
        file_name = item.get("file_name", "")
        file_path = item.get("file_path", "")
        if not task_id or not question_text:
            print(f"Skipping item with missing task_id or question: {item}")
            continue
        # Build the task input: append file hint and ReAct instructions
        task_input = question_text
        if file_name:
            fp_arg = f", file_path='{file_path}'" if file_path else ""
            task_input += (
                f"\n\n[Attached file: '{file_name}'. "
                f"Call read_task_file(task_id='{task_id}', file_name='{file_name}'{fp_arg}) "
                f"to download and read its contents before answering.]"
            )
        task_input += REACT_INSTRUCTIONS
        try:
            raw_answer = _run_with_retry(agent, task_input)
            submitted_answer = _extract_final_answer(raw_answer)
            answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
            results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
        except Exception as e:
             print(f"Error running agent on task {task_id}: {e}")
             results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})

    if not answers_payload:
        print("Agent did not produce any answers to submit.")
        return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)

    # 4. Prepare Submission 
    submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
    status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
    print(status_update)

    # 5. Submit
    print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
    try:
        response = requests.post(submit_url, json=submission_data, timeout=60)
        response.raise_for_status()
        result_data = response.json()
        final_status = (
            f"Submission Successful!\n"
            f"User: {result_data.get('username')}\n"
            f"Overall Score: {result_data.get('score', 'N/A')}% "
            f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
            f"Message: {result_data.get('message', 'No message received.')}"
        )
        print("Submission successful.")
        results_df = pd.DataFrame(results_log)
        return final_status, results_df
    except requests.exceptions.HTTPError as e:
        error_detail = f"Server responded with status {e.response.status_code}."
        try:
            error_json = e.response.json()
            error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
        except requests.exceptions.JSONDecodeError:
            error_detail += f" Response: {e.response.text[:500]}"
        status_message = f"Submission Failed: {error_detail}"
        print(status_message)
        results_df = pd.DataFrame(results_log)
        return status_message, results_df
    except requests.exceptions.Timeout:
        status_message = "Submission Failed: The request timed out."
        print(status_message)
        results_df = pd.DataFrame(results_log)
        return status_message, results_df
    except requests.exceptions.RequestException as e:
        status_message = f"Submission Failed: Network error - {e}"
        print(status_message)
        results_df = pd.DataFrame(results_log)
        return status_message, results_df
    except Exception as e:
        status_message = f"An unexpected error occurred during submission: {e}"
        print(status_message)
        results_df = pd.DataFrame(results_log)
        return status_message, results_df


# --- Build Gradio Interface using Blocks ---
with gr.Blocks() as demo:
    gr.Markdown("# Basic Agent Evaluation Runner")
    gr.Markdown(
        """
        **Instructions:**

        1.  Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
        2.  Log in to your Hugging Face account using the button below. This uses your HF username for submission.
        3.  Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.

        ---
        **Disclaimers:**
        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).
        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.
        """
    )

    gr.LoginButton()

    run_button = gr.Button("Run Evaluation & Submit All Answers")

    status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
    # Removed max_rows=10 from DataFrame constructor
    results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)

    run_button.click(
        fn=run_and_submit_all,
        outputs=[status_output, results_table]
    )

if __name__ == "__main__":
    print("\n" + "-"*30 + " App Starting " + "-"*30)
    # Check for SPACE_HOST and SPACE_ID at startup for information
    space_host_startup = os.getenv("SPACE_HOST")
    space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup

    if space_host_startup:
        print(f"βœ… SPACE_HOST found: {space_host_startup}")
        print(f"   Runtime URL should be: https://{space_host_startup}.hf.space")
    else:
        print("ℹ️  SPACE_HOST environment variable not found (running locally?).")

    if space_id_startup: # Print repo URLs if SPACE_ID is found
        print(f"βœ… SPACE_ID found: {space_id_startup}")
        print(f"   Repo URL: https://huggingface.co/spaces/{space_id_startup}")
        print(f"   Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
    else:
        print("ℹ️  SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")

    print("-"*(60 + len(" App Starting ")) + "\n")

    print("Launching Gradio Interface for Basic Agent Evaluation...")
    demo.launch(debug=True, share=False)