File size: 26,819 Bytes
09e6612
1963930
4b49c6e
09e6612
c19d193
09e6612
 
 
c11c48d
9dbe8fc
8fe992b
4b49c6e
 
 
 
 
c11c48d
 
 
 
 
e6cb7bb
 
afbe2c8
be4fa8c
6ef1d3a
be4fa8c
 
afbe2c8
703a126
e6cb7bb
09e6612
e6cb7bb
c11c48d
09e6612
 
b51b4fd
c11c48d
1963930
 
 
 
e13b460
 
84a30e8
c11c48d
1963930
 
 
 
d22ad00
1963930
d22ad00
1963930
4b49c6e
 
 
 
1963930
 
 
 
 
 
 
 
 
 
c11c48d
e6cb7bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4b49c6e
1963930
4b49c6e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1963930
4b49c6e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1963930
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e6cb7bb
 
 
c11c48d
 
 
8c01ffb
 
e6cb7bb
c11c48d
e6cb7bb
c11c48d
e6cb7bb
c11c48d
 
 
e6cb7bb
c11c48d
 
 
09e6612
ae7a494
e6cb7bb
c11c48d
6ef1d3a
 
 
 
c11c48d
 
 
 
09e6612
be4fa8c
c11c48d
6ef1d3a
c11c48d
 
 
e6cb7bb
c11c48d
e6cb7bb
f664f59
519a01d
35b39cd
f664f59
907db7f
35b39cd
 
 
 
 
f664f59
a879385
c11c48d
a81247f
c11c48d
 
f664f59
13d500a
f664f59
8c01ffb
e6cb7bb
 
 
8c01ffb
1963930
8fe992b
c11c48d
 
8c01ffb
1f4bcf2
c11c48d
 
e6cb7bb
 
 
 
c11c48d
 
8fe992b
 
1963930
 
 
 
 
 
 
 
 
9b5b26a
09e6612
 
 
 
 
 
 
 
1963930
09e6612
 
 
 
 
 
 
 
f9e27e9
1963930
 
 
 
 
a30cbf7
1963930
 
09e6612
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1963930
09e6612
 
1963930
09e6612
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4b49c6e
d22ad00
 
 
 
 
 
 
4b49c6e
 
09e6612
 
 
 
 
4b49c6e
 
 
09e6612
1963930
09e6612
1963930
4b49c6e
 
 
 
 
1963930
4b49c6e
 
 
09e6612
 
1963930
09e6612
4b49c6e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
09e6612
 
 
 
 
 
 
 
 
1963930
09e6612
 
1963930
09e6612
 
 
 
 
 
 
 
4b49c6e
 
 
 
 
09e6612
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e6cb7bb
09e6612
e6cb7bb
c11c48d
09e6612
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c11c48d
09e6612
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
import os
import json
import time
import inspect
import yaml
import requests
import gradio as gr
import pandas as pd

from smolagents import CodeAgent, OpenAIServerModel, LiteLLMModel, Tool, HfApiModel, ToolCallingAgent

try:
    from litellm.exceptions import RateLimitError
except ImportError:
    RateLimitError = None

from tools.calculator import Calculator
from tools.final_answer import FinalAnswerTool
from tools.visit_webpage import VisitWebpageTool
from tools.web_search import DuckDuckGoSearchTool

from research_state import ResearchState
from verifier import verify_research
import litellm
from tools.youtube_transcript import YouTubeTranscriptTool
from tools.youtube_video_analysis import YouTubeVideoAnalysisTool

youtube_transcript = YouTubeTranscriptTool()

litellm._turn_on_debug()
# =========================================================
# Constants
# =========================================================

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

MODEL_ID = "gemini/gemini-2.5-flash"  #"" "Qwen/Qwen2.5-Coder-32B-Instruct" #"groq/groq/compound-mini" #"meta-llama/llama-prompt-guard-2-22m"

# Which agent to build: "tool_calling" (ToolCallingAgent) or "code" (CodeAgent).
# Both use prompts.yaml.
AGENT_TYPE = "tool_calling"

MAX_STEPS = 6
MAX_TOKENS = 900
TEMPERATURE = 0.2

# If this file exists next to app.py, questions are read from it instead of
# being fetched from the scoring server (avoids 429s from the shared Space IP).
QUESTIONS_FILE = "questions.json"

# Number of questions to skip from the start of the list (set to 0 to run all)
SKIP_FIRST_N = 1

