File size: 9,455 Bytes
8e9c347
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import spaces
import gradio as gr
from sd3_pipeline import StableDiffusion3Pipeline
import torch
import random
import numpy as np
import os
import gc
from diffusers import AutoencoderKLWan
from wan_pipeline import WanPipeline
from diffusers.schedulers.scheduling_unipc_multistep import UniPCMultistepScheduler
from PIL import Image
from diffusers.utils import export_to_video
from huggingface_hub import login

# Authenticate with HF
login(token=os.getenv('HF_TOKEN'))

def set_seed(seed):
    random.seed(seed)
    os.environ['PYTHONHASHSEED'] = str(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    if torch.cuda.is_available():
        torch.cuda.manual_seed(seed)

# Updated model paths - now includes gated models
model_paths = {
    "sd2.1": "stabilityai/stable-diffusion-2-1",
    "sdxl": "stabilityai/stable-diffusion-xl-base-1.0",
    "sd3": "stabilityai/stable-diffusion-3-medium-diffusers",
    "sd3.5": "stabilityai/stable-diffusion-3.5-large",
    # "wan-t2v": "Wan-AI/Wan2.1-T2V-1.3B-Diffusers"  # Keep commented if you don't have access to this one
}

current_model = None
OUTPUT_DIR = "generated_videos"
os.makedirs(OUTPUT_DIR, exist_ok=True)

def load_model(model_name):
    global current_model
    if current_model is not None:
        del current_model
        if torch.cuda.is_available():
            torch.cuda.empty_cache()
        gc.collect()

    # Determine device
    device = "cuda" if torch.cuda.is_available() else "cpu"
    
    if "wan-t2v" in model_name:
        vae = AutoencoderKLWan.from_pretrained(
            model_paths[model_name], 
            subfolder="vae", 
            torch_dtype=torch.bfloat16 if device == "cuda" else torch.float32
        )
        scheduler = UniPCMultistepScheduler(
            prediction_type='flow_prediction', 
            use_flow_sigmas=True, 
            num_train_timesteps=1000, 
            flow_shift=8.0
        )
        current_model = WanPipeline.from_pretrained(
            model_paths[model_name], 
            vae=vae, 
            torch_dtype=torch.float16 if device == "cuda" else torch.float32
        ).to(device)
        current_model.scheduler = scheduler
    else:
        # Handle different model types
        if model_name in ["sd2.1"]:
            from diffusers import StableDiffusionPipeline
            current_model = StableDiffusionPipeline.from_pretrained(
                model_paths[model_name], 
                torch_dtype=torch.bfloat16 if device == "cuda" else torch.float32
            ).to(device)
        elif model_name in ["sdxl"]:
            from diffusers import StableDiffusionXLPipeline
            current_model = StableDiffusionXLPipeline.from_pretrained(
                model_paths[model_name], 
                torch_dtype=torch.bfloat16 if device == "cuda" else torch.float32
            ).to(device)
        else:
            # For SD3 models (when access is granted)
            current_model = StableDiffusion3Pipeline.from_pretrained(
                model_paths[model_name], 
                torch_dtype=torch.bfloat16 if device == "cuda" else torch.float32
            ).to(device)

    return current_model

@spaces.GPU(duration=120)
def generate_content(prompt, model_name, guidance_scale=7.5, num_inference_steps=50,
                     use_cfg_zero_star=True, use_zero_init=True, zero_steps=0,
                     seed=None, compare_mode=False):
    
    model = load_model(model_name)
    if seed is None:
        seed = random.randint(0, 2**32 - 1)
    set_seed(seed)

    is_video_model = "wan-t2v" in model_name
    print('prompt: ', prompt)
    
    if is_video_model:
        negative_prompt = "Bright tones, overexposed, static, blurred details, subtitles, style, works, paintings, images, static, overall gray, worst quality, low quality, JPEG compression residue, ugly, incomplete, extra fingers, poorly drawn hands, poorly drawn faces, deformed, disfigured, misshapen limbs, fused fingers, still picture, messy background, three legs, many people in the background, walking backwards"
        video1_frames = model(
            prompt=prompt,
            negative_prompt=negative_prompt,
            height=480,
            width=832,
            num_frames=81,
            num_inference_steps=num_inference_steps,
            guidance_scale=guidance_scale,
            use_cfg_zero_star=True,
            use_zero_init=True,
            zero_steps=zero_steps
        ).frames[0]
        video1_path = os.path.join(OUTPUT_DIR, f"{seed}_CFG-Zero-Star.mp4")
        export_to_video(video1_frames, video1_path, fps=16)

        return None, None, video1_path, seed

    # Handle different model types for image generation
    if model_name in ["sd2.1", "sdxl"]:
        # Standard diffusers pipeline interface
        if compare_mode:
            set_seed(seed)
            image1 = model(
                prompt,
                guidance_scale=guidance_scale,
                num_inference_steps=num_inference_steps,
            ).images[0]

            set_seed(seed)
            image2 = model(
                prompt,
                guidance_scale=guidance_scale,
                num_inference_steps=num_inference_steps,
            ).images[0]

            return image1, image2, None, seed
        else:
            image = model(
                prompt,
                guidance_scale=guidance_scale,
                num_inference_steps=num_inference_steps,
            ).images[0]

            return image, None, None, seed
    else:
        # SD3 models with custom parameters
        if compare_mode:
            set_seed(seed)
            image1 = model(
                prompt,
                guidance_scale=guidance_scale,
                num_inference_steps=num_inference_steps,
                use_cfg_zero_star=True,
                use_zero_init=use_zero_init,
                zero_steps=zero_steps
            ).images[0]

            set_seed(seed)
            image2 = model(
                prompt,
                guidance_scale=guidance_scale,
                num_inference_steps=num_inference_steps,
                use_cfg_zero_star=False,
                use_zero_init=use_zero_init,
                zero_steps=zero_steps
            ).images[0]

            return image1, image2, None, seed
        else:
            image = model(
                prompt,
                guidance_scale=guidance_scale,
                num_inference_steps=num_inference_steps,
                use_cfg_zero_star=use_cfg_zero_star,
                use_zero_init=use_zero_init,
                zero_steps=zero_steps
            ).images[0]

            if use_cfg_zero_star:
                return image, None, None, seed
            else:
                return None, image, None, seed

# Gradio UI with left-right layout
with gr.Blocks() as demo:
    gr.HTML("""
        <div style="text-align: center; font-size: 32px; font-weight: bold; margin-bottom: 20px;">
            CFG-Zero*: Improved Classifier-Free Guidance for Flow Matching Models
        </div>
        <div style="text-align: center;">
            <a href="https://github.com/WeichenFan/CFG-Zero-star">Code</a> |
            <a href="https://arxiv.org/abs/2503.18886">Paper</a>
        </div>
    """)

    with gr.Row():
        with gr.Column(scale=1):
            prompt = gr.Textbox(value="A spooky haunted mansion on a hill silhouetted by a full moon.", label="Enter your prompt")
            model_choice = gr.Dropdown(choices=list(model_paths.keys()), label="Choose Model")
            guidance_scale = gr.Slider(1, 20, value=4.0, step=0.5, label="Guidance Scale")
            inference_steps = gr.Slider(10, 100, value=50, step=5, label="Inference Steps")
            use_opt_scale = gr.Checkbox(value=True, label="Use Optimized-Scale")
            use_zero_init = gr.Checkbox(value=True, label="Use Zero Init")
            zero_steps = gr.Slider(0, 20, value=1, step=1, label="Zero out steps")
            seed = gr.Number(value=42, label="Seed (Leave blank for random)")
            compare_mode = gr.Checkbox(value=True, label="Compare Mode")
            generate_btn = gr.Button("Generate")

        with gr.Column(scale=2):
            out1 = gr.Image(type="pil", label="CFG-Zero* Image")
            out2 = gr.Image(type="pil", label="CFG Image")
            video = gr.Video(label="Video")
            used_seed = gr.Textbox(label="Used Seed")

    def update_params(model_name):
        print('model_name: ', model_name)
        if model_name == "wan-t2v":
            return (
                gr.update(value=5),
                gr.update(value=50),
                gr.update(value=True),
                gr.update(value=True),
                gr.update(value=1)
            )
        else:
            return (
                gr.update(value=4.0),
                gr.update(value=50),
                gr.update(value=True),
                gr.update(value=True),
                gr.update(value=1)
            )

    model_choice.change(
        fn=update_params,
        inputs=[model_choice],
        outputs=[guidance_scale, inference_steps, use_opt_scale, use_zero_init, zero_steps]
    )

    generate_btn.click(
        fn=generate_content,
        inputs=[
            prompt, model_choice, guidance_scale, inference_steps,
            use_opt_scale, use_zero_init, zero_steps, seed, compare_mode
        ],
        outputs=[out1, out2, video, used_seed]
    )

demo.launch(ssr_mode=False)