rumaisa1054 commited on
Commit
4732c62
·
verified ·
1 Parent(s): 03f470d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -74
app.py CHANGED
@@ -1,11 +1,7 @@
1
  import streamlit as st
2
  from responser import responsr
3
- from io import BytesIO
4
- 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
- import asyncio
9
 
10
  # Function to convert text to speech and return audio file
11
  def text_to_speech(text):
@@ -15,83 +11,54 @@ def text_to_speech(text):
15
  audio_file.seek(0)
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")
36
-
 
 
 
 
 
37
  # Initialize chat history if not already initialized
38
  if "chat_messages" not in st.session_state:
39
  st.session_state.chat_messages = []
40
 
41
- # Display chat history with audio only
42
  for message in st.session_state.chat_messages:
43
  if message["role"] == "user":
44
- st.write("User:")
45
  else:
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()
 
 
1
  import streamlit as st
2
  from responser import responsr
 
 
 
 
3
  from gtts import gTTS
4
+ from io import BytesIO
5
 
6
  # Function to convert text to speech and return audio file
7
  def text_to_speech(text):
 
11
  audio_file.seek(0)
12
  return audio_file
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  def main():
15
+ # Layout with three columns
16
+ col1, col2, col3 = st.columns([3, 1, 1])
17
+
18
+ with col1:
19
+ # Title with custom CSS styling for top margin
20
+ st.markdown('<div style="margin-top: -25px;" class="title-wrapper"><h1 style="text-align: center;">Your Health Guide</h1></div>', unsafe_allow_html=True)
21
+
22
  # Initialize chat history if not already initialized
23
  if "chat_messages" not in st.session_state:
24
  st.session_state.chat_messages = []
25
 
26
+ # Display chat history
27
  for message in st.session_state.chat_messages:
28
  if message["role"] == "user":
29
+ st.text_area("User:", message["content"], height=40, key=message["content"], disabled=True)
30
  else:
31
+ st.text_area("AI:", message["content"], height=40, key=message["content"], disabled=True)
32
+ # Display audio in chat interface
33
+ st.audio(message["audio"], format="audio/mp3")
34
+
35
+ # User input
36
+ if prompt := st.chat_input("Welcome - How can I help you?"):
37
+ # Display user's message in chat message container
38
+ with st.chat_message("user"):
39
+ st.markdown(prompt)
40
+
41
+ # Add user message to chat history
42
+ st.session_state.chat_messages.append({"role": "user", "content": prompt})
43
+
44
+ # Get AI response using responsr function
45
+ response = responsr(prompt)
46
+
47
+ # Convert AI response to speech
48
+ audio_file = text_to_speech(response)
49
+
50
+ # Display assistant's response in chat message container
51
+ with st.chat_message("assistant"):
52
+ st.markdown(response)
53
+ st.audio(audio_file, format="audio/mp3")
54
+
55
+ # Add assistant's response and audio to chat history
56
+ st.session_state.chat_messages.append({
57
+ "role": "assistant",
58
+ "content": response,
59
+ "audio": audio_file.getvalue()
60
+ })
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
  if __name__ == "__main__":
63
  main()
64
+