Streamlit component providing tools to convert text to speech and play audio directly in the browser
Project description
streamlit-TTS
Streamlit component providing tools to convert text to speech and autoplay audio directly in the browser without any visible UI elements.
Features
- UI-less Audio Autoplay: Plays audio invisibly without disrupting your app's layout
- Cross-Browser Compatibility: Robust autoplay with fallback strategies for Chrome, Safari, Firefox, and mobile browsers
- Dual TTS Engines: Support for both Google TTS (gTTS) and OpenAI TTS
- Smart Text Cleanup: Automatically removes markdown, LaTeX, code snippets, and URLs before synthesis
- Type-Safe: Full type hints for better IDE support and code quality
Installation
pip install streamlit-TTS
API Reference
Five main functions are provided:
1. auto_play(audio, wait=True, lag=0.25, key=None)
Plays audio directly in the browser without showing any UI element.
Parameters:
audio(Optional[Dict]): Audio dictionary with keys'bytes','sample_rate', and'sample_width', orNonewait(bool): If True, blocks execution until audio finishes playing (default: True)lag(float): Additional wait time in seconds to account for network latency (default: 0.25)key(Optional[str]): Unique key for the component
Audio Dictionary Format:
audio = {
'bytes': bytes, # WAV audio bytes
'sample_rate': int, # Sample rate (e.g., 44100)
'sample_width': int, # Sample width in bytes (e.g., 2)
}
Example:
from streamlit_TTS import auto_play
auto_play(audio_dict, wait=True)
2. text_to_audio(text, language='en', cleanup_hook=None)
Converts text to audio using Google Text-to-Speech (gTTS).
Parameters:
text(str): The text to convert to speechlanguage(str): Language code (e.g., 'en', 'fr', 'es') (default: 'en')cleanup_hook(Optional[Callable]): Custom text cleaning function (default: built-in cleanup)
Returns: Audio dictionary or None if text is empty
Example:
from streamlit_TTS import text_to_audio
audio = text_to_audio("Hello world!", language='en')
3. text_to_speech(text, language='en', wait=True, lag=0.25, key=None)
Convenience function combining text_to_audio() and auto_play() using Google TTS.
Parameters:
text(str): The text to convert to speechlanguage(str): Language code (default: 'en')wait(bool): If True, blocks until audio finishes (default: True)lag(float): Additional wait time in seconds (default: 0.25)key(Optional[str]): Unique key for the component
Example:
from streamlit_TTS import text_to_speech
text_to_speech("Welcome to my app!", language='en')
4. openai_text_to_audio(text, openai_api_key=None, language=None, cleanup_hook=None, voice='shimmer', model='gpt-4o-mini-tts')
Converts text to audio using OpenAI's Text-to-Speech API.
Parameters:
text(str): The text to convert to speechopenai_api_key(Optional[str]): OpenAI API key (usesOPENAI_API_KEYenv var if not provided)language(Optional[str]): Language code (currently unused by OpenAI API)cleanup_hook(Optional[Callable]): Custom text cleaning functionvoice(str): Voice name - 'alloy', 'echo', 'fable', 'onyx', 'nova', or 'shimmer' (default: 'shimmer')model(str): Model to use - 'gpt-4o-mini-tts', 'gpt-4o-audio-preview', 'tts-1', or 'tts-1-hd' (default: 'gpt-4o-mini-tts')
Returns: Audio dictionary or None if text is empty
Example:
from streamlit_TTS import openai_text_to_audio
audio = openai_text_to_audio("Hello!", voice='nova', model='gpt-4o-mini-tts')
5. openai_text_to_speech(text, language=None, wait=True, lag=0.25, key=None, voice='shimmer', model='gpt-4o-mini-tts')
Convenience function combining openai_text_to_audio() and auto_play().
Parameters:
text(str): The text to convert to speechlanguage(Optional[str]): Language code (currently unused)wait(bool): If True, blocks until audio finishes (default: True)lag(float): Additional wait time in seconds (default: 0.25)key(Optional[str]): Unique key for the componentvoice(str): Voice name (default: 'shimmer')model(str): Model to use (default: 'gpt-4o-mini-tts')
Example:
from streamlit_TTS import openai_text_to_speech
openai_text_to_speech("Welcome!", voice='nova')
Examples
Basic Usage (Google TTS)
import streamlit as st
from streamlit_TTS import text_to_speech
# Simple text-to-speech
text_to_speech("Hello! Welcome to my Streamlit app!", language='en')
# With user input
text = st.text_input("Enter text to speak:")
if st.button("Speak") and text:
text_to_speech(text, language='en')
Using OpenAI TTS
import streamlit as st
from streamlit_TTS import openai_text_to_speech
import os
# Set your OpenAI API key
os.environ['OPENAI_API_KEY'] = 'your-api-key-here'
# Use OpenAI TTS with different voices
voice = st.selectbox("Choose voice", ['alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer'])
text = st.text_input("Enter text:")
if st.button("Speak") and text:
openai_text_to_speech(text, voice=voice, model='gpt-4o-mini-tts')
Advanced Usage with Custom Cleanup
import streamlit as st
from streamlit_TTS import text_to_audio, auto_play
# Custom text cleanup function
def my_cleanup(text):
# Remove specific patterns
text = text.replace("[SPEAKER]", "")
return text.strip()
# Generate audio with custom cleanup
audio = text_to_audio(
"Some text with [SPEAKER] tags",
language='en',
cleanup_hook=my_cleanup
)
# Play the audio
if audio:
auto_play(audio, wait=True, lag=0.5)
Multi-Language Example
import streamlit as st
from streamlit_TTS import text_to_speech
from gtts.lang import tts_langs
# Get available languages
langs = tts_langs()
# Welcome message
text_to_speech(
"Choose a language, type some text, and click 'Speak it out!'",
language='en'
)
# Language selection
lang = st.selectbox("Choose a language", options=list(langs.keys()))
text = st.text_input("Enter text to speak:")
if st.button("Speak it out!") and lang and text:
text_to_speech(text=text, language=lang)
Browser Compatibility
This component includes robust autoplay with fallback strategies for maximum browser compatibility:
- ✅ Chrome (Desktop & Mobile)
- ✅ Safari (Desktop & Mobile)
- ✅ Firefox
- ✅ Edge
- ✅ Opera
The component automatically handles browser autoplay restrictions with multiple fallback strategies.
Requirements
- Python >= 3.7
- streamlit >= 0.63
- gtts
- pydub
- openai (optional, for OpenAI TTS features)
License
MIT
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file streamlit_tts-0.0.8.tar.gz.
File metadata
- Download URL: streamlit_tts-0.0.8.tar.gz
- Upload date:
- Size: 590.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
15269edbc2a4fd7d86721ef68939959e09cd9f2f6488977759b03643daf7ba7c
|
|
| MD5 |
7771967326c7f0c494828f025f5dd729
|
|
| BLAKE2b-256 |
459d2ed924ef160554f212af36cf95682aeedaa2d139d2993939e2a2983a792b
|
File details
Details for the file streamlit_tts-0.0.8-py3-none-any.whl.
File metadata
- Download URL: streamlit_tts-0.0.8-py3-none-any.whl
- Upload date:
- Size: 594.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cdc4e2ba7f5370d0bf25808770850ee0eb51c1aefb0272da7fd4742dd7920b2c
|
|
| MD5 |
687fc88c4e89cd606feb517ddc200ff0
|
|
| BLAKE2b-256 |
24a6c5c9e11a3d66bad520a9b507d5db157a540d188cec8afa1cd53a71111832
|