File size: 29,721 Bytes
10e9b7d
 
8e99e29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eccf8e4
3c4371f
8e99e29
 
19aefcd
10e9b7d
d59f015
e80aab9
3db6293
e80aab9
133c3b8
 
 
d59f015
8e99e29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cd68a0f
8e99e29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cd68a0f
 
 
8e99e29
 
 
43df6ab
8e99e29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133c3b8
8e99e29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133c3b8
8e99e29
133c3b8
8e99e29
 
 
133c3b8
8e99e29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4021bf3
b90251f
31243f4
133c3b8
31243f4
 
3c4371f
7e4a06b
1ca9f65
3c4371f
7e4a06b
3c4371f
7d65c66
3c4371f
7e4a06b
31243f4
 
e80aab9
b177367
31243f4
8e99e29
31243f4
3c4371f
31243f4
b177367
65a6806
3c4371f
7d65c66
31243f4
eccf8e4
31243f4
7d65c66
31243f4
 
3c4371f
 
31243f4
e80aab9
31243f4
 
3c4371f
 
7d65c66
3c4371f
7d65c66
31243f4
 
e80aab9
b177367
7d65c66
 
3c4371f
8e99e29
133c3b8
7cb355a
31243f4
8e99e29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133c3b8
8e99e29
 
 
 
31243f4
 
 
8e99e29
 
7d65c66
8e99e29
31243f4
 
8e99e29
 
 
 
31243f4
 
3c4371f
31243f4
8e99e29
b177367
7d65c66
3c4371f
31243f4
e80aab9
7d65c66
31243f4
e80aab9
7d65c66
e80aab9
 
31243f4
e80aab9
 
3c4371f
 
 
e80aab9
 
31243f4
 
e80aab9
3c4371f
e80aab9
 
3c4371f
e80aab9
7d65c66
3c4371f
31243f4
7d65c66
31243f4
3c4371f
 
 
 
 
e80aab9
31243f4
 
 
 
7d65c66
31243f4
 
 
 
8e99e29
e80aab9
 
 
31243f4
0ee0419
e514fd7
 
 
81917a3
e514fd7
 
 
 
 
 
 
 
e80aab9
 
7e4a06b
e80aab9
31243f4
e80aab9
9088b99
7d65c66
 
e80aab9
31243f4
 
 
e80aab9
 
 
8e99e29
3c4371f
7d65c66
3c4371f
7d65c66
 
3c4371f
 
7d65c66
3c4371f
7d65c66
 
 
 
 
 
 
 
 
3c4371f
 
31243f4
3c4371f
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
787
788
789
790
791
import os
import gradio as gr
import base64
import ffmpeg, cv2, numpy as np, tempfile, io, base64, os, pathlib
import openai
from pathlib import Path
from typing import List, TypedDict, Dict, Any
from langchain_core.messages import SystemMessage, HumanMessage, AIMessage
from pytube import YouTube
from langchain.tools import tool
from langchain_community.utilities import WikipediaAPIWrapper
from langchain.agents import OpenAIFunctionsAgent, AgentExecutor
from langchain_openai import ChatOpenAI
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
from langgraph.graph import START, StateGraph, END
from langchain_community.tools.tavily_search import TavilySearchResults
import PIL.Image as Image
import subprocess
import requests, os, tempfile, shutil
import requests
import pandas as pd
import time

openai.api_key = os.getenv("OPENAI_API_KEY")

# (Keep Constants as is)
# --- Constants ---
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"

#llm = HuggingFaceInferenceAPI(model_name="meta-llama/Llama-3.2-3B-Instruct")

# --- Final Agent Definition ---
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------

general_llm = ChatOpenAI(model="gpt-4o-mini")
audio_llm = "whisper-1"

class AgentState(TypedDict, total=False):
    file_path: str | None  # Contains file path
    question: str  # Contains tabular file path (CSV)
    answer: str | None
    agent_type: str | None
    messages: list[AIMessage | HumanMessage | SystemMessage]

@tool
def addition_tool(list: List[float]) -> float:
    """
    Description:
        A simple addition tool that takes a list of numbers and returns their sum.

    Arguments:
        • list (List[float]): List of numbers to add.

    Return:
        float – The sum of the numbers in the list.
    """

    return sum(list)

