Spaces:
Runtime error
Runtime error
File size: 6,554 Bytes
1b76d08 26b5a39 a1cb6e4 26b5a39 0e189d9 26b5a39 1b76d08 26b5a39 9071e48 26b5a39 9071e48 26b5a39 9071e48 0e189d9 26b5a39 0e189d9 26b5a39 0e189d9 9071e48 26b5a39 | 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 | import random
import spaces
import torch
import gradio as gr
from diffusers import DiffusionPipeline
BASE_MODEL = "krea/Krea-2-Turbo"
LORA_REPO = "ostris/krea2_turbo_style_reference"
LORA_WEIGHT = "krea2_style_reference.safetensors"
CUSTOM_PIPELINE = "ostris/Krea2OstrisEdit"
DTYPE = torch.bfloat16
MAX_SEED = 2**31 - 1
pipe = DiffusionPipeline.from_pretrained(
BASE_MODEL,
custom_pipeline=CUSTOM_PIPELINE,
torch_dtype=DTYPE,
trust_remote_code=True,
)
pipe.to("cuda")
pipe.load_lora_weights(LORA_REPO, weight_name=LORA_WEIGHT)
@spaces.GPU(duration=90, size="large")
def generate(
prompt,
style_ref_image,
lora_scale=1.0,
steps=8,
guidance=0.0,
width=1024,
height=1024,
seed=0,
randomize_seed=True,
progress=gr.Progress(track_tqdm=True),
):
if not prompt or not prompt.strip():
raise gr.Error("Please enter a prompt.")
if style_ref_image is None:
raise gr.Error("Please upload a style reference image.")
if randomize_seed or seed is None:
seed = random.randint(0, MAX_SEED)
seed = int(seed)
# Defensive defaults: if a value ever arrives as None (e.g. an example
# row that omits an input), fall back to the same defaults used elsewhere.
if lora_scale is None:
lora_scale = 1.0
if steps is None:
steps = 8
if guidance is None:
guidance = 0.0
if width is None:
width = 1024
if height is None:
height = 1024
# Snap dimensions to multiples of 16 (vae_scale_factor * patch_size = 16)
multiple = 16
width = ((int(width) + multiple - 1) // multiple) * multiple
height = ((int(height) + multiple - 1) // multiple) * multiple
generator = torch.Generator("cuda").manual_seed(seed)
image = pipe(
prompt=prompt,
image=style_ref_image,
num_inference_steps=int(steps),
guidance_scale=float(guidance),
width=width,
height=height,
generator=generator,
attention_kwargs={"scale": float(lora_scale)},
).images[0]
return image, seed
CSS = """
#page { max-width: 1100px; margin: 0 auto; padding: 4px 8px 32px; }
#header { padding: 24px 4px 18px; border-bottom: 1px solid #e5e5e5; margin-bottom: 20px; }
#header h1 { font-size: 32px; font-weight: 700; margin: 0 0 6px; letter-spacing: -0.02em; }
#header .subtitle { font-size: 15px; color: #666; margin: 0; max-width: 70ch; line-height: 1.5; }
#header .links { margin-top: 12px; display: flex; gap: 16px; }
#header .links a {
font-size: 13px; color: #888; text-decoration: none;
border: 1px solid #ddd; border-radius: 6px; padding: 4px 10px;
}
#header .links a:hover { color: #333; border-color: #aaa; }
footer { display: none !important; }
"""
HEADER = """
<div id="header">
<h1>Krea 2 Style Reference</h1>
<p class="subtitle">
Generate an image from a text prompt, guided by a style reference image.
Powered by Krea 2 Turbo with the
<a href="https://huggingface.co/ostris/krea2_turbo_style_reference" target="_blank">Krea2 Style Reference LoRA</a>
and the
<a href="https://huggingface.co/ostris/Krea2OstrisEdit" target="_blank">Krea2OstrisEdit community pipeline</a>.
</p>
<div class="links">
<a href="https://huggingface.co/krea/Krea-2-Turbo" target="_blank">Base model ↗</a>
<a href="https://huggingface.co/ostris/krea2_turbo_style_reference" target="_blank">LoRA ↗</a>
<a href="https://github.com/ostris/ComfyUI-Krea2-Ostris-Edit" target="_blank">ComfyUI nodes ↗</a>
</div>
</div>
"""
with gr.Blocks(title="Krea 2 Style Reference") as demo:
with gr.Column(elem_id="page"):
gr.HTML(HEADER)
with gr.Row(equal_height=False):
with gr.Column(scale=1):
prompt = gr.Textbox(
label="Prompt",
lines=3,
placeholder="Describe what you want to generate, e.g. 'a white yeti with horns reading a book'",
)
style_ref = gr.Image(
label="Style Reference Image",
type="pil",
height=300,
)
generate_btn = gr.Button("Generate", variant="primary", size="lg")
with gr.Accordion("Advanced", open=False):
lora_scale = gr.Slider(
0.0, 2.0, value=1.0, step=0.01,
label="LoRA scale",
info="Strength of the style reference influence",
)
steps = gr.Slider(1, 30, value=8, step=1, label="Steps")
guidance = gr.Slider(
0.0, 10.0, value=0.0, step=0.1,
label="Guidance scale",
info="Krea 2 Turbo uses 0.0 (guidance disabled)",
)
with gr.Row():
width = gr.Slider(512, 1536, value=1024, step=16, label="Width")
height = gr.Slider(512, 1536, value=1024, step=16, label="Height")
with gr.Row():
seed = gr.Slider(0, MAX_SEED, value=0, step=1, label="Seed")
randomize_seed = gr.Checkbox(value=True, label="Randomize seed")
with gr.Column(scale=1):
result = gr.Image(label="Result", format="png", height=420)
used_seed = gr.Number(label="Seed used", visible=True, interactive=False)
inputs = [
prompt, style_ref, lora_scale, steps, guidance,
width, height, seed, randomize_seed,
]
outputs = [result, used_seed]
# Lean examples: only supply the two inputs a user actually varies
# (prompt + style reference image). generate() provides defaults for
# the remaining params, so the missing example columns fall back to
# sane values instead of None.
# Columns: prompt, style_ref
gr.Examples(
examples=[
["a white yeti with horns reading a book", "examples/style_ref_yeti.png"],
["a futuristic city skyline at sunset, cyberpunk aesthetic", "examples/style_ref_01.png"],
],
inputs=[prompt, style_ref],
outputs=outputs,
fn=generate,
cache_examples=True,
cache_mode="lazy",
)
gr.on([generate_btn.click, prompt.submit], generate, inputs, outputs)
if __name__ == "__main__":
demo.launch(theme=gr.themes.Citrus(), css=CSS) |