Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -11,7 +11,7 @@ from PIL import Image
|
|
11 |
import os
|
12 |
|
13 |
# Set device to 'cpu' or 'cuda' if available
|
14 |
-
device = torch.device('
|
15 |
|
16 |
# Parameters
|
17 |
sample_rate = 44100 # 44.1kHz stereo sounds
|
@@ -105,19 +105,6 @@ class Generator(nn.Module):
|
|
105 |
return generated_spectrogram
|
106 |
|
107 |
|
108 |
-
|
109 |
-
# Function to save audio
|
110 |
-
def save_audio(audio, path, sample_rate=44100):
|
111 |
-
# Ensure audio is in stereo by checking the channels
|
112 |
-
if audio.dim() == 1:
|
113 |
-
audio = audio.unsqueeze(0).repeat(2, 1) # Convert mono to stereo
|
114 |
-
elif audio.size(0) == 1:
|
115 |
-
audio = audio.repeat(2, 1) # Convert mono to stereo
|
116 |
-
|
117 |
-
# Save audio to a file
|
118 |
-
torchaudio.save(path, audio, sample_rate)
|
119 |
-
|
120 |
-
|
121 |
# Function to generate and save audio from a test image using the pre-trained GAN model
|
122 |
def test_model(generator, test_img_path, output_audio_path, device):
|
123 |
# Load and preprocess test image
|
@@ -134,9 +121,6 @@ def test_model(generator, test_img_path, output_audio_path, device):
|
|
134 |
# Convert the generated spectrogram to audio
|
135 |
generated_audio = spectrogram_to_audio(generated_spectrogram.squeeze(0).cpu()) # Remove batch dimension
|
136 |
|
137 |
-
# Save the generated audio
|
138 |
-
save_audio(generated_audio, output_audio_path)
|
139 |
-
|
140 |
print(f"Generated audio saved to {output_audio_path}")
|
141 |
|
142 |
# Load the pre-trained GAN model
|
|
|
11 |
import os
|
12 |
|
13 |
# Set device to 'cpu' or 'cuda' if available
|
14 |
+
device = torch.device('cpu')
|
15 |
|
16 |
# Parameters
|
17 |
sample_rate = 44100 # 44.1kHz stereo sounds
|
|
|
105 |
return generated_spectrogram
|
106 |
|
107 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
# Function to generate and save audio from a test image using the pre-trained GAN model
|
109 |
def test_model(generator, test_img_path, output_audio_path, device):
|
110 |
# Load and preprocess test image
|
|
|
121 |
# Convert the generated spectrogram to audio
|
122 |
generated_audio = spectrogram_to_audio(generated_spectrogram.squeeze(0).cpu()) # Remove batch dimension
|
123 |
|
|
|
|
|
|
|
124 |
print(f"Generated audio saved to {output_audio_path}")
|
125 |
|
126 |
# Load the pre-trained GAN model
|