ASesYusuf1 commited on
Commit
a4d751d
·
verified ·
1 Parent(s): 06686d3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -8
app.py CHANGED
@@ -65,7 +65,7 @@ except Exception as e:
65
  print(f"Setup failed: {e}")
66
  raise
67
 
68
- @spaces.GPU(duration=60)
69
  def process_audio(input_file, model, chunk_size, overlap, progress=gr.Progress()):
70
  if not input_file:
71
  return "No file uploaded.", None, None, None
@@ -128,31 +128,38 @@ def process_audio(input_file, model, chunk_size, overlap, progress=gr.Progress()
128
  process.stdout.close()
129
  process.wait()
130
  if process.returncode != 0:
131
- return "Error processing audio.", None, None, None
132
  total_duration = str(timedelta(seconds=int(time.time() - start_time)))
133
  progress(1.0, desc=f"Processing completed. Total time: {total_duration}")
134
  return output_file_path, input_file_path, None, f"Processing completed. Total time: {total_duration}"
135
  except Exception as e:
136
- return f"Error: {str(e)}", None, None, None
137
 
138
  def mid_side_separation(audio_file):
139
  try:
 
140
  y, sr = librosa.load(audio_file, sr=None, mono=False)
 
141
  if y.ndim == 1:
142
- raise ValueError("Stereo audio file required!")
143
  left, right = y[0], y[1]
 
144
  mid = (left + right) / 2
145
  side = (left - right) / 2
146
  mid_path = os.path.join(output_folder, "mid.wav")
147
  side_path = os.path.join(output_folder, "side.wav")
 
148
  sf.write(mid_path, mid, sr)
149
  sf.write(side_path, side, sr)
 
150
  return mid_path, side_path, sr
151
  except Exception as e:
 
152
  raise ValueError(f"Error in mid/side separation: {str(e)}")
153
 
154
  def mid_side_combine(mid_file, side_file, output_path):
155
  try:
 
156
  mid_data, sr_mid = librosa.load(mid_file, sr=None, mono=True)
157
  side_data, sr_side = librosa.load(side_file, sr=None, mono=True)
158
  if sr_mid != sr_side:
@@ -160,42 +167,57 @@ def mid_side_combine(mid_file, side_file, output_path):
160
  left = mid_data + side_data
161
  right = mid_data - side_data
162
  stereo = np.stack([left, right], axis=0)
 
163
  sf.write(output_path, stereo.T, sr_mid)
164
  return output_path
165
  except Exception as e:
 
166
  raise ValueError(f"Error in mid/side combination: {str(e)}")
167
 
 
168
  def process_mid_side_upscale(input_file, model, chunk_size, overlap, progress=gr.Progress()):
169
  if not input_file:
170
  return "No file uploaded.", None, None, None
171
  try:
172
  total_start_time = time.time()
 
173
 
 
174
  print("Separating Mid and Side channels...")
175
  mid_path, side_path, sr = mid_side_separation(input_file)
 
176
 
 
177
  print("Processing Mid channel...")
178
  mid_restored, _, _, mid_status = process_audio(mid_path, model, chunk_size, overlap, progress=progress)
179
  if not mid_restored.endswith(".wav"):
180
- return mid_status, None, None, None
 
181
 
 
182
  print("Processing Side channel...")
183
  side_restored, _, _, side_status = process_audio(side_path, model, chunk_size, overlap, progress=progress)
184
  if not side_restored.endswith(".wav"):
185
- return side_status, None, None, None
 
186
 
 
187
  original_file_name = os.path.splitext(os.path.basename(input_file))[0]
188
  final_output_path = os.path.join(output_folder, f"{original_file_name}_upscaled.wav")
189
 
 
190
  print("Combining processed Mid and Side channels...")
191
  final_audio = mid_side_combine(mid_restored, side_restored, final_output_path)
 
192
 
193
  total_duration = str(timedelta(seconds=int(time.time() - total_start_time)))
194
  progress(1.0, desc=f"Mid/Side upscaling completed. Total time: {total_duration}")
195
 
196
  return final_audio, input_file, None, f"Mid/Side upscaling completed. Total time: {total_duration}"
197
  except Exception as e:
198
- return f"Error: {str(e)}", None, None, None
 
 
199
 
200
  def spectrum(audio_file):
201
  if not audio_file:
@@ -428,5 +450,5 @@ if __name__ == "__main__":
428
  app.launch(
429
  server_name="0.0.0.0",
430
  server_port=7860,
431
- show_api=False, # Hides "Use via API"
432
  )
 
65
  print(f"Setup failed: {e}")
66
  raise
67
 
68
+ @spaces.GPU(duration=120) # Süreyi 60'tan 120 saniyeye çıkardım
69
  def process_audio(input_file, model, chunk_size, overlap, progress=gr.Progress()):
70
  if not input_file:
71
  return "No file uploaded.", None, None, None
 
128
  process.stdout.close()
129
  process.wait()
130
  if process.returncode != 0:
131
+ return f"Error processing audio: Non-zero exit code {process.returncode}.", None, None, None
132
  total_duration = str(timedelta(seconds=int(time.time() - start_time)))
133
  progress(1.0, desc=f"Processing completed. Total time: {total_duration}")
134
  return output_file_path, input_file_path, None, f"Processing completed. Total time: {total_duration}"
135
  except Exception as e:
136
+ return f"Error in process_audio: {str(e)}", None, None, None
137
 
138
  def mid_side_separation(audio_file):
139
  try:
140
+ print(f"Loading audio file: {audio_file}")
141
  y, sr = librosa.load(audio_file, sr=None, mono=False)
142
+ print(f"Audio shape: {y.shape}, Sample rate: {sr}")
143
  if y.ndim == 1:
144
+ raise ValueError("Stereo audio file required! Please upload a stereo .wav or .mp3 file.")
145
  left, right = y[0], y[1]
146
+ print("Performing Mid/Side separation...")
147
  mid = (left + right) / 2
148
  side = (left - right) / 2
149
  mid_path = os.path.join(output_folder, "mid.wav")
150
  side_path = os.path.join(output_folder, "side.wav")
151
+ print(f"Saving Mid to {mid_path} and Side to {side_path}")
152
  sf.write(mid_path, mid, sr)
153
  sf.write(side_path, side, sr)
154
+ print("Mid/Side separation completed.")
155
  return mid_path, side_path, sr
156
  except Exception as e:
157
+ print(f"Error in mid/side separation: {str(e)}")
158
  raise ValueError(f"Error in mid/side separation: {str(e)}")
159
 
160
  def mid_side_combine(mid_file, side_file, output_path):
161
  try:
162
+ print(f"Combining Mid: {mid_file} and Side: {side_file}")
163
  mid_data, sr_mid = librosa.load(mid_file, sr=None, mono=True)
164
  side_data, sr_side = librosa.load(side_file, sr=None, mono=True)
165
  if sr_mid != sr_side:
 
167
  left = mid_data + side_data
168
  right = mid_data - side_data
169
  stereo = np.stack([left, right], axis=0)
170
+ print(f"Saving combined audio to {output_path}")
171
  sf.write(output_path, stereo.T, sr_mid)
172
  return output_path
173
  except Exception as e:
174
+ print(f"Error in mid/side combination: {str(e)}")
175
  raise ValueError(f"Error in mid/side combination: {str(e)}")
176
 
177
+ @spaces.GPU(duration=120) # Süreyi 60'tan 120 saniyeye çıkardım
178
  def process_mid_side_upscale(input_file, model, chunk_size, overlap, progress=gr.Progress()):
179
  if not input_file:
180
  return "No file uploaded.", None, None, None
181
  try:
182
  total_start_time = time.time()
183
+ print(f"Starting Mid/Side upscale for: {input_file}")
184
 
185
+ # Mid/Side ayrımı
186
  print("Separating Mid and Side channels...")
187
  mid_path, side_path, sr = mid_side_separation(input_file)
188
+ print(f"Mid path: {mid_path}, Side path: {side_path}, Sample rate: {sr}")
189
 
190
+ # Mid kanalını işle
191
  print("Processing Mid channel...")
192
  mid_restored, _, _, mid_status = process_audio(mid_path, model, chunk_size, overlap, progress=progress)
193
  if not mid_restored.endswith(".wav"):
194
+ return f"Mid channel processing failed: {mid_status}", None, None, None
195
+ print(f"Mid channel processed: {mid_restored}")
196
 
197
+ # Side kanalını işle
198
  print("Processing Side channel...")
199
  side_restored, _, _, side_status = process_audio(side_path, model, chunk_size, overlap, progress=progress)
200
  if not side_restored.endswith(".wav"):
201
+ return f"Side channel processing failed: {side_status}", None, None, None
202
+ print(f"Side channel processed: {side_restored}")
203
 
204
+ # Orijinal dosya adını al ve çıktı yolunu oluştur
205
  original_file_name = os.path.splitext(os.path.basename(input_file))[0]
206
  final_output_path = os.path.join(output_folder, f"{original_file_name}_upscaled.wav")
207
 
208
+ # Mid ve Side kanallarını birleştir
209
  print("Combining processed Mid and Side channels...")
210
  final_audio = mid_side_combine(mid_restored, side_restored, final_output_path)
211
+ print(f"Final audio saved: {final_audio}")
212
 
213
  total_duration = str(timedelta(seconds=int(time.time() - total_start_time)))
214
  progress(1.0, desc=f"Mid/Side upscaling completed. Total time: {total_duration}")
215
 
216
  return final_audio, input_file, None, f"Mid/Side upscaling completed. Total time: {total_duration}"
217
  except Exception as e:
218
+ error_msg = f"Error in Mid/Side upscale: {str(e)}"
219
+ print(error_msg)
220
+ return error_msg, None, None, None
221
 
222
  def spectrum(audio_file):
223
  if not audio_file:
 
450
  app.launch(
451
  server_name="0.0.0.0",
452
  server_port=7860,
453
+ show_api=False,
454
  )