In this tutorial, you will learn how to use the Smallest AI platform to synthesize text to speech both synchronously and asynchronously. By the end of this tutorial, you will be able to convert text into speech using our API.

You can access the source code for the Python SDK on our GitHub repository.

Requirements

Before you begin, ensure you have the following:

  • Python (3.9 or higher) installed on your machine.
  • An API key from the Smallest AI platform (sign up here).

Setup

Install our SDK

pip install smallestai

Set your API key as an environment variable

export SMALLEST_API_KEY=YOUR_API_KEY

Synchronous Text to Speech

Here is an example of how to synthesize text to speech synchronously:

Asynchronous Text to Speech

Here is an example of how to synthesize text to speech asynchronously:

Parameters

  • api_key (str): Your API key (can be set via SMALLEST_API_KEY environment variable)
  • model (str): TTS model to use (default: “lightning”)
  • sample_rate (int): Audio sample rate (default: 24000)
  • voice_id (str): Voice ID (default: “emily”)
  • speed (float): Speech speed multiplier (default: 1.0)
  • add_wav_header (bool): Include WAV header in output (default: True)
  • transliterate (bool): Enable text transliteration (default: False)
  • remove_extra_silence (bool): Remove additional silence (default: True)

Note: While the transliterate parameter is provided, please note that it is not fully supported and may not perform consistently across all cases. It is recommended to use the model without relying on this parameter. Please refer to Best practices for more details.

These parameters are part of the Smallest and AsyncSmallest instance. They can be set when creating the instance (as shown above). However, the synthesize function also accepts kwargs, allowing you to override any of these parameters on a per-request basis.

For example, you can modify the speech speed and sample rate just for a particular synthesis request:

Conclusion

The Smallest AI Text-to-Speech SDK offers both synchronous and asynchronous options, catering to a variety of use cases:

  • Synchronous TTS: Ideal for applications where immediate responses are needed, such as real-time voice assistants, chatbot integrations, or interactive voice systems. It ensures that the audio is generated and available instantly for use within the same execution flow.

  • Asynchronous TTS: Designed for scenarios that involve handling multiple requests or large-scale processing. For example, if you need to convert multiple text inputs into speech concurrently, such as creating audio files for an audiobook or processing a batch of text-based announcements, asynchronous TTS allows you to execute these tasks efficiently without blocking other operations. This approach ensures scalability and optimal resource utilization, particularly in environments where time and performance are critical.

By understanding these modes and tailoring their usage to specific requirements, you can build highly responsive, scalable, and efficient solutions using the Smallest AI platform.

If you have any questions or suggestions, please create an issue on the smallest-python-sdk GitHub .