asigalov61 commited on
Commit
a44bbef
1 Parent(s): 29230bd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -89
app.py CHANGED
@@ -23,15 +23,16 @@ in_space = os.getenv("SYSTEM") == "spaces"
23
  # =================================================================================================
24
 
25
  @spaces.GPU
26
- def GenerateMusic(input_title, input_num_tokens, input_prompt_type):
 
27
  print('=' * 70)
28
  print('Req start time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT)))
29
  start_time = reqtime.time()
30
 
31
  print('Loading model...')
32
 
33
- SEQ_LEN = 4096 # Models seq len
34
- PAD_IDX = 2571 # Models pad index
35
  DEVICE = 'cuda' # 'cuda'
36
 
37
  # instantiate the model
@@ -39,10 +40,10 @@ def GenerateMusic(input_title, input_num_tokens, input_prompt_type):
39
  model = TransformerWrapper(
40
  num_tokens = PAD_IDX+1,
41
  max_seq_len = SEQ_LEN,
42
- attn_layers = Decoder(dim = 2048, depth = 8, heads = 16, attn_flash = True)
43
  )
44
 
45
- model = AutoregressiveWrapper(model, ignore_index = PAD_IDX)
46
 
47
  model.to(DEVICE)
48
  print('=' * 70)
@@ -50,7 +51,7 @@ def GenerateMusic(input_title, input_num_tokens, input_prompt_type):
50
  print('Loading model checkpoint...')
51
 
52
  model.load_state_dict(
53
- torch.load('Text_to_Music_Transformer_Medium_Trained_Model_33934_steps_0.6093_loss_0.813_acc.pth',
54
  map_location=DEVICE))
55
  print('=' * 70)
56
 
@@ -66,83 +67,25 @@ def GenerateMusic(input_title, input_num_tokens, input_prompt_type):
66
  print('Done!')
67
  print('=' * 70)
68
 
69
- input_num_tokens = max(8, min(2048, input_num_tokens))
70
-
71
- print('-' * 70)
72
- print('Input title:', input_title)
73
- print('Req num toks:', input_num_tokens)
74
- print('Open-ended prompt:', input_prompt_type)
75
  print('-' * 70)
76
 
77
  #===============================================================================
78
 
79
- print('Setting up model patches and loading helper functions...')
80
-
81
- # @title Setup and load model channels MIDI patches
82
-
83
- model_channel_0_piano_family = "Acoustic Grand" # @param ["Acoustic Grand", "Bright Acoustic", "Electric Grand", "Honky-Tonk", "Electric Piano 1", "Electric Piano 2", "Harpsichord", "Clav"]
84
- model_channel_1_chromatic_percussion_family = "Music Box" # @param ["Celesta", "Glockenspiel", "Music Box", "Vibraphone", "Marimba", "Xylophone", "Tubular Bells", "Dulcimer"]
85
- model_channel_2_organ_family = "Church Organ" # @param ["Drawbar Organ", "Percussive Organ", "Rock Organ", "Church Organ", "Reed Organ", "Accordion", "Harmonica", "Tango Accordion"]
86
- model_channel_3_guitar_family = "Acoustic Guitar(nylon)" # @param ["Acoustic Guitar(nylon)", "Acoustic Guitar(steel)", "Electric Guitar(jazz)", "Electric Guitar(clean)", "Electric Guitar(muted)", "Overdriven Guitar", "Distortion Guitar", "Guitar Harmonics"]
87
- model_channel_4_bass_family = "Fretless Bass" # @param ["Acoustic Bass", "Electric Bass(finger)", "Electric Bass(pick)", "Fretless Bass", "Slap Bass 1", "Slap Bass 2", "Synth Bass 1", "Synth Bass 2"]
88
- model_channel_5_strings_family = "Violin" # @param ["Violin", "Viola", "Cello", "Contrabass", "Tremolo Strings", "Pizzicato Strings", "Orchestral Harp", "Timpani"]
89
- model_channel_6_ensemble_family = "Choir Aahs" # @param ["String Ensemble 1", "String Ensemble 2", "SynthStrings 1", "SynthStrings 2", "Choir Aahs", "Voice Oohs", "Synth Voice", "Orchestra Hit"]
90
- model_channel_7_brass_family = "Trumpet" # @param ["Trumpet", "Trombone", "Tuba", "Muted Trumpet", "French Horn", "Brass Section", "SynthBrass 1", "SynthBrass 2"]
91
- model_channel_8_reed_family = "Alto Sax" # @param ["Soprano Sax", "Alto Sax", "Tenor Sax", "Baritone Sax", "Oboe", "English Horn", "Bassoon", "Clarinet"]
92
- model_channel_9_pipe_family = "Flute" # @param ["Piccolo", "Flute", "Recorder", "Pan Flute", "Blown Bottle", "Skakuhachi", "Whistle", "Ocarina"]
93
- model_channel_10_synth_lead_family = "Lead 8 (bass+lead)" # @param ["Lead 1 (square)", "Lead 2 (sawtooth)", "Lead 3 (calliope)", "Lead 4 (chiff)", "Lead 5 (charang)", "Lead 6 (voice)", "Lead 7 (fifths)", "Lead 8 (bass+lead)"]
94
- model_channel_11_synth_pad_family = "Pad 2 (warm)" # @param ["Pad 1 (new age)", "Pad 2 (warm)", "Pad 3 (polysynth)", "Pad 4 (choir)", "Pad 5 (bowed)", "Pad 6 (metallic)", "Pad 7 (halo)", "Pad 8 (sweep)"]
95
- model_channel_12_synth_effects_family = "FX 3 (crystal)" # @param ["FX 1 (rain)", "FX 2 (soundtrack)", "FX 3 (crystal)", "FX 4 (atmosphere)", "FX 5 (brightness)", "FX 6 (goblins)", "FX 7 (echoes)", "FX 8 (sci-fi)"]
96
- model_channel_13_ethnic_family = "Banjo" # @param ["Sitar", "Banjo", "Shamisen", "Koto", "Kalimba", "Bagpipe", "Fiddle", "Shanai"]
97
- model_channel_14_percussive_family = "Melodic Tom" # @param ["Tinkle Bell", "Agogo", "Steel Drums", "Woodblock", "Taiko Drum", "Melodic Tom", "Synth Drum", "Reverse Cymbal"]
98
- model_channel_15_sound_effects_family = "Bird Tweet" # @param ["Guitar Fret Noise", "Breath Noise", "Seashore", "Bird Tweet", "Telephone Ring", "Helicopter", "Applause", "Gunshot"]
99
- model_channel_16_drums_family = "Drums" # @param ["Drums"]
100
-
101
  print('=' * 70)
102
  print('Loading helper functions...')
103
 
104
  def txt2tokens(txt):
105
- return [ord(char)+2440 if 0 < ord(char) < 128 else 0+2440 for char in txt.lower()]
106
 
107
  def tokens2txt(tokens):
108
- return [chr(tok-2440) for tok in tokens if 0+2440 < tok < 128+2440 ]
109
 
110
  print('=' * 70)
111
- print('Setting up patches...')
112
- print('=' * 70)
113
-
114
- instruments = [v[1] for v in TMIDIX.Number2patch.items()]
115
-
116
- patches = [instruments.index(model_channel_0_piano_family),
117
- instruments.index(model_channel_1_chromatic_percussion_family),
118
- instruments.index(model_channel_2_organ_family),
119
- instruments.index(model_channel_3_guitar_family),
120
- instruments.index(model_channel_4_bass_family),
121
- instruments.index(model_channel_5_strings_family),
122
- instruments.index(model_channel_6_ensemble_family),
123
- instruments.index(model_channel_7_brass_family),
124
- instruments.index(model_channel_8_reed_family),
125
- 9, # Drums patch
126
- instruments.index(model_channel_9_pipe_family),
127
- instruments.index(model_channel_10_synth_lead_family),
128
- instruments.index(model_channel_11_synth_pad_family),
129
- instruments.index(model_channel_12_synth_effects_family),
130
- instruments.index(model_channel_13_ethnic_family),
131
- instruments.index(model_channel_15_sound_effects_family)
132
- ]
133
-
134
- print('Done!')
135
- print('=' * 70)
136
-
137
  print('Generating...')
138
 
139
  #@title Standard Text-to-Music Generator
140
 
141
- #@markdown Prompt settings
142
-
143
- song_title_prompt = input_title
144
- open_ended_prompt = input_prompt_type
145
-
146
  #@markdown Generation settings
147
 
148
  number_of_tokens_to_generate = input_num_tokens
@@ -150,22 +93,10 @@ def GenerateMusic(input_title, input_num_tokens, input_prompt_type):
150
  temperature = 0.9 # @param {type:"slider", min:0.1, max:1, step:0.05}
151
 
152
  print('=' * 70)
153
- print('Text-to-Music Model Generator')
154
  print('=' * 70)
155
 
156
- if song_title_prompt == '':
157
- outy = [2569]
158
-
159
- else:
160
- if open_ended_prompt:
161
- outy = [2569] + txt2tokens(song_title_prompt)
162
-
163
- else:
164
- outy = [2569] + txt2tokens(song_title_prompt) + [2570]
165
-
166
- print('Selected prompt sequence:')
167
- print(outy[:12])
168
- print('=' * 70)
169
 
170
  torch.cuda.empty_cache()
171
 
@@ -313,25 +244,19 @@ if __name__ == "__main__":
313
  "(https://colab.research.google.com/github/asigalov61/Text-to-Music-Transformer/blob/main/Text_to_Music_Transformer.ipynb)"
314
  " for faster execution and endless generation"
315
  )
