File size: 6,918 Bytes
2dd154f
bd113ad
2dd154f
 
5dfd9f8
 
2dd154f
5dfd9f8
2dd154f
bd113ad
2dd154f
5dfd9f8
 
 
 
 
 
 
bd113ad
5dfd9f8
 
bd113ad
5dfd9f8
 
2dd154f
5dfd9f8
2dd154f
 
 
5dfd9f8
 
 
 
 
 
 
 
 
 
 
2dd154f
 
 
 
5dfd9f8
2dd154f
 
 
 
 
 
5dfd9f8
 
2dd154f
 
5dfd9f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2dd154f
 
 
 
 
 
5dfd9f8
2dd154f
5dfd9f8
 
2dd154f
 
5dfd9f8
2dd154f
 
 
 
5dfd9f8
2dd154f
 
5dfd9f8
2dd154f
 
 
 
5dfd9f8
2dd154f
5dfd9f8
 
2dd154f
bd113ad
 
2dd154f
 
 
5dfd9f8
2dd154f
 
 
5dfd9f8
 
 
 
 
bd113ad
2dd154f
5dfd9f8
bd113ad
2dd154f
5dfd9f8
 
bd113ad
2dd154f
 
bd113ad
2dd154f
5dfd9f8
 
 
 
 
 
 
 
 
2dd154f
 
 
 
 
 
5dfd9f8
2dd154f
 
 
 
 
 
5dfd9f8
2dd154f
5dfd9f8
2dd154f
 
5dfd9f8
 
 
2dd154f
 
 
 
5dfd9f8
2dd154f
 
 
5dfd9f8
2dd154f
5dfd9f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2dd154f
 
 
 
5dfd9f8
 
 
2dd154f
 
 
 
 
 
bd113ad
2dd154f
 
 
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import spaces
import gradio as gr
import numpy as np
import os
import torch
from PIL import Image
from pathlib import Path
from diffusers import HunyuanVideoPipeline
from huggingface_hub import snapshot_download

# Configuration
LORA_CHOICES = [
    "Top_Off.safetensors",
    "huanyan_helper.safetensors",
    "huanyan_helper_alpha.safetensors",
    "hunyuan-t-solo-v1.0.safetensors",
    "stripe_v2.safetensors"
]

MAX_SEED = np.iinfo(np.int32).max
MAX_IMAGE_SIZE = 1024

# Initialize pipeline with ZeroGPU optimizations
model_id = "Tencent-Hunyuan/Hunyuan-Video-Lite"
pipe = HunyuanVideoPipeline.from_pretrained(
    model_id,
    torch_dtype=torch.float16
).to("cuda")

# Load all available LoRAs
for lora_file in LORA_CHOICES:
    try:
        pipe.load_lora_weights(
            "Sergidev/TTV4ME",
            weight_name=lora_file,
            adapter_name=lora_file.split('.')[0],
            token=os.environ.get("HF_TOKEN")
        )
    except Exception as e:
        print(f"Error loading {lora_file}: {str(e)}")

@spaces.GPU(duration=300)
def generate(
    prompt,
    image_input,
    height,
    width,
    num_frames,
    num_inference_steps,
    seed_value,
    fps,
    selected_loras,
    lora_weights,
    progress=gr.Progress(track_tqdm=True)
):
    # Image validation
    if image_input is not None:
        img = Image.open(image_input)
        if img.size != (width, height):
            raise gr.Error(f"Image resolution {img.size} must match video resolution {width}x{height}")
        prompt = f"Image prompt: {prompt}" if prompt else "Based on uploaded image"

    # Set active LoRAs
    active_adapters = []
    adapter_weights = []
    for idx, selected in enumerate(selected_loras):
        if selected:
            active_adapters.append(LORA_CHOICES[idx].split('.')[0])
            adapter_weights.append(lora_weights[idx])

    if active_adapters:
        pipe.set_adapters(active_adapters, adapter_weights)

    # Generation logic
    torch.cuda.empty_cache()
    if seed_value == -1:
        seed_value = torch.randint(0, MAX_SEED, (1,)).item()

    generator = torch.Generator('cuda').manual_seed(seed_value)

    try:
        if image_input:
            output = pipe.image_to_video(
                Image.open(image_input).convert("RGB"),
                prompt=prompt,
                height=height,
                width=width,
                num_frames=num_frames,
                num_inference_steps=num_inference_steps,
                generator=generator,
            )
        else:
            output = pipe.text_to_video(
                prompt=prompt,
                height=height,
                width=width,
                num_frames=num_frames,
                num_inference_steps=num_inference_steps,
                generator=generator,
            )

        return output.video
    finally:
        torch.cuda.empty_cache()

