Update app.py
Browse files
app.py
CHANGED
|
@@ -1,53 +1,139 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from rvc_python.infer import RVCInference
|
| 4 |
|
| 5 |
def rvc_process(audio_path, model_file, index_file, pitch_change, f0_method):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
if not audio_path:
|
| 7 |
-
return None, "⚠️
|
|
|
|
| 8 |
if not model_file:
|
| 9 |
-
return None, "⚠️ فایل مدل (.pth) آپلود
|
| 10 |
|
| 11 |
try:
|
| 12 |
-
#
|
| 13 |
-
#
|
| 14 |
model_path = model_file.name if hasattr(model_file, 'name') else model_file
|
| 15 |
-
index_path =
|
| 16 |
-
|
| 17 |
-
print(f"Processing Model: {model_path}")
|
| 18 |
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
rvc = RVCInference(device="cpu")
|
| 21 |
|
|
|
|
| 22 |
rvc.load_model(model_path)
|
| 23 |
-
output_path = "output.wav"
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
rvc.infer_file(
|
| 26 |
input_path=audio_path,
|
| 27 |
output_path=output_path,
|
| 28 |
-
f0_up_key=int(pitch_change),
|
| 29 |
-
index_path=index_path,
|
| 30 |
-
f0_method=f0_method,
|
| 31 |
-
index_rate=0.75 if index_path else 0,
|
| 32 |
-
protect=0.33
|
| 33 |
)
|
| 34 |
-
|
|
|
|
|
|
|
| 35 |
|
| 36 |
except Exception as e:
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
demo.queue().launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import shutil
|
| 4 |
+
|
| 5 |
+
# تنظیم مسیرهای موقت برای جلوگیری از خطای دسترسی
|
| 6 |
+
os.environ["TEMP"] = "/tmp"
|
| 7 |
+
os.environ["TMPDIR"] = "/tmp"
|
| 8 |
+
|
| 9 |
+
# ایمپورت اصلی پس از تنظیم متغیرهای محیطی
|
| 10 |
from rvc_python.infer import RVCInference
|
| 11 |
|
| 12 |
def rvc_process(audio_path, model_file, index_file, pitch_change, f0_method):
|
| 13 |
+
"""
|
| 14 |
+
تابع اصلی پردازش صدا
|
| 15 |
+
"""
|
| 16 |
+
# بررسی ورودیها
|
| 17 |
if not audio_path:
|
| 18 |
+
return None, "⚠️ خطا: لطفاً صدای ورودی را ضبط یا آپلود کنید."
|
| 19 |
+
|
| 20 |
if not model_file:
|
| 21 |
+
return None, "⚠️ خطا: لطفاً فایل مدل (.pth) را آپلود کنید."
|
| 22 |
|
| 23 |
try:
|
| 24 |
+
# 1. استخراج مسیر فایلها
|
| 25 |
+
# در نسخههای مختلف Gradio، فایل آپلود شده ممکن است string مسیر باشد یا شیء فایل
|
| 26 |
model_path = model_file.name if hasattr(model_file, 'name') else model_file
|
| 27 |
+
index_path = None
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
if index_file:
|
| 30 |
+
index_path = index_file.name if hasattr(index_file, 'name') else index_file
|
| 31 |
+
|
| 32 |
+
print(f"--- شروع پردازش ---")
|
| 33 |
+
print(f"مدل: {model_path}")
|
| 34 |
+
print(f"صدا: {audio_path}")
|
| 35 |
+
print(f"پیچ: {pitch_change}")
|
| 36 |
+
|
| 37 |
+
# 2. راهاندازی RVC
|
| 38 |
+
# نکته: در نسخه رایگان HF فقط CPU داریم
|
| 39 |
rvc = RVCInference(device="cpu")
|
| 40 |
|
| 41 |
+
# 3. بارگذاری مدل
|
| 42 |
rvc.load_model(model_path)
|
|
|
|
| 43 |
|
| 44 |
+
# 4. تعیین مسیر خروجی در پوشه tmp (برای دسترسی نوشتن)
|
| 45 |
+
output_path = "/tmp/output_converted.wav"
|
| 46 |
+
|
| 47 |
+
# پاک کردن خروجی قبلی اگر وجود داشته باشد
|
| 48 |
+
if os.path.exists(output_path):
|
| 49 |
+
os.remove(output_path)
|
| 50 |
+
|
| 51 |
+
# 5. اجرای تبدیل (Inference)
|
| 52 |
rvc.infer_file(
|
| 53 |
input_path=audio_path,
|
| 54 |
output_path=output_path,
|
| 55 |
+
f0_up_key=int(pitch_change), # تغییر گام
|
| 56 |
+
index_path=index_path, # فایل ایندکس (اختیاری)
|
| 57 |
+
f0_method=f0_method, # متد پردازش
|
| 58 |
+
index_rate=0.75 if index_path else 0, # تاثیر ایندکس (اگر باشد)
|
| 59 |
+
protect=0.33 # حفاظت از فرکانسهای پایین
|
| 60 |
)
|
| 61 |
+
|
| 62 |
+
print("✅ تبدیل با موفقیت انجام شد.")
|
| 63 |
+
return output_path, "✅ تبدیل موفقیتآمیز بود! فایل آماده پخش است."
|
| 64 |
|
| 65 |
except Exception as e:
|
| 66 |
+
error_msg = f"❌ خطا در پردازش: {str(e)}"
|
| 67 |
+
print(error_msg)
|
| 68 |
+
return None, error_msg
|
| 69 |
+
|
| 70 |
+
# --- رابط کاربری Gradio ---
|
| 71 |
+
with gr.Blocks(title="RVC Web Inference", theme=gr.themes.Soft()) as demo:
|
| 72 |
+
|
| 73 |
+
gr.Markdown("""
|
| 74 |
+
# 🎵 پردازنده RVC (آپلود مستقیم)
|
| 75 |
+
|
| 76 |
+
با استفاده از این ابزار میتوانید:
|
| 77 |
+
1. صدای خود را ضبط یا آپلود کنید.
|
| 78 |
+
2. فایل مدل RVC (`.pth`) خود را آپلود کنید.
|
| 79 |
+
3. صدای خود را به صدای مدل تبدیل کنید.
|
| 80 |
+
""")
|
| 81 |
+
|
| 82 |
+
with gr.Row():
|
| 83 |
+
# ستون اول: ورودیها
|
| 84 |
+
with gr.Column(scale=1):
|
| 85 |
+
gr.Markdown("### 📥 ورودیها")
|
| 86 |
+
|
| 87 |
+
audio_input = gr.Audio(
|
| 88 |
+
label="1. صدای ورودی (میکروفون یا فایل)",
|
| 89 |
+
type="filepath"
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
model_input = gr.File(
|
| 93 |
+
label="2. فایل مدل (.pth)",
|
| 94 |
+
file_types=[".pth"],
|
| 95 |
+
file_count="single"
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
index_input = gr.File(
|
| 99 |
+
label="3. فایل ایندکس (.index) [اختیاری]",
|
| 100 |
+
file_types=[".index"],
|
| 101 |
+
file_count="single"
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
gr.Markdown("### ⚙️ تنظیمات")
|
| 105 |
+
|
| 106 |
+
pitch_slider = gr.Slider(
|
| 107 |
+
minimum=-12,
|
| 108 |
+
maximum=12,
|
| 109 |
+
step=1,
|
| 110 |
+
value=0,
|
| 111 |
+
label="تغییر گام (Pitch)",
|
| 112 |
+
info="اگر مدل زن است و شما مرد هستید: +12 | برعکس: -12"
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
method_drop = gr.Dropdown(
|
| 116 |
+
choices=["rmvpe", "pm", "crepe"],
|
| 117 |
+
value="rmvpe",
|
| 118 |
+
label="الگوریتم پردازش",
|
| 119 |
+
info="rmvpe بهترین کیفیت را دارد اما کمی کندتر است."
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
convert_btn = gr.Button("🚀 شروع تبدیل صدا", variant="primary", size="lg")
|
| 123 |
+
|
| 124 |
+
# ستون دوم: خروجی
|
| 125 |
+
with gr.Column(scale=1):
|
| 126 |
+
gr.Markdown("### 🎧 خروجی")
|
| 127 |
+
audio_output = gr.Audio(label="صدای تبدیل شده", type="filepath")
|
| 128 |
+
status_output = gr.Textbox(label="وضعیت عملیات", lines=2)
|
| 129 |
+
|
| 130 |
+
# اتصال دکمه به تابع
|
| 131 |
+
convert_btn.click(
|
| 132 |
+
fn=rvc_process,
|
| 133 |
+
inputs=[audio_input, model_input, index_input, pitch_slider, method_drop],
|
| 134 |
+
outputs=[audio_output, status_output]
|
| 135 |
+
)
|
| 136 |
+
|
| 137 |
+
# اجرای برنامه
|
| 138 |
+
if __name__ == "__main__":
|
| 139 |
demo.queue().launch(server_name="0.0.0.0", server_port=7860)
|