Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from huggingface_hub import InferenceClient
|
2 |
+
import json
|
3 |
+
import folium
|
4 |
+
from gradio_folium import Folium
|
5 |
+
import gradio as gr
|
6 |
+
from geopy.geocoders import Nominatim
|
7 |
+
import os
|
8 |
+
|
9 |
+
repo_id = "mistralai/Mistral-7B-Instruct-v0.2"
|
10 |
+
llm_client = InferenceClient(model=repo_id, timeout=180, token=os.getenv("HF_TOKEN"))
|
11 |
+
|
12 |
+
def show_map(places_list):
|
13 |
+
|
14 |
+
geolocator = Nominatim(user_agent="my_geocoder")
|
15 |
+
|
16 |
+
i = 0
|
17 |
+
for place in places_list:
|
18 |
+
city = place['city']
|
19 |
+
attraction = place['attraction']
|
20 |
+
desc = place['description']
|
21 |
+
|
22 |
+
locate = geolocator.geocode(f"{attraction}, {city}")
|
23 |
+
if locate is None:
|
24 |
+
locate = geolocator.geocode(f"{city}")
|
25 |
+
|
26 |
+
if locate is not None:
|
27 |
+
if i == 0:
|
28 |
+
mymap = folium.Map(location=[locate.latitude, locate.longitude], zoom_start=10)
|
29 |
+
folium.Marker(location=[locate.latitude, locate.longitude], popup=folium.Popup(f"{attraction}: {desc}", max_width=200)).add_to(mymap)
|
30 |
+
i += 1
|
31 |
+
|
32 |
+
return mymap
|
33 |
+
|
34 |
+
|
35 |
+
def parse_output(output):
|
36 |
+
|
37 |
+
itinerary = "Itinerary" + output.split("Key points:")[0].split('Itinerary:')[1]
|
38 |
+
key_points = output.split("Key points:")[1].strip()
|
39 |
+
itinerary_list = json.loads(key_points)
|
40 |
+
|
41 |
+
return itinerary, itinerary_list
|
42 |
+
|
43 |
+
|
44 |
+
def run_program(place, duration, start, end):
|
45 |
+
|
46 |
+
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'.
|
47 |
+
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.
|
48 |
+
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.
|
49 |
+
Only generate two sections: 'Itinerary:' provides a descriptive explanation of the itinerary, then you list the locations in 'Key points:'"""
|
50 |
+
|
51 |
+
response = llm_client.text_generation(prompt, max_new_tokens=3000) #, max_new_tokens=2000, stream=True)
|
52 |
+
itinerary, places_list = parse_output(response)
|
53 |
+
|
54 |
+
mymap = show_map(places_list)
|
55 |
+
|
56 |
+
return itinerary, mymap
|
57 |
+
|
58 |
+
|
59 |
+
with gr.Blocks(
|
60 |
+
theme=gr.themes.Soft(
|
61 |
+
primary_hue=gr.themes.colors.yellow,
|
62 |
+
secondary_hue=gr.themes.colors.blue,
|
63 |
+
)
|
64 |
+
) as demo:
|
65 |
+
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.")
|
66 |
+
dest = gr.Textbox(
|
67 |
+
label="Destination"
|
68 |
+
)
|
69 |
+
dur = gr.Textbox(
|
70 |
+
label="duration"
|
71 |
+
)
|
72 |
+
start = gr.Textbox(
|
73 |
+
label="start"
|
74 |
+
)
|
75 |
+
end = gr.Textbox(
|
76 |
+
label="end"
|
77 |
+
)
|
78 |
+
button = gr.Button("Generate trip!")
|
79 |
+
|
80 |
+
gr.Markdown("### LLM Output 👇")
|
81 |
+
itinery, starting_map = run_program(dest, dur, start, end)
|
82 |
+
itinerary = gr.Markdown("```text\n" + itinery + "\n```")
|
83 |
+
gr.Markdown("_Click the markers on the map map to display information about the places._")
|
84 |
+
# Get initial map
|
85 |
+
# starting_map = folium.Map(location=[0, 0], zoom_start=1)
|
86 |
+
map = Folium(value=starting_map, height=600, label="Chosen locations")
|
87 |
+
|
88 |
+
|
89 |
+
# Dynamics
|
90 |
+
button.click(run_program, inputs=[dest, dur, start, end], outputs=[itinerary, map])
|
91 |
+
|
92 |
+
|
93 |
+
if __name__ == "__main__":
|
94 |
+
demo.launch()
|