316
- gr.Markdown("## Enter any desired song title")
317
 
318
- input_title = gr.Textbox(value="Nothing Else Matters", label="Song title")
319
- input_prompt_type = gr.Checkbox(label="Open-ended prompt")
320
-
321
- input_num_tokens = gr.Slider(8, 2048, value=512, step=8, label="Number of tokens to generate")
322
-
323
  run_btn = gr.Button("generate", variant="primary")
324
 
325
  gr.Markdown("## Generation results")
326
 
327
  output_midi_title = gr.Textbox(label="Generated MIDI title")
328
- output_midi_summary = gr.Textbox(label="Output MIDI summary")
329
  output_audio = gr.Audio(label="Output MIDI audio", format="wav", elem_id="midi_audio")
330
  output_plot = gr.Plot(label="Output MIDI score plot")
331
  output_midi = gr.File(label="Output MIDI file", file_types=[".mid"])
332
 
333
 
334
- run_event = run_btn.click(GenerateMusic, [input_title, input_num_tokens, input_prompt_type],
335
  [output_midi_title, output_midi_summary, output_midi, output_audio, output_plot])
336
 
337
  app.queue().launch()
 
23
  # =================================================================================================
24
 
25
  @spaces.GPU
26
+ def GenerateMusic(input_title):
27
+
28
  print('=' * 70)
