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
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
import errno
import os
from bs4 import BeautifulSoup
import requests
from requests.exceptions import RequestException, Timeout, TooManyRedirects
import pandas as pd
import urllib
from yt_dlp import YoutubeDL
from yt_dlp.utils import DownloadError
import cv2
import numpy as np
import base64
from typing import Dict, List, Optional



def download_file(file_name: str) -> str:
    """
    Download a file from a given task ID and save it to the local filesystem.

    Args:
        file_name (str): The name of the file to be saved.

    Returns:
        str: A message indicating the result of the download operation.
    """
    task_id = file_name.split(".")[0].split("/")[-1]
    url = f"https://agents-course-unit4-scoring.hf.space/files/{task_id}"

    try:
        # Check if file_name already exists in the folder
        if os.path.exists(file_name):
            return f"File {file_name} already exists. No need to download again."

        # Download the file
        try:
            response = requests.get(url, timeout=10)
            response.raise_for_status()  # Raises an HTTPError for bad responses (4XX, 5XX)

            try:
                with open(file_name, 'wb') as file:
                    file.write(response.content)
                return f"Success downloading {file_name}"

            except IOError as e:
                if e.errno == errno.ENOSPC:
                    return f"Failed to save file {file_name}. No space left on the device."
                else:
                    return f"Failed to save file {file_name}. Error: {e}"

        except Timeout:
            return f"Request timed out while trying to download file {file_name}."
        except TooManyRedirects:
            return f"Too many redirects while trying to download file {file_name}."
        except RequestException as e:
            return f"Failed to download file {file_name}. Error: {e}"

    except Exception as e:
        return f"An unexpected error occurred: {e}"
    
def read_file(file_name: str) -> str:
    """
    Read the content of a file.

    Args:
        file_name (str): The name of the file to be read.

    Returns:
        str: The content of the file if successful, or an error message if failed.
    """
    try:
        # Check if the file exists
        if not os.path.exists(file_name):
            return f"File not found: {file_name}"
        
        # Check file extension
        extension = os.path.splitext(file_name)[1].lower()
        if extension not in ['.csv', '.xlsx', '.py', '.txt', '.md',]:
            return "Unsupported file format. Please provide a file with one of those format: csv, xlsx, py, txt, md."
        
        # Read the file using pandas
        try:
            if extension in ['.txt', '.py', '.md']:
                with open(file_name, 'r', encoding='utf-8') as file:
                    file_content = file.read()
            if extension == '.csv':
                file_content = pd.read_csv(file_name).to_string(index=False)
            elif extension == '.xlsx':
                file_content = pd.read_excel(file_name).to_string(index=False)

            return file_content

        except pd.errors.EmptyDataError:
            return "The file is empty."
        except pd.errors.ParserError:
            return "Error parsing the file. The file might be corrupted."
        except Exception as e:
            return f"Error reading file: {e}"

    except PermissionError:
        return f"Permission denied: Unable to access the file at {file_name}."
    except Exception as e:
        return f"An unexpected error occurred: {e}"
    
def sum_pandas_df_cols(df: pd.DataFrame, cols: list):
    return df[cols].sum().sum()
        
def download_yt_video(url: str) -> str:
    """
    Download a YouTube video based on its URL.

    Args:
        url (str): The YouTube URL.

    Returns:
        str: The name of the file downloaded or an error message.
    """
    # Options to download best video ≤ 480p + audio or fallback
    ydl_opts = {
        'format': 'bestvideo[height=480]/bestvideo[height<=480]+bestaudio/best[height<=480]',
        'outtmpl': '%(id)s.%(ext)s',
        'quiet': True,
        'no_warnings': True,
    }

    try:
        # Extract video ID safely from URL
        if 'v=' in url:
            video_id = url.split('v=')[-1].split('&')[0]
        else:
            # fallback to URL itself if no standard param found
            video_id = url.split('/')[-1]

        # Check for existing files with common extensions before downloading
        for ext in ('mp4', 'webm', 'mkv', 'flv'):
            file_path = f'{video_id}.{ext}'
            if os.path.exists(file_path):
                return file_path

        with YoutubeDL(ydl_opts) as ydl:
            info_dict = ydl.extract_info(url, download=True)
            file_name = ydl.prepare_filename(info_dict)
            print(f"Downloaded video: {file_name}")
            return file_name

    except DownloadError as de:
        return f"Download error: {de}"
    except Exception as e:
        return f"Unexpected error: {e}"
        