# Pacing / rate-limit handling (model provider)
MIN_SECONDS_BETWEEN_QUESTIONS = 60
MAX_RATE_LIMIT_RETRIES = 5
RATE_LIMIT_BASE_DELAY = 30  # seconds; doubles each retry

# Throttling / retry for every individual LLM call (each agent step is one call)
LLM_MIN_SECONDS_BETWEEN_CALLS = 7   # ~8 calls/min; lower this if your quota allows
LLM_MAX_RETRIES = 5
LLM_RETRY_BASE_DELAY = 15           # seconds; doubles each retry, capped at 120

# Rate-limit handling for the scoring server (fetch questions / submit)
HTTP_MAX_RETRIES = 6
HTTP_BASE_DELAY = 10  # seconds; doubles each retry (10, 20, 40, 80, ...)
HTTP_MAX_DELAY = 120  # cap on any single wait


# =========================================================
# Research Context
# =========================================================

class ResearchContext:
    """
    Holds the state for a single research run.
    The context is shared by the research tools so that
    searches, webpages, claims, and verification results
    belong to the same research session.
    """

    def __init__(self):
        self.state = None

    def start(self, question: str):
        self.state = ResearchState(question=question)

    def ensure_started(self, question: str = ""):
        if self.state is None:
            self.start(question)

    def reset(self):
        self.state = None


research_context = ResearchContext()


# =========================================================
# Stateful Web Search
# =========================================================

class StatefulWebSearchTool(DuckDuckGoSearchTool):
    """
    Web search tool that also records discovered sources
    in the current ResearchState.
    """

    name = "web_search"

    description = (
        "Searches the web for relevant sources. "
        "Use this for source discovery. Search results are "
        "leads and should normally be followed by visit_webpage "
        "when factual evidence is needed."
    )

    def forward(self, query: str) -> str:
        research_context.ensure_started()

        results = super().forward(query)

        # Store the search itself as research activity.
        # The actual source evidence is recorded more precisely
        # when visit_webpage reads the source.
        return results


# =========================================================
# Stateful Webpage Reader
# =========================================================

class StatefulVisitWebpageTool(VisitWebpageTool):
    """
    Reads webpages and records the resulting source content
    in ResearchState.
    """

    name = "visit_webpage"

    description = (
        "Visits a webpage and reads its contents. "
        "Use this after web_search when you need actual "
        "source evidence rather than only a search snippet."
    )

    def forward(self, url: str) -> str:
        research_context.ensure_started()

        content = super().forward(url)

        # Record the source as evidence.
        #
        # At this stage we don't know which exact claim the
        # source supports. The verification step can associate
        # the collected sources with the claims supplied by
        # the agent.
        research_context.state.add_evidence(
            claim="__SOURCE__",
            source_url=url,
            excerpt=content[:4000],
            supports_claim=None,
        )

        return content


# =========================================================
# Verification Tool
# =========================================================

class VerifyResearchTool(Tool):
    """
    Runs the deterministic verification layer over the
    current research state.
    """

    name = "verify_research"

    description = (
        "Verifies the claims in the current research session "
        "against the sources that have been collected. "
        "Use this before producing the final answer."
    )

    inputs = {
        "claims": {
            "type": "string",
            "description": (
                "The factual claims that need verification, "
                "separated by newline."
            ),
        }
    }

    output_type = "string"

    def forward(self, claims: str) -> str:
        research_context.ensure_started()

        state = research_context.state

        # Normalize claims supplied by the agent.
        claim_list = [
            claim.strip()
            for claim in claims.splitlines()
            if claim.strip()
        ]

        # Register the claims.
        for claim in claim_list:
            state.add_claim(claim)

        # Existing evidence was initially recorded against
        # __SOURCE__. Associate the collected sources with
        # the claims being verified.
        #
        # This is intentionally conservative: the verifier
        # knows that these sources were collected, but the
        # model still needs to determine whether they actually
        # support or contradict each claim.
        source_evidence = [
            evidence
            for evidence in state.evidence
            if evidence.claim == "__SOURCE__"
        ]

        # Rebuild claim-level evidence.
        #
        # We do not automatically mark sources as supporting.
        # They remain uncertain until the agent has inspected
        # and reasoned about the evidence.
        for claim in claim_list:
            existing = state.evidence_for_claim(claim)

            if not existing:
                for source in source_evidence:
                    state.add_evidence(
                        claim=claim,
                        source_url=source.source_url,
                        excerpt=source.excerpt,
                        supports_claim=None,
                    )

        verification = verify_research(state)

        return _format_verification_result(verification)


