Pijush2023 commited on
Commit
1c8f285
·
verified ·
1 Parent(s): 6448cc2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +169 -51
app.py CHANGED
@@ -540,33 +540,184 @@ chain_neo4j = (
540
 
541
 
542
 
543
- import re
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
544
 
545
- def clean_response(response_text):
546
- # Remove any metadata-like information and focus on the main content
547
- # Removes "Document(metadata=...)" and other similar patterns
548
- cleaned_response = re.sub(r'Document\(metadata=.*?\),?\s*', '', response_text, flags=re.DOTALL)
549
- cleaned_response = re.sub(r'page_content=".*?"\),?', '', cleaned_response, flags=re.DOTALL)
550
- cleaned_response = re.sub(r'\[.*?\]', '', cleaned_response, flags=re.DOTALL) # Remove content in brackets
551
- cleaned_response = re.sub(r'\s+', ' ', cleaned_response).strip()
552
- # Remove any unwanted follow-up questions or unnecessary text
553
- cleaned_response = re.sub(r'Question:.*\nAnswer:', '', cleaned_response, flags=re.DOTALL).strip()
554
- return cleaned_response
555
 
556
 
557
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
558
 
559
- # Define the custom template for Phi-3.5
560
  phi_custom_template = """
561
  <|system|>
562
- You are a helpful assistant.<|end|>
563
  <|user|>
564
  {context}
565
- {question}<|end|>
566
  <|assistant|>
 
567
  """
568
- import traceback
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
569
 
 
 
570
  def generate_answer(message, choice, retrieval_mode, selected_model):
571
  logging.debug(f"generate_answer called with choice: {choice}, retrieval_mode: {retrieval_mode}, and selected_model: {selected_model}")
572
 
@@ -627,10 +778,10 @@ def generate_answer(message, choice, retrieval_mode, selected_model):
627
  logging.debug(f"Generated Phi-3.5 prompt: {prompt}")
628
 
629
  response = selected_model(prompt, **{
630
- "max_new_tokens": 160, # Increased to handle longer responses
631
  "return_full_text": True,
632
- "temperature": 0.7, # Adjusted to avoid cutting off
633
- "do_sample": True, # Allow sampling to increase response diversity
634
  })
635
 
636
  if response:
@@ -660,39 +811,6 @@ def generate_answer(message, choice, retrieval_mode, selected_model):
660
 
661
 
662
 
663
-
664
- def bot(history, choice, tts_choice, retrieval_mode, model_choice):
665
- if not history:
666
- return history
667
-
668
- # Select the model
669
- selected_model = chat_model if model_choice == "GPT-4o" else phi_pipe
670
-
671
- response, addresses = generate_answer(history[-1][0], choice, retrieval_mode, selected_model)
672
- history[-1][1] = ""
673
-
674
- with concurrent.futures.ThreadPoolExecutor() as executor:
675
- if tts_choice == "Alpha":
676
- audio_future = executor.submit(generate_audio_elevenlabs, response)
677
- elif tts_choice == "Beta":
678
- audio_future = executor.submit(generate_audio_parler_tts, response)
679
- # elif tts_choice == "Gamma":
680
- # audio_future = executor.submit(generate_audio_mars5, response)
681
-
682
- for character in response:
683
- history[-1][1] += character
684
- time.sleep(0.05)
685
- yield history, None
686
-
687
- audio_path = audio_future.result()
688
- yield history, audio_path
689
-
690
- history.append([response, None])
691
-
692
-
693
-
694
-
695
-
696
  def add_message(history, message):
697
  history.append((message, None))
698
  return history, gr.Textbox(value="", interactive=True, show_label=False)
 
540
 
541
 
542
 
543
+ # import re
544
+
545
+ # def clean_response(response_text):
546
+ # # Remove any metadata-like information and focus on the main content
547
+ # # Removes "Document(metadata=...)" and other similar patterns
548
+ # cleaned_response = re.sub(r'Document\(metadata=.*?\),?\s*', '', response_text, flags=re.DOTALL)
549
+ # cleaned_response = re.sub(r'page_content=".*?"\),?', '', cleaned_response, flags=re.DOTALL)
550
+ # cleaned_response = re.sub(r'\[.*?\]', '', cleaned_response, flags=re.DOTALL) # Remove content in brackets
551
+ # cleaned_response = re.sub(r'\s+', ' ', cleaned_response).strip()
552
+ # # Remove any unwanted follow-up questions or unnecessary text
553
+ # cleaned_response = re.sub(r'Question:.*\nAnswer:', '', cleaned_response, flags=re.DOTALL).strip()
554
+ # return cleaned_response
555
+
556
+
557
+
558
+
559
+ # # Define the custom template for Phi-3.5
560
+ # phi_custom_template = """
561
+ # <|system|>
562
+ # You are a helpful assistant.<|end|>
563
+ # <|user|>
564
+ # {context}
565
+ # {question}<|end|>
566
+ # <|assistant|>
567
+ # """
568
+ # import traceback
569
+
570
+ # def generate_answer(message, choice, retrieval_mode, selected_model):
571
+ # logging.debug(f"generate_answer called with choice: {choice}, retrieval_mode: {retrieval_mode}, and selected_model: {selected_model}")
572
+
573
+ # try:
574
+ # # Handle hotel-related queries
575
+ # if "hotel" in message.lower() or "hotels" in message.lower() and "birmingham" in message.lower():
576
+ # logging.debug("Handling hotel-related query")
577
+ # response = fetch_google_hotels()
578
+ # logging.debug(f"Hotel response: {response}")
579
+ # return response, extract_addresses(response)
580
+
581
+ # # Handle restaurant-related queries
582
+ # if "restaurant" in message.lower() or "restaurants" in message.lower() and "birmingham" in message.lower():
583
+ # logging.debug("Handling restaurant-related query")
584
+ # response = fetch_yelp_restaurants()
585
+ # logging.debug(f"Restaurant response: {response}")
586
+ # return response, extract_addresses(response)
587
+
588
+ # # Handle flight-related queries
589
+ # if "flight" in message.lower() or "flights" in message.lower() and "birmingham" in message.lower():
590
+ # logging.debug("Handling flight-related query")
591
+ # response = fetch_google_flights()
592
+ # logging.debug(f"Flight response: {response}")
593
+ # return response, extract_addresses(response)
594
+
595
+ # # Retrieval-based response
596
+ # if retrieval_mode == "VDB":
597
+ # logging.debug("Using VDB retrieval mode")
598
+ # if selected_model == chat_model:
599
+ # logging.debug("Selected model: GPT-4o")
600
+ # retriever = gpt_retriever
601
+ # prompt_template = QA_CHAIN_PROMPT_1 if choice == "Details" else QA_CHAIN_PROMPT_2
602
+ # context = retriever.get_relevant_documents(message)
603
+ # logging.debug(f"Retrieved context: {context}")
604
+
605
+ # prompt = prompt_template.format(context=context, question=message)
606
+ # logging.debug(f"Generated prompt: {prompt}")
607
+
608
+ # qa_chain = RetrievalQA.from_chain_type(
609
+ # llm=chat_model,
610
+ # chain_type="stuff",
611
+ # retriever=retriever,
612
+ # chain_type_kwargs={"prompt": prompt_template}
613
+ # )
614
+ # response = qa_chain({"query": message})
615
+ # logging.debug(f"GPT-4o response: {response}")
616
+ # return response['result'], extract_addresses(response['result'])
617
+
618
+ # elif selected_model == phi_pipe:
619
+ # logging.debug("Selected model: Phi-3.5")
620
+ # retriever = phi_retriever
621
+ # context_documents = retriever.get_relevant_documents(message)
622
+ # context = "\n".join([doc.page_content for doc in context_documents])
623
+ # logging.debug(f"Retrieved context for Phi-3.5: {context}")
624
+
625
+ # # Use the correct template variable
626
+ # prompt = phi_custom_template.format(context=context, question=message)
627
+ # logging.debug(f"Generated Phi-3.5 prompt: {prompt}")
628
+
629
+ # response = selected_model(prompt, **{
630
+ # "max_new_tokens": 160, # Increased to handle longer responses
631
+ # "return_full_text": True,
632
+ # "temperature": 0.7, # Adjusted to avoid cutting off
633
+ # "do_sample": True, # Allow sampling to increase response diversity
634
+ # })
635
+
636
+ # if response:
637
+ # generated_text = response[0]['generated_text']
638
+ # logging.debug(f"Phi-3.5 Response: {generated_text}")
639
+ # cleaned_response = clean_response(generated_text)
640
+ # return cleaned_response, extract_addresses(cleaned_response)
641
+ # else:
642
+ # logging.error("Phi-3.5 did not return any response.")
643
+ # return "No response generated.", []
644
+
645
+ # elif retrieval_mode == "KGF":
646
+ # logging.debug("Using KGF retrieval mode")
647
+ # response = chain_neo4j.invoke({"question": message})
648
+ # logging.debug(f"KGF response: {response}")
649
+ # return response, extract_addresses(response)
650
+ # else:
651
+ # logging.error("Invalid retrieval mode selected.")
652
+ # return "Invalid retrieval mode selected.", []
653
+
654
+ # except Exception as e:
655
+ # logging.error(f"Error in generate_answer: {str(e)}")
656
+ # logging.error(traceback.format_exc())
657
+ # return "Sorry, I encountered an error while processing your request.", []
658
+
659
+
660
 
 
 
 
 
 
 
 
 
 
 
661
 
662
 
663
 
664
+ # def bot(history, choice, tts_choice, retrieval_mode, model_choice):
665
+ # if not history:
666
+ # return history
667
+
668
+ # # Select the model
669
+ # selected_model = chat_model if model_choice == "GPT-4o" else phi_pipe
670
+
671
+ # response, addresses = generate_answer(history[-1][0], choice, retrieval_mode, selected_model)
672
+ # history[-1][1] = ""
673
+
674
+ # with concurrent.futures.ThreadPoolExecutor() as executor:
675
+ # if tts_choice == "Alpha":
676
+ # audio_future = executor.submit(generate_audio_elevenlabs, response)
677
+ # elif tts_choice == "Beta":
678
+ # audio_future = executor.submit(generate_audio_parler_tts, response)
679
+ # # elif tts_choice == "Gamma":
680
+ # # audio_future = executor.submit(generate_audio_mars5, response)
681
+
682
+ # for character in response:
683
+ # history[-1][1] += character
684
+ # time.sleep(0.05)
685
+ # yield history, None
686
+
687
+ # audio_path = audio_future.result()
688
+ # yield history, audio_path
689
+
690
+ # history.append([response, None])
691
+
692
 
 
693
  phi_custom_template = """
694
  <|system|>
695
+ You are a helpful assistant who provides clear, organized, and conversational responses.<|end|>
696
  <|user|>
697
  {context}
698
+ Question: {question}<|end|>
699
  <|assistant|>
700
+ Sure! Here's the information you requested:
701
  """
702
+ import re
703
+
704
+ def clean_response(response_text):
705
+ # Remove system and user tags
706
+ response_text = re.sub(r'<\|system\|>.*?<\|end\|>', '', response_text, flags=re.DOTALL)
707
+ response_text = re.sub(r'<\|user\|>.*?<\|end\|>', '', response_text, flags=re.DOTALL)
708
+ response_text = re.sub(r'<\|assistant\|>', '', response_text, flags=re.DOTALL)
709
+
710
+ # Clean up the text by removing extra whitespace
711
+ cleaned_response = response_text.strip()
712
+ cleaned_response = re.sub(r'\s+', ' ', cleaned_response)
713
+
714
+ # Ensure the response is conversational and organized
715
+ cleaned_response = cleaned_response.replace('1.', '\n1.').replace('2.', '\n2.').replace('3.', '\n3.').replace('4.', '\n4.').replace('5.', '\n5.')
716
+
717
+ return cleaned_response
718
 
719
+
720
+ import traceback
721
  def generate_answer(message, choice, retrieval_mode, selected_model):
722
  logging.debug(f"generate_answer called with choice: {choice}, retrieval_mode: {retrieval_mode}, and selected_model: {selected_model}")
723
 
 
778
  logging.debug(f"Generated Phi-3.5 prompt: {prompt}")
779
 
780
  response = selected_model(prompt, **{
781
+ "max_new_tokens": 160,
782
  "return_full_text": True,
783
+ "temperature": 0.7,
784
+ "do_sample": True,
785
  })
786
 
787
  if response:
 
811
 
812
 
813
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
814
  def add_message(history, message):
815
  history.append((message, None))
816
  return history, gr.Textbox(value="", interactive=True, show_label=False)