-
Notifications
You must be signed in to change notification settings - Fork 8
/
app_v2.py
58 lines (43 loc) · 1.63 KB
/
app_v2.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
55
56
57
58
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 18 11:02:30 2019
@author: Yan
"""
# best ressources are https://scotch.io/bar-talk/processing-incoming-request-data-in-flask
# https://blog.keras.io/building-a-simple-keras-deep-learning-rest-api.html?source=post_page
# https://towardsdatascience.com/deploying-a-keras-deep-learning-model-as-a-web-application-in-p-fc0f2354a7ff
from generate_data import word_to_array
from keras.models import load_model
from flask import Flask, request, jsonify
import tensorflow as tf
app = Flask(__name__)
def load():
global models
models = {}
models['CNN'] = load_model('modelCNN.hdf5')
models['RNN'] = load_model('modelRNN.hdf5')
models['FF'] = load_model('modelFF.hdf5')
global graph
graph = tf.get_default_graph()
@app.route('/')
def predict():
data = {}
word = request.args.get('word')
data['word'] = word
model_name = request.args.get('model')
data['model'] = model_name
model = models[model_name]
if model_name == 'FF':
arr = word_to_array(word).reshape(1,26*12)
else:
arr = word_to_array(word).reshape(1,26,12)
with graph.as_default():
prediction = model.predict(arr)
data['Francais'] = round(100*prediction[0, 0],2)
data['English'] = round(100*prediction[0, 1],2)
data['Espanol'] = round(100*prediction[0, 2],2)
return jsonify(data)
if __name__ == '__main__':
print('Loading model...')
load()
app.run(debug=False)