-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.py
241 lines (192 loc) · 7.18 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import os.path
from flask import Flask, request, send_file, jsonify, make_response
from Generation import *
from music21 import *
from exceptions import *
from Dictionary import *
from music21.clef import TrebleClef, BassClef, Treble8vbClef
app = Flask(__name__)
# INITIALIZATION
xml_response_headers = {"Content-Type": "text/xml",
"charset": "utf-8"
}
'''TODO: add a routes to get all notes, clefs, times, and keys available in dictionary'''
'''TODO: add route to get all available datasets'''
'''
try:
self.dic.load_dictionary(self.DATASET)
self.dic.load_list(self.DATASET)
except:
print(
"No dictionary file available for loading. Please run the Extraction.py script before generation or training.")
exit(-998)
'''
@app.route('/datasets', methods=['GET'])
def datasets():
if request.method == 'GET':
dic = Dictionary()
datasets = "datasets"
dataset_folder_names = [os.path.split(os.path.split(dp)[0])[1] for dp, dn, filenames in os.walk(os.path.join(os.getcwd(), datasets)) for f in filenames if
os.path.splitext(f)[1] == '.pth']
return jsonify({'datasets': dataset_folder_names})
@app.route('/clefs', methods=['GET'])
def clefs():
if request.method == 'GET':
dic = Dictionary()
datasets = "datasets"
dataset = request.args.get('dataset')
path = os.path.join(os.getcwd(), os.path.join(datasets, dataset))
dic.load_list(path)
return jsonify({'clefs': [i for i in dic.idx2word if "Clef" in i]})
@app.route('/keys', methods=['GET'])
def keys():
if request.method == 'GET':
dic = Dictionary()
datasets = "datasets"
dataset = request.args.get('dataset')
path = os.path.join(os.getcwd(), os.path.join(datasets, dataset))
dic.load_list(path)
return jsonify({'keys': [i for i in dic.idx2word if "Key" in i]})
@app.route('/times', methods=['GET'])
def times():
if request.method == 'GET':
dic = Dictionary()
datasets = "datasets"
dataset = request.args.get('dataset')
path = os.path.join(os.getcwd(), os.path.join(datasets, dataset))
dic.load_list(path)
return jsonify({'times': [i for i in dic.idx2word if "Time" in i]})
@app.route('/notes', methods=['GET'])
def notes():
if request.method == 'GET':
dic = Dictionary()
datasets = "datasets"
dataset = request.args.get('dataset')
path = os.path.join(os.getcwd(), os.path.join(datasets, dataset))
dic.load_list(path)
return jsonify({'notes': [i for i in dic.idx2word if "Note" in i]})
'''
request looks like:
req = {
dataset: "Good"
length: 100
random_seq_length: 1
songs: 1
temperature: 0.85
abc: "M:?\nV:1 name=?\nK:?\n?"
}
'''
@app.route('/mgen', methods=['POST'])
def mgen():
if request.method == 'POST':
content = request.json
print(content)
'''TODO: check all keys of content and do error handling.'''
DATASETS = "datasets"
content['dataset'] = os.path.join(DATASETS, content['dataset'])
g = Generation(**content)
try:
g.checkDataset()
except DatasetNotFound as dnf:
return jsonify({'error': str(dnf)})
g.loadModel()
g.loadDictionary()
try:
g.checkInitClef()
g.checkInitKey()
g.checkInitTime()
g.checkInitSeq()
except NoteNotFoundInDictionary as nnf:
print(nnf)
return jsonify({'error': str(nnf)})
except ClefNotFoundInDictionary as cnf:
print(cnf)
return jsonify({'error': str(cnf)})
except TimeNotFoundInDictionary as tnf:
print(tnf)
return jsonify({'error': str(tnf)})
except KeyNotFoundInDictionary as knf:
print(knf)
return jsonify({'error': str(knf)})
except:
print("Could not run generation with inputs.")
return jsonify({'error': "could not run generation with inputs."})
print(g.iSeq)
g.generate()
try:
g.save()
except CouldNotSaveInference as e:
print(e)
return jsonify({'error': str(e)})
except CouldNotSaveMidiFile as e:
print(e)
return jsonify({'error': str(e)})
except CouldNotSaveMxlFile as e:
print(e)
return jsonify({'error': str(e)})
except CouldNotSaveTxtFile as e:
print(e)
return jsonify({'error': str(e)})
midi = g.GENERATION_PREFIX+"_1.mid"
mxl = g.GENERATION_PREFIX+"_1.mxl"
if os.path.exists(mxl) and os.path.exists(midi):
mxl_path = mxl.split('\\')[-2:]
midi_path = midi.split('\\')[-2:]
if len(mxl_path) < 2:
mxl_path = mxl.split('/')[-2:]
midi_path = midi.split('/')[-2:]
print("mxl: {}\nmidi: {}".format(mxl_path, midi_path))
return jsonify({
'mxl': mxl_path,
'midi': midi_path
})
else:
print("mxl or midi file does not exist.")
return jsonify({'saved': False})
@app.route('/mxl', methods=['GET'])
def mxl():
if request.method == 'GET':
folder = request.args.get('folder')
file = request.args.get('file')
path = os.path.join(os.getcwd(), os.path.join("outputs", os.path.join(folder, file)))
return send_file(path,
mimetype='application/vnd.recordare.musicxml',
attachment_filename=file,
as_attachment=True)
@app.route('/midi', methods=['GET'])
def midi():
if request.method == 'GET':
folder = request.args.get('folder')
file = request.args.get('file')
path = os.path.join(os.getcwd(), os.path.join("outputs", os.path.join(folder, file)))
return send_file(path, mimetype='audio/midi')
@app.route('/mxl-data', methods=['GET'])
def ex():
folder = request.args.get('folder')
file = request.args.get('file')
path = os.path.join(os.getcwd(), os.path.join("outputs", os.path.join(folder, file)))
print(path)
_current_sheet = converter.parse(path)
#next line throws error
return sheet_to_xml_response(_current_sheet)
def insert_musicxml_metadata(sheet: stream.Stream):
"""
Insert various metadata into the provided XML document
The timesignature in particular is required for proper MIDI conversion
"""
md = metadata.Metadata()
sheet.insert(0, md)
# required for proper musicXML formatting
sheet.metadata.title = 'mgen'
sheet.metadata.composer = 'mgen'
def sheet_to_xml_bytes(sheet: stream.Stream):
"""Convert a music21 sheet to a MusicXML document"""
# first insert necessary MusicXML metadata
insert_musicxml_metadata(sheet)
sheet_to_xml_bytes = musicxml.m21ToXml.GeneralObjectExporter(sheet).parse()
return sheet_to_xml_bytes
def sheet_to_xml_response(sheet: stream.Stream):
"""Generate and send XML sheet"""
xml_sheet_bytes = sheet_to_xml_bytes(sheet)
response = make_response((xml_sheet_bytes, xml_response_headers))
return response