lauraibnz commited on
Commit
24b4874
1 Parent(s): 1403efb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -7
app.py CHANGED
@@ -27,8 +27,6 @@ def predict(midi_file=None, prompt="", negative_prompt="", audio_length_in_s=10,
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,8 +38,15 @@ def predict(midi_file=None, prompt="", negative_prompt="", audio_length_in_s=10,
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(
@@ -55,9 +60,10 @@ with gr.Blocks(title="🎹 MIDI-AudioLDM", theme=gr.themes.Base(text_size=gr.the
55
  with gr.Row():
56
  with gr.Column(variant='panel'):
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.")
@@ -68,7 +74,7 @@ with gr.Blocks(title="🎹 MIDI-AudioLDM", theme=gr.themes.Base(text_size=gr.the
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=[synth, audio], cache_examples=True)
73
 
74
  demo.launch()
 
27
  if isinstance(midi_file, _TemporaryFileWrapper):
28
  midi_file = midi_file.name
29
  midi = PrettyMIDI(midi_file)
 
 
30
  audio = pipe(
31
  prompt,
32
  negative_prompt=negative_prompt,
 
38
  generator=generator.manual_seed(int(random_seed)),
39
  guidance_scale=float(guidance_scale),
40
  )
41
+ return (SAMPLE_RATE, audio.audios.T)
42
+
43
+ def synthesize(midi_file=None):
44
+ if isinstance(midi_file, _TemporaryFileWrapper):
45
+ midi_file = midi_file.name
46
+ midi = PrettyMIDI(midi_file)
47
+ midi_synth = midi.synthesize(fs=SAMPLE_RATE)
48
+ midi_synth = midi_synth.reshape(midi_synth.shape[0], 1)
49
+ return (SAMPLE_RATE, midi_synth)
50
 
51
  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:
52
  gr.HTML(
 
60
  with gr.Row():
61
  with gr.Column(variant='panel'):
62
  midi = gr.File(label="midi file", file_types=[".mid"])
63
+ midi_synth = gr.Audio(label="synthesized midi")
64
+ midi.upload(synthesize, inputs=midi, outputs=midi_synth)
65
  prompt = gr.Textbox(label="prompt", info="Enter a descriptive text prompt to guide the audio generation.")
66
  with gr.Column(variant='panel'):
 
67
  audio = gr.Audio(label="generated audio")
68
  with gr.Accordion("Advanced Settings", open=False):
69
  duration = gr.Slider(0, 30, value=10, step=2.5, label="duration", info="Modify the duration in seconds of the output audio file.")
 
74
  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.")
75
  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.")
76
  btn = gr.Button("Generate")
77
+ btn.click(predict, inputs=[midi, prompt, neg_prompt, duration, seed, cond, inf, guidance_scale, guess], outputs=[audio])
78
+ 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)
79
 
80
  demo.launch()