Skip to main content

model-serving

Flask based python wrapper for deploying models as a REST based service based on

💡 Flask

💡 Gunicorn

💡 Protobuf3 (optional for schema validation)

🥳 flask-caching

🥳 prometheus metrics

The repo also contains examples of registering end points and a Makefile to run the service

Installation

 pip install model-serving
 

Project Structure

aylien_model_serving
│
|--- requirements.txt
|--- Makefile
│   
│
└───app
│    |-- app_factory.py
│    |-- cached_app_factory.py       
│      
│   
└───examples
    │-----example_schema.proto
    |-----example_schema_pb2.py(autogenerated by protoc)
    │-----example_serving_handler.py
    |-----example_serving_handler_cached.py

How it works

  • It runs a web service on the given port (defaults to 8000).
  • Any incoming request JSON will be passed to your ServingHandler.process_request
  • Your ServingHandler.process_request is expected to return a json
  • The request and response will be validated with a protobuf schema (optional)
  • This library wraps common service code, monitoring, exception handling, etc.

Usage

  1. Install this library as a dependency for whatever model you want to serve.
  2. Create a ServingHandler (see below for interface details).
  3. Run the make target make COMMAND_UNCACHED='ServingHandler.run_app()' example-service

Interfaces

The main interface to flask apps defined in app_factory is the process_json function. This function expects to receive json input, optionally perform schema validation, then call the callable_handler function using each of the fields in the json object as a keyword argument to the function. The function is expected to return an object that can be parsed to json and sent as the response.

This design allows for a very simple but powerful interface that can easily make an endpoint out of just about any Python function.

Example Serving Handler

The example serving handler defined here does the following

  1. Defines a method predict_lang. For the purposes of this example, this returns a static prediction. Ideally would be the prediction or classification from your model.
  2. Imports a protobuf3 generated .py schema file(only if you require the json message to be schema validated)
  3. Defines a function process_request that calls the wrapper function process_json with the callable from 1 and schema from 2
  4. Registers process_request and its route mapping
  5. Repeat 1-4 for a (route, callable) pair if you have more than one service end point.
import examples.example_schema_pb2 as schema
from aylien_model_serving.app_factory import FlaskAppWrapper, InvalidRequest


def predict_lang(text):
    return "en", 0.71


def predict(title=None, body=None, enrichments=None):
    if body is None:
        body = enrichments["extracted"]["value"]["body"]
    if title is None and body is None:
        raise InvalidRequest("Missing text")
    article_text = f"{title} {body}"
    detected_lang, confidence = predict_lang(article_text)
    return {
        'language': detected_lang,
        'confidence': confidence,
        'error': 'Not an error',
        'version': '0.0.1'
    }


def process_request():
    return FlaskAppWrapper.process_json(predict, schema=schema) 


def run_app():
    routes = [
        {
            "endpoint": "/",
            "callable": process_request,
            "methods": ["POST"]
        }
    ]
    return FlaskAppWrapper.create_app(routes)

Note that the FlaskAppWrapper accepts a callable in the process_json , and if you'd like to load a classifier or model.bin in your memory you could modify it like below 👇

class ClassifyHandler:
    def __init__(self):
        self.classifier = Classifier() #this is the classifier to load , or a binary in local file storage

    def __call__(self, text):
        return self.classifier.predict(text)


def run_app():
    classify_handler = ClassifyHandler()
    routes = [
        {
            "endpoint": "/classify",
            "callable": classify_handler,
            "methods": ["POST"]
        }
    ]
    return FlaskAppWrapper.create_app(routes)
    

Metadata

Release files for model-serving 2.0.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for model-serving 2.0.6
File Size Uploaded Provenance Actions
model-serving-2.0.6.tar.gz 11.7 kB Without provenance Details for model-serving-2.0.6.tar.gz

Release files / model-serving-2.0.6.tar.gz

Download URL model-serving-2.0.6.tar.gz
Size 11.7 kB
Tags Source
SHA-256 checksum
How to use checksums
88f67e4b5e0c5bd5e61d07ea82a0790198f7d39bbd429abc87604dd41a428974
BLAKE2b-256 checksum
How to use checksums
8ad7faa7b78c118d8595293ce3193c51946c4eab53fcba454207ff04ed5639b2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.12

Security for model-serving 2.0.6

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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page