Skip to main content

A minimalist framework for online deployment of sklearn-like models

Project description

modelib

A minimalist framework for online deployment of sklearn-like models

Package version Code style: black Semantic Versions License

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_name="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(
    "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.

Swagger UI

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

modelib-0.3.0a2.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

modelib-0.3.0a2-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

Details for the file modelib-0.3.0a2.tar.gz.

File metadata

  • Download URL: modelib-0.3.0a2.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.2 CPython/3.11.10 Linux/6.8.0-1014-azure

File hashes

Hashes for modelib-0.3.0a2.tar.gz
Algorithm Hash digest
SHA256 194da3dc8fa3d4e5d26d71b771ede63e508038a69a1537f6d1b26478a3ac71ed
MD5 6ab0cac711b67e75a1861d263a0fe67b
BLAKE2b-256 9f9955a920834e8eacc997e4e64dacfa88ee1955efcf6a31fc0b4475c773d68c

See more details on using hashes here.

File details

Details for the file modelib-0.3.0a2-py3-none-any.whl.

File metadata

  • Download URL: modelib-0.3.0a2-py3-none-any.whl
  • Upload date:
  • Size: 10.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.2 CPython/3.11.10 Linux/6.8.0-1014-azure

File hashes

Hashes for modelib-0.3.0a2-py3-none-any.whl
Algorithm Hash digest
SHA256 0bfe3bfca25d6e5ca9ba8328b18ac74c3def777aa445edc12bd19a9022065953
MD5 efb3c60c7888abd3e764c7aa657696a7
BLAKE2b-256 8b054d7018ab31bd46676d0003558446cdc8bf74fbbcd808ca7d568fde8ac86b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page