Pijush2023 commited on
Commit
1ea6432
·
verified ·
1 Parent(s): bb26c86

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +122 -21
app.py CHANGED
@@ -402,24 +402,105 @@ Answer:
402
  """
403
 
404
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
405
  import re
 
406
 
407
  def clean_response(response_text):
408
  # Remove any metadata-like information and focus on the main content
409
- # Removes "Document(metadata=...)" and other similar patterns
410
  cleaned_response = re.sub(r'Document\(metadata=.*?\),?\s*', '', response_text, flags=re.DOTALL)
411
  cleaned_response = re.sub(r'page_content=".*?"\),?', '', cleaned_response, flags=re.DOTALL)
412
  cleaned_response = re.sub(r'\[.*?\]', '', cleaned_response, flags=re.DOTALL) # Remove content in brackets
413
  cleaned_response = re.sub(r'\s+', ' ', cleaned_response).strip()
414
- #Remove any unwanted follow-up questions or unnecessary text
415
- cleaned_response = re.sub(r'Question:.*\nAnswer:', '', response_text, flags=re.DOTALL).strip()
416
  return cleaned_response
417
-
418
 
419
  def generate_answer(message, choice, retrieval_mode, selected_model):
420
  logging.debug(f"generate_answer called with choice: {choice} and retrieval_mode: {retrieval_mode}")
421
 
422
  try:
 
 
423
  if "hotel" in message.lower() or "hotels" in message.lower() and "birmingham" in message.lower():
424
  response = fetch_google_hotels()
425
  return response, extract_addresses(response)
@@ -455,21 +536,43 @@ def generate_answer(message, choice, retrieval_mode, selected_model):
455
 
456
  logging.debug(f"Phi-3.5 Prompt: {prompt}")
457
 
458
- response = selected_model(prompt, **{
459
- "max_new_tokens": 128, # Increased to handle longer responses
460
- "return_full_text": False,
461
- "temperature": 0.7, # Adjusted to avoid cutting off
462
- "do_sample": True, # Allow sampling to increase response diversity
463
- })
464
-
465
- if response:
466
- generated_text = response[0]['generated_text']
467
- logging.debug(f"Phi-3.5 Response: {generated_text}")
468
- cleaned_response = clean_response(generated_text)
469
- return cleaned_response, extract_addresses(cleaned_response)
470
- else:
471
- logging.error("Phi-3.5 did not return any response.")
472
- return "No response generated.", []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
473
 
474
  elif retrieval_mode == "KGF":
475
  response = chain_neo4j.invoke({"question": message})
@@ -488,8 +591,6 @@ def generate_answer(message, choice, retrieval_mode, selected_model):
488
 
489
 
490
 
491
-
492
-
493
  def bot(history, choice, tts_choice, retrieval_mode, model_choice):
494
  if not history:
495
  return history
 
402
  """
403
 
404
 
405
+ # import re
406
+
407
+ # def clean_response(response_text):
408
+ # # Remove any metadata-like information and focus on the main content
409
+ # # Removes "Document(metadata=...)" and other similar patterns
410
+ # cleaned_response = re.sub(r'Document\(metadata=.*?\),?\s*', '', response_text, flags=re.DOTALL)
411
+ # cleaned_response = re.sub(r'page_content=".*?"\),?', '', cleaned_response, flags=re.DOTALL)
412
+ # cleaned_response = re.sub(r'\[.*?\]', '', cleaned_response, flags=re.DOTALL) # Remove content in brackets
413
+ # cleaned_response = re.sub(r'\s+', ' ', cleaned_response).strip()
414
+ # #Remove any unwanted follow-up questions or unnecessary text
415
+ # cleaned_response = re.sub(r'Question:.*\nAnswer:', '', response_text, flags=re.DOTALL).strip()
416
+ # return cleaned_response
417
+
418
+
419
+ # def generate_answer(message, choice, retrieval_mode, selected_model):
420
+ # logging.debug(f"generate_answer called with choice: {choice} and retrieval_mode: {retrieval_mode}")
421
+
422
+ # try:
423
+ # if "hotel" in message.lower() or "hotels" in message.lower() and "birmingham" in message.lower():
424
+ # response = fetch_google_hotels()
425
+ # return response, extract_addresses(response)
426
+
427
+ # if "restaurant" in message.lower() or "restaurants" in message.lower() and "birmingham" in message.lower():
428
+ # response = fetch_yelp_restaurants()
429
+ # return response, extract_addresses(response)
430
+
431
+ # if "flight" in message.lower() or "flights" in message.lower() and "birmingham" in message.lower():
432
+ # response = fetch_google_flights()
433
+ # return response, extract_addresses(response)
434
+
435
+ # if retrieval_mode == "VDB":
436
+ # if selected_model == chat_model:
437
+ # retriever = gpt_retriever
438
+ # prompt_template = QA_CHAIN_PROMPT_1 if choice == "Details" else QA_CHAIN_PROMPT_2
439
+ # context = retriever.get_relevant_documents(message)
440
+ # prompt = prompt_template.format(context=context, question=message)
441
+
442
+ # qa_chain = RetrievalQA.from_chain_type(
443
+ # llm=chat_model,
444
+ # chain_type="stuff",
445
+ # retriever=retriever,
446
+ # chain_type_kwargs={"prompt": prompt_template}
447
+ # )
448
+ # response = qa_chain({"query": message})
449
+ # return response['result'], extract_addresses(response['result'])
450
+
451
+ # elif selected_model == phi_pipe:
452
+ # retriever = phi_retriever
453
+ # context = retriever.get_relevant_documents(message)
454
+ # prompt = phi_short_template.format(context=context, question=message)
455
+
456
+ # logging.debug(f"Phi-3.5 Prompt: {prompt}")
457
+
458
+ # response = selected_model(prompt, **{
459
+ # "max_new_tokens": 128, # Increased to handle longer responses
460
+ # "return_full_text": False,
461
+ # "temperature": 0.7, # Adjusted to avoid cutting off
462
+ # "do_sample": True, # Allow sampling to increase response diversity
463
+ # })
464
+
465
+ # if response:
466
+ # generated_text = response[0]['generated_text']
467
+ # logging.debug(f"Phi-3.5 Response: {generated_text}")
468
+ # cleaned_response = clean_response(generated_text)
469
+ # return cleaned_response, extract_addresses(cleaned_response)
470
+ # else:
471
+ # logging.error("Phi-3.5 did not return any response.")
472
+ # return "No response generated.", []
473
+
474
+ # elif retrieval_mode == "KGF":
475
+ # response = chain_neo4j.invoke({"question": message})
476
+ # return response, extract_addresses(response)
477
+ # else:
478
+ # return "Invalid retrieval mode selected.", []
479
+
480
+ # except Exception as e:
481
+ # logging.error(f"Error in generate_answer: {e}")
482
+ # return "Sorry, I encountered an error while processing your request.", []
483
+
484
+
485
  import re
