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.
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 speech
input_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, and BOOLEAN are supported as input datatypes.
Input shape: The shape of each parameter should be [1]. For multiple inputs or complex objects, use json.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:
Copy
INPUT_SCHEMA = { "input_value": { 'datatype': 'STRING', 'required': True, 'shape': [1], 'example': ["A male speaker with a low-pitched voice delivering his words at a fast pace in a small, confined space with a very clear audio and an animated tone."] }, "prompt_value": { 'datatype': 'STRING', 'required': True, 'shape': [1], 'example': ["Remember - this is only the first iteration of the model! To improve the prosody and naturalness of the speech further, we're scaling up the amount of training data by a factor of five times."] }}IS_STREAMING_OUTPUT = True
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 the ParlerTTSStreamer class which will load the model. You can define any variable that you want to use during inference.
def infer: The infer 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 the output_dict:
Copy
output_dict['OUT'] = mp3_str
We will use the stream_output_handler for streaming the generated audio output chunks. It provides stream_output_handler.send_streamed_output() function to send this chunk to the client:
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.
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.