Spaces:
Running on Zero
Running on Zero
Upload folder using huggingface_hub
Browse files- .gitattributes +3 -0
- README.md +37 -6
- app.py +184 -0
- examples/out_style_yeti_clean.png +3 -0
- examples/out_yeti_no_ref.png +3 -0
- examples/style_ref_clean.png +3 -0
- requirements.txt +7 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,6 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
examples/out_style_yeti_clean.png filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
examples/out_yeti_no_ref.png filter=lfs diff=lfs merge=lfs -text
|
| 38 |
+
examples/style_ref_clean.png filter=lfs diff=lfs merge=lfs -text
|
README.md
CHANGED
|
@@ -1,13 +1,44 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 6.20.0
|
| 8 |
-
python_version: '3.12'
|
| 9 |
app_file: app.py
|
| 10 |
pinned: false
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
---
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Krea 2 Ostris Edit
|
| 3 |
+
emoji: 🎨
|
| 4 |
+
colorFrom: pink
|
| 5 |
+
colorTo: green
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 6.20.0
|
|
|
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
+
hardware: zero-a10g
|
| 11 |
+
short_description: Reference-image conditioned generation with Krea 2
|
| 12 |
+
python_version: "3.10"
|
| 13 |
+
startup_duration_timeout: "30m"
|
| 14 |
+
models:
|
| 15 |
+
- krea/Krea-2-Turbo
|
| 16 |
+
- ostris/Krea2OstrisEdit
|
| 17 |
+
- ostris/krea2_turbo_style_reference
|
| 18 |
---
|
| 19 |
|
| 20 |
+
# Krea 2 Ostris Edit
|
| 21 |
+
|
| 22 |
+
Reference-image conditioned image generation with [Krea 2 Turbo](https://huggingface.co/krea/Krea-2-Turbo), powered by the [Krea2OstrisEdit](https://huggingface.co/ostris/Krea2OstrisEdit) community pipeline.
|
| 23 |
+
|
| 24 |
+
## How it works
|
| 25 |
+
|
| 26 |
+
Upload a reference image and type a prompt describing what you want to generate. The [Style Reference LoRA](https://huggingface.co/ostris/krea2_turbo_style_reference) conditions the Krea 2 Turbo model in two ways:
|
| 27 |
+
|
| 28 |
+
1. **Text encoder** — the reference image is embedded into the prompt via Qwen3-VL vision placeholders, so the text conditioning "sees" the reference
|
| 29 |
+
2. **Latent space** — clean VAE latents of the reference are appended to the transformer sequence at flow time t=0 (Kontext-style "index" placement)
|
| 30 |
+
|
| 31 |
+
Toggle the "Use reference image" checkbox to switch between reference-conditioned generation and plain text-to-image mode.
|
| 32 |
+
|
| 33 |
+
## Components
|
| 34 |
+
|
| 35 |
+
- **Base model:** [krea/Krea-2-Turbo](https://huggingface.co/krea/Krea-2-Turbo) (8-step distilled Krea 2)
|
| 36 |
+
- **Custom pipeline:** [ostris/Krea2OstrisEdit](https://huggingface.co/ostris/Krea2OstrisEdit) (reference-image conditioning + AI-Toolkit LoRA loading)
|
| 37 |
+
- **LoRA:** [ostris/krea2_turbo_style_reference](https://huggingface.co/ostris/krea2_turbo_style_reference) (style reference conditioning)
|
| 38 |
+
|
| 39 |
+
## Inference recipe
|
| 40 |
+
|
| 41 |
+
- **Steps:** 8 (Turbo distilled)
|
| 42 |
+
- **Guidance:** 0.0 (disabled, per Krea 2 Turbo convention)
|
| 43 |
+
- **LoRA scale:** 1.0 (adjustable)
|
| 44 |
+
- **Resolution:** 1024x1024 (adjustable, must be multiples of 16)
|
app.py
ADDED
|
@@ -0,0 +1,184 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import random
|
| 2 |
+
|
| 3 |
+
import spaces
|
| 4 |
+
import torch
|
| 5 |
+
import gradio as gr
|
| 6 |
+
from diffusers import DiffusionPipeline
|
| 7 |
+
|
| 8 |
+
BASE_MODEL = "krea/Krea-2-Turbo"
|
| 9 |
+
CUSTOM_PIPELINE = "ostris/Krea2OstrisEdit"
|
| 10 |
+
LORA_REPO = "ostris/krea2_turbo_style_reference"
|
| 11 |
+
LORA_WEIGHT = "krea2_style_reference.safetensors"
|
| 12 |
+
|
| 13 |
+
DTYPE = torch.bfloat16
|
| 14 |
+
MAX_SEED = 2**31 - 1
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
pipe = DiffusionPipeline.from_pretrained(
|
| 18 |
+
BASE_MODEL,
|
| 19 |
+
custom_pipeline=CUSTOM_PIPELINE,
|
| 20 |
+
torch_dtype=DTYPE,
|
| 21 |
+
trust_remote_code=True,
|
| 22 |
+
)
|
| 23 |
+
pipe.to("cuda")
|
| 24 |
+
pipe.load_lora_weights(LORA_REPO, weight_name=LORA_WEIGHT)
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
@spaces.GPU(duration=90, size="large")
|
| 28 |
+
def generate(
|
| 29 |
+
prompt,
|
| 30 |
+
reference_image,
|
| 31 |
+
use_reference,
|
| 32 |
+
lora_scale=1.0,
|
| 33 |
+
steps=8,
|
| 34 |
+
guidance=0.0,
|
| 35 |
+
width=1024,
|
| 36 |
+
height=1024,
|
| 37 |
+
seed=0,
|
| 38 |
+
randomize_seed=True,
|
| 39 |
+
progress=gr.Progress(track_tqdm=True),
|
| 40 |
+
):
|
| 41 |
+
if not prompt or not prompt.strip():
|
| 42 |
+
raise gr.Error("Please enter a prompt.")
|
| 43 |
+
if use_reference and reference_image is None:
|
| 44 |
+
raise gr.Error("Please upload a reference image or disable the reference option.")
|
| 45 |
+
|
| 46 |
+
if randomize_seed or seed is None:
|
| 47 |
+
seed = random.randint(0, MAX_SEED)
|
| 48 |
+
seed = int(seed)
|
| 49 |
+
|
| 50 |
+
if lora_scale is None:
|
| 51 |
+
lora_scale = 1.0
|
| 52 |
+
if steps is None:
|
| 53 |
+
steps = 8
|
| 54 |
+
if guidance is None:
|
| 55 |
+
guidance = 0.0
|
| 56 |
+
if width is None:
|
| 57 |
+
width = 1024
|
| 58 |
+
if height is None:
|
| 59 |
+
height = 1024
|
| 60 |
+
|
| 61 |
+
# Snap dimensions to multiples of 16 (vae_scale_factor * patch_size = 16)
|
| 62 |
+
multiple = 16
|
| 63 |
+
width = ((int(width) + multiple - 1) // multiple) * multiple
|
| 64 |
+
height = ((int(height) + multiple - 1) // multiple) * multiple
|
| 65 |
+
|
| 66 |
+
generator = torch.Generator("cuda").manual_seed(seed)
|
| 67 |
+
|
| 68 |
+
pipe_kwargs = dict(
|
| 69 |
+
prompt=prompt,
|
| 70 |
+
num_inference_steps=int(steps),
|
| 71 |
+
guidance_scale=float(guidance),
|
| 72 |
+
width=width,
|
| 73 |
+
height=height,
|
| 74 |
+
generator=generator,
|
| 75 |
+
attention_kwargs={"scale": float(lora_scale)},
|
| 76 |
+
)
|
| 77 |
+
if use_reference and reference_image is not None:
|
| 78 |
+
pipe_kwargs["image"] = reference_image
|
| 79 |
+
|
| 80 |
+
image = pipe(**pipe_kwargs).images[0]
|
| 81 |
+
return image, seed
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
CSS = """
|
| 85 |
+
#page { max-width: 1100px; margin: 0 auto; padding: 4px 8px 32px; }
|
| 86 |
+
#header { padding: 24px 4px 18px; border-bottom: 1px solid #e5e5e5; margin-bottom: 20px; }
|
| 87 |
+
#header h1 { font-size: 32px; font-weight: 700; margin: 0 0 6px; letter-spacing: -0.02em; }
|
| 88 |
+
#header .subtitle { font-size: 15px; color: #666; margin: 0; max-width: 70ch; line-height: 1.5; }
|
| 89 |
+
#header .links { margin-top: 12px; display: flex; gap: 16px; }
|
| 90 |
+
#header .links a {
|
| 91 |
+
font-size: 13px; color: #888; text-decoration: none;
|
| 92 |
+
border: 1px solid #ddd; border-radius: 6px; padding: 4px 10px;
|
| 93 |
+
}
|
| 94 |
+
#header .links a:hover { color: #333; border-color: #aaa; }
|
| 95 |
+
footer { display: none !important; }
|
| 96 |
+
"""
|
| 97 |
+
|
| 98 |
+
HEADER = """
|
| 99 |
+
<div id="header">
|
| 100 |
+
<h1>Krea 2 Ostris Edit</h1>
|
| 101 |
+
<p class="subtitle">
|
| 102 |
+
Reference-image conditioned image generation with
|
| 103 |
+
<a href="https://huggingface.co/krea/Krea-2-Turbo" target="_blank">Krea 2 Turbo</a>
|
| 104 |
+
via the
|
| 105 |
+
<a href="https://huggingface.co/ostris/Krea2OstrisEdit" target="_blank">Krea2OstrisEdit</a>
|
| 106 |
+
community pipeline. Upload a reference image and describe what you want — the model
|
| 107 |
+
uses the reference for style, subject, or edit context. Powered by the
|
| 108 |
+
<a href="https://huggingface.co/ostris/krea2_turbo_style_reference" target="_blank">Style Reference LoRA</a>.
|
| 109 |
+
</p>
|
| 110 |
+
<div class="links">
|
| 111 |
+
<a href="https://huggingface.co/ostris/Krea2OstrisEdit" target="_blank">Pipeline ↗</a>
|
| 112 |
+
<a href="https://huggingface.co/krea/Krea-2-Turbo" target="_blank">Base model ↗</a>
|
| 113 |
+
<a href="https://huggingface.co/ostris/krea2_turbo_style_reference" target="_blank">LoRA ↗</a>
|
| 114 |
+
</div>
|
| 115 |
+
</div>
|
| 116 |
+
"""
|
| 117 |
+
|
| 118 |
+
with gr.Blocks(title="Krea 2 Ostris Edit") as demo:
|
| 119 |
+
with gr.Column(elem_id="page"):
|
| 120 |
+
gr.HTML(HEADER)
|
| 121 |
+
with gr.Row(equal_height=False):
|
| 122 |
+
with gr.Column(scale=1):
|
| 123 |
+
prompt = gr.Textbox(
|
| 124 |
+
label="Prompt",
|
| 125 |
+
lines=3,
|
| 126 |
+
placeholder="Describe what you want to generate, e.g. 'a white yeti with horns reading a book'",
|
| 127 |
+
)
|
| 128 |
+
use_reference = gr.Checkbox(
|
| 129 |
+
value=True,
|
| 130 |
+
label="Use reference image",
|
| 131 |
+
info="Toggle to switch between reference-conditioned and plain text-to-image mode",
|
| 132 |
+
)
|
| 133 |
+
reference_image = gr.Image(
|
| 134 |
+
label="Reference Image",
|
| 135 |
+
type="pil",
|
| 136 |
+
height=300,
|
| 137 |
+
)
|
| 138 |
+
generate_btn = gr.Button("Generate", variant="primary", size="lg")
|
| 139 |
+
with gr.Accordion("Advanced", open=False):
|
| 140 |
+
lora_scale = gr.Slider(
|
| 141 |
+
0.0, 2.0, value=1.0, step=0.01,
|
| 142 |
+
label="LoRA scale",
|
| 143 |
+
info="Strength of the reference image influence",
|
| 144 |
+
)
|
| 145 |
+
steps = gr.Slider(1, 30, value=8, step=1, label="Steps")
|
| 146 |
+
guidance = gr.Slider(
|
| 147 |
+
0.0, 10.0, value=0.0, step=0.1,
|
| 148 |
+
label="Guidance scale",
|
| 149 |
+
info="Krea 2 Turbo uses 0.0 (guidance disabled)",
|
| 150 |
+
)
|
| 151 |
+
with gr.Row():
|
| 152 |
+
width = gr.Slider(512, 1536, value=1024, step=16, label="Width")
|
| 153 |
+
height = gr.Slider(512, 1536, value=1024, step=16, label="Height")
|
| 154 |
+
with gr.Row():
|
| 155 |
+
seed = gr.Slider(0, MAX_SEED, value=0, step=1, label="Seed")
|
| 156 |
+
randomize_seed = gr.Checkbox(value=True, label="Randomize seed")
|
| 157 |
+
with gr.Column(scale=1):
|
| 158 |
+
result = gr.Image(label="Result", format="png", height=420)
|
| 159 |
+
used_seed = gr.Number(label="Seed used", visible=True, interactive=False)
|
| 160 |
+
|
| 161 |
+
inputs = [
|
| 162 |
+
prompt, reference_image, use_reference,
|
| 163 |
+
lora_scale, steps, guidance,
|
| 164 |
+
width, height, seed, randomize_seed,
|
| 165 |
+
]
|
| 166 |
+
outputs = [result, used_seed]
|
| 167 |
+
|
| 168 |
+
gr.Examples(
|
| 169 |
+
examples=[
|
| 170 |
+
["a white yeti with horns reading a book", "examples/style_ref_clean.png", True],
|
| 171 |
+
["a futuristic city skyline at sunset, cyberpunk aesthetic", "examples/style_ref_clean.png", True],
|
| 172 |
+
["a white yeti with horns reading a book", None, False],
|
| 173 |
+
],
|
| 174 |
+
inputs=[prompt, reference_image, use_reference],
|
| 175 |
+
outputs=outputs,
|
| 176 |
+
fn=generate,
|
| 177 |
+
cache_examples=True,
|
| 178 |
+
cache_mode="lazy",
|
| 179 |
+
)
|
| 180 |
+
|
| 181 |
+
gr.on([generate_btn.click, prompt.submit], generate, inputs, outputs)
|
| 182 |
+
|
| 183 |
+
if __name__ == "__main__":
|
| 184 |
+
demo.launch(theme=gr.themes.Citrus(), css=CSS)
|
examples/out_style_yeti_clean.png
ADDED
|
Git LFS Details
|
examples/out_yeti_no_ref.png
ADDED
|
Git LFS Details
|
examples/style_ref_clean.png
ADDED
|
Git LFS Details
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
diffusers
|
| 2 |
+
transformers
|
| 3 |
+
accelerate
|
| 4 |
+
peft
|
| 5 |
+
safetensors
|
| 6 |
+
torchvision
|
| 7 |
+
sentencepiece
|