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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import random
|
| 4 |
+
import spaces
|
| 5 |
+
import torch
|
| 6 |
+
from diffusers import SanaSprintPipeline
|
| 7 |
+
import peft
|
| 8 |
+
from peft.tuners.lora.layer import Linear as LoraLinear
|
| 9 |
+
import types
|
| 10 |
+
|
| 11 |
+
dtype = torch.bfloat16
|
| 12 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 13 |
+
adapter_name = "hypernoise_adapter"
|
| 14 |
+
|
| 15 |
+
# Load the pipeline and adapter
|
| 16 |
+
pipe = SanaSprintPipeline.from_pretrained(
|
| 17 |
+
"Efficient-Large-Model/Sana_Sprint_0.6B_1024px_diffusers",
|
| 18 |
+
torch_dtype=dtype,
|
| 19 |
+
).to(device, dtype)
|
| 20 |
+
|
| 21 |
+
pipe.transformer = peft.PeftModel.from_pretrained(
|
| 22 |
+
pipe.transformer,
|
| 23 |
+
"lucaeyring/HyperNoise_Sana_Sprint_0.6B",
|
| 24 |
+
adapter_name=adapter_name,
|
| 25 |
+
dtype=dtype,
|
| 26 |
+
).to(device, dtype)
|
| 27 |
+
|
| 28 |
+
# Define the custom forward function for LoRA
|
| 29 |
+
def scaled_base_lora_forward(self, x, *args, **kwargs):
|
| 30 |
+
if self.disable_adapters:
|
| 31 |
+
return self.base_layer(x, *args, **kwargs)
|
| 32 |
+
return self.lora_B[adapter_name](self.lora_A[adapter_name](x)) * self.scaling[adapter_name]
|
| 33 |
+
|
| 34 |
+
# Apply the custom forward to proj_out module
|
| 35 |
+
for name, module in pipe.transformer.base_model.model.named_modules():
|
| 36 |
+
if name == "proj_out" and isinstance(module, LoraLinear):
|
| 37 |
+
module.forward = types.MethodType(scaled_base_lora_forward, module)
|
| 38 |
+
break
|
| 39 |
+
|
| 40 |
+
MAX_SEED = np.iinfo(np.int32).max
|
| 41 |
+
MAX_IMAGE_SIZE = 1024 # Sana Sprint is optimized for 1024px
|
| 42 |
+
|
| 43 |
+
@spaces.GPU()
|
| 44 |
+
def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024,
|
| 45 |
+
num_inference_steps=4, guidance_scale=4.5, progress=gr.Progress(track_tqdm=True)):
|
| 46 |
+
|
| 47 |
+
if randomize_seed:
|
| 48 |
+
seed = random.randint(0, MAX_SEED)
|
| 49 |
+
|
| 50 |
+
# Set random seed for reproducibility
|
| 51 |
+
torch.manual_seed(seed)
|
| 52 |
+
torch.cuda.manual_seed_all(seed)
|
| 53 |
+
|
| 54 |
+
with torch.inference_mode():
|
| 55 |
+
# Encode the prompt
|
| 56 |
+
prompt_embeds, prompt_attention_mask = pipe.encode_prompt(
|
| 57 |
+
[prompt],
|
| 58 |
+
device=device
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
# Generate initial random latents
|
| 62 |
+
init_latents = torch.randn(
|
| 63 |
+
[1, 32, 32, 32],
|
| 64 |
+
device=device,
|
| 65 |
+
dtype=dtype
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
# Apply HyperNoise modulation with adapter enabled
|
| 69 |
+
pipe.transformer.enable_adapter_layers()
|
| 70 |
+
modulated_latents = pipe.transformer(
|
| 71 |
+
hidden_states=init_latents,
|
| 72 |
+
encoder_hidden_states=prompt_embeds,
|
| 73 |
+
encoder_attention_mask=prompt_attention_mask,
|
| 74 |
+
guidance=torch.tensor([guidance_scale], device=device, dtype=dtype) * 0.1,
|
| 75 |
+
timestep=torch.tensor([1.0], device=device, dtype=dtype),
|
| 76 |
+
).sample + init_latents
|
| 77 |
+
|
| 78 |
+
# Generate final image with adapter disabled
|
| 79 |
+
pipe.transformer.disable_adapter_layers()
|
| 80 |
+
image = pipe(
|
| 81 |
+
latents=modulated_latents,
|
| 82 |
+
prompt_embeds=prompt_embeds,
|
| 83 |
+
prompt_attention_mask=prompt_attention_mask,
|
| 84 |
+
num_inference_steps=num_inference_steps,
|
| 85 |
+
height=height,
|
| 86 |
+
width=width,
|
| 87 |
+
).images[0]
|
| 88 |
+
|
| 89 |
+
return image, seed
|
| 90 |
+
|
| 91 |
+
examples = [
|
| 92 |
+
"A smiling slice of pizza doing yoga on a mountain top",
|
| 93 |
+
"A fluffy cat wearing a wizard hat casting spells",
|
| 94 |
+
"A robot painting a self-portrait in Van Gogh style",
|
| 95 |
+
"A tiny dragon sleeping in a teacup",
|
| 96 |
+
"An astronaut riding a unicorn through a rainbow",
|
| 97 |
+
]
|
| 98 |
+
|
| 99 |
+
css = """
|
| 100 |
+
#col-container {
|
| 101 |
+
margin: 0 auto;
|
| 102 |
+
max-width: 520px;
|
| 103 |
+
}
|
| 104 |
+
"""
|
| 105 |
+
|
| 106 |
+
with gr.Blocks(css=css) as demo:
|
| 107 |
+
|
| 108 |
+
with gr.Column(elem_id="col-container"):
|
| 109 |
+
gr.Markdown("""# HyperNoise Sana Sprint 0.6B
|
| 110 |
+
|
| 111 |
+
Fast text-to-image generation with HyperNoise adapter for Sana Sprint model.
|
| 112 |
+
|
| 113 |
+
[[Sana Sprint Model](https://huggingface.co/Efficient-Large-Model/Sana_Sprint_0.6B_1024px_diffusers)]
|
| 114 |
+
[[HyperNoise Adapter](https://huggingface.co/lucaeyring/HyperNoise_Sana_Sprint_0.6B)]
|
| 115 |
+
""")
|
| 116 |
+
|
| 117 |
+
with gr.Row():
|
| 118 |
+
prompt = gr.Text(
|
| 119 |
+
label="Prompt",
|
| 120 |
+
show_label=False,
|
| 121 |
+
max_lines=1,
|
| 122 |
+
placeholder="Enter your prompt",
|
| 123 |
+
container=False,
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
run_button = gr.Button("Run", scale=0)
|
| 127 |
+
|
| 128 |
+
result = gr.Image(label="Result", show_label=False)
|
| 129 |
+
|
| 130 |
+
with gr.Accordion("Advanced Settings", open=False):
|
| 131 |
+
|
| 132 |
+
seed = gr.Slider(
|
| 133 |
+
label="Seed",
|
| 134 |
+
minimum=0,
|
| 135 |
+
maximum=MAX_SEED,
|
| 136 |
+
step=1,
|
| 137 |
+
value=42,
|
| 138 |
+
)
|
| 139 |
+
|
| 140 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 141 |
+
|
| 142 |
+
with gr.Row():
|
| 143 |
+
width = gr.Slider(
|
| 144 |
+
label="Width",
|
| 145 |
+
minimum=256,
|
| 146 |
+
maximum=MAX_IMAGE_SIZE,
|
| 147 |
+
step=64,
|
| 148 |
+
value=1024,
|
| 149 |
+
)
|
| 150 |
+
|
| 151 |
+
height = gr.Slider(
|
| 152 |
+
label="Height",
|
| 153 |
+
minimum=256,
|
| 154 |
+
maximum=MAX_IMAGE_SIZE,
|
| 155 |
+
step=64,
|
| 156 |
+
value=1024,
|
| 157 |
+
)
|
| 158 |
+
|
| 159 |
+
with gr.Row():
|
| 160 |
+
num_inference_steps = gr.Slider(
|
| 161 |
+
label="Number of inference steps",
|
| 162 |
+
minimum=1,
|
| 163 |
+
maximum=20,
|
| 164 |
+
step=1,
|
| 165 |
+
value=4,
|
| 166 |
+
)
|
| 167 |
+
|
| 168 |
+
guidance_scale = gr.Slider(
|
| 169 |
+
label="Guidance Scale",
|
| 170 |
+
minimum=1.0,
|
| 171 |
+
maximum=10.0,
|
| 172 |
+
step=0.5,
|
| 173 |
+
value=4.5,
|
| 174 |
+
)
|
| 175 |
+
|
| 176 |
+
gr.Examples(
|
| 177 |
+
examples=examples,
|
| 178 |
+
fn=infer,
|
| 179 |
+
inputs=[prompt],
|
| 180 |
+
outputs=[result, seed],
|
| 181 |
+
cache_examples="lazy"
|
| 182 |
+
)
|
| 183 |
+
|
| 184 |
+
gr.on(
|
| 185 |
+
triggers=[run_button.click, prompt.submit],
|
| 186 |
+
fn=infer,
|
| 187 |
+
inputs=[prompt, seed, randomize_seed, width, height, num_inference_steps, guidance_scale],
|
| 188 |
+
outputs=[result, seed]
|
| 189 |
+
)
|
| 190 |
+
|
| 191 |
+
demo.launch()
|