File size: 1,317 Bytes
86b946a
 
43773a4
bfaeb19
 
 
d06f4eb
2dfe1eb
1e3c563
2dfe1eb
 
 
 
 
 
 
 
1e3c563
2dfe1eb
1e3c563
d06f4eb
883b37e
f845a0a
883b37e
1e3c563
883b37e
a197dc7
883b37e
1e3c563
 
 
883b37e
 
e0d541d
883b37e
3405778
1e3c563
 
 
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
import streamlit as st
from gradio_client import Client
from st_audiorec import st_audiorec
from gtts import gTTS
import os

# ... (previous code remains unchanged)

# Function to convert text to speech
def text_to_speech(text, language='en', filename='output.mp3'):
    # Create a gTTS object
    tts = gTTS(text=text, lang=language, slow=False)

    # Save the audio file
    tts.save(filename)

    # Play the audio file
    os.system(f'start {filename}')  # This works on Windows. For other OS, you might need a different command.

# ... (previous code remains unchanged)

# React to user input
if prompt := textinput:
    # Display user message in chat message container
    st.chat_message("human", avatar="💬: ").markdown(prompt)
    # Add user message to chat history
    st.session_state.messages.append({"role": "human", "content": prompt})

    # Update the global response variable
    response = predict(message=prompt)

    # Display assistant response in chat message container
    with st.chat_message("assistant", avatar='🦙'):
        st.markdown(response)
    # Add assistant response to chat history
    st.session_state.messages.append({"role": "assistant", "content": response})

    # Convert response to audio
    text_to_speech(response)  # Call text_to_speech after getting the response