Using the ML Model Base Class

Using Flask to deploy an ML model

Image for post
Image for post

Introduction

When creating software, interacting with a component through an abstraction makes the code easier to understand and evolve.

Flask Web Application

- model_service
- static ( folder containing the static web assets )
- templates ( folder for the html templates
- __init__.py
- config.py
- endpoints.py
- model_manager.py
- schemas.py
- views.py
- scripts ( folder containing scripts )
- tests ( folder containing the unit test suite)
- requirements.txt
- test_requirements.txt
app = Flask(__name__)
if os.environ.get(“APP_SETTINGS”) is not None:
app.config.from_object(os.environ[‘APP_SETTINGS’])
bootstrap = Bootstrap(app)

Model Manager Class

class ModelManager(object):
_models = []
@classmethod
def load_models(cls, configuration):
for c in configuration:
model_module = importlib.import_module(c[“module_name”])
model_class = getattr(model_module, c[“class_name”])
model_object = model_class()

if isinstance(model_object, MLModel) is False:
raise ValueError(“The ModelManager can only hold references to objects of type MLModel.”)
cls._models.append(model_object)
pip install git+https://github.com/schmidtbri/ml-model-abc-improvements
>>> from model_service.model_manager import ModelManager>>> model_manager = ModelManager()>>> model_manager.load_models(configuration=[{“module_name”: “iris_model.iris_predict”,”class_name”: “IrisModel”}])>>> model_manager.get_models()[{‘display_name’: ‘Iris Model’, ‘qualified_name’: ‘iris_model’, ‘description’: ‘A machine learning model for predicting the species of a flower based on its measurements.’, ‘major_version’: 0, ‘minor_version’: 1}]
@app.before_first_request
def instantiate_model_manager():
model_manager = ModelManager()
model_manager.load_models(configuration=app.config[“MODELS”])

Flask REST Endpoints

@app.route(“/api/models”, methods=[‘GET’])
def get_models():
model_manager = ModelManager()
models = model_manager.get_models()
response_data = model_collection_schema.dumps(dict(models=models)).data
return response_data, 200

Flask Views

@app.route(‘/’, methods=[‘GET’])
def index():
model_manager = ModelManager()
models = model_manager.get_models()
return render_template(‘index.html’, models=models)
Image for post
Image for post
The index page of the web app.
Image for post
Image for post
The metadata page of the web app.

Dynamic Web Form

@app.route(“/models/<qualified_name>/predict”, methods=[‘GET’])
def display_form(qualified_name):
model_manager = ModelManager()
model_metadata = model_manager.get_model_metadata(qualified_name=qualified_name)
return render_template(‘predict.html’, model_metadata=model_metadata)
$(document).ready(function() {
$.ajax({
url: ‘/api/models/{{ model_metadata.qualified_name }}/metadata’,
success: function(data) {
var container = document.getElementById(‘prediction_form’);
var BrutusinForms = brutusin[“json-forms”];
bf = BrutusinForms.create(data.input_schema);
bf.render(container);
}
Image for post
Image for post
The prediction page of the web app

Documentation

spec = APISpec(
openapi_version=”3.0.2",
title=’Model Service’,
version=’0.1.0',
info=dict(description=__doc__),
plugins=[FlaskPlugin(), MarshmallowPlugin()],
)
spec.components.schema(“ModelSchema”, schema=ModelSchema)
spec.components.schema(“ModelCollectionSchema”, schema=ModelCollectionSchema)
spec.components.schema(“JsonSchemaProperty”, schema=JsonSchemaProperty)
spec.components.schema(“JSONSchema”, schema=JSONSchema)
spec.components.schema(“ModelMetadataSchema”, schema=ModelMetadataSchema)
spec.components.schema(“ErrorSchema”, schema=ErrorSchema)
with app.test_request_context():
spec.path(view=get_models)
spec.path(view=get_metadata)
spec.path(view=predict)
Image for post
Image for post
Swagger UI view of the OpenAPI specification created by the openapi.py script.

Conclusion

Coder and machine learning enthusiast

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store