-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathml_server.py
More file actions
33 lines (26 loc) · 934 Bytes
/
ml_server.py
File metadata and controls
33 lines (26 loc) · 934 Bytes
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
import json
import tensorflow as tf
import numpy as np
import os
import random
import string
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 ml server'
if __name__ == '__main__':
app.run()