File size: 3,640 Bytes
4fe6fd4
9b5b26a
 
 
c19d193
6aae614
4fe6fd4
8fe992b
9b5b26a
 
4fe6fd4
 
 
 
 
 
 
 
9b5b26a
4fe6fd4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40d9ae7
4fe6fd4
 
 
 
 
 
 
 
 
 
 
 
9b5b26a
4fe6fd4
 
9b5b26a
4fe6fd4
9b5b26a
4fe6fd4
 
 
8c01ffb
4fe6fd4
 
 
8c01ffb
4fe6fd4
 
 
 
 
 
ae7a494
4fe6fd4
 
ae7a494
e121372
4fe6fd4
 
 
 
13d500a
8c01ffb
9b5b26a
 
8c01ffb
861422e
 
4fe6fd4
8c01ffb
8fe992b
4fe6fd4
8c01ffb
 
 
 
 
 
861422e
8fe992b
 
8c01ffb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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
@tool
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()