ghostofdivinity commited on
Commit
71e82b9
1 Parent(s): 4854ddd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -4
app.py CHANGED
@@ -1,7 +1,71 @@
1
  import gradio as gr
 
 
 
2
 
3
- def greet(name):
4
- return "Hi there " + name + "!!"
 
 
 
 
5
 
6
- iface = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import torch
3
+ import torchaudio
4
+ import IPython.display as ipd
5
 
6
+ # Generate new kick drum samples
7
+ generator.eval()
8
+ with torch.no_grad():
9
+ for i in range(num_generated_samples):
10
+ noise = torch.randn(1, latent_dim).to(device)
11
+ generated_sample = generator(noise).squeeze().cpu()
12
 
13
+ # Save the generated sample
14
+ output_filename = f"generated_kick_{i+1}.wav"
15
+ torchaudio.save(output_filename, generated_sample.unsqueeze(0), 16000)
16
+
17
+ # Play the generated sample
18
+ print(f"Generated Sample {i+1}:")
19
+ display(ipd.Audio(output_filename))
20
+
21
+ # Load the saved generator model
22
+ class Generator(nn.Module):
23
+ def __init__(self, latent_dim):
24
+ super(Generator, self).__init__()
25
+ self.generator = nn.Sequential(
26
+ nn.Linear(latent_dim, 1024),
27
+ nn.ReLU(),
28
+ nn.Linear(1024, 4096),
29
+ nn.ReLU(),
30
+ nn.Linear(4096, 8192),
31
+ nn.Tanh()
32
+ )
33
+
34
+ def forward(self, x):
35
+ return self.generator(x)
36
+
37
+ latent_dim = 100
38
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
39
+ generator = Generator(latent_dim).to(device)
40
+ generator_model_path = "generator_model.pkl"
41
+ generator.load_state_dict(torch.load(generator_model_path))
42
+
43
+ def generate_kick_drums():
44
+ # Define the number of samples you want to generate
45
+ num_generated_samples = 3
46
+ output_files = []
47
+
48
+ # Generate new kick drum samples
49
+ generator.eval()
50
+ with torch.no_grad():
51
+ for i in range(num_generated_samples):
52
+ noise = torch.randn(1, latent_dim).to(device)
53
+ generated_sample = generator(noise).squeeze().cpu()
54
+
55
+ # Save the generated sample
56
+ output_filename = f"generated_kick_{i+1}.wav"
57
+ torchaudio.save(output_filename, generated_sample.unsqueeze(0), 16000)
58
+
59
+ output_files.append(output_filename)
60
+
61
+ return tuple(output_files)
62
+
63
+ def gradio_interface():
64
+ generate_button = gr.Interface(fn=generate_kick_drums,
65
+ inputs=None,
66
+ outputs=[gr.Audio(type='filepath', label=f"generated_kick_{i+1}") for i in range(3)],
67
+ live=True)
68
+ generate_button.launch(debug=True)
69
+
70
+ # Run the Gradio interface
71
+ gradio_interface()