Spaces:
Sleeping
Sleeping
| from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool | |
| import datetime | |
| import requests | |
| import pytz | |
| import yaml | |
| from tools.final_answer import FinalAnswerTool | |
| from geopy.distance import geodesic # Pour le calcul des distances | |
| from Gradio_UI import GradioUI | |
| # Fonction pour obtenir les coordonnées GPS d'une adresse | |
| def geocode_address(address): | |
| response = requests.get(f"https://api-adresse.data.gouv.fr/search/?q={address}") | |
| data = response.json() | |
| if not data['features']: | |
| return None, None | |
| coordinates = data['features'][0]['geometry']['coordinates'] | |
| return coordinates[1], coordinates[0] # Retourne latitude, longitude | |
| # Fonction pour récupérer toutes les stations Vélib' | |
| def get_stations(): | |
| response = requests.get("https://velib-metropole-opendata.smovengo.cloud/opendata/Velib_Metropole/station_information.json") | |
| data = response.json() | |
| return data['data']['stations'] | |
| # Trouver la station Vélib' la plus proche d'une adresse donnée | |
| def find_nearest_station(address): | |
| user_lat, user_lon = geocode_address(address) | |
| if user_lat is None or user_lon is None: | |
| return None | |
| stations = get_stations() | |
| min_distance = float('inf') | |
| nearest_station = None | |
| for station in stations: | |
| station_coords = (station['lat'], station['lon']) | |
| distance = geodesic((user_lat, user_lon), station_coords).meters | |
| if distance < min_distance: | |
| min_distance = distance | |
| nearest_station = station | |
| return nearest_station | |
| # Fonction pour récupérer le statut d'une station | |
| def get_station_status(station_id): | |
| response = requests.get("https://velib-metropole-opendata.smovengo.cloud/opendata/Velib_Metropole/station_status.json") | |
| data = response.json() | |
| for station in data['data']['stations']: | |
| if station['station_id'] == station_id: | |
| return station | |
| return None | |
| # Outil final qui donne le nombre de vélos disponibles à une adresse donnée | |
| def get_bike_availability(address: str) -> str: | |
| """A tool that fetches the number of available Velib' bikes at the nearest station to a given address. | |
| Args: | |
| address: The address where the user wants to find a Velib' station. | |
| """ | |
| nearest_station = find_nearest_station(address) | |
| if not nearest_station: | |
| return "Aucune station Vélib' trouvée à proximité." | |
| status = get_station_status(nearest_station['station_id']) | |
| if not status: | |
| return "Impossible de récupérer le statut de la station." | |
| mechanical_bikes = status['num_bikes_available_types'][0].get('mechanical', 0) | |
| ebikes = status['num_bikes_available_types'][1].get('ebike', 0) | |
| return (f"🚲 Station la plus proche : {nearest_station['name']}\n" | |
| f"🔧 Vélos mécaniques disponibles : {mechanical_bikes}\n" | |
| f"⚡ Vélos électriques disponibles : {ebikes}") | |
| # Ajout du tool à l'agent | |
| final_answer = FinalAnswerTool() | |
| model = HfApiModel( | |
| max_tokens=2096, | |
| temperature=0.5, | |
| model_id='Qwen/Qwen2.5-Coder-32B-Instruct', | |
| custom_role_conversions=None, | |
| ) | |
| # Import tool from Hub | |
| image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True) | |
| with open("prompts.yaml", 'r') as stream: | |
| prompt_templates = yaml.safe_load(stream) | |
| agent = CodeAgent( | |
| model=model, | |
| tools=[final_answer, get_bike_availability], # Ajout du nouvel outil ici | |
| max_steps=6, | |
| verbosity_level=1, | |
| grammar=None, | |
| planning_interval=None, | |
| name=None, | |
| description=None, | |
| prompt_templates=prompt_templates | |
| ) | |
| GradioUI(agent).launch() |