File size: 2,506 Bytes
12ed90e
daf1b42
 
6aae614
9b5b26a
 
fc5a09d
9b5b26a
fc5a09d
0653303
 
 
ab284f7
0653303
fc5a09d
 
 
 
 
 
daf1b42
fc5a09d
 
daf1b42
fc5a09d
 
 
0653303
0a32d9c
 
 
 
 
 
 
 
0653303
fc5a09d
 
 
 
 
0a32d9c
12ed90e
9b5b26a
 
 
0653303
ab284f7
0a32d9c
 
 
 
 
 
0653303
9b5b26a
 
 
 
 
daf1b42
8c01ffb
0a32d9c
fc5a09d
8c01ffb
e121372
2742d1d
54f2aef
48e6cdf
13d500a
8c01ffb
ab284f7
8c01ffb
ab284f7
861422e
 
ab284f7
8c01ffb
8fe992b
ab284f7
8c01ffb
 
861422e
8fe992b
 
ab284f7
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
from smolagents import CodeAgent, tool, load_tool, HfApiModel
from youtube_transcript_api import YouTubeTranscriptApi
import datetime, pytz, yaml, re
from tools.final_answer import FinalAnswerTool
from Gradio_UI import GradioUI

# ---------- TOOLS ----------
@tool
def get_transcript(url: str) -> str:
    """
    Télécharge la transcription (FR ou EN) d’une vidéo YouTube.
    Args:
        url: lien complet YouTube
    """
    video_id = re.search(r"(?:v=|youtu\.be/)([^&\n?#]+)", url)
    if not video_id:
        return "Impossible de détecter l’ID vidéo."
    video_id = video_id.group(1)

    try:
        transcript = YouTubeTranscriptApi.get_transcript(video_id, languages=['fr', 'en'])
        return " ".join(seg["text"] for seg in transcript)
    except Exception as e:
        return f"Erreur transcript : {e}"

@tool
def summarize(text: str, max_chars: int = 1500) -> str:
    """
    Résume le texte fourni en 8-10 bullet points clairs.

    Args:
        text (str): Le texte à résumer.
        max_chars (int, optional): Nombre maximum de caractères à considérer dans le texte. Par défaut : 1500.

    Returns:
        str: Un prompt de résumé à envoyer au modèle.
    """
    snippet = text[:max_chars]
    prompt = (
        "Résume le texte suivant en 8‑10 bullet points clairs :\n\n"
        f"{snippet}\n\n# Résumé :"
    )
    return prompt


@tool
def get_current_time_in_timezone(timezone: str) -> str:
    """
    Renvoie l’heure actuelle dans un fuseau horaire donné.

    Args:
        timezone (str): Nom du fuseau horaire (ex: 'Europe/Paris', 'America/New_York').

    Returns:
        str: Heure locale actuelle dans le fuseau horaire spécifié.
    """
    try:
        tz = pytz.timezone(timezone)
        local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
        return f"The current local time in {timezone} is: {local_time}"
    except Exception as e:
        return f"Error fetching time: {str(e)}"


# ---------- AGENT ----------

model = HfApiModel(
    model_id="mistralai/Mistral-7B-Instruct-v0.1",
    temperature=0.5,
    max_tokens=2048,
)

final_answer = FinalAnswerTool()

# Charge les prompts
with open("prompts.yaml", 'r') as stream:
    prompt_templates = yaml.safe_load(stream)

agent = CodeAgent(
    model=model,
    tools=[final_answer, get_transcript, summarize, get_current_time_in_timezone],
    max_steps=6,
    verbosity_level=1,
    prompt_templates=prompt_templates
)

GradioUI(agent).launch()