Your first model
Build and deploy your first model
This quickstart guide shows you how to build and deploy your first model, using Baseten’s Truss framework.
Prerequisites
To use Truss, install a recent Truss version and ensure pydantic is v2:
To deploy Truss remotely, you also need a
Baseten account.
It is handy to export your API key to the current shell session or permanently in your .bashrc
:
Initialize your model
Truss is a tool that helps you package your model code and configuration, and ship it to Baseten for deployment, testing, and scaling.
To create your first model, you can use the truss init
command.
This will create a new directory called hello-world
with the following files:
config.yaml
- A configuration file for your model.model.py
- A Python file that contains your model codepackages/
- A folder to hold any dependencies your model needsdata/
- A folder to hold any data your model needs
For this example, we’ll focus on the config.yaml
file and the model.py
file.
config.yaml
The config.yaml
file is used to configure dependencies, resources, and
other settings for your model.
Let’s take a look at the contents:
Some key fields to note:
requirements
: This is a list ofpip
packages that will be installed when your model is deployed.resources
: This is where you can specify the resources your model will use.secrets
: This is where you can specify any secrets your model will need, such as HuggingFace API keys.
See the Configuration page for more information on the config.yaml
file.
model.py
Next, let’s take a look at the model.py
file.
In Truss models, we expect users to provide a Python class with the following methods:
__init__
: This is the constructor.load
: This is called at model startup, and should include any setup logic, such as weight downloading or initializationpredict
: This is the method that is called during inference.
Deploy your model
To deploy your model, you can use the truss push
command.
This will deploy your model to Baseten.
Invoke your model
After deploying your model, you can invoke it with the invocation URL provided:
A Real Example
To show a slightly more complex example, let’s deploy a text classification model from HuggingFace!
In this example, we’ll use the transformers
library to load a pre-trained model,
from HuggingFace, and use it to classify the given text.
config.yaml
To deploy this model, we need to add a few more dependencies to our config.yaml
file.
model.py
Next, let’s change our model.py
file to use the transformers
library to load the model,
and then use it to predict the sentiment of a given text.
Running inference
Similarly to our previous example, we can deploy this model using truss push
And then invoke it using the invocation URL on Baseten.
Next steps
Now that you’ve deployed your first model, you can learn more about more options for configuring your model, and implementing your model.
Was this page helpful?