fluorescenrgrey commited on
Commit
3d0d529
1 Parent(s): ad55137

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +181 -42
app.py CHANGED
@@ -5,8 +5,11 @@ from pretty_midi import PrettyMIDI
5
  from tempfile import _TemporaryFileWrapper
6
  import torch
7
  import torchaudio
 
8
 
9
- SAMPLE_RATE=16000
 
 
10
 
11
  if torch.cuda.is_available():
12
  device = "cuda"
@@ -16,32 +19,51 @@ else:
16
  torch_dtype = torch.float32
17
 
18
  controlnet = ControlNetModel.from_pretrained(
19
- "lauraibnz/midi-audioldm-v2", torch_dtype=torch_dtype)
 
20
  pipe = AudioLDMControlNetPipeline.from_pretrained(
21
- "cvssp/audioldm-m-full", controlnet=controlnet, torch_dtype=torch_dtype)
 
22
  pipe = pipe.to(device)
23
  generator = torch.Generator(device)
24
 
25
- def predict(midi_file=None, midi_synth=None, prompt="", neg_prompt="", duration=None, seed=0, cond=1, inf=20, guidance_scale=2.5, guess=False):
26
- if isinstance(midi_file, _TemporaryFileWrapper):
27
- midi_file = midi_file.name
28
- midi = PrettyMIDI(midi_file)
29
- if not duration or duration == 0:
30
- duration = midi_synth[1].shape[0]/SAMPLE_RATE
31
- if not prompt and not neg_prompt:
32
- guess_mode = True
33
- audio = pipe(
34
- prompt,
35
- negative_prompt=neg_prompt,
36
- midi=midi,
37
- audio_length_in_s=duration,
38
- num_inference_steps=inf,
39
- controlnet_conditioning_scale=float(cond),
40
- guess_mode=guess,
41
- generator=generator.manual_seed(int(seed)),
42
- guidance_scale=float(guidance_scale),
43
- )
44
- return (SAMPLE_RATE, audio.audios.T)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
  def synthesize(midi_file=None):
47
  if isinstance(midi_file, _TemporaryFileWrapper):
@@ -51,23 +73,56 @@ def synthesize(midi_file=None):
51
  midi_synth = midi_synth.reshape(midi_synth.shape[0], 1)
52
  return (SAMPLE_RATE, midi_synth)
53
 
54
- def run_example(midi_file=None, prompt="", neg_prompt="", duration=None, seed=0, cond=1, inf=20, guidance_scale=2.5, guess=False):
 
 
 
 
 
 
 
 
 
 
 
55
  midi_synth = synthesize(midi_file)
56
- gen_audio = predict(midi_file, midi_synth, prompt, neg_prompt, duration, seed, cond, inf, guidance_scale, guess)
 
 
 
 
 
 
 
 
 
 
 
57
  return midi_synth, gen_audio
58
 
59
- with gr.Blocks(title="🎹 MIDI-AudioLDM", theme=gr.themes.Base(text_size=gr.themes.sizes.text_md, font=[gr.themes.GoogleFont("Nunito Sans")])) as demo:
 
 
 
 
 
 
60
  gr.HTML(
61
  """
62
  <h1 align="center"; size="16">🎹 MIDI-AudioLDM </h1>
63
- """)
 
64
  gr.Markdown(
65
- """
66
  MIDI-AudioLDM is a MIDI-conditioned text-to-audio model based on the project [AudioLDM](https://huggingface.co/spaces/haoheliu/audioldm-text-to-audio-generation). The model has been conditioned using the ControlNet architecture and has been developed within Hugging Face’s [🧨 Diffusers](https://huggingface.co/docs/diffusers/) framework. Once trained, MIDI-AudioLDM accepts a MIDI file and a text prompt as input and returns an audio file, which is an interpretation of the MIDI based on the given text description. This enables detailed control over different musical aspects such as notes, mood and timbre.
67
- """)
68
- with gr.Column(variant='panel'):
 
69
  midi = gr.File(label="midi file", file_types=[".mid"])
70
- prompt = gr.Textbox(label="prompt", info="Enter a descriptive text prompt to guide the audio generation.")
 
 
 
71
  with gr.Row():
72
  with gr.Column():
73
  midi_synth = gr.Audio(label="synthesized midi")
@@ -75,15 +130,99 @@ with gr.Blocks(title="🎹 MIDI-AudioLDM", theme=gr.themes.Base(text_size=gr.the
75
  with gr.Column():
76
  audio = gr.Audio(label="generated audio")
77
  with gr.Accordion("Advanced Settings", open=False):
78
- duration = gr.Slider(0, 20, step=2.5, label="duration", info="Modify the duration in seconds of the output audio file. If not set it will be determined by the MIDI file.")
79
- inf = gr.Slider(0, 100, value=40, step=1, label="inference steps", info="Edit the number of denoising steps. A larger number usually leads to higher quality but slower results.")
80
- guidance_scale = gr.Slider(0, 4, value=2.5, step=0.5, label="guidance scale", info="Modify the guidance scale. The higher the value the more linked the generated audio to the text prompt, sometimes at the expense of lower quality.")
81
- neg_prompt = gr.Textbox(label="negative prompt", info="Optionally enter a negative text prompt not to guide the audio generation.")
82
- seed = gr.Number(value=48, label="random seed", info="Change the random seed for a different generation result.")
83
- cond = gr.Slider(0.0, 1.0, value=1.0, step=0.1, label="conditioning scale", info="Choose a value between 0 and 1. The larger the more it will take the conditioning into account. Lower values are recommended for more creative prompts.")
84
- guess = gr.Checkbox(label="guess mode", info="Optionally select guess mode. If so, the model will try to recognize the content of the MIDI without the need of a text prompt.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
  btn = gr.Button("Generate")
86
- btn.click(predict, inputs=[midi, midi_synth, prompt, neg_prompt, duration, seed, cond, inf, guidance_scale, guess], outputs=[audio])
87
- gr.Examples(examples=[["S00.mid", "piano", "", 10, 48, 1.0, 20, 2.5, False], ["S00.mid", "violin", "", 10, 48, 1.0, 20, 2.5, False], ["S00.mid", "woman singing, studio recording", "noise", 10, 48, 1.0, 20, 2.5, False], ["S00.mid", "jazz band, clean", "noise", 10, 48, 1.0, 20, 2.5, False], ["S00.mid", "choir", "noise, percussion", 10, 48, 1.0, 20, 2.5, False]], inputs=[midi, prompt, neg_prompt, duration, seed, cond, inf, guidance_scale, guess], fn=run_example, outputs=[midi_synth, audio], cache_examples=True)
88
-
89
- demo.launch(show_error=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  from tempfile import _TemporaryFileWrapper
6
  import torch
7
  import torchaudio
8
+ import logging
9
 
10
+ logging.basicConfig(level=logging.INFO)
11
+
12
+ SAMPLE_RATE = 16000
13
 
14
  if torch.cuda.is_available():
15
  device = "cuda"
 
19
  torch_dtype = torch.float32
20
 
21
  controlnet = ControlNetModel.from_pretrained(
22
+ "lauraibnz/midi-audioldm-v2", torch_dtype=torch_dtype
23
+ )
24
  pipe = AudioLDMControlNetPipeline.from_pretrained(
25
+ "cvssp/audioldm-m-full", controlnet=controlnet, torch_dtype=torch_dtype
26
+ )
27
  pipe = pipe.to(device)
28
  generator = torch.Generator(device)
29
 
30
+
31
+ def predict(
32
+ midi_file=None,
33
+ midi_synth=None,
34
+ prompt="",
35
+ neg_prompt="",
36
+ duration=None,
37
+ seed=0,
38
+ cond=1,
39
+ inf=20,
40
+ guidance_scale=2.5,
41
+ guess=False,
42
+ ):
43
+ try:
44
+ if isinstance(midi_file, _TemporaryFileWrapper):
45
+ midi_file = midi_file.name
46
+ midi = PrettyMIDI(midi_file)
47
+ if not duration or duration == 0:
48
+ duration = midi_synth[1].shape[0] / SAMPLE_RATE
49
+ if not prompt and not neg_prompt:
50
+ guess_mode = True
51
+ audio = pipe(
52
+ prompt,
53
+ negative_prompt=neg_prompt,
54
+ midi=midi,
55
+ audio_length_in_s=duration,
56
+ num_inference_steps=inf,
57
+ controlnet_conditioning_scale=float(cond),
58
+ guess_mode=guess,
59
+ generator=generator.manual_seed(int(seed)),
60
+ guidance_scale=float(guidance_scale),
61
+ )
62
+ return (SAMPLE_RATE, audio.audios.T)
63
+ except Exception as e:
64
+ logging.error(f"Error in predict function: {str(e)}")
65
+ raise gr.Error(f"An error occurred: {str(e)}")
66
+
67
 
68
  def synthesize(midi_file=None):
69
  if isinstance(midi_file, _TemporaryFileWrapper):
 
73
  midi_synth = midi_synth.reshape(midi_synth.shape[0], 1)
74
  return (SAMPLE_RATE, midi_synth)
75
 
76
+
77
+ def run_example(
78
+ midi_file=None,
79
+ prompt="",
80
+ neg_prompt="",
81
+ duration=None,
82
+ seed=0,
83
+ cond=1,
84
+ inf=20,
85
+ guidance_scale=2.5,
86
+ guess=False,
87
+ ):
88
  midi_synth = synthesize(midi_file)
89
+ gen_audio = predict(
90
+ midi_file,
91
+ midi_synth,
92
+ prompt,
93
+ neg_prompt,
94
+ duration,
95
+ seed,
96
+ cond,
97
+ inf,
98
+ guidance_scale,
99
+ guess,
100
+ )
101
  return midi_synth, gen_audio
102
 
103
+
104
+ with gr.Blocks(
105
+ title="🎹 MIDI-AudioLDM",
106
+ theme=gr.themes.Base(
107
+ text_size=gr.themes.sizes.text_md, font=[gr.themes.GoogleFont("Nunito Sans")]
108
+ ),
109
+ ) as demo:
110
  gr.HTML(
111
  """
112
  <h1 align="center"; size="16">🎹 MIDI-AudioLDM </h1>
113
+ """
114
+ )
115
  gr.Markdown(
116
+ """
117
  MIDI-AudioLDM is a MIDI-conditioned text-to-audio model based on the project [AudioLDM](https://huggingface.co/spaces/haoheliu/audioldm-text-to-audio-generation). The model has been conditioned using the ControlNet architecture and has been developed within Hugging Face’s [🧨 Diffusers](https://huggingface.co/docs/diffusers/) framework. Once trained, MIDI-AudioLDM accepts a MIDI file and a text prompt as input and returns an audio file, which is an interpretation of the MIDI based on the given text description. This enables detailed control over different musical aspects such as notes, mood and timbre.
118
+ """
119
+ )
120
+ with gr.Column(variant="panel"):
121
  midi = gr.File(label="midi file", file_types=[".mid"])
122
+ prompt = gr.Textbox(
123
+ label="prompt",
124
+ info="Enter a descriptive text prompt to guide the audio generation.",
125
+ )
126
  with gr.Row():
127
  with gr.Column():
128
  midi_synth = gr.Audio(label="synthesized midi")
 
130
  with gr.Column():
131
  audio = gr.Audio(label="generated audio")
132
  with gr.Accordion("Advanced Settings", open=False):
133
+ duration = gr.Slider(
134
+ 0,
135
+ 20,
136
+ step=2.5,
137
+ label="duration",
138
+ info="Modify the duration in seconds of the output audio file. If not set it will be determined by the MIDI file.",
139
+ )
140
+ inf = gr.Slider(
141
+ 0,
142
+ 100,
143
+ value=40,
144
+ step=1,
145
+ label="inference steps",
146
+ info="Edit the number of denoising steps. A larger number usually leads to higher quality but slower results.",
147
+ )
148
+ guidance_scale = gr.Slider(
149
+ 0,
150
+ 4,
151
+ value=2.5,
152
+ step=0.5,
153
+ label="guidance scale",
154
+ info="Modify the guidance scale. The higher the value the more linked the generated audio to the text prompt, sometimes at the expense of lower quality.",
155
+ )
156
+ neg_prompt = gr.Textbox(
157
+ label="negative prompt",
158
+ info="Optionally enter a negative text prompt not to guide the audio generation.",
159
+ )
160
+ seed = gr.Number(
161
+ value=48,
162
+ label="random seed",
163
+ info="Change the random seed for a different generation result.",
164
+ )
165
+ cond = gr.Slider(
166
+ 0.0,
167
+ 1.0,
168
+ value=1.0,
169
+ step=0.1,
170
+ label="conditioning scale",
171
+ info="Choose a value between 0 and 1. The larger the more it will take the conditioning into account. Lower values are recommended for more creative prompts.",
172
+ )
173
+ guess = gr.Checkbox(
174
+ label="guess mode",
175
+ info="Optionally select guess mode. If so, the model will try to recognize the content of the MIDI without the need of a text prompt.",
176
+ )
177
  btn = gr.Button("Generate")
178
+ btn.click(
179
+ predict,
180
+ inputs=[
181
+ midi,
182
+ midi_synth,
183
+ prompt,
184
+ neg_prompt,
185
+ duration,
186
+ seed,
187
+ cond,
188
+ inf,
189
+ guidance_scale,
190
+ guess,
191
+ ],
192
+ outputs=[audio],
193
+ )
194
+ gr.Examples(
195
+ examples=[
196
+ ["S00.mid", "piano", "", 10, 48, 1.0, 20, 2.5, False],
197
+ ["S00.mid", "violin", "", 10, 48, 1.0, 20, 2.5, False],
198
+ [
199
+ "S00.mid",
200
+ "woman singing, studio recording",
201
+ "noise",
202
+ 10,
203
+ 48,
204
+ 1.0,
205
+ 20,
206
+ 2.5,
207
+ False,
208
+ ],
209
+ ["S00.mid", "jazz band, clean", "noise", 10, 48, 1.0, 20, 2.5, False],
210
+ ["S00.mid", "choir", "noise, percussion", 10, 48, 1.0, 20, 2.5, False],
211
+ ],
212
+ inputs=[
213
+ midi,
214
+ prompt,
215
+ neg_prompt,
216
+ duration,
217
+ seed,
218
+ cond,
219
+ inf,
220
+ guidance_scale,
221
+ guess,
222
+ ],
223
+ fn=run_example,
224
+ outputs=[midi_synth, audio],
225
+ cache_examples=True,
226
+ )
227
+
228
+ demo.launch(show_error=True)