Spaces:
Sleeping
Sleeping
File size: 2,251 Bytes
dc86cbf 5fb9ab8 c873095 33cd6d1 c873095 d616fde c873095 4da441b 4732c62 c873095 c916659 c873095 c916659 c873095 33cd6d1 c916659 c873095 c916659 c873095 a569e38 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 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 |
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('<div style="margin-top: -5px;" class="title-wrapper"><h1 style="text-align: center;">Your English Speaking Guide</h1></div>', 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()
|