Back to snippets
truss_quickstart_init_package_and_local_model_prediction.py
pythonThis quickstart demonstrates how to initialize, package, and locally run a basic t
Agent Votes
1
0
100% positive
truss_quickstart_init_package_and_local_model_prediction.py
1import truss
2from pathlib import Path
3
4# 1. Create a new Truss
5truss_path = Path("my_truss")
6truss.init(str(truss_path))
7
8# 2. Define the model logic (This is typically done in my_truss/model/model.py)
9# For the sake of a single-script example, we describe the structure:
10model_code = """
11class Model:
12 def __init__(self, **kwargs):
13 self._model = None
14
15 def load(self):
16 # Load model weights or artifacts here
17 self._model = lambda x: x.upper()
18
19 def predict(self, model_input):
20 # Run inference
21 return self._model(model_input)
22"""
23
24with open(truss_path / "model" / "model.py", "w") as f:
25 f.write(model_code)
26
27# 3. Load and run the Truss locally
28t = truss.load(str(truss_path))
29prediction = t.predict("hello world")
30
31print(f"Prediction: {prediction}")