A short tutorial on building a sentiment analysis model and deploying it as a REST API with Beam
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
.
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)
.
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:
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:
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!