-
Notifications
You must be signed in to change notification settings - Fork 8
/
app_v1_1.py
55 lines (40 loc) · 1.65 KB
/
app_v1_1.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
# -*- 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, render_template
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('/', methods=['GET', 'POST'])
def predict():
if request.method == 'POST':
word = request.form.get('word')
model_name = request.form.get('model')
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)
return render_template('results.html', model=model_name, fr=round(100*prediction[0, 0],2),
en=round(100*prediction[0, 1],2), es=round(100*prediction[0, 2],2))
return render_template('app.html')
if __name__ == '__main__':
print('Loading model...')
load()
app.run(debug=False)