multimodalart HF Staff commited on
Commit
20ea87d
·
verified ·
1 Parent(s): 0cbe794

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. README.md +30 -6
  2. app.py +192 -0
  3. requirements.txt +5 -0
README.md CHANGED
@@ -1,13 +1,37 @@
1
  ---
2
- title: Stepx Opd
3
- emoji: 🏃
4
- colorFrom: red
5
- colorTo: purple
6
  sdk: gradio
7
  sdk_version: 6.31.0
8
- python_version: '3.12'
9
  app_file: app.py
10
  pinned: false
 
 
 
 
 
11
  ---
12
 
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: StepX-OPD — 4-Step Distilled SD 3.5
3
+ emoji: ⚡
4
+ colorFrom: green
5
+ colorTo: yellow
6
  sdk: gradio
7
  sdk_version: 6.31.0
 
8
  app_file: app.py
9
  pinned: false
10
+ short_description: 4-step text-to-image via on-policy distillation of SD 3.5
11
+ python_version: "3.12"
12
+ models:
13
+ - stabilityai/stable-diffusion-3.5-medium
14
+ - ShimaRinC/StepX-OPD-SD3.5
15
  ---
16
 
17
+ # ⚡ StepX-OPD
18
+
19
+ **StepX-OPD** rethinks multi-teacher on-policy distillation for diffusion models and distills
20
+ [Stable Diffusion 3.5 Medium](https://huggingface.co/stabilityai/stable-diffusion-3.5-medium) into a
21
+ **4-step** generator: a single student trained on-policy against three specialized teachers
22
+ (Geneval, OCR, aesthetic) produces full-quality images in seconds, without classifier-free
23
+ guidance.
24
+
25
+ This Space runs the official recipe from the
26
+ [model card](https://huggingface.co/ShimaRinC/StepX-OPD-SD3.5): SD3.5-Medium in bf16 with the
27
+ folded StepX-OPD inference LoRA, 4 steps, guidance 1.0, on the pre-shift sigma grid
28
+ `np.linspace(1.0, 1/4, 4)` at 512×512.
29
+
30
+ - Paper: [StepX-OPD: Rethinking Multi-Teacher On-Policy Distillation in Diffusion Models](https://huggingface.co/papers/2611.04386)
31
+ - Code: [WinterCCC/StepX-OPD](https://github.com/WinterCCC/StepX-OPD)
32
+ - Project page: [winterccc.github.io/Step-OPD](https://winterccc.github.io/Step-OPD/)
33
+ - Weights: [ShimaRinC/StepX-OPD-SD3.5](https://huggingface.co/ShimaRinC/StepX-OPD-SD3.5)
34
+
35
+ The base model is subject to the
36
+ [Stability AI Community License](https://huggingface.co/stabilityai/stable-diffusion-3.5-medium);
37
+ the StepX-OPD code is Apache-2.0.
app.py ADDED
@@ -0,0 +1,192 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import spaces # must be imported before torch / diffusers
2
+
3
+ import os
4
+ import random
5
+
6
+ import gradio as gr
7
+ import numpy as np
8
+ import torch
9
+ from diffusers import StableDiffusion3Pipeline
10
+
11
+ BASE_MODEL = "stabilityai/stable-diffusion-3.5-medium"
12
+ LORA_REPO = "ShimaRinC/StepX-OPD-SD3.5"
13
+
14
+ MAX_SEED = np.iinfo(np.int32).max
15
+ DEFAULT_STEPS = 4 # the distilled student is trained to be sampled in 4 steps
16
+ MAX_IMAGE_SIZE = 1024
17
+
18
+ # Official recipe from the StepX-OPD model card: SD3.5-Medium in bf16 with the
19
+ # folded StepX-OPD inference LoRA on top, sampled in 4 steps without CFG on the
20
+ # pre-shift sigma grid np.linspace(1.0, 1/N, N).
21
+ pipe = StableDiffusion3Pipeline.from_pretrained(
22
+ BASE_MODEL, torch_dtype=torch.bfloat16
23
+ ).to("cuda")
24
+ pipe.load_lora_weights(LORA_REPO)
25
+
26
+
27
+ @spaces.GPU(duration=65)
28
+ def generate(
29
+ prompt,
30
+ negative_prompt="",
31
+ seed=0,
32
+ randomize_seed=True,
33
+ width=512,
34
+ height=512,
35
+ guidance_scale=1.0,
36
+ num_inference_steps=DEFAULT_STEPS,
37
+ progress=gr.Progress(track_tqdm=True),
38
+ ):
39
+ """Generate an image from a text prompt with StepX-OPD, a 4-step distilled SD 3.5 Medium."""
40
+ if not prompt or not prompt.strip():
41
+ raise gr.Error("Please enter a prompt first.")
42
+
43
+ if randomize_seed:
44
+ seed = random.randint(0, MAX_SEED)
45
+ seed = int(seed)
46
+
47
+ generator = torch.Generator("cuda").manual_seed(seed)
48
+
49
+ num_inference_steps = int(num_inference_steps)
50
+ sigmas = np.linspace(1.0, 1.0 / num_inference_steps, num_inference_steps)
51
+
52
+ image = pipe(
53
+ prompt=prompt,
54
+ negative_prompt=negative_prompt,
55
+ guidance_scale=guidance_scale,
56
+ num_inference_steps=num_inference_steps,
57
+ sigmas=sigmas,
58
+ width=int(width),
59
+ height=int(height),
60
+ generator=generator,
61
+ ).images[0]
62
+ return image, seed
63
+
64
+
65
+ examples = [
66
+ ["a corgi wearing sunglasses, studio photo"],
67
+ ["a photo of two toilets"],
68
+ ["a photo of a dog right of a teddy bear"],
69
+ ["a photo of a red cake and a purple chair"],
70
+ [
71
+ "A vibrant science fair poster titled \"Volcano Experiment\", featuring "
72
+ "a detailed illustration of a volcanic eruption with lava flowing down "
73
+ "its slopes, surrounded by labeled diagrams and facts about volcanic "
74
+ "processes, all set against a bright, engaging background."
75
+ ],
76
+ ["cat knight in the style of darkest dungeon"],
77
+ ["realistic cartoon mouse with a birthday candle"],
78
+ ]
79
+
80
+ css = """
81
+ #col-container {
82
+ margin: 0 auto;
83
+ max-width: 900px;
84
+ }
85
+ .dark .gradio-container {
86
+ color: var(--body-text-color);
87
+ }
88
+ """
89
+
90
+ with gr.Blocks(theme=gr.themes.Citrus(), css=css) as demo:
91
+ with gr.Column(elem_id="col-container"):
92
+ gr.Markdown(
93
+ "# ⚡ StepX-OPD\n"
94
+ "**StepX-OPD** distills Stable Diffusion 3.5 Medium into a "
95
+ "**4-step** generator via multi-teacher on-policy distillation — "
96
+ "full-quality images in seconds, without classifier-free guidance.\n\n"
97
+ "[Paper](https://huggingface.co/papers/2611.04386) · "
98
+ "[Code](https://github.com/WinterCCC/StepX-OPD) · "
99
+ "[Project page](https://winterccc.github.io/Step-OPD/) · "
100
+ "[Weights](https://huggingface.co/ShimaRinC/StepX-OPD-SD3.5) · "
101
+ "[Base model](https://huggingface.co/stabilityai/stable-diffusion-3.5-medium)"
102
+ )
103
+ with gr.Row():
104
+ prompt = gr.Text(
105
+ label="Prompt",
106
+ show_label=False,
107
+ max_lines=1,
108
+ placeholder="Enter your prompt",
109
+ container=False,
110
+ scale=4,
111
+ )
112
+ run_button = gr.Button("Run", variant="primary", scale=1)
113
+
114
+ result = gr.Image(label="Result", show_label=False)
115
+
116
+ with gr.Accordion("Advanced settings", open=False):
117
+ negative_prompt = gr.Text(
118
+ label="Negative prompt",
119
+ max_lines=1,
120
+ placeholder="Only used when Guidance scale > 1.0",
121
+ info="The distilled student is trained to run at guidance 1.0 (no CFG); a negative prompt only takes effect above 1.0.",
122
+ )
123
+
124
+ seed = gr.Slider(
125
+ label="Seed",
126
+ minimum=0,
127
+ maximum=MAX_SEED,
128
+ step=1,
129
+ value=0,
130
+ )
131
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
132
+
133
+ with gr.Row():
134
+ width = gr.Slider(
135
+ label="Width",
136
+ minimum=384,
137
+ maximum=MAX_IMAGE_SIZE,
138
+ step=64,
139
+ value=512,
140
+ )
141
+ height = gr.Slider(
142
+ label="Height",
143
+ minimum=384,
144
+ maximum=MAX_IMAGE_SIZE,
145
+ step=64,
146
+ value=512,
147
+ )
148
+
149
+ guidance_scale = gr.Slider(
150
+ label="Guidance scale",
151
+ minimum=1.0,
152
+ maximum=7.5,
153
+ step=0.1,
154
+ value=1.0,
155
+ info="1.0 matches the training recipe of the distilled student.",
156
+ )
157
+ num_inference_steps = gr.Slider(
158
+ label="Number of inference steps",
159
+ minimum=1,
160
+ maximum=8,
161
+ step=1,
162
+ value=DEFAULT_STEPS,
163
+ info="4 steps is the distilled setting; the sigma grid adapts to the step count.",
164
+ )
165
+
166
+ gr.Examples(
167
+ examples=examples,
168
+ inputs=[prompt],
169
+ outputs=[result, seed],
170
+ fn=generate,
171
+ cache_examples=True,
172
+ cache_mode="lazy",
173
+ )
174
+
175
+ gr.on(
176
+ triggers=[run_button.click, prompt.submit],
177
+ fn=generate,
178
+ inputs=[
179
+ prompt,
180
+ negative_prompt,
181
+ seed,
182
+ randomize_seed,
183
+ width,
184
+ height,
185
+ guidance_scale,
186
+ num_inference_steps,
187
+ ],
188
+ outputs=[result, seed],
189
+ )
190
+
191
+ if __name__ == "__main__":
192
+ demo.launch(mcp_server=True)
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ diffusers==0.36.0
2
+ transformers==4.57.1
3
+ peft==0.17.1
4
+ accelerate
5
+ sentencepiece