Opera8 commited on
Commit
a4df331
·
verified ·
1 Parent(s): 4863b79

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -15
app.py CHANGED
@@ -12,8 +12,9 @@ import subprocess
12
  import re
13
  import spaces
14
  import uuid
 
15
 
16
- # دانلود فقط منابع ضروری
17
  downloaded_resources = {
18
  "configs": False,
19
  "tokenizer_vq8192": False,
@@ -28,8 +29,6 @@ def install_espeak():
28
  print("Installing espeak-ng...")
29
  subprocess.run(["apt-get", "update"], check=True)
30
  subprocess.run(["apt-get", "install", "-y", "espeak-ng", "espeak-ng-data"], check=True)
31
- else:
32
- print("espeak-ng is installed.")
33
  except Exception as e:
34
  print(f"Error installing espeak-ng: {e}")
35
 
@@ -85,8 +84,20 @@ if os.path.dirname(os.path.abspath("Amphion")) not in sys.path:
85
  os.makedirs("wav", exist_ok=True)
86
  os.makedirs("ckpts/Vevo", exist_ok=True)
87
 
88
- # اینجا دیگر مشکلی ندارد چون نسخه torchaudio را درست کردیم
89
- from models.vc.vevo.vevo_utils import VevoInferencePipeline, save_audio
 
 
 
 
 
 
 
 
 
 
 
 
90
 
91
  def setup_configs():
92
  if downloaded_resources["configs"]: return
@@ -158,7 +169,7 @@ def get_pipeline():
158
 
159
  @spaces.GPU()
160
  def vevo_timbre(content_wav, reference_wav):
161
- # ایجاد نام فایل منحصر به فرد برای جلوگیری از تداخل کاربران
162
  session_id = str(uuid.uuid4())[:8]
163
  temp_content_path = f"wav/c_{session_id}.wav"
164
  temp_reference_path = f"wav/r_{session_id}.wav"
@@ -168,7 +179,7 @@ def vevo_timbre(content_wav, reference_wav):
168
  raise ValueError("Please upload audio files")
169
 
170
  try:
171
- # --- پردازش صدای اصلی ---
172
  if isinstance(content_wav, tuple):
173
  content_sr, content_data = content_wav if isinstance(content_wav[0], int) else (content_wav[1], content_wav[0])
174
  else:
@@ -178,13 +189,15 @@ def vevo_timbre(content_wav, reference_wav):
178
  content_data = np.mean(content_data, axis=1)
179
 
180
  content_tensor = torch.FloatTensor(content_data).unsqueeze(0)
 
181
  if content_sr != 24000:
182
  content_tensor = torchaudio.functional.resample(content_tensor, content_sr, 24000)
183
  content_sr = 24000
184
 
 
185
  content_tensor = content_tensor / (torch.max(torch.abs(content_tensor)) + 1e-6) * 0.95
186
 
187
- # --- پردازش صدای رفرنس ---
188
  if isinstance(reference_wav, tuple):
189
  ref_sr, ref_data = reference_wav if isinstance(reference_wav[0], int) else (reference_wav[1], reference_wav[0])
190
  else:
@@ -200,15 +213,15 @@ def vevo_timbre(content_wav, reference_wav):
200
 
201
  ref_tensor = ref_tensor / (torch.max(torch.abs(ref_tensor)) + 1e-6) * 0.95
202
 
203
- # ذخیره فایل‌ها با torchaudio (چون نسخه قدیمی است، بدون ارور کار می‌کند و فرمت دقیق را حفظ می‌کند)
204
- torchaudio.save(temp_content_path, content_tensor, content_sr)
205
- torchaudio.save(temp_reference_path, ref_tensor, ref_sr)
206
 
207
- print(f"[{session_id}] Processing Audio...")
208
 
209
  pipeline = get_pipeline()
210
 
211
- # اجرای مدل روی کل فایل (بدون تکه تکه کردن - چون قبلاً اینطوری کار می‌کرد)
212
  gen_audio = pipeline.inference_fm(
213
  src_wav_path=temp_content_path,
214
  timbre_ref_wav_path=temp_reference_path,
@@ -219,7 +232,7 @@ def vevo_timbre(content_wav, reference_wav):
219
  print("Warning: NaN fixed")
220
  gen_audio = torch.nan_to_num(gen_audio, nan=0.0, posinf=0.95, neginf=-0.95)
221
 
222
- save_audio(gen_audio, output_path=output_path)
223
  return output_path
224
 
225
  finally:
@@ -227,7 +240,7 @@ def vevo_timbre(content_wav, reference_wav):
227
  if os.path.exists(temp_content_path): os.remove(temp_content_path)
228
  if os.path.exists(temp_reference_path): os.remove(temp_reference_path)
229
 
230
- with gr.Blocks(title="Vevo-Timbre (Stable)") as demo:
231
  gr.Markdown("## Vevo-Timbre: Zero-Shot Voice Conversion")
232
 
233
  with gr.Row():
 
12
  import re
13
  import spaces
14
  import uuid
15
+ import soundfile as sf # استفاده مستقیم برای حل مشکل ذخیره‌سازی
16
 
17
+ # فقط منابع ضروری
18
  downloaded_resources = {
19
  "configs": False,
20
  "tokenizer_vq8192": False,
 
29
  print("Installing espeak-ng...")
30
  subprocess.run(["apt-get", "update"], check=True)
31
  subprocess.run(["apt-get", "install", "-y", "espeak-ng", "espeak-ng-data"], check=True)
 
 
32
  except Exception as e:
33
  print(f"Error installing espeak-ng: {e}")
34
 
 
84
  os.makedirs("wav", exist_ok=True)
85
  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):
93
+ waveform = waveform.detach().cpu()
94
+ if waveform.dim() == 2 and waveform.shape[0] == 1:
95
+ waveform = waveform.squeeze(0)
96
+ waveform = waveform.numpy()
97
+ sf.write(output_path, waveform, sample_rate)
98
+ except Exception as e:
99
+ print(f"Save error: {e}")
100
+ raise e
101
 
102
  def setup_configs():
103
  if downloaded_resources["configs"]: return
 
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
  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
  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
 
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,
 
232
  print("Warning: NaN fixed")
233
  gen_audio = torch.nan_to_num(gen_audio, nan=0.0, posinf=0.95, neginf=-0.95)
234
 
235
+ my_save_audio(gen_audio, output_path=output_path)
236
  return output_path
237
 
238
  finally:
 
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
 
243
+ with gr.Blocks(title="Vevo-Timbre (Secure)") as demo:
244
  gr.Markdown("## Vevo-Timbre: Zero-Shot Voice Conversion")
245
 
246
  with gr.Row():