Skip to content

Instantly share code, notes, and snippets.

@himanshurawlani
Last active October 19, 2018 23:05
Show Gist options
  • Save himanshurawlani/f543b32b5d4a4e19f3ab7986cb80ab71 to your computer and use it in GitHub Desktop.
Save himanshurawlani/f543b32b5d4a4e19f3ab7986cb80ab71 to your computer and use it in GitHub Desktop.
Flask server to be used on top of TensorFlow Serving server
import base64
import json
from io import BytesIO
import numpy as np
import requests
from flask import Flask, request, jsonify
from keras.applications import inception_v3
from keras.preprocessing import image
# from flask_cors import CORS
app = Flask(__name__)
# Uncomment this line if you are making a Cross domain request
# CORS(app)
# Testing URL
@app.route('/hello/', methods=['GET', 'POST'])
def hello_world():
return 'Hello, World!'
@app.route('/imageclassifier/predict/', methods=['POST'])
def image_classifier():
# Decoding and pre-processing base64 image
img = image.img_to_array(image.load_img(BytesIO(base64.b64decode(request.form['b64'])),
target_size=(224, 224))) / 255.
# this line is added because of a bug in tf_serving(1.10.0-dev)
img = img.astype('float16')
# Creating payload for TensorFlow serving request
payload = {
"instances": [{'input_image': img.tolist()}]
}
# Making POST request
r = requests.post('http://localhost:9000/v1/models/ImageClassifier:predict', json=payload)
# Decoding results from TensorFlow Serving server
pred = json.loads(r.content.decode('utf-8'))
# Returning JSON response to the frontend
return jsonify(inception_v3.decode_predictions(np.array(pred['predictions']))[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment