pythonintermediate
BentoML Model Serving Service
Package and serve a scikit-learn model as a REST API with BentoML in Python.
pythonPress ⌘/Ctrl + Shift + C to copy
import bentoml
import numpy as np
from sklearn.ensemble import RandomForestClassifier
# Save model to BentoML store
model = RandomForestClassifier(n_estimators=50, random_state=42)
model.fit([[0,0],[1,1],[2,2]], [0,1,2])
bento_model = bentoml.sklearn.save_model('iris_classifier', model)
print(f'Saved: {bento_model.tag}')
# Define service (in service.py)
SERVICE_CODE = '''
import bentoml
import numpy as np
from pydantic import BaseModel
class IrisInput(BaseModel):
features: list[list[float]]
@bentoml.service(resources={'cpu': '2'})
class IrisService:
model = bentoml.depends(bentoml.sklearn.get('iris_classifier:latest'))
@bentoml.api
def predict(self, data: IrisInput) -> list[int]:
arr = np.array(data.features)
return self.model.predict(arr).tolist()
'''
with open('service.py', 'w') as f:
f.write(SERVICE_CODE)
print('Run: bentoml serve service:IrisService')Use Cases
- model deployment
- ML serving
- production APIs
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
pythonadvanced
Ray Serve ML Model Deployment
Deploy a scalable ML serving endpoint with Ray Serve, handling concurrent requests and model loading.
Best for: ML serving
#ray#model-serving
typescriptintermediate
Vercel API — Trigger and List Deployments
Use the Vercel REST API to trigger deployments, list recent builds, and check deployment status.
Best for: Triggering deployments from CI or Slack bots
#vercel#api
pythonbeginner
Mistral AI API Client in Python
Make chat and embedding requests to Mistral AI using the official Python SDK.
Best for: European AI compliance
#mistral#llm
pythonintermediate
ONNX Runtime Fast ML Inference
Export a PyTorch model to ONNX and run fast CPU inference with ONNX Runtime.
Best for: model deployment
#onnx#inference