File size: 27,236 Bytes
10e9b7d
c76b236
288e56a
c76b236
288e56a
 
 
 
 
 
 
eccf8e4
3c4371f
c76b236
461fbe3
a267afe
 
461fbe3
a267afe
 
 
288e56a
 
a267afe
 
 
461fbe3
c76b236
 
288e56a
c76b236
a938b0e
fd43747
c76b236
 
10e9b7d
288e56a
 
 
3db6293
288e56a
8fd0e4d
288e56a
e80aab9
c76b236
288e56a
c76b236
288e56a
 
 
c76b236
288e56a
 
a938b0e
c76b236
288e56a
 
 
a938b0e
 
 
 
 
 
 
 
 
 
 
c76b236
a938b0e
 
 
288e56a
 
 
c76b236
 
288e56a
 
 
 
 
 
c76b236
a938b0e
288e56a
 
 
a938b0e
 
 
288e56a
a938b0e
288e56a
a938b0e
288e56a
 
 
 
 
 
 
 
 
c76b236
4021bf3
a938b0e
288e56a
a938b0e
 
288e56a
c76b236
 
288e56a
c76b236
 
 
288e56a
 
 
 
 
 
c76b236
 
288e56a
 
 
 
 
 
c76b236
 
 
288e56a
c76b236
 
 
 
 
288e56a
c76b236
 
288e56a
 
 
c76b236
 
288e56a
c76b236
 
 
 
288e56a
fd43747
 
 
 
 
 
 
 
 
 
 
 
 
 
288e56a
a938b0e
 
 
 
 
 
288e56a
a938b0e
 
 
 
 
 
 
 
 
 
 
 
288e56a
a938b0e
 
 
 
288e56a
a938b0e
 
 
 
288e56a
 
 
 
a938b0e
c76b236
 
288e56a
c76b236
a938b0e
 
 
288e56a
 
 
a938b0e
288e56a
 
 
a938b0e
 
 
 
 
288e56a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a938b0e
 
288e56a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a938b0e
 
 
 
288e56a
 
 
 
 
 
a938b0e
288e56a
 
 
 
 
 
a938b0e
288e56a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c76b236
 
288e56a
 
 
 
 
8fd0e4d
 
c76b236
8fd0e4d
288e56a
8fd0e4d
 
 
 
 
 
 
 
288e56a
8fd0e4d
 
a938b0e
288e56a
 
 
c76b236
 
 
 
 
a938b0e
 
c76b236
 
 
 
 
 
a938b0e
c76b236
 
a938b0e
 
 
 
 
 
 
 
c76b236
288e56a
8fd0e4d
288e56a
8fd0e4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
288e56a
c76b236
288e56a
 
 
c76b236
 
 
 
288e56a
c76b236
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8fd0e4d
c76b236
a938b0e
 
c76b236
288e56a
 
 
8fd0e4d
 
 
 
 
 
 
 
 
 
 
 
288e56a
 
8fd0e4d
288e56a
 
 
c76b236
288e56a
c76b236
8fd0e4d
a938b0e
 
8fd0e4d
a938b0e
c76b236
 
288e56a
c76b236
 
 
3c4371f
7e4a06b
c76b236
3c4371f
7e4a06b
3c4371f
7d65c66
3c4371f
7e4a06b
31243f4
 
e80aab9
31243f4
c76b236
31243f4
3c4371f
31243f4
c76b236
36ed51a
c1fd3d2
3c4371f
31243f4
eccf8e4
31243f4
7d65c66
31243f4
 
c76b236
 
31243f4
e80aab9
31243f4
 
3c4371f
c76b236
 
 
7d65c66
31243f4
 
e80aab9
7d65c66
 
3c4371f
31243f4
 
 
 
 
 
 
c76b236
7d65c66
 
31243f4
c76b236
 
31243f4
 
3c4371f
31243f4
 
7d65c66
3c4371f
31243f4
e80aab9
31243f4
e80aab9
c76b236
e80aab9
 
31243f4
e80aab9
 
3c4371f
 
 
e80aab9
 
31243f4
 
e80aab9
3c4371f
e80aab9
 
