File size: 4,206 Bytes
f51e2d4
 
 
 
 
 
f8fce5b
f51e2d4
 
95eac8e
 
 
f51e2d4
 
 
 
 
b70216b
f51e2d4
 
 
 
 
 
 
1365a32
b70216b
1365a32
 
 
f51e2d4
1365a32
b70216b
1365a32
 
f51e2d4
a67fffd
b70216b
a67fffd
f51e2d4
 
 
 
 
 
 
 
 
 
 
 
 
1365a32
 
 
 
 
f51e2d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
from huggingface_hub import InferenceClient
import json
import folium
from gradio_folium import Folium
import gradio as gr
from geopy.geocoders import Nominatim
from geopy.adapters import AioHTTPAdapter
import os

from dotenv import load_dotenv
load_dotenv()

repo_id = "mistralai/Mistral-7B-Instruct-v0.2"
llm_client = InferenceClient(model=repo_id, timeout=180, token=os.getenv("HF_TOKEN"))

def show_map(places_list):
    
    geolocator = Nominatim(user_agent="HF_smart-travel-planner") #, adapter_factory=AioHTTPAdapter)

    i = 0
    for place in places_list:
        city = place['city']
        attraction = place['attraction']
        desc = place['description']

        try:
            locate = geolocator.geocode(f"{attraction}, {city}", timeout=10)
        except:
            locate = None
            
        if locate is None:
            try:
                locate = geolocator.geocode(f"{city}", timeout=10)
            except:
                locate = None

        if locate is None:
            locate = geolocator.geocode("Dortmund, Germany", timeout=10)
            
        if locate is not None:
            if i == 0:
                mymap = folium.Map(location=[locate.latitude, locate.longitude], zoom_start=10)
            folium.Marker(location=[locate.latitude, locate.longitude], popup=folium.Popup(f"{attraction}: {desc}", max_width=200)).add_to(mymap)
            i += 1

    return mymap


def parse_output(output):
    
    itinerary = "Itinerary" + output.split("Key points:")[0].split('Itinerary:')[1]
    key_points = output.split("Key points:")[1].strip()
    try:
        itinerary_list = json.loads(key_points)
    except:
        itinerary_list = []
        
    return itinerary, itinerary_list


def run_program(place, duration, start, end):
    
    prompt = f"""Please generate a list of key cities and attractions in each cities for the following place: {place} suitable for {duration}, starting from {start} and ending at {end}, as a json list of less than 10 dictionaries with the following keys: 'city', 'attraction' and 'description'.
ALWAYS write the city and country in the 'city'. For instance do not only "city": "Paris" as the city but "city": "Paris, France". Don't combine multiplpe locations in one element. In the 'description', write the main attractions in that city as well.
Try to minimize the distance between locations. Always think of the transportation means that you want to use, and the timing: morning, afternoon, where to sleep.
Only generate two sections: 'Itinerary:' provides a descriptive explanation of the itinerary, then you list the locations in 'Key points:'"""

    response = llm_client.text_generation(prompt, max_new_tokens=3000) #, max_new_tokens=2000, stream=True)
    itinerary, places_list = parse_output(response)

    mymap = show_map(places_list)

    return itinerary, mymap


with gr.Blocks(
    theme=gr.themes.Soft(
        primary_hue=gr.themes.colors.yellow,
        secondary_hue=gr.themes.colors.blue,
    )
) as demo:
    gr.Markdown("# ๐Ÿ—บ๏ธ AI Travel Planner ๐Ÿ•๏ธ\nThis personal travel planner is based on Mixtral-8x7B, called through the Hugging Face API. Describe your ideal trip below, and let our AI assistant guide you!\n Beware that the model does not really have access to train or plane schedules, it is relying on general world knowledge for its propositions.")
    dest = gr.Textbox(
        label="Destination"
    )
    dur = gr.Textbox(
        label="duration"
    )
    start = gr.Textbox(
        label="start"
    )
    end = gr.Textbox(
        label="end"
    )
    button = gr.Button("Generate trip!")

    gr.Markdown("### LLM Output ๐Ÿ‘‡")
    itinery, starting_map = run_program(dest, dur, start, end)
    itinerary = gr.Markdown("```text\n" + itinery + "\n```")
    gr.Markdown("_Click the markers on the map map to display information about the places._")
    # Get initial map
    # starting_map = folium.Map(location=[0, 0], zoom_start=1)
    map = Folium(value=starting_map, height=600, label="Chosen locations")


    # Dynamics
    button.click(run_program, inputs=[dest, dur, start, end], outputs=[itinerary, map])


if __name__ == "__main__":
    demo.launch()