Deploying an ML Model
A short tutorial on building a sentiment analysis model and deploying it as a REST API with Beam
Define the environment
The first thing we’ll do is define the environment that our app will run on. For this example, we’re building a Sentiment Analysis model using Huggingface.
First, create a file with your Beam App definition. You can name this whatever
you want. In this example, we’ll call it app.py
.
Invoking the Huggingface Model
Now, we’ll write some code to predict the sentiment of a given text prompt.
Create a new file. Again, you can name this whatever you want. We’ll name ours
inference.py
Our function takes keyword arguments, as (**inputs)
.
Setting a REST API Trigger
To deploy the API, we’ll create a REST API Trigger in our app.py
.
Our trigger requires three things:
-
Inputs - the name and type of all inputs to the API
-
Outputs - the name and type of all outputs returned from the API
-
Handler - the file and function to be invoked when the API is called
Add the following lines to your app.py
file:
(Optional) Caching Model on Disk
For performance reasons, you want to store the model on disk rather than downloading it from HuggingFace for each request.
Create a Persistent Volume to store the model weights.
Add the following lines to your app.py
:
The complete app.py
file will look like this:
Deploying the app
To deploy the model, enter your terminal and cd
to the directory you’re
working on.
Then, run the following:
Once you deploy, you’ll see the following console output:
At the bottom of the console, you’ll see a URL for invoking your function. Here’s what a cURL request would look like:
The requests are authenticated with basic auth. Your username is your ClientID, and password is your Client Secret.
Congrats - you just deployed your first function on Beam!