radames commited on
Commit
88c405d
1 Parent(s): 000b8ee

clean up UI

Browse files
Files changed (1) hide show
  1. app.py +51 -74
app.py CHANGED
@@ -18,7 +18,6 @@ from audiocraft.data.audio import audio_write
18
  from share_btn import community_icon_html, loading_icon_html, share_js, css
19
 
20
  MODEL = None
21
- IS_SHARED_SPACE = "radames/MusicGen-Continuation" in os.environ.get("SPACE_ID", "")
22
 
23
 
24
  def load_model(version):
@@ -30,7 +29,6 @@ def predict(
30
  text,
31
  melody_input,
32
  duration=30,
33
- continuation=False,
34
  continuation_start=0,
35
  continuation_end=30,
36
  topk=250,
@@ -42,10 +40,13 @@ def predict(
42
  topk = int(topk)
43
  if MODEL is None:
44
  MODEL = load_model("melody")
 
 
 
45
 
46
  if duration > MODEL.lm.cfg.dataset.segment_duration:
47
  raise gr.Error("MusicGen currently supports durations of up to 30 seconds!")
48
- if continuation and continuation_end < continuation_start:
49
  raise gr.Error("The end time must be greater than the start time!")
50
  MODEL.set_generation_params(
51
  use_sampling=True,
@@ -61,34 +62,19 @@ def predict(
61
  # sr, melody = melody_input[0], torch.from_numpy(melody_input[1]).to(MODEL.device).float().t().unsqueeze(0)
62
  if melody.dim() == 2:
63
  melody = melody[None]
64
- if continuation:
65
- print("\nGenerating continuation\n")
66
- melody_wavform = melody[
67
- ..., int(sr * continuation_start) : int(sr * continuation_end)
68
- ]
69
- melody_duration = melody_wavform.shape[-1] / sr
70
- if duration + melody_duration > MODEL.lm.cfg.dataset.segment_duration:
71
- raise gr.Error("Duration + continuation duration must be <= 30 seconds")
72
- output = MODEL.generate_continuation(
73
- prompt=melody_wavform,
74
- prompt_sample_rate=sr,
75
- descriptions=[text],
76
- progress=True,
77
- )
78
- else:
79
- print("\nGenerating with melody\n")
80
- melody_wavform = melody[
81
- ..., : int(sr * MODEL.lm.cfg.dataset.segment_duration)
82
- ]
83
- output = MODEL.generate_with_chroma(
84
- descriptions=[text],
85
- melody_wavs=melody_wavform,
86
- melody_sample_rate=sr,
87
- progress=True,
88
- )
89
- else:
90
- print("\nGenerating without melody\n")
91
- output = MODEL.generate(descriptions=[text], progress=False)
92
 
93
  output = output.detach().cpu().float()[0]
94
  with NamedTemporaryFile("wb", suffix=".wav", delete=False) as file:
@@ -150,20 +136,19 @@ def ui(**kwargs):
150
  gr.Markdown(
151
  """
152
  # MusicGen Continuation
153
- This is your private demo for [MusicGen](https://github.com/facebookresearch/audiocraft), a simple and controllable model for music generation
154
  presented at: ["Simple and Controllable Music Generation"](https://huggingface.co/papers/2306.05284)
 
 
155
  """
156
  )
157
- if IS_SHARED_SPACE:
158
- gr.Markdown(
159
- """
160
- ⚠ This Space doesn't work in this shared UI ⚠
161
-
162
  <a href="https://huggingface.co/spaces/radames/MusicGen-Continuation?duplicate=true" style="display: inline-block;margin-top: .5em;margin-right: .25em;" target="_blank">
163
  <img style="margin-bottom: 0em;display: inline;margin-top: -.25em;" src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>
164
- to use it privately, or use the <a href="https://huggingface.co/spaces/facebook/MusicGen">public demo</a>
165
  """
166
- )
167
  with gr.Row():
168
  with gr.Column():
169
  with gr.Row():
@@ -177,7 +162,8 @@ def ui(**kwargs):
177
  radio = gr.Radio(
178
  ["file", "mic"],
179
  value="file",
180
- label="Melody Condition (optional) File or Mic",
 
181
  )
182
  melody = gr.Audio(
183
  source="upload",
@@ -188,46 +174,38 @@ def ui(**kwargs):
188
  )
189
  with gr.Row():
190
  submit = gr.Button("Submit")
191
- # with gr.Row():
192
- # model = gr.Radio(
193
- # ["melody", "medium", "small", "large"],
194
- # label="Model",
195
- # value="melody",
196
- # interactive=True,
197
- # )
198
  with gr.Row():
199
  duration = gr.Slider(
200
  minimum=1,
201
  maximum=30,
202
  value=10,
203
- label="Total Duration",
204
  interactive=True,
205
  )
206
- with gr.Row():
207
- continuation = gr.Checkbox(value=False, label="Enable Continuation")
208
- with gr.Row():
209
- continuation_start = gr.Slider(
210
- minimum=0,
211
- maximum=30,
212
- step=0.01,
213
- value=0,
214
- label="melody cut start",
215
- interactive=True,
216
- )
217
- continuation_end = gr.Slider(
218
- minimum=0,
219
- maximum=30,
220
- step=0.01,
221
- value=0,
222
- label="melody cut end",
223
- interactive=True,
224
- )
225
- cut_btn = gr.Button("Cut Melody").style(full_width=False)
226
- with gr.Row():
227
- preview_cut = gr.Audio(
228
- type="numpy",
229
- label="Cut Preview",
230
- )
231
  with gr.Accordion(label="Advanced Settings", open=False):
232
  with gr.Row():
233
  topk = gr.Number(label="Top-k", value=250, interactive=True)
@@ -276,7 +254,6 @@ def ui(**kwargs):
276
  text,
277
  melody,
278
  duration,
279
- continuation,
280
  continuation_start,
281
  continuation_end,
282
  topk,
 
18
  from share_btn import community_icon_html, loading_icon_html, share_js, css
19
 
20
  MODEL = None
 
21
 
22
 
23
  def load_model(version):
 
29
  text,
30
  melody_input,
31
  duration=30,
 
32
  continuation_start=0,
33
  continuation_end=30,
34
  topk=250,
 
40
  topk = int(topk)
41
  if MODEL is None:
42
  MODEL = load_model("melody")
43
+
44
+ if melody_input is None:
45
+ raise gr.Error("Please upload a melody to continue!")
46
 
47
  if duration > MODEL.lm.cfg.dataset.segment_duration:
48
  raise gr.Error("MusicGen currently supports durations of up to 30 seconds!")
49
+ if continuation_end < continuation_start:
50
  raise gr.Error("The end time must be greater than the start time!")
51
  MODEL.set_generation_params(
52
  use_sampling=True,
 
62
  # sr, melody = melody_input[0], torch.from_numpy(melody_input[1]).to(MODEL.device).float().t().unsqueeze(0)
63
  if melody.dim() == 2:
64
  melody = melody[None]
65
+ print("\nGenerating continuation\n")
66
+ melody_wavform = melody[
67
+ ..., int(sr * continuation_start) : int(sr * continuation_end)
68
+ ]
69
+ melody_duration = melody_wavform.shape[-1] / sr
70
+ if duration + melody_duration > MODEL.lm.cfg.dataset.segment_duration:
71
+ raise gr.Error("Duration + continuation duration must be <= 30 seconds")
72
+ output = MODEL.generate_continuation(
73
+ prompt=melody_wavform,
74
+ prompt_sample_rate=sr,
75
+ descriptions=[text],
76
+ progress=True,
77
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
 
79
  output = output.detach().cpu().float()[0]
80
  with NamedTemporaryFile("wb", suffix=".wav", delete=False) as file:
 
136
  gr.Markdown(
137
  """
138
  # MusicGen Continuation
139
+ This a [MusicGen](https://github.com/facebookresearch/audiocraft), a simple and controllable model for music generation
140
  presented at: ["Simple and Controllable Music Generation"](https://huggingface.co/papers/2306.05284)
141
+
142
+ This Spaces only does melody continuation, you can try other features [here](https://huggingface.co/spaces/facebook/MusicGen)
143
  """
144
  )
145
+ gr.Markdown(
146
+ """
 
 
 
147
  <a href="https://huggingface.co/spaces/radames/MusicGen-Continuation?duplicate=true" style="display: inline-block;margin-top: .5em;margin-right: .25em;" target="_blank">
148
  <img style="margin-bottom: 0em;display: inline;margin-top: -.25em;" src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>
149
+ to use it privately
150
  """
151
+ )
152
  with gr.Row():
153
  with gr.Column():
154
  with gr.Row():
 
162
  radio = gr.Radio(
163
  ["file", "mic"],
164
  value="file",
165
+ label="Melody Inital Condition File or Mic",
166
+ info="Make sure the audio is no longer than total generation duration which is max 30 seconds, you can trim the audio in the next section",
167
  )
168
  melody = gr.Audio(
169
  source="upload",
 
174
  )
175
  with gr.Row():
176
  submit = gr.Button("Submit")
 
 
 
 
 
 
 
177
  with gr.Row():
178
  duration = gr.Slider(
179
  minimum=1,
180
  maximum=30,
181
  value=10,
182
+ label="Total Generation Duration",
183
  interactive=True,
184
  )
185
+ with gr.Accordion(label="Input Melody Trimming (optional)", open=False):
186
+ with gr.Row():
187
+ continuation_start = gr.Slider(
188
+ minimum=0,
189
+ maximum=30,
190
+ step=0.01,
191
+ value=0,
192
+ label="melody cut start",
193
+ interactive=True,
194
+ )
195
+ continuation_end = gr.Slider(
196
+ minimum=0,
197
+ maximum=30,
198
+ step=0.01,
199
+ value=0,
200
+ label="melody cut end",
201
+ interactive=True,
202
+ )
203
+ cut_btn = gr.Button("Cut Melody").style(full_width=False)
204
+ with gr.Row():
205
+ preview_cut = gr.Audio(
206
+ type="numpy",
207
+ label="Cut Preview",
208
+ )
 
209
  with gr.Accordion(label="Advanced Settings", open=False):
210
  with gr.Row():
211
  topk = gr.Number(label="Top-k", value=250, interactive=True)
 
254
  text,
255
  melody,
256
  duration,
 
257
  continuation_start,
258
  continuation_end,
259
  topk,