Spaces:
Running

File size: 2,821 Bytes
3956638
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import gradio as gr
import torch
import os
from diffusers import WanPipeline
from diffusers.utils import export_to_video

# تنظیمات شناسه‌های مدل در هاگینگ‌فیس
# اگر GPU شما محدود است از نسخه 1.3B استفاده کنید، در غیر این صورت 14B
BASE_MODEL_ID = "Wan-AI/Wan2.1-T2V-1.3B-Diffusers" 
LORA_ID = "LiconStudio/VBVR-wan2.2-comfy" # آیدی مخزن حاوی لورای VBVR

print("در حال بارگذاری مدل پایه...")
# بارگذاری پایپ‌لاین مدل
pipe = WanPipeline.from_pretrained(
    BASE_MODEL_ID,
    torch_dtype=torch.bfloat16
)

print("در حال بارگذاری LoRA...")
# اعمال لورای VBVR روی مدل
pipe.load_lora_weights(LORA_ID)

# انتقال مدل به GPU و بهینه‌سازی حافظه
pipe.enable_model_cpu_offload()

def generate_video(prompt, negative_prompt, num_frames, num_steps):
    print(f"شروع ساخت ویدیو با پرامپت: {prompt}")
    
    # تولید فریم‌های ویدیو
    output = pipe(
        prompt=prompt,
        negative_prompt=negative_prompt,
        num_frames=num_frames,
        num_inference_steps=num_steps,
        guidance_scale=5.0
    ).frames[0]
    
    # ذخیره فریم‌ها به عنوان یک فایل MP4
    output_path = "output_video.mp4"
    export_to_video(output, output_path, fps=24)
    
    return output_path

# طراحی رابط کاربری با Gradio
with gr.Blocks(theme=gr.themes.Soft()) as demo:
    gr.Markdown("# 🎬 ساخت ویدیو با Wan 2.2 + VBVR LoRA")
    gr.Markdown("مدل پایه Wan به همراه افزونه استدلال بصری (VBVR) برای تولید ویدیوهای منسجم‌تر.")
    
    with gr.Row():
        with gr.Column():
            prompt_input = gr.Textbox(label="پرامپت شما (متن به انگلیسی)", lines=3, placeholder="مثال: A polar bear solving a Rubik's cube...")
            negative_prompt = gr.Textbox(label="پرامپت منفی", value="low quality, bad anatomy, worst quality, distorted")
            
            with gr.Row():
                frames_slider = gr.Slider(minimum=17, maximum=81, value=49, step=1, label="تعداد فریم‌ها")
                steps_slider = gr.Slider(minimum=10, maximum=50, value=30, step=1, label="تعداد مراحل (Steps)")
                
            generate_btn = gr.Button("🚀 تولید ویدیو", variant="primary")
            
        with gr.Column():
            video_output = gr.Video(label="ویدیوی نهایی")
            
    generate_btn.click(
        fn=generate_video,
        inputs=[prompt_input, negative_prompt, frames_slider, steps_slider],
        outputs=video_output
    )

if __name__ == "__main__":
    demo.launch()