Pijush2023 commited on
Commit
a2add61
·
verified ·
1 Parent(s): 7f3430b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +1328 -19
app.py CHANGED
@@ -1,3 +1,1311 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import requests
3
  import os
@@ -339,7 +1647,7 @@ def generate_bot_response(history, choice, retrieval_mode, model_choice):
339
  return
340
 
341
  # Select the model
342
- selected_model = chat_model if model_choice == "GPT-4o" else phi_pipe
343
 
344
  response, addresses = generate_answer(history[-1][0], choice, retrieval_mode, selected_model)
345
  history[-1][1] = ""
@@ -407,7 +1715,7 @@ def generate_bot_response(history, choice, retrieval_mode, model_choice):
407
  return
408
 
409
  # Select the model
410
- selected_model = chat_model if model_choice == "GPT-4o" else phi_pipe
411
 
412
  response, addresses = generate_answer(history[-1][0], choice, retrieval_mode, selected_model)
413
  history[-1][1] = ""
@@ -455,7 +1763,7 @@ def generate_answer(message, choice, retrieval_mode, selected_model):
455
  logging.debug(f"generate_answer called with choice: {choice}, retrieval_mode: {retrieval_mode}, and selected_model: {selected_model}")
456
 
457
  # Logic for disabling options for Phi-3.5
458
- if selected_model == "Phi-3.5":
459
  choice = None
460
  retrieval_mode = None
461
 
@@ -493,7 +1801,7 @@ def generate_answer(message, choice, retrieval_mode, selected_model):
493
  if retrieval_mode == "VDB":
494
  logging.debug("Using VDB retrieval mode")
495
  if selected_model == chat_model:
496
- logging.debug("Selected model: GPT-4o")
497
  retriever = gpt_retriever
498
  context = retriever.get_relevant_documents(message)
499
  logging.debug(f"Retrieved context: {context}")
@@ -508,19 +1816,19 @@ def generate_answer(message, choice, retrieval_mode, selected_model):
508
  chain_type_kwargs={"prompt": prompt_template}
509
  )
510
  response = qa_chain({"query": message})
511
- logging.debug(f"GPT-4o response: {response}")
512
  return response['result'], extract_addresses(response['result'])
513
 
514
  elif selected_model == phi_pipe:
515
- logging.debug("Selected model: Phi-3.5")
516
  retriever = phi_retriever
517
  context_documents = retriever.get_relevant_documents(message)
518
  context = "\n".join([doc.page_content for doc in context_documents])
519
- logging.debug(f"Retrieved context for Phi-3.5: {context}")
520
 
521
  # Use the correct template variable
522
  prompt = phi_custom_template.format(context=context, question=message)
523
- logging.debug(f"Generated Phi-3.5 prompt: {prompt}")
524
 
