Skip to main content

Deploy mlflow models as JSON APIs with minimal new code.

Project description

fastapi mlflow

Deploy mlflow models as JSON APIs using FastAPI with minimal new code.

Installation

pip install fastapi-mlflow

For running the app in production, you will also need an ASGI server, such as Uvicorn or Hypercorn.

Install on Apple Silicon (ARM / M1)

If you experience problems installing on a newer generation Apple silicon based device, this solution from StackOverflow before retrying install has been found to help.

brew install openblas gfortran
export OPENBLAS="$(brew --prefix openblas)"

License

Copyright © 2022-23 Auto Trader Group plc.

Apache-2.0

Examples

Simple

Create

Create a file main.py containing:

from fastapi_mlflow.applications import build_app
from mlflow.pyfunc import load_model

model = load_model("/Users/me/path/to/local/model")
app = build_app(model)

Run

Run the server with:

uvicorn main:app

Check

Open your browser at http://127.0.0.1:8000/docs

You should see the automatically generated docs for your model, and be able to test it out using the Try it out button in the UI.

Serve multiple models

It should be possible to host multiple models (assuming that they have compatible dependencies...) by leveraging FastAPIs Sub Applications:

from fastapi import FastAPI
from fastapi_mlflow.applications import build_app
from mlflow.pyfunc import load_model

app = FastAPI()

model1 = load_model("/Users/me/path/to/local/model1")
model1_app = build_app(model1)
app.mount("/model1", model1_app)

model2 = load_model("/Users/me/path/to/local/model2")
model2_app = build_app(model2)
app.mount("/model2", model2_app)

Run and Check as above.

Custom routing

If you want more control over where and how the prediction end-point is mounted in your API, you can build the predictor function directly and use it as you need:

from inspect import signature

from fastapi import FastAPI
from fastapi_mlflow.predictors import build_predictor
from mlflow.pyfunc import load_model

model = load_model("/Users/me/path/to/local/model")
predictor = build_predictor(model)
app = FastAPI()
app.add_api_route(
    "/classify",
    predictor,
    response_model=signature(predictor).return_annotation,
    methods=["POST"],
)

Run and Check as above.

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

fastapi-mlflow-0.3.2.tar.gz (8.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_mlflow-0.3.2-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

Details for the file fastapi-mlflow-0.3.2.tar.gz.

File metadata

  • Download URL: fastapi-mlflow-0.3.2.tar.gz
  • Upload date:
  • Size: 8.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.15 CPython/3.10.9 Darwin/21.6.0

File hashes

Hashes for fastapi-mlflow-0.3.2.tar.gz
Algorithm Hash digest
SHA256 fac5cb68b48e03450fc0504eb12de4e500038df5817f60294dd45d3d37ff91c6
MD5 e63c2a7f78de44fba28dd694d7d6c3a2
BLAKE2b-256 fe70d425472fa26e2cf797a0a3952f3a935c3b417aeb06bc8685447157a6d8ce

See more details on using hashes here.

File details

Details for the file fastapi_mlflow-0.3.2-py3-none-any.whl.

File metadata

  • Download URL: fastapi_mlflow-0.3.2-py3-none-any.whl
  • Upload date:
  • Size: 9.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.15 CPython/3.10.9 Darwin/21.6.0

File hashes

Hashes for fastapi_mlflow-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c5fb715479b519251578a738221b77c81ec6f09c5247d345266c8c173de80b7b
MD5 a8753d2355f6fcd6be51e16da551cfe2
BLAKE2b-256 849b7deaa6a017ddf4c7b12853e4f8932b95991d80b7079f49ce6733a071050b

See more details on using hashes here.

Supported by

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