3c4371f
e80aab9
7d65c66
3c4371f
31243f4
7d65c66
31243f4
3c4371f
 
 
 
 
e80aab9
31243f4
 
 
 
7d65c66
31243f4
 
 
 
e80aab9
 
 
288e56a
0ee0419
e514fd7
 
288e56a
c76b236
 
e514fd7
288e56a
 
 
 
 
 
e514fd7
e80aab9
7e4a06b
31243f4
9088b99
7d65c66
e80aab9
c76b236
e80aab9
 
c76b236
3c4371f
c76b236
7d65c66
3c4371f
 
7d65c66
3c4371f
7d65c66
 
c76b236
7d65c66
 
 
c76b236
3c4371f
c76b236
 
e81e660
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
import os
import re
import io
import json
import time
import math
import traceback
import contextlib
from typing import TypedDict, Annotated
import operator

import requests
import pandas as pd
import gradio as gr

try:
    import spaces

    @spaces.GPU
    def _zerogpu_warmup():
        # Dummy function so this ZeroGPU Space passes its startup check.
        # This app doesn't need GPU compute (inference runs via Gemini/Groq
        # APIs), so this function is never called for real work.
        return True
except ImportError:
    pass

from langgraph.graph import StateGraph, END
from langgraph.prebuilt import ToolNode
from langchain_core.messages import HumanMessage, SystemMessage
from langchain_core.tools import tool
from langchain_google_genai import ChatGoogleGenerativeAI
from groq import Groq as GroqClient
from langchain_community.tools import DuckDuckGoSearchRun, WikipediaQueryRun
from langchain_community.utilities import WikipediaAPIWrapper

# =========================================================
# CONSTANTS
# =========================================================
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
MODEL_NAME = "gemini-2.5-flash"
RECURSION_LIMIT = 18          # ceiling on ReAct agent<->tool round-trips per pass
MAX_REFLECTION_ROUNDS = 2     # extra research passes allowed if not yet confident

# =========================================================
# MODELS / CLIENTS
# =========================================================
# Gemini 2.5 Flash handles ALL reasoning, tool-calling, vision, and cleanup.
# Requires GOOGLE_API_KEY as a Space secret (aistudio.google.com/apikey).
llm = ChatGoogleGenerativeAI(model=MODEL_NAME, temperature=0)

# Groq is used ONLY for its free Whisper transcription API.
# Requires GROQ_API_KEY as a Space secret (console.groq.com).
_groq_client = GroqClient(api_key=os.getenv("GROQ_API_KEY"))

# =========================================================
# FORMAT / CLEANUP (GAIA grades by exact string match)
# =========================================================
FORMAT_RULES = """
Formatting rules for the final answer (graded by EXACT STRING MATCH):
- No explanations, no "the answer is", no "FINAL ANSWER:" prefix β€” just the answer itself.
- If asked for a number, write only the number (no commas, no units, no $ sign) unless the
  question explicitly asks for units.
- If asked for a string, use as few words as possible, no articles ("a", "the") unless the
  question requires them, and do not abbreviate unless asked.
- If asked for a comma separated list, apply the above rules to each element and follow any
  ordering/alphabetization instructions exactly.
- Match the exact capitalization and spelling implied by the question when naming entities.
"""

CLEANUP_PROMPT = (
    "Extract ONLY the final answer from the text below. Strip ALL extra words, units, labels, "
    "explanations, and surrounding context β€” return the bare answer only. If it's a number, "
    "return digits only (no commas, no $ signs) unless units were explicitly requested. If it's "
    "a name, return just the name. No punctuation unless it is part of the answer itself. "
    "No 'FINAL ANSWER:' prefix. No surrounding quotes."
)

_STRIP_PATTERNS = [
    r'^\s*final answer\s*[:\-]\s*',
    r'^\s*the answer is\s*[:\-]?\s*',
    r'^\s*answer\s*[:\-]\s*',
]


