Spaces:
Sleeping
Sleeping
Update app.py by adding search_on_internet and generate_image tools
Browse files
app.py
CHANGED
|
@@ -18,6 +18,58 @@ def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return
|
|
| 18 |
"""
|
| 19 |
return "What magic will you build ?"
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
@tool
|
| 22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 23 |
"""A tool that fetches the current local time in a specified timezone.
|
|
@@ -40,10 +92,10 @@ final_answer = FinalAnswerTool()
|
|
| 40 |
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
| 41 |
|
| 42 |
model = HfApiModel(
|
| 43 |
-
max_tokens=2096,
|
| 44 |
-
temperature=0.5,
|
| 45 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
|
| 46 |
-
custom_role_conversions=None,
|
| 47 |
)
|
| 48 |
|
| 49 |
|
|
@@ -55,7 +107,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 55 |
|
| 56 |
agent = CodeAgent(
|
| 57 |
model=model,
|
| 58 |
-
tools=[final_answer], ## add your tools here (don't remove final answer)
|
| 59 |
max_steps=6,
|
| 60 |
verbosity_level=1,
|
| 61 |
grammar=None,
|
|
|
|
| 18 |
"""
|
| 19 |
return "What magic will you build ?"
|
| 20 |
|
| 21 |
+
@tool
|
| 22 |
+
def search_on_internet(subject: str) -> str:
|
| 23 |
+
"""A tool that does research on internet on a specificied subject.
|
| 24 |
+
Args:
|
| 25 |
+
subject: A string reprensenting the object to research.
|
| 26 |
+
"""
|
| 27 |
+
try:
|
| 28 |
+
# 1. Instancier l'outil de recherche DuckDuckGo
|
| 29 |
+
search_tool = DuckDuckGoSearchTool()
|
| 30 |
+
|
| 31 |
+
# 2. Exécuter la recherche avec le sujet fourni
|
| 32 |
+
results = search_tool.run(subject)
|
| 33 |
+
|
| 34 |
+
# 3. Retourner les résultats de la recherche
|
| 35 |
+
return results
|
| 36 |
+
|
| 37 |
+
except Exception as e:
|
| 38 |
+
# 4. Gérer les erreurs et retourner un message explicatif
|
| 39 |
+
return f"Une erreur s'est produite lors de la recherche sur Internet : {e}"
|
| 40 |
+
|
| 41 |
+
from smolagents import load_tool, tool
|
| 42 |
+
|
| 43 |
+
# Charger l'outil une seule fois pour éviter de le recharger à chaque appel
|
| 44 |
+
# Cette ligne doit être placée en dehors de la fonction.
|
| 45 |
+
try:
|
| 46 |
+
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 47 |
+
except Exception as e:
|
| 48 |
+
# Gérer le cas où l'outil ne peut pas être chargé
|
| 49 |
+
print(f"Erreur lors du chargement de l'outil de génération d'images : {e}")
|
| 50 |
+
image_generation_tool = None
|
| 51 |
+
|
| 52 |
+
@tool
|
| 53 |
+
def generate_image(prompt: str) -> str:
|
| 54 |
+
"""A tool that generates an image from a text description.
|
| 55 |
+
Args:
|
| 56 |
+
prompt: A string representing the text description for the image to generate.
|
| 57 |
+
"""
|
| 58 |
+
if image_generation_tool is None:
|
| 59 |
+
return "L'outil de génération d'images n'a pas pu être chargé."
|
| 60 |
+
|
| 61 |
+
try:
|
| 62 |
+
# Appeler la méthode `run` de l'outil de génération d'images avec le prompt
|
| 63 |
+
result = image_generation_tool.run(prompt=prompt)
|
| 64 |
+
|
| 65 |
+
# L'outil retourne généralement un dictionnaire ou une chaîne de caractères
|
| 66 |
+
# qui peut contenir un lien vers l'image générée.
|
| 67 |
+
return result
|
| 68 |
+
|
| 69 |
+
except Exception as e:
|
| 70 |
+
# Gérer les erreurs et retourner un message explicatif
|
| 71 |
+
return f"Une erreur s'est produite lors de la génération de l'image : {e}"
|
| 72 |
+
|
| 73 |
@tool
|
| 74 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 75 |
"""A tool that fetches the current local time in a specified timezone.
|
|
|
|
| 92 |
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
| 93 |
|
| 94 |
model = HfApiModel(
|
| 95 |
+
max_tokens=2096,
|
| 96 |
+
temperature=0.5,
|
| 97 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
|
| 98 |
+
custom_role_conversions=None,
|
| 99 |
)
|
| 100 |
|
| 101 |
|
|
|
|
| 107 |
|
| 108 |
agent = CodeAgent(
|
| 109 |
model=model,
|
| 110 |
+
tools=[final_answer, search_on_internet, generate_image], ## add your tools here (don't remove final answer)
|
| 111 |
max_steps=6,
|
| 112 |
verbosity_level=1,
|
| 113 |
grammar=None,
|