Spaces:
Sleeping
Sleeping
Upload tool
Browse files- app.py +6 -0
- requirements.txt +2 -0
- tool.py +54 -0
app.py
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import launch_gradio_demo
|
| 2 |
+
from tool import SimpleTool
|
| 3 |
+
|
| 4 |
+
tool = SimpleTool()
|
| 5 |
+
|
| 6 |
+
launch_gradio_demo(tool)
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
geopy
|
| 2 |
+
smolagents
|
tool.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import Tool
|
| 2 |
+
from typing import Any, Optional
|
| 3 |
+
|
| 4 |
+
class SimpleTool(Tool):
|
| 5 |
+
name = "get_travel_duration"
|
| 6 |
+
description = "Gets the travel time between two places using straight-line distance."
|
| 7 |
+
inputs = {"start_location":{"type":"string","description":"The starting point of the journey."},"destination_location":{"type":"string","description":"The destination point of the journey."},"transportation_mode":{"type":"string","nullable":True,"description":"Mode of transport ('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 using straight-line distance.
|
| 12 |
+
|
| 13 |
+
Args:
|
| 14 |
+
start_location: The starting point of the journey.
|
| 15 |
+
destination_location: The destination point of the journey.
|
| 16 |
+
transportation_mode: Mode of transport ('driving', 'walking', 'bicycling', or 'transit'). Defaults to 'driving'.
|
| 17 |
+
|
| 18 |
+
Returns:
|
| 19 |
+
str: The estimated duration of travel between the two locations.
|
| 20 |
+
"""
|
| 21 |
+
# All imports must be inside the function for proper Hub sharing
|
| 22 |
+
from geopy.distance import geodesic
|
| 23 |
+
from geopy.geocoders import Nominatim
|
| 24 |
+
from datetime import datetime
|
| 25 |
+
|
| 26 |
+
try:
|
| 27 |
+
geolocator = Nominatim(user_agent="my_travel_calculator")
|
| 28 |
+
|
| 29 |
+
# Get coordinates
|
| 30 |
+
start = geolocator.geocode(start_location)
|
| 31 |
+
end = geolocator.geocode(destination_location)
|
| 32 |
+
|
| 33 |
+
if not start or not end:
|
| 34 |
+
return "Could not find one or both locations"
|
| 35 |
+
|
| 36 |
+
# Calculate distance
|
| 37 |
+
distance = geodesic((start.latitude, start.longitude),
|
| 38 |
+
(end.latitude, end.longitude)).kilometers
|
| 39 |
+
|
| 40 |
+
# Approximate speeds in km/h
|
| 41 |
+
speeds = {
|
| 42 |
+
'driving': 30, # Urban average
|
| 43 |
+
'walking': 5,
|
| 44 |
+
'bicycling': 15, # Changed 'cycling' to 'bicycling' to match documentation
|
| 45 |
+
'transit': 25
|
| 46 |
+
}
|
| 47 |
+
speed = speeds.get(transportation_mode, speeds['driving'])
|
| 48 |
+
|
| 49 |
+
# Calculate time in minutes
|
| 50 |
+
time_minutes = (distance / speed) * 60
|
| 51 |
+
|
| 52 |
+
return f"Approximately {int(time_minutes)} minutes (straight-line estimate)"
|
| 53 |
+
except Exception as e:
|
| 54 |
+
return f"Error calculating route: {str(e)}"
|