def clean_answer(raw_text: str) -> str:
    """Second-pass LLM call + regex safety net to strip a draft answer down to
    the bare exact-match string GAIA expects."""
    text = str(raw_text).strip()
    try:
        cleaned = llm.invoke([
            SystemMessage(content=CLEANUP_PROMPT),
            HumanMessage(content=text),
        ])
        text = str(cleaned.content).strip()
    except Exception as e:
        print(f"clean_answer LLM pass failed, using regex fallback only: {e}")

    # Regex safety net in case the cleanup call itself left boilerplate in place.
    for pattern in _STRIP_PATTERNS:
        text = re.sub(pattern, '', text, flags=re.IGNORECASE)
    text = text.strip().strip('"').strip("'").strip()
    if text.endswith('.') and not re.search(r'\d\.\d$', text):
        text = text.rstrip('.')
    return text.strip()


# =========================================================
# RAW IMPLEMENTATIONS (wrapped as @tool below)
# =========================================================

def _read_file_impl(file_path: str) -> str:
    if not os.path.exists(file_path):
        return f"Error: file not found at {file_path}"
    ext = file_path.lower().rsplit(".", 1)[-1]
    try:
        if ext == "csv":
            df = pd.read_csv(file_path)
            return (
                f"CSV loaded. Shape: {df.shape}. Columns: {list(df.columns)}\n\n"
                f"Preview:\n{df.head(20).to_string()}\n\n"
                "If the question needs a max/min/sum/average/count/filter/sort, "
                f"use python_tool with pd.read_csv(r'{file_path}') instead of this preview."
            )
        elif ext in ("xlsx", "xls"):
            df = pd.read_excel(file_path)
            return (
                f"Excel loaded. Shape: {df.shape}. Columns: {list(df.columns)}\n\n"
                f"Preview:\n{df.head(20).to_string()}\n\n"
                "If the question needs a max/min/sum/average/count/filter/sort, "
                f"use python_tool with pd.read_excel(r'{file_path}') instead of this preview."
            )
        elif ext == "json":
            with open(file_path, "r") as f:
                data = json.load(f)
            return json.dumps(data, indent=2)[:6000]
        elif ext == "pdf":
            try:
                from pypdf import PdfReader
                reader = PdfReader(file_path)
                text = "\n".join(page.extract_text() or "" for page in reader.pages)
                return text[:10000] if text.strip() else "No extractable text found in PDF (it may be scanned/image-based)."
            except Exception as e:
                return f"Error reading PDF: {e}"
        elif ext in ("txt", "md"):
            with open(file_path, "r", errors="ignore") as f:
                return f.read()[:10000]
        else:
            with open(file_path, "r", errors="ignore") as f:
                return f.read()[:10000]
    except Exception as e:
        return f"Error reading file: {e}"


def _transcribe_audio_impl(file_path: str) -> str:
    if not os.path.exists(file_path):
        return f"Error: file not found at {file_path}"
    try:
        with open(file_path, "rb") as f:
            transcription = _groq_client.audio.transcriptions.create(
                file=(os.path.basename(file_path), f.read()),
                model="whisper-large-v3-turbo",
                response_format="text",
            )
        return str(transcription)
    except Exception as e:
        return f"Error transcribing audio: {e}"


def _analyze_image_impl(file_path: str, question: str) -> str:
    import base64 as b64
    if not os.path.exists(file_path):
        return f"Error: file not found at {file_path}"
    try:
        with open(file_path, "rb") as f:
            img_b64 = b64.b64encode(f.read()).decode("utf-8")
        ext = file_path.lower().rsplit(".", 1)[-1]
        mime = "image/png" if ext == "png" else "image/jpeg"
        response = llm.invoke([
            HumanMessage(content=[
                {"type": "text", "text": question},
                {"type": "image_url", "image_url": f"data:{mime};base64,{img_b64}"},
            ])
        ])
        return str(response.content)
    except Exception as e:
        return f"Error analyzing image: {e}"


def _get_youtube_transcript_impl(url: str) -> str:
    try:
        from youtube_transcript_api import YouTubeTranscriptApi
        match = re.search(r'(?:v=|youtu\.be/)([\w-]+)', url)
        if not match:
            return "Error: could not extract a video ID from that URL."
        video_id = match.group(1)
        transcript = YouTubeTranscriptApi.get_transcript(video_id)
        return " ".join(seg["text"] for seg in transcript)
    except Exception as e:
        return (
            f"Error fetching transcript ({e}). Transcript unavailable β€” fall back to "
            "web_search for the video's title, description, or discussions of its content."
        )


