Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
from smolagents import CodeAgent, HfApiModel, load_tool, tool
|
|
|
|
| 2 |
import datetime
|
| 3 |
import pytz
|
| 4 |
import yaml
|
|
@@ -7,8 +8,11 @@ import yfinance as yf
|
|
| 7 |
from Gradio_UI import GradioUI
|
| 8 |
import json
|
| 9 |
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
#
|
| 12 |
@tool
|
| 13 |
def get_stock_price(ticker: str) -> str:
|
| 14 |
"""Un outil qui retourne le prix actuel d’un actif coté en bourse au format JSON.
|
|
@@ -27,8 +31,22 @@ def get_stock_price(ticker: str) -> str:
|
|
| 27 |
except Exception as e:
|
| 28 |
return json.dumps({"error": f"Impossible de récupérer le cours pour {ticker}: {str(e)}"})
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
#
|
| 32 |
@tool
|
| 33 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 34 |
"""Un outil qui retourne l’heure locale actuelle dans un fuseau horaire donné.
|
|
@@ -42,13 +60,12 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 42 |
except Exception as e:
|
| 43 |
return f"Erreur pour le fuseau horaire '{timezone}': {str(e)}"
|
| 44 |
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
# ---- Initialisation ----
|
| 47 |
final_answer = FinalAnswerTool()
|
| 48 |
|
| 49 |
-
# Ajout de l'outil de recherche internet (DuckDuckGo)
|
| 50 |
-
web_search = load_tool("duckduckgo_search", trust_remote_code=True)
|
| 51 |
-
|
| 52 |
model = HfApiModel(
|
| 53 |
max_tokens=2096,
|
| 54 |
temperature=0.5,
|
|
@@ -67,7 +84,7 @@ agent = CodeAgent(
|
|
| 67 |
final_answer,
|
| 68 |
get_stock_price,
|
| 69 |
get_current_time_in_timezone,
|
| 70 |
-
web_search
|
| 71 |
],
|
| 72 |
max_steps=6,
|
| 73 |
verbosity_level=1,
|
|
@@ -78,5 +95,5 @@ agent = CodeAgent(
|
|
| 78 |
prompt_templates=prompt_templates
|
| 79 |
)
|
| 80 |
|
| 81 |
-
#
|
| 82 |
GradioUI(agent).launch()
|
|
|
|
| 1 |
from smolagents import CodeAgent, HfApiModel, load_tool, tool
|
| 2 |
+
from duckduckgo_search import DDGS
|
| 3 |
import datetime
|
| 4 |
import pytz
|
| 5 |
import yaml
|
|
|
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
import json
|
| 10 |
|
| 11 |
+
#---------------------------------------------------------------------------
|
| 12 |
+
#---------------------------------------------------------------------------tools
|
| 13 |
+
#---------------------------------------------------------------------------
|
| 14 |
|
| 15 |
+
#stock
|
| 16 |
@tool
|
| 17 |
def get_stock_price(ticker: str) -> str:
|
| 18 |
"""Un outil qui retourne le prix actuel d’un actif coté en bourse au format JSON.
|
|
|
|
| 31 |
except Exception as e:
|
| 32 |
return json.dumps({"error": f"Impossible de récupérer le cours pour {ticker}: {str(e)}"})
|
| 33 |
|
| 34 |
+
#web search
|
| 35 |
+
@tool
|
| 36 |
+
def web_search(query: str, max_results: int = 5) -> str:
|
| 37 |
+
"""Recherche des informations sur internet via DuckDuckGo et retourne une liste de résultats JSON.
|
| 38 |
+
Args:
|
| 39 |
+
query: La requête de recherche.
|
| 40 |
+
max_results: Nombre maximum de résultats à retourner.
|
| 41 |
+
"""
|
| 42 |
+
try:
|
| 43 |
+
with DDGS() as ddgs:
|
| 44 |
+
results = list(ddgs.text(query, max_results=max_results))
|
| 45 |
+
return json.dumps(results, ensure_ascii=False, indent=2)
|
| 46 |
+
except Exception as e:
|
| 47 |
+
return json.dumps({"error": str(e)})
|
| 48 |
|
| 49 |
+
#timezone
|
| 50 |
@tool
|
| 51 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 52 |
"""Un outil qui retourne l’heure locale actuelle dans un fuseau horaire donné.
|
|
|
|
| 60 |
except Exception as e:
|
| 61 |
return f"Erreur pour le fuseau horaire '{timezone}': {str(e)}"
|
| 62 |
|
| 63 |
+
#---------------------------------------------------------------------------
|
| 64 |
+
#---------------------------------------------------------------------------init
|
| 65 |
+
#---------------------------------------------------------------------------
|
| 66 |
|
|
|
|
| 67 |
final_answer = FinalAnswerTool()
|
| 68 |
|
|
|
|
|
|
|
|
|
|
| 69 |
model = HfApiModel(
|
| 70 |
max_tokens=2096,
|
| 71 |
temperature=0.5,
|
|
|
|
| 84 |
final_answer,
|
| 85 |
get_stock_price,
|
| 86 |
get_current_time_in_timezone,
|
| 87 |
+
web_search
|
| 88 |
],
|
| 89 |
max_steps=6,
|
| 90 |
verbosity_level=1,
|
|
|
|
| 95 |
prompt_templates=prompt_templates
|
| 96 |
)
|
| 97 |
|
| 98 |
+
#---------------------------------------------------------------------------main
|
| 99 |
GradioUI(agent).launch()
|