File size: 1,243 Bytes
d896bd4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from flask import Flask, render_template, jsonify, request
import json
import subprocess

app = Flask(__name__)


@app.route('/')
def index():
    return render_template('index.html')


@app.route('/generate_midi', methods=['POST'])
def generate_midi():
    data = request.json
    keep_tonality = data.get('keepTonality', False)
    use_grid = data.get('useGrid', True)

    arguments = ['python3', 'generate.py', 'models/LMD2/', 'static/',
                 '--n', '1', '--no_audio']

    if keep_tonality:
        arguments.extend(['--z_file', 'static/0/z'])

    if use_grid:
        arguments.extend(['--s_file', 'static/0/structure.json'])

    subprocess.run(arguments, capture_output=True, text=True)

    filename = "generated.mid"
    return jsonify({"filename": filename})


@app.route('/save_json', methods=['POST'])
def save_json():
    data = request.json
    filepath = 'static/0/structure.json'

    with open(filepath, 'w') as file:
        json.dump(data, file)

    return jsonify(message="JSON saved successfully!")


@app.route('/load_grid')
def load_grid():
    with open('static/0/structure.json', 'r') as file:
        data = json.load(file)
    return jsonify(data)


if __name__ == '__main__':
    app.run(debug=True)