real-jiakai
commited on
Upload tool
Browse files- app.py +7 -0
- requirements.txt +2 -0
- tool.py +34 -0
app.py
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from smolagents import launch_gradio_demo
|
2 |
+
from typing import Optional
|
3 |
+
from tool import SimpleTool
|
4 |
+
|
5 |
+
tool = SimpleTool()
|
6 |
+
|
7 |
+
launch_gradio_demo(tool)
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
smolagents
|
2 |
+
google
|
tool.py
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from smolagents import Tool
|
2 |
+
from typing import Optional
|
3 |
+
|
4 |
+
class SimpleTool(Tool):
|
5 |
+
name = "get_travel_duration"
|
6 |
+
description = "Gets the travel time in car between two places."
|
7 |
+
inputs = {"start_location":{"type":"string","description":"the place from which you start your ride"},"destination_location":{"type":"string","description":"the place of arrival"},"departure_time":{"type":"integer","nullable":True,"description":"the departure time, provide only a `datetime.datetime` if you want to specify this"}}
|
8 |
+
output_type = "string"
|
9 |
+
|
10 |
+
def forward(self, start_location: str, destination_location: str, departure_time: Optional[int] = None) -> str:
|
11 |
+
"""Gets the travel time in car between two places.
|
12 |
+
|
13 |
+
Args:
|
14 |
+
start_location: the place from which you start your ride
|
15 |
+
destination_location: the place of arrival
|
16 |
+
departure_time: the departure time, provide only a `datetime.datetime` if you want to specify this
|
17 |
+
"""
|
18 |
+
import googlemaps # All imports are placed within the function, to allow for sharing to Hub.
|
19 |
+
from google.colab import userdata
|
20 |
+
import os
|
21 |
+
|
22 |
+
gmaps = googlemaps.Client(userdata.get("GMAPS_API_KEY"))
|
23 |
+
|
24 |
+
if departure_time is None:
|
25 |
+
from datetime import datetime
|
26 |
+
departure_time = datetime(2025, 1, 6, 11, 0)
|
27 |
+
|
28 |
+
directions_result = gmaps.directions(
|
29 |
+
start_location,
|
30 |
+
destination_location,
|
31 |
+
mode="transit",
|
32 |
+
departure_time=departure_time
|
33 |
+
)
|
34 |
+
return directions_result[0]["legs"][0]["duration"]["text"]
|