How to Stream Speech with Parler-TTS using Inferless
This tutorial demonstrates how to implement real-time text-to-speech (TTS) streaming using the parler_tts_mini model and Parler-TTS library.
Introduction
This tutorial implements a text-to-speech (TTS) streaming model, parler_tts_mini using Parler_TTS library. It will enable real-time TTS streaming, converting text inputs into speech and stream the audio chunk by chunk.
Defining Dependencies
We are using the Parler_TTS and Transformers libraries for the deployment.
Constructing the GitHub/GitLab Template
Now quickly construct the GitHub/GitLab template, this process is mandatory and make sure you don’t add any file named model.py
You can also add other files to this directory.
Create the Input Schema
Let’s begin by creating the input_schema.py
file, which defines the input structure for our model. You can find the complete file in our GitHub repository.
For this tutorial, we’ll use two text inputs:
prompt_value
: The main text to be converted to speechinput_value
: The voice instructions for the TTS model
Both inputs are of string
data type. The output will be streamed using Server-Sent Events (SSE), delivering audio chunks as base64
encoded strings. This approach allows for real-time audio playback as the speech is generated.
To enable streaming with SSE, it’s crucial to set the IS_STREAMING_OUTPUT
property to True
in your model configuration. This tells the system to expect and handle a continuous output stream rather than a single response.
It’s important to note the limitations when working with streaming inputs:
- Supported datatypes: Only
INT
,STRING
, andBOOLEAN
are supported as input datatypes. - Input shape: The shape of each parameter should be
[1]
. For multiple inputs or complex objects, usejson.dumps(object)
to convert them to a string before passing. - Consistent output schema: All iterative responses in the output stream must adhere to the same schema.
Now, let’s create the input_schema.py
file with the following content:
Create the class for Text-to-Speech Streamer
In the parler.py file, we define the ParlerTTSStreamer
class and import all the required functions.
Create the class for inference
In the app.py
we will define the class and import all the required functions
-
def initialize
: In this function, we will create an object of theParlerTTSStreamer
class which will load the model. You can define anyvariable
that you want to use during inference. -
def infer
: Theinfer
function is the core of your model’s inference process. It’s invoked for each incoming request and is responsible for processing the input and generating the streamed output. Here’s a breakdown of its key components:a. Output Streaming Setup:
- We create a dictionary
output_dict
with a key'OUT'
. - This dictionary will hold each chunk of the generated audio as a base64-encoded string.
b. Processing and Streaming:
- As the model generates audio chunks, we encode each chunk to base64.
- For each encoded chunk (
mp3_str
), we update theoutput_dict
: - We will use the
stream_output_handler
for streaming the generated audio output chunks. It providesstream_output_handler.send_streamed_output()
function to send this chunk to the client: - This process repeats for each audio chunk, allowing real-time streaming of the generated speech.
c. Finalizing the Stream:
- After all chunks have been processed and sent, we call:
- This function signals the end of the stream to the client, properly closing the event streamer.
- We create a dictionary
-
def finalize
: This function cleans up all the allocated memory.
Creating the Custom Runtime
This is a mandatory step where we allow the users to upload their own custom runtime through inferless-runtime-config.yaml.
To enable streaming functionality, ensure you are using CUDA version 12.4.1
.
Method A: Deploying the model on Inferless Platform
Inferless supports multiple ways of importing your model. For this tutorial, we will use GitHub.
Step 1: Login to the inferless dashboard can click on Import model button
Navigate to your desired workspace in Inferless and Click on Add a custom model
button that you see on the top right. An import wizard will open up.
Step 2: Follow the UI to complete the model Import
- Select the GitHub/GitLab Integration option to connect your source code repository with the deployment environment.
- Navigate to the specific GitHub repository that contains your model’s code. Here, you will need to identify and enter the name of the model you wish to import.
- Choose the appropriate type of machine that suits your model’s requirements. Additionally, specify the minimum and maximum number of replicas to define the scalability range for deploying your model.
- Optionally, you have the option to enable automatic build and deployment. This feature triggers a new deployment automatically whenever there is a new code push to your repository.
- If your model requires additional software packages, configure the Custom Runtime settings by including necessary pip or apt packages. Also, set up environment variables such as Inference Timeout, Container Concurrency, and Scale Down Timeout to tailor the runtime environment according to your needs.
- Wait for the validation process to complete, ensuring that all settings are correct and functional. Once validation is successful, click on the “Import” button to finalize the import of your model.
Step 3: Wait for the model build to complete usually takes ~5-10 minutes
Step 4: Use the APIs to call the model
Once the model is in ‘Active’ status you can click on the ‘API’ page to call the model
Here is the Demo:
Method B: Deploying the model on Inferless CLI
Inferless allows you to deploy your model using Inferless-CLI. Follow the steps to deploy using Inferless CLI.
Clone the repository of the model
Let’s begin by cloning the model repository:
Deploy the Model
To deploy the model using Inferless CLI, execute the following command:
Explanation of the Command:
--gpu A100
: Specifies the GPU type for deployment. Available options includeA10
,A100
, andT4
.--runtime inferless-runtime-config.yaml
: Defines the runtime configuration file. If not specified, the default Inferless runtime is used.