import streamlit as st from gtts import gTTS from io import BytesIO from responser import responsr # Function to convert text to speech and return audio file def text_to_speech(text): tts = gTTS(text) audio_file = BytesIO() tts.write_to_fp(audio_file) audio_file.seek(0) return audio_file def main(): # Layout with three columns col1, col2 = st.columns([11, 1]) with col1: # Title with custom CSS styling for top margin st.markdown('

Your English Speaking Guide

', unsafe_allow_html=True) # Initialize chat history if not already initialized if "chat_messages" not in st.session_state: st.session_state.chat_messages = [] # Display chat history for message in st.session_state.chat_messages: if message["role"] == "user": st.text_area("User:", message["content"], height=40, key=message["content"], disabled=True) else: st.text_area("AI:", message["content"], height=40, key=message["content"], disabled=True) # Display audio in chat interface st.audio(message["audio"], format="audio/mp3") # User input if prompt := st.chat_input("Welcome - How can I help you?"): # Display user's message in chat message container with st.chat_message("user"): st.markdown(prompt) # Add user message to chat history st.session_state.chat_messages.append({"role": "user", "content": prompt}) # Get AI response using responsr function response = responsr(prompt) # Convert AI response to speech audio_file = text_to_speech(response) # Display assistant's response in chat message container with st.chat_message("assistant"): st.markdown(response) st.audio(audio_file, format="audio/mp3") # Add assistant's response and audio to chat history st.session_state.chat_messages.append({ "role": "assistant", "content": response, "audio": audio_file.getvalue() }) if __name__ == "__main__": main()