Opera8 commited on
Commit
b78fc58
·
verified ·
1 Parent(s): bf03bd3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -11
app.py CHANGED
@@ -12,7 +12,7 @@ import subprocess
12
  import re
13
  import spaces
14
  import uuid
15
- import soundfile as sf # استفاده مستقیم برای حل مشکل ذخیره‌سازی
16
 
17
  # فقط منابع ضروری
18
  downloaded_resources = {
@@ -86,7 +86,7 @@ os.makedirs("ckpts/Vevo", exist_ok=True)
86
 
87
  from models.vc.vevo.vevo_utils import VevoInferencePipeline
88
 
89
- # تابع ذخیره سازی امن (جایگزین torchaudio.save)
90
  def my_save_audio(waveform, output_path, sample_rate=24000):
91
  try:
92
  if isinstance(waveform, torch.Tensor):
@@ -169,7 +169,6 @@ def get_pipeline():
169
 
170
  @spaces.GPU()
171
  def vevo_timbre(content_wav, reference_wav):
172
- # 1. ایجاد نام یکتا برای هر کاربر (جلوگیری از قاطی شدن فایل‌ها)
173
  session_id = str(uuid.uuid4())[:8]
174
  temp_content_path = f"wav/c_{session_id}.wav"
175
  temp_reference_path = f"wav/r_{session_id}.wav"
@@ -179,7 +178,6 @@ def vevo_timbre(content_wav, reference_wav):
179
  raise ValueError("Please upload audio files")
180
 
181
  try:
182
- # --- پردازش و نرمال‌سازی صداها ---
183
  if isinstance(content_wav, tuple):
184
  content_sr, content_data = content_wav if isinstance(content_wav[0], int) else (content_wav[1], content_wav[0])
185
  else:
@@ -189,15 +187,12 @@ def vevo_timbre(content_wav, reference_wav):
189
  content_data = np.mean(content_data, axis=1)
190
 
191
  content_tensor = torch.FloatTensor(content_data).unsqueeze(0)
192
- # مهم: استفاده از torchaudio برای ریسمپل دقیق (جلوگیری از نویز)
193
  if content_sr != 24000:
194
  content_tensor = torchaudio.functional.resample(content_tensor, content_sr, 24000)
195
  content_sr = 24000
196
 
197
- # نرمال‌سازی صدا (خیلی مهم برای کیفیت)
198
  content_tensor = content_tensor / (torch.max(torch.abs(content_tensor)) + 1e-6) * 0.95
199
 
200
- # --- پردازش رفرنس ---
201
  if isinstance(reference_wav, tuple):
202
  ref_sr, ref_data = reference_wav if isinstance(reference_wav[0], int) else (reference_wav[1], reference_wav[0])
203
  else:
@@ -213,15 +208,14 @@ def vevo_timbre(content_wav, reference_wav):
213
 
214
  ref_tensor = ref_tensor / (torch.max(torch.abs(ref_tensor)) + 1e-6) * 0.95
215
 
216
- # استفاده از soundfile برای ذخیره (چون torchaudio در نسخه جدید ارور می‌دهد)
217
  sf.write(temp_content_path, content_tensor.squeeze().cpu().numpy(), content_sr)
218
  sf.write(temp_reference_path, ref_tensor.squeeze().cpu().numpy(), ref_sr)
219
 
220
- print(f"[{session_id}] Processing Audio ({content_tensor.shape[1]/24000:.2f}s)...")
221
 
222
  pipeline = get_pipeline()
223
 
224
- # اجرای مدل روی کل فایل (بدون تکه تکه کردن)
225
  gen_audio = pipeline.inference_fm(
226
  src_wav_path=temp_content_path,
227
  timbre_ref_wav_path=temp_reference_path,
@@ -236,7 +230,6 @@ def vevo_timbre(content_wav, reference_wav):
236
  return output_path
237
 
238
  finally:
239
- # پاکسازی فایل‌های موقت
240
  if os.path.exists(temp_content_path): os.remove(temp_content_path)
241
  if os.path.exists(temp_reference_path): os.remove(temp_reference_path)
242
 
 
12
  import re
13
  import spaces
14
  import uuid
15
+ import soundfile as sf
16
 
17
  # فقط منابع ضروری
18
  downloaded_resources = {
 
86
 
87
  from models.vc.vevo.vevo_utils import VevoInferencePipeline
88
 
89
+ # تابع ذخیره سازی امن
90
  def my_save_audio(waveform, output_path, sample_rate=24000):
91
  try:
92
  if isinstance(waveform, torch.Tensor):
 
169
 
170
  @spaces.GPU()
171
  def vevo_timbre(content_wav, reference_wav):
 
172
  session_id = str(uuid.uuid4())[:8]
173
  temp_content_path = f"wav/c_{session_id}.wav"
174
  temp_reference_path = f"wav/r_{session_id}.wav"
 
178
  raise ValueError("Please upload audio files")
179
 
180
  try:
 
181
  if isinstance(content_wav, tuple):
182
  content_sr, content_data = content_wav if isinstance(content_wav[0], int) else (content_wav[1], content_wav[0])
183
  else:
 
187
  content_data = np.mean(content_data, axis=1)
188
 
189
  content_tensor = torch.FloatTensor(content_data).unsqueeze(0)
 
190
  if content_sr != 24000:
191
  content_tensor = torchaudio.functional.resample(content_tensor, content_sr, 24000)
192
  content_sr = 24000
193
 
 
194
  content_tensor = content_tensor / (torch.max(torch.abs(content_tensor)) + 1e-6) * 0.95
195
 
 
196
  if isinstance(reference_wav, tuple):
197
  ref_sr, ref_data = reference_wav if isinstance(reference_wav[0], int) else (reference_wav[1], reference_wav[0])
198
  else:
 
208
 
209
  ref_tensor = ref_tensor / (torch.max(torch.abs(ref_tensor)) + 1e-6) * 0.95
210
 
211
+ # استفاده از soundfile برای ذخیره موقت
212
  sf.write(temp_content_path, content_tensor.squeeze().cpu().numpy(), content_sr)
213
  sf.write(temp_reference_path, ref_tensor.squeeze().cpu().numpy(), ref_sr)
214
 
215
+ print(f"[{session_id}] Processing Audio...")
216
 
217
  pipeline = get_pipeline()
218
 
 
219
  gen_audio = pipeline.inference_fm(
220
  src_wav_path=temp_content_path,
221
  timbre_ref_wav_path=temp_reference_path,
 
230
  return output_path
231
 
232
  finally:
 
233
  if os.path.exists(temp_content_path): os.remove(temp_content_path)
234
  if os.path.exists(temp_reference_path): os.remove(temp_reference_path)
235