flosstradamus commited on
Commit
c4fb7a3
·
verified ·
1 Parent(s): 0dacaeb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -13
app.py CHANGED
@@ -125,18 +125,23 @@ def load_model(model_name, device, model_url=None):
125
  def load_resources(device):
126
  global global_t5, global_clap, global_vae, global_vocoder, global_diffusion
127
 
128
- print("Loading T5 and CLAP models...")
129
- global_t5 = load_t5(device, max_length=256)
130
- global_clap = load_clap(device, max_length=256)
131
-
132
- print("Loading VAE and vocoder...")
133
- global_vae = AutoencoderKL.from_pretrained('cvssp/audioldm2', subfolder="vae").to(device)
134
- global_vocoder = SpeechT5HifiGan.from_pretrained('cvssp/audioldm2', subfolder="vocoder").to(device)
135
-
136
- print("Initializing diffusion...")
137
- global_diffusion = RF()
138
-
139
- print("Base resources loaded successfully!")
 
 
 
 
 
140
 
141
  def generate_music(prompt, seed, cfg_scale, steps, duration, device, batch_size=4, progress=gr.Progress()):
142
  global global_model, global_t5, global_clap, global_vae, global_vocoder, global_diffusion
@@ -144,6 +149,9 @@ def generate_music(prompt, seed, cfg_scale, steps, duration, device, batch_size=
144
  if global_model is None:
145
  return "Please select and load a model first.", None
146
 
 
 
 
147
  if seed == 0:
148
  seed = random.randint(1, 1000000)
149
  print(f"Using seed: {seed}")
@@ -293,6 +301,9 @@ with gr.Blocks(theme=theme) as iface:
293
  output_audio = gr.Audio(type="filepath")
294
 
295
  def on_load_model_click(model_name, device, url):
 
 
 
296
  if url:
297
  result = load_model(None, device, model_url=url)
298
  else:
@@ -303,7 +314,7 @@ with gr.Blocks(theme=theme) as iface:
303
  generate_button.click(generate_music, inputs=[prompt, seed, cfg_scale, steps, duration, device_choice], outputs=[output_status, output_audio])
304
 
305
  # Load default model and resources on startup
306
- iface.load(lambda: (load_resources("cpu"), on_load_model_click(default_model, "cpu", None)), inputs=None, outputs=None)
307
 
308
  # Launch the interface
309
  iface.launch()
 
125
  def load_resources(device):
126
  global global_t5, global_clap, global_vae, global_vocoder, global_diffusion
127
 
128
+ try:
129
+ print("Loading T5 and CLAP models...")
130
+ global_t5 = load_t5(device, max_length=256)
131
+ global_clap = load_clap(device, max_length=256)
132
+
133
+ print("Loading VAE and vocoder...")
134
+ global_vae = AutoencoderKL.from_pretrained('cvssp/audioldm2', subfolder="vae").to(device)
135
+ global_vocoder = SpeechT5HifiGan.from_pretrained('cvssp/audioldm2', subfolder="vocoder").to(device)
136
+
137
+ print("Initializing diffusion...")
138
+ global_diffusion = RF()
139
+
140
+ print("Base resources loaded successfully!")
141
+ return "Resources loaded successfully!"
142
+ except Exception as e:
143
+ print(f"Error loading resources: {str(e)}")
144
+ return f"Failed to load resources. Error: {str(e)}"
145
 
146
  def generate_music(prompt, seed, cfg_scale, steps, duration, device, batch_size=4, progress=gr.Progress()):
147
  global global_model, global_t5, global_clap, global_vae, global_vocoder, global_diffusion
 
149
  if global_model is None:
150
  return "Please select and load a model first.", None
151
 
152
+ if global_t5 is None or global_clap is None or global_vae is None or global_vocoder is None or global_diffusion is None:
153
+ return "Resources not properly loaded. Please reload the page and try again.", None
154
+
155
  if seed == 0:
156
  seed = random.randint(1, 1000000)
157
  print(f"Using seed: {seed}")
 
301
  output_audio = gr.Audio(type="filepath")
302
 
303
  def on_load_model_click(model_name, device, url):
304
+ resource_status = load_resources(device)
305
+ if "Failed" in resource_status:
306
+ return resource_status
307
  if url:
308
  result = load_model(None, device, model_url=url)
309
  else:
 
314
  generate_button.click(generate_music, inputs=[prompt, seed, cfg_scale, steps, duration, device_choice], outputs=[output_status, output_audio])
315
 
316
  # Load default model and resources on startup
317
+ iface.load(lambda: on_load_model_click(default_model, "cpu", None), inputs=None, outputs=None)
318
 
319
  # Launch the interface
320
  iface.launch()