File size: 2,391 Bytes
601ab9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b9ed0a4
 
 
601ab9c
 
 
 
 
 
 
 
b9ed0a4
 
601ab9c
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from typing import Optional
from smolagents import CodeAgent, HfApiModel, tool
import os
from datetime import datetime
import googlemaps
import gradio as gr

# Define tool for calculating travel duration
@tool
def get_travel_duration(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'. Defaults to 'driving'.
    """
    gmaps = googlemaps.Client(os.getenv("GMAPS_API_KEY"))

    if transportation_mode is None:
        transportation_mode = "driving"
    try:
        directions_result = gmaps.directions(
            start_location,
            destination_location,
            mode=transportation_mode,
            departure_time=datetime(2025, 6, 6, 11, 0), # At 11, date far in the future
        )
        if len(directions_result) == 0:
            return "No way found between these places with the required transportation mode."
        return directions_result[0]["legs"][0]["duration"]["text"]
    except Exception as e:
        print(e)
        return str(e)

# Create the AI agent
agent = CodeAgent(tools=[get_travel_duration], model=HfApiModel(), additional_authorized_imports=["datetime"])

# Define Gradio interface
def generate_itinerary(city: str, transportation_mode: str) -> str:
    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}."
    return agent.run(query)

description_md = """
Plan your perfect one-day trip with our travel itinerary generator. Simply input your desired city and select transportation mode, and receive a curated schedule of sightseeing spots tailored for a seamless journey.
"""
# Create Gradio UI
iface = gr.Interface(
    fn=generate_itinerary,
    inputs=[
        gr.Textbox(lines=1, placeholder="Enter the city/place", label="City"),
        gr.Dropdown(["driving", "walking", "bicycling"], label="Transportation Mode", value="driving")
    ],
    outputs="text",
    title="One Day Guide",
    description=description_md
)

# Launch Gradio UI
iface.launch(share = True)