def apply_preset(preset_name):
    if preset_name == "Higher Resolution":
        return [608, 448, 24, 29, 12]
    elif preset_name == "More Frames":
        return [512, 320, 42, 27, 14]
    return [512, 512, 24, 25, 12]

css = """
/* Existing CSS remains unchanged */
"""

with gr.Blocks(css=css, theme="dark") as demo:
    with gr.Column(elem_id="col-container"):
        gr.Markdown("# 🎬 Hunyuan Studio", elem_classes=["title"])
        gr.Markdown(
            """Text-to-Video & Image-to-Video generation with multiple LoRA adapters.<br>
            Ensure image resolution matches selected video dimensions.""",
            elem_classes=["description"]
        )

        with gr.Column(elem_classes=["prompt-container"]):
            prompt = gr.Textbox(
                label="Prompt",
                placeholder="Enter text prompt or describe the image...",
                elem_classes=["prompt-textbox"],
                lines=3
            )
            image_input = gr.Image(
                label="Upload Reference Image (Optional)",
                type="filepath",
                visible=True
            )

        with gr.Row():
            run_button = gr.Button("🎬 Generate Video", variant="primary", size="lg")

        with gr.Row(elem_classes=["preset-buttons"]):
            preset_high_res = gr.Button("📺 Resolution Preset")
            preset_more_frames = gr.Button("🎞️ Frames Preset")

        with gr.Row():
            result = gr.Video(label="Generated Video")

        with gr.Accordion("⚙️ Advanced Settings", open=False):
            with gr.Row():
                seed = gr.Slider(
                    label="Seed (-1 for random)",
                    minimum=-1,
                    maximum=MAX_SEED,
                    step=1,
                    value=-1,
                )

            with gr.Row():
                height = gr.Slider(
                    label="Height",
                    minimum=256,
                    maximum=MAX_IMAGE_SIZE,
                    step=16,
                    value=512,
                )
                width = gr.Slider(
                    label="Width",
                    minimum=256,
                    maximum=MAX_IMAGE_SIZE,
                    step=16,
                    value=512,
                )

            with gr.Row():
                num_frames = gr.Slider(
                    label="Frame Count",
                    minimum=1,
                    maximum=257,
                    step=1,
                    value=24,
                )
                num_inference_steps = gr.Slider(
                    label="Inference Steps",
                    minimum=1,
                    maximum=50,
                    step=1,
                    value=25,
                )
                fps = gr.Slider(
                    label="FPS",
                    minimum=1,
                    maximum=60,
                    step=1,
                    value=12,
                )

            with gr.Accordion("🧩 LoRA Configuration", open=False):
                lora_checkboxes = []
                lora_sliders = []
                for lora in LORA_CHOICES:
                    with gr.Row():
                        cb = gr.Checkbox(label=f"Enable {lora}", value=False)
                        sl = gr.Slider(0.0, 1.0, value=0.8, label=f"{lora} Weight")
                        lora_checkboxes.append(cb)
                        lora_sliders.append(sl)

    # Event handling
    run_button.click(
        fn=generate,
        inputs=[prompt, image_input, height, width, num_frames,
               num_inference_steps, seed, fps, lora_checkboxes, lora_sliders],
        outputs=result
    )

    preset_high_res.click(
        fn=lambda: apply_preset("Higher Resolution"),
        outputs=[height, width, num_frames, num_inference_steps, fps]
    )

    preset_more_frames.click(
        fn=lambda: apply_preset("More Frames"),
        outputs=[height, width, num_frames, num_inference_steps, fps]
    )