Spaces:
Sleeping
Sleeping
Upload tool
Browse files- requirements.txt +0 -1
- tool.py +23 -18
requirements.txt
CHANGED
@@ -1,3 +1,2 @@
|
|
1 |
geopy
|
2 |
-
osmnx
|
3 |
smolagents
|
|
|
1 |
geopy
|
|
|
2 |
smolagents
|
tool.py
CHANGED
@@ -3,12 +3,12 @@ from typing import Any, Optional
|
|
3 |
|
4 |
class SimpleTool(Tool):
|
5 |
name = "calculate_journey_metrics"
|
6 |
-
description = "Calculates
|
7 |
inputs = {"start_location":{"type":"string","description":"The starting point of the journey."},"destination_location":{"type":"string","description":"The endpoint 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 |
-
"""Calculates
|
12 |
|
13 |
Args:
|
14 |
start_location: The starting point of the journey.
|
@@ -17,25 +17,34 @@ class SimpleTool(Tool):
|
|
17 |
Defaults to 'driving'.
|
18 |
|
19 |
Returns:
|
20 |
-
str:
|
21 |
"""
|
22 |
-
|
23 |
from geopy.distance import geodesic
|
24 |
|
25 |
try:
|
|
|
|
|
|
|
26 |
# Get coordinates
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
29 |
|
30 |
-
# Calculate
|
31 |
-
distance_km = geodesic(
|
|
|
|
|
|
|
32 |
|
33 |
# Define speeds (km/h)
|
34 |
speeds = {
|
35 |
'driving': 60,
|
36 |
'walking': 5,
|
37 |
'bicycling': 15,
|
38 |
-
'transit':
|
39 |
}
|
40 |
|
41 |
# Get speed for mode
|
@@ -43,11 +52,10 @@ class SimpleTool(Tool):
|
|
43 |
|
44 |
# Calculate time
|
45 |
hours = distance_km / speed
|
46 |
-
minutes = hours * 60
|
47 |
|
48 |
-
# Format
|
49 |
-
if
|
50 |
-
time_str = f"{int(
|
51 |
elif hours < 24:
|
52 |
hrs = int(hours)
|
53 |
mins = int((hours - hrs) * 60)
|
@@ -56,14 +64,11 @@ class SimpleTool(Tool):
|
|
56 |
days = hours / 24
|
57 |
time_str = f"{days:.1f} days"
|
58 |
|
59 |
-
|
60 |
f"Journey metrics:\n"
|
61 |
f"Distance: {distance_km:.1f} kilometers\n"
|
62 |
-
f"Estimated time: {time_str}
|
63 |
-
f"Mode: {transportation_mode or 'driving'}"
|
64 |
)
|
65 |
|
66 |
-
return response
|
67 |
-
|
68 |
except Exception as e:
|
69 |
return f"Error calculating journey metrics: {str(e)}"
|
|
|
3 |
|
4 |
class SimpleTool(Tool):
|
5 |
name = "calculate_journey_metrics"
|
6 |
+
description = "Calculates journey metrics including distance and time."
|
7 |
inputs = {"start_location":{"type":"string","description":"The starting point of the journey."},"destination_location":{"type":"string","description":"The endpoint 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 |
+
"""Calculates journey metrics including distance and time.
|
12 |
|
13 |
Args:
|
14 |
start_location: The starting point of the journey.
|
|
|
17 |
Defaults to 'driving'.
|
18 |
|
19 |
Returns:
|
20 |
+
str: Journey distance and estimated time.
|
21 |
"""
|
22 |
+
from geopy.geocoders import Nominatim
|
23 |
from geopy.distance import geodesic
|
24 |
|
25 |
try:
|
26 |
+
# Initialize geocoder
|
27 |
+
geolocator = Nominatim(user_agent="journey_metrics_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_km = geodesic(
|
38 |
+
(start.latitude, start.longitude),
|
39 |
+
(end.latitude, end.longitude)
|
40 |
+
).kilometers
|
41 |
|
42 |
# Define speeds (km/h)
|
43 |
speeds = {
|
44 |
'driving': 60,
|
45 |
'walking': 5,
|
46 |
'bicycling': 15,
|
47 |
+
'transit': 30
|
48 |
}
|
49 |
|
50 |
# Get speed for mode
|
|
|
52 |
|
53 |
# Calculate time
|
54 |
hours = distance_km / speed
|
|
|
55 |
|
56 |
+
# Format time string
|
57 |
+
if hours < 1:
|
58 |
+
time_str = f"{int(hours * 60)} minutes"
|
59 |
elif hours < 24:
|
60 |
hrs = int(hours)
|
61 |
mins = int((hours - hrs) * 60)
|
|
|
64 |
days = hours / 24
|
65 |
time_str = f"{days:.1f} days"
|
66 |
|
67 |
+
return (
|
68 |
f"Journey metrics:\n"
|
69 |
f"Distance: {distance_km:.1f} kilometers\n"
|
70 |
+
f"Estimated time by {transportation_mode or 'driving'}: {time_str}"
|
|
|
71 |
)
|
72 |
|
|
|
|
|
73 |
except Exception as e:
|
74 |
return f"Error calculating journey metrics: {str(e)}"
|