Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,95 +1,59 @@
|
|
|
|
1 |
import streamlit as st
|
2 |
-
from responser import responsr
|
3 |
from gtts import gTTS
|
4 |
from io import BytesIO
|
5 |
-
import
|
6 |
-
|
7 |
-
import
|
8 |
|
9 |
-
#
|
10 |
-
whisper_model = whisper.load_model("base")
|
11 |
-
|
12 |
-
# Function to convert text to speech and return audio file
|
13 |
def text_to_speech(text):
|
|
|
14 |
tts = gTTS(text)
|
|
|
15 |
audio_file = BytesIO()
|
16 |
tts.write_to_fp(audio_file)
|
17 |
audio_file.seek(0)
|
|
|
18 |
return audio_file
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
audio_data = sd.rec(int(duration * fs), samplerate=fs, channels=1, dtype='int16')
|
24 |
-
sd.wait() # Wait until recording is finished
|
25 |
-
st.write("Recording complete.")
|
26 |
-
return audio_data.flatten()
|
27 |
-
|
28 |
-
# Function to recognize speech using Whisper
|
29 |
-
def recognize_speech(audio_data):
|
30 |
-
# Assuming audio_data is a numpy array of int16
|
31 |
-
audio_data = audio_data.astype(np.float32) / 32768.0
|
32 |
-
result = whisper_model.transcribe(audio_data)
|
33 |
-
return result['text']
|
34 |
|
35 |
def main():
|
36 |
-
|
37 |
-
col1, col2, col3 = st.columns([3, 1, 1])
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
st.
|
42 |
-
|
43 |
-
#
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
46 |
|
47 |
-
#
|
48 |
-
|
49 |
-
with col1:
|
50 |
-
if message["role"] == "assistant":
|
51 |
-
st.markdown(f"**Assistant:** {message['content']}")
|
52 |
-
st.audio(message["audio"], format="audio/mp3")
|
53 |
-
else:
|
54 |
-
st.markdown(f"**User:** {message['content']}")
|
55 |
-
st.audio(message["audio"], format="audio/mp3")
|
56 |
|
57 |
-
|
58 |
-
if st.button('Record Audio'):
|
59 |
-
audio_data = record_audio(duration=5) # Adjust duration as needed
|
60 |
-
st.write("Processing...")
|
61 |
-
user_input = recognize_speech(audio_data)
|
62 |
-
|
63 |
if user_input:
|
64 |
-
#
|
65 |
-
|
66 |
-
|
67 |
-
# Add user message (as audio) to chat history
|
68 |
-
st.session_state.chat_messages.append({
|
69 |
-
"role": "user",
|
70 |
-
"content": user_input,
|
71 |
-
"audio": user_audio.getvalue()
|
72 |
-
})
|
73 |
-
|
74 |
-
# Get AI response using responsr function
|
75 |
-
response = responsr(user_input)
|
76 |
|
|
|
|
|
|
|
77 |
# Convert AI response to speech
|
78 |
-
|
79 |
|
80 |
-
#
|
81 |
-
st.
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
})
|
86 |
-
|
87 |
-
# Display the audio files for both user input and AI response
|
88 |
-
with col1:
|
89 |
-
st.markdown(f"**User:** {user_input}")
|
90 |
-
st.audio(user_audio, format="audio/mp3")
|
91 |
-
st.markdown(f"**Assistant:** {response}")
|
92 |
-
st.audio(response_audio, format="audio/mp3")
|
93 |
|
94 |
if __name__ == "__main__":
|
95 |
main()
|
|
|
1 |
+
import os
|
2 |
import streamlit as st
|
|
|
3 |
from gtts import gTTS
|
4 |
from io import BytesIO
|
5 |
+
from pydub import AudioSegment
|
6 |
+
from pydub.playback import play
|
7 |
+
import base64
|
8 |
|
9 |
+
# Function to convert text to speech and play it
|
|
|
|
|
|
|
10 |
def text_to_speech(text):
|
11 |
+
# Convert text to speech
|
12 |
tts = gTTS(text)
|
13 |
+
# Save to a BytesIO object
|
14 |
audio_file = BytesIO()
|
15 |
tts.write_to_fp(audio_file)
|
16 |
audio_file.seek(0)
|
17 |
+
|
18 |
return audio_file
|
19 |
|
20 |
+
def play_audio(audio_file):
|
21 |
+
audio_segment = AudioSegment.from_file(audio_file, format="mp3")
|
22 |
+
play(audio_segment)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
def main():
|
25 |
+
st.title("Real-Time Audio Chat with AI")
|
|
|
26 |
|
27 |
+
# Initialize chat history
|
28 |
+
if "chat_history" not in st.session_state:
|
29 |
+
st.session_state.chat_history = []
|
30 |
+
|
31 |
+
# Display chat history
|
32 |
+
for message in st.session_state.chat_history:
|
33 |
+
if message["role"] == "user":
|
34 |
+
st.text_area("User:", message["content"], height=40, key=message["content"], disabled=True)
|
35 |
+
else:
|
36 |
+
st.text_area("AI:", message["content"], height=40, key=message["content"], disabled=True)
|
37 |
|
38 |
+
# User input
|
39 |
+
user_input = st.text_input("Type your message here:")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
+
if st.button("Send"):
|
|
|
|
|
|
|
|
|
|
|
42 |
if user_input:
|
43 |
+
# Display user's message
|
44 |
+
st.session_state.chat_history.append({"role": "user", "content": user_input})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
+
# Simulated AI response (replace this with actual model response)
|
47 |
+
ai_response = f"You said: {user_input}"
|
48 |
+
|
49 |
# Convert AI response to speech
|
50 |
+
audio_file = text_to_speech(ai_response)
|
51 |
|
52 |
+
# Play audio in real-time
|
53 |
+
st.audio(audio_file, format="audio/mp3")
|
54 |
+
|
55 |
+
# Display AI's response
|
56 |
+
st.session_state.chat_history.append({"role": "assistant", "content": ai_response})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
if __name__ == "__main__":
|
59 |
main()
|