def extract_frames(video_path: str, interval: int=2, output_folder: str='extracted_frames') -> List[str]:
    """
    Extract frames from a .webm video and save them to a specified folder.
    If the frame is already existing, do not save it again but include it into the list of frames paths.

    Args:
        video_path (str): The path where the video is stored.
        interval (int): The time interval (in seconds) between 2 frames extraction in the video.
        output_folder (str): The folder where the extracted frames will be saved.

    Returns:
        List[str]: A list of paths to the saved frames (including the ones already existing).
    """
    # Create the output folder if it doesn't exist
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)

    # Open the video file
    cap = cv2.VideoCapture(video_path)
    if not cap.isOpened():
        raise ValueError("Could not open video file")

    # Get video properties
    fps = cap.get(cv2.CAP_PROP_FPS)
    frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
    duration = frame_count / fps

    frame_paths = []

    # Calculate the frame interval based on the time interval
    frame_interval = int(interval * fps)

    current_frame = 0
    while current_frame < frame_count:
        # Set the current frame position
        cap.set(cv2.CAP_PROP_POS_FRAMES, current_frame)

        # Read the frame
        ret, frame = cap.read()
        if not ret:
            break

        # Generate the output filename
        frame_time = current_frame / fps
        frame_filename = os.path.join(output_folder, f"frame_{frame_time:.2f}.jpg")

        # Check if the frame already exists
        if not os.path.exists(frame_filename):
            # Save the frame
            cv2.imwrite(frame_filename, frame)
            print(f"{frame_filename} saved")
        else:
            print(f"{frame_filename} already existing")

        # Add the frame path to the list
        frame_paths.append(frame_filename)

        # Move to the next frame interval
        current_frame += frame_interval

    # Release the video capture object
    cap.release()

    return frame_paths

def encode_image(image_path):
    """Encode the image to base64."""
    try:
        with open(image_path, "rb") as image_file:
            return base64.b64encode(image_file.read()).decode('utf-8')
    except FileNotFoundError:
        print(f"Error: The file {image_path} was not found.")
        return None
    except Exception as e:  # Added general exception handling
        print(f"Error: {e}")
        return None
    
def generate_prompt_for_video_frame_analysis(client, video_question: str, model="mistral-large-latest") -> str:
    chat_response = client.chat.complete(
    model=model,                   
    messages=
        [ 
            {
                "role": "system",
                "content": [
                    {"type": "text", "text":
                        """
                        You are a prompt engineer AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a prompt that can be provided as a user text input to a Large Language Model. DO NOT use any formating like **bold** or "quotation marks" to enclose YOUR FINAL ANSWER.
                        """
                    }
                    
                ]

            },
            {
                "role": "user",
                "content": [
                    {"type": "text", "text": 
                        f"""
                        To answer the question about the video, I'm going to extract each frame and send them one by one to an independent Large Language Model that has the ability to analyze an image. What prompt should be given to the model so that it can answer the original question? The prompt should be as faithful as possible to the original question, but adapted to an image rather than a video.\n
                        \n
                        <original_question>\n
                        {video_question}\n
                        </original_question>\n
                        """
                    }, 
                ],  
            },
        ]
    )
    return chat_response.choices[0].message.content.split('FINAL ANSWER: ')[-1]

def analyze_frame(client, question: str, base64_image: str, model="pixtral-12b-2409") -> str:
    chat_response = client.chat.complete(
        model=model,                   # Spécification du modèle à utiliser : pixtral-12b-2409 ou pixtral-large-latest
        messages=[                   
            {
                "role": "system", 
                "content": [
                    {
                        "type": "text",
                        "text": """
                            You are an image analyst AI assistant. I will ask you a question. Examine EVERY detail of the image, report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER].\n 
                            \n
                            If the question is about giving a number, YOUR FINAL ANSWER should only be the number asked.\n 
                            If the question is about giving a word or a group of words, YOUR FINAL ANSWER should only be the word or group of words.\n 
                            \n
                            Do not use any formating like **bold** or "quotation marks" to enclose YOUR FINAL ANSWER.
                        """
                    }
                ]
            },
            {
                "role": "user", 
                "content": [
                    {
                        "type": "text", 
                        "text": question
                    }, 
                    {
                        "type": "image_url", 
                        "image_url": f"data:image/jpeg;base64,{base64_image}"
                    }
                ],  
            },
        ]
    )
    return chat_response.choices[0].message.content.split('FINAL ANSWER: ')[-1]