def _format_verification_result(result: dict) -> str:
    lines = []

    lines.append("## Verification Results")
    lines.append("")

    for item in result["claims"]:
        lines.append(
            f"### {item['status']}: {item['claim']}"
        )
        lines.append(item["reason"])

        if item["sources"]:
            lines.append("Sources:")
            for source in item["sources"]:
                lines.append(f"- {source}")

        lines.append("")

    if result["verified"]:
        lines.append("Overall result: SUPPORTED")
    else:
        lines.append(
            "Overall result: NOT FULLY VERIFIED"
        )

    return "\n".join(lines)


# =========================================================
# Rate limit / quota handling (model provider)
# =========================================================

class DailyQuotaExhaustedError(Exception):
    """Raised when the model provider reports a daily/per-day quota
    exhaustion, as opposed to a transient per-minute rate limit."""
    pass


def _is_rate_limit_error(exc: Exception) -> bool:
    """
    Detect rate-limit / quota errors across providers, since exact
    exception types can vary by SDK version. Falls back to matching
    on common message fragments.
    """
    if RateLimitError is not None and isinstance(exc, RateLimitError):
        return True

    message = str(exc).lower()
    signals = [
        "rate limit",
        "rate_limit",
        "429",
        "quota",
        "resource_exhausted",
        "too many requests",
    ]
    return any(signal in message for signal in signals)


def _is_daily_quota_exhausted(exc: Exception) -> bool:
    """
    Heuristically detect a *daily/per-day* quota exhaustion (as opposed
    to a transient per-minute rate limit). These typically won't resolve
    with a short backoff, so retrying is pointless and the whole run
    should stop early rather than burn time and further requests.
    Gemini/Google API quota errors usually include phrases like
    "PerDay", "per day", "daily limit", or a quota_id containing
    "PerDay" when the daily free-tier quota is hit.
    """
    message = str(exc).lower()
    signals = [
        "per day",
        "perday",
        "daily limit",
        "daily quota",
        "generate_content_free_tier_requests_per_day",
    ]
    return any(signal in message for signal in signals)


def _run_agent_with_backoff(agent_runner, question_text,
                             max_retries=MAX_RATE_LIMIT_RETRIES,
                             base_delay=RATE_LIMIT_BASE_DELAY):
    """
    Runs the agent on a single question, retrying with exponential
    backoff if a transient rate-limit error is hit. If the error looks
    like a daily/per-day quota exhaustion, raises DailyQuotaExhaustedError
    immediately instead of retrying, since backoff won't help.
    Non-rate-limit errors are raised immediately (handled by the caller).
    """
    for attempt in range(max_retries + 1):
        try:
            return agent_runner(question_text)
        except Exception as e:
            if _is_daily_quota_exhausted(e):
                raise DailyQuotaExhaustedError(str(e)) from e

            if _is_rate_limit_error(e) and attempt < max_retries:
                delay = base_delay * (2 ** attempt)  # 30, 60, 120, 240, 480...
                print(
                    f"Rate limit/quota hit (attempt {attempt + 1}/{max_retries}). "
                    f"Backing off for {delay}s..."
                )
                time.sleep(delay)
                continue
            raise


# =========================================================
# Rate limit handling (scoring server HTTP requests)
# =========================================================

def _request_with_backoff(method, url,
                          max_retries=HTTP_MAX_RETRIES,
                          base_delay=HTTP_BASE_DELAY,
                          max_delay=HTTP_MAX_DELAY,
                          **kwargs):
    """
    Performs an HTTP request, retrying on 429 (Too Many Requests) and
    transient 502/503/504 responses. Honours the server's Retry-After
    header when present, otherwise uses exponential backoff.
    Raises requests.exceptions.HTTPError (via raise_for_status) if the
    request still fails after all retries or fails for another reason.
    """
    retry_statuses = {429, 502, 503, 504}

    for attempt in range(max_retries + 1):
        response = requests.request(method, url, **kwargs)

        if response.status_code in retry_statuses and attempt < max_retries:
            retry_after = response.headers.get("Retry-After")
            try:
                delay = float(retry_after)
            except (TypeError, ValueError):
                delay = base_delay * (2 ** attempt)
            delay = min(delay, max_delay)

            print(
                f"{method} {url} returned {response.status_code} "
                f"(attempt {attempt + 1}/{max_retries}). "
                f"Retrying in {delay:.0f}s..."
            )
            time.sleep(delay)
            continue

        response.raise_for_status()
        return response


