Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -277,30 +277,110 @@ def generate_answer(message, choice, retrieval_mode):
|
|
277 |
else:
|
278 |
return "Invalid retrieval mode selected.", []
|
279 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
280 |
def bot(history, choice, tts_choice, retrieval_mode):
|
281 |
if not history:
|
282 |
return history
|
283 |
|
284 |
-
|
285 |
-
history[-1][1] = ""
|
286 |
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
294 |
|
295 |
-
|
296 |
-
history[-1][1] += character
|
297 |
-
time.sleep(0.05)
|
298 |
-
yield history, None
|
299 |
|
300 |
-
audio_path = audio_future.result()
|
301 |
-
yield history, audio_path
|
302 |
|
303 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
304 |
|
305 |
|
306 |
|
|
|
277 |
else:
|
278 |
return "Invalid retrieval mode selected.", []
|
279 |
|
280 |
+
# def bot(history, choice, tts_choice, retrieval_mode):
|
281 |
+
# if not history:
|
282 |
+
# return history
|
283 |
+
|
284 |
+
# response, addresses = generate_answer(history[-1][0], choice, retrieval_mode)
|
285 |
+
# history[-1][1] = ""
|
286 |
+
|
287 |
+
# with concurrent.futures.ThreadPoolExecutor() as executor:
|
288 |
+
# if tts_choice == "Alpha":
|
289 |
+
# audio_future = executor.submit(generate_audio_elevenlabs, response)
|
290 |
+
# elif tts_choice == "Beta":
|
291 |
+
# audio_future = executor.submit(generate_audio_parler_tts, response)
|
292 |
+
# elif tts_choice == "Gamma":
|
293 |
+
# audio_future = executor.submit(generate_audio_mars5, response)
|
294 |
+
|
295 |
+
# for character in response:
|
296 |
+
# history[-1][1] += character
|
297 |
+
# time.sleep(0.05)
|
298 |
+
# yield history, None
|
299 |
+
|
300 |
+
# audio_path = audio_future.result()
|
301 |
+
# yield history, audio_path
|
302 |
+
|
303 |
+
# history.append([response, None]) # Ensure the response is added in the correct format
|
304 |
+
|
305 |
+
|
306 |
def bot(history, choice, tts_choice, retrieval_mode):
|
307 |
if not history:
|
308 |
return history
|
309 |
|
310 |
+
user_message = history[-1][0].lower()
|
|
|
311 |
|
312 |
+
# Check if the query is related to restaurants
|
313 |
+
if "restaurant" in user_message:
|
314 |
+
response = process_restaurant_query(user_message)
|
315 |
+
else:
|
316 |
+
# Continue with the normal process if not a restaurant query
|
317 |
+
response, addresses = generate_answer(user_message, choice, retrieval_mode)
|
318 |
+
history[-1][1] = ""
|
319 |
+
|
320 |
+
with concurrent.futures.ThreadPoolExecutor() as executor:
|
321 |
+
if tts_choice == "Alpha":
|
322 |
+
audio_future = executor.submit(generate_audio_elevenlabs, response)
|
323 |
+
elif tts_choice == "Beta":
|
324 |
+
audio_future = executor.submit(generate_audio_parler_tts, response)
|
325 |
+
elif tts_choice == "Gamma":
|
326 |
+
audio_future = executor.submit(generate_audio_mars5, response)
|
327 |
+
|
328 |
+
for character in response:
|
329 |
+
history[-1][1] += character
|
330 |
+
time.sleep(0.05)
|
331 |
+
yield history, None
|
332 |
+
|
333 |
+
audio_path = audio_future.result()
|
334 |
+
yield history, audio_path
|
335 |
|
336 |
+
history.append([response, None]) # Ensure the response is added in the correct format
|
|
|
|
|
|
|
337 |
|
|
|
|
|
338 |
|
339 |
+
def fetch_yelp_restaurants(location="Birmingham, AL, USA", term="Restaurant"):
|
340 |
+
params = {
|
341 |
+
"engine": "yelp",
|
342 |
+
"find_desc": term,
|
343 |
+
"find_loc": location,
|
344 |
+
"api_key": os.getenv("SERP_API")
|
345 |
+
}
|
346 |
+
|
347 |
+
search = GoogleSearch(params)
|
348 |
+
results = search.get_dict()
|
349 |
+
organic_results = results.get("organic_results", [])
|
350 |
+
|
351 |
+
yelp_data = []
|
352 |
+
for result in organic_results:
|
353 |
+
restaurant_info = {
|
354 |
+
"name": result.get("title", "No name"),
|
355 |
+
"rating": result.get("rating", "No rating"),
|
356 |
+
"reviews": result.get("reviews", "No reviews"),
|
357 |
+
"phone": result.get("phone", "Not Available"),
|
358 |
+
"snippet": result.get("snippet", "Not Available"),
|
359 |
+
"services": result.get("service_options", "Not Known"),
|
360 |
+
"link": result.get("link", "#")
|
361 |
+
}
|
362 |
+
yelp_data.append(restaurant_info)
|
363 |
+
return yelp_data
|
364 |
+
|
365 |
+
def format_restaurant_info(restaurant_info):
|
366 |
+
formatted_info = []
|
367 |
+
for restaurant in restaurant_info:
|
368 |
+
formatted_info.append(
|
369 |
+
f"**Name:** {restaurant['name']}\n"
|
370 |
+
f"**Rating:** {restaurant['rating']} stars\n"
|
371 |
+
f"**Reviews:** {restaurant['reviews']}\n"
|
372 |
+
f"**Phone:** {restaurant['phone']}\n"
|
373 |
+
f"**Snippet:** {restaurant['snippet']}\n"
|
374 |
+
f"**Services:** {restaurant['services']}\n"
|
375 |
+
f"**Yelp URL:** [Link]({restaurant['link']})\n"
|
376 |
+
)
|
377 |
+
return "\n\n".join(formatted_info)
|
378 |
+
|
379 |
+
def process_restaurant_query(query):
|
380 |
+
location = "Birmingham, AL, USA" # Default location, can be extracted from query if needed
|
381 |
+
restaurant_info = fetch_yelp_restaurants(location=location, term=query)
|
382 |
+
formatted_info = format_restaurant_info(restaurant_info)
|
383 |
+
return formatted_info
|
384 |
|
385 |
|
386 |
|