Truss
Truss is a Python library that provides a seamless bridge from model development to model delivery. It allows data scientists to containerize, test, and deploy machine learning models as production-ready services with ease, often to platforms like Baseten. The current stable version is 0.15.12, with a fairly active release cadence, frequently releasing patch versions.
Warnings
- breaking Starting with version 0.15.13, Truss will change its default Python version for new models to 3.13 and officially deprecate support for Python 3.9. Models currently using Python 3.9 will need to update their Python version.
- gotcha Truss utilizes certain environment variables internally for its operations and configuration. Overriding these with your own custom environment variables can lead to unexpected behavior, build failures, or runtime errors within your deployed model.
- gotcha Since version 0.15.10, the `config.yaml` file for Truss models adheres to a JSON schema, with validation occurring during build and push operations. This can cause older `config.yaml` files with deprecated or invalid syntax to fail validation.
Install
-
pip install truss
Imports
- truss
import truss
Quickstart
import truss
import os
import pathlib
import shutil
# Define model name and directory
model_name = "my_truss_model"
model_dir = pathlib.Path(f"./{model_name}")
# Clean up previous runs if any
if model_dir.exists():
shutil.rmtree(model_dir)
# Initialize a new Truss model structure, creating the directory
truss.init(model_name)
# Modify the model.py file to add a simple predict function
model_py_path = model_dir / "model" / "model.py"
with open(model_py_path, "w") as f:
f.write("""
import os
class Model:
def __init__(self):
self._data = None
def load(self):
# Simulate loading a model artifact or configuration
self._data = os.environ.get("TRUSS_MODEL_CONFIG", "default_config_value")
def predict(self, model_input):
return f"Hello, {model_input}! Model loaded with config: {self._data}"
""")
print(f"Truss model initialized in: {model_dir.resolve()}")
print(f"To serve locally: Change directory to '{model_name}' (cd {model_name}) and run 'truss serve' in your terminal.")
# To clean up the created model directory (uncomment to run):
# shutil.rmtree(model_dir)