@tool
def xlsx_handler(filepath: str) -> List[Dict[str, Any]]:
    """
    Description:
        Load the first sheet of an Excel workbook and convert it into
        a JSON-serialisable list of row dictionaries (records).

    Arguments:
        • filepath (str): Absolute or relative path to the .xlsx file.

    Return:
        str – A list of dictionaries representing the column names and their values.
    """
    # Load the Excel file
    df = pd.read_excel(filepath)

    columns = df.columns.tolist()

    result = []
    for col in columns:
        result.append({"column": col, "values": df[col].tolist()})
    # Convert to list of dictionaries (records)
    #data = df.to_dict(orient="records")

    # Convert to JSON string (pretty-printed)
    #return json.dumps(data, indent=4)
    return result

@tool
def python_handler(filepath: str) -> str:
    """
    Description:
        Execute a stand-alone Python script in a sandboxed subprocess and
        capture anything the script prints to stdout.  Stderr is returned
        instead if the script exits with a non-zero status.

    Arguments:
        • filepath (str): Path to the .py file to run.

    Return:
        str – The final output of the .py file.
    """
    try:
        result = subprocess.run(
            ["python", filepath],
            capture_output=True,
            text=True,
            timeout=30 # Safety
        )
        return result.stdout.strip() if result.returncode == 0 else result.stderr
    except Exception as e:
        return f"Execution failed: {str(e)}"

@tool
def video_decomposition(url: str, task: str) -> str:
    """
    Description:
        Download a YouTube video, extract ≤ 10 visually distinct key frames
        and a Whisper transcript, feed them plus the user’s task to a
        vision-capable LLM, and return the model’s answer.

    Arguments:
        • url (str)          : Full YouTube link.
        • task (str)         : The question the model should answer about the clip.

    Return:
        str – The final response to the user question derived from both audio and visuals.
    """

    with tempfile.TemporaryDirectory() as tmp:
        tmp_dir = pathlib.Path(tmp)

        # 1) Fetch clip
        vid_path = download_youtube(url, tmp_dir)

        # 2) Key-frame extraction
        frames = key_frames_retrieval(vid_path)

        # 3) Audio extraction
        transcript = audio_retrieval(vid_path)

        system_msg = SystemMessage(
        content=("You are a Vision AI assistant that can process videos and answer correctly the user's questions"
                 "You are provided with key video frames, an audio transcript and a task related with those"
                 "Read the task **carefully**, examine all the video frames and the audio transcript and your final response **MUST** be only the final answer to the task's question"
                 "The content and format of your final respose is dictated by the task and only that")
        )

        # 4) Build multimodal prompt
        parts = [
            {
                "type": "text",
                "Task": (f"{task}")
            },
            {
                "type": "text",
                "Transcript": (f"{transcript[:4000]}") 
            }
        ]
        for im in frames:
            parts.extend(
                {
                    "type": "image_url",
                    "image_url": {"url": img_to_data(im)},
                }
        )
            
        messages = [
            system_msg,
            HumanMessage(
                content=parts
            )
        ]

        response = general_llm.invoke(messages)

        return response

@tool
def reverse_string(text: str) -> str:
    """
    Description:
        Reverse the order of words *and* the letters inside each word.
        Converts a fully reversed sentence back to readable form.

    Arguments:
        • text (str): Original sentence to transform.

    Return:
        str – The readable reversed sentence.
    """
    # 1️⃣  split into words, 2️⃣  reverse word order,
    # 3️⃣  reverse letters in each word, 4️⃣  re-join
    reversed_words = [word[::-1] for word in reversed(text.split())]
    return " ".join(reversed_words)

@tool
def web_search(query: str):
    """
    Description:
        A web search tool. Scrapes the top results and returns each on its own line.

    Arguments:
        • query (str) : question you want to web search.

    Return:
        str – A newline-separated text summary: '<title> — <url> : <snippet>' or 'No results found'
    """
    search = TavilySearchResults()
    results = search.run(query)
    return "\n".join([f"- {r['content']} ({r['url']})" for r in results])

@tool
def wikipedia_search(query: str):
    """
    Description:
        Query the English-language Wikipedia via the MediaWiki API and
        return a short plain-text extract.

    Arguments:
        • query (str)     : Page title or free-text search string.

    Return:
        str – Extracted summary paragraph.
    """
    
    wiki = WikipediaAPIWrapper()  
    return wiki.run(query)

def download_youtube(url: str, out_dir: pathlib.Path) -> pathlib.Path:
    delay = 2 
    yt = YouTube(url)
    stream = yt.streams.filter(progressive=True, file_extension="mp4")\
                       .order_by("resolution").desc().first()
    return pathlib.Path(stream.download(output_path=out_dir))

