Spaces:
Running
Running
File size: 1,608 Bytes
6e4eef0 c6aa194 6e4eef0 a36133a 6e4eef0 6351c15 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 |
from smolagents import Tool
from typing import Optional
class SimpleTool(Tool):
name = "get_travel_duration"
description = "Gets the travel time in car 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"},"departure_time":{"type":"integer","nullable":True,"description":"the departure time, provide only a `datetime.datetime` if you want to specify this"}}
output_type = "string"
def forward(self, start_location: str, destination_location: str, departure_time: Optional[int] = None) -> str:
"""Gets the travel time in car between two places.
Args:
start_location: the place from which you start your ride
destination_location: the place of arrival
departure_time: the departure time, provide only a `datetime.datetime` if you want to specify this
"""
import googlemaps
import os
gmaps = googlemaps.Client(os.getenv("GMAPS_API_KEY"))
if departure_time is None:
from datetime import datetime
monday = datetime(2025, 1, 6, 11, 0)
try:
directions_result = gmaps.directions(
start_location,
destination_location,
mode="transit",
departure_time=departure_time
)
return directions_result[0]["legs"][0]["duration"]["text"]
except Exception as e:
print(e, directions_result)
return e |