486
+ import time
487
 
488
  def clean_response(response_text):
489
  # Remove any metadata-like information and focus on the main content
 
490
  cleaned_response = re.sub(r'Document\(metadata=.*?\),?\s*', '', response_text, flags=re.DOTALL)
491
  cleaned_response = re.sub(r'page_content=".*?"\),?', '', cleaned_response, flags=re.DOTALL)
492
  cleaned_response = re.sub(r'\[.*?\]', '', cleaned_response, flags=re.DOTALL) # Remove content in brackets
493
  cleaned_response = re.sub(r'\s+', ' ', cleaned_response).strip()
494
+ # Remove any unwanted follow-up questions or unnecessary text
495
+ cleaned_response = re.sub(r'Question:.*\nAnswer:', '', cleaned_response, flags=re.DOTALL).strip()
496
  return cleaned_response
 
497
 
498
  def generate_answer(message, choice, retrieval_mode, selected_model):
499
  logging.debug(f"generate_answer called with choice: {choice} and retrieval_mode: {retrieval_mode}")
500
 
501
  try:
502
+ start_time = time.time()
503
+
504
  if "hotel" in message.lower() or "hotels" in message.lower() and "birmingham" in message.lower():
505
  response = fetch_google_hotels()
506
  return response, extract_addresses(response)
 
536
 
537
  logging.debug(f"Phi-3.5 Prompt: {prompt}")
538
 
539
+ total_response = ""
540
+ stop_generation = False
541
+
542
+ while not stop_generation:
543
+ elapsed_time = time.time() - start_time
544
+ if elapsed_time > 5:
545
+ logging.warning("Stopping generation due to time limit.")
546
+ break # Stop generation if it exceeds 5 seconds
547
+
548
+ response = selected_model(prompt, **{
549
+ "max_new_tokens": 256, # Generate more tokens at a time to reduce iterations
550
+ "return_full_text": False,
551
+ "temperature": 0.7, # Adjusted to avoid cutting off
552
+ "do_sample": True, # Allow sampling to increase response diversity
553
+ })
554
+
555
+ if response:
556
+ generated_text = response[0]['generated_text']
557
+ logging.debug(f"Phi-3.5 Partial Response: {generated_text}")
558
+
559
+ # Append the new text to the total response
560
+ total_response += " " + generated_text
561
+
562
+ # Check if the generated text is starting to be irrelevant or the response seems complete
563
+ if len(generated_text.strip()) < 256 or "Thank you" in generated_text or generated_text.endswith('.'):
564
+ stop_generation = True
565
+
566
+ # Update the prompt to continue the context
567
+ prompt = generated_text.strip()
568
+
569
+ else:
570
+ logging.error("Phi-3.5 did not return any response.")
571
+ return "No response generated.", []
572
+
573
+ # Final cleanup of the response
574
+ cleaned_response = clean_response(total_response)
575
+ return cleaned_response, extract_addresses(cleaned_response)
576
 
577
  elif retrieval_mode == "KGF":
578
  response = chain_neo4j.invoke({"question": message})
 
591
 
592
 
593
 
 
 
594
  def bot(history, choice, tts_choice, retrieval_mode, model_choice):
595
  if not history:
596
  return history