osanseviero commited on
Commit
13efc84
·
1 Parent(s): 76cf08e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +132 -0
app.py ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoTokenizer, AutoModelForCausalLM
2
+
3
+ tokenizer = AutoTokenizer.from_pretrained("TristanBehrens/js-fakes-4bars")
4
+ model = AutoModelForCausalLM.from_pretrained("TristanBehrens/js-fakes-4bars")
5
+
6
+ import note_seq
7
+
8
+ NOTE_LENGTH_16TH_120BPM = 0.25 * 60 / 120
9
+ BAR_LENGTH_120BPM = 4.0 * 60 / 120
10
+ SAMPLE_RATE=44100
11
+
12
+ def token_sequence_to_note_sequence(token_sequence, use_program=True, use_drums=True, instrument_mapper=None, only_piano=False):
13
+ if isinstance(token_sequence, str):
14
+ token_sequence = token_sequence.split()
15
+ note_sequence = empty_note_sequence()
16
+
17
+ # Render all notes.
18
+ current_program = 1
19
+ current_is_drum = False
20
+ current_instrument = 0
21
+ track_count = 0
22
+ for token_index, token in enumerate(token_sequence):
23
+
24
+ if token == "PIECE_START":
25
+ pass
26
+ elif token == "PIECE_END":
27
+ print("The end.")
28
+ break
29
+ elif token == "TRACK_START":
30
+ current_bar_index = 0
31
+ track_count += 1
32
+ pass
33
+ elif token == "TRACK_END":
34
+ pass
35
+ elif token == "KEYS_START":
36
+ pass
37
+ elif token == "KEYS_END":
38
+ pass
39
+ elif token.startswith("KEY="):
40
+ pass
41
+ elif token.startswith("INST"):
42
+ instrument = token.split("=")[-1]
43
+ if instrument != "DRUMS" and use_program:
44
+ if instrument_mapper is not None:
45
+ if instrument in instrument_mapper:
46
+ instrument = instrument_mapper[instrument]
47
+ current_program = int(instrument)
48
+ current_instrument = track_count
49
+ current_is_drum = False
50
+ if instrument == "DRUMS" and use_drums:
51
+ current_instrument = 0
52
+ current_program = 0
53
+ current_is_drum = True
54
+ elif token == "BAR_START":
55
+ current_time = current_bar_index * BAR_LENGTH_120BPM
56
+ current_notes = {}
57
+ elif token == "BAR_END":
58
+ current_bar_index += 1
59
+ pass
60
+ elif token.startswith("NOTE_ON"):
61
+ pitch = int(token.split("=")[-1])
62
+ note = note_sequence.notes.add()
63
+ note.start_time = current_time
64
+ note.end_time = current_time + 4 * NOTE_LENGTH_16TH_120BPM
65
+ note.pitch = pitch
66
+ note.instrument = current_instrument
67
+ note.program = current_program
68
+ note.velocity = 80
69
+ note.is_drum = current_is_drum
70
+ current_notes[pitch] = note
71
+ elif token.startswith("NOTE_OFF"):
72
+ pitch = int(token.split("=")[-1])
73
+ if pitch in current_notes:
74
+ note = current_notes[pitch]
75
+ note.end_time = current_time
76
+ elif token.startswith("TIME_DELTA"):
77
+ delta = float(token.split("=")[-1]) * NOTE_LENGTH_16TH_120BPM
78
+ current_time += delta
79
+ elif token.startswith("DENSITY="):
80
+ pass
81
+ elif token == "[PAD]":
82
+ pass
83
+ else:
84
+ #print(f"Ignored token {token}.")
85
+ pass
86
+
87
+ # Make the instruments right.
88
+ instruments_drums = []
89
+ for note in note_sequence.notes:
90
+ pair = [note.program, note.is_drum]
91
+ if pair not in instruments_drums:
92
+ instruments_drums += [pair]
93
+ note.instrument = instruments_drums.index(pair)
94
+
95
+ if only_piano:
96
+ for note in note_sequence.notes:
97
+ if not note.is_drum:
98
+ note.instrument = 0
99
+ note.program = 0
100
+
101
+ return note_sequence
102
+
103
+ def empty_note_sequence(qpm=120.0, total_time=0.0):
104
+ note_sequence = note_seq.protobuf.music_pb2.NoteSequence()
105
+ note_sequence.tempos.add().qpm = qpm
106
+ note_sequence.ticks_per_quarter = note_seq.constants.STANDARD_PPQ
107
+ note_sequence.total_time = total_time
108
+ return note_sequence
109
+
110
+ def process(text):
111
+ input_ids = tokenizer.encode(text, return_tensors="pt")
112
+ generated_ids = model.generate(input_ids, max_length=500)
113
+ generated_sequence = tokenizer.decode(generated_ids[0])
114
+ print(generated_sequence)
115
+
116
+ # Convert text of notes to audio
117
+ note_sequence = token_sequence_to_note_sequence(generated_sequence)
118
+ synth = note_seq.midi_synth.synthesize
119
+ array_of_floats = synth(note_sequence, sample_rate=SAMPLE_RATE)
120
+ return SAMPLE_RATE, array_of_floats
121
+
122
+ title = "Music generation with GPT-2"
123
+
124
+ iface = gr.Interface(
125
+ fn=process,
126
+ inputs=[gr.inputs.Textbox(placeholder="PIECE_START")],
127
+ outputs='audio',
128
+ title=title,
129
+ examples=[["PIECE_START"], ["PIECE_START STYLE=JSFAKES GENRE=JSFAKES TRACK_START INST=48 BAR_START NOTE_ON=61"]]
130
+ )
131
+
132
+ iface.launch(debug=True)