File size: 3,345 Bytes
bd113ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import gradio as gr
from PIL import Image
import torch
from diffusers import HunyuanVideoPipeline
# ... other imports ...

# Add LORA configuration
LORA_LIST = [
    "Top_Off.safetensors",
    "huanyan_helper.safetensors",
    "huanyan_helper_alpha.safetensors",
    "hunyuan-t-solo-v1.0.safetensors",
    "stripe_v2.safetensors"
]

def create_advanced_settings():
    with gr.Accordion("Advanced Settings", open=False):
        # LORA Selection
        lora_choices = gr.CheckboxGroup(
            choices=LORA_LIST,
            label="Select LORAs",
            value=[LORA_LIST[0]]
        )

        lora_weights = {}
        for lora in LORA_LIST:
            lora_weights[lora] = gr.Slider(0.0, 1.0, value=0.8,
                                        label=f"{lora} Weight")

        # Resolution Settings
        resolution = gr.Dropdown(
            choices=["512x512", "768x768", "1024x1024"],
            value="512x512",
            label="Output Resolution"
        )

    return lora_choices, lora_weights, resolution

def validate_image_resolution(image, resolution):
    if image is None:
        return
    img = Image.open(image)
    w, h = img.size
    if f"{w}x{h}" != resolution:
        raise gr.Error(f"Image resolution ({w}x{h}) must match output resolution ({resolution})")

def generate_video(prompt, negative_prompt, lora_choices, lora_weights,
                  resolution, image_input=None, steps=30):
    # Validate image resolution if provided
    if image_input:
        validate_image_resolution(image_input, resolution)

    # Load base model
    pipe = HunyuanVideoPipeline.from_pretrained(
        "Tencent-Hunyuan/Hunyuan-Video-Lite",
        torch_dtype=torch.float16
    ).to("cuda")

    # Apply selected LORAs
    for lora in lora_choices:
        pipe.load_lora_weights(
            f"TTV4ME/{lora}",
            adapter_name="hunyuanvideo-lora",
            weight_name=lora_weights[lora]
        )

    # Generate from image or text
    if image_input:
        image = Image.open(image_input).convert("RGB")
        output = pipe.image_to_video(
            image,
            prompt=prompt,
            negative_prompt=negative_prompt,
            num_frames=24,
            height=int(resolution.split("x")[1]),
            width=int(resolution.split("x")[0]),
            num_inference_steps=steps
        )
    else:
        output = pipe.text_to_video(
            prompt=prompt,
            negative_prompt=negative_prompt,
            height=int(resolution.split("x")[1]),
            width=int(resolution.split("x")[0]),
            num_inference_steps=steps
        )

    return output.video

# Update interface
with gr.Blocks() as demo:
    with gr.Row():
        with gr.Column():
            prompt = gr.Textbox(label="Prompt")
            negative_prompt = gr.Textbox(label="Negative Prompt")
            image_input = gr.Image(label="Input Image", type="filepath")

            lora_choices, lora_weights, resolution = create_advanced_settings()

            generate_btn = gr.Button("Generate Video")

        with gr.Column():
            output_video = gr.Video(label="Generated Video")

    generate_btn.click(
        fn=generate_video,
        inputs=[prompt, negative_prompt, lora_choices,
               lora_weights, resolution, image_input],
        outputs=output_video
    )