Pijush2023 commited on
Commit
103eef7
·
verified ·
1 Parent(s): c48f8ec

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -11
app.py CHANGED
@@ -1233,7 +1233,7 @@ def neo4j_retrieval_conversational(question: str):
1233
  structured_data = structured_retriever(question)
1234
  logging.debug(f"Structured data (Conversational): {structured_data}")
1235
  prompt = QA_CHAIN_PROMPT_NEO4J_CONVERSATIONAL.format(context=structured_data, question=question)
1236
- response = chat_model(prompt)
1237
  return response, []
1238
 
1239
  # Neo4j Retrieval chain for detailed mode
@@ -1241,7 +1241,7 @@ def neo4j_retrieval_details(question: str):
1241
  structured_data = structured_retriever(question)
1242
  logging.debug(f"Structured data (Details): {structured_data}")
1243
  prompt = QA_CHAIN_PROMPT_NEO4J_DETAILS.format(context=structured_data, question=question)
1244
- response = chat_model(prompt)
1245
  return response, extract_addresses(response)
1246
 
1247
  # Update the generate_answer function to include Neo4j retrieval modes
@@ -1270,24 +1270,26 @@ def generate_answer(message, choice, retrieval_mode):
1270
  else:
1271
  return "Invalid retrieval mode selected.", []
1272
 
1273
-
1274
 
1275
  def bot(history, choice, tts_choice, retrieval_mode):
1276
  if not history:
1277
  return history
1278
 
1279
  response, addresses = generate_answer(history[-1][0], choice, retrieval_mode)
 
 
1280
  history[-1][1] = ""
1281
 
1282
  with concurrent.futures.ThreadPoolExecutor() as executor:
1283
  if tts_choice == "Alpha":
1284
- audio_future = executor.submit(generate_audio_elevenlabs, response)
1285
  elif tts_choice == "Beta":
1286
- audio_future = executor.submit(generate_audio_parler_tts, response)
1287
  elif tts_choice == "Gamma":
1288
- audio_future = executor.submit(generate_audio_mars5, response)
1289
 
1290
- for character in response:
1291
  history[-1][1] += character
1292
  time.sleep(0.05)
1293
  yield history, None
@@ -1295,7 +1297,7 @@ def bot(history, choice, tts_choice, retrieval_mode):
1295
  audio_path = audio_future.result()
1296
  yield history, audio_path
1297
 
1298
- history.append([response, None]) # Ensure the response is added in the correct format
1299
 
1300
  def add_message(history, message):
1301
  history.append((message, None))
@@ -1351,7 +1353,7 @@ def fetch_local_news():
1351
  api_key = os.environ['SERP_API']
1352
  url = f'https://serpapi.com/search.json?engine=google_news&q=birmingham headline&api_key={api_key}'
1353
  response = requests.get(url)
1354
- if response.status_code == 200:
1355
  results = response.json().get("news_results", [])
1356
  news_html = """
1357
  <h2 style="font-family: 'Georgia', serif; color: #ff0000; background-color: #f8f8f8; padding: 10px; border-radius: 10px;">Birmingham Today</h2>
@@ -1469,7 +1471,7 @@ def show_map_if_details(history, choice):
1469
  if choice in ["Details", "Conversational"]:
1470
  return gr.update(visible=True), update_map_with_response(history)
1471
  else:
1472
- return gr.update(visible=False), ""
1473
 
1474
  def generate_audio_elevenlabs(text):
1475
  XI_API_KEY = os.environ['ELEVENLABS_API']
@@ -1644,7 +1646,7 @@ def fetch_local_events():
1644
  api_key = os.environ['SERP_API']
1645
  url = f'https://serpapi.com/search.json?engine=google_events&q=Events+in+Birmingham&hl=en&gl=us&api_key={api_key}'
1646
  response = requests.get(url)
1647
- if response.status_code == 200:
1648
  events_results = response.json().get("events_results", [])
1649
  events_html = """
1650
  <h2 style="font-family: 'Georgia', serif; color: #ff0000; background-color: #f8f8f8; padding: 10px; border-radius: 10px;">Local Events</h2>
 
1233
  structured_data = structured_retriever(question)
1234
  logging.debug(f"Structured data (Conversational): {structured_data}")
1235
  prompt = QA_CHAIN_PROMPT_NEO4J_CONVERSATIONAL.format(context=structured_data, question=question)
1236
+ response = chat_model(prompt.to_messages())
1237
  return response, []
1238
 
1239
  # Neo4j Retrieval chain for detailed mode
 
1241
  structured_data = structured_retriever(question)
1242
  logging.debug(f"Structured data (Details): {structured_data}")
1243
  prompt = QA_CHAIN_PROMPT_NEO4J_DETAILS.format(context=structured_data, question=question)
1244
+ response = chat_model(prompt.to_messages())
1245
  return response, extract_addresses(response)
1246
 
1247
  # Update the generate_answer function to include Neo4j retrieval modes
 
1270
  else:
1271
  return "Invalid retrieval mode selected.", []
1272
 
1273
+ # Rest of the code remains the same
1274
 
1275
  def bot(history, choice, tts_choice, retrieval_mode):
1276
  if not history:
1277
  return history
1278
 
1279
  response, addresses = generate_answer(history[-1][0], choice, retrieval_mode)
1280
+ response_text = response if isinstance(response, str) else response[0]
1281
+
1282
  history[-1][1] = ""
1283
 
1284
  with concurrent.futures.ThreadPoolExecutor() as executor:
1285
  if tts_choice == "Alpha":
1286
+ audio_future = executor.submit(generate_audio_elevenlabs, response_text)
1287
  elif tts_choice == "Beta":
1288
+ audio_future = executor.submit(generate_audio_parler_tts, response_text)
1289
  elif tts_choice == "Gamma":
1290
+ audio_future = executor.submit(generate_audio_mars5, response_text)
1291
 
1292
+ for character in response_text:
1293
  history[-1][1] += character
1294
  time.sleep(0.05)
1295
  yield history, None
 
1297
  audio_path = audio_future.result()
1298
  yield history, audio_path
1299
 
1300
+ history.append([response_text, None]) # Ensure the response is added in the correct format
1301
 
1302
  def add_message(history, message):
1303
  history.append((message, None))
 
1353
  api_key = os.environ['SERP_API']
1354
  url = f'https://serpapi.com/search.json?engine=google_news&q=birmingham headline&api_key={api_key}'
1355
  response = requests.get(url)
1356
+ if response.status_code == 200):
1357
  results = response.json().get("news_results", [])
1358
  news_html = """
1359
  <h2 style="font-family: 'Georgia', serif; color: #ff0000; background-color: #f8f8f8; padding: 10px; border-radius: 10px;">Birmingham Today</h2>
 
1471
  if choice in ["Details", "Conversational"]:
1472
  return gr.update(visible=True), update_map_with_response(history)
1473
  else:
1474
+ return gr.update(visible(False), "")
1475
 
1476
  def generate_audio_elevenlabs(text):
1477
  XI_API_KEY = os.environ['ELEVENLABS_API']
 
1646
  api_key = os.environ['SERP_API']
1647
  url = f'https://serpapi.com/search.json?engine=google_events&q=Events+in+Birmingham&hl=en&gl=us&api_key={api_key}'
1648
  response = requests.get(url)
1649
+ if response.status_code == 200):
1650
  events_results = response.json().get("events_results", [])
1651
  events_html = """
1652
  <h2 style="font-family: 'Georgia', serif; color: #ff0000; background-color: #f8f8f8; padding: 10px; border-radius: 10px;">Local Events</h2>