LitServe
Deploying sample Whisper Speech to Text model with LitServe
LitServe is a lightweight and fast inference server for machine learning models. It can be a good alternative to FastAPI if you are looking for dynamic batching support. It also has higher level abstractions for authentication, middleware, OpenAI compatible spec, streaming etc built on top of FastAPI.
In this example, we will deploy a simple Whisper model (speech to text) using faster-whisper and Litserve. You can find the code for this example here.
Live Demo
You can view this example deployed here.
The key files are:
whisper_server.py
: Contains theWhisperLitAPI
that implements theLitAPI
interface.requirements.txt
: Contains the dependencies.
How to write the inference function in LitServe
The whisper_server.py
file contains the WhisperLitAPI
class that implements the LitAPI
interface.
Mainly we inherit the LitAPI
class and implement the setup
, decode_request
, predict
and encode_response
methods.
setup
: Load the model.decode_request
: Decodes and transforms the request body to the input format expected by the model.predict
: Processes the output ofdecode_request
and runs model inference.encode_response
: Formats the response. Can perform any postprocessing on the response.
See LitServe Docs for reference and more examples.
Load the file from the path specified in the MODEL_DIR
environment variable
We are using an environment variable MODEL_DIR
to read the model path. This is useful when we want to load a model from a different path that can be downloaded and cached separately. See Cache Models and Artifacts guide for more details.
Running the server locally
- Install the dependencies
- Run the server
- Test the server
We’ll see something like this:
Deploying to TrueFoundry
Since the models are being pulled from HuggingFace Hub, we can directly deploy the code to TrueFoundry and use Artifacts Download feature to automatically download the model.
You can also log the model to TrueFoundry Model Registry and use it as source for automatic model download.
Push the code to a Git repository or directly deploy from local machine
Once you have tested your code locally, we highly recommend pushing the code a Git repository. This allows you to version control the code and also makes the deployment process much easier. However, if you don’t have access to a Git repository, or the Git repositories are not integrated with Truefoundry, you can directly deploy from local laptop. You can follow the guide here to deploy your code.
Configure PythonBuild
Download Model from HuggingFace Hub in the deployment configuration
Add the model id and revision from HuggingFace Hub in Artifacts Download
section
View the deployment, logs and metrics
Once the deployment goes through, you can view the deployment, the pods, logs, metrics and events to debug any issues.