Surn commited on
Commit
2f9a1a4
·
1 Parent(s): 618d8b2

Add Waveform Animation and Video orientation options

Browse files
Files changed (1) hide show
  1. app.py +20 -7
app.py CHANGED
@@ -134,6 +134,12 @@ def git_tag():
134
  except Exception:
135
  return "<none>"
136
 
 
 
 
 
 
 
137
  def load_melody_filepath(melody_filepath, title, assigned_model,topp, temperature, cfg_coef, segment_length = 30):
138
  # get melody filename
139
  #$Union[str, os.PathLike]
@@ -169,7 +175,7 @@ def load_melody_filepath(melody_filepath, title, assigned_model,topp, temperatur
169
 
170
  return gr.update(value=melody_name), gr.update(maximum=MAX_PROMPT_INDEX, value=-1), gr.update(value=assigned_model, interactive=True), gr.update(value=topp), gr.update(value=temperature), gr.update(value=cfg_coef), gr.update(maximum=MAX_OVERLAP)
171
 
172
- def predict(model, text, melody_filepath, duration, dimension, topk, topp, temperature, cfg_coef, background, title, settings_font, settings_font_color, seed, overlap=1, prompt_index = 0, include_title = True, include_settings = True, harmony_only = False, profile = gr.OAuthProfile, segment_length = 30, settings_font_size=28, progress=gr.Progress(track_tqdm=True)):
173
  global MODEL, INTERRUPTED, INTERRUPTING, MOVE_TO_CPU
174
  output_segments = None
175
  melody_name = "Not Used"
@@ -323,16 +329,20 @@ def predict(model, text, melody_filepath, duration, dimension, topk, topp, tempe
323
  return None, None, seed
324
  else:
325
  output = output.detach().cpu().float()[0]
 
 
 
 
326
 
327
  title_file_name = convert_title_to_filename(title)
328
  with NamedTemporaryFile("wb", suffix=".wav", delete=False, prefix=title_file_name) as file:
329
  video_description = f"{text}\n Duration: {str(initial_duration)} Dimension: {dimension}\n Top-k:{topk} Top-p:{topp}\n Randomness:{temperature}\n cfg:{cfg_coef} overlap: {overlap}\n Seed: {seed}\n Model: {model}\n Melody Condition:{melody_name}\n Sample Segment: {prompt_index}"
330
  if include_settings or include_title:
331
- background = add_settings_to_image(title if include_title else "",video_description if include_settings else "",background_path=background,font=settings_font,font_color=settings_font_color, font_size=settings_font_size)
332
  audio_write(
333
  file.name, output, MODEL.sample_rate, strategy="loudness",
334
  loudness_headroom_db=18, loudness_compressor=True, add_suffix=False, channels=2)
335
- waveform_video_path = get_waveform(file.name, bg_image=background, bar_count=45, name=title_file_name, animate=False, progress=gr.Progress(track_tqdm=True))
336
  # Remove the extension from file.name
337
  file_name_without_extension = os.path.splitext(file.name)[0]
338
  # Get the directory, filename, name, extension, and new extension of the waveform video path
@@ -352,8 +362,8 @@ def predict(model, text, melody_filepath, duration, dimension, topk, topp, tempe
352
  "negative_prompt": "",
353
  "Seed": seed,
354
  "steps": 1,
355
- "width": "768px",
356
- "height": "512px",
357
  "Dimension": dimension,
358
  "Top-k": topk,
359
  "Top-p": topp,
@@ -483,11 +493,13 @@ def ui(**kwargs):
483
  with gr.Column():
484
  include_title = gr.Checkbox(label="Add Title", value=True, interactive=True,key="add_title")
485
  include_settings = gr.Checkbox(label="Add Settings to background", value=True, interactive=True, key="add_settings")
 
486
  with gr.Row():
487
  title = gr.Textbox(label="Title", value="UnlimitedMusicGen", interactive=True, key="song_title")
488
  settings_font = gr.Text(label="Settings Font", value="./assets/arial.ttf", interactive=True)
489
  settings_font_color = gr.ColorPicker(label="Settings Font Color", value="#c87f05", interactive=True, key="settings_font_color")
490
  settings_font_size = gr.Slider(minimum=8, maximum=64, value=28, step=1, label="Settings Font Size", interactive=True, key="settings_font_size")
 
491
  with gr.Accordion("Expert", open=False):
492
  with gr.Row():
493
  segment_length = gr.Slider(minimum=10, maximum=30, value=30, step=1,label="Music Generation Segment Length (s)", interactive=True,key="segment_length")
@@ -507,7 +519,8 @@ def ui(**kwargs):
507
  wave_file = gr.File(label=".wav file", elem_id="output_wavefile", interactive=True)
508
  seed_used = gr.Number(label='Seed used', value=-1, interactive=False)
509
 
510
- radio.change(toggle_audio_src, radio, [melody_filepath], queue=False, show_progress=False)
 
511
  melody_filepath.change(load_melody_filepath, inputs=[melody_filepath, title, model,topp, temperature, cfg_coef, segment_length], outputs=[title, prompt_index , model, topp, temperature, cfg_coef, overlap], api_name="melody_filepath_change", queue=False)
512
  reuse_seed.click(fn=lambda x: x, inputs=[seed_used], outputs=[seed], queue=False, api_name="reuse_seed_click")
513
  autoplay_cb.change(fn=lambda x: gr.update(autoplay=x), inputs=[autoplay_cb], outputs=[output], queue=False, api_name="autoplay_cb_change")
@@ -582,7 +595,7 @@ def ui(**kwargs):
582
  api_name="submit"
583
  ).then(
584
  predict,
585
- inputs=[model, text,melody_filepath, duration, dimension, topk, topp, temperature, cfg_coef, background, title, settings_font, settings_font_color, seed, overlap, prompt_index, include_title, include_settings, harmony_only, user_profile, segment_length, settings_font_size],
586
  outputs=[output, wave_file, seed_used], scroll_to_output=True)
587
 
588
  # Show the interface
 
134
  except Exception:
135
  return "<none>"
136
 
137
+ def load_background_filepath(video_orientation):
138
+ if video_orientation == "Landscape":
139
+ return "./assets/background.png"
140
+ else:
141
+ return "./assets/background_portrait.png"
142
+
143
  def load_melody_filepath(melody_filepath, title, assigned_model,topp, temperature, cfg_coef, segment_length = 30):
144
  # get melody filename
145
  #$Union[str, os.PathLike]
 
175
 
176
  return gr.update(value=melody_name), gr.update(maximum=MAX_PROMPT_INDEX, value=-1), gr.update(value=assigned_model, interactive=True), gr.update(value=topp), gr.update(value=temperature), gr.update(value=cfg_coef), gr.update(maximum=MAX_OVERLAP)
177
 
178
+ def predict(model, text, melody_filepath, duration, dimension, topk, topp, temperature, cfg_coef, background, title, settings_font, settings_font_color, seed, overlap=1, prompt_index = 0, include_title = True, include_settings = True, harmony_only = False, profile = gr.OAuthProfile, segment_length = 30, settings_font_size=28, settings_animate_waveform=False, video_orientation="Landscape", progress=gr.Progress(track_tqdm=True)):
179
  global MODEL, INTERRUPTED, INTERRUPTING, MOVE_TO_CPU
180
  output_segments = None
181
  melody_name = "Not Used"
 
329
  return None, None, seed
330
  else:
331
  output = output.detach().cpu().float()[0]
332
+
333
+ video_width, video_height = 768, 512
334
+ if video_orientation == "Portait":
335
+ video_width, video_height = 512, 768
336
 
337
  title_file_name = convert_title_to_filename(title)
338
  with NamedTemporaryFile("wb", suffix=".wav", delete=False, prefix=title_file_name) as file:
339
  video_description = f"{text}\n Duration: {str(initial_duration)} Dimension: {dimension}\n Top-k:{topk} Top-p:{topp}\n Randomness:{temperature}\n cfg:{cfg_coef} overlap: {overlap}\n Seed: {seed}\n Model: {model}\n Melody Condition:{melody_name}\n Sample Segment: {prompt_index}"
340
  if include_settings or include_title:
341
+ background = add_settings_to_image(title if include_title else "",video_description if include_settings else "",width=video_width, height=video_height, background_path=background,font=settings_font,font_color=settings_font_color, font_size=settings_font_size)
342
  audio_write(
343
  file.name, output, MODEL.sample_rate, strategy="loudness",
344
  loudness_headroom_db=18, loudness_compressor=True, add_suffix=False, channels=2)
345
+ waveform_video_path = get_waveform(file.name, bg_image=background, bar_count=45, name=title_file_name, animate=settings_animate_waveform, progress=gr.Progress(track_tqdm=True))
346
  # Remove the extension from file.name
347
  file_name_without_extension = os.path.splitext(file.name)[0]
348
  # Get the directory, filename, name, extension, and new extension of the waveform video path
 
362
  "negative_prompt": "",
363
  "Seed": seed,
364
  "steps": 1,
365
+ "wdth": video_width,
366
+ "hght": video_height,
367
  "Dimension": dimension,
368
  "Top-k": topk,
369
  "Top-p": topp,
 
493
  with gr.Column():
494
  include_title = gr.Checkbox(label="Add Title", value=True, interactive=True,key="add_title")
495
  include_settings = gr.Checkbox(label="Add Settings to background", value=True, interactive=True, key="add_settings")
496
+ video_orientation = gr.Radio(label="Video Orientation", choices=["Landscape", "Portait"], value="Landscape", interactive=True, key="video_orientation")
497
  with gr.Row():
498
  title = gr.Textbox(label="Title", value="UnlimitedMusicGen", interactive=True, key="song_title")
499
  settings_font = gr.Text(label="Settings Font", value="./assets/arial.ttf", interactive=True)
500
  settings_font_color = gr.ColorPicker(label="Settings Font Color", value="#c87f05", interactive=True, key="settings_font_color")
501
  settings_font_size = gr.Slider(minimum=8, maximum=64, value=28, step=1, label="Settings Font Size", interactive=True, key="settings_font_size")
502
+ settings_animate_waveform = gr.Checkbox(label="Animate Waveform", value=False, interactive=True, key="animate_waveform")
503
  with gr.Accordion("Expert", open=False):
504
  with gr.Row():
505
  segment_length = gr.Slider(minimum=10, maximum=30, value=30, step=1,label="Music Generation Segment Length (s)", interactive=True,key="segment_length")
 
519
  wave_file = gr.File(label=".wav file", elem_id="output_wavefile", interactive=True)
520
  seed_used = gr.Number(label='Seed used', value=-1, interactive=False)
521
 
522
+ radio.change(toggle_audio_src, radio, [melody_filepath], queue=False, show_progress=False, api_name="audio_src_change")
523
+ video_orientation.change(load_background_filepath, inputs=[video_orientation], outputs=[background], queue=False, show_progress=False, api_name="video_orientation_change")
524
  melody_filepath.change(load_melody_filepath, inputs=[melody_filepath, title, model,topp, temperature, cfg_coef, segment_length], outputs=[title, prompt_index , model, topp, temperature, cfg_coef, overlap], api_name="melody_filepath_change", queue=False)
525
  reuse_seed.click(fn=lambda x: x, inputs=[seed_used], outputs=[seed], queue=False, api_name="reuse_seed_click")
526
  autoplay_cb.change(fn=lambda x: gr.update(autoplay=x), inputs=[autoplay_cb], outputs=[output], queue=False, api_name="autoplay_cb_change")
 
595
  api_name="submit"
596
  ).then(
597
  predict,
598
+ inputs=[model, text,melody_filepath, duration, dimension, topk, topp, temperature, cfg_coef, background, title, settings_font, settings_font_color, seed, overlap, prompt_index, include_title, include_settings, harmony_only, user_profile, segment_length, settings_font_size, settings_animate_waveform, video_orientation],
599
  outputs=[output, wave_file, seed_used], scroll_to_output=True)
600
 
601
  # Show the interface