Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,6 +5,7 @@ import speech_recognition as sr
|
|
5 |
from streamlit_webrtc import webrtc_streamer, AudioProcessorBase, WebRtcMode
|
6 |
import numpy as np
|
7 |
from gtts import gTTS
|
|
|
8 |
|
9 |
# Function to convert text to speech and return audio file
|
10 |
def text_to_speech(text):
|
@@ -15,11 +16,20 @@ def text_to_speech(text):
|
|
15 |
return audio_file
|
16 |
|
17 |
# Function to convert speech to text using SpeechRecognition
|
18 |
-
def speech_to_text(
|
19 |
recognizer = sr.Recognizer()
|
20 |
-
with sr.AudioFile(
|
21 |
-
|
22 |
-
return recognizer.recognize_google(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
def main():
|
25 |
st.title("Real-Time Audio Chat with AI")
|
@@ -36,42 +46,52 @@ def main():
|
|
36 |
st.write("AI:")
|
37 |
st.audio(message["audio"], format="audio/mp3")
|
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 |
if __name__ == "__main__":
|
77 |
main()
|
|
|
5 |
from streamlit_webrtc import webrtc_streamer, AudioProcessorBase, WebRtcMode
|
6 |
import numpy as np
|
7 |
from gtts import gTTS
|
8 |
+
import asyncio
|
9 |
|
10 |
# Function to convert text to speech and return audio file
|
11 |
def text_to_speech(text):
|
|
|
16 |
return audio_file
|
17 |
|
18 |
# Function to convert speech to text using SpeechRecognition
|
19 |
+
def speech_to_text(audio_data):
|
20 |
recognizer = sr.Recognizer()
|
21 |
+
with sr.AudioFile(BytesIO(audio_data)) as source:
|
22 |
+
audio = recognizer.record(source)
|
23 |
+
return recognizer.recognize_google(audio)
|
24 |
+
|
25 |
+
class AudioProcessor(AudioProcessorBase):
|
26 |
+
def __init__(self):
|
27 |
+
self.audio_buffer = BytesIO()
|
28 |
+
|
29 |
+
def recv(self, frame):
|
30 |
+
audio_data = frame.to_ndarray().tobytes()
|
31 |
+
self.audio_buffer.write(audio_data)
|
32 |
+
return frame
|
33 |
|
34 |
def main():
|
35 |
st.title("Real-Time Audio Chat with AI")
|
|
|
46 |
st.write("AI:")
|
47 |
st.audio(message["audio"], format="audio/mp3")
|
48 |
|
49 |
+
# Initialize WebRTC audio streamer
|
50 |
+
webrtc_ctx = webrtc_streamer(
|
51 |
+
key="speech-to-text",
|
52 |
+
mode=WebRtcMode.SENDRECV,
|
53 |
+
audio_processor_factory=AudioProcessor,
|
54 |
+
rtc_configuration={"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]},
|
55 |
+
media_stream_constraints={"audio": True, "video": False},
|
56 |
+
async_processing=True,
|
57 |
+
)
|
58 |
+
|
59 |
+
if webrtc_ctx.state.playing:
|
60 |
+
processor = webrtc_ctx.audio_processor
|
61 |
+
if processor and processor.audio_buffer.getvalue():
|
62 |
+
# Convert audio buffer to speech
|
63 |
+
audio_data = processor.audio_buffer.getvalue()
|
64 |
+
user_text = speech_to_text(audio_data)
|
65 |
+
|
66 |
+
# Convert the user's speech input to audio for playback
|
67 |
+
user_audio = text_to_speech(user_text)
|
68 |
+
|
69 |
+
# Add user message (as audio) to chat history
|
70 |
+
st.session_state.chat_messages.append({
|
71 |
+
"role": "user",
|
72 |
+
"content": user_text,
|
73 |
+
"audio": user_audio.getvalue()
|
74 |
+
})
|
75 |
+
|
76 |
+
# Get AI response using the responsr function
|
77 |
+
response = responsr(user_text)
|
78 |
+
|
79 |
+
# Convert AI response to speech
|
80 |
+
response_audio = text_to_speech(response)
|
81 |
+
|
82 |
+
# Add assistant's response (as audio) to chat history
|
83 |
+
st.session_state.chat_messages.append({
|
84 |
+
"role": "assistant",
|
85 |
+
"content": response,
|
86 |
+
"audio": response_audio.getvalue()
|
87 |
+
})
|
88 |
+
|
89 |
+
# Display the audio files for both user input and AI response
|
90 |
+
st.audio(user_audio, format="audio/mp3")
|
91 |
+
st.audio(response_audio, format="audio/mp3")
|
92 |
+
|
93 |
+
# Clear the audio buffer
|
94 |
+
processor.audio_buffer = BytesIO()
|
95 |
|
96 |
if __name__ == "__main__":
|
97 |
main()
|