Pijush2023 commited on
Commit
6ce49e9
·
verified ·
1 Parent(s): aae0613

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -102
app.py CHANGED
@@ -277,117 +277,31 @@ 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
- # 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 format_restaurant_info(restaurant_info):
340
- if not isinstance(restaurant_info, list): # Check if it's not a list
341
- print("Error: restaurant_info is not a list") # Add a helpful error message
342
- return "Error: Unable to retrieve restaurant information."
343
-
344
- formatted_info = []
345
- for restaurant in restaurant_info:
346
- if not isinstance(restaurant, dict): # Ensure each item is a dictionary
347
- continue
348
- formatted_info.append(
349
- f"**Name:** {restaurant.get('name', 'N/A')}\n"
350
- f"**Rating:** {restaurant.get('rating', 'N/A')} stars\n"
351
- f"**Reviews:** {restaurant.get('reviews', 'N/A')}\n"
352
- f"**Phone:** {restaurant.get('phone', 'N/A')}\n"
353
- f"**Snippet:** {restaurant.get('snippet', 'N/A')}\n"
354
- f"**Services:** {restaurant.get('services', 'N/A')}\n"
355
- f"**Yelp URL:** [Link]({restaurant.get('link', '#')})\n"
356
- )
357
- return "\n\n".join(formatted_info)
358
-
359
 
360
- def fetch_yelp_restaurants():
361
- params = {
362
- "engine": "yelp",
363
- "find_desc": "Restaurant",
364
- "find_loc": "Birmingham, AL, USA", # Fixed location
365
- "api_key": os.getenv("SERP_API")
366
- }
367
 
368
- search = GoogleSearch(params)
369
- results = search.get_dict()
370
- organic_results = results.get("organic_results", [])
371
 
372
- yelp_data = []
373
- for result in organic_results:
374
- restaurant_info = {
375
- "name": result.get("title", "No name"),
376
- "rating": result.get("rating", "No rating"),
377
- "reviews": result.get("reviews", "No reviews"),
378
- "phone": result.get("phone", "Not Available"),
379
- "snippet": result.get("snippet", "Not Available"),
380
- "services": result.get("service_options", "Not Known"),
381
- "link": result.get("link", "#")
382
- }
383
- yelp_data.append(restaurant_info)
384
- return yelp_data
385
-
386
- def process_restaurant_query(query):
387
- restaurant_info = fetch_yelp_restaurants()
388
- print("DEBUG: restaurant_info =", restaurant_info) # Add this line for debugging
389
- formatted_info = format_restaurant_info(restaurant_info)
390
- return formatted_info
391
 
392
 
393
 
 
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
 
307