Skip to main content

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', or None
  • wait (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 speech
  • language (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 speech
  • language (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 speech
  • openai_api_key (Optional[str]): OpenAI API key (uses OPENAI_API_KEY env var if not provided)
  • language (Optional[str]): Language code (currently unused by OpenAI API)
  • cleanup_hook (Optional[Callable]): Custom text cleaning function
  • voice (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 speech
  • language (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 component
  • voice (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

Metadata

Release files for streamlit-TTS 0.0.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for streamlit-TTS 0.0.8
File Size Uploaded
streamlit_tts-0.0.8.tar.gz 590.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for streamlit-TTS 0.0.8
File Interpreter ABI Platform
streamlit_tts-0.0.8-py3-none-any.whl Python 3 none any Details

Total release size: 1.2 MB

Release files / streamlit_tts-0.0.8.tar.gz

Download URL streamlit_tts-0.0.8.tar.gz
Size 590.7 kB
Tags Source
SHA-256 checksum
How to use checksums
15269edbc2a4fd7d86721ef68939959e09cd9f2f6488977759b03643daf7ba7c
BLAKE2b-256 checksum
How to use checksums
459d2ed924ef160554f212af36cf95682aeedaa2d139d2993939e2a2983a792b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.12

Release files / streamlit_tts-0.0.8-py3-none-any.whl

Download URL streamlit_tts-0.0.8-py3-none-any.whl
Size 594.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cdc4e2ba7f5370d0bf25808770850ee0eb51c1aefb0272da7fd4742dd7920b2c
BLAKE2b-256 checksum
How to use checksums
24a6c5c9e11a3d66bad520a9b507d5db157a540d188cec8afa1cd53a71111832
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.12

Release history Release notifications | RSS feed

This release

0.0.8 This release

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page