29
  print('Req start time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT)))
30
  start_time = reqtime.time()
31
 
32
  print('Loading model...')
33
 
34
+ SEQ_LEN = 2048
35
+ PAD_IDX = 780
36
  DEVICE = 'cuda' # 'cuda'
37
 
38
  # instantiate the model
 
40
  model = TransformerWrapper(
41
  num_tokens = PAD_IDX+1,
42
  max_seq_len = SEQ_LEN,
43
+ attn_layers = Decoder(dim = 1024, depth = 32, heads = 16, attn_flash = True)
44
  )
45
 
46
+ model = AutoregressiveWrapper(model, ignore_index = PAD_IDX, pad_value=PAD_IDX)
47
 
48
  model.to(DEVICE)
49
  print('=' * 70)
 
51
  print('Loading model checkpoint...')
52
 
53
  model.load_state_dict(
54
+ torch.load('Descriptive_Music_Transformer_Trained_Model_20631_steps_0.3218_loss_0.8947_acc.pth',
55
  map_location=DEVICE))
56
  print('=' * 70)
57
 
 
67
  print('Done!')
68
  print('=' * 70)
69
 
70
+ input_num_tokens = 2040
 
 
 
 
 
71
  print('-' * 70)
72
 
73
  #===============================================================================
74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
  print('=' * 70)
76
  print('Loading helper functions...')
77
 
78
  def txt2tokens(txt):
79
+ return [ord(char)+648 if 0 < ord(char) < 128 else 0+648 for char in txt.lower()]
80
 
81
  def tokens2txt(tokens):
82
+ return [chr(tok-648) for tok in tokens if 0+648 < tok < 128+648 ]
83
 
84
  print('=' * 70)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
  print('Generating...')
86
 
87
  #@title Standard Text-to-Music Generator
88
 
 
 
 
 
 
89
  #@markdown Generation settings
90
 
91
  number_of_tokens_to_generate = input_num_tokens
 
93
  temperature = 0.9 # @param {type:"slider", min:0.1, max:1, step:0.05}
94
 
95
  print('=' * 70)
96
+ print('Descriptive Music Transformer Model Generator')
97
  print('=' * 70)
98
 
99
+ outy = [777]
 
 
 
 
 
 
 
 
 
 
 
 
100
 
101
  torch.cuda.empty_cache()
102
 
 
244
  "(https://colab.research.google.com/github/asigalov61/Text-to-Music-Transformer/blob/main/Text_to_Music_Transformer.ipynb)"
245
  " for faster execution and endless generation"
246
  )
 
247
 
 
 
 
 
 
248
  run_btn = gr.Button("generate", variant="primary")
249
 
250
  gr.Markdown("## Generation results")
251
 
252
  output_midi_title = gr.Textbox(label="Generated MIDI title")
253
+ output_midi_summary = gr.Textbox(label="Generated music description")
254
  output_audio = gr.Audio(label="Output MIDI audio", format="wav", elem_id="midi_audio")
255
  output_plot = gr.Plot(label="Output MIDI score plot")
256
  output_midi = gr.File(label="Output MIDI file", file_types=[".mid"])
257
 
258
 
259
+ run_event = run_btn.click(GenerateMusic, [input_title],
260
  [output_midi_title, output_midi_summary, output_midi, output_audio, output_plot])
261
 
262
  app.queue().launch()