-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathwsgi.py
More file actions
123 lines (90 loc) · 3.47 KB
/
wsgi.py
File metadata and controls
123 lines (90 loc) · 3.47 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import os
from flask import Flask, request, flash, redirect
from flask_cors import CORS
import tensorflow as tf
from tensorflow.python.keras.backend import set_session
from microfaune_package.microfaune.detection import RNNDetector
import json
import time
tf_config = tf.ConfigProto(
intra_op_parallelism_threads=1,
allow_soft_placement=True
)
sess = tf.Session(config=tf_config)
graph = tf.get_default_graph()
app = Flask(__name__)
cors = CORS(app)
# get env var
app.config['SECRET_KEY'] = os.environ['SECRET_KEY']
app.config['UPLOAD_FOLDER'] = os.environ['UPLOAD_FOLDER']
def load_my_model():
print("* Loading model...")
global model
print(model)
model = RNNDetector('models/model_weights-20190919_220113.h5')
model = None
load_my_model()
def is_allowed_file(filename):
""" Checks if a filename's extension is acceptable """
allowed_ext = filename.rsplit('.', 1)[1].lower() in ['wav', 'mp3', 'flac', 'ogg']
return '.' in filename and allowed_ext
# def predict(filename):
# filepath = os.path.join(os.path.dirname(__file__), app.config['UPLOAD_FOLDER'], filename)
# pred = model.predict_on_wav(filepath)
# return pred
@app.route('/static/*', methods=['GET'])
def get_static():
print('asdfasdfasdfasdfasdf')
return 'ff'
@app.route('/', methods=['GET', 'POST'])
def home():
if request.method == 'GET':
return "Hello"
if request.method == 'POST':
print("================================================")
t0 = time.time()
if 'file' not in request.files:
flash('Please choose a file to upload')
return redirect(request.url)
audio_file = request.files['file']
print('a', time.time() - t0)
if audio_file.filename == '':
flash('Please choose a file to upload')
return redirect(request.url)
if audio_file:
print('a', audio_file.filename)
if is_allowed_file(audio_file.filename):
passed = False
try:
print('b', time.time() - t0)
filename = audio_file.filename
filepath = os.path.join(os.path.dirname(__file__),
app.config['UPLOAD_FOLDER'], filename)
audio_file.save(filepath)
print('c', time.time() - t0)
passed = True
except Exception:
passed = False
if passed:
filepath = os.path.join(os.path.dirname(__file__), app.config['UPLOAD_FOLDER'], filename)
with graph.as_default():
set_session(sess)
pred = model.predict_on_ogg(filepath)
print('d', time.time() - t0)
# os.remove(filepath)
return json.dumps(pred[1].tolist())
# return redirect(url_for('predict', filename=filename))
else:
return 'asdf'
else:
flash('Choose a wav file.')
return redirect(request.url)
else:
flash('An error occurred, try again.')
return redirect(request.url)
# if this is the main thread of execution first load the model and
# then start the server
if __name__ == "__main__":
print(("* Loading Keras model and Flask starting server..."
"please wait until server has fully started"))
app.run()