lauraibnz commited on
Commit
7c70572
1 Parent(s): 337d060

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -4
app.py CHANGED
@@ -6,6 +6,8 @@ from tempfile import _TemporaryFileWrapper
6
  import torch
7
  import torchaudio
8
 
 
 
9
  if torch.cuda.is_available():
10
  device = "cuda"
11
  torch_dtype = torch.float16
@@ -21,10 +23,12 @@ pipe = pipe.to(device)
21
  generator = torch.Generator(device)
22
 
23
 
24
- def predict(midi_file=None, prompt="", negative_prompt="", audio_length_in_s=5, random_seed=0, controlnet_conditioning_scale=1, num_inference_steps=20, guidance_scale=2.5, guess_mode=False):
25
  if isinstance(midi_file, _TemporaryFileWrapper):
26
  midi_file = midi_file.name
27
  midi = PrettyMIDI(midi_file)
 
 
28
  audio = pipe(
29
  prompt,
30
  negative_prompt=negative_prompt,
@@ -36,7 +40,8 @@ def predict(midi_file=None, prompt="", negative_prompt="", audio_length_in_s=5,
36
  generator=generator.manual_seed(int(random_seed)),
37
  guidance_scale=float(guidance_scale),
38
  )
39
- return (16000, audio.audios.T)
 
40
 
41
  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:
42
  gr.HTML(
@@ -52,7 +57,8 @@ with gr.Blocks(title="🎹 MIDI-AudioLDM", theme=gr.themes.Base(text_size=gr.the
52
  midi = gr.File(label="midi file", file_types=[".mid"])
53
  prompt = gr.Textbox(label="prompt", info="Enter a descriptive text prompt to guide the audio generation.")
54
  with gr.Column(variant='panel'):
55
- audio = gr.Audio(label="audio")
 
56
  with gr.Accordion("Advanced Settings", open=False):
57
  duration = gr.Slider(0, 30, value=10, step=2.5, label="duration", info="Modify the duration in seconds of the output audio file.")
58
  inf = gr.Slider(0, 50, value=20, step=1, label="inference steps", info="Edit the number of denoising steps. A larger number usually leads to higher quality but slower results.")
@@ -62,7 +68,7 @@ with gr.Blocks(title="🎹 MIDI-AudioLDM", theme=gr.themes.Base(text_size=gr.the
62
  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.")
63
  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.")
64
  btn = gr.Button("Generate")
65
- btn.click(predict, inputs=[midi, prompt, neg_prompt, duration, seed, cond, inf, guidance_scale, guess], outputs=[audio])
66
  gr.Examples(examples=[["S00.mid", "piano", "", 10, 25, 1.0, 20, 2.5, False], ["S00.mid", "violin", "", 10, 25, 1.0, 20, 2.5, False], ["S00.mid", "woman singing, studio recording", "noise", 10, 25, 1.0, 20, 2.5, False], ["S00.mid", "jazz band, clean", "noise", 10, 25, 0.8, 20, 2.5, False], ["S00.mid", "choir", "noise, percussion", 10, 25, 0.7, 20, 2.5, False]], inputs=[midi, prompt, neg_prompt, duration, seed, cond, inf, guidance_scale, guess], fn=predict, outputs=audio, cache_examples=True)
67
 
68
  demo.launch()
 
6
  import torch
7
  import torchaudio
8
 
9
+ SAMPLE_RATE=16000
10
+
11
  if torch.cuda.is_available():
12
  device = "cuda"
13
  torch_dtype = torch.float16
 
23
  generator = torch.Generator(device)
24
 
25
 
26
+ def predict(midi_file=None, prompt="", negative_prompt="", audio_length_in_s=10, random_seed=0, controlnet_conditioning_scale=1, num_inference_steps=20, guidance_scale=2.5, guess_mode=False):
27
  if isinstance(midi_file, _TemporaryFileWrapper):
28
  midi_file = midi_file.name
29
  midi = PrettyMIDI(midi_file)
30
+ midi_synth = midi.synthesize(fs=SAMPLE_RATE)[:int(SAMPLE_RATE*audio_length_in_s)]
31
+ midi_synth = midi_synth.reshape(midi_synth.shape[0], 1)
32
  audio = pipe(
33
  prompt,
34
  negative_prompt=negative_prompt,
 
40
  generator=generator.manual_seed(int(random_seed)),
41
  guidance_scale=float(guidance_scale),
42
  )
43
+ return (SAMPLE_RATE, midi_synth), (SAMPLE_RATE, audio.audios.T)
44
+
45
 
46
  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:
47
  gr.HTML(
 
57
  midi = gr.File(label="midi file", file_types=[".mid"])
58
  prompt = gr.Textbox(label="prompt", info="Enter a descriptive text prompt to guide the audio generation.")
59
  with gr.Column(variant='panel'):
60
+ synth = gr.Audio(label="synthesized audio")
61
+ audio = gr.Audio(label="generated audio")
62
  with gr.Accordion("Advanced Settings", open=False):
63
  duration = gr.Slider(0, 30, value=10, step=2.5, label="duration", info="Modify the duration in seconds of the output audio file.")
64
  inf = gr.Slider(0, 50, value=20, step=1, label="inference steps", info="Edit the number of denoising steps. A larger number usually leads to higher quality but slower results.")
 
68
  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.")
69
  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.")
70
  btn = gr.Button("Generate")
71
+ btn.click(predict, inputs=[midi, prompt, neg_prompt, duration, seed, cond, inf, guidance_scale, guess], outputs=[synth, audio])
72
  gr.Examples(examples=[["S00.mid", "piano", "", 10, 25, 1.0, 20, 2.5, False], ["S00.mid", "violin", "", 10, 25, 1.0, 20, 2.5, False], ["S00.mid", "woman singing, studio recording", "noise", 10, 25, 1.0, 20, 2.5, False], ["S00.mid", "jazz band, clean", "noise", 10, 25, 0.8, 20, 2.5, False], ["S00.mid", "choir", "noise, percussion", 10, 25, 0.7, 20, 2.5, False]], inputs=[midi, prompt, neg_prompt, duration, seed, cond, inf, guidance_scale, guess], fn=predict, outputs=audio, cache_examples=True)
73
 
74
  demo.launch()