-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathserver.py
63 lines (45 loc) · 1.4 KB
/
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import flask
import heapq
import uuid
import os
import numpy as np
import errno
import tensorflow as tf
from keras.models import load_model
from process_audio import process_track
app = flask.Flask(__name__)
app.config['MAX_CONTENT_LENGTH'] = 30 * 1024 * 1024
model = load_model('models/model.hdf5')
graph = tf.get_default_graph()
@app.route("/rate", methods=["POST"])
def rating():
audio = flask.request.files.get("audio")
if audio is None:
return 'Missing required parameter: audio', 400
audio_id = uuid.uuid4().hex
filename = 'audio/' + audio_id + '.mp3'
audio.save(filename)
try:
slices = process_track(audio_id, store_file=False)
except:
return 'Could not process provided file', 400
finally:
os.remove(filename)
if slices is None:
return 'Audio must be longer than 30 seconds', 400
x = np.empty((len(slices), 1, 128, 1291))
for i in range(len(slices)):
x[i, ] = slices[i]/255.
global graph
with graph.as_default():
predictions = [pred[0] for pred in model.predict(x)]
best_predictions = heapq.nlargest(5, predictions)
score = (sum(best_predictions) / len(best_predictions)) * 100
return str('{:.2f}'.format(score))
if __name__ == "__main__":
try:
os.makedirs('audio')
except OSError as e:
if e.errno != errno.EEXIST:
raise
app.run()