Update app.py
Browse files
app.py
CHANGED
|
@@ -5,85 +5,97 @@ import gradio as gr
|
|
| 5 |
from rvc_python.infer import RVCInference
|
| 6 |
import static_ffmpeg
|
| 7 |
|
| 8 |
-
# تنظیمات
|
| 9 |
static_ffmpeg.add_paths()
|
| 10 |
ffmpeg_path = imageio_ffmpeg.get_ffmpeg_exe()
|
| 11 |
os.environ["PATH"] += os.pathsep + os.path.dirname(ffmpeg_path)
|
| 12 |
|
| 13 |
-
def rvc_process(audio_path, model_file, index_file, pitch_change, f0_method):
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
return None, "⚠️ خطا: لطفاً فایل صوتی را آپلود یا ضبط کنید."
|
| 17 |
-
if not model_file:
|
| 18 |
-
return None, "⚠️ خطا: لطفاً فایل مدل (.pth) را آپلود کنید."
|
| 19 |
|
| 20 |
try:
|
| 21 |
-
# گرفتن مسیر فایلها
|
| 22 |
model_path = model_file.name
|
| 23 |
index_path = index_file.name if index_file else None
|
| 24 |
|
| 25 |
-
print(f"
|
| 26 |
|
| 27 |
-
# اجرای RVC روی CPU
|
| 28 |
rvc = RVCInference(device="cpu")
|
| 29 |
rvc.load_model(model_path)
|
| 30 |
|
| 31 |
-
out_path = "/tmp/
|
| 32 |
if os.path.exists(out_path): os.remove(out_path)
|
| 33 |
|
| 34 |
-
#
|
| 35 |
sig = inspect.signature(rvc.infer_file)
|
| 36 |
-
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
| 41 |
|
| 42 |
-
#
|
| 43 |
-
if "
|
| 44 |
-
elif "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
#
|
| 47 |
-
if "
|
| 48 |
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
| 50 |
rvc.infer_file(**kwargs)
|
| 51 |
|
| 52 |
-
return out_path, "✅ تبدیل
|
| 53 |
|
| 54 |
except Exception as e:
|
| 55 |
return None, f"❌ خطا: {str(e)}"
|
| 56 |
|
| 57 |
-
#
|
| 58 |
-
with gr.Blocks(title="RVC
|
| 59 |
-
gr.Markdown("#
|
| 60 |
-
gr.Markdown("
|
| 61 |
|
| 62 |
with gr.Row():
|
| 63 |
-
# ستون ورودیها
|
| 64 |
with gr.Column():
|
| 65 |
-
gr.Markdown("### 1.
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
|
|
|
| 73 |
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
outputs=[audio_output, status_output]
|
| 87 |
)
|
| 88 |
|
| 89 |
if __name__ == "__main__":
|
|
|
|
| 5 |
from rvc_python.infer import RVCInference
|
| 6 |
import static_ffmpeg
|
| 7 |
|
| 8 |
+
# تنظیمات سیستمی و FFmpeg
|
| 9 |
static_ffmpeg.add_paths()
|
| 10 |
ffmpeg_path = imageio_ffmpeg.get_ffmpeg_exe()
|
| 11 |
os.environ["PATH"] += os.pathsep + os.path.dirname(ffmpeg_path)
|
| 12 |
|
| 13 |
+
def rvc_process(audio_path, model_file, index_file, pitch_change, f0_method, index_rate, protect_val, filter_radius):
|
| 14 |
+
if not audio_path or not model_file:
|
| 15 |
+
return None, "⚠️ فایلها کامل نیستند."
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
try:
|
|
|
|
| 18 |
model_path = model_file.name
|
| 19 |
index_path = index_file.name if index_file else None
|
| 20 |
|
| 21 |
+
print(f"Processing Model: {model_path}")
|
| 22 |
|
|
|
|
| 23 |
rvc = RVCInference(device="cpu")
|
| 24 |
rvc.load_model(model_path)
|
| 25 |
|
| 26 |
+
out_path = "/tmp/output_persian_fix.wav"
|
| 27 |
if os.path.exists(out_path): os.remove(out_path)
|
| 28 |
|
| 29 |
+
# کشف پارامترهای تابع (برای سازگاری با نسخههای مختلف)
|
| 30 |
sig = inspect.signature(rvc.infer_file)
|
| 31 |
+
params = sig.parameters
|
| 32 |
|
| 33 |
+
kwargs = {
|
| 34 |
+
"input_path": audio_path,
|
| 35 |
+
"output_path": out_path,
|
| 36 |
+
}
|
| 37 |
|
| 38 |
+
# 1. پیچ (Pitch)
|
| 39 |
+
if "pitch" in params: kwargs["pitch"] = int(pitch_change)
|
| 40 |
+
elif "f0_up_key" in params: kwargs["f0_up_key"] = int(pitch_change)
|
| 41 |
+
|
| 42 |
+
# 2. متد (Method) - حتما RMVPE
|
| 43 |
+
if "method" in params: kwargs["method"] = f0_method
|
| 44 |
+
elif "f0_method" in params: kwargs["f0_method"] = f0_method
|
| 45 |
+
|
| 46 |
+
# 3. ایندکس (Index)
|
| 47 |
+
if "index_path" in params: kwargs["index_path"] = index_path
|
| 48 |
+
if "index_rate" in params: kwargs["index_rate"] = float(index_rate)
|
| 49 |
+
|
| 50 |
+
# 4. محافظت (Protect) - حیاتی برای فارسی
|
| 51 |
+
if "protect" in params: kwargs["protect"] = float(protect_val)
|
| 52 |
|
| 53 |
+
# 5. فیلتر (Filter Radius) - برای رفع لرزش صدا
|
| 54 |
+
if "filter_radius" in params: kwargs["filter_radius"] = int(filter_radius)
|
| 55 |
|
| 56 |
+
# 6. دقت زمانی (Hop Length) - برای کیفیت بهتر
|
| 57 |
+
if "hop_length" in params: kwargs["hop_length"] = 64 # دقت بالاتر (پیشفرض 128 است)
|
| 58 |
+
|
| 59 |
+
print(f"Running with: {kwargs}")
|
| 60 |
rvc.infer_file(**kwargs)
|
| 61 |
|
| 62 |
+
return out_path, "✅ تبدیل انجام شد (تنظیمات فارسی اعمال شد)"
|
| 63 |
|
| 64 |
except Exception as e:
|
| 65 |
return None, f"❌ خطا: {str(e)}"
|
| 66 |
|
| 67 |
+
# رابط کاربری پیشرفته فارسی
|
| 68 |
+
with gr.Blocks(title="Persian RVC", theme=gr.themes.Soft()) as demo:
|
| 69 |
+
gr.Markdown("# 🇮🇷 مبدل صدای RVC (بهینه برای فارسی)")
|
| 70 |
+
gr.Markdown("این نسخه برای رفع مشکل خشدار بودن صدا و تداخل لهجه تنظیم شده است.")
|
| 71 |
|
| 72 |
with gr.Row():
|
|
|
|
| 73 |
with gr.Column():
|
| 74 |
+
gr.Markdown("### 1. فایلهای ورودی")
|
| 75 |
+
audio_in = gr.Audio(label="صدای ورودی (واضح و بدون نویز)", type="filepath")
|
| 76 |
+
model_in = gr.File(label="فایل مدل (.pth)", file_types=[".pth"])
|
| 77 |
+
index_in = gr.File(label="فایل ایندکس (.index)", file_types=[".index"])
|
| 78 |
|
| 79 |
+
with gr.Column():
|
| 80 |
+
gr.Markdown("### 2. تنظیمات حیاتی")
|
| 81 |
+
pitch = gr.Slider(-12, 12, value=0, step=1, label="تغییر گام (Pitch)", info="مرد به زن: +12 | زن به مرد: -12")
|
| 82 |
+
method = gr.Dropdown(["rmvpe", "pm"], value="rmvpe", label="الگوریتم", info="فقط RMVPE کیفیت مناسب دارد.")
|
| 83 |
|
| 84 |
+
gr.Markdown("### 3. تنظیمات رفع خش و تداخل")
|
| 85 |
+
index_rate = gr.Slider(0, 1, value=0.3, step=0.05, label="شدت ایندکس (Index Rate)", info="پیشنهاد فارسی: 0.3 تا 0.4 (کمتر = تداخل کمتر)")
|
| 86 |
+
protect = gr.Slider(0, 0.5, value=0.5, step=0.01, label="محافظت (Protect)", info="پیشنهاد فارسی: 0.5 (حداکثر) برای جلوگیری از خشدار شدن")
|
| 87 |
+
filter_radius = gr.Slider(0, 7, value=3, step=1, label="فیلتر نرمکننده (Filter Radius)", info="برای حذف لرزشهای اضافه (پیشنهاد: 3)")
|
| 88 |
|
| 89 |
+
btn = gr.Button("✨ شروع پردازش", variant="primary")
|
| 90 |
+
|
| 91 |
+
with gr.Row():
|
| 92 |
+
audio_out = gr.Audio(label="خروجی نهایی")
|
| 93 |
+
status = gr.Textbox(label="پیام سیستم")
|
| 94 |
|
| 95 |
+
btn.click(
|
| 96 |
+
rvc_process,
|
| 97 |
+
[audio_in, model_in, index_in, pitch, method, index_rate, protect, filter_radius],
|
| 98 |
+
[audio_out, status]
|
|
|
|
| 99 |
)
|
| 100 |
|
| 101 |
if __name__ == "__main__":
|