Prathamesh1420 commited on
Commit
20d640d
·
verified ·
1 Parent(s): a7a28b1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -49
app.py CHANGED
@@ -1,50 +1,43 @@
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()
 
 
 
 
1
  import streamlit as st
2
+ import asyncio
3
+ import websockets
4
+
5
+ st.markdown('<h1 style="color: darkblue;">AI Voice Assistant</h1>', unsafe_allow_html=True)
6
+
7
+ # JavaScript for real-time voice streaming
8
+ audio_recorder_js = """
9
+ <script>
10
+ let mediaRecorder;
11
+ let ws;
12
+
13
+ function startRecording() {
14
+ navigator.mediaDevices.getUserMedia({ audio: true }).then(stream => {
15
+ ws = new WebSocket("ws://localhost:8765"); // Replace with your server's WebSocket URL
16
+ mediaRecorder = new MediaRecorder(stream);
17
+ mediaRecorder.start();
18
+
19
+ mediaRecorder.ondataavailable = event => {
20
+ ws.send(event.data);
21
+ };
22
+
23
+ ws.onmessage = function(event) {
24
+ document.getElementById("response").innerHTML += "<br><b>AI:</b> " + event.data;
25
+ };
26
+ });
27
+ }
28
+
29
+ function stopRecording() {
30
+ mediaRecorder.stop();
31
+ ws.close();
32
+ }
33
+ </script>
34
+ """
35
+
36
+ # Display buttons for real-time recording
37
+ st.components.v1.html(
38
+ audio_recorder_js + """
39
+ <button onclick="startRecording()">🎤 Start Talking</button>
40
+ <button onclick="stopRecording()">🛑 Stop</button>
41
+ <div id="response" style="margin-top: 10px; padding: 10px; border: 1px solid #ccc;"></div>
42
+ """, height=200
43
+ )