Pijush2023 commited on
Commit
5203bf1
·
verified ·
1 Parent(s): 88d5dc9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +970 -113
app.py CHANGED
@@ -1,4 +1,959 @@
1
- #Main code header Library
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  import gradio as gr
3
  import requests
4
  import os
@@ -44,11 +999,11 @@ from langchain_core.messages import AIMessage, HumanMessage
44
  from langchain_core.output_parsers import StrOutputParser
45
  from langchain_core.runnables import RunnableBranch, RunnableLambda, RunnableParallel, RunnablePassthrough
46
 
47
- # Set environment variables for Torch- CUDA
 
48
  os.environ['PYTORCH_USE_CUDA_DSA'] = '1'
49
  os.environ['CUDA_LAUNCH_BLOCKING'] = '1'
50
 
51
- #Hugging face token Initilization
52
 
53
  hf_token = os.getenv("HF_TOKEN")
54
  if hf_token is None:
@@ -58,10 +1013,10 @@ else:
58
 
59
  logging.basicConfig(level=logging.DEBUG)
60
 
61
- #Embedding the vector with openai
62
 
63
  embeddings = OpenAIEmbeddings(api_key=os.environ['OPENAI_API_KEY'])
64
 
 
65
  # Pinecone setup
66
  from pinecone import Pinecone
67
  pc = Pinecone(api_key=os.environ['PINECONE_API_KEY'])
@@ -120,8 +1075,6 @@ In light of this, how can I assist you today? Feel free to ask any questions or
120
  Question: {{question}}
121
  Helpful Answer:"""
122
 
123
- #QA_Chain_templates
124
-
125
  QA_CHAIN_PROMPT_1 = PromptTemplate(input_variables=["context", "question"], template=template1)
126
  QA_CHAIN_PROMPT_2 = PromptTemplate(input_variables=["context", "question"], template=template2)
127
 
@@ -133,7 +1086,6 @@ graph = Neo4jGraph(
133
  )
134
 
135
  # Avoid pushing the graph documents to Neo4j every time
136
-
137
  # Only push the documents once and comment the code below after the initial push
138
  # dataset_name = "Pijush2023/birmindata07312024"
139
  # page_content_column = 'events_description'
@@ -147,10 +1099,6 @@ graph = Neo4jGraph(
147
  # graph_documents = llm_transformer.convert_to_graph_documents(documents)
148
  # graph.add_graph_documents(graph_documents, baseEntityLabel=True, include_source=True)
149
 
150
-
151
- #Neo4j Setup
152
-
153
-
154
  class Entities(BaseModel):
155
  names: list[str] = Field(..., description="All the person, organization, or business entities that appear in the text")
156
 
@@ -161,17 +1109,12 @@ entity_prompt = ChatPromptTemplate.from_messages([
161
 
162
  entity_chain = entity_prompt | chat_model.with_structured_output(Entities)
163
 
164
-
165
- #Remove Lucene Characther
166
-
167
  def remove_lucene_chars(input: str) -> str:
168
  return input.translate(str.maketrans({"\\": r"\\", "+": r"\+", "-": r"\-", "&": r"\&", "|": r"\|", "!": r"\!",
169
  "(": r"\(", ")": r"\)", "{": r"\{", "}": r"\}", "[": r"\[", "]": r"\]",
170
  "^": r"\^", "~": r"\~", "*": r"\*", "?": r"\?", ":": r"\:", '"': r'\"',
171
  ";": r"\;", " ": r"\ "}))
172
 
173
- #Full Text query Generator
174
-
175
  def generate_full_text_query(input: str) -> str:
176
  full_text_query = ""
177
  words = [el for el in remove_lucene_chars(input).split() if el]
@@ -180,8 +1123,6 @@ def generate_full_text_query(input: str) -> str:
180
  full_text_query += f" {words[-1]}~2"
181
  return full_text_query.strip()
182
 
183
- # Neo4j Retrieval connection
184
-
185
  def structured_retriever(question: str) -> str:
186
  result = ""
187
  entities = entity_chain.invoke({"question": question})
@@ -241,72 +1182,20 @@ _search_query = RunnableBranch(
241
  RunnableLambda(lambda x : x["question"]),
242
  )
243
 
244
- # # template = """Answer the question based only on the following context:
245
  # {context}
246
  # Question: {question}
247
  # Use natural language and be concise.
248
  # Answer:"""
249
 
250
- # Define conversational and detailed prompt templates for Neo4j responses
251
- neo4j_conversational_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. Given today's sunny and bright weather on __, 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.
252
  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.
253
-
254
  "It was my pleasure!"
255
  {{context}}
256
  Question: {{question}}
257
  Helpful Answer: __"""
258
 
259
- neo4j_details_template = 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, and activities in Birmingham that can enhance your experience.
260
- 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.
261
- Now, let me guide you through some of the exciting events happening today in Birmingham, Alabama:
262
- Address: >>, Birmingham, AL
263
- Time: >>__
264
- Date: >>__
265
- Description: >>__
266
- Address: >>, Birmingham, AL
267
- Time: >>__
268
- Date: >>__
269
- Description: >>__
270
- Address: >>, Birmingham, AL
271
- Time: >>__
272
- Date: >>__
273
- Description: >>__
274
- Address: >>, Birmingham, AL
275
- Time: >>__
276
- Date: >>__
277
- Description: >>__
278
- Address: >>, Birmingham, AL
279
- Time: >>__
280
- Date: >>__
281
- Description: >>__
282
- 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.
283
- It was my pleasure!
284
- {{context}}
285
- Question: {{question}}
286
- Helpful Answer:"""
287
-
288
- # Create prompt templates
289
- QA_CHAIN_PROMPT_NEO4J_CONVERSATIONAL = PromptTemplate(input_variables=["context", "question"], template=neo4j_conversational_template)
290
- QA_CHAIN_PROMPT_NEO4J_DETAILS = PromptTemplate(input_variables=["context", "question"], template=neo4j_details_template)
291
-
292
- # Define Neo4j retrieval chain for conversational mode
293
- def neo4j_retrieval_conversational(question: str):
294
- structured_data = structured_retriever(question)
295
- logging.debug(f"Structured data (Conversational): {structured_data}")
296
- prompt = QA_CHAIN_PROMPT_NEO4J_CONVERSATIONAL.format(context=structured_data, question=question)
297
- response = chat_model({"query": prompt})
298
- return response, []
299
-
300
- # Define Neo4j retrieval chain for detailed mode
301
- def neo4j_retrieval_details(question: str):
302
- structured_data = structured_retriever(question)
303
- logging.debug(f"Structured data (Details): {structured_data}")
304
- prompt = QA_CHAIN_PROMPT_NEO4J_DETAILS.format(context=structured_data, question=question)
305
- response = chat_model({"query": prompt})
306
- return response, extract_addresses(response)
307
-
308
-
309
- # qa_prompt = ChatPromptTemplate.from_template(template)
310
 
