Audio_Chatbot / app.py
Vageesh1's picture
Update app.py
cddfb37
raw
history blame
5.58 kB
import streamlit as st
from audiorecorder import audiorecorder
import torch
from transformers import pipeline
import torch
import torchaudio
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain import HuggingFaceHub, LLMChain, PromptTemplate
from langchain.memory import ConversationBufferWindowMemory
from langchain.chat_models import ChatOpenAI
from langchain.chains import ConversationalRetrievalChain
from langchain.document_loaders.csv_loader import CSVLoader
from langchain.vectorstores import FAISS
import tempfile
from streamlit_chat import message
import streamlit as st
from elevenlabs import set_api_key
from elevenlabs import clone, generate, play
from pydub import AudioSegment
import os
import re
import sys
import pandas as pd
import librosa
from helper import parse_transcription,hindi_to_english,translate_english_to_hindi,hindi_tts
def extract_text_from_html(html):
cleanr = re.compile('<.*?>')
cleantext = re.sub(cleanr, '', html)
def conversational_chat(chain,query):
result = chain({"question": query,
"chat_history": st.session_state['history']})
st.session_state['history'].append((query, result["answer"]))
return result["answer"]
def save_uploaded_file_as_mp3(uploaded_file, output_file_path):
audio = AudioSegment.from_file(uploaded_file)
audio.export(output_file_path, format="mp3")
user_api_key = st.sidebar.text_input(
label="#### Your OpenAI API key πŸ‘‡",
placeholder="Paste your openAI API key, sk-",
type="password")
def ui():
if user_api_key is not None and user_api_key.strip() != "":
os.environ["OPENAI_API_KEY"] = user_api_key
template = """
Your custom prompt
{history}
Me: Behave like a Telecomm customer service call agent and don't include any website address, company name, or any other parameter in your output {human_input}
Jack:
"""
prompt = PromptTemplate(
input_variables=["history", "human_input"],
template=template
)
llm_chain = LLMChain(
llm=ChatOpenAI(temperature=0.0, model_name='gpt-3.5-turbo'),
prompt=prompt,
verbose=True,
memory=ConversationBufferWindowMemory(k=2)
)
if 'history' not in st.session_state:
st.session_state['history'] = []
if 'generated' not in st.session_state:
st.session_state['generated'] = []
if 'past' not in st.session_state:
st.session_state['past'] = []
eleven_labs_api_key = st.sidebar.text_input(
label="Your Eleven Labs API key πŸ‘‡",
placeholder="Paste your Eleven Labs API key",
type="password")
set_api_key(user_api_key)
audio_file = st.file_uploader("Upload an audio file", type=["wav", "mp4", "mp3"])
if audio_file is not None:
output_file_path = "./output_audio.mp3"
save_uploaded_file_as_mp3(audio_file, output_file_path)
hindi_input_audio, sample_rate = librosa.load(output_file_path, sr=None, mono=True)
# Applying audio recognition
hindi_transcription = parse_transcription('./output_audio.mp3')
st.success(f"Audio file saved as {output_file_path}")
# Convert Hindi to English
english_input = hindi_to_english(hindi_transcription)
# Feeding the input to the LLM
english_output = conversational_chat(llm_chain, english_input)
# Convert English to Hindi
hin_output = translate_english_to_hindi(english_output)
# Getting the Hindi TTS
hindi_output_audio = hindi_tts(hin_output)
# Show original uploaded audio
st.audio(audio_file, format='audio/mp3')
# Show processed output audio
st.audio(hindi_output_audio, format='audio/mp3')
# st.markdown("---")
# # Add a new audio uploader for users to upload another audio file
# with st.form(key='my_form', clear_on_submit=True):
# audio_file_new = st.file_uploader("Upload another audio file", type=["wav", "mp4", "mp3"])
# submit_button = st.form_submit_button(label='Process and Play')
# if audio_file_new is not None and submit_button:
# output_file_path_new = "./output_audio_new.mp3"
# save_uploaded_file_as_mp3(audio_file_new, output_file_path_new)
# hindi_input_audio_new, sample_rate_new = librosa.load(output_file_path_new, sr=None, mono=True)
# # Applying audio recognition for the new file
# hindi_transcription_new = parse_transcription(output_file_path_new)
# st.success(f"Audio file saved as {output_file_path_new}")
# # Convert Hindi to English for the new file
# english_input_new = hindi_to_english(hindi_transcription_new)
# # Feeding the input to the LLM for the new file
# english_output_new = conversational_chat(llm_chain, english_input_new)
# # Convert English to Hindi for the new file
# hin_output_new = translate_english_to_hindi(english_output_new)
# # Getting the Hindi TTS for the new file
# hindi_output_audio_new = hindi_tts(hin_output_new)
# # Show original uploaded audio for the new file
# st.audio(audio_file_new, format='audio/mp3')
# # Show processed output audio for the new file
# st.audio(hindi_output_audio_new, format='audio/mp3')
if __name__ == '__main__':
ui()