def key_frames_retrieval(video: pathlib.Path, max: int = 6, thresh: float = 0.35, max_frame_mb: float = 0.25):
    """
    Scan *all* frames in `video`, keep every frame whose colour-histogram
    differs from the previous scene by more than `thresh`, then return the first
    `max` most-distinct ones (highest histogram distance).

    Returns
    -------
    List[PIL.Image]   # ≤ `limit` images, sorted by descending “scene change” score
    """
    cap = cv2.VideoCapture(str(video))
    ok, frame = cap.read()

    if not ok:
        cap.release()
        return []
    
    def hsv_hist(img) -> np.ndarray:
        return cv2.calcHist(
            [cv2.cvtColor(img, cv2.COLOR_BGR2HSV)],
            [0, 1], None, [50, 60], [0, 180, 0, 256]
        )

    def bgr_to_pil(bgr) -> Image.Image:
        img = Image.fromarray(cv2.cvtColor(bgr, cv2.COLOR_BGR2RGB))
        # shrink oversized frames so base64 prompt stays small
        if (img.width * img.height * 3 / 1_048_576) > max_frame_mb:
            img.thumbnail((800, 800))
        return img
    
    prev_hist = hsv_hist(frame)
    candidates: list[tuple[float, Image.Image]] = [(1.0, bgr_to_pil(frame))]  # always keep first

    while ok:

        ok, frame = cap.read()

        if not ok:
            break

        hist = hsv_hist(frame)
        
        diff = cv2.compareHist(prev_hist, hist, cv2.HISTCMP_BHATTACHARYYA)

        if diff > thresh:

            candidates.append((diff, bgr_to_pil(frame)))
            prev_hist = hist
    
    cap.release()

    candidates.sort(key=lambda t: t[0], reverse=True)

    top_frames = [img for _, img in candidates[:max]]

    return top_frames

def audio_retrieval(video: pathlib.Path) -> str:
    """
    Extract the audio track from `video`, save it as a temporary MP3,
    and return the transcript produced by `audio_llm.audio_to_text`.
    """
    with tempfile.NamedTemporaryFile(suffix=".mp3") as tmp_mp3:
        (
            ffmpeg
            .input(str(video))
            .output(
                tmp_mp3.name,
                ac=1, ar="16000",          # mono, 16 kHz (keeps Whisper happy)
                audio_bitrate="128k",
                format="mp3",
                loglevel="quiet"
            )
            .overwrite_output()
            .run()
        )
        tmp_mp3.seek(0)                    # rewind before passing the handle
        transcript = openai.audio.transcriptions.create(model=audio_llm, file=tmp_mp3, response_format="text")

    return transcript

def img_to_data(img: Image.Image) -> str:
    buf = io.BytesIO(); img.save(buf, format="PNG", optimize=True)
    b64 = base64.b64encode(buf.getvalue()).decode()
    return f"data:image/png;base64,{b64}"

def task_examiner(state: AgentState):
    file_path = state["file_path"]

    if file_path != None:
        p = Path(file_path)
        suffix = p.suffix
        if suffix == ".png":
            state["agent_type"] = "vision"
        elif suffix == ".mp3":
            state["agent_type"] = "audio"
        elif suffix == ".py" or suffix == ".xlsx":
            state["agent_type"] = "code"
    else:
        #if "video" in state["question"]:
        #    state["agent_type"] = "vision"
        #else:
        state["agent_type"] = "general"
    return state

def task_router(state: AgentState) -> str:

    return state["agent_type"]