# =========================================================
# TOOLS β€” the agent chooses which of these to call, and when.
# =========================================================

@tool
def calculator(expression: str) -> str:
    """Evaluate ONE simple arithmetic expression, e.g. "12 * (3 + 4)" or "156/4 - 2".
    Use this for quick single-line arithmetic. For anything involving dates,
    multi-step logic, data files, or statistics, use python_tool instead."""
    try:
        allowed_chars = set("0123456789+-*/(). %")
        if not all(c in allowed_chars or c.isspace() for c in expression):
            return "Error: expression contains characters this calculator doesn't support. Use python_tool instead."
        return str(eval(expression, {"__builtins__": {}}))
    except Exception as e:
        return f"Error evaluating expression: {e}"


@tool
def python_tool(code: str) -> str:
    """Execute Python code β€” the preferred way to do ANY calculation you want
    verified rather than done mentally: percentages, statistics, averages,
    dates/time, currency conversion, geometry, counting, or CSV/Excel analysis.
    Pandas (pd), math, and re are pre-imported. If a data file was downloaded,
    load it yourself with pd.read_csv(path) or pd.read_excel(path) and use
    pandas operations (.sum(), .mean(), .max(), .sort_values(), filtering, etc.)
    rather than reasoning over a printed preview. Print anything you want to
    see with print() β€” only printed output is returned to you."""
    import datetime
    safe_globals = {
        "__builtins__": __builtins__,
        "pd": pd,
        "math": math,
        "re": re,
        "datetime": datetime,
    }
    buffer = io.StringIO()
    try:
        with contextlib.redirect_stdout(buffer):
            exec(code, safe_globals)
        output = buffer.getvalue().strip()
        return output if output else "Code ran with no printed output β€” use print() to surface results."
    except Exception as e:
        return f"Error executing code: {e}"


_ddg_search = DuckDuckGoSearchRun(name="ddg_search")


@tool
def web_search(query: str) -> str:
    """Search the web via DuckDuckGo for current facts, names, dates, or events
    you aren't fully certain of. If results look thin, empty, or irrelevant,
    call this again with a rewritten query β€” different keywords, more or less
    specific, or a different angle β€” rather than giving up after one try."""
    for attempt in range(2):
        try:
            result = _ddg_search.run(query)
            if result and len(result.strip()) > 20:
                return result
        except Exception as e:
            print(f"web_search attempt {attempt + 1} failed: {e}")
            time.sleep(1)
    return (
        "No usable results for this query. Rewrite it with different or more "
        "specific keywords and try again, or try wikipedia_search."
    )


_wiki = WikipediaQueryRun(api_wrapper=WikipediaAPIWrapper(top_k_results=2, doc_content_chars_max=3000))


@tool
def wikipedia_search(query: str) -> str:
    """Look up a topic on Wikipedia. Best for encyclopedic facts about people,
    places, organizations, historical events, and concepts."""
    try:
        result = _wiki.run(query)
        return result if result else "No Wikipedia page found for this query. Try web_search instead."
    except Exception as e:
        return f"Wikipedia lookup failed: {e}. Try web_search instead."


@tool
def read_file(file_path: str) -> str:
    """Read a downloaded file's contents β€” supports csv, xlsx, xls, json, pdf,
    txt, and md. For CSV/Excel this returns a schema + preview only β€” if the
    question needs a max/min/sum/average/count/filter/sort, use python_tool
    with pandas on this same file_path instead of reasoning over the preview."""
    return _read_file_impl(file_path)


IMAGE_ANALYSIS_HINT = (
    "Examine the image closely and account for whatever is relevant: text "
    "(read it via OCR), object/species/logo identification, exact counts of "
    "items or people, colors, chart or graph values and axis labels, map "
    "locations or routes, table rows and columns, screenshots (UI text, "
    "filenames, timestamps), and small/fine details."
)


