Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,72 +1,20 @@
|
|
|
|
|
|
|
|
| 1 |
import requests
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
from
|
| 5 |
-
|
| 6 |
-
class Tool:
|
| 7 |
-
"""
|
| 8 |
-
Représente un outil réutilisable pour l'agent.
|
| 9 |
-
|
| 10 |
-
Attributs:
|
| 11 |
-
name (str): Nom descriptif de l'outil.
|
| 12 |
-
description (str): Description détaillée de l'outil.
|
| 13 |
-
func (Callable): La fonction que l'outil exécute.
|
| 14 |
-
arguments (List[str]): Liste des arguments requis par la fonction.
|
| 15 |
-
outputs (Union[str, List[str]]): Description des sorties produites par l'outil.
|
| 16 |
-
"""
|
| 17 |
-
def __init__(self, name: str, description: str, func: Callable, arguments: List[str], outputs: Union[str, List[str]]):
|
| 18 |
-
self.name = name
|
| 19 |
-
self.description = description
|
| 20 |
-
self.func = func
|
| 21 |
-
self.arguments = arguments
|
| 22 |
-
self.outputs = outputs
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
return self.func(*args, **kwargs)
|
| 27 |
-
|
| 28 |
-
def tool(name: str = None, description: str = "", arguments: List[str] = None, outputs: Union[str, List[str]] = None):
|
| 29 |
-
"""
|
| 30 |
-
Décorateur pour transformer une fonction en instance de Tool.
|
| 31 |
-
|
| 32 |
-
Args:
|
| 33 |
-
name (str): Nom de l'outil. Si None, le nom de la fonction sera utilisé.
|
| 34 |
-
description (str): Description détaillée de l'outil.
|
| 35 |
-
arguments (List[str]): Liste des arguments requis par la fonction.
|
| 36 |
-
outputs (Union[str, List[str]]): Description des sorties produites par l'outil.
|
| 37 |
-
|
| 38 |
-
Returns:
|
| 39 |
-
Callable: Une instance de Tool contenant la fonction décorée.
|
| 40 |
-
"""
|
| 41 |
-
def decorator(func: Callable) -> Tool:
|
| 42 |
-
tool_name = name if name is not None else func.__name__
|
| 43 |
-
tool_arguments = arguments if arguments is not None else []
|
| 44 |
-
tool_outputs = outputs if outputs is not None else ""
|
| 45 |
-
@wraps(func)
|
| 46 |
-
def wrapper(*args, **kwargs):
|
| 47 |
-
return func(*args, **kwargs)
|
| 48 |
-
return Tool(
|
| 49 |
-
name=tool_name,
|
| 50 |
-
description=description or func.__doc__,
|
| 51 |
-
func=func,
|
| 52 |
-
arguments=tool_arguments,
|
| 53 |
-
outputs=tool_outputs
|
| 54 |
-
)
|
| 55 |
-
return decorator
|
| 56 |
-
|
| 57 |
-
@tool(
|
| 58 |
-
name="RechercheGoogle",
|
| 59 |
-
description="Outil pour effectuer une recherche sur Google par mot-clé et extraire le premier résultat.",
|
| 60 |
-
arguments=["query (str)"],
|
| 61 |
-
outputs="dict (contenant 'url' et 'titre')"
|
| 62 |
-
)
|
| 63 |
-
def recherche(query: str) -> dict:
|
| 64 |
-
"""
|
| 65 |
-
Recherche sur Google un mot-clé donné.
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
Args:
|
| 68 |
-
query
|
| 69 |
-
|
| 70 |
Returns:
|
| 71 |
dict: Dictionnaire contenant l'URL et le titre du premier résultat.
|
| 72 |
"""
|
|
@@ -75,30 +23,75 @@ def recherche(query: str) -> dict:
|
|
| 75 |
"User-Agent": (
|
| 76 |
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
|
| 77 |
"AppleWebKit/537.36 (KHTML, like Gecko) "
|
| 78 |
-
"Chrome/
|
| 79 |
)
|
| 80 |
}
|
| 81 |
-
params = {"q": query}
|
| 82 |
|
| 83 |
response = requests.get(url_search, params=params, headers=headers)
|
| 84 |
if response.status_code != 200:
|
| 85 |
raise Exception("Erreur lors de la requête vers Google")
|
| 86 |
|
| 87 |
-
soup = BeautifulSoup(response.text,
|
| 88 |
-
#
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
| 97 |
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
try:
|
| 101 |
-
|
| 102 |
-
|
|
|
|
| 103 |
except Exception as e:
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
from bs4 import BeautifulSoup
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
@tool
|
| 12 |
+
def recherche_google(query: str) -> dict:
|
| 13 |
+
"""Recherche sur Google un mot-clé donné.
|
| 14 |
+
|
| 15 |
Args:
|
| 16 |
+
query: Le mot-clé à rechercher.
|
| 17 |
+
|
| 18 |
Returns:
|
| 19 |
dict: Dictionnaire contenant l'URL et le titre du premier résultat.
|
| 20 |
"""
|
|
|
|
| 23 |
"User-Agent": (
|
| 24 |
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
|
| 25 |
"AppleWebKit/537.36 (KHTML, like Gecko) "
|
| 26 |
+
"Chrome/98.0.4758.102 Safari/537.36"
|
| 27 |
)
|
| 28 |
}
|
| 29 |
+
params = {"q": query, "hl": "fr"}
|
| 30 |
|
| 31 |
response = requests.get(url_search, params=params, headers=headers)
|
| 32 |
if response.status_code != 200:
|
| 33 |
raise Exception("Erreur lors de la requête vers Google")
|
| 34 |
|
| 35 |
+
soup = BeautifulSoup(response.text, "html.parser")
|
| 36 |
+
# Recherche des blocs de résultats ; la structure de Google pouvant varier,
|
| 37 |
+
# nous itérons sur les résultats pour trouver un lien valide.
|
| 38 |
+
results = soup.find_all("div", class_="g")
|
| 39 |
+
for result in results:
|
| 40 |
+
a_tag = result.find("a")
|
| 41 |
+
if a_tag:
|
| 42 |
+
url_result = a_tag.get("href")
|
| 43 |
+
titre = a_tag.get_text(strip=True)
|
| 44 |
+
if url_result and titre:
|
| 45 |
+
return {"url": url_result, "titre": titre}
|
| 46 |
+
return {"url": None, "titre": None}
|
| 47 |
|
| 48 |
+
@tool
|
| 49 |
+
def my_custom_tool(arg1: str, arg2: int) -> str:
|
| 50 |
+
"""A tool that does nothing yet
|
| 51 |
+
Args:
|
| 52 |
+
arg1: the first argument
|
| 53 |
+
arg2: the second argument
|
| 54 |
+
"""
|
| 55 |
+
return "What magic will you build ?"
|
| 56 |
+
|
| 57 |
+
@tool
|
| 58 |
+
def get_current_time_in_timezone(timezone: str) -> str:
|
| 59 |
+
"""A tool that fetches the current local time in a specified timezone.
|
| 60 |
+
|
| 61 |
+
Args:
|
| 62 |
+
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 63 |
+
"""
|
| 64 |
try:
|
| 65 |
+
tz = pytz.timezone(timezone)
|
| 66 |
+
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 67 |
+
return f"The current local time in {timezone} is: {local_time}"
|
| 68 |
except Exception as e:
|
| 69 |
+
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 70 |
+
|
| 71 |
+
final_answer = FinalAnswerTool()
|
| 72 |
+
model = HfApiModel(
|
| 73 |
+
max_tokens=2096,
|
| 74 |
+
temperature=0.5,
|
| 75 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
| 76 |
+
custom_role_conversions=None,
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
# Import tool from Hub (exemple d'outil de génération d'image)
|
| 80 |
+
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 81 |
+
|
| 82 |
+
with open("prompts.yaml", 'r') as stream:
|
| 83 |
+
prompt_templates = yaml.safe_load(stream)
|
| 84 |
+
|
| 85 |
+
agent = CodeAgent(
|
| 86 |
+
model=model,
|
| 87 |
+
tools=[final_answer, recherche_google, my_custom_tool, get_current_time_in_timezone],
|
| 88 |
+
max_steps=6,
|
| 89 |
+
verbosity_level=1,
|
| 90 |
+
grammar=None,
|
| 91 |
+
planning_interval=None,
|
| 92 |
+
name=None,
|
| 93 |
+
description=None,
|
| 94 |
+
prompt_templates=prompt_templates
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
GradioUI(agent).launch()
|