Spaces:
Sleeping
Sleeping
Upload tool
Browse files- app.py +7 -0
- requirements.txt +2 -0
- tool.py +38 -0
app.py
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import launch_gradio_demo
|
| 2 |
+
from typing import Optional
|
| 3 |
+
from tool import SimpleTool
|
| 4 |
+
|
| 5 |
+
tool = SimpleTool()
|
| 6 |
+
|
| 7 |
+
launch_gradio_demo(tool)
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
googlemaps
|
| 2 |
+
smolagents
|
tool.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import Tool
|
| 2 |
+
from typing import Optional
|
| 3 |
+
|
| 4 |
+
class SimpleTool(Tool):
|
| 5 |
+
name = "get_travel_duration"
|
| 6 |
+
description = "Gets the travel time between two places."
|
| 7 |
+
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'."}}
|
| 8 |
+
output_type = "string"
|
| 9 |
+
|
| 10 |
+
def forward(self, start_location: str, destination_location: str, transportation_mode: Optional[str] = None) -> str:
|
| 11 |
+
"""Gets the travel time between two places.
|
| 12 |
+
|
| 13 |
+
Args:
|
| 14 |
+
start_location: the place from which you start your ride
|
| 15 |
+
destination_location: the place of arrival
|
| 16 |
+
transportation_mode: The transportation mode, in 'driving', 'walking', 'bicycling', or 'transit'. Defaults to 'driving'.
|
| 17 |
+
"""
|
| 18 |
+
import os # All imports are placed within the function, to allow for sharing to Hub.
|
| 19 |
+
import googlemaps
|
| 20 |
+
from datetime import datetime
|
| 21 |
+
|
| 22 |
+
gmaps = googlemaps.Client(os.getenv("GMAPS_API_KEY"))
|
| 23 |
+
|
| 24 |
+
if transportation_mode is None:
|
| 25 |
+
transportation_mode = "driving"
|
| 26 |
+
try:
|
| 27 |
+
directions_result = gmaps.directions(
|
| 28 |
+
start_location,
|
| 29 |
+
destination_location,
|
| 30 |
+
mode=transportation_mode,
|
| 31 |
+
departure_time=datetime(2025, 6, 6, 11, 0), # At 11, date far in the future
|
| 32 |
+
)
|
| 33 |
+
if len(directions_result) == 0:
|
| 34 |
+
return "No way found between these places with the required transportation mode."
|
| 35 |
+
return directions_result[0]["legs"][0]["duration"]["text"]
|
| 36 |
+
except Exception as e:
|
| 37 |
+
print(e)
|
| 38 |
+
return e
|