I was looking simple solution where I can stream text and simultaneously play voice(text to speech) in the background.
from threading import Thread
def speech_thread(text):
"""Run TTS in a separate thread."""
engine = pyTextToSpeech()
tts_thread = Thread(target=engine.text_to_speech, args=(text,))
tts_thread.start()
print("TTS thread started")
def response_generator(response:str ):
speech_thread(response)
for word in response.split():
yield word + " "
time.sleep(0.2)
The first function speech_thread creates a secondary thread using pythonās thread() function, thread take two arguments ātargetā which is the function you want to run on that thread in my case ātext_to_speech()ā and āargsā which goes into target function which is ātextā in text_to_speech().
The second function response_generator() is streamimg function, simply takes string as input and streams it to the streamlit web app, and before the streaming loop we call our speech_thread() which generates speech along the text stream.
Right, I did something very similar, but only saw the word by word text loaded into Streamlit after the TTS was completed. I was expecting when you said āspeech_thread() which generates speech along the text streamā that the printed words and voiced words would be almost in sync.
pyttsx3 is not thread safe, hence the run and wait, so Iām not sure itās a good idea to use it in a thread in Streamlit. It also blue-screened my PC when I got it to voice a long piece of text. There are reports on the web that it does that. I found a pyttsx4 which has additional drivers, but my brief research suggested using gTTS or a Hugging Face transformer TTS model.
the simultaneous stream and speech is working for me, but yeah I am also figuring out to get it synched, One way is to adjust the sleep counter in response_generator function but require trial and error.
I was thinking write it as an python async function but read somewhere that streamlit does not support async.
will share once i have some solution.
ps : was using gtts, but it does not support much configurations so droped it
class googleTextToSpeech():
def text_to_speech(self, text:str):
'''
Function to convert text to speech using Google Text-to-Speech (gTTS) API.
The function saves the audio file as MP3 and then converts it to WAV format.
The audio file is then played using simpleaudio package.
Parameters:
text (str): The text to be converted to speech.
Returns:
None
'''
# Convert the text to speech
tts = gTTS(text=text, lang='en', speed=1.5)
audio_data = BytesIO()
tts.write_to_fp(audio_data)
audio_data.seek(0)
# Convert MP3 file to WAV format
audio = AudioSegment.from_file(audio_data, format="mp3")
play(audio)
# return audio_data
return
Thanks. Not sure gTTS will stream the word segments, so syncing words and text, as you see in Karaoke or Spotify lyrics, will require other techniques.
Iāve done a lot of research on using asyncio with Streamlit, so might be worth writing it up. It is possible, and not too hard! Iāve built a commercial app using asyncio in Streamlit. Thereās very little info about this (that I could find) specifically for Streamlit, especially in this forum. Some small solutions exist, but with no explanations or a complete Streamlit app showing how to do it. The main ideas on how to get it to work come from the Jupyter world, so you can start there.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking āAccept allā, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.