311
  chain_neo4j = (
312
  RunnableParallel(
@@ -321,28 +1210,6 @@ chain_neo4j = (
321
  )
322
 
323
  # Define a function to select between Pinecone and Neo4j
324
- # def generate_answer(message, choice, retrieval_mode):
325
- # logging.debug(f"generate_answer called with choice: {choice} and retrieval_mode: {retrieval_mode}")
326
-
327
- # prompt_template = QA_CHAIN_PROMPT_1 if choice == "Details" else QA_CHAIN_PROMPT_2
328
-
329
- # if retrieval_mode == "Vector":
330
- # qa_chain = RetrievalQA.from_chain_type(
331
- # llm=chat_model,
332
- # chain_type="stuff",
333
- # retriever=retriever,
334
- # chain_type_kwargs={"prompt": prompt_template}
335
- # )
336
- # response = qa_chain({"query": message})
337
- # logging.debug(f"Vector response: {response}")
338
- # return response['result'], extract_addresses(response['result'])
339
- # elif retrieval_mode == "Knowledge-Graph":
340
- # response = chain_neo4j.invoke({"question": message})
341
- # logging.debug(f"Knowledge-Graph response: {response}")
342
- # return response, extract_addresses(response)
343
- # else:
344
- # return "Invalid retrieval mode selected.", []
345
-
346
  def generate_answer(message, choice, retrieval_mode):
347
  logging.debug(f"generate_answer called with choice: {choice} and retrieval_mode: {retrieval_mode}")
348
 
@@ -359,16 +1226,12 @@ def generate_answer(message, choice, retrieval_mode):
359
  logging.debug(f"Vector response: {response}")
360
  return response['result'], extract_addresses(response['result'])
361
  elif retrieval_mode == "Knowledge-Graph":
362
- if choice == "Details":
363
- response, addresses = neo4j_retrieval_details(message)
364
- else:
365
- response, addresses = neo4j_retrieval_conversational(message)
366
  logging.debug(f"Knowledge-Graph response: {response}")
367
- return response, addresses
368
  else:
369
  return "Invalid retrieval mode selected.", []
370
 
371
-
372
  def bot(history, choice, tts_choice, retrieval_mode):
373
  if not history:
374
  return history
@@ -566,7 +1429,7 @@ def show_map_if_details(history, choice):
566
  if choice in ["Details", "Conversational"]:
567
  return gr.update(visible=True), update_map_with_response(history)
568
  else:
569
- return gr.update(visible=False), ""
570
 
571
  def generate_audio_elevenlabs(text):
572
  XI_API_KEY = os.environ['ELEVENLABS_API']
@@ -916,16 +1779,16 @@ with gr.Blocks(theme='Pijush2023/scikit-learn-pijush') as demo:
916
 
917
  # Define the sequence of actions for the "Retriever" button
918
  retriever_sequence = (
919
- retriever_button.click(fn=stop_audio, inputs=[], outputs=[audio_output], api_name="Ask_Retriever")
920
- .then(fn=add_message, inputs=[chatbot, chat_input], outputs=[chatbot, chat_input], api_name="voice_query")
921
- .then(fn=bot, inputs=[chatbot, choice, tts_choice, retrieval_mode], outputs=[chatbot, audio_output], api_name="generate_voice_response")
922
  .then(fn=show_map_if_details, inputs=[chatbot, choice], outputs=[location_output, location_output], api_name="map_finder")
923
  .then(fn=clear_textbox, inputs=[], outputs=[chat_input])
924
  )
925
 
926
  # Link the "Enter" key (submit event) to the same sequence of actions
927
  chat_input.submit(fn=stop_audio, inputs=[], outputs=[audio_output])
928
- chat_input.submit(fn=add_message, inputs=[chatbot, chat_input], outputs=[chatbot, chat_input], api_name="voice_query").then(
929
  fn=bot, inputs=[chatbot, choice, tts_choice, retrieval_mode], outputs=[chatbot, audio_output], api_name="generate_voice_response"
930
  ).then(
931
  fn=show_map_if_details, inputs=[chatbot, choice], outputs=[location_output, location_output], api_name="map_finder"
@@ -936,13 +1799,6 @@ with gr.Blocks(theme='Pijush2023/scikit-learn-pijush') as demo:
936
  audio_input = gr.Audio(sources=["microphone"], streaming=True, type='numpy', every=0.1)
937
  audio_input.stream(transcribe_function, inputs=[state, audio_input], outputs=[state, chat_input], api_name="voice_query_to_text")
938
 
939
- #Api Integration to gradio call function
940
-
941
- # with gr.Column():
942
- # weather_output = gr.HTML(value=fetch_local_weather())
943
- # news_output = gr.HTML(value=fetch_local_news())
944
- # events_output = gr.HTML(value=fetch_local_events())
945
-
946
  with gr.Column():
947
  image_output_1 = gr.Image(value=generate_image(hardcoded_prompt_1), width=400, height=400)
948
  image_output_2 = gr.Image(value=generate_image(hardcoded_prompt_2), width=400, height=400)
@@ -957,3 +1813,4 @@ demo.launch(share=True)
957
 
958
 
959
 
 
 
1
+ # #Main code header Library
2
+ # import gradio as gr
3
+ # import requests
4
+ # import os
5
+ # import time
6
+ # import re
7
+ # import logging
8
+ # import tempfile
9
+ # import folium
10
+ # import concurrent.futures
11
+ # import torch
12
+ # from PIL import Image
13
+ # from datetime import datetime
14
+ # from transformers import pipeline, AutoModelForSpeechSeq2Seq, AutoProcessor
15
+ # from googlemaps import Client as GoogleMapsClient
16
+ # from gtts import gTTS
17
+ # from diffusers import StableDiffusionPipeline
18
+ # from langchain_openai import OpenAIEmbeddings, ChatOpenAI
19
+ # from langchain_pinecone import PineconeVectorStore
20
+ # from langchain.prompts import PromptTemplate
21
+ # from langchain.chains import RetrievalQA
22
+ # from langchain.chains.conversation.memory import ConversationBufferWindowMemory
23
+ # from huggingface_hub import login
24
+ # from transformers.models.speecht5.number_normalizer import EnglishNumberNormalizer
25
+ # from parler_tts import ParlerTTSForConditionalGeneration
26
+ # from transformers import AutoTokenizer, AutoFeatureExtractor, set_seed
27
+ # from scipy.io.wavfile import write as write_wav
28
+ # from pydub import AudioSegment
29
+ # from string import punctuation
30
+ # import librosa
31
+ # from pathlib import Path
32
+ # import torchaudio
33
+ # import numpy as np
34
+
35
+ # # Neo4j imports
36
+ # from langchain.chains import GraphCypherQAChain
37
+ # from langchain_community.graphs import Neo4jGraph
38
+ # from langchain_community.document_loaders import HuggingFaceDatasetLoader
39
+ # from langchain_text_splitters import CharacterTextSplitter
40
+ # from langchain_experimental.graph_transformers import LLMGraphTransformer
41
+ # from langchain_core.prompts import ChatPromptTemplate
42
+ # from langchain_core.pydantic_v1 import BaseModel, Field
43
+ # from langchain_core.messages import AIMessage, HumanMessage
44
+ # from langchain_core.output_parsers import StrOutputParser
45
+ # from langchain_core.runnables import RunnableBranch, RunnableLambda, RunnableParallel, RunnablePassthrough
46
+
47
+ # # Set environment variables for Torch- CUDA
48
+ # os.environ['PYTORCH_USE_CUDA_DSA'] = '1'
49
+ # os.environ['CUDA_LAUNCH_BLOCKING'] = '1'
50
+
51
+ # #Hugging face token Initilization
52
+
53
+ # hf_token = os.getenv("HF_TOKEN")
54
+ # if hf_token is None:
55
+ # print("Please set your Hugging Face token in the environment variables.")
56
+ # else:
57
+ # login(token=hf_token)
58
+
59
+ # logging.basicConfig(level=logging.DEBUG)
60
+
61
+ # #Embedding the vector with openai
62
+
63
+ # embeddings = OpenAIEmbeddings(api_key=os.environ['OPENAI_API_KEY'])
64
+
65
+ # # Pinecone setup
66
+ # from pinecone import Pinecone
67
+ # pc = Pinecone(api_key=os.environ['PINECONE_API_KEY'])
68
+
69
+ # index_name = "radardata07242024"
70
+ # vectorstore = PineconeVectorStore(index_name=index_name, embedding=embeddings)
71
+ # retriever = vectorstore.as_retriever(search_kwargs={'k': 5})
72
+
73
+ # chat_model = ChatOpenAI(api_key=os.environ['OPENAI_API_KEY'], temperature=0, model='gpt-4o')
74
+
75
+ # conversational_memory = ConversationBufferWindowMemory(
76
+ # memory_key='chat_history',
77
+ # k=10,
78
+ # return_messages=True
79
+ # )
80
+
81
+ # # Prompt templates
82
+ # def get_current_date():
83
+ # return datetime.now().strftime("%B %d, %Y")
84
+
85
+ # current_date = get_current_date()
86
+
87
+ # 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, and activities in Birmingham that can enhance your experience.
88
+ # 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.
89
+ # Now, let me guide you through some of the exciting events happening today in Birmingham, Alabama:
90
+ # Address: >>, Birmingham, AL
91
+ # Time: >>__
92
+ # Date: >>__
93
+ # Description: >>__
94
+ # Address: >>, Birmingham, AL
95
+ # Time: >>__
96
+ # Date: >>__
97
+ # Description: >>__
98
+ # Address: >>, Birmingham, AL
99
+ # Time: >>__
100
+ # Date: >>__
101
+ # Description: >>__
102
+ # Address: >>, Birmingham, AL
103
+ # Time: >>__
104
+ # Date: >>__
105
+ # Description: >>__
106
+ # Address: >>, Birmingham, AL
107
+ # Time: >>__
108
+ # Date: >>__
109
+ # Description: >>__
110
+ # 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.
111
+ # It was my pleasure!
112
+ # {{context}}
113
+ # Question: {{question}}
114
+ # Helpful Answer:"""
115
+
116
+ # 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 specific locations. I draw upon my extensive knowledge of the area, including perennial events and historical context.
117
+ # 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.
118
+ # "It was my pleasure!"
119
+ # {{context}}
120
+ # Question: {{question}}
121
+ # Helpful Answer:"""
122
+
123
+ # #QA_Chain_templates
124
+
125
+ # QA_CHAIN_PROMPT_1 = PromptTemplate(input_variables=["context", "question"], template=template1)
126
+ # QA_CHAIN_PROMPT_2 = PromptTemplate(input_variables=["context", "question"], template=template2)
127
+
128
+ # # Neo4j setup
129
+ # graph = Neo4jGraph(
130
+ # url="neo4j+s://98f45cc0.databases.neo4j.io",
131
+ # username="neo4j",
132
+ # password="B_sZbapCTZoQDWj1JrhwqElsNa-jm5Zq1m_mAnyPYog"
133
+ # )
134
+
135
+ # # Avoid pushing the graph documents to Neo4j every time
136
+
137
+ # # Only push the documents once and comment the code below after the initial push
138
+ # # dataset_name = "Pijush2023/birmindata07312024"
139
+ # # page_content_column = 'events_description'
140
+ # # loader = HuggingFaceDatasetLoader(dataset_name, page_content_column)
141
+ # # data = loader.load()
142
+
143
+ # # text_splitter = CharacterTextSplitter(chunk_size=100, chunk_overlap=50)
144
+ # # documents = text_splitter.split_documents(data)
145
+
146
+ # # llm_transformer = LLMGraphTransformer(llm=chat_model)
147
+ # # graph_documents = llm_transformer.convert_to_graph_documents(documents)
148
+ # # graph.add_graph_documents(graph_documents, baseEntityLabel=True, include_source=True)
149
+
150
+
151
+ # #Neo4j Setup
152
+
153
+
154
+ # class Entities(BaseModel):
155
+ # names: list[str] = Field(..., description="All the person, organization, or business entities that appear in the text")
156
+
157
+ # entity_prompt = ChatPromptTemplate.from_messages([
158
+ # ("system", "You are extracting organization and person entities from the text."),
159
+ # ("human", "Use the given format to extract information from the following input: {question}"),
160
+ # ])
161
+
162
+ # entity_chain = entity_prompt | chat_model.with_structured_output(Entities)
163
+
164
+
165
+ # #Remove Lucene Characther
166
+
167
+ # def remove_lucene_chars(input: str) -> str:
168
+ # return input.translate(str.maketrans({"\\": r"\\", "+": r"\+", "-": r"\-", "&": r"\&", "|": r"\|", "!": r"\!",
169
+ # "(": r"\(", ")": r"\)", "{": r"\{", "}": r"\}", "[": r"\[", "]": r"\]",
170
+ # "^": r"\^", "~": r"\~", "*": r"\*", "?": r"\?", ":": r"\:", '"': r'\"',
171
+ # ";": r"\;", " ": r"\ "}))
172
+
173
+ # #Full Text query Generator
174
+
175
+ # def generate_full_text_query(input: str) -> str:
176
+ # full_text_query = ""
177
+ # words = [el for el in remove_lucene_chars(input).split() if el]
178
+ # for word in words[:-1]:
179
+ # full_text_query += f" {word}~2 AND"
180
+ # full_text_query += f" {words[-1]}~2"
181
+ # return full_text_query.strip()
182
+
183
+ # # Neo4j Retrieval connection
184
+
185
+ # def structured_retriever(question: str) -> str:
186
+ # result = ""
187
+ # entities = entity_chain.invoke({"question": question})
188
+ # for entity in entities.names:
189
+ # response = graph.query(
190
+ # """CALL db.index.fulltext.queryNodes('entity', $query, {limit:2})
191
+ # YIELD node,score
192
+ # CALL {
193
+ # WITH node
194
+ # MATCH (node)-[r:!MENTIONS]->(neighbor)
195
+ # RETURN node.id + ' - ' + type(r) + ' -> ' + neighbor.id AS output
196
+ # UNION ALL
197
+ # WITH node
198
+ # MATCH (node)<-[r:!MENTIONS]-(neighbor)
199
+ # RETURN neighbor.id + ' - ' + type(r) + ' -> ' + node.id AS output
200
+ # }
201
+ # RETURN output LIMIT 50
202
+ # """,
203
+ # {"query": generate_full_text_query(entity)},
204
+ # )
205
+ # result += "\n".join([el['output'] for el in response])
206
+ # return result
207
+
208
+ # def retriever_neo4j(question: str):
209
+ # structured_data = structured_retriever(question)
210
+ # logging.debug(f"Structured data: {structured_data}")
211
+ # return structured_data
212
+
213
+ # _template = """Given the following conversation and a follow-up question, rephrase the follow-up question to be a standalone question,
214
+ # in its original language.
215
+ # Chat History:
216
+ # {chat_history}
217
+ # Follow Up Input: {question}
218
+ # Standalone question:"""
219
+
220
+ # CONDENSE_QUESTION_PROMPT = PromptTemplate.from_template(_template)
221
+
222
+ # def _format_chat_history(chat_history: list[tuple[str, str]]) -> list:
223
+ # buffer = []
224
+ # for human, ai in chat_history:
225
+ # buffer.append(HumanMessage(content=human))
226
+ # buffer.append(AIMessage(content=ai))
227
+ # return buffer
228
+
229
+ # _search_query = RunnableBranch(
230
+ # (
231
+ # RunnableLambda(lambda x: bool(x.get("chat_history"))).with_config(
232
+ # run_name="HasChatHistoryCheck"
233
+ # ),
234
+ # RunnablePassthrough.assign(
235
+ # chat_history=lambda x: _format_chat_history(x["chat_history"])
236
+ # )
237
+ # | CONDENSE_QUESTION_PROMPT
238
+ # | ChatOpenAI(temperature=0, api_key=os.environ['OPENAI_API_KEY'])
239
+ # | StrOutputParser(),
240
+ # ),
241
+ # RunnableLambda(lambda x : x["question"]),
242
+ # )
243
+
244
+ # # # template = """Answer the question based only on the following context:
245
+ # # {context}
246
+ # # Question: {question}
247
+ # # Use natural language and be concise.
248
+ # # Answer:"""
249
+
250
+ # # Define conversational and detailed prompt templates for Neo4j responses
251
+ # neo4j_conversational_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. Given today's sunny and bright weather on __, 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.
252
+ # 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.
253
+
254
+ # "It was my pleasure!"
255
+ # {{context}}
256
+ # Question: {{question}}
257
+ # Helpful Answer: __"""
258
+
259
+ # neo4j_details_template = 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, and activities in Birmingham that can enhance your experience.
260
+ # 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.
261
+ # Now, let me guide you through some of the exciting events happening today in Birmingham, Alabama:
262
+ # Address: >>, Birmingham, AL
263
+ # Time: >>__
264
+ # Date: >>__
265
+ # Description: >>__
266
+ # Address: >>, Birmingham, AL
267
+ # Time: >>__
268
+ # Date: >>__
269
+ # Description: >>__
270
+ # Address: >>, Birmingham, AL
271
+ # Time: >>__
272
+ # Date: >>__
273
+ # Description: >>__
274
+ # Address: >>, Birmingham, AL
275
+ # Time: >>__
276
+ # Date: >>__
277
+ # Description: >>__
278
+ # Address: >>, Birmingham, AL
279
+ # Time: >>__
280
+ # Date: >>__
281
+ # Description: >>__
282
+ # 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.
283
+ # It was my pleasure!
284
+ # {{context}}
285
+ # Question: {{question}}
286
+ # Helpful Answer:"""
287
+
288
+ # # Create prompt templates
289
+ # QA_CHAIN_PROMPT_NEO4J_CONVERSATIONAL = PromptTemplate(input_variables=["context", "question"], template=neo4j_conversational_template)
290
+ # QA_CHAIN_PROMPT_NEO4J_DETAILS = PromptTemplate(input_variables=["context", "question"], template=neo4j_details_template)
291
+
292
+ # # Define Neo4j retrieval chain for conversational mode
293
+ # def neo4j_retrieval_conversational(question: str):
294
+ # structured_data = structured_retriever(question)
295
+ # logging.debug(f"Structured data (Conversational): {structured_data}")
296
+ # prompt = QA_CHAIN_PROMPT_NEO4J_CONVERSATIONAL.format(context=structured_data, question=question)
297
+ # response = chat_model({"query": prompt})
298
+ # return response, []
299
+
300
+ # # Define Neo4j retrieval chain for detailed mode
301
+ # def neo4j_retrieval_details(question: str):
302
+ # structured_data = structured_retriever(question)
303
+ # logging.debug(f"Structured data (Details): {structured_data}")
304
+ # prompt = QA_CHAIN_PROMPT_NEO4J_DETAILS.format(context=structured_data, question=question)
305
+ # response = chat_model({"query": prompt})
306
+ # return response, extract_addresses(response)
307
+
308
+
309
+ # # qa_prompt = ChatPromptTemplate.from_template(template)
310
+
311
+ # chain_neo4j = (
312
+ # RunnableParallel(
313
+ # {
314
+ # "context": _search_query | retriever_neo4j,
315
+ # "question": RunnablePassthrough(),
316
+ # }
317
+ # )
318
+ # | qa_prompt
319
+ # | chat_model
320
+ # | StrOutputParser()
321
+ # )
322
+
323
+ # # Define a function to select between Pinecone and Neo4j
324
+ # # def generate_answer(message, choice, retrieval_mode):
325
+ # # logging.debug(f"generate_answer called with choice: {choice} and retrieval_mode: {retrieval_mode}")
326
+
327
+ # # prompt_template = QA_CHAIN_PROMPT_1 if choice == "Details" else QA_CHAIN_PROMPT_2
328
+
329
+ # # if retrieval_mode == "Vector":
330
+ # # qa_chain = RetrievalQA.from_chain_type(
331
+ # # llm=chat_model,
332
+ # # chain_type="stuff",
333
+ # # retriever=retriever,
334
+ # # chain_type_kwargs={"prompt": prompt_template}
335
+ # # )
336
+ # # response = qa_chain({"query": message})
337
+ # # logging.debug(f"Vector response: {response}")
338
+ # # return response['result'], extract_addresses(response['result'])
339
+ # # elif retrieval_mode == "Knowledge-Graph":
340
+ # # response = chain_neo4j.invoke({"question": message})
341
+ # # logging.debug(f"Knowledge-Graph response: {response}")
342
+ # # return response, extract_addresses(response)
343
+ # # else:
344
+ # # return "Invalid retrieval mode selected.", []
345
+
346
+ # def generate_answer(message, choice, retrieval_mode):
347
+ # logging.debug(f"generate_answer called with choice: {choice} and retrieval_mode: {retrieval_mode}")
348
+
349
+ # prompt_template = QA_CHAIN_PROMPT_1 if choice == "Details" else QA_CHAIN_PROMPT_2
350
+
351
+ # if retrieval_mode == "Vector":
352
+ # qa_chain = RetrievalQA.from_chain_type(
353
+ # llm=chat_model,
354
+ # chain_type="stuff",
355
+ # retriever=retriever,
356
+ # chain_type_kwargs={"prompt": prompt_template}
357
+ # )
358
+ # response = qa_chain({"query": message})
359
+ # logging.debug(f"Vector response: {response}")
360
+ # return response['result'], extract_addresses(response['result'])
361
+ # elif retrieval_mode == "Knowledge-Graph":
362
+ # if choice == "Details":
363
+ # response, addresses = neo4j_retrieval_details(message)
364
+ # else:
365
+ # response, addresses = neo4j_retrieval_conversational(message)
366
+ # logging.debug(f"Knowledge-Graph response: {response}")
367
+ # return response, addresses
368
+ # else:
369
+ # return "Invalid retrieval mode selected.", []
370
+
371
+
372
+ # def bot(history, choice, tts_choice, retrieval_mode):
373
+ # if not history:
374
+ # return history
375
+
376
+ # response, addresses = generate_answer(history[-1][0], choice, retrieval_mode)
377
+ # history[-1][1] = ""
378
+
379
+ # with concurrent.futures.ThreadPoolExecutor() as executor:
380
+ # if tts_choice == "Alpha":
381
+ # audio_future = executor.submit(generate_audio_elevenlabs, response)
382
+ # elif tts_choice == "Beta":
383
+ # audio_future = executor.submit(generate_audio_parler_tts, response)
384
+ # elif tts_choice == "Gamma":
385
+ # audio_future = executor.submit(generate_audio_mars5, response)
386
+
387
+ # for character in response:
388
+ # history[-1][1] += character
389
+ # time.sleep(0.05)
390
+ # yield history, None
391
+
392
+ # audio_path = audio_future.result()
393
+ # yield history, audio_path
394
+
395
+ # history.append([response, None]) # Ensure the response is added in the correct format
396
+
397
+ # def add_message(history, message):
398
+ # history.append((message, None))
399
+ # return history, gr.Textbox(value="", interactive=True, placeholder="Enter message or upload file...", show_label=False)
400
+
401
+ # def print_like_dislike(x: gr.LikeData):
402
+ # print(x.index, x.value, x.liked)
403
+
404
+ # def extract_addresses(response):
405
+ # if not isinstance(response, str):
406
+ # response = str(response)
407
+ # address_patterns = [
408
+ # r'([A-Z].*,\sBirmingham,\sAL\s\d{5})',
409
+ # r'(\d{4}\s.*,\sBirmingham,\sAL\s\d{5})',
410
+ # r'([A-Z].*,\sAL\s\d{5})',
411
+ # r'([A-Z].*,.*\sSt,\sBirmingham,\sAL\s\d{5})',
412
+ # r'([A-Z].*,.*\sStreets,\sBirmingham,\sAL\s\d{5})',
413
+ # r'(\d{2}.*\sStreets)',
414
+ # r'([A-Z].*\s\d{2},\sBirmingham,\sAL\s\d{5})',
415
+ # r'([a-zA-Z]\s Birmingham)',
416
+ # r'([a-zA-Z].*,\sBirmingham,\sAL)',
417
+ # r'(^Birmingham,AL$)'
418
+ # ]
419
+ # addresses = []
420
+ # for pattern in address_patterns:
421
+ # addresses.extend(re.findall(pattern, response))
422
+ # return addresses
423
+
424
+ # all_addresses = []
425
+
426
+ # def generate_map(location_names):
427
+ # global all_addresses
428
+ # all_addresses.extend(location_names)
429
+
430
+ # api_key = os.environ['GOOGLEMAPS_API_KEY']
431
+ # gmaps = GoogleMapsClient(key=api_key)
432
+
433
+ # m = folium.Map(location=[33.5175, -86.809444], zoom_start=12)
434
+
435
+ # for location_name in all_addresses:
436
+ # geocode_result = gmaps.geocode(location_name)
437
+ # if geocode_result:
438
+ # location = geocode_result[0]['geometry']['location']
439
+ # folium.Marker(
440
+ # [location['lat'], location['lng']],
441
+ # tooltip=f"{geocode_result[0]['formatted_address']}"
442
+ # ).add_to(m)
443
+
444
+ # map_html = m._repr_html_()
445
+ # return map_html
446
+
447
+ # def fetch_local_news():
448
+ # api_key = os.environ['SERP_API']
449
+ # url = f'https://serpapi.com/search.json?engine=google_news&q=birmingham headline&api_key={api_key}'
450
+ # response = requests.get(url)
451
+ # if response.status_code == 200:
452
+ # results = response.json().get("news_results", [])
453
+ # news_html = """
454
+ # <h2 style="font-family: 'Georgia', serif; color: #ff0000; background-color: #f8f8f8; padding: 10px; border-radius: 10px;">Birmingham Today</h2>
455
+ # <style>
456
+ # .news-item {
457
+ # font-family: 'Verdana', sans-serif;
458
+ # color: #333;
459
+ # background-color: #f0f8ff;
460
+ # margin-bottom: 15px;
461
+ # padding: 10px;
462
+ # border-radius: 5px;
463
+ # transition: box-shadow 0.3s ease, background-color 0.3s ease;
464
+ # font-weight: bold;
465
+ # }
466
+ # .news-item:hover {
467
+ # box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
468
+ # background-color: #e6f7ff;
469
+ # }
470
+ # .news-item a {
471
+ # color: #1E90FF;
472
+ # text-decoration: none;
473
+ # font-weight: bold;
474
+ # }
475
+ # .news-item a:hover {
476
+ # text-decoration: underline;
477
+ # }
478
+ # .news-preview {
479
+ # position: absolute;
480
+ # display: none;
481
+ # border: 1px solid #ccc;
482
+ # border-radius: 5px;
483
+ # box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
484
+ # background-color: white;
485
+ # z-index: 1000;
486
+ # max-width: 300px;
487
+ # padding: 10px;
488
+ # font-family: 'Verdana', sans-serif;
489
+ # color: #333;
490
+ # }
491
+ # </style>
492
+ # <script>
493
+ # function showPreview(event, previewContent) {
494
+ # var previewBox = document.getElementById('news-preview');
495
+ # previewBox.innerHTML = previewContent;
496
+ # previewBox.style.left = event.pageX + 'px';
497
+ # previewBox.style.top = event.pageY + 'px';
498
+ # previewBox.style.display = 'block';
499
+ # }
500
+ # function hidePreview() {
501
+ # var previewBox = document.getElementById('news-preview');
502
+ # previewBox.style.display = 'none';
503
+ # }
504
+ # </script>
505
+ # <div id="news-preview" class="news-preview"></div>
506
+ # """
507
+ # for index, result in enumerate(results[:7]):
508
+ # title = result.get("title", "No title")
509
+ # link = result.get("link", "#")
510
+ # snippet = result.get("snippet", "")
511
+ # news_html += f"""
512
+ # <div class="news-item" onmouseover="showPreview(event, '{snippet}')" onmouseout="hidePreview()">
513
+ # <a href='{link}' target='_blank'>{index + 1}. {title}</a>
514
+ # <p>{snippet}</p>
515
+ # </div>
516
+ # """
517
+ # return news_html
518
+ # else:
519
+ # return "<p>Failed to fetch local news</p>"
520
+
521
+ # import numpy as np
522
+ # import torch
523
+ # from transformers import pipeline, AutoModelForSpeechSeq2Seq, AutoProcessor
524
+
525
+ # model_id = 'openai/whisper-large-v3'
526
+ # device = "cuda:0" if torch.cuda.is_available() else "cpu"
527
+ # torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
528
+ # model = AutoModelForSpeechSeq2Seq.from_pretrained(model_id, torch_dtype=torch_dtype).to(device)
529
+ # processor = AutoProcessor.from_pretrained(model_id)
530
+
531
+ # 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)
532
+
533
+ # base_audio_drive = "/data/audio"
534
+
535
+ # def transcribe_function(stream, new_chunk):
536
+ # try:
537
+ # sr, y = new_chunk[0], new_chunk[1]
538
+ # except TypeError:
539
+ # print(f"Error chunk structure: {type(new_chunk)}, content: {new_chunk}")
540
+ # return stream, "", None
541
+
542
+ # y = y.astype(np.float32) / np.max(np.abs(y))
543
+
544
+ # if stream is not None:
545
+ # stream = np.concatenate([stream, y])
546
+ # else:
547
+ # stream = y
548
+
549
+ # result = pipe_asr({"array": stream, "sampling_rate": sr}, return_timestamps=False)
550
+
551
+ # full_text = result.get("text","")
552
+
553
+ # return stream, full_text, result
554
+
555
+ # def update_map_with_response(history):
556
+ # if not history:
557
+ # return ""
558
+ # response = history[-1][1]
559
+ # addresses = extract_addresses(response)
560
+ # return generate_map(addresses)
561
+
562
+ # def clear_textbox():
563
+ # return ""
564
+
565
+ # def show_map_if_details(history, choice):
566
+ # if choice in ["Details", "Conversational"]:
567
+ # return gr.update(visible=True), update_map_with_response(history)
568
+ # else:
569
+ # return gr.update(visible=False), ""
570
+
571
+ # def generate_audio_elevenlabs(text):
572
+ # XI_API_KEY = os.environ['ELEVENLABS_API']
573
+ # VOICE_ID = 'd9MIrwLnvDeH7aZb61E9'
574
+ # tts_url = f"https://api.elevenlabs.io/v1/text-to-speech/{VOICE_ID}/stream"
575
+ # headers = {
576
+ # "Accept": "application/json",
577
+ # "xi-api-key": XI_API_KEY
578
+ # }
579
+ # data = {
580
+ # "text": str(text),
581
+ # "model_id": "eleven_multilingual_v2",
582
+ # "voice_settings": {
583
+ # "stability": 1.0,
584
+ # "similarity_boost": 0.0,
585
+ # "style": 0.60,
586
+ # "use_speaker_boost": False
587
+ # }
588
+ # }
589
+ # response = requests.post(tts_url, headers=headers, json=data, stream=True)
590
+ # if response.ok:
591
+ # audio_segments = []
592
+ # with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as f:
593
+ # for chunk in response.iter_content(chunk_size=1024):
594
+ # if chunk:
595
+ # f.write(chunk)
596
+ # audio_segments.append(chunk)
597
+ # temp_audio_path = f.name
598
+
599
+ # # Combine all audio chunks into a single file
600
+ # combined_audio = AudioSegment.from_file(temp_audio_path, format="mp3")
601
+ # combined_audio_path = os.path.join(tempfile.gettempdir(), "elevenlabs_combined_audio.mp3")
602
+ # combined_audio.export(combined_audio_path, format="mp3")
603
+
604
+ # logging.debug(f"Audio saved to {combined_audio_path}")
605
+ # return combined_audio_path
606
+ # else:
607
+ # logging.error(f"Error generating audio: {response.text}")
608
+ # return None
609
+
610
+
611
+ # repo_id = "parler-tts/parler-tts-mini-expresso"
612
+
613
+ # parler_model = ParlerTTSForConditionalGeneration.from_pretrained(repo_id).to(device)
614
+ # parler_tokenizer = AutoTokenizer.from_pretrained(repo_id)
615
+ # parler_feature_extractor = AutoFeatureExtractor.from_pretrained(repo_id)
616
+
617
+ # SAMPLE_RATE = parler_feature_extractor.sampling_rate
618
+ # SEED = 42
619
+
620
+ # def preprocess(text):
621
+ # number_normalizer = EnglishNumberNormalizer()
622
+ # text = number_normalizer(text).strip()
623
+ # if text[-1] not in punctuation:
624
+ # text = f"{text}."
625
+
626
+ # abbreviations_pattern = r'\b[A-Z][A-Z\.]+\b'
627
+
628
+ # def separate_abb(chunk):
629
+ # chunk = chunk.replace(".", "")
630
+ # return " ".join(chunk)
631
+
632
+ # abbreviations = re.findall(abbreviations_pattern, text)
633
+ # for abv in abbreviations:
634
+ # if abv in text:
635
+ # text is text.replace(abv, separate_abb(abv))
636
+ # return text
637
+
638
+ # def chunk_text(text, max_length=250):
639
+ # words = text.split()
640
+ # chunks = []
641
+ # current_chunk = []
642
+ # current_length = 0
643
+
644
+ # for word in words:
645
+ # if current_length + len(word) + 1 <= max_length:
646
+ # current_chunk.append(word)
647
+ # current_length += len(word) + 1
648
+ # else:
649
+ # chunks.append(' '.join(current_chunk))
650
+ # current_chunk = [word]
651
+ # current_length = len(word) + 1
652
+
653
+ # if current_chunk:
654
+ # chunks.append(' '.join(current_chunk))
655
+
656
+ # return chunks
657
+
658
+ # def generate_audio_parler_tts(text):
659
+ # description = "Thomas speaks with emphasis and excitement at a moderate pace with high quality."
660
+ # chunks = chunk_text(preprocess(text))
661
+ # audio_segments = []
662
+
663
+ # for chunk in chunks:
664
+ # inputs = parler_tokenizer(description, return_tensors="pt").to(device)
665
+ # prompt = parler_tokenizer(chunk, return_tensors="pt").to(device)
666
+
667
+ # set_seed(SEED)
668
+ # generation = parler_model.generate(input_ids=inputs.input_ids, prompt_input_ids=prompt.input_ids)
669
+ # audio_arr = generation.cpu().numpy().squeeze()
670
+
671
+ # temp_audio_path = os.path.join(tempfile.gettempdir(), f"parler_tts_audio_{len(audio_segments)}.wav")
672
+ # write_wav(temp_audio_path, SAMPLE_RATE, audio_arr)
673
+ # audio_segments.append(AudioSegment.from_wav(temp_audio_path))
674
+
675
+ # combined_audio = sum(audio_segments)
676
+ # combined_audio_path = os.path.join(tempfile.gettempdir(), "parler_tts_combined_audio.wav")
677
+ # combined_audio.export(combined_audio_path, format="wav")
678
+
679
+ # logging.debug(f"Audio saved to {combined_audio_path}")
680
+ # return combined_audio_path
681
+
682
+ # # Load the MARS5 model
683
+ # mars5, config_class = torch.hub.load('Camb-ai/mars5-tts', 'mars5_english', trust_repo=True)
684
+
685
+ # def generate_audio_mars5(text):
686
+ # description = "Thomas speaks with emphasis and excitement at a moderate pace with high quality."
687
+ # kwargs_dict = {
688
+ # 'temperature': 0.2,
689
+ # 'top_k': -1,
690
+ # 'top_p': 0.2,
691
+ # 'typical_p': 1.0,
692
+ # 'freq_penalty': 2.6,
693
+ # 'presence_penalty': 0.4,
694
+ # 'rep_penalty_window': 100,
695
+ # 'max_prompt_phones': 360,
696
+ # 'deep_clone': True,
697
+ # 'nar_guidance_w': 3
698
+ # }
699
+
700
+ # chunks = chunk_text(preprocess(text))
701
+ # audio_segments = []
702
+
703
+ # for chunk in chunks:
704
+ # wav = torch.zeros(1, mars5.sr) # Use a placeholder silent audio for the reference
705
+ # cfg = config_class(**{k: kwargs_dict[k] for k in kwargs_dict if k in config_class.__dataclass_fields__})
706
+ # ar_codes, wav_out = mars5.tts(chunk, wav, "", cfg=cfg)
707
+
708
+ # temp_audio_path = os.path.join(tempfile.gettempdir(), f"mars5_audio_{len(audio_segments)}.wav")
709
+ # torchaudio.save(temp_audio_path, wav_out.unsqueeze(0), mars5.sr)
710
+ # audio_segments.append(AudioSegment.from_wav(temp_audio_path))
711
+
712
+ # combined_audio = sum(audio_segments)
713
+ # combined_audio_path = os.path.join(tempfile.gettempdir(), "mars5_combined_audio.wav")
714
+ # combined_audio.export(combined_audio_path, format="wav")
715
+
716
+ # logging.debug(f"Audio saved to {combined_audio_path}")
717
+ # return combined_audio_path
718
+
719
+ # pipe = StableDiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2", torch_dtype=torch.float16)
720
+ # pipe.to(device)
721
+
722
+ # def generate_image(prompt):
723
+ # with torch.cuda.amp.autocast():
724
+ # image = pipe(
725
+ # prompt,
726
+ # num_inference_steps=28,
727
+ # guidance_scale=3.0,
728
+ # ).images[0]
729
+ # return image
730
+
731
+ # hardcoded_prompt_1 = "Give a high quality photograph of a great looking red 2026 Toyota coupe against a skyline setting in the night, michael mann style in omaha enticing the consumer to buy this product"
732
+ # hardcoded_prompt_2 = "A vibrant and dynamic football game scene in the style of Peter Paul Rubens, showcasing the intense match between Alabama and Nebraska. The players are depicted with the dramatic, muscular physiques and expressive faces typical of Rubens' style. The Alabama team is wearing their iconic crimson and white uniforms, while the Nebraska team is in their classic red and white attire. The scene is filled with action, with players in mid-motion, tackling, running, and catching the ball. The background features a grand stadium filled with cheering fans, banners, and the natural landscape in the distance. The colors are rich and vibrant, with a strong use of light and shadow to create depth and drama. The overall atmosphere captures the intensity and excitement of the game, infused with the grandeur and dynamism characteristic of Rubens' work."
733
+ # hardcoded_prompt_3 = "Create a high-energy scene of a DJ performing on a large stage with vibrant lights, colorful lasers, a lively dancing crowd, and various electronic equipment in the background."
734
+
735
+ # def update_images():
736
+ # image_1 = generate_image(hardcoded_prompt_1)
737
+ # image_2 = generate_image(hardcoded_prompt_2)
738
+ # image_3 = generate_image(hardcoded_prompt_3)
739
+ # return image_1, image_2, image_3
740
+
741
+ # def fetch_local_events():
742
+ # api_key = os.environ['SERP_API']
743
+ # url = f'https://serpapi.com/search.json?engine=google_events&q=Events+in+Birmingham&hl=en&gl=us&api_key={api_key}'
744
+ # response = requests.get(url)
745
+ # if response.status_code == 200:
746
+ # events_results = response.json().get("events_results", [])
747
+ # events_html = """
748
+ # <h2 style="font-family: 'Georgia', serif; color: #ff0000; background-color: #f8f8f8; padding: 10px; border-radius: 10px;">Local Events</h2>
749
+ # <style>
750
+ # table {
751
+ # font-family: 'Verdana', sans-serif;
752
+ # color: #333;
753
+ # border-collapse: collapse;
754
+ # width: 100%;
755
+ # }
756
+ # th, td {
757
+ # border: 1px solid #fff !important;
758
+ # padding: 8px;
759
+ # }
760
+ # th {
761
+ # background-color: #f2f2f2;
762
+ # color: #333;
763
+ # text-align: left;
764
+ # }
765
+ # tr:hover {
766
+ # background-color: #f5f5f5;
767
+ # }
768
+ # .event-link {
769
+ # color: #1E90FF;
770
+ # text-decoration: none;
771
+ # }
772
+ # .event-link:hover {
773
+ # text-decoration: underline;
774
+ # }
775
+ # </style>
776
+ # <table>
777
+ # <tr>
778
+ # <th>Title</th>
779
+ # <th>Date and Time</th>
780
+ # <th>Location</th>
781
+ # </tr>
782
+ # """
783
+ # for event in events_results:
784
+ # title = event.get("title", "No title")
785
+ # date_info = event.get("date", {})
786
+ # date = f"{date_info.get('start_date', '')} {date_info.get('when', '')}".replace("{", "").replace("}", "")
787
+ # location = event.get("address", "No location")
788
+ # if isinstance(location, list):
789
+ # location = " ".join(location)
790
+ # location = location.replace("[", "").replace("]", "")
791
+ # link = event.get("link", "#")
792
+ # events_html += f"""
793
+ # <tr>
794
+ # <td><a class='event-link' href='{link}' target='_blank'>{title}</a></td>
795
+ # <td>{date}</td>
796
+ # <td>{location}</td>
797
+ # </tr>
798
+ # """
799
+ # events_html += "</table>"
800
+ # return events_html
801
+ # else:
802
+ # return "<p>Failed to fetch local events</p>"
803
+
804
+ # def get_weather_icon(condition):
805
+ # condition_map = {
806
+ # "Clear": "c01d",
807
+ # "Partly Cloudy": "c02d",
808
+ # "Cloudy": "c03d",
809
+ # "Overcast": "c04d",
810
+ # "Mist": "a01d",
811
+ # "Patchy rain possible": "r01d",
812
+ # "Light rain": "r02d",
813
+ # "Moderate rain": "r03d",
814
+ # "Heavy rain": "r04d",
815
+ # "Snow": "s01d",
816
+ # "Thunderstorm": "t01d",
817
+ # "Fog": "a05d",
818
+ # }
819
+ # return condition_map.get(condition, "c04d")
820
+
821
+ # def fetch_local_weather():
822
+ # try:
823
+ # api_key = os.environ['WEATHER_API']
824
+ # url = f'https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/birmingham?unitGroup=metric&include=events%2Calerts%2Chours%2Cdays%2Ccurrent&key={api_key}'
825
+ # response = requests.get(url)
826
+ # response.raise_for_status()
827
+ # jsonData = response.json()
828
+
829
+ # current_conditions = jsonData.get("currentConditions", {})
830
+ # temp_celsius = current_conditions.get("temp", "N/A")
831
+
832
+ # if temp_celsius != "N/A":
833
+ # temp_fahrenheit = int((temp_celsius * 9/5) + 32)
834
+ # else:
835
+ # temp_fahrenheit = "N/A"
836
+
837
+ # condition = current_conditions.get("conditions", "N/A")
838
+ # humidity = current_conditions.get("humidity", "N/A")
839
+
840
+ # weather_html = f"""
841
+ # <div class="weather-theme">
842
+ # <h2 style="font-family: 'Georgia', serif; color: #ff0000; background-color: #f8f8f8; padding: 10px; border-radius: 10px;">Local Weather</h2>
843
+ # <div class="weather-content">
844
+ # <div class="weather-icon">
845
+ # <img src="https://www.weatherbit.io/static/img/icons/{get_weather_icon(condition)}.png" alt="{condition}" style="width: 100px; height: 100px;">
846
+ # </div>
847
+ # <div class="weather-details">
848
+ # <p style="font-family: 'Verdana', sans-serif; color: #333; font-size: 1.2em;">Temperature: {temp_fahrenheit}°F</p>
849
+ # <p style="font-family: 'Verdana', sans-serif; color: #333; font-size: 1.2em;">Condition: {condition}</p>
850
+ # <p style="font-family: 'Verdana', sans-serif; color: #333; font-size: 1.2em;">Humidity: {humidity}%</p>
851
+ # </div>
852
+ # </div>
853
+ # </div>
854
+ # <style>
855
+ # .weather-theme {{
856
+ # animation: backgroundAnimation 10s infinite alternate;
857
+ # border-radius: 10px;
858
+ # padding: 10px;
859
+ # margin-bottom: 15px;
860
+ # background: linear-gradient(45deg, #ffcc33, #ff6666, #ffcc33, #ff6666);
861
+ # background-size: 400% 400%;
862
+ # box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
863
+ # transition: box-shadow 0.3s ease, background-color 0.3s ease;
864
+ # }}
865
+ # .weather-theme:hover {{
866
+ # box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
867
+ # background-position: 100% 100%;
868
+ # }}
869
+ # @keyframes backgroundAnimation {{
870
+ # 0% {{ background-position: 0% 50%; }}
871
+ # 100% {{ background-position: 100% 50%; }}
872
+ # }}
873
+ # .weather-content {{
874
+ # display: flex;
875
+ # align-items: center;
876
+ # }}
877
+ # .weather-icon {{
878
+ # flex: 1;
879
+ # }}
880
+ # .weather-details {{
881
+ # flex 3;
882
+ # }}
883
+ # </style>
884
+ # """
885
+ # return weather_html
886
+ # except requests.exceptions.RequestException as e:
887
+ # return f"<p>Failed to fetch local weather: {e}</p>"
888
+
889
+ # with gr.Blocks(theme='Pijush2023/scikit-learn-pijush') as demo:
890
+ # with gr.Row():
891
+ # with gr.Column():
892
+ # state = gr.State()
893
+
894
+ # chatbot = gr.Chatbot([], elem_id="RADAR:Channel 94.1", bubble_full_width=False)
895
+ # choice = gr.Radio(label="Select Style", choices=["Details", "Conversational"], value="Conversational")
896
+ # retrieval_mode = gr.Radio(label="Retrieval Mode", choices=["Vector", "Knowledge-Graph"], value="Vector")
897
+
898
+ # gr.Markdown("<h1 style='color: red;'>Talk to RADAR</h1>", elem_id="voice-markdown")
899
+
900
+ # chat_input = gr.Textbox(show_copy_button=True, interactive=True, show_label=False, label="ASK Radar !!!", placeholder="After Prompt,click Retriever Only")
901
+ # tts_choice = gr.Radio(label="Select TTS System", choices=["Alpha", "Beta", "Gamma"], value="Alpha")
902
+ # retriever_button = gr.Button("Retriever")
903
+
904
+ # clear_button = gr.Button("Clear")
905
+ # clear_button.click(lambda:[None,None] ,outputs=[chat_input, state])
906
+
907
+ # gr.Markdown("<h1 style='color: red;'>Radar Map</h1>", elem_id="Map-Radar")
908
+ # location_output = gr.HTML()
909
+
910
+ # # Define a single audio component
911
+ # audio_output = gr.Audio(interactive=False, autoplay=True)
912
+
913
+ # def stop_audio():
914
+ # audio_output.stop()
915
+ # return None
916
+
917
+ # # Define the sequence of actions for the "Retriever" button
918
+ # retriever_sequence = (
919
+ # retriever_button.click(fn=stop_audio, inputs=[], outputs=[audio_output], api_name="Ask_Retriever")
920
+ # .then(fn=add_message, inputs=[chatbot, chat_input], outputs=[chatbot, chat_input], api_name="voice_query")
921
+ # .then(fn=bot, inputs=[chatbot, choice, tts_choice, retrieval_mode], outputs=[chatbot, audio_output], api_name="generate_voice_response")
922
+ # .then(fn=show_map_if_details, inputs=[chatbot, choice], outputs=[location_output, location_output], api_name="map_finder")
923
+ # .then(fn=clear_textbox, inputs=[], outputs=[chat_input])
924
+ # )
925
+
926
+ # # Link the "Enter" key (submit event) to the same sequence of actions
927
+ # chat_input.submit(fn=stop_audio, inputs=[], outputs=[audio_output])
928
+ # chat_input.submit(fn=add_message, inputs=[chatbot, chat_input], outputs=[chatbot, chat_input], api_name="voice_query").then(
929
+ # fn=bot, inputs=[chatbot, choice, tts_choice, retrieval_mode], outputs=[chatbot, audio_output], api_name="generate_voice_response"
930
+ # ).then(
931
+ # fn=show_map_if_details, inputs=[chatbot, choice], outputs=[location_output, location_output], api_name="map_finder"
932
+ # ).then(
933
+ # fn=clear_textbox, inputs=[], outputs=[chat_input]
934
+ # )
935
+
936
+ # audio_input = gr.Audio(sources=["microphone"], streaming=True, type='numpy', every=0.1)
937
+ # audio_input.stream(transcribe_function, inputs=[state, audio_input], outputs=[state, chat_input], api_name="voice_query_to_text")
938
+
939
+ # #Api Integration to gradio call function
940
+
941
+ # # with gr.Column():
942
+ # # weather_output = gr.HTML(value=fetch_local_weather())
943
+ # # news_output = gr.HTML(value=fetch_local_news())
944
+ # # events_output = gr.HTML(value=fetch_local_events())
945
+
946
+ # with gr.Column():
947
+ # image_output_1 = gr.Image(value=generate_image(hardcoded_prompt_1), width=400, height=400)
948
+ # image_output_2 = gr.Image(value=generate_image(hardcoded_prompt_2), width=400, height=400)
949
+ # image_output_3 = gr.Image(value=generate_image(hardcoded_prompt_3), width=400, height=400)
950
+
951
+ # refresh_button = gr.Button("Refresh Images")
952
+ # refresh_button.click(fn=update_images, inputs=None, outputs=[image_output_1, image_output_2, image_output_3], api_name="update_image")
953
+
954
+ # demo.queue()
955
+ # demo.launch(share=True)
956
+
957
  import gradio as gr
958
  import requests
959
  import os
 
999
  from langchain_core.output_parsers import StrOutputParser
1000
  from langchain_core.runnables import RunnableBranch, RunnableLambda, RunnableParallel, RunnablePassthrough
1001
 
1002
+
1003
+ # Set environment variables for CUDA
1004
  os.environ['PYTORCH_USE_CUDA_DSA'] = '1'
1005
  os.environ['CUDA_LAUNCH_BLOCKING'] = '1'
1006
 
 
1007
 
1008
  hf_token = os.getenv("HF_TOKEN")
1009
  if hf_token is None:
 
1013
 
1014
  logging.basicConfig(level=logging.DEBUG)
1015
 
 
1016
 
1017
  embeddings = OpenAIEmbeddings(api_key=os.environ['OPENAI_API_KEY'])
1018
 
1019
+
1020
  # Pinecone setup
1021
  from pinecone import Pinecone
1022
  pc = Pinecone(api_key=os.environ['PINECONE_API_KEY'])
 
1075
  Question: {{question}}
1076
  Helpful Answer:"""
1077
 
 
 
1078
  QA_CHAIN_PROMPT_1 = PromptTemplate(input_variables=["context", "question"], template=template1)
1079
  QA_CHAIN_PROMPT_2 = PromptTemplate(input_variables=["context", "question"], template=template2)
1080
 
 
1086
  )
1087
 
1088
  # Avoid pushing the graph documents to Neo4j every time
 
1089
  # Only push the documents once and comment the code below after the initial push
1090
  # dataset_name = "Pijush2023/birmindata07312024"
1091
  # page_content_column = 'events_description'
 
1099
  # graph_documents = llm_transformer.convert_to_graph_documents(documents)
1100
  # graph.add_graph_documents(graph_documents, baseEntityLabel=True, include_source=True)
1101
 
 
 
 
 
1102
  class Entities(BaseModel):
1103
  names: list[str] = Field(..., description="All the person, organization, or business entities that appear in the text")
1104
 
 
1109
 
1110
  entity_chain = entity_prompt | chat_model.with_structured_output(Entities)
1111
 
 
 
 
1112
  def remove_lucene_chars(input: str) -> str:
1113
  return input.translate(str.maketrans({"\\": r"\\", "+": r"\+", "-": r"\-", "&": r"\&", "|": r"\|", "!": r"\!",
1114
  "(": r"\(", ")": r"\)", "{": r"\{", "}": r"\}", "[": r"\[", "]": r"\]",
1115
  "^": r"\^", "~": r"\~", "*": r"\*", "?": r"\?", ":": r"\:", '"': r'\"',
1116
  ";": r"\;", " ": r"\ "}))
1117
 
 
 
1118
  def generate_full_text_query(input: str) -> str:
1119
  full_text_query = ""
1120
  words = [el for el in remove_lucene_chars(input).split() if el]
 
1123
  full_text_query += f" {words[-1]}~2"
1124
  return full_text_query.strip()
1125
 
 
 
1126
  def structured_retriever(question: str) -> str:
1127
  result = ""
1128
  entities = entity_chain.invoke({"question": question})
 
1182
  RunnableLambda(lambda x : x["question"]),
1183
  )
1184
 
1185
+ # template = """Answer the question based only on the following context:
1186
  # {context}
1187
  # Question: {question}
1188
  # Use natural language and be concise.
1189
  # Answer:"""
1190
 
1191
+ 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. Given today's sunny and bright weather on __, 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.
 
1192
  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.
 
1193
  "It was my pleasure!"
1194
  {{context}}
1195
  Question: {{question}}
1196
  Helpful Answer: __"""
1197
 
1198
+ qa_prompt = ChatPromptTemplate.from_template(template)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1199
 
1200
  chain_neo4j = (
1201
  RunnableParallel(
 
1210
  )
1211
 
1212
  # Define a function to select between Pinecone and Neo4j
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1213
  def generate_answer(message, choice, retrieval_mode):
1214
  logging.debug(f"generate_answer called with choice: {choice} and retrieval_mode: {retrieval_mode}")
1215
 
 
1226
  logging.debug(f"Vector response: {response}")
1227
  return response['result'], extract_addresses(response['result'])
1228
  elif retrieval_mode == "Knowledge-Graph":
1229
+ response = chain_neo4j.invoke({"question": message})
 
 
 
1230
  logging.debug(f"Knowledge-Graph response: {response}")
1231
+ return response, extract_addresses(response)
1232
  else:
1233
  return "Invalid retrieval mode selected.", []
1234
 
 
1235
  def bot(history, choice, tts_choice, retrieval_mode):
1236
  if not history:
1237
  return history
 
1429
  if choice in ["Details", "Conversational"]:
1430
  return gr.update(visible=True), update_map_with_response(history)
1431
  else:
1432
+ return gr.update(visible(False), "")
1433
 
1434
  def generate_audio_elevenlabs(text):
1435
  XI_API_KEY = os.environ['ELEVENLABS_API']
 
1779
 
1780
  # Define the sequence of actions for the "Retriever" button
1781
  retriever_sequence = (
1782
+ retriever_button.click(fn=stop_audio, inputs=[], outputs=[audio_output],api_name="Ask_Retriever")
1783
+ .then(fn=add_message, inputs=[chatbot, chat_input], outputs=[chatbot, chat_input],api_name="voice_query")
1784
+ .then(fn=bot, inputs=[chatbot, choice, tts_choice, retrieval_mode], outputs=[chatbot, audio_output],api_name="generate_voice_response" )
1785
  .then(fn=show_map_if_details, inputs=[chatbot, choice], outputs=[location_output, location_output], api_name="map_finder")
1786
  .then(fn=clear_textbox, inputs=[], outputs=[chat_input])
1787
  )
1788
 
1789
  # Link the "Enter" key (submit event) to the same sequence of actions
1790
  chat_input.submit(fn=stop_audio, inputs=[], outputs=[audio_output])
1791
+ chat_input.submit(fn=add_message, inputs=[chatbot, chat_input], outputs=[chatbot, chat_input],api_name="voice_query").then(
1792
  fn=bot, inputs=[chatbot, choice, tts_choice, retrieval_mode], outputs=[chatbot, audio_output], api_name="generate_voice_response"
1793
  ).then(
1794
  fn=show_map_if_details, inputs=[chatbot, choice], outputs=[location_output, location_output], api_name="map_finder"
 
1799
  audio_input = gr.Audio(sources=["microphone"], streaming=True, type='numpy', every=0.1)
1800
  audio_input.stream(transcribe_function, inputs=[state, audio_input], outputs=[state, chat_input], api_name="voice_query_to_text")
1801
 
 
 
 
 
 
 
 
1802
  with gr.Column():
1803
  image_output_1 = gr.Image(value=generate_image(hardcoded_prompt_1), width=400, height=400)
1804
  image_output_2 = gr.Image(value=generate_image(hardcoded_prompt_2), width=400, height=400)
 
1813
 
1814
 
1815
 
1816
+