Spaces:
Build error
Build error
Download tool.py from everettjf/get-travel-duration-tool: direct link, hf CLI and curl.
- Browser
- Download file 3.47 kB
-
https://huggingface.co/spaces/everettjf/get-travel-duration-tool/resolve/main/tool.py
- Command line
-
hf download hf://spaces/everettjf/get-travel-duration-tool/tool.py
-
curl -L -o tool.py https://huggingface.co/spaces/everettjf/get-travel-duration-tool/resolve/main/tool.py
3.47 kB
| from smolagents import Tool | |
| from typing import Any, Optional | |
| class SimpleTool(Tool): | |
| name = "get_travel_duration" | |
| description = "Gets the travel time between two places." | |
| inputs = {"start_location":{"type":"string","description":"the place from which you start your ride"},"destination_location":{"type":"string","description":"the place of arrival"},"transportation_mode":{"type":"string","nullable":True,"description":"The transportation mode, in 'driving', 'walking', 'bicycling', or 'transit'. Defaults to 'driving'."}} | |
| output_type = "string" | |
| def forward(self, start_location: str, destination_location: str, transportation_mode: Optional[str] = None) -> str: | |
| """Gets the travel time between two places. | |
| Args: | |
| start_location: the place from which you start your ride | |
| destination_location: the place of arrival | |
| transportation_mode: The transportation mode, in 'driving', 'walking', 'bicycling', or 'transit'. Defaults to 'driving'. | |
| """ | |
| import os # All imports are placed within the function, to allow for sharing to Hub. | |
| import openrouteservice as ors | |
| from openrouteservice.geocode import pelias_search | |
| import json | |
| # Initialize the client | |
| client = ors.Client(key=os.getenv("ORS_API_KEY")) | |
| if transportation_mode is None: | |
| transportation_mode = "driving-car" | |
| elif transportation_mode == "driving": | |
| transportation_mode = "driving-car" | |
| elif transportation_mode == "walking": | |
| transportation_mode = "foot-walking" | |
| elif transportation_mode == "bicycling": | |
| transportation_mode = "cycling-regular" | |
| elif transportation_mode == "transit": | |
| return "Transit mode is not supported by OpenRouteService. Please choose driving, walking, or bicycling." | |
| try: | |
| # Geocode start location | |
| start_coords = pelias_search(client, start_location) | |
| if not start_coords or not start_coords['features']: | |
| return f"Could not find coordinates for {start_location}" | |
| start_point = start_coords['features'][0]['geometry']['coordinates'] | |
| # Geocode destination location | |
| dest_coords = pelias_search(client, destination_location) | |
| if not dest_coords or not dest_coords['features']: | |
| return f"Could not find coordinates for {destination_location}" | |
| dest_point = dest_coords['features'][0]['geometry']['coordinates'] | |
| # Get directions | |
| routes = client.directions( | |
| coordinates=[start_point, dest_point], | |
| profile=transportation_mode, | |
| format='geojson' | |
| ) | |
| if not routes or 'features' not in routes or not routes['features']: | |
| return "No route found between these places with the required transportation mode." | |
| # Extract duration in seconds and convert to human-readable format | |
| duration_seconds = routes['features'][0]['properties']['segments'][0]['duration'] | |
| hours = int(duration_seconds // 3600) | |
| minutes = int((duration_seconds % 3600) // 60) | |
| if hours > 0: | |
| return f"{hours} hour{'s' if hours != 1 else ''} {minutes} min" | |
| else: | |
| return f"{minutes} min" | |
| except Exception as e: | |
| print(e) | |
| return str(e) |