musdfakoc commited on
Commit
adf8868
·
verified ·
1 Parent(s): 8cdbc50

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -6
app.py CHANGED
@@ -10,6 +10,7 @@ import os
10
  import random
11
  import tempfile
12
  import matplotlib.pyplot as plt
 
13
 
14
  # Load your Pix2Pix model (make sure the path is correct)
15
  model = load_model('./model_022600.h5', compile=False)
@@ -66,7 +67,7 @@ def save_spectrogram_image(spectrogram, name):
66
  plt.imshow(spectrogram, aspect='auto', origin='lower', cmap='gray')
67
  plt.axis('off')
68
 
69
- # Create a spectrogram filename based on the uploaded image's name
70
  temp_image_path = f"{name}_spectrogram.png"
71
  plt.savefig(temp_image_path, bbox_inches='tight', pad_inches=0)
72
 
@@ -75,8 +76,8 @@ def save_spectrogram_image(spectrogram, name):
75
 
76
  # Process the input image and convert to audio
77
  def process_image(input_image):
78
- # Extract the base name from the uploaded image filename (without extension)
79
- image_name = os.path.splitext(os.path.basename(input_image.name))[0]
80
 
81
  def load_image(image, size=(256, 256)):
82
  image = image.resize(size)
@@ -102,13 +103,13 @@ def process_image(input_image):
102
  # Modify the spectrogram randomly
103
  img = modify_spectrogram(img)
104
 
105
- # Save the modified spectrogram as an image, using the same name as the uploaded file
106
  spectrogram_image_path = save_spectrogram_image(img, image_name)
107
 
108
  # Convert the spectrogram back to audio using librosa
109
  wav = librosa.feature.inverse.mel_to_audio(img, sr=44100, n_fft=2048, hop_length=512)
110
 
111
- # Save the audio file to a location, using the same name as the uploaded file
112
  audio_file_path = f"{image_name}_generated_audio.wav"
113
  sf.write(audio_file_path, wav, samplerate=44100)
114
 
@@ -132,7 +133,7 @@ interface = gr.Interface(
132
  inputs=gr.Image(type="pil"), # Input is an image
133
  outputs=[gr.Image(type="filepath"), gr.Audio(type="filepath")], # Output both spectrogram image and audio file
134
  title="Image to Audio Generator with Spectrogram Display",
135
- description="Upload an image, and get an audio file generated using Pix2Pix.",
136
  )
137
 
138
  # Launch the interface
 
10
  import random
11
  import tempfile
12
  import matplotlib.pyplot as plt
13
+ import time # To generate unique filenames
14
 
15
  # Load your Pix2Pix model (make sure the path is correct)
16
  model = load_model('./model_022600.h5', compile=False)
 
67
  plt.imshow(spectrogram, aspect='auto', origin='lower', cmap='gray')
68
  plt.axis('off')
69
 
70
+ # Save the spectrogram image using the unique name
71
  temp_image_path = f"{name}_spectrogram.png"
72
  plt.savefig(temp_image_path, bbox_inches='tight', pad_inches=0)
73
 
 
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
80
+ image_name = f"image_{int(time.time())}"
81
 
82
  def load_image(image, size=(256, 256)):
83
  image = image.resize(size)
 
103
  # Modify the spectrogram randomly
104
  img = modify_spectrogram(img)
105
 
106
+ # Save the modified spectrogram as an image, using the unique name
107
  spectrogram_image_path = save_spectrogram_image(img, image_name)
108
 
109
  # Convert the spectrogram back to audio using librosa
110
  wav = librosa.feature.inverse.mel_to_audio(img, sr=44100, n_fft=2048, hop_length=512)
111
 
112
+ # Save the audio file, using the unique name
113
  audio_file_path = f"{image_name}_generated_audio.wav"
114
  sf.write(audio_file_path, wav, samplerate=44100)
115
 
 
133
  inputs=gr.Image(type="pil"), # Input is an image
134
  outputs=[gr.Image(type="filepath"), gr.Audio(type="filepath")], # Output both spectrogram image and audio file
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