Vgjkmhf commited on
Commit
d0c0da1
·
verified ·
1 Parent(s): 65b7903

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -46
app.py CHANGED
@@ -5,85 +5,97 @@ import gradio as gr
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):
14
- # بررسی ورودی‌ها
15
- if not audio_path:
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"Loading model: {model_path}")
26
 
27
- # اجرای RVC روی CPU
28
  rvc = RVCInference(device="cpu")
29
  rvc.load_model(model_path)
30
 
31
- out_path = "/tmp/output_rvc.wav"
32
  if os.path.exists(out_path): os.remove(out_path)
33
 
34
- # تشخیص هوشمند پارامترهای تابع (چون در نسخه‌های مختلف فرق دارد)
35
  sig = inspect.signature(rvc.infer_file)
36
- kwargs = {"input_path": audio_path, "output_path": out_path}
37
 
38
- # تنظیم پارامتر Pitch
39
- if "pitch" in sig.parameters: kwargs["pitch"] = int(pitch_change)
40
- elif "f0_up_key" in sig.parameters: kwargs["f0_up_key"] = int(pitch_change)
 
41
 
42
- # تنظیم پارامتر Method
43
- if "method" in sig.parameters: kwargs["method"] = f0_method
44
- elif "f0_method" in sig.parameters: kwargs["f0_method"] = f0_method
 
 
 
 
 
 
 
 
 
 
 
45
 
46
- # تنظیم پارامتر Index
47
- if "index_path" in sig.parameters: kwargs["index_path"] = index_path
48
 
49
- print(f"Processing with params: {kwargs}")
 
 
 
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 WebUI", theme=gr.themes.Soft()) as demo:
59
- gr.Markdown("# 🎵 RVC Voice Converter (CPU)")
60
- gr.Markdown("فایل‌های مدل (.pth) و ایندکس (.index) خود را آپلود کنید و صدای خود را تغییر دهید.")
61
 
62
  with gr.Row():
63
- # ستون ورودی‌ها
64
  with gr.Column():
65
- gr.Markdown("### 1. ورودی‌ها")
66
- audio_input = gr.Audio(label="🎤 صدای ورودی (ضبط یا آپلود)", type="filepath")
67
- model_input = gr.File(label="🤖 فایل مدل (.pth)", file_types=[".pth"], file_count="single")
68
- index_input = gr.File(label="📂 فایل ایندکس (.index) - اختیاری", file_types=[".index"], file_count="single")
69
 
70
- gr.Markdown("### 2. تنظیمات")
71
- pitch_slider = gr.Slider(minimum=-12, maximum=12, value=0, step=1, label="🎵 تغییر گام (Pitch)", info="مرد به زن: +12 | زن به مرد: -12")
72
- method_drop = gr.Dropdown(choices=["rmvpe", "pm"], value="rmvpe", label="⚙️ الگوریتم پردازش")
 
73
 
74
- convert_btn = gr.Button("🚀 شروع تبدیل صدا", variant="primary")
 
 
 
75
 
76
- # ستون خروجی
77
- with gr.Column():
78
- gr.Markdown("### 3. خروجی")
79
- audio_output = gr.Audio(label="🎧 صدای نهایی")
80
- status_output = gr.Textbox(label="وضعیت عملیات", lines=2)
81
 
82
- # اتصال دکمه به تابع
83
- convert_btn.click(
84
- fn=rvc_process,
85
- inputs=[audio_input, model_input, index_input, pitch_slider, method_drop],
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__":