@tool
def analyze_image(file_path: str, question: str) -> str:
    """Analyze a downloaded image with vision to answer a specific question
    about it. Pass the exact file_path and a focused question describing
    exactly what to look for (e.g. "What number is on the scoreboard?")."""
    return _analyze_image_impl(file_path, f"{question}\n\n{IMAGE_ANALYSIS_HINT}")


@tool
def transcribe_audio(file_path: str) -> str:
    """Transcribe a downloaded audio file (mp3, wav, m4a, ogg, flac) to text
    via Whisper. This returns the raw transcript only β€” reason over it
    yourself in a follow-up step rather than treating it as the final answer."""
    return _transcribe_audio_impl(file_path)


@tool
def get_youtube_transcript(url: str) -> str:
    """Fetch the transcript of a YouTube video given its URL. If the
    transcript is unavailable, this returns an error β€” fall back to
    web_search for information about the video instead."""
    return _get_youtube_transcript_impl(url)


TOOLS = [
    web_search,
    wikipedia_search,
    calculator,
    python_tool,
    read_file,
    analyze_image,
    transcribe_audio,
    get_youtube_transcript,
]
llm_with_tools = llm.bind_tools(TOOLS)

# =========================================================
# SYSTEM PROMPT β€” one prompt for every question, of every kind.
# There is no separate prompt per question "type": the agent is told what
# tools exist and decides for itself whether and which to use.
# =========================================================
## WITH:
RESEARCH_SYSTEM_PROMPT = f"""You are answering GAIA benchmark questions. You MUST use tools to find answers - NEVER guess.

Available tools: web_search, wikipedia_search, calculator, python_tool, read_file, analyze_image, transcribe_audio, get_youtube_transcript.

CRITICAL RULES:
1. ALWAYS use tools. Never answer from memory or guess.
2. If a file is provided, use read_file/analyze_image/transcribe_audio FIRST.
3. Use python_tool for ALL calculations - never calculate mentally.
4. Use web_search for ANY fact you're not 100% certain about.
5. If a tool fails, try a different approach or tool.
6. Complete your research in 10 steps or less.
7. Your final message must be ONLY the answer - no other text.

{FORMAT_RULES}
"""

# =========================================================
# LANGGRAPH β€” true ReAct loop (Agent <-> Tools), no upfront routing.
# =========================================================
class AgentState(TypedDict):
    messages: Annotated[list, operator.add]


def call_model(state: AgentState):
    response = llm_with_tools.invoke(state["messages"])
    return {"messages": [response]}


def should_continue(state: AgentState):
    last_message = state["messages"][-1]
    if getattr(last_message, "tool_calls", None):
        return "tools"
    return END


tool_node = ToolNode(TOOLS)
research_graph = StateGraph(AgentState)
research_graph.add_node("agent", call_model)
research_graph.add_node("tools", tool_node)
research_graph.set_entry_point("agent")
research_graph.add_conditional_edges("agent", should_continue, {"tools": "tools", END: END})
research_graph.add_edge("tools", "agent")
compiled_research_graph = research_graph.compile()

def run_react_agent(question: str, context_note: str = "") -> str:
    """Run the ReAct loop once without complex reflection to avoid loops."""
    full_question = f"{context_note}\n\n{question}".strip() if context_note else question
    
    # Force tool usage by making it explicit
    forced_prompt = f"""IMPORTANT: You MUST use your tools to answer this question. Do not guess.{full_question}"""
    
    messages = [SystemMessage(content=RESEARCH_SYSTEM_PROMPT), HumanMessage(content=forced_prompt)]
    
    try:
        result = compiled_research_graph.invoke(
            {"messages": messages}, 
            config={"recursion_limit": RECURSION_LIMIT}
        )
        candidate = str(result["messages"][-1].content).strip()
        
        # If the answer looks like it contains reasoning, try to extract just the final answer
        if len(candidate) > 500:
            # Too long - ask LLM to extract just the answer
            try:
                cleaned = llm.invoke([
                    SystemMessage(content="Extract only the final answer from this text. Return just the answer, nothing else."),
                    HumanMessage(content=candidate)
                ])
                candidate = str(cleaned.content).strip()
            except:
                pass
        
        return clean_answer(candidate) if candidate else "unknown"
        
    except Exception as e:
        print(f"Agent failed: {e}")
        return "unknown"
