Spaces:
Running
Running
File size: 1,891 Bytes
6e4eef0 c6aa194 6e4eef0 3284f34 26ec7eb 6e4eef0 0c5dccc 3284f34 0c5dccc 6e4eef0 26ec7eb 6e4eef0 042710c 6e4eef0 0c5dccc 042710c 6e4eef0 042710c d2c8496 26ec7eb 6351c15 d2c8496 68b1787 6351c15 3366410 6351c15 0c5dccc 3494381 |
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 |
from smolagents import Tool
from typing import Optional
class SimpleTool(Tool):
name = "get_travel_duration"
description = "Gets the travel time between two places."
inputs = {"start_location":{"type":"string","description":"the place from which you start your ride"},"destination_location":{"type":"string","description":"the place of arrival"},"transportation_mode":{"type":"string","nullable":True,"description":"The transportation mode, in 'driving', 'walking', 'bicycling', or 'transit'. Defaults to 'driving'."}}
output_type = "string"
def forward(self, start_location: str, destination_location: str, transportation_mode: Optional[str] = None) -> str:
"""Gets the travel time between two places.
Args:
start_location: the place from which you start your ride
destination_location: the place of arrival
transportation_mode: The transportation mode, in 'driving', 'walking', 'bicycling', or 'transit'. Defaults to 'driving'.
"""
import os # All imports are placed within the function, to allow for sharing to Hub.
import googlemaps
from datetime import datetime
gmaps = googlemaps.Client(os.getenv("GMAPS_API_KEY"))
if transportation_mode is None:
transportation_mode = "driving"
try:
directions_result = gmaps.directions(
start_location,
destination_location,
mode=transportation_mode,
departure_time=datetime(2025, 12, 6, 11, 0), # At 11, date far in the future
)
if len(directions_result) == 0:
return "No way found between these places with the required transportation mode."
return directions_result[0]["legs"][0]["duration"]["text"]
except Exception as e:
print(e)
return e |