def predict(qualified_name): """ endpoint that uses a model to make a prediction --- get: parameters: - in: path name: qualified_name schema: type: string required: true description: The qualified name of the model being used for prediction. responses: 200: description: Prediction is succesful. The schema of the body of the response is described by the model's output schema. 400: description: Input is not valid JSON or does not meet the model's input schema. content: application/json: schema: ErrorSchema 404: description: Model not found. content: application/json: schema: ErrorSchema 500: description: Server error. content: application/json: schema: ErrorSchema """ # attempting to deserialize JSON in body of request try: data = json.loads(request.data) except json.decoder.JSONDecodeError as e: response = dict(type="DESERIALIZATION_ERROR", message=str(e)) response_data = error_schema.dumps(response).data return Response(response_data, status=400, mimetype='application/json') # getting the model object from the Model Manager model_manager = ModelManager() model_object = model_manager.get_model(qualified_name=qualified_name) # returning a 404 if model is not found if model_object is None: response = dict(type="ERROR", message="Model not found.") response_data = error_schema.dumps(response).data return Response(response_data, status=404, mimetype='application/json') try: prediction = model_object.predict(data) return jsonify(prediction), 200 except MLModelSchemaValidationException as e: # responding with a 400 if the schema does not meet the model's input schema response = dict(type="SCHEMA_ERROR", message=str(e)) response_data = error_schema.dumps(response).data return Response(response_data, status=400, mimetype='application/json') except Exception as e: response = dict(type="ERROR", message="Could not make a prediction.") response_data = error_schema.dumps(response).data return Response(response_data, status=500, mimetype='application/json')
def display_form(qualified_name): """ view that displays the prediction form of a model """ model_manager = ModelManager() model_metadata = model_manager.get_model_metadata( qualified_name=qualified_name) return render_template('predict.html', model_metadata=model_metadata)
def get_metadata(qualified_name): """ Metadata about one model --- get: parameters: - in: path name: qualified_name schema: type: string required: true description: The qualified name of the model for which metadata is being requested. responses: 200: description: Metadata about one model content: application/json: schema: ModelMetadataSchema 404: description: Model not found. content: application/json: schema: ErrorSchema """ model_manager = ModelManager() metadata = model_manager.get_model_metadata(qualified_name=qualified_name) if metadata is not None: response_data = model_metadata_schema.dumps(metadata).data return Response(response_data, status=200, mimetype='application/json') else: response = dict(type="ERROR", message="Model not found.") response_data = error_schema.dumps(response).data return Response(response_data, status=400, mimetype='application/json')
def index(): """ view that displays a list of models """ # instantiating ModelManager singleton model_manager = ModelManager() # retrieving the model object from the model manager models = model_manager.get_models() return render_template('index.html', models=models)
def display_metadata(qualified_name): """ view that displays metadata about a model """ model_manager = ModelManager() model_metadata = model_manager.get_model_metadata( qualified_name=qualified_name) if model_metadata is not None: return render_template('metadata.html', model_metadata=model_metadata) else: return render_template('model_not_found.html')
def test2(self): """ testing that the ModelManager will return the same instance of an MLModel class from several different instances of ModelManager """ # arrange # instantiating the model manager class first_model_manager = ModelManager() # loading the MLModel objects from configuration first_model_manager.load_models( configuration=[{ "module_name": "iris_model.iris_predict", "class_name": "IrisModel" }]) # act first_model_object = first_model_manager.get_model( qualified_name="iris_model") # instantiating the ModelManager class again second_model_manager = ModelManager() second_model_object = second_model_manager.get_model( qualified_name="iris_model") # assert self.assertTrue(str(first_model_object) == str(second_model_object))
def get_models(): """ List of models available --- get: responses: 200: description: List of model available content: application/json: schema: ModelCollectionSchema """ # instantiating ModelManager singleton model_manager = ModelManager() # retrieving the model object from the model manager models = model_manager.get_models() response_data = model_collection_schema.dumps(dict(models=models)).data return response_data, 200
def test3(self): """ testing that the ModelManager only allows MLModel objects to be stored """ # arrange model_manager = ModelManager() # act exception_raised = False exception_message = "" try: model_manager.load_models(configuration=[{ "module_name": "tests.model_manager_test", "class_name": "SomeClass" # using the class defined at the top of this file to test }]) except Exception as e: exception_raised = True exception_message = str(e) # assert self.assertTrue(exception_raised) self.assertTrue( exception_message == "The ModelManager can only hold references to objects of type MLModel." )
def test1(self): """ testing the load_models() method """ # arrange # instantiating the model manager class model_manager = ModelManager() # loading the MLModel objects from configuration model_manager.load_models(configuration=[{ "module_name": "iris_model.iris_predict", "class_name": "IrisModel" }]) # act exception_raised = False model_object = None # accessing the IrisModel model object try: model_object = model_manager.get_model(qualified_name="iris_model") except Exception as e: exception_raised = True print_tb(e) # assert self.assertFalse(exception_raised) self.assertTrue(model_object is not None)
def instantiate_model_manager(): """ This function runs at application startup it loads all of the model found in the configuration """ model_manager = ModelManager() model_manager.load_models(configuration=app.config["MODELS"])