File size: 4,909 Bytes
a477b7c
 
c47605f
 
a477b7c
c47605f
 
a477b7c
 
309caec
 
a6221a5
 
 
 
 
 
 
 
309caec
 
 
 
 
 
 
 
a477b7c
 
 
 
309caec
a477b7c
 
c47605f
b4ca855
309caec
a477b7c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c47605f
 
 
 
 
 
 
a477b7c
c47605f
a477b7c
 
 
 
 
c47605f
 
 
 
a477b7c
 
 
 
 
 
 
 
 
 
c47605f
 
a477b7c
 
 
 
 
 
 
 
 
 
 
 
c4979f5
 
 
 
 
c47605f
309caec
c4979f5
 
 
34bd01c
 
 
c4979f5
 
34bd01c
a477b7c
 
 
 
 
 
 
34bd01c
77d013c
 
 
 
 
383bc3f
a477b7c
 
 
 
 
2a353b4
010f68b
a477b7c
309caec
34bd01c
c4979f5
 
 
a477b7c
 
 
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import gradio as gr
from folium import Map
import numpy as np
from ast import literal_eval
import pandas as pd

from gradio_folium import Folium
import folium
from huggingface_hub import InferenceClient
from geopy.geocoders import Nominatim

from examples import (
    description_sf,
    output_example_sf,
    description_loire,
    output_example_loire,
    df_examples
)

geolocator = Nominatim(user_agent="HF-trip-planner")

def get_coordinates(address):
    location = geolocator.geocode(address)
    if location:
        return (location.latitude, location.longitude)
    else:
        return None

repo_id = "mistralai/Mixtral-8x7B-Instruct-v0.1"
llm_client = InferenceClient(model=repo_id, timeout=180)


def generate_key_points(text):
    prompt = f"""             
    Please generate a set of key geographical points for the following description: {text}, as a json list of less than 10 dictionnaries with the following keys: 'name', 'description'. Precise the full location in the 'name' if there is a possible ambiguity.
    Generally try to minimze 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 a 'Thought:' and a 'Key points:' sections, nothing else.

    For instance:
    Description: {description_sf}
    Thought: {output_example_sf}

    Description: {description_loire}
    Thought: {output_example_loire}

    Now begin. You can make the descriptions a bit more verbose than in the examples.

    Description: {text}
    Thought: 
    """
    return llm_client.text_generation(prompt, max_new_tokens=2000)


def parse_llm_output(output):
    rationale = "Thought: " + output.split("Key points:")[0]
    key_points = output.split("Key points:")[1]
    output = key_points.replace("    ", "")
    parsed_output = literal_eval(output)
    dataframe = pd.DataFrame.from_dict(parsed_output)
    return dataframe, rationale


def get_coordinates_row(row):
    coords = get_coordinates(row["name"])
    if coords is not None:
        row["lat"], row["lon"] = coords
    return row


def create_map_from_markers(dataframe):
    dataframe = dataframe.apply(get_coordinates_row, axis=1)
    f_map = Map(
        location=[dataframe["lat"].mean(), dataframe["lon"].mean()],
        zoom_start=5,
        tiles="CartoDB Voyager",
    )
    for _, row in dataframe.iterrows():
        if np.isnan(row["lat"]) or np.isnan(row["lon"]):
            continue
        marker = folium.CircleMarker(
            location=[row["lat"], row["lon"]],
            radius=10,
            popup=folium.Popup(
                f"<h4>{row['name']}</h4><p>{row['description']}</p>", max_width=450
            ),
            fill=True,
            fill_color="blue",
            fill_opacity=0.6,
            color="blue",
            weight=1,
        )
        marker.add_to(f_map),

    bounds = [[row["lat"], row["lon"]] for _, row in dataframe.iterrows()]
    f_map.fit_bounds(bounds, padding=(100, 100))
    return f_map


def run_display(text):
    output = generate_key_points(text)
    dataframe, rationale = parse_llm_output(output)
    map = create_map_from_markers(dataframe)
    return map, rationale


df_examples = pd.DataFrame.from_dict(
    [
        {"description": description_loire, "output": output_example_loire},
        {"description": description_aligned, "output": output_example_aligned},
        {"description": description_chinatown, "output": output_example_chinatown},
        {"description": description_taiwan, "output": output_example_taiwan},
    ]
)


def select_example(df, data: gr.SelectData):
    row = df.iloc[data.index[0], :]
    dataframe, rationale = parse_llm_output(row["output"])
    return row["description"], create_map_from_markers(dataframe), rationale


with gr.Blocks(
    theme=gr.themes.Soft(
        primary_hue=gr.themes.colors.yellow,
        secondary_hue=gr.themes.colors.blue,
    )
) as demo:
    gr.Markdown("# 🗺️ LLM trip planner (based on Mixtral)")
    text = gr.Textbox(
        label="Describe your trip here:",
        value=description_sf,
    )
    button = gr.Button()
    gr.Markdown("### LLM Output 👇\n_Click the map to see information about the places._")

    # Get initial map and rationale
    example_dataframe, example_rationale = parse_llm_output(output_example_sf)
    display_rationale = gr.Markdown(example_rationale)
    starting_map = create_map_from_markers(example_dataframe)
    map = Folium(value=starting_map, height=700, label="Chosen locations")
    button.click(run_display, inputs=[text], outputs=[map, display_rationale])

    gr.Markdown("### Other examples")
    clickable_examples = gr.DataFrame(value=df_examples, height=200)
    clickable_examples.select(
        select_example, clickable_examples, outputs=[text, map, display_rationale]
    )

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