A minimalist framework for online deployment of sklearn-like models
Installation
pip install modelib
Usage
The modelib package provides a simple interface to deploy and serve models online. The package is designed to be used with the fastapi package, and supports serving models that are compatible with the sklearn package.
First, you will need to create a model that is compatible with the sklearn package. For example, let's create a simple RandomForestClassifier model with a StandardScaler preprocessor:
MODEL = Pipeline(
[
("scaler", StandardScaler()),
("clf", RandomForestClassifier(random_state=42)),
]
).set_output(transform="pandas")
Let's assume that you have a dataset with the following columns:
request_model = [
{"name": "sepal length (cm)", "dtype": "float64"},
{"name": "sepal width (cm)", "dtype": "float64"},
{"name": "petal length (cm)", "dtype": "float64"},
{"name": "petal width (cm)", "dtype": "float64"},
]
Alternatively, you can use a pydantic model to define the request model, where the alias field is used to match the variable names with the column names in the training dataset:
class InputData(pydantic.BaseModel):
sepal_length: float = pydantic.Field(alias="sepal length (cm)")
sepal_width: float = pydantic.Field(alias="sepal width (cm)")
petal_length: float = pydantic.Field(alias="petal length (cm)")
petal_width: float = pydantic.Field(alias="petal width (cm)")
request_model = InputData
After the model is created and trained, you can create a modelib runner for this model as follows:
import modelib as ml
simple_runner = ml.SklearnRunner(
name="my simple model",
predictor=MODEL,
method_names="predict",
request_model=request_model,
)
Another option is to use the SklearnPipelineRunner class which allows you to get all the outputs of the pipeline:
pipeline_runner = ml.SklearnPipelineRunner(
name="Pipeline Model",
predictor=MODEL,
method_names=["transform", "predict"],
request_model=request_model,
)
Now you can create a FastAPI app with the runners:
app = ml.init_app(runners=[simple_runner, pipeline_runner])
You can also pass an existing FastAPI app to the init_app function:
import fastapi
app = fastapi.FastAPI()
app = ml.init_app(app=app, runners=[simple_runner, pipeline_runner])
The init_app function will add the necessary routes to the FastAPI app to serve the models. You can now start the app with:
uvicorn <replace-with-the-script-filename>:app --reload
After the app is running you can check the created routes in the Swagger UI at the /docs endpoint.
The created routes expect a JSON payload with the features as keys and the values as the input to the model. For example, to make a prediction with the simple model runner you can send a POST request to the /my-simple-model endpoint with the following payload:
{
"sepal length (cm)": 5.1,
"sepal width (cm)": 3.5,
"petal length (cm)": 1.4,
"petal width (cm)": 0.2
}
The response will be a JSON with the prediction:
{
"result": 0
}
Contributing
If you want to contribute to the project, please read the CONTRIBUTING.md file for more information.
Metadata
Release files for modelib 0.3.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| modelib-0.3.1.tar.gz | 8.9 kB | Without provenance | Details for modelib-0.3.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| modelib-0.3.1-py3-none-any.whl | Python 3 | none | any | Details for modelib-0.3.1-py3-none-any.whl |
Total release size: 18.8 kB
Release files / modelib-0.3.1.tar.gz
| Download URL | modelib-0.3.1.tar.gz |
|---|---|
| Size | 8.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
dd577123f7c6f25af579cad2add3682a01f0f1a6078b703950dd95d71a29b1d0
|
|
BLAKE2b-256 checksum How to use checksums |
781c8668133b44342b1b10b4014110310c4f0276d7e0783e422f8f85b384c067
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.4.2 CPython/3.11.11 Linux/6.8.0-1021-azure
|
Release files / modelib-0.3.1-py3-none-any.whl
| Download URL | modelib-0.3.1-py3-none-any.whl |
|---|---|
| Size | 10.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
bc3333c23cba8f3fce6a74dac8f2e0a2e1262936096e7b3c825dabac94395d70
|
|
BLAKE2b-256 checksum How to use checksums |
32a747a4ff38ec5fbc39fea4dc572da496025bb3762d0480e33c8d7bbd2f868c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.4.2 CPython/3.11.11 Linux/6.8.0-1021-azure
|
Security for modelib 0.3.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.