I’m working on an application which will have an automatic text-to-speech feature. I have a piece of text which I’ve converted to speech using google text-to-speech library. I’m aware that we can generate an audio file of the same using st.audio() but here I want the audio to play automatically after it is generated. (without the user having to click any button anywhere).
I’m a bit late but thanks a lot for the solution. It worked for me perfectly!
One problem I’m facing is that whenever the text to be converted to speech changes, the speech output isnt changing
automatically. I’ve to refresh the page by some way to play the audio file again. Is there a way to resolve that? That would be really helpful!
Hi @ayanatherate, could you share a code snippet that shows this issue? Are you using st.experimental_memo to cache the autoplay_audio function, perhaps?
I think what you probably need is the “callback”, which is called when something changes.
This is a callback demo I wrote using langchain. when you get a stream answer from ChatGPT, the callback is called once for each token. when the answer accumulates to one sentence, I send azure’s text to speech to generate speech and play it.
I haven’t tried google text-to-speech, I think you can set an onchange in the text_input of the user input, and then call the callback.
Another trick here is to use html to include the base64 of the audio so that it can be played with st.markdown. Maybe this is what you need to autoplay audio
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.