File size: 5,578 Bytes
7ad1106
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86572ce
7ad1106
 
 
 
 
 
fbe12a1
 
7ad1106
 
 
 
 
 
 
 
f7aa67f
 
 
 
7ad1106
 
97f3692
7ad1106
97f3692
7ad1106
97f3692
7ad1106
97f3692
7ad1106
97f3692
 
7ad1106
97f3692
 
 
 
79050de
97f3692
79050de
 
 
 
97f3692
67f4d3b
 
97f3692
67f4d3b
178a93a
97f3692
67f4d3b
6d6d6e1
7ad1106
97f3692
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cddfb37
 
 
 
 
97f3692
cddfb37
 
 
 
97f3692
cddfb37
 
 
97f3692
cddfb37
 
97f3692
cddfb37
 
97f3692
cddfb37
 
97f3692
cddfb37
 
97f3692
cddfb37
 
97f3692
cddfb37
 
7ad1106
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
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()