pythonintermediate

BentoML Model Serving Service

Package and serve a scikit-learn model as a REST API with BentoML in Python.

python
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.