m-ric HF staff commited on
Commit
c47605f
1 Parent(s): 3822047

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -36
app.py CHANGED
@@ -1,10 +1,12 @@
1
  import gradio as gr
2
- from gradio_folium import Folium
3
  from folium import Map
 
 
4
  import pandas as pd
 
 
5
  import folium
6
  from huggingface_hub import InferenceClient
7
- from ast import literal_eval
8
 
9
  repo_id = "mistralai/Mixtral-8x7B-Instruct-v0.1"
10
  llm_client = InferenceClient(model=repo_id, timeout=180)
@@ -15,38 +17,38 @@ Since this is on foot, walking distances should be kept to a minimum. I'll make
15
  I want to start at Fisherman's Wharf, then head to Ghirardelli Square, followed by the Cable Car Museum, Union Square, Chinatown, and finally Coit Tower, to provide a full day of sightseeing, with a mix of history, culture, and beautiful views.
16
 
17
  Key points: [
18
- {"name": "Fisherman's Wharf", "lat": 37.8081, "lon": -122.4082, "description": "Fisherman's Wharf is a popular tourist destination in San Francisco, featuring Pier 39, the Maritime National Historical Park, and Boudin Bakery. Start your day with breakfast and enjoy the sea lions."},
19
- {"name": "Ghirardelli Square", "lat": 37.8094, "lon": -122.4154, "description": "Ghirardelli Square is a historic square in San Francisco, known for its chocolate shop and various boutiques. It's a great place to grab a sweet treat and do some shopping."},
20
- {"name": "Cable Car Museum", "lat": 37.7931, "lon": -122.4058, "description": "The Cable Car Museum is a museum in San Francisco that showcases the history of the city's iconic cable cars. It's a great place to learn about the technology and history behind these unique vehicles."},
21
- {"name": "Union Square", "lat": 37.7873, "lon": -122.4062, "description": "Union Square is a public plaza in San Francisco, known for its shopping, dining, and entertainment options. Relax, grab a bite to eat, and do some shopping."},
22
- {"name": "Chinatown", "lat": 37.7937, "lon": -122.4081, "description": "Chinatown is a vibrant neighborhood in San Francisco, known for its Chinese culture, history, and cuisine. It's a great place to explore the streets, try some delicious food, and learn about Chinese culture."},
23
- {"name": "Coit Tower", "lat": 37.8024, "lon": -122.4058, "description": "Coit Tower is a historic tower in San Francisco, known for its panoramic views of the city. You can end your day here, taking in the sights and sounds of the city from above."}
24
  ]
25
  """
26
 
27
  description_loire = "A 3-day bike trip through the Loire Valley."
28
  output_example_loire = """
29
  To make the most of the trip, we will bike between the châteaux to enjoy the beautiful scenery and explore the Loire Valley at a leisurely pace. Here is a suggested itinerary for a 3-day bike trip through the Loire Valley:
30
- 1. Start at Château de Cheverny, a smal but charming château with beautiful gardens.
31
  2. Bike to Château de Chambord, the largest and most recognizable château in the Loire Valley.
32
- 3. Continue to Château de Blois, a historic château with a rich history.
33
  4. End the day at Château de Chaumont-sur-Loire, a château with a stunning view of the Loire River.
34
  5. On the second day, bike to Château de Chenonceau, a stunning château built over the River Cher.
35
  6. Continue to Château d'Amboise, a historic château with ties to the French Renaissance.
36
- 7. End the day at Château d'Azay-le-Rideau, a charming château with a moat.
37
- 8. On the third day, bike to Château d'Ussé, a fairy-tale like château said to have inspired Sleeping Beauty.
38
- 9. End the trip at Château de Villandry, known for its beautiful gardens.
39
 
