m-ric HF staff commited on
Commit
72590af
1 Parent(s): f5caaa8
Files changed (1) hide show
  1. app.py +8 -35
app.py CHANGED
@@ -62,30 +62,6 @@ def parse_llm_output(output):
62
  return dataframe, rationale
63
 
64
 
65
- class AsyncLRUCache:
66
- def __init__(self, maxsize=100):
67
- self.cache = OrderedDict()
68
- self.maxsize = maxsize
69
-
70
- async def get(self, key):
71
- if key not in self.cache:
72
- return None
73
- self.cache.move_to_end(key)
74
- return self.cache[key]
75
-
76
- async def aset(self, key, value):
77
- self.set(key, value)
78
-
79
- def set(self, key, value):
80
- if key in self.cache:
81
- self.cache.move_to_end(key)
82
- self.cache[key] = value
83
- if len(self.cache) > self.maxsize:
84
- self.cache.popitem(last=False)
85
-
86
- # Instantiate the cache
87
- cache = AsyncLRUCache(maxsize=500)
88
-
89
  preset_values = {
90
  "Fisherman's Wharf, San Francisco": {'lat': 37.808332, 'lon': -122.415715},
91
  'Ghirardelli Square, San Francisco': {'lat': 37.80587075, 'lon': -122.42294914207058},
@@ -133,15 +109,11 @@ preset_values = {
133
  'Fort Point National Historic Site, San Francisco': {'lat': 37.81045495, 'lon': -122.47713831312802},
134
  'Presidio of San Francisco': {'lat': 37.798745600000004, 'lon': -122.46458892410745}
135
  }
136
- for key, value in preset_values.items():
137
- cache.set(key, value)
138
-
139
 
140
  async def geocode_address(address):
141
  # Check if the result is in cache
142
- cached_location = await cache.get(address)
143
- if cached_location:
144
- return cached_location
145
 
146
  # If not in cache, perform the geolocation request
147
  async with Nominatim(
@@ -151,8 +123,6 @@ async def geocode_address(address):
151
  location = await geolocator.geocode(address, timeout=10)
152
  if location:
153
  coords = {'lat': location.latitude, "lon": location.longitude}
154
- # Save the result in cache for future use
155
- await cache.aset(address, coords)
156
  return coords
157
  return None
158
 
@@ -162,11 +132,14 @@ async def ageocode_addresses(addresses):
162
  return locations
163
 
164
  def geocode_addresses(addresses):
165
- loop = asyncio.get_event_loop()
 
 
 
 
166
  result = loop.run_until_complete(ageocode_addresses(addresses))
167
  return result
168
 
169
-
170
  def create_map_from_markers(dataframe):
171
  coordinates = geocode_addresses(dataframe["name"])
172
  print({name: coordinates[i] for i, name in enumerate(dataframe["name"].to_list())})
@@ -228,7 +201,7 @@ with gr.Blocks(
228
  secondary_hue=gr.themes.colors.blue,
229
  )
230
  ) as demo:
231
- 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, the model does not really have access to train or plane schedules, it is relying on general world knowledge for its propositions.")
232
  text = gr.Textbox(
233
  label="Describe your ideal trip:",
234
  value=description_taiwan,
 
62
  return dataframe, rationale
63
 
64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  preset_values = {
66
  "Fisherman's Wharf, San Francisco": {'lat': 37.808332, 'lon': -122.415715},
67
  'Ghirardelli Square, San Francisco': {'lat': 37.80587075, 'lon': -122.42294914207058},
 
109
  'Fort Point National Historic Site, San Francisco': {'lat': 37.81045495, 'lon': -122.47713831312802},
110
  'Presidio of San Francisco': {'lat': 37.798745600000004, 'lon': -122.46458892410745}
111
  }
 
 
 
112
 
113
  async def geocode_address(address):
114
  # Check if the result is in cache
115
+ if address in preset_values:
116
+ return preset_values[address]
 
117
 
118
  # If not in cache, perform the geolocation request
119
  async with Nominatim(
 
123
  location = await geolocator.geocode(address, timeout=10)
124
  if location:
125
  coords = {'lat': location.latitude, "lon": location.longitude}
 
 
126
  return coords
127
  return None
128
 
 
132
  return locations
133
 
134
  def geocode_addresses(addresses):
135
+ try:
136
+ loop = asyncio.get_event_loop()
137
+ except RuntimeError:
138
+ loop = asyncio.new_event_loop()
139
+ asyncio.set_event_loop(loop)
140
  result = loop.run_until_complete(ageocode_addresses(addresses))
141
  return result
142
 
 
143
  def create_map_from_markers(dataframe):
144
  coordinates = geocode_addresses(dataframe["name"])
145
  print({name: coordinates[i] for i, name in enumerate(dataframe["name"].to_list())})
 
201
  secondary_hue=gr.themes.colors.blue,
202
  )
203
  ) as demo:
204
+ gr.Markdown("# 🗺️ AI Travel Planner 🏕️\nThis personal travel planner is based on Llama-3-70B-Instruct, called through the Hugging Face API. Describe your ideal trip below, and let our AI assistant guide you!\n Beware, the model does not really have access to train or plane schedules, it is relying on general world knowledge for its propositions.")
205
  text = gr.Textbox(
206
  label="Describe your ideal trip:",
207
  value=description_taiwan,