def general_agent(state: AgentState):

    question = state["question"]

    tools = [web_search, wikipedia_search, reverse_string]

    system_prompt = ChatPromptTemplate.from_messages([
        ("system",
        """
        SYSTEM GUIDELINES:
            -   You are a general AI assistant that is tasked with answering correctly the user's questions.
            -   You have several tools in your disposal for differend kinds of tasks.
            -   You **MUST** think step by step before using any tool and call the tools only when you are sure that you need them.
        **Tool-reuse rule:** 
            -   Keep an internal list of tool names you have already called in this answer
            -   If a name is on that list you MUST NOT call it again. (You may still call a different tool once.)
        TOOLS:
            -   reverse_string: This is a tool that reverses a sentence so if a question is not readable then try to pass it to this tool.
            -   web_search: This tool takes a question as input and searches the web for up-to-date information and return an answer.
            -   wikipedia_search: This searches exclusively the english wikipedia page for up-to-date information that may not available in your training data.
        INPUT FORMAT:
            -   A question (text) that you should answer correctly.
        OUTPUT FORMAT:
        Output **ONLY** the final answer dictated by the user's question and only that.
        **NEVER** wrap your final answer like this: <sentence> answer </sentence>.
        <**IMPORTANT**> If the question contains a youtube link (https://www.youtube.com/watch?...) and **ONLY THEN** output this "Don't know".
        If the question tells you to output 'How many ...' you **MUST** response with **only** a single numeral and absolutely nothing else (no punctuation, no sentence, no units).
        If the question tells you to output 'What number ...' you **MUST** response with **only** a single numeral and absolutely nothing else (no punctuation, no sentence, no units).
        If the question tells you to output 'Who did ...' you **MUST** response with **only** the full name unless the question directs you otherwise and absolutely nothing else (no punctuation, no sentence, no units).
        If the question asks to provide a comma-separated list that you **MUST** response with **only** a comma-separated list '[...,...,...]'. **ABSOLUTELY NEVER** output a list like this a,b,c,d,e.
        If the question asks to output a list -> Output: [item1,item2,item3]
        If the question tells you to output 'What does the person A say when ...' you **MUST** response with **only** the phrase that person says and absolutely nothing else (no punctuation, no sentence, no units).
        """),
        ("user", "{input}"), 
        MessagesPlaceholder("agent_scratchpad"),
    ])

    
    agent = OpenAIFunctionsAgent(
        llm=general_llm,
        tools=tools,
        prompt=system_prompt
    )

    agent_executor = AgentExecutor.from_agent_and_tools(
        agent=agent,
        tools=tools,
        verbose=True,
    )

    response = agent_executor.invoke({"input": question})

    state["answer"] = response["output"]
    
    return state

def audio_agent(state: AgentState):

    with open(state["file_path"], "rb") as f:
        transcript = openai.audio.transcriptions.create(model=audio_llm, file=f, response_format="text")

    question = state["question"]

    system_msg = SystemMessage(
        content=("You are an AI assistant that answers the user's question based solely on the provided transcript."
                 "When the user asks for a “comma-delimited / comma-separated list”, you must:" 
                 "  -   Filter the items exactly as requested." 
                 "  -   Output one single line that contains the items separated by commas and a space enclosed in square brackets."
                 "  -   Output nothing else- no extra words or explanations"
                 "OUTPUT FORMAT EXAMPLES:"
                 "If asked to output a list -> Output: [item1,item2,item3]"
                 "If asked something else -> Output: text answering exactly that question and nothing more"
                 )
    )

    messages = [
        system_msg,
        HumanMessage(
            content=[
                {
                    "type": "text",
                    "text": f"Transcript:\n{transcript}\n\nQuestion:\n{question}"
                }
            ]
        )
    ]

    response = general_llm.invoke(messages)

    state["answer"] = response.content.strip()

    return state

def vision_agent(state: AgentState):    

    file_path   = state["file_path"]
    question = state["question"]

    with open(file_path, "rb") as image_file:
        
        image_bytes = image_file.read()

    image_base64 = base64.b64encode(image_bytes).decode("utf-8")

    system_msg = SystemMessage(
        content=("""
                You are a Vision AI assistant that can process images and answer correctly the user's questions"
                **OUTPUT** only the final answer and absolutely nothing else (no punctuation, no sentence, no units).
                """)
    )

    messages = [
        system_msg,
        HumanMessage(
            content=[
                {
                    "type": "text",
                    "text": (f"{question}")
                },
                {
                    "type": "image_url",
                    "image_url": {
                        "url": f"data:image/png;base64,{image_base64}"
                    },
                }                
            ]
        )
    ]

    response = general_llm.invoke(messages)

    state["answer"] = response.content.strip()
    
    return state