def get_response_from_frames_analysis(client, video_question: str, frames_answers: list, model="mistral-small-latest") -> str:
    chat_response = client.chat.complete(
        model=model,                   
        messages=[ 
            {
                "role": "system",
                "content": [
                    {"type": "text", "text":
                        """
                        You are a general AI assistant. I will ask you a question about a video and provide you with a list of possible answers. Since I can't analyze the video directly, I've broken it down into frames. The list contains the answers to the question I asked for each frame of the video. Your aim is to find the most likely answer to the question for the whole video. Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. Do not use any formating like **bold** or "quotation marks" to enclose YOUR FINAL ANSWER.
                        """
                    }
                    
                ]
            },
            {
                "role": "user",
                "content": [
                    {"type": "text", "text": 
                        f"""
                        {video_question}\n
                        \n
                        <List_of_answers_for_each_frame>\n
                        {frames_answers}\n
                        </List_of_answers_for_each_frame>\n
                        """
                    }, 
                ],  
            },
        ]
    )
    return chat_response.choices[0].message.content.split('FINAL ANSWER: ')[-1]


## Not possible to install PyTorch 2.6 on my config, which is requiered to use CLIP Model and Processor :
# MacBook Pro 2017 ; MacOS 13.7.6 ; 2,3 GHz Intel Core i5 double cœur ; Intel Iris Plus Graphics 640 1536 Mo ; 8 Go 2133 MHz LPDDR3

# import torch
# from transformers import CLIPProcessor, CLIPModel
# from sklearn.cluster import DBSCAN

# def embed_frame(img, model, processor, device='gpu'):
#     inputs = processor(images=img, return_tensors="pt").to(device)
#     with torch.no_grad():
#         feat = model.get_image_features(**inputs)
#     return feat.cpu().numpy().squeeze()

# def count_species(embeddings, eps=0.5, min_samples=2):
#     km = DBSCAN(eps=eps, min_samples=min_samples)
#     labels = km.fit_predict(embeddings)
#     return len(set(l for l in labels if l != -1))

# device = 'cpu'
# model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32").to(device)
# processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")

# embedded_image = embed_frame(img=test_image, model=model)

# nb_species = count_species(embedded_image)

def transcript_audio_file(client, file_path: str) -> str:
    with open(file_path, "rb") as file:

        # Create a transcription of the audio file

        transcription = client.audio.transcriptions.create(
            file=file, # Required audio file
            model="whisper-large-v3-turbo", # Required model to use for transcription
            language="en",  # Optional
        )
    return transcription

def search_wikipedia(query: str, lang_tag: str) -> Optional[str]:
    search_url = f"https://{lang_tag}.wikipedia.org/w/api.php?action=query&list=search&srsearch={query}&format=json"
    response = requests.get(search_url, timeout=10)
    response.raise_for_status()
    data = response.json()

    search_results = data.get('query', {}).get('search', [])
    title = search_results[0]['title'] if search_results else None

    if not title:
        return "No relevant Wikipedia page found."
    return title.replace(' ', '_')

def fetch_page_content(url: str) -> Optional[str]:
    page_resp = requests.get(url)
    if page_resp.status_code != 200:
        return None

    content_soup = BeautifulSoup(page_resp.text, 'html.parser')
    content_div = content_soup.find("div", id="mw-content-text")
    return content_div.get_text(separator="\n", strip=True) if content_div else None


def get_history_versions(page_title: str, lang_tag: str) -> List[Dict[str, str]]:
    history_url = f"https://{lang_tag}.wikipedia.org/w/index.php?title={page_title}&action=history&limit=100"
    history_resp = requests.get(history_url)
    if history_resp.status_code != 200:
        return []

    history_soup = BeautifulSoup(history_resp.text, 'html.parser')

    history_items = history_soup.find_all("a", class_="mw-changeslist-date")
    if not history_items:
        return []

    versions = []
    for item in history_items:
        if item and 'oldid=' in item['href']:
            versions.append({
                "id": item['href'].split('oldid=')[-1],
                "date": item.get_text()
            })
    return versions


def select_historical_version(client, versions: List[Dict[str, str]], date: str) -> Optional[str]:
    formatted_versions = "\n".join([f"{v['date']} -> {v['id']}" for v in versions])
    prompt = f"""
        You are an AI assistant. I am trying to retrieve the most relevant version of a Wikipedia page for the date described as: "{date}". 

        Here is a list of available version timestamps and their IDs:
        {formatted_versions}

        Which ID best matches the given date? Return ONLY the ID.
        """

    resp = client.chat.complete(
        model="mistral-small-latest",
        messages=[
            {"role": "user", "content": prompt}
        ]
    )

    selected_id = resp.choices[0].message.content.strip()
    return selected_id