40
  Key points: [
41
- {"name": "Château de Cheverny", "lat": 47.6333, "lon": 1.5667, "description": "Château de Cheverny is a smaller but equally charming château with beautiful gardens, known for its French classical architecture and rich history."},
42
- {"name": "Château de Chambord", "lat": 47.6111, "lon": 1.5167, "description": "Château de Chambord is the largest and most recognizable château in the Loire Valley, known for its French Renaissance architecture and beautiful gardens."},
43
- {"name": "Château de Blois", "lat": 47.5833, "lon": 1.3333, "description": "Château de Blois is a historic château with a rich history, known for its stunning architecture and beautiful gardens."},
44
- {"name": "Château de Chaumont-sur-Loire", "lat": 47.5167, "lon": 1.3167, "description": "Château de Chaumont-sur-Loire is a château with a stunning view of the Loire River, known for its beautiful gardens and rich history."},
45
- {"name": "Château de Chenonceau", "lat": 47.3917, "lon": 1.3667, "description": "Château de Chenonceau is a stunning château built over the River Cher, known for its beautiful gardens and rich history."},
46
- {"name": "Château d'Amboise", "lat": 47.4167, "lon": 1.0500, "description": "Château d'Amboise is a historic château with ties to the French Renaissance, known for its beautiful gardens and stunning views of the Loire Valley."},
47
- {"name": "Château d'Azay-le-Rideau", "lat": 47.3333, "lon": 0.6167, "description": "Château d'Azay-le-Rideau is a charming château with a moat, known for its French Renaissance architecture and beautiful gardens."},
48
- {"name": "Château d'Ussé", "lat": 47.2667, "lon": 0.5167, "description": "Château d'Ussé is a fairy-tale like château said to have inspired Sleeping Beauty, known for its beautiful gardens and stunning views of the Indre Valley."},
49
- {"name": "Château de Villandry", "lat": 47.1667, "lon": 0.4167, "description": "Château de Villandry is known for its beautiful gardens, featuring a water garden, ornamental flower gardens, and vegetable gardens."}
50
  ]
51
  """
52
 
@@ -64,18 +66,43 @@ output_example_aligned = """To find five cities that form a perfect line on the
64
  These five cities form a perfect line on the map, running from north to south, and offer a diverse range of cultures and experiences.
65
 
66
  Key points: [
67
- {"name": "Groningen, Netherlands", "lat": 53.2192, "lon": 6.5667, "description": "Groningen is a vibrant student city in the north of the Netherlands, known for its lively cultural scene and historic center. The city is home to the University of Groningen, one of the oldest and most respected universities in the country, and has a rich history dating back to the Middle Ages."},
68
- {"name": "Osnabrück, Germany", "lat": 52.2708, "lon": 8.0431, "description": "Osnabrück is a picturesque city in northwest Germany, known for its medieval center and rich history. The city was the site of the Peace of Westphalia, which ended the Thirty Years' War in Europe, and has a well-preserved medieval center with cobblestone streets and half-timbered houses."},
69
- {"name": "Erfurt, Germany", "lat": 50.9778, "lon": 11.0278, "description": "Erfurt is a charming city in central Germany, known for its well-preserved medieval center and beautiful gardens. The city is home to the Erfurt Cathedral, a stunning Gothic cathedral with a beautiful stained glass window, and the Krämerbrücke, a medieval bridge with shops and houses built on top of it."},
70
- {"name": "Nuremberg, Germany", "lat": 49.4500, "lon": 11.0750, "description": "Nuremberg is a historic city in southern Germany, known for its medieval architecture and rich history. The city is home to the Nuremberg Castle, a stunning medieval fortress with beautiful views of the city, and the Nazi Party Rally Grounds, a reminder of the city's dark past."},
71
- {"name": "Innsbruck, Austria", "lat": 47.2692, "lon": 11.3914, "description": "Innsbruck is a stunning city in the Austrian Alps, known for its beautiful mountain scenery and winter sports. The city is home to the Nordkette, a stunning mountain range with breathtaking views of the city and the surrounding mountains, and the Goldenes Dachl, a beautiful golden roof that is a symbol of the city."}
72
  ]
73
  """
74
 
 
 
 
 
 
 
 
 
 
 
 
 
75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
  def generate_key_points(text):
77
  prompt = f"""
78
- 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', 'lat', 'lon', 'description'.
79
  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.
80
 
81
  For instance:
@@ -94,7 +121,6 @@ def generate_key_points(text):
94
 
95
 
96
  def parse_llm_output(output):
97
- print(output)
98
  rationale = "Thought: " + output.split("Key points:")[0]
99
  key_points = output.split("Key points:")[1]
100
  output = key_points.replace(" ", "")
@@ -103,15 +129,24 @@ def parse_llm_output(output):
103
  return dataframe, rationale
104
 
105
 
 
 
 
 
 
 
 
106
  def create_map_from_markers(dataframe):
 
107
  f_map = Map(
108
  location=[dataframe["lat"].mean(), dataframe["lon"].mean()],
109
  zoom_start=5,
110
  tiles="CartoDB Voyager",
111
  )
