Update app.py
Browse files
app.py
CHANGED
|
@@ -14,7 +14,7 @@ import spaces
|
|
| 14 |
import uuid
|
| 15 |
import soundfile as sf
|
| 16 |
|
| 17 |
-
# ---
|
| 18 |
downloaded_resources = {
|
| 19 |
"configs": False,
|
| 20 |
"tokenizer_vq8192": False,
|
|
@@ -34,6 +34,7 @@ def install_espeak():
|
|
| 34 |
|
| 35 |
install_espeak()
|
| 36 |
|
|
|
|
| 37 |
def patch_langsegment_init():
|
| 38 |
try:
|
| 39 |
spec = importlib.util.find_spec("LangSegment")
|
|
@@ -71,6 +72,7 @@ def patch_langsegment_init():
|
|
| 71 |
|
| 72 |
patch_langsegment_init()
|
| 73 |
|
|
|
|
| 74 |
if not os.path.exists("Amphion"):
|
| 75 |
subprocess.run(["git", "clone", "https://github.com/open-mmlab/Amphion.git"])
|
| 76 |
os.chdir("Amphion")
|
|
@@ -174,26 +176,25 @@ def vevo_timbre(content_wav, reference_wav):
|
|
| 174 |
raise ValueError("Please upload audio files")
|
| 175 |
|
| 176 |
try:
|
| 177 |
-
# --- 1.
|
| 178 |
if isinstance(content_wav, tuple):
|
| 179 |
content_sr, content_data = content_wav if isinstance(content_wav[0], int) else (content_wav[1], content_wav[0])
|
| 180 |
else:
|
| 181 |
content_sr, content_data = content_wav
|
| 182 |
|
| 183 |
-
# تبدیل استریو به مونو
|
| 184 |
if len(content_data.shape) > 1 and content_data.shape[1] > 1:
|
| 185 |
content_data = np.mean(content_data, axis=1)
|
| 186 |
|
| 187 |
-
# نرمالسازی و ریسمپل
|
| 188 |
content_tensor = torch.FloatTensor(content_data).unsqueeze(0)
|
| 189 |
if content_sr != 24000:
|
| 190 |
content_tensor = torchaudio.functional.resample(content_tensor, content_sr, 24000)
|
| 191 |
content_sr = 24000
|
| 192 |
|
|
|
|
| 193 |
content_tensor = content_tensor / (torch.max(torch.abs(content_tensor)) + 1e-6) * 0.95
|
| 194 |
-
|
| 195 |
|
| 196 |
-
# --- 2.
|
| 197 |
if isinstance(reference_wav, tuple):
|
| 198 |
ref_sr, ref_data = reference_wav if isinstance(reference_wav[0], int) else (reference_wav[1], reference_wav[0])
|
| 199 |
else:
|
|
@@ -210,142 +211,119 @@ def vevo_timbre(content_wav, reference_wav):
|
|
| 210 |
ref_max = torch.max(torch.abs(ref_tensor)) + 1e-6
|
| 211 |
ref_tensor = ref_tensor / ref_max * 0.95
|
| 212 |
|
| 213 |
-
# محدود کردن طول رفرنس برای جلوگیری از خطای حافظه
|
| 214 |
if ref_tensor.shape[1] > 24000 * 20:
|
| 215 |
ref_tensor = ref_tensor[:, :24000 * 20]
|
| 216 |
|
| 217 |
save_audio_pcm16(ref_tensor, temp_reference_path, ref_sr)
|
| 218 |
|
| 219 |
-
# --- 3. منطق
|
| 220 |
pipeline = get_pipeline()
|
| 221 |
SR = 24000
|
| 222 |
|
| 223 |
-
#
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
#
|
| 227 |
-
HOP_SEC = PROCESS_CHUNK_SEC - OVERLAP_SEC
|
| 228 |
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
overlap_samples = OVERLAP_SEC * SR
|
| 232 |
|
| 233 |
-
|
| 234 |
-
|
|
|
|
| 235 |
|
| 236 |
-
|
| 237 |
|
| 238 |
-
#
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
|
| 245 |
-
# اگر
|
| 246 |
-
if
|
| 247 |
-
|
| 248 |
|
|
|
|
|
|
|
|
|
|
| 249 |
# ذخیره موقت برای مدل
|
| 250 |
-
save_audio_pcm16(torch.FloatTensor(
|
| 251 |
|
| 252 |
try:
|
| 253 |
-
#
|
| 254 |
gen = pipeline.inference_fm(
|
| 255 |
src_wav_path=temp_content_path,
|
| 256 |
timbre_ref_wav_path=temp_reference_path,
|
| 257 |
flow_matching_steps=64,
|
| 258 |
)
|
| 259 |
|
| 260 |
-
# تبدیل خروجی به numpy
|
| 261 |
if torch.isnan(gen).any(): gen = torch.nan_to_num(gen, nan=0.0)
|
| 262 |
if gen.dim() == 1: gen = gen.unsqueeze(0)
|
| 263 |
gen_np = gen.cpu().squeeze(0).numpy()
|
| 264 |
|
| 265 |
-
# ---
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
# انتظار داریم خروجی مدل تقریباً هماندازه ورودی باشد
|
| 273 |
-
# انتهای صدای فعلی در final_output_audio کجاست؟
|
| 274 |
-
# ما در هر دور hop_samples جلو میرویم.
|
| 275 |
-
|
| 276 |
-
# روش ایمن:
|
| 277 |
-
# دو ثانیه آخر صدای قبلی را با دو ثانیه اول صدای جدید ترکیب میکنیم
|
| 278 |
-
|
| 279 |
-
prev_audio_len = len(final_output_audio)
|
| 280 |
-
new_audio_len = len(gen_np)
|
| 281 |
-
|
| 282 |
-
# اگر خروجی مدل خیلی کوتاه بود، فقط بچسبان
|
| 283 |
-
if new_audio_len < overlap_samples:
|
| 284 |
-
final_output_audio = np.concatenate([final_output_audio, gen_np])
|
| 285 |
-
continue
|
| 286 |
-
|
| 287 |
-
# برش بخش همپوشانی
|
| 288 |
-
# ما قبلاً در دور قبل، تمام خروجی (۱۲ ثانیه) را ذخیره کردیم.
|
| 289 |
-
# اما الان ۱۰ ثانیه جلو آمدیم. پس ۲ ثانیه آخر دور قبلی، با ۲ ثانیه اول دور جدید همپوشانی دارد.
|
| 290 |
-
# (توجه: این منطق فرض میکند خروجی مدل دقیقا همطول ورودی است که در VC تقریبا درست است)
|
| 291 |
-
|
| 292 |
-
# حذف ۲ ثانیه آخر از بافر نهایی (برای آمادهسازی میکس)
|
| 293 |
-
# اما صبر کن، ما در دور قبل کامل اضافه کردیم. پس الان بافر نهایی شامل Overlap هم هست.
|
| 294 |
-
# بخش غیر اورلپ قبلی: تا سرِ overlap
|
| 295 |
-
# بخش اورلپ قبلی: از سرِ overlap تا آخر
|
| 296 |
-
|
| 297 |
-
# منطق سادهتر Overlap-Add:
|
| 298 |
-
# همیشه فقط بخش "جدید" (۱۰ ثانیه) را نگه نداریم، بلکه همیشه میکس کنیم.
|
| 299 |
-
|
| 300 |
-
# پیادهسازی دقیق Cross-Fade:
|
| 301 |
-
fade_out = np.linspace(1, 0, overlap_samples)
|
| 302 |
-
fade_in = np.linspace(0, 1, overlap_samples)
|
| 303 |
-
|
| 304 |
-
# ۲ ثانیه آخر بافر فعلی
|
| 305 |
-
prev_tail = final_output_audio[-overlap_samples:]
|
| 306 |
-
# ۲ ثانیه اول صدای جدید
|
| 307 |
-
curr_head = gen_np[:overlap_samples]
|
| 308 |
-
|
| 309 |
-
# اطمینان از هماندازه بودن (ممکن است مدل خروجی کمی متفاوت بدهد)
|
| 310 |
-
min_len = min(len(prev_tail), len(curr_head))
|
| 311 |
-
if min_len < overlap_samples:
|
| 312 |
-
# اگر به هر دلیلی سایزها نخواند (نادر)، برش بزن
|
| 313 |
-
prev_tail = prev_tail[:min_len]
|
| 314 |
-
curr_head = curr_head[:min_len]
|
| 315 |
-
fade_out = fade_out[:min_len]
|
| 316 |
-
fade_in = fade_in[:min_len]
|
| 317 |
-
# اصلاح بافر اصلی
|
| 318 |
-
final_output_audio = final_output_audio[:len(final_output_audio)-(overlap_samples-min_len)]
|
| 319 |
|
| 320 |
-
#
|
| 321 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 322 |
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
# ۳. اضافه کردن باقیمانده صدای جدید (بعد از بخش Overlap)
|
| 329 |
-
remaining_new = gen_np[len(blended_overlap):]
|
| 330 |
-
final_output_audio = np.concatenate([final_output_audio, remaining_new])
|
| 331 |
-
|
| 332 |
except Exception as e:
|
| 333 |
-
print(f"Error
|
| 334 |
-
# در صورت خطا، سکوت اضافه
|
| 335 |
-
|
| 336 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 337 |
|
| 338 |
-
|
| 339 |
-
save_audio_pcm16(final_output_audio, output_path, SR)
|
| 340 |
return output_path
|
| 341 |
|
| 342 |
finally:
|
| 343 |
if os.path.exists(temp_content_path): os.remove(temp_content_path)
|
| 344 |
if os.path.exists(temp_reference_path): os.remove(temp_reference_path)
|
| 345 |
|
| 346 |
-
with gr.Blocks(title="Vevo-Timbre (
|
| 347 |
gr.Markdown("## Vevo-Timbre: Zero-Shot Voice Conversion")
|
| 348 |
-
gr.Markdown("
|
| 349 |
|
| 350 |
with gr.Row():
|
| 351 |
with gr.Column():
|
|
|
|
| 14 |
import uuid
|
| 15 |
import soundfile as sf
|
| 16 |
|
| 17 |
+
# --- تنظیمات اولیه و نصب پکیجها ---
|
| 18 |
downloaded_resources = {
|
| 19 |
"configs": False,
|
| 20 |
"tokenizer_vq8192": False,
|
|
|
|
| 34 |
|
| 35 |
install_espeak()
|
| 36 |
|
| 37 |
+
# پچ کردن مشکل LangSegment
|
| 38 |
def patch_langsegment_init():
|
| 39 |
try:
|
| 40 |
spec = importlib.util.find_spec("LangSegment")
|
|
|
|
| 72 |
|
| 73 |
patch_langsegment_init()
|
| 74 |
|
| 75 |
+
# دریافت ریپازیتوری Amphion
|
| 76 |
if not os.path.exists("Amphion"):
|
| 77 |
subprocess.run(["git", "clone", "https://github.com/open-mmlab/Amphion.git"])
|
| 78 |
os.chdir("Amphion")
|
|
|
|
| 176 |
raise ValueError("Please upload audio files")
|
| 177 |
|
| 178 |
try:
|
| 179 |
+
# --- 1. آمادهسازی فایل ورودی (Content) ---
|
| 180 |
if isinstance(content_wav, tuple):
|
| 181 |
content_sr, content_data = content_wav if isinstance(content_wav[0], int) else (content_wav[1], content_wav[0])
|
| 182 |
else:
|
| 183 |
content_sr, content_data = content_wav
|
| 184 |
|
|
|
|
| 185 |
if len(content_data.shape) > 1 and content_data.shape[1] > 1:
|
| 186 |
content_data = np.mean(content_data, axis=1)
|
| 187 |
|
|
|
|
| 188 |
content_tensor = torch.FloatTensor(content_data).unsqueeze(0)
|
| 189 |
if content_sr != 24000:
|
| 190 |
content_tensor = torchaudio.functional.resample(content_tensor, content_sr, 24000)
|
| 191 |
content_sr = 24000
|
| 192 |
|
| 193 |
+
# نرمالسازی
|
| 194 |
content_tensor = content_tensor / (torch.max(torch.abs(content_tensor)) + 1e-6) * 0.95
|
| 195 |
+
content_full_np = content_tensor.squeeze().numpy()
|
| 196 |
|
| 197 |
+
# --- 2. آمادهسازی فایل رفرنس (Reference) ---
|
| 198 |
if isinstance(reference_wav, tuple):
|
| 199 |
ref_sr, ref_data = reference_wav if isinstance(reference_wav[0], int) else (reference_wav[1], reference_wav[0])
|
| 200 |
else:
|
|
|
|
| 211 |
ref_max = torch.max(torch.abs(ref_tensor)) + 1e-6
|
| 212 |
ref_tensor = ref_tensor / ref_max * 0.95
|
| 213 |
|
|
|
|
| 214 |
if ref_tensor.shape[1] > 24000 * 20:
|
| 215 |
ref_tensor = ref_tensor[:, :24000 * 20]
|
| 216 |
|
| 217 |
save_audio_pcm16(ref_tensor, temp_reference_path, ref_sr)
|
| 218 |
|
| 219 |
+
# --- 3. منطق جدید: Pre-roll Context (بدون همپوشانی طولانی) ---
|
| 220 |
pipeline = get_pipeline()
|
| 221 |
SR = 24000
|
| 222 |
|
| 223 |
+
# استراتژی:
|
| 224 |
+
# ما میخواهیم ۱۰ ثانیه جدید بسازیم.
|
| 225 |
+
# اما برای اینکه لحن نپرد، ۳ ثانیه از صدای قبلی را هم به ورودی اضافه میکنیم.
|
| 226 |
+
# بعد از تولید، آن ۳ ثانیه اول را دور میریزیم.
|
|
|
|
| 227 |
|
| 228 |
+
NEW_CHUNK_SEC = 10.0 # مقدار صدای جدید در هر مرحله
|
| 229 |
+
CONTEXT_SEC = 3.0 # مقدار صدای قدیمی برای حفظ لحن
|
|
|
|
| 230 |
|
| 231 |
+
new_chunk_samples = int(NEW_CHUNK_SEC * SR)
|
| 232 |
+
context_samples = int(CONTEXT_SEC * SR)
|
| 233 |
+
total_samples = len(content_full_np)
|
| 234 |
|
| 235 |
+
final_output = []
|
| 236 |
|
| 237 |
+
# نشانگر: تا کجای فایل را "نهایی" و تولید کردهایم
|
| 238 |
+
current_cursor = 0
|
| 239 |
+
|
| 240 |
+
print(f"[{session_id}] Starting processing with Context-Discard strategy...")
|
| 241 |
+
|
| 242 |
+
while current_cursor < total_samples:
|
| 243 |
+
# تعیین محدوده برش از فایل اصلی
|
| 244 |
+
# شروع: کمی عقبتر از جایی که هستیم (برای کانتکست)
|
| 245 |
+
start_slice = max(0, current_cursor - context_samples)
|
| 246 |
+
# پایان: ۱۰ ثانیه جلوتر از جایی که هستیم
|
| 247 |
+
end_slice = min(total_samples, current_cursor + new_chunk_samples)
|
| 248 |
|
| 249 |
+
# اگر چیزی برای پردازش نمانده
|
| 250 |
+
if start_slice >= end_slice:
|
| 251 |
+
break
|
| 252 |
|
| 253 |
+
# استخراج تکه از فایل اصلی
|
| 254 |
+
chunk_np = content_full_np[start_slice:end_slice]
|
| 255 |
+
|
| 256 |
# ذخیره موقت برای مدل
|
| 257 |
+
save_audio_pcm16(torch.FloatTensor(chunk_np).unsqueeze(0), temp_content_path, SR)
|
| 258 |
|
| 259 |
try:
|
| 260 |
+
# تولید صدا توسط مدل
|
| 261 |
gen = pipeline.inference_fm(
|
| 262 |
src_wav_path=temp_content_path,
|
| 263 |
timbre_ref_wav_path=temp_reference_path,
|
| 264 |
flow_matching_steps=64,
|
| 265 |
)
|
| 266 |
|
|
|
|
| 267 |
if torch.isnan(gen).any(): gen = torch.nan_to_num(gen, nan=0.0)
|
| 268 |
if gen.dim() == 1: gen = gen.unsqueeze(0)
|
| 269 |
gen_np = gen.cpu().squeeze(0).numpy()
|
| 270 |
|
| 271 |
+
# --- برش هوشمند (Trimming) ---
|
| 272 |
+
# ما (current_cursor - start_slice) مقدار سمپل را قبلا تولید کرده بودیم (کانتکست)
|
| 273 |
+
# پس باید این مقدار را از ابتدای خروجی جدید حذف کنیم تا صدای دو نفره نشود.
|
| 274 |
+
trim_amount = current_cursor - start_slice
|
| 275 |
+
|
| 276 |
+
if len(gen_np) > trim_amount:
|
| 277 |
+
valid_audio = gen_np[trim_amount:]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 278 |
|
| 279 |
+
# --- اتصال نرم (Micro Cross-fade) ---
|
| 280 |
+
# فقط ۱۰ میلی ثانیه فید میکنیم تا صدای "تیک" ندهد.
|
| 281 |
+
# این مقدار آنقدر کم است که گوش انسان صدای دو نفره نمیشنود.
|
| 282 |
+
if len(final_output) > 0:
|
| 283 |
+
# 10ms fade
|
| 284 |
+
fade_len = int(0.01 * SR)
|
| 285 |
+
if len(final_output[-1]) > fade_len and len(valid_audio) > fade_len:
|
| 286 |
+
fade_out_curve = np.linspace(1, 0, fade_len)
|
| 287 |
+
fade_in_curve = np.linspace(0, 1, fade_len)
|
| 288 |
+
|
| 289 |
+
# میکس فقط روی ۱۰ میلی ثانیه مرز
|
| 290 |
+
prev_tail = final_output[-1][-fade_len:]
|
| 291 |
+
curr_head = valid_audio[:fade_len]
|
| 292 |
+
|
| 293 |
+
blended = (prev_tail * fade_out_curve) + (curr_head * fade_in_curve)
|
| 294 |
+
|
| 295 |
+
# جایگزینی
|
| 296 |
+
final_output[-1][-fade_len:] = blended
|
| 297 |
+
valid_audio = valid_audio[fade_len:]
|
| 298 |
|
| 299 |
+
final_output.append(valid_audio)
|
| 300 |
+
|
| 301 |
+
# مکاننما را جلو میبریم
|
| 302 |
+
current_cursor = end_slice
|
| 303 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 304 |
except Exception as e:
|
| 305 |
+
print(f"Error in chunk: {e}")
|
| 306 |
+
# در صورت خطا، سکوت اضافه میکنیم تا زمانبندی به هم نریزد
|
| 307 |
+
missing_len = end_slice - current_cursor
|
| 308 |
+
final_output.append(np.zeros(missing_len))
|
| 309 |
+
current_cursor = end_slice
|
| 310 |
+
|
| 311 |
+
# چسباندن نهایی
|
| 312 |
+
if len(final_output) > 0:
|
| 313 |
+
full_audio = np.concatenate(final_output)
|
| 314 |
+
else:
|
| 315 |
+
full_audio = np.zeros(SR)
|
| 316 |
|
| 317 |
+
save_audio_pcm16(full_audio, output_path, SR)
|
|
|
|
| 318 |
return output_path
|
| 319 |
|
| 320 |
finally:
|
| 321 |
if os.path.exists(temp_content_path): os.remove(temp_content_path)
|
| 322 |
if os.path.exists(temp_reference_path): os.remove(temp_reference_path)
|
| 323 |
|
| 324 |
+
with gr.Blocks(title="Vevo-Timbre (Clean Stitch)") as demo:
|
| 325 |
gr.Markdown("## Vevo-Timbre: Zero-Shot Voice Conversion")
|
| 326 |
+
gr.Markdown("No Ghosting Version: Uses Context-Discard buffering to ensure single voice playback.")
|
| 327 |
|
| 328 |
with gr.Row():
|
| 329 |
with gr.Column():
|