525
  response = selected_model(prompt, **{
526
  "max_new_tokens": 400,
@@ -531,11 +1839,11 @@ def generate_answer(message, choice, retrieval_mode, selected_model):
531
 
532
  if response:
533
  generated_text = response[0]['generated_text']
534
- logging.debug(f"Phi-3.5 Response: {generated_text}")
535
  cleaned_response = clean_response(generated_text)
536
  return cleaned_response, extract_addresses(cleaned_response)
537
  else:
538
- logging.error("Phi-3.5 did not return any response.")
539
  return "No response generated.", []
540
 
541
  elif retrieval_mode == "KGF":
@@ -802,7 +2110,7 @@ def generate_audio_elevenlabs(text):
802
  import concurrent.futures
803
  import tempfile
804
  import os
805
- import numpy as np
806
  import logging
807
  from queue import Queue
808
  from threading import Thread
@@ -1027,15 +2335,15 @@ def handle_retrieval_mode_change(choice):
1027
  return gr.update(interactive=True), gr.update(interactive=True)
1028
 
1029
  def handle_model_choice_change(selected_model):
1030
- if selected_model == "Phi-3.5":
1031
- # Disable retrieval mode and select style when Phi-3.5 is selected
1032
- return gr.update(interactive=False), gr.update(interactive=False), gr.update(interactive=False)
1033
- elif selected_model == "GPT-4o":
1034
- # Enable retrieval mode and select style for GPT-4o
1035
- return gr.update(interactive=True), gr.update(interactive=True), gr.update(interactive=True)
1036
  else:
1037
  # Default case: allow interaction
1038
- return gr.update(interactive=True), gr.update(interactive=True), gr.update(interactive=True)
1039
 
1040
 
1041
 
@@ -1216,7 +2524,7 @@ with gr.Blocks(theme='Pijush2023/scikit-learn-pijush') as demo:
1216
  chatbot = gr.Chatbot([], elem_id="RADAR:Channel 94.1", bubble_full_width=False)
1217
  choice = gr.Radio(label="Select Style", choices=["Details", "Conversational"], value="Conversational")
1218
  retrieval_mode = gr.Radio(label="Retrieval Mode", choices=["VDB", "KGF"], value="VDB")
1219
- model_choice = gr.Dropdown(label="Choose Model", choices=["GPT-4o", "Phi-3.5"], value="GPT-4o")
1220
 
1221
  # Link the dropdown change to handle_model_choice_change
1222
  model_choice.change(fn=handle_model_choice_change, inputs=model_choice, outputs=[retrieval_mode, choice, choice])
@@ -1302,3 +2610,4 @@ with gr.Blocks(theme='Pijush2023/scikit-learn-pijush') as demo:
1302
 
1303
  demo.queue()
1304
  demo.launch(show_error=True)
 
 
1
+ # import gradio as gr
2
+ # import requests
3
+ # import os
4
+ # import time
5
+ # import re
6
+ # import logging
7
+ # import tempfile
8
+ # import folium
9
+ # import concurrent.futures
10
+ # import torch
11
+ # from PIL import Image
12
+ # from datetime import datetime
13
+ # from transformers import pipeline, AutoModelForSpeechSeq2Seq, AutoProcessor
14
+ # from googlemaps import Client as GoogleMapsClient
15
+ # from gtts import gTTS
16
+ # from diffusers import StableDiffusionPipeline
17
+ # from langchain_openai import OpenAIEmbeddings, ChatOpenAI
18
+ # from langchain_pinecone import PineconeVectorStore
19
+ # from langchain.prompts import PromptTemplate
20
+ # from langchain.chains import RetrievalQA
21
+ # from langchain.chains.conversation.memory import ConversationBufferWindowMemory
22
+ # from huggingface_hub import login
23
+ # from transformers.models.speecht5.number_normalizer import EnglishNumberNormalizer
24
+ # from parler_tts import ParlerTTSForConditionalGeneration
25
+ # from transformers import AutoTokenizer, AutoFeatureExtractor, set_seed
26
+ # from scipy.io.wavfile import write as write_wav
27
+ # from pydub import AudioSegment
28
+ # from string import punctuation
29
+ # import librosa
30
+ # from pathlib import Path
31
+ # import torchaudio
32
+ # import numpy as np
33
+ # from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
34
+
35
+
36
+ # # Neo4j imports
37
+ # from langchain.chains import GraphCypherQAChain
38
+ # from langchain_community.graphs import Neo4jGraph
39
+ # from langchain_community.document_loaders import HuggingFaceDatasetLoader
40
+ # from langchain_text_splitters import CharacterTextSplitter
41
+ # from langchain_experimental.graph_transformers import LLMGraphTransformer
42
+ # from langchain_core.prompts import ChatPromptTemplate
43
+ # from langchain_core.pydantic_v1 import BaseModel, Field
44
+ # from langchain_core.messages import AIMessage, HumanMessage
45
+ # from langchain_core.output_parsers import StrOutputParser
46
+ # from langchain_core.runnables import RunnableBranch, RunnableLambda, RunnableParallel, RunnablePassthrough
47
+ # from serpapi.google_search import GoogleSearch
48
+
49
+ # #Parler TTS v1 Modules
50
+
51
+ # import os
52
+ # import re
53
+ # import tempfile
54
+ # import soundfile as sf
55
+ # from string import punctuation
56
+ # from pydub import AudioSegment
57
+ # from transformers import AutoTokenizer, AutoFeatureExtractor
58
+
59
+
60
+
61
+ # #API AutoDate Fix Up
62
+ # def get_current_date1():
63
+ # return datetime.now().strftime("%Y-%m-%d")
64
+
65
+ # # Usage
66
+ # current_date1 = get_current_date1()
67
+
68
+
69
+
70
+ # # Set environment variables for CUDA
71
+ # os.environ['PYTORCH_USE_CUDA_DSA'] = '1'
72
+ # os.environ['CUDA_LAUNCH_BLOCKING'] = '1'
73
+
74
+
75
+ # hf_token = os.getenv("HF_TOKEN")
76
+ # if hf_token is None:
77
+ # print("Please set your Hugging Face token in the environment variables.")
78
+ # else:
79
+ # login(token=hf_token)
80
+
81
+ # logging.basicConfig(level=logging.DEBUG)
82
+
83
+
84
+ # embeddings = OpenAIEmbeddings(api_key=os.environ['OPENAI_API_KEY'])
85
+
86
+
87
+ # #Initialization
88
+
89
+ # # Initialize the models
90
+ # def initialize_phi_model():
91
+ # model = AutoModelForCausalLM.from_pretrained(
92
+ # "microsoft/Phi-3.5-mini-instruct",
93
+ # device_map="cuda",
94
+ # torch_dtype="auto",
95
+ # trust_remote_code=True,
96
+ # )
97
+ # tokenizer = AutoTokenizer.from_pretrained("microsoft/Phi-3.5-mini-instruct")
98
+ # return pipeline("text-generation", model=model, tokenizer=tokenizer)
99
+
100
+ # def initialize_gpt_model():
101
+ # return ChatOpenAI(api_key=os.environ['OPENAI_API_KEY'], temperature=0, model='gpt-4o')
102
+
103
+ # # Initialize both models
104
+ # phi_pipe = initialize_phi_model()
105
+ # gpt_model = initialize_gpt_model()
106
+
107
+
108
+ # # Existing embeddings and vector store for GPT-4o
109
+ # gpt_embeddings = OpenAIEmbeddings(api_key=os.environ['OPENAI_API_KEY'])
110
+ # gpt_vectorstore = PineconeVectorStore(index_name="radarfinaldata08192024", embedding=gpt_embeddings)
111
+ # gpt_retriever = gpt_vectorstore.as_retriever(search_kwargs={'k': 5})
112
+
113
+ # # New vector store setup for Phi-3.5
114
+ # phi_embeddings = OpenAIEmbeddings(api_key=os.environ['OPENAI_API_KEY'])
115
+ # phi_vectorstore = PineconeVectorStore(index_name="phivector08252024", embedding=phi_embeddings)
116
+ # phi_retriever = phi_vectorstore.as_retriever(search_kwargs={'k': 5})
117
+
118
+
119
+
120
+ # # Pinecone setup
121
+ # from pinecone import Pinecone
122
+ # pc = Pinecone(api_key=os.environ['PINECONE_API_KEY'])
123
+
124
+ # index_name = "radarfinaldata08192024"
125
+ # vectorstore = PineconeVectorStore(index_name=index_name, embedding=embeddings)
126
+ # retriever = vectorstore.as_retriever(search_kwargs={'k': 5})
127
+
128
+ # chat_model = ChatOpenAI(api_key=os.environ['OPENAI_API_KEY'], temperature=0, model='gpt-4o')
129
+
130
+ # conversational_memory = ConversationBufferWindowMemory(
131
+ # memory_key='chat_history',
132
+ # k=10,
133
+ # return_messages=True
134
+ # )
135
+
136
+ # # Prompt templates
137
+ # def get_current_date():
138
+ # return datetime.now().strftime("%B %d, %Y")
139
+
140
+ # current_date = get_current_date()
141
+
142
+ # template1 = f"""As an expert concierge in Birmingham, Alabama, known for being a helpful and renowned guide, I am here to assist you on this sunny bright day of {current_date}. Given the current weather conditions and date, I have access to a plethora of information regarding events, places,sports and activities in Birmingham that can enhance your experience.
143
+ # If you have any questions or need recommendations, feel free to ask. I have a wealth of knowledge of perennial events in Birmingham and can provide detailed information to ensure you make the most of your time here. Remember, I am here to assist you in any way possible.
144
+ # Now, let me guide you through some of the exciting events happening today in Birmingham, Alabama:
145
+ # Address: >>, Birmingham, AL
146
+ # Time: >>__
147
+ # Date: >>__
148
+ # Description: >>__
149
+ # Address: >>, Birmingham, AL
150
+ # Time: >>__
151
+ # Date: >>__
152
+ # Description: >>__
153
+ # Address: >>, Birmingham, AL
154
+ # Time: >>__
155
+ # Date: >>__
156
+ # Description: >>__
157
+ # Address: >>, Birmingham, AL
158
+ # Time: >>__
159
+ # Date: >>__
160
+ # Description: >>__
161
+ # Address: >>, Birmingham, AL
162
+ # Time: >>__
163
+ # Date: >>__
164
+ # Description: >>__
165
+ # If you have any specific preferences or questions about these events or any other inquiries, please feel free to ask. Remember, I am here to ensure you have a memorable and enjoyable experience in Birmingham, AL.
166
+ # It was my pleasure!
167
+ # {{context}}
168
+ # Question: {{question}}
169
+ # Helpful Answer:"""
170
+
171
+ # # template2 = f"""As an expert concierge known for being helpful and a renowned guide for Birmingham, Alabama, I assist visitors in discovering the best that the city has to offer. Given today's sunny and bright weather on {current_date}, I am well-equipped to provide valuable insights and recommendations without revealing the locations. I draw upon my extensive knowledge of the area, including perennial events and historical context.
172
+ # # In light of this, how can I assist you today? Feel free to ask any questions or seek recommendations for your day in Birmingham. If there's anything specific you'd like to know or experience, please share, and I'll be glad to help. Remember, keep the question concise for a quick and accurate response.
173
+ # # "It was my pleasure!"
174
+ # # {{context}}
175
+ # # Question: {{question}}
176
+ # # Helpful Answer:"""
177
+
178
+
179
+
180
+ # template2 =f"""Hello there! As your friendly and knowledgeable guide here in Birmingham, Alabama . I'm here to help you discover the best experiences this beautiful city has to offer. It's a bright and sunny day today, {current_date}, and I’m excited to assist you with any insights or recommendations you need.
181
+ # Whether you're looking for local events, sports ,clubs,concerts etc or just a great place to grab a bite, I've got you covered.Keep your response casual, short and sweet for the quickest response.Don't reveal the location and give the response in a descriptive way, I'm here to help make your time in Birmingham unforgettable!
182
+ # "It’s always a pleasure to assist you!"
183
+ # {{context}}
184
+ # Question: {{question}}
185
+ # Helpful Answer:"""
186
+
187
+
188
+ # QA_CHAIN_PROMPT_1 = PromptTemplate(input_variables=["context", "question"], template=template1)
189
+ # QA_CHAIN_PROMPT_2 = PromptTemplate(input_variables=["context", "question"], template=template2)
190
+
191
+ # # Neo4j setup
192
+ # graph = Neo4jGraph(url="neo4j+s://6457770f.databases.neo4j.io",
193
+ # username="neo4j",
194
+ # password="Z10duoPkKCtENuOukw3eIlvl0xJWKtrVSr-_hGX1LQ4"
195
+ # )
196
+ # # Avoid pushing the graph documents to Neo4j every time
197
+ # # Only push the documents once and comment the code below after the initial push
198
+ # # dataset_name = "Pijush2023/birmindata07312024"
199
+ # # page_content_column = 'events_description'
200
+ # # loader = HuggingFaceDatasetLoader(dataset_name, page_content_column)
201
+ # # data = loader.load()
202
+
203
+ # # text_splitter = CharacterTextSplitter(chunk_size=100, chunk_overlap=50)
204
+ # # documents = text_splitter.split_documents(data)
205
+
206
+ # # llm_transformer = LLMGraphTransformer(llm=chat_model)
207
+ # # graph_documents = llm_transformer.convert_to_graph_documents(documents)
208
+ # # graph.add_graph_documents(graph_documents, baseEntityLabel=True, include_source=True)
209
+
210
+ # class Entities(BaseModel):
211
+ # names: list[str] = Field(..., description="All the person, organization, or business entities that appear in the text")
212
+
213
+ # entity_prompt = ChatPromptTemplate.from_messages([
214
+ # ("system", "You are extracting organization and person entities from the text."),
215
+ # ("human", "Use the given format to extract information from the following input: {question}"),
216
+ # ])
217
+
218
+ # entity_chain = entity_prompt | chat_model.with_structured_output(Entities)
219
+
220
+ # def remove_lucene_chars(input: str) -> str:
221
+ # return input.translate(str.maketrans({"\\": r"\\", "+": r"\+", "-": r"\-", "&": r"\&", "|": r"\|", "!": r"\!",
222
+ # "(": r"\(", ")": r"\)", "{": r"\{", "}": r"\}", "[": r"\[", "]": r"\]",
223
+ # "^": r"\^", "~": r"\~", "*": r"\*", "?": r"\?", ":": r"\:", '"': r'\"',
224
+ # ";": r"\;", " ": r"\ "}))
225
+
226
+ # def generate_full_text_query(input: str) -> str:
227
+ # full_text_query = ""
228
+ # words = [el for el in remove_lucene_chars(input).split() if el]
229
+ # for word in words[:-1]:
230
+ # full_text_query += f" {word}~2 AND"
231
+ # full_text_query += f" {words[-1]}~2"
232
+ # return full_text_query.strip()
233
+
234
+ # def structured_retriever(question: str) -> str:
235
+ # result = ""
236
+ # entities = entity_chain.invoke({"question": question})
237
+ # for entity in entities.names:
238
+ # response = graph.query(
239
+ # """CALL db.index.fulltext.queryNodes('entity', $query, {limit:2})
240
+ # YIELD node,score
241
+ # CALL {
242
+ # WITH node
243
+ # MATCH (node)-[r:!MENTIONS]->(neighbor)
244
+ # RETURN node.id + ' - ' + type(r) + ' -> ' + neighbor.id AS output
245
+ # UNION ALL
246
+ # WITH node
247
+ # MATCH (node)<-[r:!MENTIONS]-(neighbor)
248
+ # RETURN neighbor.id + ' - ' + type(r) + ' -> ' + node.id AS output
249
+ # }
250
+ # RETURN output LIMIT 50
251
+ # """,
252
+ # {"query": generate_full_text_query(entity)},
253
+ # )
254
+ # result += "\n".join([el['output'] for el in response])
255
+ # return result
256
+
257
+ # def retriever_neo4j(question: str):
258
+ # structured_data = structured_retriever(question)
259
+ # logging.debug(f"Structured data: {structured_data}")
260
+ # return structured_data
261
+
262
+ # _template = """Given the following conversation and a follow-up question, rephrase the follow-up question to be a standalone question,
263
+ # in its original language.
264
+ # Chat History:
265
+ # {chat_history}
266
+ # Follow Up Input: {question}
267
+ # Standalone question:"""
268
+
269
+ # CONDENSE_QUESTION_PROMPT = PromptTemplate.from_template(_template)
270
+
271
+ # def _format_chat_history(chat_history: list[tuple[str, str]]) -> list:
272
+ # buffer = []
273
+ # for human, ai in chat_history:
274
+ # buffer.append(HumanMessage(content=human))
275
+ # buffer.append(AIMessage(content=ai))
276
+ # return buffer
277
+
278
+ # _search_query = RunnableBranch(
279
+ # (
280
+ # RunnableLambda(lambda x: bool(x.get("chat_history"))).with_config(
281
+ # run_name="HasChatHistoryCheck"
282
+ # ),
283
+ # RunnablePassthrough.assign(
284
+ # chat_history=lambda x: _format_chat_history(x["chat_history"])
285
+ # )
286
+ # | CONDENSE_QUESTION_PROMPT
287
+ # | ChatOpenAI(temperature=0, api_key=os.environ['OPENAI_API_KEY'])
288
+ # | StrOutputParser(),
289
+ # ),
290
+ # RunnableLambda(lambda x : x["question"]),
291
+ # )
292
+
293
+ # # template = """Answer the question based only on the following context:
294
+ # # {context}
295
+ # # Question: {question}
296
+ # # Use natural language and be concise.
297
+ # # Answer:"""
298
+
299
+ # template = f"""As an expert concierge known for being helpful and a renowned guide for Birmingham, Alabama, I assist visitors in discovering the best that the city has to offer.I also assist the visitors about various sports and activities. Given today's sunny and bright weather on {current_date}, I am well-equipped to provide valuable insights and recommendations without revealing specific locations. I draw upon my extensive knowledge of the area, including perennial events and historical context.
300
+ # In light of this, how can I assist you today? Feel free to ask any questions or seek recommendations for your day in Birmingham. If there's anything specific you'd like to know or experience, please share, and I'll be glad to help. Remember, keep the question concise for a quick,short ,crisp and accurate response.
301
+ # "It was my pleasure!"
302
+ # {{context}}
303
+ # Question: {{question}}
304
+ # Helpful Answer:"""
305
+
306
+ # qa_prompt = ChatPromptTemplate.from_template(template)
307
+
308
+ # chain_neo4j = (
309
+ # RunnableParallel(
310
+ # {
311
+ # "context": _search_query | retriever_neo4j,
312
+ # "question": RunnablePassthrough(),
313
+ # }
314
+ # )
315
+ # | qa_prompt
316
+ # | chat_model
317
+ # | StrOutputParser()
318
+ # )
319
+
320
+
321
+
322
+
323
+
324
+
325
+
326
+ # phi_custom_template = """
327
+ # <|system|>
328
+ # You are a helpful assistant who provides clear, organized, crisp and conversational responses about an events,concerts,sports and all other activities of Birmingham,Alabama .<|end|>
329
+ # <|user|>
330
+ # {context}
331
+ # Question: {question}<|end|>
332
+ # <|assistant|>
333
+ # Sure! Here's the information you requested:
334
+ # """
335
+
336
+
337
+ # def generate_bot_response(history, choice, retrieval_mode, model_choice):
338
+ # if not history:
339
+ # return
340
+
341
+ # # Select the model
342
+ # selected_model = chat_model if model_choice == "GPT-4o" else phi_pipe
343
+
344
+ # response, addresses = generate_answer(history[-1][0], choice, retrieval_mode, selected_model)
345
+ # history[-1][1] = ""
346
+
347
+ # for character in response:
348
+ # history[-1][1] += character
349
+ # yield history # Stream each character as it is generated
350
+ # time.sleep(0.05) # Add a slight delay to simulate streaming
351
+
352
+ # yield history # Final yield with the complete response
353
+
354
+
355
+
356
+ # def generate_tts_response(response, tts_choice):
357
+ # with concurrent.futures.ThreadPoolExecutor() as executor:
358
+ # if tts_choice == "Alpha":
359
+ # audio_future = executor.submit(generate_audio_elevenlabs, response)
360
+ # elif tts_choice == "Beta":
361
+ # audio_future = executor.submit(generate_audio_parler_tts, response)
362
+ # # elif tts_choice == "Gamma":
363
+ # # audio_future = executor.submit(generate_audio_mars5, response)
364
+
365
+ # audio_path = audio_future.result()
366
+ # return audio_path
367
+
368
+
369
+
370
+
371
+
372
+ # import concurrent.futures
373
+ # # Existing bot function with concurrent futures for parallel processing
374
+ # def bot(history, choice, tts_choice, retrieval_mode, model_choice):
375
+ # # Initialize an empty response
376
+ # response = ""
377
+
378
+ # # Create a thread pool to handle both text generation and TTS conversion in parallel
379
+ # with concurrent.futures.ThreadPoolExecutor() as executor:
380
+ # # Start the bot response generation in parallel
381
+ # bot_future = executor.submit(generate_bot_response, history, choice, retrieval_mode, model_choice)
382
+
383
+ # # Wait for the text generation to start
384
+ # for history_chunk in bot_future.result():
385
+ # response = history_chunk[-1][1] # Update the response with the current state
386
+ # yield history_chunk, None # Stream the text output as it's generated
387
+
388
+ # # Once text is fully generated, start the TTS conversion
389
+ # tts_future = executor.submit(generate_tts_response, response, tts_choice)
390
+
391
+ # # Get the audio output after TTS is done
392
+ # audio_path = tts_future.result()
393
+
394
+ # # Stream the final text and audio output
395
+ # yield history, audio_path
396
+
397
+
398
+
399
+
400
+
401
+
402
+
403
+ # # Modified bot function to separate chatbot response and TTS generation
404
+
405
+ # def generate_bot_response(history, choice, retrieval_mode, model_choice):
406
+ # if not history:
407
+ # return
408
+
409
+ # # Select the model
410
+ # selected_model = chat_model if model_choice == "GPT-4o" else phi_pipe
411
+
412
+ # response, addresses = generate_answer(history[-1][0], choice, retrieval_mode, selected_model)
413
+ # history[-1][1] = ""
414
+
415
+ # for character in response:
416
+ # history[-1][1] += character
417
+ # yield history # Stream each character as it is generated
418
+ # time.sleep(0.05) # Add a slight delay to simulate streaming
419
+
420
+ # yield history # Final yield with the complete response
421
+
422
+
423
+
424
+
425
+ # def generate_audio_after_text(response, tts_choice):
426
+ # # Generate TTS audio after text response is completed
427
+ # with concurrent.futures.ThreadPoolExecutor() as executor:
428
+ # tts_future = executor.submit(generate_tts_response, response, tts_choice)
429
+ # audio_path = tts_future.result()
430
+ # return audio_path
431
+
432
+ # import re
433
+
434
+ # def clean_response(response_text):
435
+ # # Remove system and user tags
436
+ # response_text = re.sub(r'<\|system\|>.*?<\|end\|>', '', response_text, flags=re.DOTALL)
437
+ # response_text = re.sub(r'<\|user\|>.*?<\|end\|>', '', response_text, flags=re.DOTALL)
438
+ # response_text = re.sub(r'<\|assistant\|>', '', response_text, flags=re.DOTALL)
439
+
440
+ # # Clean up the text by removing extra whitespace
441
+ # cleaned_response = response_text.strip()
442
+ # cleaned_response = re.sub(r'\s+', ' ', cleaned_response)
443
+
444
+ # # Ensure the response is conversational and organized
445
+ # cleaned_response = cleaned_response.replace('1.', '\n1.').replace('2.', '\n2.').replace('3.', '\n3.').replace('4.', '\n4.').replace('5.', '\n5.')
446
+
447
+ # return cleaned_response
448
+
449
+
450
+
451
+
452
+ # import traceback
453
+
454
+ # def generate_answer(message, choice, retrieval_mode, selected_model):
455
+ # logging.debug(f"generate_answer called with choice: {choice}, retrieval_mode: {retrieval_mode}, and selected_model: {selected_model}")
456
+
457
+ # # Logic for disabling options for Phi-3.5
458
+ # if selected_model == "Phi-3.5":
459
+ # choice = None
460
+ # retrieval_mode = None
461
+
462
+ # try:
463
+ # # Select the appropriate template based on the choice
464
+ # if choice == "Details":
465
+ # prompt_template = QA_CHAIN_PROMPT_1
466
+ # elif choice == "Conversational":
467
+ # prompt_template = QA_CHAIN_PROMPT_2
468
+ # else:
469
+ # prompt_template = QA_CHAIN_PROMPT_1 # Fallback to template1
470
+
471
+ # # Handle hotel-related queries
472
+ # if "hotel" in message.lower() or "hotels" in message.lower() and "birmingham" in message.lower():
473
+ # logging.debug("Handling hotel-related query")
474
+ # response = fetch_google_hotels()
475
+ # logging.debug(f"Hotel response: {response}")
476
+ # return response, extract_addresses(response)
477
+
478
+ # # Handle restaurant-related queries
479
+ # if "restaurant" in message.lower() or "restaurants" in message.lower() and "birmingham" in message.lower():
480
+ # logging.debug("Handling restaurant-related query")
481
+ # response = fetch_yelp_restaurants()
482
+ # logging.debug(f"Restaurant response: {response}")
483
+ # return response, extract_addresses(response)
484
+
485
+ # # Handle flight-related queries
486
+ # if "flight" in message.lower() or "flights" in message.lower() and "birmingham" in message.lower():
487
+ # logging.debug("Handling flight-related query")
488
+ # response = fetch_google_flights()
489
+ # logging.debug(f"Flight response: {response}")
490
+ # return response, extract_addresses(response)
491
+
492
+ # # Retrieval-based response
493
+ # if retrieval_mode == "VDB":
494
+ # logging.debug("Using VDB retrieval mode")
495
+ # if selected_model == chat_model:
496
+ # logging.debug("Selected model: GPT-4o")
497
+ # retriever = gpt_retriever
498
+ # context = retriever.get_relevant_documents(message)
499
+ # logging.debug(f"Retrieved context: {context}")
500
+
501
+ # prompt = prompt_template.format(context=context, question=message)
502
+ # logging.debug(f"Generated prompt: {prompt}")
503
+
504
+ # qa_chain = RetrievalQA.from_chain_type(
505
+ # llm=chat_model,
506
+ # chain_type="stuff",
507
+ # retriever=retriever,
508
+ # chain_type_kwargs={"prompt": prompt_template}
509
+ # )
510
+ # response = qa_chain({"query": message})
511
+ # logging.debug(f"GPT-4o response: {response}")
512
+ # return response['result'], extract_addresses(response['result'])
513
+
514
+ # elif selected_model == phi_pipe:
515
+ # logging.debug("Selected model: Phi-3.5")
516
+ # retriever = phi_retriever
517
+ # context_documents = retriever.get_relevant_documents(message)
518
+ # context = "\n".join([doc.page_content for doc in context_documents])
519
+ # logging.debug(f"Retrieved context for Phi-3.5: {context}")
520
+
521
+ # # Use the correct template variable
522
+ # prompt = phi_custom_template.format(context=context, question=message)
523
+ # logging.debug(f"Generated Phi-3.5 prompt: {prompt}")
524
+
525
+ # response = selected_model(prompt, **{
526
+ # "max_new_tokens": 400,
527
+ # "return_full_text": True,
528
+ # "temperature": 0.7,
529
+ # "do_sample": True,
530
+ # })
531
+
532
+ # if response:
533
+ # generated_text = response[0]['generated_text']
534
+ # logging.debug(f"Phi-3.5 Response: {generated_text}")
535
+ # cleaned_response = clean_response(generated_text)
536
+ # return cleaned_response, extract_addresses(cleaned_response)
537
+ # else:
538
+ # logging.error("Phi-3.5 did not return any response.")
539
+ # return "No response generated.", []
540
+
541
+ # elif retrieval_mode == "KGF":
542
+ # logging.debug("Using KGF retrieval mode")
543
+ # response = chain_neo4j.invoke({"question": message})
544
+ # logging.debug(f"KGF response: {response}")
545
+ # return response, extract_addresses(response)
546
+ # else:
547
+ # logging.error("Invalid retrieval mode selected.")
548
+ # return "Invalid retrieval mode selected.", []
549
+
550
+ # except Exception as e:
551
+ # logging.error(f"Error in generate_answer: {str(e)}")
552
+ # logging.error(traceback.format_exc())
553
+ # return "Sorry, I encountered an error while processing your request.", []
554
+
555
+
556
+
557
+
558
+ # def add_message(history, message):
559
+ # history.append((message, None))
560
+ # return history, gr.Textbox(value="", interactive=True, show_label=False)
561
+
562
+ # def print_like_dislike(x: gr.LikeData):
563
+ # print(x.index, x.value, x.liked)
564
+
565
+ # def extract_addresses(response):
566
+ # if not isinstance(response, str):
567
+ # response = str(response)
568
+ # address_patterns = [
569
+ # r'([A-Z].*,\sBirmingham,\sAL\s\d{5})',
570
+ # r'(\d{4}\s.*,\sBirmingham,\sAL\s\d{5})',
571
+ # r'([A-Z].*,\sAL\s\d{5})',
572
+ # r'([A-Z].*,.*\sSt,\sBirmingham,\sAL\s\d{5})',
573
+ # r'([A-Z].*,.*\sStreets,\sBirmingham,\sAL\s\d{5})',
574
+ # r'(\d{2}.*\sStreets)',
575
+ # r'([A-Z].*\s\d{2},\sBirmingham,\sAL\s\d{5})',
576
+ # r'([a-zA-Z]\s Birmingham)',
577
+ # r'([a-zA-Z].*,\sBirmingham,\sAL)',
578
+ # r'(.*),(Birmingham, AL,USA)$'
579
+ # r'(^Birmingham,AL$)',
580
+ # r'((.*)(Stadium|Field),.*,\sAL$)',
581
+ # r'((.*)(Stadium|Field),.*,\sFL$)',
582
+ # r'((.*)(Stadium|Field),.*,\sMS$)',
583
+ # r'((.*)(Stadium|Field),.*,\sAR$)',
584
+ # r'((.*)(Stadium|Field),.*,\sKY$)',
585
+ # r'((.*)(Stadium|Field),.*,\sTN$)',
586
+ # r'((.*)(Stadium|Field),.*,\sLA$)',
587
+ # r'((.*)(Stadium|Field),.*,\sFL$)'
588
+
589
+ # ]
590
+ # addresses = []
591
+ # for pattern in address_patterns:
592
+ # addresses.extend(re.findall(pattern, response))
593
+ # return addresses
594
+
595
+ # all_addresses = []
596
+
597
+ # def generate_map(location_names):
598
+ # global all_addresses
599
+ # all_addresses.extend(location_names)
600
+
601
+ # api_key = os.environ['GOOGLEMAPS_API_KEY']
602
+ # gmaps = GoogleMapsClient(key=api_key)
603
+
604
+ # m = folium.Map(location=[33.5175, -86.809444], zoom_start=12)
605
+
606
+ # for location_name in all_addresses:
607
+ # geocode_result = gmaps.geocode(location_name)
608
+ # if geocode_result:
609
+ # location = geocode_result[0]['geometry']['location']
610
+ # folium.Marker(
611
+ # [location['lat'], location['lng']],
612
+ # tooltip=f"{geocode_result[0]['formatted_address']}"
613
+ # ).add_to(m)
614
+
615
+ # map_html = m._repr_html_()
616
+ # return map_html
617
+
618
+
619
+ # def fetch_local_news():
620
+ # api_key = os.environ['SERP_API']
621
+ # url = f'https://serpapi.com/search.json?engine=google_news&q=birmingham headline&api_key={api_key}'
622
+ # response = requests.get(url)
623
+ # if response.status_code == 200:
624
+ # results = response.json().get("news_results", [])
625
+ # news_html = """
626
+ # <h2 style="font-family: 'Georgia', serif; color: #ff0000; background-color: #f8f8f8; padding: 10px; border-radius: 10px;">Birmingham Today</h2>
627
+ # <style>
628
+ # .news-item {
629
+ # font-family: 'Verdana', sans-serif;
630
+ # color: #333;
631
+ # background-color: #f0f8ff;
632
+ # margin-bottom: 15px;
633
+ # padding: 10px;
634
+ # border-radius: 5px;
635
+ # transition: box-shadow 0.3s ease, background-color 0.3s ease;
636
+ # font-weight: bold;
637
+ # }
638
+ # .news-item:hover {
639
+ # box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
640
+ # background-color: #e6f7ff;
641
+ # }
642
+ # .news-item a {
643
+ # color: #1E90FF;
644
+ # text-decoration: none;
645
+ # font-weight: bold;
646
+ # }
647
+ # .news-item a:hover {
648
+ # text-decoration: underline;
649
+ # }
650
+ # .news-preview {
651
+ # position: absolute;
652
+ # display: none;
653
+ # border: 1px solid #ccc;
654
+ # border-radius: 5px;
655
+ # box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
656
+ # background-color: white;
657
+ # z-index: 1000;
658
+ # max-width: 300px;
659
+ # padding: 10px;
660
+ # font-family: 'Verdana', sans-serif;
661
+ # color: #333;
662
+ # }
663
+ # </style>
664
+ # <script>
665
+ # function showPreview(event, previewContent) {
666
+ # var previewBox = document.getElementById('news-preview');
667
+ # previewBox.innerHTML = previewContent;
668
+ # previewBox.style.left = event.pageX + 'px';
669
+ # previewBox.style.top = event.pageY + 'px';
670
+ # previewBox.style.display = 'block';
671
+ # }
672
+ # function hidePreview() {
673
+ # var previewBox = document.getElementById('news-preview');
674
+ # previewBox.style.display = 'none';
675
+ # }
676
+ # </script>
677
+ # <div id="news-preview" class="news-preview"></div>
678
+ # """
679
+ # for index, result in enumerate(results[:7]):
680
+ # title = result.get("title", "No title")
681
+ # link = result.get("link", "#")
682
+ # snippet = result.get("snippet", "")
683
+ # news_html += f"""
684
+ # <div class="news-item" onmouseover="showPreview(event, '{snippet}')" onmouseout="hidePreview()">
685
+ # <a href='{link}' target='_blank'>{index + 1}. {title}</a>
686
+ # <p>{snippet}</p>
687
+ # </div>
688
+ # """
689
+ # return news_html
690
+ # else:
691
+ # return "<p>Failed to fetch local news</p>"
692
+
693
+ # import numpy as np
694
+ # import torch
695
+ # from transformers import pipeline, AutoModelForSpeechSeq2Seq, AutoProcessor
696
+
697
+ # model_id = 'openai/whisper-large-v3'
698
+ # device = "cuda:0" if torch.cuda.is_available() else "cpu"
699
+ # torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
700
+ # model = AutoModelForSpeechSeq2Seq.from_pretrained(model_id, torch_dtype=torch_dtype).to(device)
701
+ # processor = AutoProcessor.from_pretrained(model_id)
702
+
703
+ # pipe_asr = pipeline("automatic-speech-recognition", model=model, tokenizer=processor.tokenizer, feature_extractor=processor.feature_extractor, max_new_tokens=128, chunk_length_s=15, batch_size=16, torch_dtype=torch_dtype, device=device, return_timestamps=True)
704
+
705
+ # base_audio_drive = "/data/audio"
706
+
707
+ # #Normal Code with sample rate is 44100 Hz
708
+
709
+ # def transcribe_function(stream, new_chunk):
710
+ # try:
711
+ # sr, y = new_chunk[0], new_chunk[1]
712
+ # except TypeError:
713
+ # print(f"Error chunk structure: {type(new_chunk)}, content: {new_chunk}")
714
+ # return stream, "", None
715
+
716
+ # y = y.astype(np.float32) / np.max(np.abs(y))
717
+
718
+ # if stream is not None:
719
+ # stream = np.concatenate([stream, y])
720
+ # else:
721
+ # stream = y
722
+
723
+ # result = pipe_asr({"array": stream, "sampling_rate": sr}, return_timestamps=False)
724
+
725
+ # full_text = result.get("text","")
726
+
727
+ # return stream, full_text, result
728
+
729
+
730
+
731
+
732
+
733
+
734
+
735
+ # def update_map_with_response(history):
736
+ # if not history:
737
+ # return ""
738
+ # response = history[-1][1]
739
+ # addresses = extract_addresses(response)
740
+ # return generate_map(addresses)
741
+
742
+ # def clear_textbox():
743
+ # return ""
744
+
745
+ # def show_map_if_details(history, choice):
746
+ # if choice in ["Details", "Conversational"]:
747
+ # return gr.update(visible=True), update_map_with_response(history)
748
+ # else:
749
+ # return gr.update(visible(False), "")
750
+
751
+
752
+
753
+
754
+
755
+
756
+
757
+
758
+ # def generate_audio_elevenlabs(text):
759
+ # XI_API_KEY = os.environ['ELEVENLABS_API']
760
+ # VOICE_ID = 'd9MIrwLnvDeH7aZb61E9'
761
+ # tts_url = f"https://api.elevenlabs.io/v1/text-to-speech/{VOICE_ID}/stream"
762
+ # headers = {
763
+ # "Accept": "application/json",
764
+ # "xi-api-key": XI_API_KEY
765
+ # }
766
+ # data = {
767
+ # "text": str(text),
768
+ # "model_id": "eleven_multilingual_v2",
769
+ # "voice_settings": {
770
+ # "stability": 1.0,
771
+ # "similarity_boost": 0.0,
772
+ # "style": 0.60,
773
+ # "use_speaker_boost": False
774
+ # }
775
+ # }
776
+ # response = requests.post(tts_url, headers=headers, json=data, stream=True)
777
+ # if response.ok:
778
+ # audio_segments = []
779
+ # with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as f:
780
+ # for chunk in response.iter_content(chunk_size=1024):
781
+ # if chunk:
782
+ # f.write(chunk)
783
+ # audio_segments.append(chunk)
784
+ # temp_audio_path = f.name
785
+
786
+ # # Combine all audio chunks into a single file
787
+ # combined_audio = AudioSegment.from_file(temp_audio_path, format="mp3")
788
+ # combined_audio_path = os.path.join(tempfile.gettempdir(), "elevenlabs_combined_audio.mp3")
789
+ # combined_audio.export(combined_audio_path, format="mp3")
790
+
791
+ # logging.debug(f"Audio saved to {combined_audio_path}")
792
+ # return combined_audio_path
793
+ # else:
794
+ # logging.error(f"Error generating audio: {response.text}")
795
+ # return None
796
+
797
+
798
+
799
+
800
+ # # chunking audio and then Process
801
+
802
+ # import concurrent.futures
803
+ # import tempfile
804
+ # import os
805
+ # import numpy as np
806
+ # import logging
807
+ # from queue import Queue
808
+ # from threading import Thread
809
+ # from scipy.io.wavfile import write as write_wav
810
+ # from parler_tts import ParlerTTSForConditionalGeneration, ParlerTTSStreamer
811
+ # from transformers import AutoTokenizer
812
+
813
+ # # Ensure your device is set to CUDA
814
+ # device = "cuda:0" if torch.cuda.is_available() else "cpu"
815
+
816
+ # repo_id = "parler-tts/parler-tts-mini-v1"
817
+
818
+ # def generate_audio_parler_tts(text):
819
+ # description = "A female speaker delivers a slightly expressive and animated speech with a moderate speed and pitch. The recording is of very high quality, with the speaker's voice sounding clear and very close up."
820
+ # chunk_size_in_s = 0.5
821
+
822
+ # # Initialize the tokenizer and model
823
+ # parler_tokenizer = AutoTokenizer.from_pretrained(repo_id)
824
+ # parler_model = ParlerTTSForConditionalGeneration.from_pretrained(repo_id).to(device)
825
+ # sampling_rate = parler_model.audio_encoder.config.sampling_rate
826
+ # frame_rate = parler_model.audio_encoder.config.frame_rate
827
+
828
+ # def generate(text, description, play_steps_in_s=0.5):
829
+ # play_steps = int(frame_rate * play_steps_in_s)
830
+ # streamer = ParlerTTSStreamer(parler_model, device=device, play_steps=play_steps)
831
+
832
+ # inputs = parler_tokenizer(description, return_tensors="pt").to(device)
833
+ # prompt = parler_tokenizer(text, return_tensors="pt").to(device)
834
+
835
+ # generation_kwargs = dict(
836
+ # input_ids=inputs.input_ids,
837
+ # prompt_input_ids=prompt.input_ids,
838
+ # attention_mask=inputs.attention_mask,
839
+ # prompt_attention_mask=prompt.attention_mask,
840
+ # streamer=streamer,
841
+ # do_sample=True,
842
+ # temperature=1.0,
843
+ # min_new_tokens=10,
844
+ # )
845
+
846
+ # thread = Thread(target=parler_model.generate, kwargs=generation_kwargs)
847
+ # thread.start()
848
+
849
+ # for new_audio in streamer:
850
+ # if new_audio.shape[0] == 0:
851
+ # break
852
+ # # Save or process each audio chunk as it is generated
853
+ # yield sampling_rate, new_audio
854
+
855
+ # audio_segments = []
856
+ # for (sampling_rate, audio_chunk) in generate(text, description, chunk_size_in_s):
857
+ # audio_segments.append(audio_chunk)
858
+
859
+ # temp_audio_path = os.path.join(tempfile.gettempdir(), f"parler_tts_audio_chunk_{len(audio_segments)}.wav")
860
+ # write_wav(temp_audio_path, sampling_rate, audio_chunk.astype(np.float32))
861
+ # logging.debug(f"Saved chunk to {temp_audio_path}")
862
+
863
+
864
+ # # Combine all the audio chunks into one audio file
865
+ # combined_audio = np.concatenate(audio_segments)
866
+ # combined_audio_path = os.path.join(tempfile.gettempdir(), "parler_tts_combined_audio_stream.wav")
867
+
868
+ # write_wav(combined_audio_path, sampling_rate, combined_audio.astype(np.float32))
869
+
870
+ # logging.debug(f"Combined audio saved to {combined_audio_path}")
871
+ # return combined_audio_path
872
+
873
+
874
+ # def fetch_local_events():
875
+ # api_key = os.environ['SERP_API']
876
+ # url = f'https://serpapi.com/search.json?engine=google_events&q=Events+in+Birmingham&hl=en&gl=us&api_key={api_key}'
877
+ # response = requests.get(url)
878
+ # if response.status_code == 200:
879
+ # events_results = response.json().get("events_results", [])
880
+ # events_html = """
881
+ # <h2 style="font-family: 'Georgia', serif; color: #ff0000; background-color: #f8f8f8; padding: 10px; border-radius: 10px;">Local Events</h2>
882
+ # <style>
883
+ # table {
884
+ # font-family: 'Verdana', sans-serif;
885
+ # color: #333;
886
+ # border-collapse: collapse;
887
+ # width: 100%;
888
+ # }
889
+ # th, td {
890
+ # border: 1px solid #fff !important;
891
+ # padding: 8px;
892
+ # }
893
+ # th {
894
+ # background-color: #f2f2f2;
895
+ # color: #333;
896
+ # text-align: left;
897
+ # }
898
+ # tr:hover {
899
+ # background-color: #f5f5f5;
900
+ # }
901
+ # .event-link {
902
+ # color: #1E90FF;
903
+ # text-decoration: none;
904
+ # }
905
+ # .event-link:hover {
906
+ # text-decoration: underline;
907
+ # }
908
+ # </style>
909
+ # <table>
910
+ # <tr>
911
+ # <th>Title</th>
912
+ # <th>Date and Time</th>
913
+ # <th>Location</th>
914
+ # </tr>
915
+ # """
916
+ # for event in events_results:
917
+ # title = event.get("title", "No title")
918
+ # date_info = event.get("date", {})
919
+ # date = f"{date_info.get('start_date', '')} {date_info.get('when', '')}".replace("{", "").replace("}", "")
920
+ # location = event.get("address", "No location")
921
+ # if isinstance(location, list):
922
+ # location = " ".join(location)
923
+ # location = location.replace("[", "").replace("]", "")
924
+ # link = event.get("link", "#")
925
+ # events_html += f"""
926
+ # <tr>
927
+ # <td><a class='event-link' href='{link}' target='_blank'>{title}</a></td>
928
+ # <td>{date}</td>
929
+ # <td>{location}</td>
930
+ # </tr>
931
+ # """
932
+ # events_html += "</table>"
933
+ # return events_html
934
+ # else:
935
+ # return "<p>Failed to fetch local events</p>"
936
+
937
+ # def get_weather_icon(condition):
938
+ # condition_map = {
939
+ # "Clear": "c01d",
940
+ # "Partly Cloudy": "c02d",
941
+ # "Cloudy": "c03d",
942
+ # "Overcast": "c04d",
943
+ # "Mist": "a01d",
944
+ # "Patchy rain possible": "r01d",
945
+ # "Light rain": "r02d",
946
+ # "Moderate rain": "r03d",
947
+ # "Heavy rain": "r04d",
948
+ # "Snow": "s01d",
949
+ # "Thunderstorm": "t01d",
950
+ # "Fog": "a05d",
951
+ # }
952
+ # return condition_map.get(condition, "c04d")
953
+
954
+ # def fetch_local_weather():
955
+ # try:
956
+ # api_key = os.environ['WEATHER_API']
957
+ # url = f'https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/birmingham?unitGroup=metric&include=events%2Calerts%2Chours%2Cdays%2Ccurrent&key={api_key}'
958
+ # response = requests.get(url)
959
+ # response.raise_for_status()
960
+ # jsonData = response.json()
961
+
962
+ # current_conditions = jsonData.get("currentConditions", {})
963
+ # temp_celsius = current_conditions.get("temp", "N/A")
964
+
965
+ # if temp_celsius != "N/A":
966
+ # temp_fahrenheit = int((temp_celsius * 9/5) + 32)
967
+ # else:
968
+ # temp_fahrenheit = "N/A"
969
+
970
+ # condition = current_conditions.get("conditions", "N/A")
971
+ # humidity = current_conditions.get("humidity", "N/A")
972
+
973
+ # weather_html = f"""
974
+ # <div class="weather-theme">
975
+ # <h2 style="font-family: 'Georgia', serif; color: #ff0000; background-color: #f8f8f8; padding: 10px; border-radius: 10px;">Local Weather</h2>
976
+ # <div class="weather-content">
977
+ # <div class="weather-icon">
978
+ # <img src="https://www.weatherbit.io/static/img/icons/{get_weather_icon(condition)}.png" alt="{condition}" style="width: 100px; height: 100px;">
979
+ # </div>
980
+ # <div class="weather-details">
981
+ # <p style="font-family: 'Verdana', sans-serif; color: #333; font-size: 1.2em;">Temperature: {temp_fahrenheit}°F</p>
982
+ # <p style="font-family: 'Verdana', sans-serif; color: #333; font-size: 1.2em;">Condition: {condition}</p>
983
+ # <p style="font-family: 'Verdana', sans-serif; color: #333; font-size: 1.2em;">Humidity: {humidity}%</p>
984
+ # </div>
985
+ # </div>
986
+ # </div>
987
+ # <style>
988
+ # .weather-theme {{
989
+ # animation: backgroundAnimation 10s infinite alternate;
990
+ # border-radius: 10px;
991
+ # padding: 10px;
992
+ # margin-bottom: 15px;
993
+ # background: linear-gradient(45deg, #ffcc33, #ff6666, #ffcc33, #ff6666);
994
+ # background-size: 400% 400%;
995
+ # box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
996
+ # transition: box-shadow 0.3s ease, background-color 0.3s ease;
997
+ # }}
998
+ # .weather-theme:hover {{
999
+ # box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
1000
+ # background-position: 100% 100%;
1001
+ # }}
1002
+ # @keyframes backgroundAnimation {{
1003
+ # 0% {{ background-position: 0% 50%; }}
1004
+ # 100% {{ background-position: 100% 50%; }}
1005
+ # }}
1006
+ # .weather-content {{
1007
+ # display: flex;
1008
+ # align-items: center;
1009
+ # }}
1010
+ # .weather-icon {{
1011
+ # flex: 1;
1012
+ # }}
1013
+ # .weather-details {{
1014
+ # flex 3;
1015
+ # }}
1016
+ # </style>
1017
+ # """
1018
+ # return weather_html
1019
+ # except requests.exceptions.RequestException as e:
1020
+ # return f"<p>Failed to fetch local weather: {e}</p>"
1021
+
1022
+
1023
+ # def handle_retrieval_mode_change(choice):
1024
+ # if choice == "KGF":
1025
+ # return gr.update(interactive=False), gr.update(interactive=False)
1026
+ # else:
1027
+ # return gr.update(interactive=True), gr.update(interactive=True)
1028
+
1029
+ # def handle_model_choice_change(selected_model):
1030
+ # if selected_model == "Phi-3.5":
1031
+ # # Disable retrieval mode and select style when Phi-3.5 is selected
1032
+ # return gr.update(interactive=False), gr.update(interactive=False), gr.update(interactive=False)
1033
+ # elif selected_model == "GPT-4o":
1034
+ # # Enable retrieval mode and select style for GPT-4o
1035
+ # return gr.update(interactive=True), gr.update(interactive=True), gr.update(interactive=True)
1036
+ # else:
1037
+ # # Default case: allow interaction
1038
+ # return gr.update(interactive=True), gr.update(interactive=True), gr.update(interactive=True)
1039
+
1040
+
1041
+
1042
+
1043
+ # def format_restaurant_hotel_info(name, link, location, phone, rating, reviews, snippet):
1044
+ # return f"""
1045
+ # {name}
1046
+ # - Link: {link}
1047
+ # - Location: {location}
1048
+ # - Contact No: {phone}
1049
+ # - Rating: {rating} stars ({reviews} reviews)
1050
+ # - Snippet: {snippet}
1051
+ # """
1052
+
1053
+ # def fetch_yelp_restaurants():
1054
+ # # Introductory prompt for restaurants
1055
+ # intro_prompt = "Here are some of the top-rated restaurants in Birmingham, Alabama. I hope these suggestions help you find the perfect place to enjoy your meal:"
1056
+
1057
+ # params = {
1058
+ # "engine": "yelp",
1059
+ # "find_desc": "Restaurant",
1060
+ # "find_loc": "Birmingham, AL, USA",
1061
+ # "api_key": os.getenv("SERP_API")
1062
+ # }
1063
+
1064
+ # search = GoogleSearch(params)
1065
+ # results = search.get_dict()
1066
+ # organic_results = results.get("organic_results", [])
1067
+
1068
+ # response_text = f"{intro_prompt}\n"
1069
+
1070
+ # for result in organic_results[:5]: # Limiting to top 5 restaurants
1071
+ # name = result.get("title", "No name")
1072
+ # rating = result.get("rating", "No rating")
1073
+ # reviews = result.get("reviews", "No reviews")
1074
+ # phone = result.get("phone", "Not Available")
1075
+ # snippet = result.get("snippet", "Not Available")
1076
+ # location = f"{name}, Birmingham, AL,USA"
1077
+ # link = result.get("link", "#")
1078
+
1079
+ # response_text += format_restaurant_hotel_info(name, link, location, phone, rating, reviews, snippet)
1080
+
1081
+
1082
+ # return response_text
1083
+
1084
+
1085
+
1086
+
1087
+
1088
+
1089
+ # def format_hotel_info(name, link, location, rate_per_night, total_rate, description, check_in_time, check_out_time, amenities):
1090
+ # return f"""
1091
+ # {name}
1092
+ # - Link: {link}
1093
+ # - Location: {location}
1094
+ # - Rate per Night: {rate_per_night} (Before taxes/fees: {total_rate})
1095
+ # - Check-in Time: {check_in_time}
1096
+ # - Check-out Time: {check_out_time}
1097
+ # - Amenities: {amenities}
1098
+ # - Description: {description}
1099
+ # """
1100
+
1101
+ # def fetch_google_hotels(query="Birmingham Hotel", check_in=current_date1, check_out="2024-09-02", adults=2):
1102
+ # # Introductory prompt for hotels
1103
+ # intro_prompt = "Here are some of the best hotels in Birmingham, Alabama, for your stay. Each of these options offers a unique experience, whether you're looking for luxury, comfort, or convenience:"
1104
+
1105
+ # params = {
1106
+ # "engine": "google_hotels",
1107
+ # "q": query,
1108
+ # "check_in_date": check_in,
1109
+ # "check_out_date": check_out,
1110
+ # "adults": str(adults),
1111
+ # "currency": "USD",
1112
+ # "gl": "us",
1113
+ # "hl": "en",
1114
+ # "api_key": os.getenv("SERP_API")
1115
+ # }
1116
+
1117
+ # search = GoogleSearch(params)
1118
+ # results = search.get_dict()
1119
+ # hotel_results = results.get("properties", [])
1120
+
1121
+ # hotel_info = f"{intro_prompt}\n"
1122
+ # for hotel in hotel_results[:5]: # Limiting to top 5 hotels
1123
+ # name = hotel.get('name', 'No name')
1124
+ # description = hotel.get('description', 'No description')
1125
+ # link = hotel.get('link', '#')
1126
+ # check_in_time = hotel.get('check_in_time', 'N/A')
1127
+ # check_out_time = hotel.get('check_out_time', 'N/A')
1128
+ # rate_per_night = hotel.get('rate_per_night', {}).get('lowest', 'N/A')
1129
+ # before_taxes_fees = hotel.get('rate_per_night', {}).get('before_taxes_fees', 'N/A')
1130
+ # total_rate = hotel.get('total_rate', {}).get('lowest', 'N/A')
1131
+ # amenities = ", ".join(hotel.get('amenities', [])) if hotel.get('amenities') else "Not Available"
1132
+
1133
+ # location = f"{name}, Birmingham, AL,USA"
1134
+
1135
+ # hotel_info += format_hotel_info(
1136
+ # name,
1137
+ # link,
1138
+ # location,
1139
+ # rate_per_night,
1140
+ # total_rate,
1141
+ # description,
1142
+ # check_in_time,
1143
+ # check_out_time,
1144
+ # amenities
1145
+ # )
1146
+
1147
+
1148
+ # return hotel_info
1149
+
1150
+
1151
+
1152
+
1153
+ # def format_flight_info(flight_number, departure_airport, departure_time, arrival_airport, arrival_time, duration, airplane):
1154
+ # return f"""
1155
+ # Flight {flight_number}
1156
+ # - Departure: {departure_airport} at {departure_time}
1157
+ # - Arrival: {arrival_airport} at {arrival_time}
1158
+ # - Duration: {duration} minutes
1159
+ # - Airplane: {airplane}
1160
+ # """
1161
+
1162
+ # def fetch_google_flights(departure_id="JFK", arrival_id="BHM", outbound_date=current_date1, return_date="2024-08-20"):
1163
+ # # Introductory prompt for flights
1164
+ # intro_prompt = "Here are some available flights from JFK to Birmingham, Alabama. These options provide a range of times and durations to fit your travel needs:"
1165
+
1166
+ # params = {
1167
+ # "engine": "google_flights",
1168
+ # "departure_id": departure_id,
1169
+ # "arrival_id": arrival_id,
1170
+ # "outbound_date": outbound_date,
1171
+ # "return_date": return_date,
1172
+ # "currency": "USD",
1173
+ # "hl": "en",
1174
+ # "api_key": os.getenv("SERP_API")
1175
+ # }
1176
+
1177
+ # search = GoogleSearch(params)
1178
+ # results = search.get_dict()
1179
+
1180
+ # # Extract flight details from the results
1181
+ # best_flights = results.get('best_flights', [])
1182
+ # flight_info = f"{intro_prompt}\n"
1183
+
1184
+ # # Process each flight in the best_flights list
1185
+ # for i, flight in enumerate(best_flights, start=1):
1186
+ # for segment in flight.get('flights', []):
1187
+ # departure_airport = segment.get('departure_airport', {}).get('name', 'Unknown Departure Airport')
1188
+ # departure_time = segment.get('departure_airport', {}).get('time', 'Unknown Time')
1189
+ # arrival_airport = segment.get('arrival_airport', {}).get('name', 'Unknown Arrival Airport')
1190
+ # arrival_time = segment.get('arrival_airport', {}).get('time', 'Unknown Time')
1191
+ # duration = segment.get('duration', 'Unknown Duration')
1192
+ # airplane = segment.get('airplane', 'Unknown Airplane')
1193
+
1194
+ # # Format the flight segment details
1195
+ # flight_info += format_flight_info(
1196
+ # flight_number=i,
1197
+ # departure_airport=departure_airport,
1198
+ # departure_time=departure_time,
1199
+ # arrival_airport=arrival_airport,
1200
+ # arrival_time=arrival_time,
1201
+ # duration=duration,
1202
+ # airplane=airplane
1203
+ # )
1204
+
1205
+
1206
+ # return flight_info
1207
+
1208
+
1209
+
1210
+
1211
+ # with gr.Blocks(theme='Pijush2023/scikit-learn-pijush') as demo:
1212
+ # with gr.Row():
1213
+ # with gr.Column():
1214
+ # state = gr.State()
1215
+
1216
+ # chatbot = gr.Chatbot([], elem_id="RADAR:Channel 94.1", bubble_full_width=False)
1217
+ # choice = gr.Radio(label="Select Style", choices=["Details", "Conversational"], value="Conversational")
1218
+ # retrieval_mode = gr.Radio(label="Retrieval Mode", choices=["VDB", "KGF"], value="VDB")
1219
+ # model_choice = gr.Dropdown(label="Choose Model", choices=["GPT-4o", "Phi-3.5"], value="GPT-4o")
1220
+
1221
+ # # Link the dropdown change to handle_model_choice_change
1222
+ # model_choice.change(fn=handle_model_choice_change, inputs=model_choice, outputs=[retrieval_mode, choice, choice])
1223
+
1224
+ # gr.Markdown("<h1 style='color: red;'>Talk to RADAR</h1>", elem_id="voice-markdown")
1225
+
1226
+ # chat_input = gr.Textbox(show_copy_button=True, interactive=True, show_label=False, label="ASK Radar !!!", placeholder="Hey Radar...!!")
1227
+ # tts_choice = gr.Radio(label="Select TTS System", choices=["Alpha", "Beta"], value="Alpha")
1228
+ # retriever_button = gr.Button("Retriever")
1229
+
1230
+ # clear_button = gr.Button("Clear")
1231
+ # clear_button.click(lambda: [None, None], outputs=[chat_input, state])
1232
+
1233
+ # gr.Markdown("<h1 style='color: red;'>Radar Map</h1>", elem_id="Map-Radar")
1234
+ # location_output = gr.HTML()
1235
+ # audio_output = gr.Audio(interactive=False, autoplay=True)
1236
+
1237
+ # def stop_audio():
1238
+ # audio_output.stop()
1239
+ # return None
1240
+
1241
+
1242
+ # # retriever_sequence = (
1243
+ # # retriever_button.click(fn=stop_audio, inputs=[], outputs=[audio_output], api_name="api_stop_audio_recording")
1244
+ # # .then(fn=add_message, inputs=[chatbot, chat_input], outputs=[chatbot, chat_input], api_name="api_addprompt_chathistory")
1245
+ # # .then(fn=bot, inputs=[chatbot, choice, tts_choice, retrieval_mode, model_choice], outputs=[chatbot, audio_output], api_name="api_askchatbot_then_generateaudio")
1246
+ # # .then(fn=show_map_if_details, inputs=[chatbot, choice], outputs=[location_output, location_output], api_name="api_show_map_details")
1247
+ # # .then(fn=clear_textbox, inputs=[], outputs=[chat_input],api_name="api_clear_textbox")
1248
+ # # )
1249
+
1250
+ # retriever_sequence = (
1251
+ # retriever_button.click(fn=stop_audio, inputs=[], outputs=[audio_output], api_name="api_stop_audio_recording")
1252
+ # .then(fn=add_message, inputs=[chatbot, chat_input], outputs=[chatbot, chat_input], api_name="api_addprompt_chathistory")
1253
+ # # First, generate the bot response
1254
+ # .then(fn=generate_bot_response, inputs=[chatbot, choice, retrieval_mode, model_choice], outputs=[chatbot], api_name="api_generate_bot_response")
1255
+ # # Then, generate the TTS response based on the bot's response
1256
+ # .then(fn=generate_tts_response, inputs=[chatbot, tts_choice], outputs=[audio_output], api_name="api_generate_tts_response")
1257
+ # .then(fn=show_map_if_details, inputs=[chatbot, choice], outputs=[location_output, location_output], api_name="api_show_map_details")
1258
+ # .then(fn=clear_textbox, inputs=[], outputs=[chat_input], api_name="api_clear_textbox")
1259
+ # )
1260
+
1261
+
1262
+
1263
+
1264
+
1265
+
1266
+ # # chat_input.submit(fn=stop_audio, inputs=[], outputs=[audio_output],api_name="api_stop_audio_recording")
1267
+ # # chat_input.submit(fn=add_message, inputs=[chatbot, chat_input], outputs=[chatbot, chat_input], api_name="api_addprompt_chathistory").then(
1268
+ # # fn=bot, inputs=[chatbot, choice, tts_choice, retrieval_mode, model_choice], outputs=[chatbot, audio_output], api_name="api_askchatbot_then_generateaudio"
1269
+ # # ).then(
1270
+ # # fn=show_map_if_details, inputs=[chatbot, choice], outputs=[location_output, location_output], api_name="api_show_map_details"
1271
+ # # ).then(
1272
+ # # fn=clear_textbox, inputs=[], outputs=[chat_input],api_name="api_clear_textbox"
1273
+ # # )
1274
+
1275
+ # chat_input.submit(fn=stop_audio, inputs=[], outputs=[audio_output], api_name="api_stop_audio_recording").then(
1276
+ # fn=add_message, inputs=[chatbot, chat_input], outputs=[chatbot, chat_input], api_name="api_addprompt_chathistory"
1277
+ # ).then(
1278
+ # # First, generate the bot response
1279
+ # fn=generate_bot_response, inputs=[chatbot, choice, retrieval_mode, model_choice], outputs=[chatbot], api_name="api_generate_bot_response"
1280
+ # ).then(
1281
+ # # Then, generate the TTS response based on the bot's response
1282
+ # fn=generate_tts_response, inputs=[chatbot, tts_choice], outputs=[audio_output], api_name="api_generate_tts_response"
1283
+ # ).then(
1284
+ # fn=show_map_if_details, inputs=[chatbot, choice], outputs=[location_output, location_output], api_name="api_show_map_details"
1285
+ # ).then(
1286
+ # fn=clear_textbox, inputs=[], outputs=[chat_input], api_name="api_clear_textbox"
1287
+ # )
1288
+
1289
+
1290
+
1291
+
1292
+
1293
+
1294
+
1295
+ # audio_input = gr.Audio(sources=["microphone"], streaming=True, type='numpy', every=0.1)
1296
+ # audio_input.stream(transcribe_function, inputs=[state, audio_input], outputs=[state, chat_input], api_name="api_voice_to_text")
1297
+
1298
+ # with gr.Column():
1299
+ # weather_output = gr.HTML(value=fetch_local_weather())
1300
+ # news_output = gr.HTML(value=fetch_local_news())
1301
+ # events_output = gr.HTML(value=fetch_local_events())
1302
+
1303
+ # demo.queue()
1304
+ # demo.launch(show_error=True)
1305
+
1306
+
1307
+
1308
+
1309
  import gradio as gr
1310
  import requests
1311
  import os
 
1647
  return
1648
 
1649
  # Select the model
1650
+ selected_model = chat_model if model_choice == "LM-1" else phi_pipe
1651
 
1652
  response, addresses = generate_answer(history[-1][0], choice, retrieval_mode, selected_model)
1653
  history[-1][1] = ""
 
1715
  return
1716
 
1717
  # Select the model
1718
+ selected_model = chat_model if model_choice == "LM-1" else phi_pipe
1719
 
1720
  response, addresses = generate_answer(history[-1][0], choice, retrieval_mode, selected_model)
1721
  history[-1][1] = ""
 
1763
  logging.debug(f"generate_answer called with choice: {choice}, retrieval_mode: {retrieval_mode}, and selected_model: {selected_model}")
1764
 
1765
  # Logic for disabling options for Phi-3.5
1766
+ if selected_model == "LM-2":
1767
  choice = None
1768
  retrieval_mode = None
1769
 
 
1801
  if retrieval_mode == "VDB":
1802
  logging.debug("Using VDB retrieval mode")
1803
  if selected_model == chat_model:
1804
+ logging.debug("Selected model: LM-1")
1805
  retriever = gpt_retriever
1806
  context = retriever.get_relevant_documents(message)
1807
  logging.debug(f"Retrieved context: {context}")
 
1816
  chain_type_kwargs={"prompt": prompt_template}
1817
  )
1818
  response = qa_chain({"query": message})
1819
+ logging.debug(f"LM-1 response: {response}")
1820
  return response['result'], extract_addresses(response['result'])
1821
 
1822
  elif selected_model == phi_pipe:
1823
+ logging.debug("Selected model: LM-2")
1824
  retriever = phi_retriever
1825
  context_documents = retriever.get_relevant_documents(message)
1826
  context = "\n".join([doc.page_content for doc in context_documents])
1827
+ logging.debug(f"Retrieved context for LM-2: {context}")
1828
 
1829
  # Use the correct template variable
1830
  prompt = phi_custom_template.format(context=context, question=message)
1831
+ logging.debug(f"Generated LM-2 prompt: {prompt}")
1832
 
1833
  response = selected_model(prompt, **{
1834
  "max_new_tokens": 400,
 
1839
 
1840
  if response:
1841
  generated_text = response[0]['generated_text']
1842
+ logging.debug(f"LM-2 Response: {generated_text}")
1843
  cleaned_response = clean_response(generated_text)
1844
  return cleaned_response, extract_addresses(cleaned_response)
1845
  else:
1846
+ logging.error("LM-2 did not return any response.")
1847
  return "No response generated.", []
1848
 
1849
  elif retrieval_mode == "KGF":
 
2110
  import concurrent.futures
2111
  import tempfile
2112
  import os
2113
+ import np
2114
  import logging
2115
  from queue import Queue
2116
  from threading import Thread
 
2335
  return gr.update(interactive=True), gr.update(interactive=True)
2336
 
2337
  def handle_model_choice_change(selected_model):
2338
+ if selected_model == "LM-2":
2339
+ # Disable retrieval mode and select style when LM-2 is selected
2340
+ return gr.update(interactive=False), gr.update(interactive=False), gr.update(interactive(False))
2341
+ elif selected_model == "LM-1":
2342
+ # Enable retrieval mode and select style for LM-1
2343
+ return gr.update(interactive=True), gr.update(interactive(True)), gr.update(interactive=True)
2344
  else:
2345
  # Default case: allow interaction
2346
+ return gr.update(interactive=True), gr.update(interactive(True)), gr.update(interactive=True)
2347
 
2348
 
2349
 
 
2524
  chatbot = gr.Chatbot([], elem_id="RADAR:Channel 94.1", bubble_full_width=False)
2525
  choice = gr.Radio(label="Select Style", choices=["Details", "Conversational"], value="Conversational")
2526
  retrieval_mode = gr.Radio(label="Retrieval Mode", choices=["VDB", "KGF"], value="VDB")
2527
+ model_choice = gr.Dropdown(label="Choose Model", choices=["LM-1", "LM-2"], value="LM-1")
2528
 
2529
  # Link the dropdown change to handle_model_choice_change
2530
  model_choice.change(fn=handle_model_choice_change, inputs=model_choice, outputs=[retrieval_mode, choice, choice])
 
2610
 
2611
  demo.queue()
2612
  demo.launch(show_error=True)
2613
+