<p align="center">
<a href="https://github.com/pier-digital/modelib"><img src="https://raw.githubusercontent.com/pier-digital/modelib/main/images/logo.png" alt="modelib"></a>
</p>
<p align="center">
<em>A minimalist framework for online deployment of sklearn-like models</em>
</p>
<div align="center">
[](https://pypi.org/project/modelib/)
[](https://github.com/psf/black)
[](https://github.com/pier-digital/modelib/releases)
[](https://github.com/pier-digital/modelib/blob/main/LICENSE)
</div>
## Installation
```bash
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](https://fastapi.tiangolo.com/) package, and supports serving models that are compatible with the [sklearn](https://scikit-learn.org/stable/) 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:
```python
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:
```python
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:
```python
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:
```python
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:
```python
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:
```python
app = ml.init_app(runners=[simple_runner, pipeline_runner])
```
You can also pass an existing FastAPI app to the `init_app` function:
```python
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:
```bash
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:
```json
{
"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:
```json
{
"result": 0
}
```
## Contributing
If you want to contribute to the project, please read the [CONTRIBUTING.md](CONTRIBUTING.md) file for more information.
Raw data
{
"_id": null,
"home_page": "https://github.com/pier-digital/modelib",
"name": "modelib",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.10",
"maintainer_email": null,
"keywords": "machine learning, fastapi, sklearn, online deployment",
"author": "Gabriel Guarisa",
"author_email": "gabrielguarisa@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/49/4a/217dfc91b033cac1c6db6b6252c4a4e7923ee9e23d04a107426f6ed61a8c/modelib-0.3.0.tar.gz",
"platform": null,
"description": "<p align=\"center\">\n <a href=\"https://github.com/pier-digital/modelib\"><img src=\"https://raw.githubusercontent.com/pier-digital/modelib/main/images/logo.png\" alt=\"modelib\"></a>\n</p>\n<p align=\"center\">\n <em>A minimalist framework for online deployment of sklearn-like models</em>\n</p>\n\n<div align=\"center\">\n\n[](https://pypi.org/project/modelib/)\n[](https://github.com/psf/black)\n[](https://github.com/pier-digital/modelib/releases)\n[](https://github.com/pier-digital/modelib/blob/main/LICENSE)\n\n</div>\n\n## Installation\n\n```bash\npip install modelib\n```\n\n## Usage\n\nThe modelib package provides a simple interface to deploy and serve models online. The package is designed to be used with the [fastapi](https://fastapi.tiangolo.com/) package, and supports serving models that are compatible with the [sklearn](https://scikit-learn.org/stable/) package.\n\nFirst, 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:\n\n```python\nMODEL = Pipeline(\n [\n (\"scaler\", StandardScaler()),\n (\"clf\", RandomForestClassifier(random_state=42)),\n ]\n).set_output(transform=\"pandas\")\n```\n\nLet's assume that you have a dataset with the following columns:\n\n```python\nrequest_model = [\n {\"name\": \"sepal length (cm)\", \"dtype\": \"float64\"},\n {\"name\": \"sepal width (cm)\", \"dtype\": \"float64\"},\n {\"name\": \"petal length (cm)\", \"dtype\": \"float64\"},\n {\"name\": \"petal width (cm)\", \"dtype\": \"float64\"},\n]\n```\n\nAlternatively, 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:\n\n```python\nclass InputData(pydantic.BaseModel):\n sepal_length: float = pydantic.Field(alias=\"sepal length (cm)\")\n sepal_width: float = pydantic.Field(alias=\"sepal width (cm)\")\n petal_length: float = pydantic.Field(alias=\"petal length (cm)\")\n petal_width: float = pydantic.Field(alias=\"petal width (cm)\")\n\nrequest_model = InputData\n```\n\nAfter the model is created and trained, you can create a modelib runner for this model as follows:\n\n```python\nimport modelib as ml\n\nsimple_runner = ml.SklearnRunner(\n name=\"my simple model\",\n predictor=MODEL,\n method_names=\"predict\",\n request_model=request_model,\n)\n```\n\nAnother option is to use the `SklearnPipelineRunner` class which allows you to get all the outputs of the pipeline:\n\n```python\npipeline_runner = ml.SklearnPipelineRunner(\n name=\"Pipeline Model\",\n predictor=MODEL,\n method_names=[\"transform\", \"predict\"],\n request_model=request_model,\n)\n```\n\nNow you can create a FastAPI app with the runners:\n\n```python\napp = ml.init_app(runners=[simple_runner, pipeline_runner])\n```\n\nYou can also pass an existing FastAPI app to the `init_app` function:\n\n```python\nimport fastapi\n\napp = fastapi.FastAPI()\n\napp = ml.init_app(app=app, runners=[simple_runner, pipeline_runner])\n```\n\nThe `init_app` function will add the necessary routes to the FastAPI app to serve the models. You can now start the app with:\n\n```bash\nuvicorn <replace-with-the-script-filename>:app --reload\n```\n\nAfter the app is running you can check the created routes in the Swagger UI at the `/docs` endpoint.\n\n\n\nThe 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:\n\n```json\n{\n \"sepal length (cm)\": 5.1,\n \"sepal width (cm)\": 3.5,\n \"petal length (cm)\": 1.4,\n \"petal width (cm)\": 0.2\n}\n```\n\nThe response will be a JSON with the prediction:\n\n```json\n{\n \"result\": 0\n}\n```\n\n## Contributing\n\nIf you want to contribute to the project, please read the [CONTRIBUTING.md](CONTRIBUTING.md) file for more information.\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "A minimalist framework for online deployment of sklearn-like models",
"version": "0.3.0",
"project_urls": {
"Homepage": "https://github.com/pier-digital/modelib",
"Repository": "https://github.com/pier-digital/modelib"
},
"split_keywords": [
"machine learning",
" fastapi",
" sklearn",
" online deployment"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "161af9f2ca256d61bca8be19144b048eb2973da38d541b3b9afd75590213bdda",
"md5": "a76a9fc35fe470fc36971d9b15a3fab8",
"sha256": "a6b98fbeb771b26bdb42eb0f663d3e32474b65b725198cdf31f85b4b288618f8"
},
"downloads": -1,
"filename": "modelib-0.3.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "a76a9fc35fe470fc36971d9b15a3fab8",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.10",
"size": 9990,
"upload_time": "2025-02-07T18:30:47",
"upload_time_iso_8601": "2025-02-07T18:30:47.534391Z",
"url": "https://files.pythonhosted.org/packages/16/1a/f9f2ca256d61bca8be19144b048eb2973da38d541b3b9afd75590213bdda/modelib-0.3.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "494a217dfc91b033cac1c6db6b6252c4a4e7923ee9e23d04a107426f6ed61a8c",
"md5": "b536c13f389332360df9f22202b34365",
"sha256": "a579af0fa2e4ca24367d0266ae88beabecdf4cb852cfb7bfb15203c86fe95f5a"
},
"downloads": -1,
"filename": "modelib-0.3.0.tar.gz",
"has_sig": false,
"md5_digest": "b536c13f389332360df9f22202b34365",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.10",
"size": 8904,
"upload_time": "2025-02-07T18:30:49",
"upload_time_iso_8601": "2025-02-07T18:30:49.175409Z",
"url": "https://files.pythonhosted.org/packages/49/4a/217dfc91b033cac1c6db6b6252c4a4e7923ee9e23d04a107426f6ed61a8c/modelib-0.3.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-02-07 18:30:49",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "pier-digital",
"github_project": "modelib",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "modelib"
}