Update app.py
#145
by Guida - opened
app.py
CHANGED
|
@@ -1,67 +1,81 @@
|
|
| 1 |
-
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
| 2 |
import datetime
|
| 3 |
-
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
|
|
|
| 7 |
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
| 10 |
-
#
|
| 11 |
@tool
|
| 12 |
-
def
|
| 13 |
-
|
| 14 |
-
|
| 15 |
Args:
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
| 18 |
"""
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
|
|
|
|
|
|
| 21 |
@tool
|
| 22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 23 |
-
"""
|
| 24 |
-
Args:
|
| 25 |
-
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 26 |
-
"""
|
| 27 |
try:
|
| 28 |
-
# Create timezone object
|
| 29 |
tz = pytz.timezone(timezone)
|
| 30 |
-
# Get current time in that timezone
|
| 31 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 32 |
-
return f"
|
| 33 |
except Exception as e:
|
| 34 |
-
return f"
|
| 35 |
-
|
| 36 |
|
|
|
|
| 37 |
final_answer = FinalAnswerTool()
|
| 38 |
|
| 39 |
-
#
|
| 40 |
-
#
|
| 41 |
-
|
| 42 |
model = HfApiModel(
|
| 43 |
-
max_tokens=2096,
|
| 44 |
-
temperature=0.5,
|
| 45 |
-
model_id='
|
| 46 |
-
custom_role_conversions=None,
|
| 47 |
)
|
| 48 |
|
| 49 |
|
| 50 |
-
#
|
| 51 |
-
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 52 |
|
| 53 |
with open("prompts.yaml", 'r') as stream:
|
| 54 |
prompt_templates = yaml.safe_load(stream)
|
| 55 |
-
|
| 56 |
agent = CodeAgent(
|
| 57 |
model=model,
|
| 58 |
-
tools=[final_answer
|
| 59 |
max_steps=6,
|
| 60 |
verbosity_level=1,
|
| 61 |
grammar=None,
|
| 62 |
planning_interval=None,
|
| 63 |
-
name=
|
| 64 |
-
description=
|
| 65 |
prompt_templates=prompt_templates
|
| 66 |
)
|
| 67 |
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
| 2 |
import datetime
|
| 3 |
+
import requests # Important pour notre outil de vérification d'URL
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
+
from urllib.parse import urlparse # Pour valider le format de l'URL
|
| 8 |
|
| 9 |
from Gradio_UI import GradioUI
|
| 10 |
|
| 11 |
+
# --- Outil de vérification d'URL ---
|
| 12 |
@tool
|
| 13 |
+
def check_url(url: str) -> str:
|
| 14 |
+
"""Vérifie si une URL est valide et accessible.
|
| 15 |
+
|
| 16 |
Args:
|
| 17 |
+
url: L'URL à vérifier.
|
| 18 |
+
|
| 19 |
+
Returns:
|
| 20 |
+
Un message indiquant si l'URL est valide et accessible, ou un message d'erreur.
|
| 21 |
"""
|
| 22 |
+
try:
|
| 23 |
+
# Validation du format de l'URL
|
| 24 |
+
result = urlparse(url)
|
| 25 |
+
if not all([result.scheme, result.netloc]):
|
| 26 |
+
return "Erreur : URL invalide (format incorrect)."
|
| 27 |
+
|
| 28 |
+
# Tentative de connexion
|
| 29 |
+
response = requests.get(url, timeout=5) # Timeout de 5 secondes
|
| 30 |
+
response.raise_for_status() # Lève une exception si le code de statut est >= 400
|
| 31 |
+
return f"L'URL {url} est valide et accessible."
|
| 32 |
+
|
| 33 |
+
except requests.exceptions.RequestException as e:
|
| 34 |
+
return f"Erreur : L'URL {url} n'est pas accessible : {e}"
|
| 35 |
+
except Exception as e:
|
| 36 |
+
return f"Erreur lors de la vérification de l'URL : {e}"
|
| 37 |
+
|
| 38 |
|
| 39 |
+
|
| 40 |
+
# --- Outil pour obtenir l'heure --- (conservé du code original)
|
| 41 |
@tool
|
| 42 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 43 |
+
"""Récupère l'heure locale actuelle dans un fuseau horaire spécifié."""
|
|
|
|
|
|
|
|
|
|
| 44 |
try:
|
|
|
|
| 45 |
tz = pytz.timezone(timezone)
|
|
|
|
| 46 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 47 |
+
return f"L'heure actuelle à {timezone} est : {local_time}"
|
| 48 |
except Exception as e:
|
| 49 |
+
return f"Erreur : {e}"
|
|
|
|
| 50 |
|
| 51 |
+
# --- Configuration de l'agent ---
|
| 52 |
final_answer = FinalAnswerTool()
|
| 53 |
|
| 54 |
+
# Utilisation d'un modèle plus accessible pour les débutants, et potentiellement moins chargé.
|
| 55 |
+
# Si problèmes de performance, essayez 'Qwen/Qwen1.5-1.8B-Chat' ou un endpoint Hugging Face.
|
|
|
|
| 56 |
model = HfApiModel(
|
| 57 |
+
max_tokens=2096,
|
| 58 |
+
temperature=0.5,
|
| 59 |
+
model_id='mistralai/Mistral-7B-Instruct-v0.2', # Mistral est un bon compromis
|
| 60 |
+
custom_role_conversions=None,
|
| 61 |
)
|
| 62 |
|
| 63 |
|
| 64 |
+
# --- Chargement des outils ---
|
| 65 |
+
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True) #conserver l'outil de generation d'image
|
| 66 |
|
| 67 |
with open("prompts.yaml", 'r') as stream:
|
| 68 |
prompt_templates = yaml.safe_load(stream)
|
| 69 |
+
|
| 70 |
agent = CodeAgent(
|
| 71 |
model=model,
|
| 72 |
+
tools=[final_answer, check_url, get_current_time_in_timezone, DuckDuckGoSearchTool(), image_generation_tool], # Ajout de notre outil!
|
| 73 |
max_steps=6,
|
| 74 |
verbosity_level=1,
|
| 75 |
grammar=None,
|
| 76 |
planning_interval=None,
|
| 77 |
+
name="URLCheckerAgent", # Nom plus descriptif
|
| 78 |
+
description="Un agent qui vérifie les URLs et donne l'heure.", # Description plus claire
|
| 79 |
prompt_templates=prompt_templates
|
| 80 |
)
|
| 81 |
|