Skip to content

Instantly share code, notes, and snippets.

@nwatab
Last active September 9, 2020 10:37
Show Gist options
  • Save nwatab/7ddc9742f63e4c2be1a9a0b9fd9ed2f1 to your computer and use it in GitHub Desktop.
Save nwatab/7ddc9742f63e4c2be1a9a0b9fd9ed2f1 to your computer and use it in GitHub Desktop.
Flask API requests tensorflow serving minimum example
import json
from flask import Flask, request, jsonify
import requests
app = Flask(__name__)
@app.route('/predict', methods=['POST'])
def predict():
if request.method == 'POST':
headers = {"content-type": "application/json"}
data = {
'instances': np.random.rand(8, 224, 224, 3).tolist()
}
data = json.dumps(data)
predictions = requests.post('http://localhost:8501/v1/models/yourmodel:predict', data=data, headers=headers)
return jsonify(predictions)
if __name__ == '__main__':
app.run()
docker pull tensorflow/serving
docker run -p 8500:8500 -p 8501:8501 \
--mount type=bind,source=/abusolute/path/to/saved_model/,target=/models/yourmodel \
-e MODEL_NAME=yourmodel -t tensorflow/serving
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment