MHamdan commited on
Commit
efa8259
·
verified ·
1 Parent(s): e872b21

Upload tool

Browse files
Files changed (3) hide show
  1. app.py +6 -0
  2. requirements.txt +3 -0
  3. tool.py +69 -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,3 @@
 
 
 
 
1
+ geopy
2
+ osmnx
3
+ smolagents
tool.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from smolagents import Tool
2
+ from typing import Any, Optional
3
+
4
+ class SimpleTool(Tool):
5
+ name = "calculate_journey_metrics"
6
+ description = "Calculates comprehensive journey metrics including distance and time using OpenStreetMap."
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 comprehensive journey metrics including distance and time using OpenStreetMap.
12
+
13
+ Args:
14
+ start_location: The starting point of the journey.
15
+ destination_location: The endpoint of the journey.
16
+ transportation_mode: Mode of transport ('driving', 'walking', 'bicycling', or 'transit').
17
+ Defaults to 'driving'.
18
+
19
+ Returns:
20
+ str: A detailed description of the journey including distance and time.
21
+ """
22
+ import osmnx as ox
23
+ from geopy.distance import geodesic
24
+
25
+ try:
26
+ # Get coordinates
27
+ start_coords = ox.geocode(start_location)
28
+ end_coords = ox.geocode(destination_location)
29
+
30
+ # Calculate straight-line distance
31
+ distance_km = geodesic(start_coords, end_coords).kilometers
32
+
33
+ # Define speeds (km/h)
34
+ speeds = {
35
+ 'driving': 60,
36
+ 'walking': 5,
37
+ 'bicycling': 15,
38
+ 'transit': 25
39
+ }
40
+
41
+ # Get speed for mode
42
+ speed = speeds.get(transportation_mode, speeds['driving'])
43
+
44
+ # Calculate time
45
+ hours = distance_km / speed
46
+ minutes = hours * 60
47
+
48
+ # Format response based on time
49
+ if minutes < 60:
50
+ time_str = f"{int(minutes)} minutes"
51
+ elif hours < 24:
52
+ hrs = int(hours)
53
+ mins = int((hours - hrs) * 60)
54
+ time_str = f"{hrs} hours and {mins} minutes"
55
+ else:
56
+ days = hours / 24
57
+ time_str = f"{days:.1f} days"
58
+
59
+ response = (
60
+ f"Journey metrics:\n"
61
+ f"Distance: {distance_km:.1f} kilometers\n"
62
+ f"Estimated time: {time_str}\n"
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)}"