Using the ML Model Base Class

Using Flask to deploy an ML model

Image for post
Image for post


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
- 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:
bootstrap = Bootstrap(app)

Model Manager Class

class ModelManager(object):
_models = []
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.”)
pip install git+
>>> 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}]
def instantiate_model_manager():
model_manager = ModelManager()

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() {
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);
Image for post
Image for post
The prediction page of the web app


spec = APISpec(
title=’Model Service’,
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():
Image for post
Image for post
Swagger UI view of the OpenAPI specification created by the script.


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