def _load_questions(questions_url):
    """
    Loads questions from a local QUESTIONS_FILE if present, otherwise
    fetches them from the scoring server (with backoff).
    """
    if os.path.exists(QUESTIONS_FILE):
        print(f"Loading questions from local file: {QUESTIONS_FILE}")
        with open(QUESTIONS_FILE, "r", encoding="utf-8") as f:
            return json.load(f)

    response = _request_with_backoff("GET", questions_url, timeout=15)
    return response.json()


# =========================================================
# Throttled + retrying LLM calls
# =========================================================
# smolagents catches model errors internally and turns them into step
# errors / a garbage "final answer", so rate limits never reach the
# question-level backoff. Handling them at the litellm.completion level
# makes every agent step wait and retry instead.

_original_litellm_completion = litellm.completion
_last_llm_call_time = 0.0


def _throttled_completion(*args, **kwargs):
    global _last_llm_call_time

    for attempt in range(LLM_MAX_RETRIES + 1):
        wait = LLM_MIN_SECONDS_BETWEEN_CALLS - (time.monotonic() - _last_llm_call_time)
        if wait > 0:
            time.sleep(wait)
        _last_llm_call_time = time.monotonic()

        try:
            return _original_litellm_completion(*args, **kwargs)
        except Exception as e:
            if _is_daily_quota_exhausted(e):
                raise

            if _is_rate_limit_error(e) and attempt < LLM_MAX_RETRIES:
                delay = min(LLM_RETRY_BASE_DELAY * (2 ** attempt), 120)
                print(
                    f"LLM rate limit hit (attempt {attempt + 1}/{LLM_MAX_RETRIES}). "
                    f"Waiting {delay}s..."
                )
                time.sleep(delay)
                continue
            raise


litellm.completion = _throttled_completion


# =========================================================
# Load Prompts
# =========================================================

with open("prompts.yaml", "r", encoding="utf-8") as stream:
    prompt_templates = yaml.safe_load(stream)


# =========================================================
# Tools
# =========================================================

web_search = StatefulWebSearchTool(
    max_results=10
)

visit_webpage = StatefulVisitWebpageTool()

calculator = Calculator()

verify_research_tool = VerifyResearchTool()

final_answer = FinalAnswerTool()

youtube_video_analysis = YouTubeVideoAnalysisTool(
    model="gemini-2.5-flash"
)

tools = [
    web_search,
    visit_webpage,
    calculator,
    verify_research_tool,
    youtube_transcript,
    final_answer,
    youtube_video_analysis,
]


# =========================================================
# Model
# =========================================================
"""
model = HfApiModel(
    model_id=MODEL_ID,
    provider="auto",     # or "together", "nebius", "fireworks-ai", etc.
    token=os.environ["GEMINI_API_KEY"],
    max_tokens=MAX_TOKENS,
    temperature=TEMPERATURE,
    timeout=120,
)
"""

model = LiteLLMModel(
    model_id=MODEL_ID,
    api_key=os.environ["GEMINI_API_KEY"],
    max_tokens=MAX_TOKENS,
    temperature=TEMPERATURE,
    #tool_config={"function_calling_config": {"mode": "NONE"}},
)


# =========================================================
# Agent
# =========================================================

_agent_kwargs = dict(
    model=model,
    tools=tools,
    max_steps=MAX_STEPS,
    verbosity_level=1,
    planning_interval=None,
    name="web_research_agent",
    description=(
        "A web research and fact-verification agent that "
        "searches for evidence, reads sources, performs "
        "calculations, checks claims, detects uncertainty "
        "and produces cited answers."
    ),
    prompt_templates=prompt_templates,
)

if AGENT_TYPE == "code":
    agent = CodeAgent(grammar=None, **_agent_kwargs)
elif AGENT_TYPE == "tool_calling":
    agent = ToolCallingAgent(**_agent_kwargs)
else:
    raise ValueError(f"Unknown AGENT_TYPE: {AGENT_TYPE!r} (use 'code' or 'tool_calling')")

print(f"Using agent type: {AGENT_TYPE}")


