Spaces:
Sleeping
Sleeping
Upload tool
Browse files
tool.py
CHANGED
@@ -23,36 +23,30 @@ class SimpleTool(Tool):
|
|
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) with more realistic values
|
43 |
speeds = {
|
44 |
'driving': {'urban': 30, 'highway': 100, 'average': 80},
|
45 |
'walking': {'average': 5},
|
46 |
'bicycling': {'casual': 12, 'average': 20},
|
47 |
'transit': {'urban': 30, 'intercity': 60},
|
48 |
'plane': {
|
49 |
-
'short_haul': 500,
|
50 |
'medium_haul': 700,
|
51 |
'long_haul': 800
|
52 |
}
|
53 |
}
|
54 |
|
55 |
-
# Get appropriate speed based on distance and mode
|
56 |
if transportation_mode == 'plane':
|
57 |
if distance_km < 500:
|
58 |
speed = speeds['plane']['short_haul']
|
@@ -60,20 +54,15 @@ class SimpleTool(Tool):
|
|
60 |
speed = speeds['plane']['medium_haul']
|
61 |
else:
|
62 |
speed = speeds['plane']['long_haul']
|
63 |
-
|
64 |
-
# Add airport procedures time (hours)
|
65 |
-
additional_time = 2 # 2 hours for airport procedures
|
66 |
else:
|
67 |
-
# Default to driving if mode not specified
|
68 |
mode = transportation_mode or 'driving'
|
69 |
speed = speeds.get(mode, speeds['driving'])['average']
|
70 |
additional_time = 0
|
71 |
|
72 |
-
# Calculate time
|
73 |
travel_time = distance_km / speed
|
74 |
total_hours = travel_time + additional_time
|
75 |
|
76 |
-
# Format time string
|
77 |
if total_hours < 1:
|
78 |
time_str = f"{int(total_hours * 60)} minutes"
|
79 |
elif total_hours < 24:
|
@@ -84,7 +73,6 @@ class SimpleTool(Tool):
|
|
84 |
days = total_hours / 24
|
85 |
time_str = f"{days:.1f} days"
|
86 |
|
87 |
-
# Additional information for plane travel
|
88 |
if transportation_mode == 'plane':
|
89 |
return (
|
90 |
f"Journey metrics:\n"
|
|
|
23 |
from geopy.distance import geodesic
|
24 |
|
25 |
try:
|
|
|
26 |
geolocator = Nominatim(user_agent="journey_metrics_calculator")
|
|
|
|
|
27 |
start = geolocator.geocode(start_location)
|
28 |
end = geolocator.geocode(destination_location)
|
29 |
|
30 |
if not start or not end:
|
31 |
return "Could not find one or both locations"
|
32 |
|
|
|
33 |
distance_km = geodesic(
|
34 |
(start.latitude, start.longitude),
|
35 |
(end.latitude, end.longitude)
|
36 |
).kilometers
|
37 |
|
|
|
38 |
speeds = {
|
39 |
'driving': {'urban': 30, 'highway': 100, 'average': 80},
|
40 |
'walking': {'average': 5},
|
41 |
'bicycling': {'casual': 12, 'average': 20},
|
42 |
'transit': {'urban': 30, 'intercity': 60},
|
43 |
'plane': {
|
44 |
+
'short_haul': 500,
|
45 |
'medium_haul': 700,
|
46 |
'long_haul': 800
|
47 |
}
|
48 |
}
|
49 |
|
|
|
50 |
if transportation_mode == 'plane':
|
51 |
if distance_km < 500:
|
52 |
speed = speeds['plane']['short_haul']
|
|
|
54 |
speed = speeds['plane']['medium_haul']
|
55 |
else:
|
56 |
speed = speeds['plane']['long_haul']
|
57 |
+
additional_time = 2
|
|
|
|
|
58 |
else:
|
|
|
59 |
mode = transportation_mode or 'driving'
|
60 |
speed = speeds.get(mode, speeds['driving'])['average']
|
61 |
additional_time = 0
|
62 |
|
|
|
63 |
travel_time = distance_km / speed
|
64 |
total_hours = travel_time + additional_time
|
65 |
|
|
|
66 |
if total_hours < 1:
|
67 |
time_str = f"{int(total_hours * 60)} minutes"
|
68 |
elif total_hours < 24:
|
|
|
73 |
days = total_hours / 24
|
74 |
time_str = f"{days:.1f} days"
|
75 |
|
|
|
76 |
if transportation_mode == 'plane':
|
77 |
return (
|
78 |
f"Journey metrics:\n"
|