Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
690 lines (545 loc) · 26.5 KB
/
Copy pathapp.py
File metadata and controls
690 lines (545 loc) · 26.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
import streamlit as st
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader, TensorDataset
import torch.nn.functional as F
import matplotlib.pyplot as plt
import tempfile
import os
from scipy.io.wavfile import write
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
st.set_page_config(page_title=" Multi-Model Music Generator", page_icon="🎵", layout="wide")
# ===== MODEL 1: MusicVAE =====
class MusicVAE(nn.Module):
def __init__(self, input_dim=128, hidden_dim=512, latent_dim=64, num_layers=3):
super(MusicVAE, self).__init__()
encoder_layers = []
curr_dim = input_dim
for i in range(num_layers):
encoder_layers.extend([
nn.Linear(curr_dim, hidden_dim),
nn.BatchNorm1d(hidden_dim),
nn.LeakyReLU(0.2),
nn.Dropout(0.2)
])
curr_dim = hidden_dim
hidden_dim = hidden_dim // 2
self.encoder = nn.Sequential(*encoder_layers)
final_dim = curr_dim
self.mu = nn.Linear(final_dim, latent_dim)
self.logvar = nn.Linear(final_dim, latent_dim)
decoder_layers = []
curr_dim = latent_dim
hidden_dim = 128
for i in range(num_layers):
decoder_layers.extend([
nn.Linear(curr_dim, hidden_dim),
nn.BatchNorm1d(hidden_dim),
nn.LeakyReLU(0.2),
nn.Dropout(0.1)
])
curr_dim = hidden_dim
hidden_dim *= 2
decoder_layers.extend([
nn.Linear(curr_dim, input_dim),
nn.Sigmoid()
])
self.decoder = nn.Sequential(*decoder_layers)
self.temperature = nn.Parameter(torch.ones(1))
def encode(self, x):
h = self.encoder(x)
return self.mu(h), self.logvar(h)
def reparameterize(self, mu, logvar, temperature=1.0):
std = torch.exp(0.5 * logvar)
eps = torch.randn_like(std)
return mu + eps * std * temperature
def decode(self, z):
return self.decoder(z)
def forward(self, x):
mu, logvar = self.encode(x)
z = self.reparameterize(mu, logvar, self.temperature)
return self.decode(z), mu, logvar
# ===== MODEL 3: Music GAN =====
class MusicGenerator(nn.Module):
def __init__(self, latent_dim=100, hidden_dim=256, output_dim=128):
super(MusicGenerator, self).__init__()
self.net = nn.Sequential(
nn.Linear(latent_dim, hidden_dim),
nn.BatchNorm1d(hidden_dim),
nn.ReLU(True),
nn.Linear(hidden_dim, hidden_dim * 2),
nn.BatchNorm1d(hidden_dim * 2),
nn.ReLU(True),
nn.Linear(hidden_dim * 2, hidden_dim * 4),
nn.BatchNorm1d(hidden_dim * 4),
nn.ReLU(True),
nn.Linear(hidden_dim * 4, output_dim),
nn.Tanh()
)
def forward(self, z):
return self.net(z)
class MusicDiscriminator(nn.Module):
def __init__(self, input_dim=128, hidden_dim=256):
super(MusicDiscriminator, self).__init__()
self.net = nn.Sequential(
nn.Linear(input_dim, hidden_dim),
nn.LeakyReLU(0.2, inplace=True),
nn.Dropout(0.3),
nn.Linear(hidden_dim, hidden_dim // 2),
nn.LeakyReLU(0.2, inplace=True),
nn.Dropout(0.3),
nn.Linear(hidden_dim // 2, 1),
nn.Sigmoid()
)
def forward(self, x):
return self.net(x)
class MusicGAN(nn.Module):
def __init__(self, latent_dim=100, hidden_dim=256, output_dim=128):
super(MusicGAN, self).__init__()
self.generator = MusicGenerator(latent_dim, hidden_dim, output_dim)
self.discriminator = MusicDiscriminator(output_dim, hidden_dim)
self.latent_dim = latent_dim
def generate(self, batch_size=1):
z = torch.randn(batch_size, self.latent_dim).to(device)
return self.generator(z)
# ===== MODEL 4: Music RNN =====
class MusicRNN(nn.Module):
def __init__(self, input_dim=128, hidden_dim=256, num_layers=3, latent_dim=64):
super(MusicRNN, self).__init__()
self.hidden_dim = hidden_dim
self.num_layers = num_layers
self.latent_dim = latent_dim
self.encoder_rnn = nn.LSTM(1, hidden_dim, num_layers, batch_first=True, dropout=0.2)
self.decoder_rnn = nn.LSTM(latent_dim, hidden_dim, num_layers, batch_first=True, dropout=0.2)
self.mu_proj = nn.Linear(hidden_dim, latent_dim)
self.logvar_proj = nn.Linear(hidden_dim, latent_dim)
self.output_proj = nn.Linear(hidden_dim, 1)
def encode(self, x):
x = x.unsqueeze(-1) # Add feature dimension
_, (hidden, _) = self.encoder_rnn(x)
hidden = hidden[-1] # Take last layer
mu = self.mu_proj(hidden)
logvar = self.logvar_proj(hidden)
return mu, logvar
def decode(self, z):
z = z.unsqueeze(1).repeat(1, 128, 1) # Repeat for sequence length
output, _ = self.decoder_rnn(z)
return torch.sigmoid(self.output_proj(output).squeeze(-1))
def reparameterize(self, mu, logvar):
std = torch.exp(0.5 * logvar)
eps = torch.randn_like(std)
return mu + eps * std
def forward(self, x):
mu, logvar = self.encode(x)
z = self.reparameterize(mu, logvar)
return self.decode(z), mu, logvar
# ===== MODEL 5: Music Diffusion =====
class MusicDiffusion(nn.Module):
def __init__(self, input_dim=128, hidden_dim=256, timesteps=1000):
super(MusicDiffusion, self).__init__()
self.timesteps = timesteps
self.noise_predictor = nn.Sequential(
nn.Linear(input_dim + 1, hidden_dim), # +1 for timestep
nn.ReLU(),
nn.Linear(hidden_dim, hidden_dim * 2),
nn.ReLU(),
nn.Linear(hidden_dim * 2, hidden_dim),
nn.ReLU(),
nn.Linear(hidden_dim, input_dim)
)
# Beta schedule for noise
self.register_buffer('betas', torch.linspace(0.0001, 0.02, timesteps))
self.register_buffer('alphas', 1.0 - self.betas)
self.register_buffer('alphas_cumprod', torch.cumprod(self.alphas, dim=0))
def forward_diffusion(self, x, t):
noise = torch.randn_like(x)
sqrt_alphas_cumprod = torch.sqrt(self.alphas_cumprod[t]).unsqueeze(-1)
sqrt_one_minus_alphas_cumprod = torch.sqrt(1.0 - self.alphas_cumprod[t]).unsqueeze(-1)
return (sqrt_alphas_cumprod * x + sqrt_one_minus_alphas_cumprod * noise), noise
def forward(self, x):
batch_size = x.shape[0]
t = torch.randint(0, self.timesteps, (batch_size,)).to(device)
x_noisy, noise = self.forward_diffusion(x, t)
# Predict noise
t_normalized = t.float() / self.timesteps
x_with_time = torch.cat([x_noisy, t_normalized.unsqueeze(-1)], dim=-1)
predicted_noise = self.noise_predictor(x_with_time)
return predicted_noise, noise
def sample(self, batch_size=1):
x = torch.randn(batch_size, 128).to(device)
for t in reversed(range(self.timesteps)):
if t > 0:
noise = torch.randn_like(x)
else:
noise = torch.zeros_like(x)
t_tensor = torch.full((batch_size,), t).to(device)
t_normalized = t_tensor.float() / self.timesteps
x_with_time = torch.cat([x, t_normalized.unsqueeze(-1)], dim=-1)
predicted_noise = self.noise_predictor(x_with_time)
alpha = self.alphas[t]
alpha_cumprod = self.alphas_cumprod[t]
beta = self.betas[t]
if t > 0:
alpha_cumprod_prev = self.alphas_cumprod[t-1]
else:
alpha_cumprod_prev = torch.tensor(1.0)
x = (1/torch.sqrt(alpha)) * (x - ((1-alpha)/torch.sqrt(1-alpha_cumprod)) * predicted_noise)
if t > 0:
x += torch.sqrt(beta) * noise
return torch.sigmoid(x)
def create_diverse_music_data(n_samples=2000, sequence_length=128):
"""Create diverse music data with multiple styles"""
data = []
styles = ['rhythmic', 'melodic', 'harmonic', 'ambient', 'percussive']
for i in range(n_samples):
pattern = np.zeros(sequence_length)
style = styles[i % len(styles)]
if style == 'rhythmic':
for beat in range(0, sequence_length, 4):
pattern[beat] = 1.0
if beat + 2 < sequence_length:
pattern[beat + 2] = 0.7
elif style == 'melodic':
scale = [0, 2, 4, 5, 7, 9, 11]
for note_idx in range(0, sequence_length, 8):
scale_note = scale[np.random.randint(0, len(scale))]
if note_idx + scale_note < sequence_length:
pattern[note_idx + scale_note] = np.random.uniform(0.5, 1.0)
elif style == 'harmonic':
chords = [[0, 4, 7], [5, 9, 12], [7, 11, 14], [2, 5, 9]]
for chord_idx in range(0, sequence_length, 32):
chord = chords[np.random.randint(0, len(chords))]
for note in chord:
if chord_idx + note < sequence_length:
pattern[chord_idx + note] = np.random.uniform(0.6, 0.9)
elif style == 'ambient':
for i in range(sequence_length):
if np.random.random() > 0.8:
end_idx = min(i + 8, sequence_length)
pattern[i:end_idx] = np.random.uniform(0.3, 0.6)
elif style == 'percussive':
for beat in [0, 6, 12, 18, 24, 30]:
if beat < sequence_length:
pattern[beat] = 1.0
for snare in [8, 16, 24]:
if snare < sequence_length:
pattern[snare] = 0.8
pattern += np.random.normal(0, 0.05, sequence_length)
pattern = np.clip(pattern, 0, 1)
data.append(pattern)
return np.array(data, dtype=np.float32)
def train_model(model, model_type, train_loader, epochs=100):
"""Universal training function for all models"""
if model_type == 'GAN':
return train_gan(model, train_loader, epochs)
elif model_type == 'Diffusion':
return train_diffusion(model, train_loader, epochs)
else:
return train_vae_style(model, train_loader, epochs)
def train_vae_style(model, train_loader, epochs=100):
optimizer = optim.AdamW(model.parameters(), lr=2e-4, weight_decay=1e-5)
scheduler = optim.lr_scheduler.CosineAnnealingLR(optimizer, epochs)
model.train()
progress_bar = st.progress(0)
loss_history = []
for epoch in range(epochs):
total_loss = 0
beta = min(1.0, epoch / (epochs * 0.5))
for batch_idx, (data,) in enumerate(train_loader):
data = data.to(device)
optimizer.zero_grad()
recon_batch, mu, logvar = model(data)
recon_loss = F.binary_cross_entropy(recon_batch, data, reduction='sum')
kl_loss = -0.5 * torch.sum(1 + logvar - mu.pow(2) - logvar.exp())
loss = recon_loss + beta * kl_loss
loss.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)
optimizer.step()
total_loss += loss.item()
scheduler.step()
avg_loss = total_loss / len(train_loader.dataset)
loss_history.append(avg_loss)
progress_bar.progress((epoch + 1) / epochs)
if epoch % 20 == 0:
st.write(f'Epoch {epoch}, Loss: {avg_loss:.4f}')
return loss_history
def train_gan(model, train_loader, epochs=100):
g_optimizer = optim.Adam(model.generator.parameters(), lr=2e-4, betas=(0.5, 0.999))
d_optimizer = optim.Adam(model.discriminator.parameters(), lr=2e-4, betas=(0.5, 0.999))
progress_bar = st.progress(0)
g_losses, d_losses = [], []
for epoch in range(epochs):
g_loss_total, d_loss_total = 0, 0
for batch_idx, (real_data,) in enumerate(train_loader):
real_data = real_data.to(device)
batch_size = real_data.size(0)
# Train Discriminator
d_optimizer.zero_grad()
real_labels = torch.ones(batch_size, 1).to(device)
fake_labels = torch.zeros(batch_size, 1).to(device)
real_output = model.discriminator(real_data)
d_real_loss = F.binary_cross_entropy(real_output, real_labels)
fake_data = model.generate(batch_size)
fake_output = model.discriminator(fake_data.detach())
d_fake_loss = F.binary_cross_entropy(fake_output, fake_labels)
d_loss = d_real_loss + d_fake_loss
d_loss.backward()
d_optimizer.step()
# Train Generator
g_optimizer.zero_grad()
fake_output = model.discriminator(fake_data)
g_loss = F.binary_cross_entropy(fake_output, real_labels)
g_loss.backward()
g_optimizer.step()
g_loss_total += g_loss.item()
d_loss_total += d_loss.item()
g_losses.append(g_loss_total / len(train_loader))
d_losses.append(d_loss_total / len(train_loader))
progress_bar.progress((epoch + 1) / epochs)
if epoch % 20 == 0:
st.write(f'Epoch {epoch}, G Loss: {g_losses[-1]:.4f}, D Loss: {d_losses[-1]:.4f}')
return g_losses, d_losses
def train_diffusion(model, train_loader, epochs=100):
optimizer = optim.AdamW(model.parameters(), lr=1e-4)
progress_bar = st.progress(0)
loss_history = []
for epoch in range(epochs):
total_loss = 0
for batch_idx, (data,) in enumerate(train_loader):
data = data.to(device)
optimizer.zero_grad()
predicted_noise, actual_noise = model(data)
loss = F.mse_loss(predicted_noise, actual_noise)
loss.backward()
optimizer.step()
total_loss += loss.item()
avg_loss = total_loss / len(train_loader.dataset)
loss_history.append(avg_loss)
progress_bar.progress((epoch + 1) / epochs)
if epoch % 20 == 0:
st.write(f'Epoch {epoch}, Loss: {avg_loss:.4f}')
return loss_history
def generate_music(model, model_type, num_samples=1, **kwargs):
"""Universal generation function"""
model.eval()
with torch.no_grad():
if model_type == 'GAN':
return model.generate(num_samples).cpu().numpy()
elif model_type == 'Diffusion':
return model.sample(num_samples).cpu().numpy()
else: # VAE-style models
latent_dim = kwargs.get('latent_dim', 64)
temperature = kwargs.get('temperature', 1.0)
z = torch.randn(num_samples, latent_dim).to(device) * temperature
if hasattr(model, 'decode'):
generated = model.decode(z).cpu().numpy()
else:
generated, _, _ = model(torch.zeros(num_samples, 128).to(device))
generated = generated.cpu().numpy()
return generated
def music_to_audio(music_data, sample_rate=22050, duration=6):
"""Convert music data to audio"""
time = np.linspace(0, duration, int(sample_rate * duration))
audio = np.zeros_like(time)
base_freqs = [110, 220, 440, 880]
segment_length = len(time) // len(music_data)
for i, intensity in enumerate(music_data):
if intensity > 0.05:
freq_range_idx = i % len(base_freqs)
base_freq = base_freqs[freq_range_idx]
freq = base_freq * (2 ** ((i % 24) / 12))
start_idx = i * segment_length
end_idx = min((i + 1) * segment_length, len(time))
segment_time = time[start_idx:end_idx]
wave = intensity * (
np.sin(2 * np.pi * freq * segment_time) +
0.3 * np.sin(2 * np.pi * freq * 2 * segment_time) +
0.1 * np.sin(2 * np.pi * freq * 3 * segment_time)
)
envelope = np.exp(-segment_time * 2)
wave *= envelope
audio[start_idx:end_idx] += wave
audio = np.tanh(audio)
if np.max(np.abs(audio)) > 0:
audio = audio / np.max(np.abs(audio)) * 0.7
return (audio * 32767).astype(np.int16)
def main():
st.title("🎵 Multi-Model Music Generator")
st.write("Choose from 4 different AI models to generate music")
# Model selection
model_choice = st.selectbox(
"Choose Model Architecture",
["MusicVAE","MusicGAN", "MusicRNN", "MusicDiffusion"]
)
st.sidebar.header(f"{model_choice} Parameters")
# Model-specific parameters
if model_choice in ["MusicVAE", "MusicRNN"]:
latent_dim = st.sidebar.slider("Latent Dimension", 16, 128, 64)
temperature = st.sidebar.slider("Generation Temperature", 0.1, 2.0, 1.0)
elif model_choice == "MusicGAN":
latent_dim = st.sidebar.slider("Noise Dimension", 50, 200, 100)
elif model_choice == "MusicDiffusion":
timesteps = st.sidebar.slider("Diffusion Steps", 100, 1000, 500)
hidden_dim = st.sidebar.slider("Hidden Dimension", 128, 512, 256)
epochs = st.sidebar.slider("Training Epochs", 10, 450, 80)
# Initialize session state
model_key = f'{model_choice.lower()}_model'
trained_key = f'{model_choice.lower()}_trained'
if model_key not in st.session_state:
st.session_state[model_key] = None
if trained_key not in st.session_state:
st.session_state[trained_key] = False
col1, col2 = st.columns(2)
with col1:
st.header(f"Train {model_choice}")
# Model info
model_descriptions = {
"MusicVAE": "Variational Autoencoder with hierarchical architecture for learning musical latent representations",
"MusicTransformer": "Transformer-based model using self-attention for capturing long-range musical dependencies",
"MusicGAN": "Generative Adversarial Network with generator and discriminator for realistic music synthesis",
"MusicRNN": "Recurrent Neural Network using LSTM layers for sequential music pattern modeling",
"MusicDiffusion": "Denoising diffusion model for high-quality music generation through iterative refinement"
}
st.info(model_descriptions[model_choice])
if st.button(f"Train {model_choice}"):
with st.spinner(f"Training {model_choice}..."):
# Create data
music_data = create_diverse_music_data(n_samples=1500, sequence_length=128)
dataset = TensorDataset(torch.tensor(music_data))
train_loader = DataLoader(dataset, batch_size=32, shuffle=True)
# Initialize model
if model_choice == "MusicVAE":
model = MusicVAE(128, hidden_dim, latent_dim, 3)
elif model_choice == "MusicGAN":
model = MusicGAN(latent_dim, hidden_dim, 128)
elif model_choice == "MusicRNN":
model = MusicRNN(128, hidden_dim, 3, latent_dim)
elif model_choice == "MusicDiffusion":
model = MusicDiffusion(128, hidden_dim, timesteps if 'timesteps' in locals() else 500)
model.to(device)
# Train model
model_type = model_choice.replace('Music', '')
loss_history = train_model(model, model_type, train_loader, epochs)
# Store model
st.session_state[model_key] = model
st.session_state[trained_key] = True
# Plot results
fig, ax = plt.subplots(figsize=(10, 4))
if model_choice == "MusicGAN":
ax.plot(loss_history[0], label='Generator Loss')
ax.plot(loss_history[1], label='Discriminator Loss')
ax.legend()
else:
ax.plot(loss_history, label='Training Loss')
ax.set_xlabel('Epoch')
ax.set_ylabel('Loss')
ax.set_title(f'{model_choice} Training Progress')
st.pyplot(fig)
final_loss = loss_history[0][-1] if model_choice == "MusicGAN" else loss_history[-1]
st.success(f"{model_choice} trained! Final loss: {final_loss:.4f}")
with col2:
st.header(f"Generate with {model_choice}")
if st.session_state[trained_key] and st.session_state[model_key] is not None:
prompt = st.text_input("Style Description", f"{model_choice} generated music")
num_generations = st.slider("Number of Generations", 1, 3, 1)
if st.button("Generate Music"):
with st.spinner("Generating music..."):
model = st.session_state[model_key]
model_type = model_choice.replace('Music', '')
# Generate
kwargs = {}
if model_choice in ["MusicVAE", "MusicRNN"]:
kwargs = {'latent_dim': latent_dim, 'temperature': temperature}
generated_music = generate_music(model, model_type, num_generations, **kwargs)
for i, music_sequence in enumerate(generated_music):
st.subheader(f"Generation {i+1}")
# Visualize
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(12, 6))
ax1.plot(music_sequence)
ax1.set_title(f'{prompt} - Sequence {i+1}')
ax1.set_ylabel('Intensity')
spectrum = np.abs(np.fft.fft(music_sequence))[:64]
ax2.plot(spectrum)
ax2.set_title('Frequency Spectrum')
ax2.set_xlabel('Frequency Bin')
ax2.set_ylabel('Magnitude')
st.pyplot(fig)
# Convert to audio
audio_data = music_to_audio(music_sequence)
with tempfile.NamedTemporaryFile(delete=False, suffix='.wav') as tmp_file:
write(tmp_file.name, 22050, audio_data)
with open(tmp_file.name, 'rb') as f:
audio_bytes = f.read()
st.audio(audio_bytes, format='audio/wav')
st.download_button(
label=f"Download {model_choice} Audio {i+1}",
data=audio_bytes,
file_name=f"{model_choice.lower()}_music_{i+1}.wav",
mime="audio/wav",
key=f"download_{model_choice}_{i}"
)
os.unlink(tmp_file.name)
else:
st.info(f"Train the {model_choice} model first!")
# Model comparison section
st.header("🔍 Model Comparison")
comparison_data = {
"Model": ["MusicVAE", "MusicTransformer", "MusicGAN", "MusicRNN", "MusicDiffusion"],
"Architecture": ["Variational Autoencoder", "Transformer", "Adversarial Network", "LSTM Network", "Diffusion Model"],
"Strengths": [
"Smooth interpolation, structured latent space",
"Long-range dependencies, attention mechanism",
"Realistic generation, adversarial training",
"Sequential modeling, temporal patterns",
"High quality output, stable training"
],
"Best For": [
"Style transfer, latent exploration",
"Complex compositions, harmony",
"Realistic textures, variety",
"Melody generation, rhythm",
"High-fidelity synthesis"
]
}
col1, col2 = st.columns(2)
with col1:
st.subheader("Architecture Details")
for i, model in enumerate(comparison_data["Model"]):
with st.expander(f"{model} Details"):
st.write(f"**Architecture:** {comparison_data['Architecture'][i]}")
st.write(f"**Strengths:** {comparison_data['Strengths'][i]}")
st.write(f"**Best For:** {comparison_data['Best For'][i]}")
with col2:
st.subheader("Training Tips")
st.write("""
**MusicVAE**:
- Higher latent dims for complex music
- Lower temperature for coherent output
**MusicTransformer**:
- More attention heads for harmony
- Deeper layers for complexity
**MusicGAN**:
- Balance generator/discriminator learning
- Higher noise dim for variety
**MusicRNN**:
- More LSTM layers for long sequences
- Bidirectional for better context
**MusicDiffusion**:
- More timesteps for quality
- Longer training for best results
""")
st.header("🎼 About This App")
st.write("""
This multi-model music generator implements 5 different AI architectures:
1. **MusicVAE**: Learns a continuous latent space for smooth music interpolation
2. **MusicTransformer**: Uses self-attention to capture musical structure and harmony
3. **MusicGAN**: Adversarial training for realistic and diverse music generation
4. **MusicRNN**: Sequential modeling with LSTM for temporal music patterns
5. **MusicDiffusion**: Iterative denoising for high-quality music synthesis
Each model has unique strengths and is suitable for different musical styles and applications.
Train multiple models to compare their outputs and find your preferred approach!
""")
if __name__ == "__main__":
main()