-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.py
142 lines (106 loc) · 3.85 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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import os
from flask import Flask, jsonify, render_template, request, redirect, url_for, send_from_directory
from werkzeug.utils import secure_filename
import json
import numpy as np
from genre_detector import GenreDetector
from helper import get_genre_distribution_over_time
# allow YouTube download if necessary libraries are present
import imp
try:
imp.find_module('pafy')
imp.find_module('youtube_dl')
allow_yt = True
except ImportError:
allow_yt = False
if allow_yt:
import pafy
UPLOADS_PATH = 'uploads'
EXAMPLES_PATH = 'examples'
ALLOWED_EXTENSIONS = set(['mp3', 'wav', 'webm', 'ogg'])
MODEL_PATH = 'models/crnn_model.yaml'
WEIGHTS_PATH = 'models/crnn_weights.h5'
app = Flask(__name__, static_folder='assets')
if not os.path.exists(UPLOADS_PATH):
os.makedirs(UPLOADS_PATH)
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def predictions_to_json(filename):
path = os.path.join(UPLOADS_PATH, filename)
genre_detector = GenreDetector(MODEL_PATH, WEIGHTS_PATH)
predictions, duration = genre_detector.detect_realtime(path)
merged_predictions = genre_detector.detect_merged()
genre_distributions = get_genre_distribution_over_time(
predictions, duration, merged_predictions)
json_path = os.path.join(UPLOADS_PATH, filename + '.json')
with open(json_path, 'w') as f:
json_data = json.dumps(genre_distributions)
f.write(json_data)
return json_data
def download_yt_audio(url):
video = pafy.new(url)
title = secure_filename(video.title)
bestaudio = video.getbestaudio(preftype='webm')
filename = title + '.' + bestaudio.extension
path = os.path.join(UPLOADS_PATH, filename)
bestaudio.download(filepath=path)
json_data = predictions_to_json(filename)
return filename, json_data
@app.route('/', methods=['GET'])
def index():
return render_template('index.html', allow_yt = allow_yt)
@app.route('/upload', methods=['POST'])
def upload_file():
uploaded_file = request.files['file']
if not allowed_file(uploaded_file.filename):
return 'Unsupported media type', 415
try:
filename = secure_filename(uploaded_file.filename)
path = os.path.join(UPLOADS_PATH, filename)
uploaded_file.save(path)
json_data = predictions_to_json(filename)
except:
return 'Internal server error', 500
return redirect(url_for('play',
filename=filename,
json_data=json_data))
if allow_yt:
@app.route('/yt_download', methods=['POST'])
def yt_download():
url = request.form.get('youtube_url')
try:
filename, json_data = download_yt_audio(url)
except:
return 'Internal server error', 500
return redirect(url_for('play',
filename=filename,
json_data=json_data))
@app.route('/play', methods=['GET'])
def play():
data = {
'filename': request.args.get('filename'),
'json_data': request.args.get('json_data')
}
return jsonify(data)
@app.route('/uploads/<filename>')
def uploaded_file(filename):
# TODO: On ocacssions, connection is being reset by peer;
# find a way to handle this
return send_from_directory(UPLOADS_PATH,
filename)
@app.route('/examples', methods=['GET'])
def examples():
return render_template('examples.html')
@app.route('/examples/<filename>')
def example(filename):
return send_from_directory(EXAMPLES_PATH,
filename)
@app.errorhandler(404)
def page_not_fount(e):
return render_template('page_not_found.html')
@app.route('/error')
def internal_server_error():
return render_template('error.html')
if __name__ == '__main__':
app.run(port=8080, debug=True)