-
Notifications
You must be signed in to change notification settings - Fork 6
/
server_application.py
54 lines (48 loc) · 2 KB
/
server_application.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# -*- coding: utf-8 -*-
""" Flask webservice application accepting HTTP POST request with JSON data
corresponding to a list of MNIST image to be classified.
See https://github.com/JoelKronander/TensorFlask for a detailed specification of
the JSON data excepted for the request.
Example:
The webservice can for example be deployed using gunicorn:
$gunicorn server:app
"""
from flask import Flask, request, jsonify
from mnist_classifiers import MNISTClassifier, MNISTClassifierInputError
#The maximum number of images to handle in a single request
#Should be set to reflect the memory and computational resources of the
#machine the server is running on.
MAX_BATCH_SIZE = 64
#Create an instance of a MNISTClassifier that initizalizes and controls the
#state of the tensorflow graph
REQUEST_HANDLER = MNISTClassifier(max_batch_size=MAX_BATCH_SIZE)
#Create the Flask application handling HTTP requests
app = Flask(__name__)
@app.route('/mnist/classify', methods=['POST'])
def classify_mnist_images():
"""Unpacks the JSON data passed with the POST request and forwards it to the
MNISTClassifier for classification"""
if request.method == 'POST':
resp = jsonify([])
try:
classifications = REQUEST_HANDLER.classify(request.json['requests'])
data = {
'responses' : classifications,
}
resp = jsonify(data)
resp.status_code = 200
return resp
except MNISTClassifierInputError as excep:
resp = bad_input("Invalid input detected: {}"
.format(excep))
return resp
#Handle Internal Server Errors
except Exception as excep:
resp = bad_input("Unexpected server API error: {}"
.format(excep))
return resp
def bad_input(message):
"""Returns a 404 status code JSON response with the provided message"""
response = jsonify({'message': message})
response.status_code = 404
return response