-
Notifications
You must be signed in to change notification settings - Fork 0
/
ml_server.py
40 lines (30 loc) · 928 Bytes
/
ml_server.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
import json
import tensorflow as tf
import numpy as np
import random
from flask import Flask, request
app = Flask(__name__)
model = tf.keras.models.load_model('model.h5')
feature_model = tf.keras.models.Model(
model.inputs,
[layer.output for layer in model.layers]
)
_, (x_test, _) = tf.keras.datasets.mnist.load_data()
x_test = x_test / 255.
def get_prediction():
index = np.random.choice(x_test.shape[0])
image = x_test[index, :, :]
image_arr = np.reshape(image, (1,784))
return feature_model.predict(image_arr), image
@app.route('/', methods=['GET','POST'])
def index():
if request.method == 'POST':
preds, image = get_prediction()
final_preds = [p.tolist() for p in preds]
return json.dumps({
'prediction': final_preds,
'image': image.tolist()
})
return 'Welcome to the model server!'
if __name__ == '__main__':
app.run()