Truss

0.15.12 · active · verified Mon Apr 13

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

Install

Imports

Quickstart

This quickstart demonstrates how to initialize a new Truss project and add a basic `predict` function. The example includes simulating model data loading via an environment variable. To actually serve and test the model, navigate into the created directory (`cd my_truss_model`) and run `truss serve` from your terminal. For deployment to platforms like Baseten, an API key (e.g., `os.environ.get('BASETEN_API_KEY', '')`) would typically be required for `truss.push()` operations.

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)

view raw JSON →