112
-
113
- dataframe.apply(
114
- lambda row: folium.CircleMarker(
 
115
  location=[row["lat"], row["lon"]],
116
  radius=10,
117
  popup=folium.Popup(
@@ -122,9 +157,8 @@ def create_map_from_markers(dataframe):
122
  fill_opacity=0.6,
123
  color="blue",
124
  weight=1,
125
- ).add_to(f_map),
126
- axis=1,
127
- )
128
 
129
  bounds = [[row["lat"], row["lon"]] for _, row in dataframe.iterrows()]
130
  f_map.fit_bounds(bounds, padding=(100, 100))
@@ -143,6 +177,7 @@ df_examples = pd.DataFrame.from_dict(
143
  {"description": description_loire, "output": output_example_loire},
144
  {"description": description_sf, "output": output_example_sf},
145
  {"description": description_aligned, "output": output_example_aligned},
 
146
  ]
147
  )
148
 
@@ -165,7 +200,7 @@ with gr.Blocks(
165
  value=description_sf,
166
  )
167
  button = gr.Button()
168
- gr.Markdown("### 👉 LLM Output\n_Click the map to see information about the places._")
169
 
170
  # Get initial map and rationale
171
  example_dataframe, example_rationale = parse_llm_output(output_example_sf)
 
1
  import gradio as gr
 
2
  from folium import Map
3
+ import numpy as np
4
+ from ast import literal_eval
5
  import pandas as pd
6
+
7
+ from gradio_folium import Folium
8
  import folium
9
  from huggingface_hub import InferenceClient
 
10
 
11
  repo_id = "mistralai/Mixtral-8x7B-Instruct-v0.1"
12
  llm_client = InferenceClient(model=repo_id, timeout=180)
 
17
  I want to start at Fisherman's Wharf, then head to Ghirardelli Square, followed by the Cable Car Museum, Union Square, Chinatown, and finally Coit Tower, to provide a full day of sightseeing, with a mix of history, culture, and beautiful views.
18
 
19
  Key points: [
20
+ {"name": "Fisherman's Wharf", "description": "Fisherman's Wharf is a popular tourist destination in San Francisco, featuring Pier 39, the Maritime National Historical Park, and Boudin Bakery. Start your day with breakfast and enjoy the sea lions."},
21
+ {"name": "Ghirardelli Square", "description": "Ghirardelli Square is a historic square in San Francisco, known for its chocolate shop and various boutiques. It's a great place to grab a sweet treat and do some shopping."},
22
+ {"name": "Cable Car Museum", "description": "The Cable Car Museum is a museum in San Francisco that showcases the history of the city's iconic cable cars. It's a great place to learn about the technology and history behind these unique vehicles."},
23
+ {"name": "Union Square, San Francisco", "description": "Union Square is a public plaza in San Francisco, known for its shopping, dining, and entertainment options. Relax, grab a bite to eat, and do some shopping."},
24
+ {"name": "Chinatown, San Francisco", "description": "Chinatown is a vibrant neighborhood in San Francisco, known for its Chinese culture, history, and cuisine. It's a great place to explore the streets, try some delicious food, and learn about Chinese culture."},
25
+ {"name": "Coit Tower, San Francisco", "description": "Coit Tower is a historic tower in San Francisco, known for its panoramic views of the city. You can end your day here, taking in the sights and sounds of the city from above."}
26
  ]
27
  """
28
 
29
  description_loire = "A 3-day bike trip through the Loire Valley."
30
  output_example_loire = """
31
  To make the most of the trip, we will bike between the châteaux to enjoy the beautiful scenery and explore the Loire Valley at a leisurely pace. Here is a suggested itinerary for a 3-day bike trip through the Loire Valley:
32
+ 1. Start at Château de Blois, a historic château with a rich history.
33
  2. Bike to Château de Chambord, the largest and most recognizable château in the Loire Valley.
34
+ 3. Continue to Château de Cheverny, a smaller but equally charming château with beautiful gardens.
35
  4. End the day at Château de Chaumont-sur-Loire, a château with a stunning view of the Loire River.
36
  5. On the second day, bike to Château de Chenonceau, a stunning château built over the River Cher.
37
  6. Continue to Château d'Amboise, a historic château with ties to the French Renaissance.
38
+ 9. End the day at Château de Villandry, known for its beautiful gardens.
39
+ 8. On the third day, bike to Château d'Azay-le-Rideau, a charming château with a moat.
40
+ 9. End the trip at Château d'Ussé, a fairy-tale like château said to have inspired Sleeping Beauty.
41
 
