Flask

  • Use Python logger.
  • Configure the logger and get session once for a worker process.
  • Log predictions in your prediction handler code.

Example

import os
import sys
from tensorflow import keras
import json
from flask import Flask
from flask import request

# Import graphsignal
import graphsignal

# Configure Graphsignal
graphsignal.configure(api_key='my_api_key')

# Get logging session for the model
sess = graphsignal.session(deployment_name='mnist_prod')

model = keras.models.load_model('mnist_model.h5')

app = Flask(__name__)

@app.route('/predict_digit', methods = ['POST'])
def predict_digit():
    features = request.get_json()

    # feature engineering code here...

    output_data = model.predict(input_data)

    # Log prediction output
    sess.log_prediction(features=features, label=output_data[0])

    return json.dumps(output_data.tolist())

app.run(port=8080)