def code_agent(state: AgentState): 

    file_path   = state["file_path"]
    question = state["question"]

    tools = [xlsx_handler, python_handler, addition_tool]

    system_prompt = ChatPromptTemplate.from_messages([
        ("system",
        """ SYSTEM GUIDELINES:
                -   You are a data AI assistant and your job is to answer questions that depend on .xlsx or .py files.
                -   You have in your disposal 2 tools that are mandatory for solving the tasks.
                -   You **MUST** use the tools as instructed below and you **MUST** output only the final numeric result of the task.
            INPUT FORMAT:
                -   A question (text) based on a file which will be either .py or .xlsx.
                -   The path of the file related to the question.
            TOOLS:
                -   Tool name: xlsx_handler, Purpose: This is the tool you should use if the file contained in the file_path is an .xlsx file and it's purpose is to return the contents of the file in a list of dictionaries for you to process, reason **INTERNALLY** and output only the final numeric result.
                -   Tool name: python_handler, Purpose: This is the tool you should use if the file contained in the file_path is a .py file and it's purpose is to execute the python file and return the final numeric result of it.
                -   Tool name: addition_tool, Purpose: This is the tool you should use if the question asks you to sum a list of numbers and return the final numeric result.
            EXAMPLE OUTPUTS:
                -   Input: "What is the result of the code in the file?" Output: "5" 
                -   Input: "What is the total sales mentioned in the file. Your answer must have 2 decimal places?" Output: "305.00"
                -   YOU MUST OUTPUT ONLY THE FINAL NUMBER.
            
            The file relevant to the task is at: {file_path}."""),
        ("user", "{input}"), 
        MessagesPlaceholder("agent_scratchpad"),
    ])

    
    agent = OpenAIFunctionsAgent(
        llm=general_llm,
        tools=tools,
        prompt=system_prompt
    )

    agent_executor = AgentExecutor.from_agent_and_tools(
        agent=agent,
        tools=tools,
        verbose=True,
    )

    #agent_executor = agent_executor.partial(file_path=file_path)

    response = agent_executor.invoke({"input": question, "file_path": file_path})

    state["answer"] = response["output"]

    return state

class Agent_Workflow:
    def __init__(self):
        print("Agent Workflow initialized.")
    def __call__(self, question: str, filepath: str) -> str:

        builder = StateGraph(AgentState)

        # Agent Nodes
        builder.add_node("task_examiner", task_examiner)
        builder.add_node("general_agent", general_agent)
        builder.add_node("audio_agent", audio_agent)
        builder.add_node("vision_agent", vision_agent)
        builder.add_node("code_agent", code_agent)

        # Edges that connect agent nodes
        builder.add_edge(START, "task_examiner")
        builder.add_conditional_edges("task_examiner", task_router,
            {
                "general": "general_agent",
                "audio": "audio_agent",
                "vision": "vision_agent",
                "code": "code_agent"
            }
        )
        builder.add_edge("general_agent", END)
        builder.add_edge("audio_agent", END)
        builder.add_edge("vision_agent", END)
        builder.add_edge("code_agent", END)

        workflow_graph = builder.compile()

        state = workflow_graph.invoke({"file_path": filepath, "question": question, "answer": "",})

        return state["answer"]

def fetch_task_file_static(task_id: str, file_name: str | None = None, session: requests.Session | None = None) -> Path:
    """
    Download the attachment for `task_id` to temp_files/<task_id>.<suffix>
    """
    if file_name == None:
        return None

    # Decide the suffix
    suffix = Path(file_name).suffix if file_name else ""
    dest = "temp/"+task_id+suffix

    url = f"{DEFAULT_API_URL}/files/{task_id}"
    s = session or requests

    with s.get(url, stream=True, timeout=30) as r:
        r.raise_for_status()
        with open(dest, "wb") as f:
            shutil.copyfileobj(r.raw, f)

    return dest

def run_and_submit_all( profile: gr.OAuthProfile | None):
    """
    Fetches all questions, runs the FinalAgent on them, submits all answers,
    and displays the results.
    """

    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 ( modify this part to create your agent)
    try:
        agent = Agent_Workflow()
    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/Psiska/Final_Assignment/tree/main"

    # 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...")
    session = requests.Session()

    j=0
    for item in questions_data:
        task_id   = item["task_id"]
        question  = item["question"]
        file_name = item.get("file_name")

        file_path = None

        if file_name:
            try:
                file_path = fetch_task_file_static(task_id, file_name, session=session)
            except requests.HTTPError as e:
                print(f"⚠️  Couldn’t fetch file for {task_id}: {e}")

        #print(f"Question is : {question}\n")
        #[2,4,5,6,7,8,10,12,15,16,17]
        """
        if j in [2,4,5,6,7,8,10,12,15,16,17]:
            time.sleep(5)
            print(f"Question is : {question}")
            print(f"File path is : {file_path}")
            submitted_answer = agent(question=question, filepath=file_path)
            print(f"Answer is : {submitted_answer}")
        
        j=j+1
        """
        print(f"Question {j+1} is : {question}")
        print(f"File path is : {file_path}")
        
        if not task_id or question is None:
            print(f"Skipping item with missing task_id or question: {item}")
            continue
        try:
            submitted_answer = agent(question=question, filepath=file_path)
            print(f"Answer for question {j+1} is: {submitted_answer}")
            answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
            results_log.append({"Task ID": task_id, "Question": question, "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, "Submitted Answer": f"AGENT ERROR: {e}"})
        
        j=j+1
        

    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(os.getenv("HF_TOKEN"))
    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)