MHamdan commited on
Commit
139193c
·
verified ·
1 Parent(s): 2444872

Upload tool

Browse files
Files changed (3) hide show
  1. app.py +4 -32
  2. requirements.txt +1 -3
  3. tool.py +48 -33
app.py CHANGED
@@ -1,34 +1,6 @@
 
 
1
 
2
- import gradio as gr
3
- from smolagents import load_tool
4
 
5
- # Load the tool
6
- journey_tool = load_tool("MHamdan/journey-metrics-tool", trust_remote_code=True)
7
-
8
- def create_interface():
9
- return gr.Interface(
10
- fn=journey_tool,
11
- inputs=[
12
- gr.Textbox(label="Start Location", placeholder="Enter starting point"),
13
- gr.Textbox(label="Destination Location", placeholder="Enter destination"),
14
- gr.Dropdown(
15
- choices=["driving", "walking", "bicycling", "transit", "plane"],
16
- label="Transportation Mode",
17
- value="driving"
18
- )
19
- ],
20
- outputs=gr.Textbox(label="Journey Details"),
21
- title="Journey Metrics Calculator",
22
- description="Calculate travel distance and time between two locations.",
23
- examples=[
24
- ["Montreal", "Toronto", "plane"],
25
- ["Vancouver", "Whistler", "driving"],
26
- ["Ottawa", "Kingston", "bicycling"],
27
- ["New York", "Los Angeles", "plane"],
28
- ["London", "Paris", "train"]
29
- ]
30
- )
31
-
32
- # Create and launch the interface
33
- iface = create_interface()
34
- iface.launch()
 
1
+ from smolagents import launch_gradio_demo
2
+ from tool import SimpleTool
3
 
4
+ tool = SimpleTool()
 
5
 
6
+ launch_gradio_demo(tool)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
requirements.txt CHANGED
@@ -1,4 +1,2 @@
1
-
2
- gradio>=4.0.0
3
- geopy>=2.3.0
4
  smolagents
 
1
+ geopy
 
 
2
  smolagents
tool.py CHANGED
@@ -21,71 +21,86 @@ class SimpleTool(Tool):
21
  """
22
  from geopy.geocoders import Nominatim
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']
53
  elif distance_km < 3000:
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:
69
- hrs = int(total_hours)
70
- mins = int((total_hours - hrs) * 60)
71
- time_str = f"{hrs} hours and {mins} minutes"
72
- else:
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"
79
  f"Distance: {distance_km:.1f} kilometers\n"
80
- f"Flight time: {int(travel_time * 60)} minutes\n"
81
- f"Total estimated time (including airport procedures): {time_str}"
82
  )
83
 
84
- return (
85
- f"Journey metrics:\n"
86
- f"Distance: {distance_km:.1f} kilometers\n"
87
- f"Estimated time by {transportation_mode or 'driving'}: {time_str}"
88
- )
89
-
90
  except Exception as e:
91
- return f"Error calculating journey metrics: {str(e)}"
 
21
  """
22
  from geopy.geocoders import Nominatim
23
  from geopy.distance import geodesic
24
+ import time
25
 
26
  try:
27
+ # Initialize geocoder with increased timeout
28
+ geolocator = Nominatim(user_agent="journey_metrics_calculator", timeout=10)
29
+
30
+ # Add delay between requests to avoid rate limiting
31
+ time.sleep(1)
32
  start = geolocator.geocode(start_location)
33
+ time.sleep(1)
34
  end = geolocator.geocode(destination_location)
35
 
36
  if not start or not end:
37
+ return "Could not find one or both locations. Please check spelling and try again."
38
 
39
+ # Calculate distance
40
  distance_km = geodesic(
41
  (start.latitude, start.longitude),
42
  (end.latitude, end.longitude)
43
  ).kilometers
44
 
45
+ # Define speeds (km/h) with more realistic values
46
  speeds = {
47
+ 'driving': {'urban': 50, 'highway': 100, 'average': 80},
48
  'walking': {'average': 5},
49
+ 'bicycling': {'casual': 15, 'average': 20},
50
  'transit': {'urban': 30, 'intercity': 60},
51
  'plane': {
52
+ 'short_haul': 500, # <500km
53
+ 'medium_haul': 700, # 500-3000km
54
+ 'long_haul': 850 # >3000km
55
  }
56
  }
57
 
58
+ # Calculate time based on mode and distance
59
  if transportation_mode == 'plane':
60
+ # Select appropriate plane speed based on distance
61
  if distance_km < 500:
62
  speed = speeds['plane']['short_haul']
63
  elif distance_km < 3000:
64
  speed = speeds['plane']['medium_haul']
65
  else:
66
  speed = speeds['plane']['long_haul']
67
+
68
+ # Calculate flight time
69
+ flight_hours = distance_km / speed
70
+
71
+ # Add airport procedures time (2 hours total)
72
+ total_hours = flight_hours + 2
73
+
74
+ # Format the response for plane travel
75
+ flight_minutes = int(flight_hours * 60)
76
+ total_hrs = int(total_hours)
77
+ total_mins = int((total_hours - total_hrs) * 60)
78
+
79
+ return (
80
+ f"Journey metrics:\n"
81
+ f"Distance: {distance_km:.1f} kilometers\n"
82
+ f"Flight time: {flight_minutes} minutes\n"
83
+ f"Total estimated time (including airport procedures): "
84
+ f"{total_hrs} hours and {total_mins} minutes"
85
+ )
86
  else:
87
+ # Get speed for other modes
88
  mode = transportation_mode or 'driving'
89
+ if mode not in speeds:
90
+ return f"Unsupported transportation mode: {mode}"
91
+
92
+ speed = speeds[mode]['average']
93
+ hours = distance_km / speed
94
+
95
+ # Format time string
96
+ hrs = int(hours)
97
+ mins = int((hours - hrs) * 60)
 
 
 
 
 
 
98
 
 
99
  return (
100
  f"Journey metrics:\n"
101
  f"Distance: {distance_km:.1f} kilometers\n"
102
+ f"Estimated time by {mode}: {hrs} hours and {mins} minutes"
 
103
  )
104
 
 
 
 
 
 
 
105
  except Exception as e:
106
+ return f"Error calculating journey metrics: Please try again with more specific location names. Error: {str(e)}"