Skip to content

Instantly share code, notes, and snippets.

@deepak-karkala
Created December 21, 2020 06:35
Show Gist options
  • Save deepak-karkala/d51c41cad25ea0363708378c5595b791 to your computer and use it in GitHub Desktop.
Save deepak-karkala/d51c41cad25ea0363708378c5595b791 to your computer and use it in GitHub Desktop.
Serve Model predictions using REST API
# Serve Model predictions using REST API
# Predict and return JSON data
@app.route("/predict", methods=["POST"])
def predict():
# initialize the data dictionary that will be returned from the view
data = {"success": False}
# ensure an image was properly uploaded to our endpoint
if flask.request.method == "POST":
if flask.request.files.get("image"):
data["predictions"] = []
# Read input as image
image = flask.request.files["image"].read()
image = Image.open(io.BytesIO(image))
# Run inference and get prediction
seg_mask, seg_product_category = get_model_output_json(image)
# Add prediction results to JSON data
r = {"label": seg_product_category}
data["predictions"].append(r)
# indicate that the request was a success
data["success"] = True
# return the data dictionary as a JSON response
return flask.jsonify(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment