Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -74,6 +74,13 @@ def save_spectrogram_image(spectrogram, name):
|
|
| 74 |
plt.close()
|
| 75 |
return temp_image_path
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
# Process the input image and convert to audio
|
| 78 |
def process_image(input_image):
|
| 79 |
# Generate a unique name based on the current time
|
|
@@ -86,6 +93,9 @@ def process_image(input_image):
|
|
| 86 |
pixels = expand_dims(pixels, 0)
|
| 87 |
return pixels
|
| 88 |
|
|
|
|
|
|
|
|
|
|
| 89 |
# Preprocess the input
|
| 90 |
src_image = load_image(input_image)
|
| 91 |
|
|
@@ -113,27 +123,28 @@ def process_image(input_image):
|
|
| 113 |
audio_file_path = f"{image_name}_generated_audio.wav"
|
| 114 |
sf.write(audio_file_path, wav, samplerate=44100)
|
| 115 |
|
| 116 |
-
return spectrogram_image_path, audio_file_path # Return
|
| 117 |
|
| 118 |
# Gradio Interface
|
| 119 |
def gradio_process_image(input_image):
|
| 120 |
-
spectrogram_image_path, audio_file_path = process_image(input_image)
|
| 121 |
|
| 122 |
# After Gradio finishes using these files, delete them to avoid keeping them around
|
| 123 |
def cleanup():
|
|
|
|
| 124 |
os.remove(spectrogram_image_path)
|
| 125 |
os.remove(audio_file_path)
|
| 126 |
-
print(f"Deleted temp files: {spectrogram_image_path}, {audio_file_path}")
|
| 127 |
|
| 128 |
-
return spectrogram_image_path, audio_file_path, cleanup
|
| 129 |
|
| 130 |
# Create the Gradio interface
|
| 131 |
interface = gr.Interface(
|
| 132 |
fn=gradio_process_image,
|
| 133 |
inputs=gr.Image(type="pil"), # Input is an image
|
| 134 |
-
outputs=[gr.Image(type="filepath"), gr.Audio(type="filepath")], # Output
|
| 135 |
title="Image to Audio Generator with Spectrogram Display",
|
| 136 |
-
description="Upload an image and get an audio file generated using Pix2Pix.",
|
| 137 |
)
|
| 138 |
|
| 139 |
# Launch the interface
|
|
|
|
| 74 |
plt.close()
|
| 75 |
return temp_image_path
|
| 76 |
|
| 77 |
+
# Save the uploaded image with the same timestamp
|
| 78 |
+
def save_uploaded_image(input_image, name):
|
| 79 |
+
# Save the uploaded image with the same unique timestamp name
|
| 80 |
+
uploaded_image_path = f"{name}_uploaded_image.png"
|
| 81 |
+
input_image.save(uploaded_image_path)
|
| 82 |
+
return uploaded_image_path
|
| 83 |
+
|
| 84 |
# Process the input image and convert to audio
|
| 85 |
def process_image(input_image):
|
| 86 |
# Generate a unique name based on the current time
|
|
|
|
| 93 |
pixels = expand_dims(pixels, 0)
|
| 94 |
return pixels
|
| 95 |
|
| 96 |
+
# Save the uploaded image with the unique timestamp name
|
| 97 |
+
uploaded_image_path = save_uploaded_image(input_image, image_name)
|
| 98 |
+
|
| 99 |
# Preprocess the input
|
| 100 |
src_image = load_image(input_image)
|
| 101 |
|
|
|
|
| 123 |
audio_file_path = f"{image_name}_generated_audio.wav"
|
| 124 |
sf.write(audio_file_path, wav, samplerate=44100)
|
| 125 |
|
| 126 |
+
return uploaded_image_path, spectrogram_image_path, audio_file_path # Return paths for uploaded image, spectrogram, and audio
|
| 127 |
|
| 128 |
# Gradio Interface
|
| 129 |
def gradio_process_image(input_image):
|
| 130 |
+
uploaded_image_path, spectrogram_image_path, audio_file_path = process_image(input_image)
|
| 131 |
|
| 132 |
# After Gradio finishes using these files, delete them to avoid keeping them around
|
| 133 |
def cleanup():
|
| 134 |
+
os.remove(uploaded_image_path)
|
| 135 |
os.remove(spectrogram_image_path)
|
| 136 |
os.remove(audio_file_path)
|
| 137 |
+
print(f"Deleted temp files: {uploaded_image_path}, {spectrogram_image_path}, {audio_file_path}")
|
| 138 |
|
| 139 |
+
return uploaded_image_path, spectrogram_image_path, audio_file_path, cleanup
|
| 140 |
|
| 141 |
# Create the Gradio interface
|
| 142 |
interface = gr.Interface(
|
| 143 |
fn=gradio_process_image,
|
| 144 |
inputs=gr.Image(type="pil"), # Input is an image
|
| 145 |
+
outputs=[gr.File(label="Uploaded Image"), gr.Image(type="filepath"), gr.Audio(type="filepath")], # Output uploaded image, spectrogram, and audio file
|
| 146 |
title="Image to Audio Generator with Spectrogram Display",
|
| 147 |
+
description="Upload an image, and get an audio file generated using Pix2Pix.",
|
| 148 |
)
|
| 149 |
|
| 150 |
# Launch the interface
|