Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -45,6 +45,17 @@ from langchain_core.output_parsers import StrOutputParser
|
|
| 45 |
from langchain_core.runnables import RunnableBranch, RunnableLambda, RunnableParallel, RunnablePassthrough
|
| 46 |
from serpapi.google_search import GoogleSearch
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
#API AutoDate Fix Up
|
| 49 |
def get_current_date1():
|
| 50 |
return datetime.now().strftime("%Y-%m-%d")
|
|
@@ -75,7 +86,7 @@ embeddings = OpenAIEmbeddings(api_key=os.environ['OPENAI_API_KEY'])
|
|
| 75 |
from pinecone import Pinecone
|
| 76 |
pc = Pinecone(api_key=os.environ['PINECONE_API_KEY'])
|
| 77 |
|
| 78 |
-
index_name = "
|
| 79 |
vectorstore = PineconeVectorStore(index_name=index_name, embedding=embeddings)
|
| 80 |
retriever = vectorstore.as_retriever(search_kwargs={'k': 5})
|
| 81 |
|
|
@@ -133,9 +144,9 @@ QA_CHAIN_PROMPT_1 = PromptTemplate(input_variables=["context", "question"], temp
|
|
| 133 |
QA_CHAIN_PROMPT_2 = PromptTemplate(input_variables=["context", "question"], template=template2)
|
| 134 |
|
| 135 |
# Neo4j setup
|
| 136 |
-
graph = Neo4jGraph(url="neo4j+s://
|
| 137 |
username="neo4j",
|
| 138 |
-
password="
|
| 139 |
)
|
| 140 |
# Avoid pushing the graph documents to Neo4j every time
|
| 141 |
# Only push the documents once and comment the code below after the initial push
|
|
@@ -335,7 +346,7 @@ def bot(history, choice, tts_choice, retrieval_mode):
|
|
| 335 |
|
| 336 |
def add_message(history, message):
|
| 337 |
history.append((message, None))
|
| 338 |
-
return history, gr.Textbox(value="", interactive=True, placeholder="Ask More
|
| 339 |
|
| 340 |
def print_like_dislike(x: gr.LikeData):
|
| 341 |
print(x.index, x.value, x.liked)
|
|
@@ -573,14 +584,13 @@ def generate_audio_elevenlabs(text):
|
|
| 573 |
return None
|
| 574 |
|
| 575 |
|
| 576 |
-
repo_id = "parler-tts/parler-tts-mini-
|
| 577 |
|
| 578 |
parler_model = ParlerTTSForConditionalGeneration.from_pretrained(repo_id).to(device)
|
| 579 |
parler_tokenizer = AutoTokenizer.from_pretrained(repo_id)
|
| 580 |
parler_feature_extractor = AutoFeatureExtractor.from_pretrained(repo_id)
|
| 581 |
|
| 582 |
SAMPLE_RATE = parler_feature_extractor.sampling_rate
|
| 583 |
-
SEED = 42
|
| 584 |
|
| 585 |
def preprocess(text):
|
| 586 |
number_normalizer = EnglishNumberNormalizer()
|
|
@@ -597,7 +607,7 @@ def preprocess(text):
|
|
| 597 |
abbreviations = re.findall(abbreviations_pattern, text)
|
| 598 |
for abv in abbreviations:
|
| 599 |
if abv in text:
|
| 600 |
-
text
|
| 601 |
return text
|
| 602 |
|
| 603 |
def chunk_text(text, max_length=250):
|
|
@@ -621,20 +631,19 @@ def chunk_text(text, max_length=250):
|
|
| 621 |
return chunks
|
| 622 |
|
| 623 |
def generate_audio_parler_tts(text):
|
| 624 |
-
description = "
|
| 625 |
chunks = chunk_text(preprocess(text))
|
| 626 |
audio_segments = []
|
| 627 |
|
| 628 |
for chunk in chunks:
|
| 629 |
-
|
| 630 |
-
|
| 631 |
|
| 632 |
-
|
| 633 |
-
generation = parler_model.generate(input_ids=inputs.input_ids, prompt_input_ids=prompt.input_ids)
|
| 634 |
audio_arr = generation.cpu().numpy().squeeze()
|
| 635 |
|
| 636 |
temp_audio_path = os.path.join(tempfile.gettempdir(), f"parler_tts_audio_{len(audio_segments)}.wav")
|
| 637 |
-
|
| 638 |
audio_segments.append(AudioSegment.from_wav(temp_audio_path))
|
| 639 |
|
| 640 |
combined_audio = sum(audio_segments)
|
|
@@ -643,6 +652,7 @@ def generate_audio_parler_tts(text):
|
|
| 643 |
|
| 644 |
logging.debug(f"Audio saved to {combined_audio_path}")
|
| 645 |
return combined_audio_path
|
|
|
|
| 646 |
|
| 647 |
# Load the MARS5 model
|
| 648 |
mars5, config_class = torch.hub.load('Camb-ai/mars5-tts', 'mars5_english', trust_repo=True)
|
|
@@ -1055,7 +1065,7 @@ with gr.Blocks(theme='Pijush2023/scikit-learn-pijush') as demo:
|
|
| 1055 |
|
| 1056 |
gr.Markdown("<h1 style='color: red;'>Talk to RADAR</h1>", elem_id="voice-markdown")
|
| 1057 |
|
| 1058 |
-
chat_input = gr.Textbox(show_copy_button=True, interactive=True, show_label=False, label="ASK Radar !!!",placeholder="Hey Radar...!!")
|
| 1059 |
tts_choice = gr.Radio(label="Select TTS System", choices=["Alpha", "Beta", "Gamma"], value="Alpha")
|
| 1060 |
retriever_button = gr.Button("Retriever")
|
| 1061 |
|
|
@@ -1121,4 +1131,3 @@ demo.launch(share=True)
|
|
| 1121 |
|
| 1122 |
|
| 1123 |
|
| 1124 |
-
|
|
|
|
| 45 |
from langchain_core.runnables import RunnableBranch, RunnableLambda, RunnableParallel, RunnablePassthrough
|
| 46 |
from serpapi.google_search import GoogleSearch
|
| 47 |
|
| 48 |
+
#Parler TTS v1 Modules
|
| 49 |
+
|
| 50 |
+
import os
|
| 51 |
+
import re
|
| 52 |
+
import tempfile
|
| 53 |
+
import soundfile as sf
|
| 54 |
+
from string import punctuation
|
| 55 |
+
from pydub import AudioSegment
|
| 56 |
+
from transformers import AutoTokenizer, AutoFeatureExtractor
|
| 57 |
+
|
| 58 |
+
|
| 59 |
#API AutoDate Fix Up
|
| 60 |
def get_current_date1():
|
| 61 |
return datetime.now().strftime("%Y-%m-%d")
|
|
|
|
| 86 |
from pinecone import Pinecone
|
| 87 |
pc = Pinecone(api_key=os.environ['PINECONE_API_KEY'])
|
| 88 |
|
| 89 |
+
index_name = "radardata08172024"
|
| 90 |
vectorstore = PineconeVectorStore(index_name=index_name, embedding=embeddings)
|
| 91 |
retriever = vectorstore.as_retriever(search_kwargs={'k': 5})
|
| 92 |
|
|
|
|
| 144 |
QA_CHAIN_PROMPT_2 = PromptTemplate(input_variables=["context", "question"], template=template2)
|
| 145 |
|
| 146 |
# Neo4j setup
|
| 147 |
+
graph = Neo4jGraph(url="neo4j+s://6457770f.databases.neo4j.io",
|
| 148 |
username="neo4j",
|
| 149 |
+
password="Z10duoPkKCtENuOukw3eIlvl0xJWKtrVSr-_hGX1LQ4"
|
| 150 |
)
|
| 151 |
# Avoid pushing the graph documents to Neo4j every time
|
| 152 |
# Only push the documents once and comment the code below after the initial push
|
|
|
|
| 346 |
|
| 347 |
def add_message(history, message):
|
| 348 |
history.append((message, None))
|
| 349 |
+
return history, gr.Textbox(value="", interactive=True, placeholder="Ask More Questions...", show_label=False)
|
| 350 |
|
| 351 |
def print_like_dislike(x: gr.LikeData):
|
| 352 |
print(x.index, x.value, x.liked)
|
|
|
|
| 584 |
return None
|
| 585 |
|
| 586 |
|
| 587 |
+
repo_id = "parler-tts/parler-tts-mini-v1"
|
| 588 |
|
| 589 |
parler_model = ParlerTTSForConditionalGeneration.from_pretrained(repo_id).to(device)
|
| 590 |
parler_tokenizer = AutoTokenizer.from_pretrained(repo_id)
|
| 591 |
parler_feature_extractor = AutoFeatureExtractor.from_pretrained(repo_id)
|
| 592 |
|
| 593 |
SAMPLE_RATE = parler_feature_extractor.sampling_rate
|
|
|
|
| 594 |
|
| 595 |
def preprocess(text):
|
| 596 |
number_normalizer = EnglishNumberNormalizer()
|
|
|
|
| 607 |
abbreviations = re.findall(abbreviations_pattern, text)
|
| 608 |
for abv in abbreviations:
|
| 609 |
if abv in text:
|
| 610 |
+
text = text.replace(abv, separate_abb(abv))
|
| 611 |
return text
|
| 612 |
|
| 613 |
def chunk_text(text, max_length=250):
|
|
|
|
| 631 |
return chunks
|
| 632 |
|
| 633 |
def generate_audio_parler_tts(text):
|
| 634 |
+
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."
|
| 635 |
chunks = chunk_text(preprocess(text))
|
| 636 |
audio_segments = []
|
| 637 |
|
| 638 |
for chunk in chunks:
|
| 639 |
+
input_ids = parler_tokenizer(description, return_tensors="pt").input_ids.to(device)
|
| 640 |
+
prompt_input_ids = parler_tokenizer(chunk, return_tensors="pt").input_ids.to(device)
|
| 641 |
|
| 642 |
+
generation = parler_model.generate(input_ids=input_ids, prompt_input_ids=prompt_input_ids)
|
|
|
|
| 643 |
audio_arr = generation.cpu().numpy().squeeze()
|
| 644 |
|
| 645 |
temp_audio_path = os.path.join(tempfile.gettempdir(), f"parler_tts_audio_{len(audio_segments)}.wav")
|
| 646 |
+
sf.write(temp_audio_path, audio_arr, parler_model.config.sampling_rate)
|
| 647 |
audio_segments.append(AudioSegment.from_wav(temp_audio_path))
|
| 648 |
|
| 649 |
combined_audio = sum(audio_segments)
|
|
|
|
| 652 |
|
| 653 |
logging.debug(f"Audio saved to {combined_audio_path}")
|
| 654 |
return combined_audio_path
|
| 655 |
+
|
| 656 |
|
| 657 |
# Load the MARS5 model
|
| 658 |
mars5, config_class = torch.hub.load('Camb-ai/mars5-tts', 'mars5_english', trust_repo=True)
|
|
|
|
| 1065 |
|
| 1066 |
gr.Markdown("<h1 style='color: red;'>Talk to RADAR</h1>", elem_id="voice-markdown")
|
| 1067 |
|
| 1068 |
+
chat_input = gr.Textbox(show_copy_button=True, interactive=True, show_label=False, label="ASK Radar !!!", placeholder="Hey Radar...!!")
|
| 1069 |
tts_choice = gr.Radio(label="Select TTS System", choices=["Alpha", "Beta", "Gamma"], value="Alpha")
|
| 1070 |
retriever_button = gr.Button("Retriever")
|
| 1071 |
|
|
|
|
| 1131 |
|
| 1132 |
|
| 1133 |
|
|
|