-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
144 lines (116 loc) · 4.67 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
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
143
from flask import Flask, render_template, Response, request
import os
from midi_util import limit_instruments
from dataset import load_all
from constants import styles
from train import train_model
app = Flask(__name__)
num_requests = 0
saved_models = []
config = {}
hidden_layers = []
data = None
@app.route("/dataset", methods=['GET', 'POST'])
def dataset():
global hidden_layers
global data
global config
global saved_models
print("Dataset")
form = request.form.to_dict()
config.update(form)
print(config)
instrument_to_idx = None
if "ips" in config and config["ips"] != '':
instrument_to_idx = limit_instruments(max_instruments_per_song=int(config["ips"]))
else:
instrument_to_idx = limit_instruments()
if "get_num_instruments" in form:
config["num_instruments"] = len(instrument_to_idx) - 1
else:
notes_per_bar = int(config["notes_per_beat"]) * int(config["beats_per_bar"]) * int(config["note_ts"])
seq_len = int(float(config["bars"]) * notes_per_bar)
config["seq_len"] = seq_len
min_note = int(config["min_note"])
max_note = int(config["max_note"])
instruments_per_song = int(config["ips"])
fs = int(config["freq"])
num_instruments = int(config["num_instruments"])
num_notes_instrument = max_note - min_note + 1
config["num_notes"] = (num_instruments + 1) * num_notes_instrument
data = load_all(styles, seq_len, instrument_to_idx, min_note=min_note, max_note=max_note,
instruments_per_song=instruments_per_song, fs=fs, num_instruments=num_instruments)
print(data[0][0].shape)
config["data_shape"] = str(data[0][0].shape)
return render_template('index.html', saved_models=saved_models, config=config, hidden_layers=hidden_layers)
@app.route("/train", methods=['GET', 'POST'])
def train():
global hidden_layers
global saved_models
global config
print("Train")
form = request.form.to_dict()
config.update(form)
print(config)
hidden_layers = []
to_delete = []
for k, v in config.items():
if "layer_" in k:
hidden_layers.append(int(v))
to_delete.append(k)
for x in to_delete:
config.pop(x)
print(hidden_layers)
seq_len = int(config["seq_len"])
num_notes = int(config["num_notes"])
latent_dim = int(config["latent_dim"])
batch_size = int(config["batch_size"])
epochs = int(config["epochs"])
name = config["name"]
generate_every_epoch = int(config["generate_every_epoch"])
beta = config["beta"]
if config["beta"] != "sigmoid" and config["beta"] != "cosine" and config["beta"] != "linear":
beta = float(config["beta"])
learning_rate = float(config["learning_rate"])
optimizer = config["optimizer"]
cvae, _, _, bce_metric, f1_metric, kl_metric = train_model(data, hidden_layers, latent_dim=latent_dim, epochs=epochs,
batch_size=batch_size, name=name,
generate_every_epoch=generate_every_epoch,
beta_strategy=beta, learning_rate=learning_rate,
keras_optimizer=optimizer,
seq_len=seq_len, num_notes=num_notes)
saved_models = []
i = 0
for file in os.listdir("out/models/"):
saved_models.append({"value": i, "name": file})
i += 1
return render_template('index.html', saved_models=saved_models, config=config, hidden_layers=hidden_layers)
@app.route("/generate", methods=['GET', 'POST'])
def generate():
global hidden_layers
global saved_models
global config
print("Generate")
form = request.form.to_dict()
config.update(form)
print(config)
return render_template('index.html', saved_models=saved_models, config=config, hidden_layers=hidden_layers)
@app.route("/", methods=['GET', 'POST'])
def index():
global hidden_layers
global num_requests
global config
global saved_models
num_requests += 1
if num_requests == 1:
i = 0
for file in os.listdir("out/models/"):
saved_models.append({"value": i, "name": file})
i += 1
return render_template('index.html', saved_models=saved_models, config=config, hidden_layers=hidden_layers)
form = request.form.to_dict()
config.update(form)
print(config)
return render_template('index.html', saved_models=saved_models, config=config, hidden_layers=hidden_layers)
if __name__ == '__main__':
app.run()