GoogleCloudPlatform / data-science-on-gcp

Source code accompanying book: Data Science on the Google Cloud Platform, Valliappa Lakshmanan, O'Reilly 2017
Apache License 2.0
1.31k stars 715 forks source link

Ch9 - Call predictions using Flask webserver #172

Closed sujituk closed 6 months ago

sujituk commented 6 months ago

Background: I have this [https://github.com/GoogleCloudPlatform/data-science-on-gcp/blob/edition2/09_vertexai/flights_model_tf2.ipynb] running and model saved as .keras file. I have python flask program to gload model and trying to get predictions out of the .keras file by providing the input (JSON type)

Code:

import json
import pandas as pd
import numpy as np

import tensorflow as tf
from flask import Flask, jsonify, request
from tensorflow import keras

model = tf.keras.models.load_model("my_model.keras")

app = Flask(__name__)

@app.route("/predict", methods=["POST"])
def index():
    data = request.get_json()
    prediction = model.predict(data)
    return jsonify({"prediction": prediction})

if __name__ == "__main__":
    app.run(host="0.0.0.0", port="8080")

Hitting the /predict endpoint using curl:

curl --header "Content-Type: application/json" -X POST http://127.0.0.1:8080/predict -d @model/sample-payload.json

Payload:

{
    "instances":
    [
        {
            "dep_hour": 2,
            "is_weekday": 1,
            "dep_delay": 40,
            "taxi_out": 17,
            "distance": 41,
            "carrier": "AS",
            "dep_airport_lat": 58.42527778,
            "dep_airport_lon": -135.7075,
            "arr_airport_lat": 58.35472222,
            "arr_airport_lon": -134.57472222,
            "origin": "GST",
            "dest": "JNU"
        }
    ]
}

Issue: While calling predict(), I get an error:

Exception has occurred: ValueError
Failed to find data adapter that can handle input: (<class 'list'> containing values of types {'(<class \'dict\'> containing {"<class \'str\'>"} keys and {\'(<class \\\'list\\\'> containing values of types {\\\'(<class \\\\\\\'dict\\\\\\\'> containing {"<class \\\\\\\'str\\\\\\\'>"} keys and {"<class \\\\\\\'str\\\\\\\'>", "<class \\\\\\\'int\\\\\\\'>", "<class \\\\\\\'float\\\\\\\'>"} values)\\\'})\'} values)'}), <class 'NoneType'>

Tried converting to np array, df as well. I guess i need to cast the input to a model.inputs type, but not sure of how-to.

Any suggestions / example to provide the input instance?

sujituk commented 6 months ago

Not required anymore