Spaces:
Sleeping
Sleeping
Commit
·
601ab9c
1
Parent(s):
85ba299
commit
Browse files- app.py +56 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Optional
|
| 2 |
+
from smolagents import CodeAgent, HfApiModel, tool
|
| 3 |
+
import os
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
import googlemaps
|
| 6 |
+
import gradio as gr
|
| 7 |
+
|
| 8 |
+
# Define tool for calculating travel duration
|
| 9 |
+
@tool
|
| 10 |
+
def get_travel_duration(start_location: str, destination_location: str, transportation_mode: Optional[str] = None) -> str:
|
| 11 |
+
"""Gets the travel time 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 |
+
transportation_mode: The transportation mode, in 'driving', 'walking', 'bicycling'. Defaults to 'driving'.
|
| 17 |
+
"""
|
| 18 |
+
gmaps = googlemaps.Client(os.getenv("GMAPS_API_KEY"))
|
| 19 |
+
|
| 20 |
+
if transportation_mode is None:
|
| 21 |
+
transportation_mode = "driving"
|
| 22 |
+
try:
|
| 23 |
+
directions_result = gmaps.directions(
|
| 24 |
+
start_location,
|
| 25 |
+
destination_location,
|
| 26 |
+
mode=transportation_mode,
|
| 27 |
+
departure_time=datetime(2025, 6, 6, 11, 0), # At 11, date far in the future
|
| 28 |
+
)
|
| 29 |
+
if len(directions_result) == 0:
|
| 30 |
+
return "No way found between these places with the required transportation mode."
|
| 31 |
+
return directions_result[0]["legs"][0]["duration"]["text"]
|
| 32 |
+
except Exception as e:
|
| 33 |
+
print(e)
|
| 34 |
+
return str(e)
|
| 35 |
+
|
| 36 |
+
# Create the AI agent
|
| 37 |
+
agent = CodeAgent(tools=[get_travel_duration], model=HfApiModel(), additional_authorized_imports=["datetime"])
|
| 38 |
+
|
| 39 |
+
# Define Gradio interface
|
| 40 |
+
def generate_itinerary(city: str, transportation_mode: str) -> str:
|
| 41 |
+
query = f"Can you give me a nice one-day trip around {city} with a few locations and the times? Could be in the city or outside, but should fit in one day. I'm travelling only with {transportation_mode}."
|
| 42 |
+
return agent.run(query)
|
| 43 |
+
|
| 44 |
+
# Create Gradio UI
|
| 45 |
+
iface = gr.Interface(
|
| 46 |
+
fn=generate_itinerary,
|
| 47 |
+
inputs=[
|
| 48 |
+
gr.Textbox(lines=1, placeholder="Enter the city/place", label="City"),
|
| 49 |
+
gr.Dropdown(["driving", "walking", "bicycling"], label="Transportation Mode", value="driving")
|
| 50 |
+
],
|
| 51 |
+
outputs="text",
|
| 52 |
+
title="One Day Guide"
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
# Launch Gradio UI
|
| 56 |
+
iface.launch(share = True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
smolagents
|
| 2 |
+
googlemaps
|
| 3 |
+
gradio
|