Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
|
3 |
+
import pyaudio
|
4 |
+
import streamlit as st
|
5 |
+
from langchain.memory import ConversationBufferMemory
|
6 |
+
|
7 |
+
from utils import record_audio_chunk, transcribe_audio, get_response_llm, play_text_to_speech, load_whisper
|
8 |
+
|
9 |
+
chunk_file = 'temp_audio_chunk.wav'
|
10 |
+
model = load_whisper()
|
11 |
+
def main():
|
12 |
+
st.markdown('<h1 style="color: darkblue;">AI Voice Assistant️</h1>', unsafe_allow_html=True)
|
13 |
+
|
14 |
+
memory = ConversationBufferMemory(memory_key="chat_history")
|
15 |
+
|
16 |
+
if st.button("Start Recording"):
|
17 |
+
while True:
|
18 |
+
# Audio Stream Initialization
|
19 |
+
audio = pyaudio.PyAudio()
|
20 |
+
stream = audio.open(format=pyaudio.paInt16, channels=1, rate=16000, input=True, frames_per_buffer=1024)
|
21 |
+
|
22 |
+
# Record and save audio chunk
|
23 |
+
record_audio_chunk(audio, stream)
|
24 |
+
|
25 |
+
text = transcribe_audio(model, chunk_file)
|
26 |
+
|
27 |
+
if text is not None:
|
28 |
+
st.markdown(
|
29 |
+
f'<div style="background-color: #f0f0f0; padding: 10px; border-radius: 5px;">Customer 👤: {text}</div>',
|
30 |
+
unsafe_allow_html=True)
|
31 |
+
|
32 |
+
os.remove(chunk_file)
|
33 |
+
|
34 |
+
response_llm = get_response_llm(user_question=text, memory=memory)
|
35 |
+
st.markdown(
|
36 |
+
f'<div style="background-color: #f0f0f0; padding: 10px; border-radius: 5px;">AI Assistant 🤖: {response_llm}</div>',
|
37 |
+
unsafe_allow_html=True)
|
38 |
+
|
39 |
+
play_text_to_speech(text=response_llm)
|
40 |
+
else:
|
41 |
+
stream.stop_stream()
|
42 |
+
stream.close()
|
43 |
+
audio.terminate()
|
44 |
+
break # Exit the while loop
|
45 |
+
print("End Conversation")
|
46 |
+
|
47 |
+
|
48 |
+
|
49 |
+
if __name__ == "__main__":
|
50 |
+
main()
|