42
  Key points: [
43
+ {"name": "Château de Blois", "description": "Château de Blois is a historic château with a rich history, known for its stunning architecture and beautiful gardens."},
44
+ {"name": "Château de Chambord", "description": "Château de Chambord is the largest and most recognizable château in the Loire Valley, known for its French Renaissance architecture and beautiful gardens."},
45
+ {"name": "Château de Cheverny", "description": "Château de Cheverny is a small but charming château with beautiful gardens, known for its French classical architecture and rich history."},
46
+ {"name": "Château de Chaumont-sur-Loire", "description": "Château de Chaumont-sur-Loire is a château with a stunning view of the Loire River, known for its beautiful gardens and rich history."},
47
+ {"name": "Château de Chenonceau", "description": "Château de Chenonceau is a stunning château built over the River Cher, known for its beautiful gardens and rich history."},
48
+ {"name": "Château d'Amboise", "description": "Château d'Amboise is a historic château with ties to the French Renaissance, known for its beautiful gardens and stunning views of the Loire Valley."},
49
+ {"name": "Château de Villandry", "description": "Château de Villandry is known for its beautiful gardens, featuring a water garden, ornamental flower gardens, and vegetable gardens."},
50
+ {"name": "Château d'Azay-le-Rideau", "description": "Château d'Azay-le-Rideau is a charming château with a moat, known for its French Renaissance architecture and beautiful gardens."},
51
+ {"name": "Château d'Ussé", "description": "Château d'Ussé is a fairy-tale like château said to have inspired Sleeping Beauty, known for its beautiful gardens and stunning views of the Indre Valley."},
52
  ]
53
  """
54
 
 
66
  These five cities form a perfect line on the map, running from north to south, and offer a diverse range of cultures and experiences.
67
 
68
  Key points: [
69
+ {"name": "Groningen, Netherlands", "description": "Groningen is a vibrant student city in the north of the Netherlands, known for its lively cultural scene and historic center. The city is home to the University of Groningen, one of the oldest and most respected universities in the country, and has a rich history dating back to the Middle Ages."},
70
+ {"name": "Osnabrück, Germany", "description": "Osnabrück is a picturesque city in northwest Germany, known for its medieval center and rich history. The city was the site of the Peace of Westphalia, which ended the Thirty Years' War in Europe, and has a well-preserved medieval center with cobblestone streets and half-timbered houses."},
71
+ {"name": "Erfurt, Germany", "description": "Erfurt is a charming city in central Germany, known for its well-preserved medieval center and beautiful gardens. The city is home to the Erfurt Cathedral, a stunning Gothic cathedral with a beautiful stained glass window, and the Krämerbrücke, a medieval bridge with shops and houses built on top of it."},
72
+ {"name": "Nuremberg, Germany", "description": "Nuremberg is a historic city in southern Germany, known for its medieval architecture and rich history. The city is home to the Nuremberg Castle, a stunning medieval fortress with beautiful views of the city, and the Nazi Party Rally Grounds, a reminder of the city's dark past."},
73
+ {"name": "Innsbruck, Austria", "description": "Innsbruck is a stunning city in the Austrian Alps, known for its beautiful mountain scenery and winter sports. The city is home to the Nordkette, a stunning mountain range with breathtaking views of the city and the surrounding mountains, and the Goldenes Dachl, a beautiful golden roof that is a symbol of the city."}
74
  ]
75
  """
76
 
