rumaisa1054 commited on
Commit
c873095
·
verified ·
1 Parent(s): c916659

Update app.py

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