-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
61 lines (44 loc) · 1.7 KB
/
app.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
from flask import Flask, request, render_template
import joblib
from pandas import DataFrame
from pydub import AudioSegment
import logging
import sys
import os
# absolute path to this file
FILE_DIR = os.path.dirname(os.path.abspath(__file__))
print(FILE_DIR)
model = joblib.load("random_forest.joblib")
encoder = joblib.load("encoder.joblib")
app = Flask(__name__)
app.logger.addHandler(logging.StreamHandler(sys.stdout))
app.logger.setLevel(logging.ERROR)
@app.route('/')
def home():
return render_template('index.html')
@app.route('/predict', methods=['POST'])
def predict():
# Extract input features from the request
request_info = DataFrame({'first':request.form['first'], 'second':request.form['second'], 'third':request.form['third']}, index = [0])
# Preprocess the input features
# Make predictions using the loaded model
prediction = [
request.form['first'],
request.form['second'],
request.form['third'],
model.predict(encoder.transform(request_info))[0]
]
file_names = []
for chord in prediction:
if chord.islower():
file_names.append(chord + '_m')
else:
file_names.append(chord)
filename = "static/audio/progressions/progression" + "".join(file_names) + ".mp3"
progression = AudioSegment.from_mp3("static/audio/" + file_names[0] + ".mp3")
for chord in file_names[1:4]:
progression = progression + AudioSegment.from_mp3("static/audio/" + chord + ".mp3")
progression.speedup().export(os.path.join(FILE_DIR, filename), format="mp3")
return render_template('index.html', prediction=prediction, filename=filename)
if __name__ == '__main__':
app.run(debug=False)