77
+ description_chinatown = "Show me 10 Chinatowns in the US."
78
+ output_example_chinatown = """
79
+ 1. New York City, NY
80
+ 2. San Francisco, CA
81
+ 3. Los Angeles, CA
82
+ 4. Chicago, IL
83
+ 5. Philadelphia, PA
84
+ 6. Boston, MA
85
+ 7. Honolulu, HI
86
+ 8. Seattle, WA
87
+ 9. Portland, OR
88
+ 10. Las Vegas, NV
89
 
90
+ Key points: [
91
+ {"name": "Manhattan Chinatown, New York City, NY", "description": "Manhattan Chinatown is the largest Chinatown in the United States, located in the heart of Manhattan, New York City. It is a bustling neighborhood with a rich cultural heritage, known for its vibrant street life, traditional Chinese markets, and delicious food."},
92
+ {"name": "San Francisco Chinatown, San Francisco, CA", "description": "San Francisco Chinatown is one of the oldest and most famous Chinatowns in the United States, located in the heart of San Francisco. It is a vibrant neighborhood with a rich cultural heritage, known for its beautiful architecture, traditional Chinese markets, and delicious food."},
93
+ {"name": "Los Angeles Chinatown, Los Angeles, CA", "description": "Los Angeles Chinatown is a historic neighborhood in downtown Los Angeles, known for its vibrant street life, traditional Chinese markets, and delicious food. It is a popular destination for tourists and locals alike, offering a unique blend of Chinese culture and American history."},
94
+ {"name": "Chicago Chinatown, Chicago, IL", "description": "Chicago Chinatown is a vibrant neighborhood in the heart of Chicago, known for its beautiful architecture, traditional Chinese markets, and delicious food. It is a popular destination for tourists and locals alike, offering a unique blend of Chinese culture and American history."},
95
+ {"name": "Philadelphia Chinatown, Philadelphia, PA", "description": "Philadelphia Chinatown is a historic neighborhood in the heart of Philadelphia, known for its vibrant street life, traditional Chinese markets, and delicious food. It is a popular destination for tourists and locals alike, offering a unique blend of Chinese culture and American history."},
96
+ {"name": "Boston Chinatown, Boston, MA", "description": "Boston Chinatown is a vibrant neighborhood in the heart of Boston, known for its beautiful architecture, traditional Chinese markets, and delicious food. It is a popular destination for tourists and locals alike, offering a unique blend of Chinese culture and American history."},
97
+ {"name": "Honolulu Chinatown, Honolulu, HI", "description": "Honolulu Chinatown is a historic neighborhood in the heart of Honolulu, known for its vibrant street life, traditional Chinese markets, and delicious food. It is a popular destination for tourists and locals alike, offering a unique blend of Chinese culture and Hawaiian history."},
98
+ {"name": "Seattle Chinatown, Seattle, WA", "description": "Seattle Chinatown, also known as the International District, is a vibrant neighborhood in the heart of Seattle, known for its beautiful architecture, traditional Chinese markets, and delicious food. It is a popular destination for tourists and locals alike, offering a unique blend of Chinese culture and American history."},
99
+ {"name": "Portland Chinatown, Portland, OR", "description": "Portland Chinatown, also known as Old Town Chinatown, is a historic neighborhood in the heart of Portland, known for its vibrant street life, traditional Chinese markets, and delicious food. It is a popular destination for tourists and locals alike, offering a unique blend of Chinese culture and American history."},
100
+ {"name": "Las Vegas Chinatown, Las Vegas, NV", "description": "Las Vegas Chinatown is a vibrant neighborhood located just west of the Las Vegas Strip, known for its beautiful architecture, traditional Chinese markets, and delicious food. It is a popular destination for tourists and locals alike, offering a unique blend of Chinese culture and American history."}
101
+ ]
102
+ """
103
  def generate_key_points(text):
104
  prompt = f"""
105
+ 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.
106
  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.
107
 
108
  For instance:
 
121
 
122
 
123
  def parse_llm_output(output):
 
124
  rationale = "Thought: " + output.split("Key points:")[0]
125
  key_points = output.split("Key points:")[1]
126
  output = key_points.replace(" ", "")
 
129
  return dataframe, rationale
130
 
131
 
132
+ def get_coordinates_row(row):
133
+ coords = get_coordinates(row["name"])
134
+ if coords is not None:
135
+ row["lat"], row["lon"] = coords
136
+ return row
137
+
138
+
139
  def create_map_from_markers(dataframe):
140
+ dataframe = dataframe.apply(get_coordinates_row, axis=1)
141
  f_map = Map(
142
  location=[dataframe["lat"].mean(), dataframe["lon"].mean()],
143
  zoom_start=5,
144
  tiles="CartoDB Voyager",
145
  )
146
+ for _, row in dataframe.iterrows():
147
+ if np.isnan(row["lat"]) or np.isnan(row["lon"]):
148
+ continue
149
+ marker = folium.CircleMarker(
150
  location=[row["lat"], row["lon"]],
151
  radius=10,
152
  popup=folium.Popup(
 
157
  fill_opacity=0.6,
158
  color="blue",
159
  weight=1,
160
+ )
161
+ marker.add_to(f_map),
 
162
 
163
  bounds = [[row["lat"], row["lon"]] for _, row in dataframe.iterrows()]
164
  f_map.fit_bounds(bounds, padding=(100, 100))
 
177
  {"description": description_loire, "output": output_example_loire},
178
  {"description": description_sf, "output": output_example_sf},
179
  {"description": description_aligned, "output": output_example_aligned},
180
+ {"description": description_chinatown, "output": output_example_chinatown},
181
  ]
182
  )
183
 
 
200
  value=description_sf,
201
  )
202
  button = gr.Button()
203
+ gr.Markdown("### LLM Output\n_Click the map to see information about the places._")
204
 
205
  # Get initial map and rationale
206
  example_dataframe, example_rationale = parse_llm_output(output_example_sf)