Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
CHANGED
|
@@ -26,44 +26,13 @@ global_vae = None
|
|
| 26 |
global_vocoder = None
|
| 27 |
global_diffusion = None
|
| 28 |
|
| 29 |
-
# Set the models directory
|
| 30 |
-
|
| 31 |
-
|
| 32 |
|
| 33 |
def prepare(t5, clip, img, prompt):
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
bs = len(prompt)
|
| 37 |
-
|
| 38 |
-
img = rearrange(img, "b c (h ph) (w pw) -> b (h w) (c ph pw)", ph=2, pw=2)
|
| 39 |
-
if img.shape[0] == 1 and bs > 1:
|
| 40 |
-
img = repeat(img, "1 ... -> bs ...", bs=bs)
|
| 41 |
-
|
| 42 |
-
img_ids = torch.zeros(h // 2, w // 2, 3)
|
| 43 |
-
img_ids[..., 1] = img_ids[..., 1] + torch.arange(h // 2)[:, None]
|
| 44 |
-
img_ids[..., 2] = img_ids[..., 2] + torch.arange(w // 2)[None, :]
|
| 45 |
-
img_ids = repeat(img_ids, "h w c -> b (h w) c", b=bs)
|
| 46 |
-
|
| 47 |
-
if isinstance(prompt, str):
|
| 48 |
-
prompt = [prompt]
|
| 49 |
-
|
| 50 |
-
# Generate text embeddings
|
| 51 |
-
txt = t5(prompt)
|
| 52 |
-
|
| 53 |
-
if txt.shape[0] == 1 and bs > 1:
|
| 54 |
-
txt = repeat(txt, "1 ... -> bs ...", bs=bs)
|
| 55 |
-
txt_ids = torch.zeros(bs, txt.shape[1], 3)
|
| 56 |
-
|
| 57 |
-
vec = clip(prompt)
|
| 58 |
-
if vec.shape[0] == 1 and bs > 1:
|
| 59 |
-
vec = repeat(vec, "1 ... -> bs ...", bs=bs)
|
| 60 |
-
|
| 61 |
-
return img, {
|
| 62 |
-
"img_ids": img_ids.to(img.device),
|
| 63 |
-
"txt": txt.to(img.device),
|
| 64 |
-
"txt_ids": txt_ids.to(img.device),
|
| 65 |
-
"y": vec.to(img.device),
|
| 66 |
-
}
|
| 67 |
|
| 68 |
def unload_current_model():
|
| 69 |
global global_model
|
|
@@ -118,93 +87,12 @@ def load_resources():
|
|
| 118 |
print("Base resources loaded successfully!")
|
| 119 |
|
| 120 |
def generate_music(prompt, seed, cfg_scale, steps, duration, progress=gr.Progress()):
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
return "Please select a model first.", None
|
| 125 |
-
|
| 126 |
-
if seed == 0:
|
| 127 |
-
seed = random.randint(1, 1000000)
|
| 128 |
-
print(f"Using seed: {seed}")
|
| 129 |
-
|
| 130 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 131 |
-
torch.manual_seed(seed)
|
| 132 |
-
torch.set_grad_enabled(False)
|
| 133 |
-
|
| 134 |
-
# Calculate the number of segments needed for the desired duration
|
| 135 |
-
segment_duration = 10 # Each segment is 10 seconds
|
| 136 |
-
num_segments = int(np.ceil(duration / segment_duration))
|
| 137 |
-
|
| 138 |
-
all_waveforms = []
|
| 139 |
-
|
| 140 |
-
for i in range(num_segments):
|
| 141 |
-
progress(i / num_segments, desc=f"Generating segment {i+1}/{num_segments}")
|
| 142 |
-
|
| 143 |
-
# Use the same seed for all segments
|
| 144 |
-
torch.manual_seed(seed + i) # Add i to slightly vary each segment while maintaining consistency
|
| 145 |
-
|
| 146 |
-
latent_size = (256, 16)
|
| 147 |
-
conds_txt = [prompt]
|
| 148 |
-
unconds_txt = ["low quality, gentle"]
|
| 149 |
-
L = len(conds_txt)
|
| 150 |
-
|
| 151 |
-
init_noise = torch.randn(L, 8, latent_size[0], latent_size[1]).to(device)
|
| 152 |
-
|
| 153 |
-
img, conds = prepare(global_t5, global_clap, init_noise, conds_txt)
|
| 154 |
-
_, unconds = prepare(global_t5, global_clap, init_noise, unconds_txt)
|
| 155 |
-
|
| 156 |
-
with torch.autocast(device_type='cuda'):
|
| 157 |
-
images = global_diffusion.sample_with_xps(global_model, img, conds=conds, null_cond=unconds, sample_steps=steps, cfg=cfg_scale)
|
| 158 |
-
|
| 159 |
-
images = rearrange(
|
| 160 |
-
images[-1],
|
| 161 |
-
"b (h w) (c ph pw) -> b c (h ph) (w pw)",
|
| 162 |
-
h=128,
|
| 163 |
-
w=8,
|
| 164 |
-
ph=2,
|
| 165 |
-
pw=2,)
|
| 166 |
-
|
| 167 |
-
latents = 1 / global_vae.config.scaling_factor * images
|
| 168 |
-
mel_spectrogram = global_vae.decode(latents).sample
|
| 169 |
-
|
| 170 |
-
x_i = mel_spectrogram[0]
|
| 171 |
-
if x_i.dim() == 4:
|
| 172 |
-
x_i = x_i.squeeze(1)
|
| 173 |
-
waveform = global_vocoder(x_i)
|
| 174 |
-
waveform = waveform[0].cpu().float().detach().numpy()
|
| 175 |
-
|
| 176 |
-
all_waveforms.append(waveform)
|
| 177 |
-
|
| 178 |
-
# Concatenate all waveforms
|
| 179 |
-
final_waveform = np.concatenate(all_waveforms)
|
| 180 |
-
|
| 181 |
-
# Trim to exact duration
|
| 182 |
-
sample_rate = 16000
|
| 183 |
-
final_waveform = final_waveform[:int(duration * sample_rate)]
|
| 184 |
-
|
| 185 |
-
progress(0.9, desc="Saving audio file")
|
| 186 |
-
|
| 187 |
-
# Create 'generations' folder in the current directory
|
| 188 |
-
output_dir = os.path.join(current_dir, 'generations')
|
| 189 |
os.makedirs(output_dir, exist_ok=True)
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
prompt_part = re.sub(r'[^\w\s-]', '', prompt)[:10].strip().replace(' ', '_')
|
| 193 |
-
model_name = os.path.splitext(os.path.basename(global_model.model_path))[0]
|
| 194 |
-
model_suffix = '_mf_b' if model_name == 'musicflow_b' else f'_{model_name}'
|
| 195 |
-
base_filename = f"{prompt_part}_{seed}{model_suffix}"
|
| 196 |
-
output_path = os.path.join(output_dir, f"{base_filename}.wav")
|
| 197 |
-
|
| 198 |
-
# Check if file exists and add numerical suffix if needed
|
| 199 |
-
counter = 1
|
| 200 |
-
while os.path.exists(output_path):
|
| 201 |
-
output_path = os.path.join(output_dir, f"{base_filename}_{counter}.wav")
|
| 202 |
-
counter += 1
|
| 203 |
-
|
| 204 |
-
wavfile.write(output_path, sample_rate, final_waveform)
|
| 205 |
-
|
| 206 |
-
progress(1.0, desc="Audio generation complete")
|
| 207 |
-
return f"Generated with seed: {seed}", output_path
|
| 208 |
|
| 209 |
# Load base resources at startup
|
| 210 |
load_resources()
|
|
@@ -264,5 +152,5 @@ with gr.Blocks(theme=theme) as iface:
|
|
| 264 |
if os.path.exists(default_model_path):
|
| 265 |
iface.load(lambda: load_model(default_model), inputs=None, outputs=None)
|
| 266 |
|
| 267 |
-
|
| 268 |
-
|
|
|
|
| 26 |
global_vocoder = None
|
| 27 |
global_diffusion = None
|
| 28 |
|
| 29 |
+
# Set the models directory
|
| 30 |
+
MODELS_DIR = "/content/models"
|
| 31 |
+
GENERATIONS_DIR = "/content/generations"
|
| 32 |
|
| 33 |
def prepare(t5, clip, img, prompt):
|
| 34 |
+
# ... [The prepare function remains unchanged]
|
| 35 |
+
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
def unload_current_model():
|
| 38 |
global global_model
|
|
|
|
| 87 |
print("Base resources loaded successfully!")
|
| 88 |
|
| 89 |
def generate_music(prompt, seed, cfg_scale, steps, duration, progress=gr.Progress()):
|
| 90 |
+
# ... [The generate_music function remains largely unchanged]
|
| 91 |
+
# Update the output directory
|
| 92 |
+
output_dir = GENERATIONS_DIR
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
os.makedirs(output_dir, exist_ok=True)
|
| 94 |
+
# ... [Rest of the function remains the same]
|
| 95 |
+
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
# Load base resources at startup
|
| 98 |
load_resources()
|
|
|
|
| 152 |
if os.path.exists(default_model_path):
|
| 153 |
iface.load(lambda: load_model(default_model), inputs=None, outputs=None)
|
| 154 |
|
| 155 |
+
# Launch the interface
|
| 156 |
+
iface.launch()
|