# =========================================================
# AGENT WRAPPER β€” same public interface as before (question, task_id) -> answer.
# No classifier: file/video info is passed as CONTEXT, and the LLM decides
# which tool(s), if any, to call.
# =========================================================
class BasicAgent:
    def __init__(self, api_url: str = DEFAULT_API_URL):
        self.api_url = api_url
        print("BasicAgent initialized (single ReAct + reflection pipeline).")

    def _download_file_if_any(self, task_id: str) -> str | None:
        url = f"{self.api_url}/files/{task_id}"
        try:
            resp = requests.get(url, timeout=15)
            if resp.status_code != 200 or not resp.content:
                return None
            cd = resp.headers.get("content-disposition", "")
            match = re.search(r'filename="?([^";]+)"?', cd)
            filename = match.group(1) if match else f"{task_id}_file"
            local_path = os.path.join("/tmp", filename)
            with open(local_path, "wb") as f:
                f.write(resp.content)
            return local_path
        except Exception as e:
            print(f"No file downloaded for {task_id}: {e}")
            return None

    def __call__(self, question: str, task_id: str | None = None) -> str:
        print(f"Processing task {task_id}: {question[:80]}...")

        local_path = self._download_file_if_any(task_id) if task_id else None
        youtube_match = re.search(r'(?:youtube\.com/watch\?v=|youtu\.be/)[\w-]+', question)

        context_lines = []
        if local_path:
            ext = local_path.lower().rsplit(".", 1)[-1]
            if ext in ('png', 'jpg', 'jpeg', 'gif', 'bmp', 'webp'):
                context_lines.append(
                    f"IMAGE FILE at: {local_path}. Use analyze_image IMMEDIATELY with this path."
                )
            elif ext in ('mp3', 'wav', 'm4a', 'ogg', 'flac'):
                context_lines.append(
                    f"AUDIO FILE at: {local_path}. Use transcribe_audio IMMEDIATELY with this path."
                )
            else:
                context_lines.append(
                    f"DATA FILE at: {local_path}. Use read_file IMMEDIATELY with this path before anything else."
                )
        if youtube_match:
            context_lines.append(
                f"YouTube video: {youtube_match.group(0)}. Use get_youtube_transcript first."
            )
        context_note = "\n".join(context_lines)

        try:
            answer = run_react_agent(question, context_note=context_note)
        except Exception as e:
            print(f"Failed: {e}")
            answer = "unknown"

        print(f"Answer: {answer[:80]}")
        return answer

# =========================================================
# GRADIO APP (submission runner) β€” UI and eval-API contract unchanged.
# =========================================================
def run_and_submit_all(profile: gr.OAuthProfile | None):
    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"

    try:
        agent = BasicAgent(api_url=api_url)
    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)

    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

    results_log = []
    answers_payload = []
    print(f"Running agent on {len(questions_data)} questions...")
    for item in 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
        try:
            submitted_answer = agent(question_text, task_id=task_id)
            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)

    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)

    print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
    try:
        response = requests.post(submit_url, json=submission_data, timeout=120)
        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


with gr.Blocks() as demo:
    gr.Markdown("# GAIA Agent Evaluation Runner (ReAct + Reflection)")
    gr.Markdown(
        """
        **Instructions:**
        1. Set `GOOGLE_API_KEY` and `GROQ_API_KEY` as secrets in your Space settings.
        2. Log in with the button below.
        3. Click 'Run Evaluation & Submit All Answers'.

        Every question runs through a single ReAct loop: the agent reasons, decides
        for itself whether it needs web_search / wikipedia_search / calculator /
        python_tool / read_file / analyze_image / transcribe_audio /
        get_youtube_transcript, observes the result, and repeats until confident β€”
        followed by a reflection pass that sends it back to research further if its
        own answer isn't fully supported by the evidence it gathered.
        """
    )
    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}")
    else:
        print("ℹ️  SPACE_ID environment variable not found (running locally?).")

    print("-" * (60 + len(" App Starting ")) + "\n")
    print("Launching Gradio Interface for GAIA Agent Evaluation...")
    demo.launch(debug=True, share=False, ssr_mode=False)