Spaces:
Sleeping
Sleeping
add tools to find a velib
Browse filesThe different tools can find the Vélib station and then obtain the number of Vélib available.
app.py
CHANGED
|
@@ -1,82 +1,97 @@
|
|
| 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 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
"""
|
| 19 |
-
return "What magic will you build ?"
|
| 20 |
|
| 21 |
-
|
| 22 |
-
def
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
@tool
|
| 43 |
-
def
|
| 44 |
-
"""A tool that fetches the
|
| 45 |
Args:
|
| 46 |
-
|
| 47 |
"""
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
# Get current time in that timezone
|
| 52 |
-
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 53 |
-
return f"The current local time in {timezone} is: {local_time}"
|
| 54 |
-
except Exception as e:
|
| 55 |
-
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 56 |
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
-
#
|
| 61 |
-
|
| 62 |
|
| 63 |
model = HfApiModel(
|
| 64 |
-
max_tokens=2096,
|
| 65 |
-
temperature=0.5,
|
| 66 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct'
|
| 67 |
-
custom_role_conversions=None,
|
| 68 |
)
|
| 69 |
|
| 70 |
-
|
| 71 |
# Import tool from Hub
|
| 72 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 73 |
|
| 74 |
with open("prompts.yaml", 'r') as stream:
|
| 75 |
prompt_templates = yaml.safe_load(stream)
|
| 76 |
-
|
| 77 |
agent = CodeAgent(
|
| 78 |
model=model,
|
| 79 |
-
tools=[final_answer],
|
| 80 |
max_steps=6,
|
| 81 |
verbosity_level=1,
|
| 82 |
grammar=None,
|
|
@@ -86,5 +101,4 @@ agent = CodeAgent(
|
|
| 86 |
prompt_templates=prompt_templates
|
| 87 |
)
|
| 88 |
|
| 89 |
-
|
| 90 |
GradioUI(agent).launch()
|
|
|
|
| 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 |
+
from geopy.distance import geodesic # Pour le calcul des distances
|
| 8 |
|
| 9 |
from Gradio_UI import GradioUI
|
| 10 |
|
| 11 |
+
# Fonction pour obtenir les coordonnées GPS d'une adresse
|
| 12 |
+
def geocode_address(address):
|
| 13 |
+
response = requests.get(f"https://api-adresse.data.gouv.fr/search/?q={address}")
|
| 14 |
+
data = response.json()
|
| 15 |
+
if not data['features']:
|
| 16 |
+
return None, None
|
| 17 |
+
coordinates = data['features'][0]['geometry']['coordinates']
|
| 18 |
+
return coordinates[1], coordinates[0] # Retourne latitude, longitude
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
# Fonction pour récupérer toutes les stations Vélib'
|
| 21 |
+
def get_stations():
|
| 22 |
+
response = requests.get("https://velib-metropole-opendata.smovengo.cloud/opendata/Velib_Metropole/station_information.json")
|
| 23 |
+
data = response.json()
|
| 24 |
+
return data['data']['stations']
|
| 25 |
+
|
| 26 |
+
# Trouver la station Vélib' la plus proche d'une adresse donnée
|
| 27 |
+
def find_nearest_station(address):
|
| 28 |
+
user_lat, user_lon = geocode_address(address)
|
| 29 |
+
if user_lat is None or user_lon is None:
|
| 30 |
+
return None
|
| 31 |
+
|
| 32 |
+
stations = get_stations()
|
| 33 |
+
min_distance = float('inf')
|
| 34 |
+
nearest_station = None
|
| 35 |
+
|
| 36 |
+
for station in stations:
|
| 37 |
+
station_coords = (station['lat'], station['lon'])
|
| 38 |
+
distance = geodesic((user_lat, user_lon), station_coords).meters
|
| 39 |
+
if distance < min_distance:
|
| 40 |
+
min_distance = distance
|
| 41 |
+
nearest_station = station
|
| 42 |
|
| 43 |
+
return nearest_station
|
| 44 |
+
|
| 45 |
+
# Fonction pour récupérer le statut d'une station
|
| 46 |
+
def get_station_status(station_id):
|
| 47 |
+
response = requests.get("https://velib-metropole-opendata.smovengo.cloud/opendata/Velib_Metropole/station_status.json")
|
| 48 |
+
data = response.json()
|
| 49 |
+
for station in data['data']['stations']:
|
| 50 |
+
if station['station_id'] == station_id:
|
| 51 |
+
return station
|
| 52 |
+
return None
|
| 53 |
+
|
| 54 |
+
# Outil final qui donne le nombre de vélos disponibles à une adresse donnée
|
| 55 |
@tool
|
| 56 |
+
def get_bike_availability(address: str) -> str:
|
| 57 |
+
"""A tool that fetches the number of available Velib' bikes at the nearest station to a given address.
|
| 58 |
Args:
|
| 59 |
+
address: The address where the user wants to find a Velib' station.
|
| 60 |
"""
|
| 61 |
+
nearest_station = find_nearest_station(address)
|
| 62 |
+
if not nearest_station:
|
| 63 |
+
return "Aucune station Vélib' trouvée à proximité."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
+
status = get_station_status(nearest_station['station_id'])
|
| 66 |
+
if not status:
|
| 67 |
+
return "Impossible de récupérer le statut de la station."
|
| 68 |
|
| 69 |
+
mechanical_bikes = status['num_bikes_available_types'][0].get('mechanical', 0)
|
| 70 |
+
ebikes = status['num_bikes_available_types'][1].get('ebike', 0)
|
| 71 |
+
|
| 72 |
+
return (f"🚲 Station la plus proche : {nearest_station['name']}\n"
|
| 73 |
+
f"🔧 Vélos mécaniques disponibles : {mechanical_bikes}\n"
|
| 74 |
+
f"⚡ Vélos électriques disponibles : {ebikes}")
|
| 75 |
|
| 76 |
+
# Ajout du tool à l'agent
|
| 77 |
+
final_answer = FinalAnswerTool()
|
| 78 |
|
| 79 |
model = HfApiModel(
|
| 80 |
+
max_tokens=2096,
|
| 81 |
+
temperature=0.5,
|
| 82 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
| 83 |
+
custom_role_conversions=None,
|
| 84 |
)
|
| 85 |
|
|
|
|
| 86 |
# Import tool from Hub
|
| 87 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 88 |
|
| 89 |
with open("prompts.yaml", 'r') as stream:
|
| 90 |
prompt_templates = yaml.safe_load(stream)
|
| 91 |
+
|
| 92 |
agent = CodeAgent(
|
| 93 |
model=model,
|
| 94 |
+
tools=[final_answer, get_bike_availability], # Ajout du nouvel outil ici
|
| 95 |
max_steps=6,
|
| 96 |
verbosity_level=1,
|
| 97 |
grammar=None,
|
|
|
|
| 101 |
prompt_templates=prompt_templates
|
| 102 |
)
|
| 103 |
|
|
|
|
| 104 |
GradioUI(agent).launch()
|