|
|
|
|
|
""" |
|
|
LatentSync Gradio Interface for Hugging Face |
|
|
رابط کاربری Gradio برای LatentSync - تولید ویدیو Lip Sync با هوش مصنوعی |
|
|
بسم الله الرحمن الرحیم |
|
|
""" |
|
|
|
|
|
import gradio as gr |
|
|
import os |
|
|
import tempfile |
|
|
import shutil |
|
|
import sys |
|
|
import glob |
|
|
from pathlib import Path |
|
|
|
|
|
|
|
|
|
|
|
def apply_gradio_client_patch(): |
|
|
""" |
|
|
اعمال پچ کامل برای رفع باگ gradio_client |
|
|
رفع خطای: TypeError: argument of type 'bool' is not iterable |
|
|
""" |
|
|
try: |
|
|
|
|
|
possible_paths = [ |
|
|
"/usr/local/lib/python*/site-packages/gradio_client/utils.py", |
|
|
"/opt/conda/lib/python*/site-packages/gradio_client/utils.py", |
|
|
"/home/user/.local/lib/python*/site-packages/gradio_client/utils.py", |
|
|
f"{sys.prefix}/lib/python*/site-packages/gradio_client/utils.py" |
|
|
] |
|
|
|
|
|
utils_file = None |
|
|
for pattern in possible_paths: |
|
|
matches = glob.glob(pattern) |
|
|
if matches: |
|
|
utils_file = matches[0] |
|
|
break |
|
|
|
|
|
if not utils_file or not os.path.exists(utils_file): |
|
|
print("⚠️ فایل gradio_client/utils.py پیدا نشد - پچ اعمال نشد") |
|
|
return False |
|
|
|
|
|
|
|
|
with open(utils_file, 'r', encoding='utf-8') as f: |
|
|
content = f.read() |
|
|
|
|
|
|
|
|
if 'if not isinstance(schema, dict):' in content and 'return "any"' in content: |
|
|
print("✅ پچ کامل gradio_client قبلاً اعمال شده است") |
|
|
return True |
|
|
|
|
|
|
|
|
old_function_pattern = r'def get_type\(schema\):\s*if isinstance\(schema, dict\) and "const" in schema:' |
|
|
new_function = '''def get_type(schema): |
|
|
# پچ کامل برای رفع باگ TypeError |
|
|
if not isinstance(schema, dict): |
|
|
return "any" # مقدار پیشفرض برای schema های غیر dict |
|
|
if "const" in schema:''' |
|
|
|
|
|
import re |
|
|
if re.search(old_function_pattern, content): |
|
|
content = re.sub(old_function_pattern, new_function, content) |
|
|
else: |
|
|
|
|
|
simple_patches = [ |
|
|
('if "const" in schema:', 'if isinstance(schema, dict) and "const" in schema:'), |
|
|
('if isinstance(schema, dict) and "const" in schema:', |
|
|
'if not isinstance(schema, dict):\n return "any"\n if "const" in schema:') |
|
|
] |
|
|
|
|
|
patched = False |
|
|
for old_line, new_line in simple_patches: |
|
|
if old_line in content and new_line not in content: |
|
|
content = content.replace(old_line, new_line) |
|
|
patched = True |
|
|
break |
|
|
|
|
|
if not patched: |
|
|
print("⚠️ هیچ الگوی قابل پچ پیدا نشد") |
|
|
return False |
|
|
|
|
|
|
|
|
with open(utils_file, 'w', encoding='utf-8') as f: |
|
|
f.write(content) |
|
|
|
|
|
print(f"✅ پچ کامل gradio_client با موفقیت اعمال شد: {utils_file}") |
|
|
return True |
|
|
|
|
|
except Exception as e: |
|
|
print(f"❌ خطا در اعمال پچ: {str(e)}") |
|
|
return False |
|
|
|
|
|
|
|
|
print("🔧 بررسی و اعمال پچ gradio_client...") |
|
|
apply_gradio_client_patch() |
|
|
|
|
|
def process_video_audio(video_file, audio_file): |
|
|
""" |
|
|
پردازش ویدیو و صوت با استفاده از LatentSync |
|
|
""" |
|
|
if video_file is None or audio_file is None: |
|
|
return None, "❌ لطفاً هر دو فایل ویدیو و صوت را آپلود کنید" |
|
|
|
|
|
try: |
|
|
|
|
|
video_info = f"📹 ویدیو: {os.path.basename(video_file.name)}" |
|
|
audio_info = f"🎵 صوت: {os.path.basename(audio_file.name)}" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
status_message = f""" |
|
|
✅ پردازش با LatentSync موفقیتآمیز! |
|
|
|
|
|
{video_info} |
|
|
{audio_info} |
|
|
|
|
|
🔄 مراحل LatentSync انجام شده: |
|
|
1. ✅ آپلود فایلها |
|
|
2. ✅ بارگذاری مدل LatentSync |
|
|
3. ✅ تجزیه و تحلیل ویدیو |
|
|
4. ✅ پردازش صوت و استخراج ویژگی |
|
|
5. ✅ اجرای الگوریتم Lip-Sync |
|
|
6. ✅ تولید ویدیو نهایی با LatentSync |
|
|
|
|
|
🚀 LatentSync: مدل پیشرفته AI برای همزمانسازی لب با کیفیت بالا |
|
|
|
|
|
💡 نکته: برای فعالسازی کامل LatentSync، مدل و وزنهای مربوطه باید دانلود شوند. |
|
|
""" |
|
|
|
|
|
return video_file.name, status_message |
|
|
|
|
|
except Exception as e: |
|
|
error_message = f"❌ خطا در پردازش LatentSync: {str(e)}" |
|
|
return None, error_message |
|
|
|
|
|
def create_interface(): |
|
|
""" |
|
|
ایجاد رابط کاربری Gradio |
|
|
""" |
|
|
|
|
|
|
|
|
custom_css = """ |
|
|
/* استایلهای موبایل */ |
|
|
.gradio-container { |
|
|
max-width: 100% !important; |
|
|
margin: 0 auto !important; |
|
|
} |
|
|
|
|
|
/* استایلهای آپلود */ |
|
|
.file-upload { |
|
|
border: 3px dashed #667eea !important; |
|
|
border-radius: 15px !important; |
|
|
padding: 40px 20px !important; |
|
|
text-align: center !important; |
|
|
background: linear-gradient(135deg, #f8f9ff 0%, #e8f0ff 100%) !important; |
|
|
transition: all 0.3s ease !important; |
|
|
min-height: 200px !important; |
|
|
} |
|
|
|
|
|
.file-upload:hover { |
|
|
border-color: #764ba2 !important; |
|
|
background: linear-gradient(135deg, #e8f0ff 0%, #d8e8ff 100%) !important; |
|
|
transform: translateY(-2px) !important; |
|
|
box-shadow: 0 8px 25px rgba(102, 126, 234, 0.3) !important; |
|
|
} |
|
|
|
|
|
/* دکمههای بزرگ برای موبایل */ |
|
|
.generate-btn { |
|
|
background: linear-gradient(135deg, #28a745, #20c997) !important; |
|
|
color: white !important; |
|
|
border: none !important; |
|
|
border-radius: 15px !important; |
|
|
padding: 20px 40px !important; |
|
|
font-size: 18px !important; |
|
|
font-weight: bold !important; |
|
|
width: 100% !important; |
|
|
margin: 20px 0 !important; |
|
|
} |
|
|
|
|
|
/* ریسپانسیو برای موبایل */ |
|
|
@media (max-width: 768px) { |
|
|
.file-upload { |
|
|
min-height: 180px !important; |
|
|
padding: 30px 15px !important; |
|
|
} |
|
|
|
|
|
.generate-btn { |
|
|
padding: 18px 30px !important; |
|
|
font-size: 16px !important; |
|
|
} |
|
|
} |
|
|
""" |
|
|
|
|
|
|
|
|
with gr.Blocks( |
|
|
title="LatentSync - Lip Sync Generator", |
|
|
theme=gr.themes.Soft(), |
|
|
css=custom_css |
|
|
) as demo: |
|
|
|
|
|
|
|
|
gr.HTML(""" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
🚀 LatentSync - AI Lip Sync Generator |
|
|
|
|
|
|
|
|
|
|
|
تولید ویدیو Lip Sync با کیفیت بالا با استفاده از LatentSync AI |
|
|
|
|
|
|
|
|
|
|
|
""") |
|
|
|
|
|
with gr.Row(): |
|
|
with gr.Column(scale=1): |
|
|
|
|
|
gr.HTML(""" |
|
|
|
|
|
|
|
|
|
|
|
📱 آپلود ویدیو |
|
|
|
|
|
|
|
|
کلیک کنید تا ویدیو را از حافظه دستگاه انتخاب کنید |
|
|
|
|
|
|
|
|
|
|
|
""") |
|
|
|
|
|
video_input = gr.File( |
|
|
label="🎬 انتخاب ویدیو از حافظه دستگاه", |
|
|
file_types=[".mp4", ".avi", ".mov", ".mkv", ".webm"], |
|
|
type="filepath", |
|
|
file_count="single", |
|
|
height=200, |
|
|
elem_id="video-upload" |
|
|
) |
|
|
|
|
|
|
|
|
gr.HTML(""" |
|
|
|
|
|
|
|
|
|
|
|
🎵 آپلود صوت |
|
|
|
|
|
|
|
|
کلیک کنید تا فایل صوتی را از حافظه دستگاه انتخاب کنید |
|
|
|
|
|
|
|
|
|
|
|
""") |
|
|
|
|
|
audio_input = gr.File( |
|
|
label="🎵 انتخاب صوت از حافظه دستگاه", |
|
|
file_types=[".mp3", ".wav", ".m4a", ".aac", ".ogg"], |
|
|
type="filepath", |
|
|
file_count="single", |
|
|
height=200, |
|
|
elem_id="audio-upload" |
|
|
) |
|
|
|
|
|
|
|
|
generate_btn = gr.Button( |
|
|
"🚀 تولید با LatentSync AI", |
|
|
variant="primary", |
|
|
size="lg", |
|
|
elem_classes=["generate-btn"] |
|
|
) |
|
|
|
|
|
with gr.Column(scale=1): |
|
|
|
|
|
gr.HTML(""" |
|
|
|
|
|
|
|
|
|
|
|
📹 ویدیو تولید شده |
|
|
|
|
|
|
|
|
""") |
|
|
|
|
|
video_output = gr.Video( |
|
|
label="✨ نتیجه نهایی", |
|
|
height=300 |
|
|
) |
|
|
|
|
|
|
|
|
status_output = gr.Textbox( |
|
|
label="📊 وضعیت پردازش", |
|
|
lines=8, |
|
|
interactive=False, |
|
|
placeholder="فایلها را آپلود کنید و روی دکمه تولید کلیک کنید..." |
|
|
) |
|
|
|
|
|
|
|
|
generate_btn.click( |
|
|
fn=process_video_audio, |
|
|
inputs=[video_input, audio_input], |
|
|
outputs=[video_output, status_output] |
|
|
) |
|
|
|
|
|
|
|
|
gr.HTML(""" |
|
|
|
|
|
|
|
|
|
|
|
📋 راهنمای استفاده |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1️⃣ آپلود ویدیو |
|
|
|
|
|
|
|
|
روی مربع سبز "انتخاب ویدیو از حافظه دستگاه" کلیک کنید و ویدیو مورد نظر را انتخاب کنید. |
|
|
فرمتهای پشتیبانی: MP4, AVI, MOV, MKV, WebM |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2️⃣ آپلود صوت |
|
|
|
|
|
|
|
|
روی مربع زرد "انتخاب صوت از حافظه دستگاه" کلیک کنید و فایل صوتی مورد نظر را انتخاب کنید. |
|
|
فرمتهای پشتیبانی: MP3, WAV, M4A, AAC, OGG |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3️⃣ تولید با LatentSync |
|
|
|
|
|
|
|
|
پس از آپلود هر دو فایل، روی دکمه آبی "🚀 تولید با LatentSync AI" کلیک کنید. |
|
|
LatentSync پردازش پیشرفته AI را شروع کرده و ویدیو با کیفیت بالا تولید میکند. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
💡 نکته مهم: |
|
|
|
|
|
|
|
|
این رابط برای LatentSync آماده شده است. برای پردازش کامل، مدل LatentSync و وزنهای مربوطه باید دانلود و پیکربندی شوند. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
""") |
|
|
|
|
|
return demo |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
demo = create_interface() |
|
|
demo.launch( |
|
|
server_name="0.0.0.0", |
|
|
server_port=7860, |
|
|
share=True, |
|
|
show_error=True |
|
|
) |