class ResearchAgentWrapper:
    """
    Thin wrapper so the research agent can be called like
    BasicAgent: agent(question) -> answer string.
    Resets the shared research context for every new question
    so state doesn't leak between questions.
    """

    def __init__(self, agent: CodeAgent):
        self.agent = agent
        print("ResearchAgentWrapper initialized.")

    def __call__(self, question: str) -> str:
        print(f"Agent received question (first 50 chars): {question[:50]}...")
        research_context.reset()
        research_context.start(question)
        answer = self.agent.run(question)

        # smolagents swallows model errors (e.g. rate limits) and returns them
        # as the "answer". Re-raise so the question-level backoff can retry it
        # and the error text is never submitted as a real answer.
        if str(answer).startswith("Error in generating final LLM output"):
            raise RuntimeError(str(answer))

        print(f"Agent returning answer (first 50 chars): {str(answer)[:50]}...")
        return str(answer)


# =========================================================
# Evaluation Runner
# =========================================================

def run_and_submit_all(profile: gr.OAuthProfile | None):
    """
    Fetches all questions, runs the research agent on them,
    submits all answers, and displays the results.
    """
    space_id = os.getenv("SPACE_ID")

    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_runner = ResearchAgentWrapper(agent)
    except Exception as e:
        print(f"Error instantiating agent: {e}")
        return f"Error initializing agent: {e}", None

    agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
    print(agent_code)

    # 2. Fetch Questions (with backoff on 429 / transient server errors)
    print(f"Fetching questions from: {questions_url}")
    try:
        questions_data = _load_questions(questions_url)
        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}")
        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 = []
    quota_exhausted = False

    # Skip the first N questions
    questions_data = questions_data[SKIP_FIRST_N:]
    print(
        f"Skipping first {SKIP_FIRST_N} question(s). "
        f"Running agent on {len(questions_data)} questions..."
    )

    for idx, item in enumerate(questions_data):
        task_id = item.get("task_id")
        question_text = item.get("question")
        if not task_id or question_text is None:
            print(f"Skipping item with missing task_id or question: {item}")
            continue

        start_time = time.monotonic()

        try:
            submitted_answer = _run_agent_with_backoff(agent_runner, question_text)
            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 DailyQuotaExhaustedError as e:
            print(f"Daily quota exhausted, stopping run early: {e}")
            results_log.append({
                "Task ID": task_id,
                "Question": question_text,
                "Submitted Answer": f"SKIPPED (daily quota exhausted): {e}",
            })
            quota_exhausted = True
            break
        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}"})

        # Pace requests: ensure at least MIN_SECONDS_BETWEEN_QUESTIONS
        # seconds between question starts, accounting for however long
        # this question took (including any backoff sleeps it already did).
        if idx < len(questions_data) - 1:
            elapsed = time.monotonic() - start_time
            remaining = MIN_SECONDS_BETWEEN_QUESTIONS - elapsed
            if remaining > 0:
                print(f"Waiting {remaining:.1f}s before the next question (pacing)...")
                time.sleep(remaining)

    if quota_exhausted:
        print(
            f"Stopped early due to daily quota exhaustion. "
            f"{len(answers_payload)} answers collected before stopping."
        )

    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 (with backoff on 429 / transient server errors)
    print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
    try:
        response = _request_with_backoff("POST", submit_url, json=submission_data, timeout=60)
        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.')}"
        )
        if quota_exhausted:
            final_status += (
                f"\n\nNote: run stopped early after {len(answers_payload)} "
                f"questions due to daily quota exhaustion."
            )
        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


# =========================================================
# Gradio Interface
# =========================================================

with gr.Blocks() as demo:
    gr.Markdown("# Web Research Agent Evaluation Runner")
    gr.Markdown(
        """
        **Instructions:**
        1.  Log in to your Hugging Face account using the button below.
        2.  Click 'Run Evaluation & Submit All Answers' to fetch questions, run the research agent, submit answers, and see the score.
        ---
        **Disclaimers:**
        Once clicking the submit button, it can take some time (the agent must search, read sources, and verify claims for every question).
        """
    )

    gr.LoginButton()

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

    status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
    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)
    space_host_startup = os.getenv("SPACE_HOST")
    space_id_startup = os.getenv("SPACE_ID")

    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(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 Web Research Agent Evaluation...")
    demo.launch(debug=True, share=False)