File size: 23,439 Bytes
82e465e
 
77db6d1
 
 
 
 
 
 
 
 
82e465e
 
 
 
d18d66f
 
82e465e
 
77db6d1
82e465e
 
 
 
d18d66f
82e465e
 
 
 
 
 
 
 
b549625
b72c5f8
 
 
b549625
77db6d1
9a7b9fd
8bce267
77db6d1
 
 
 
 
 
 
f2873a1
77db6d1
8bce267
 
f2873a1
afc496b
 
 
 
08c562a
 
 
 
afc496b
08c562a
 
 
77db6d1
 
 
 
 
 
 
82e465e
 
 
 
 
b4a69ae
 
82e465e
 
 
 
77db6d1
 
 
 
 
 
 
 
 
 
b549625
 
8c72820
b549625
 
77db6d1
b549625
77db6d1
 
 
 
 
 
 
 
 
d18d66f
 
82e465e
 
 
 
d18d66f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77db6d1
 
82e465e
f2873a1
 
82e465e
f2873a1
82e465e
ddac770
f2873a1
c683542
 
82e465e
 
 
77db6d1
 
 
 
 
 
 
 
 
d18d66f
 
 
 
 
 
77db6d1
82e465e
 
77db6d1
82e465e
 
d18d66f
 
 
 
 
82e465e
77db6d1
d18d66f
82e465e
 
 
d18d66f
 
77db6d1
 
 
 
 
d18d66f
 
77db6d1
 
afc496b
 
 
77db6d1
afc496b
 
77db6d1
 
c683542
 
 
 
 
 
 
 
 
 
 
 
 
d18d66f
 
77db6d1
82e465e
 
 
f556f2d
77db6d1
 
f556f2d
c683542
82e465e
 
 
 
 
 
c683542
 
82e465e
 
 
 
c683542
f556f2d
82e465e
 
 
 
 
 
 
f556f2d
 
d365baa
82e465e
 
 
 
 
 
 
 
 
 
77db6d1
 
82e465e
 
 
 
43b7558
 
 
 
 
 
 
 
 
 
82e465e
 
 
 
 
 
 
 
 
c09735c
77db6d1
 
f2873a1
77db6d1
 
 
 
 
 
 
658eb4c
f968f2d
8bce267
 
77db6d1
 
82e465e
f2873a1
82e465e
 
77db6d1
 
82e465e
77db6d1
b549625
77db6d1
82e465e
 
cd1923f
b4a69ae
 
 
 
 
 
 
70e4aac
 
b4a69ae
 
cd1923f
 
 
 
 
 
 
 
 
 
8c72820
cd1923f
 
 
 
08c562a
cd1923f
08c562a
 
77db6d1
08c562a
 
 
 
c683542
 
cd1923f
 
 
77db6d1
 
 
82e465e
77db6d1
 
 
 
82e465e
77db6d1
 
 
82e465e
77db6d1
 
82e465e
77db6d1
 
 
 
 
 
82e465e
b4a69ae
 
 
 
 
77db6d1
 
 
 
 
 
 
 
 
 
 
 
 
f556f2d
 
 
 
 
 
77db6d1
f556f2d
 
 
 
 
 
77db6d1
82e465e
77db6d1
b4a69ae
82e465e
 
 
43b7558
77db6d1
 
 
82e465e
 
 
 
 
 
 
 
 
 
77db6d1
82e465e
 
 
c683542
77db6d1
 
 
 
 
 
afc496b
77db6d1
82e465e
 
 
 
 
f556f2d
82e465e
 
f556f2d
c683542
82e465e
 
43b7558
 
 
 
82e465e
 
 
 
9a7b9fd
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
"""Krea 2 LoRA Trainer β€” HF Space (HF Jobs backend).

Sign in with Hugging Face, pick what you're training (a style, or an object/character), upload a
handful of images, let the built-in AI caption them, and submit a DreamBooth-LoRA job to HF Jobs.
The job trains on **Krea 2 RAW**, (optionally) validates on **Krea 2 Turbo**, and pushes the LoRA
to your Hub β€” all under your account. The Space runs on `cpu-basic`; the GPU work runs on HF Jobs.

Three tokens, three jobs:
  * the **user's** OAuth token β€” dataset + the pushed LoRA (their account / billing);
  * `KREA_TOKEN` secret β€” downloads the gated Krea 2 weights inside the job only;
  * `CAPTION_HF_TOKEN` secret β€” calls the Inference API for AI captioning on this Space only.
"""

from __future__ import annotations

import os

import gradio as gr

import caption
import jobs

MAX_IMAGES = 40
MAX_LOG = 60_000
IMAGE_EXTS = (".png", ".jpg", ".jpeg", ".webp", ".bmp", ".gif", ".tiff", ".tif")

LR_SCHEDULERS = ["constant", "cosine", "linear", "constant_with_warmup", "polynomial"]
OPTIMIZERS = ["adamW", "prodigy"]
QUANT_CHOICES = [
    ("None β€” bf16 (best quality, most VRAM)", "none"),
    ("FP8 (faster compute, needs GPU β‰₯ 8.9)", "fp8"),
    ("4-bit NF4 / QLoRA (lowest VRAM)", "4bit"),
]
CONCEPT_CHOICES = [
    ("🎨 Style", "style"),
    ("🧸 Object or character", "object"),
    ("βš™οΈ Custom", "custom"),
]

AGENT_URL = "https://huggingface.co/spaces/multimodalart/krea2-lora-trainer/raw/main/agent.md"

CSS = """
#main_title{text-align:center}
#main_title h1{font-size:2.25rem;margin-bottom:0}
#main_title h3{margin-top:.25em;font-size:1.25em}
#main_title p{margin-top:.25em;font-size:1.05em;opacity:.85}
.accordion{color:var(--body-text-color)}
.login_logout{width:100% !important}
#login{width:100% !important;margin:0 auto}
#signin_banner{text-align:center}
.agent_hint{text-align:center;opacity:.7;font-size:.9em;margin:.4em 0 -.2em}
.agent_curl{max-width:380px;margin:0 auto}
#main_ui.locked{pointer-events:none;opacity:.55;filter:grayscale(.3)}

/* captioning panel */
#captioning_area{padding:0 4px 4px 16px}
#captioning_area h2{margin:0 0 8px}
.trigger_row{gap:8px;align-items:stretch}
.suggest_btn button{width:100%;height:100%;border:var(--button-border-width) solid var(--button-secondary-border-color);background:var(--button-secondary-background-fill)}
.cap_item{border:1px solid var(--border-color-primary);border-radius:10px;padding:10px;margin-bottom:12px;gap:8px}
.cap_img{flex:0 0 auto !important;max-width:180px}
.cap_img .icon-button-wrapper,.cap_img .source-selection,.cap_img .image-frame .controls{display:none !important}
.cap_img img,.cap_img .image-frame{object-fit:cover;border-radius:8px;max-height:180px}
.cap_row{align-items:stretch !important;gap:10px}
.cap_one_btn button{height:100%;min-width:46px;padding-left:0;padding-right:0}
"""

THEME = gr.themes.Monochrome(
    text_size=gr.themes.Size(lg="18px", md="15px", sm="13px", xl="22px", xs="12px",
                             xxl="24px", xxs="9px"),
    font=[gr.themes.GoogleFont("Source Sans Pro"), "ui-sans-serif", "system-ui", "sans-serif"],
)

FLAVOR_GUIDE = """**Which GPU?** You're billed per-minute of actual runtime. Krea 2 is a 12B DiT.

| Flavor | VRAM | Best for |
|---|---|---|
| `rtx-pro-6000` | 96 GB | **recommended (default)** β€” newest Blackwell card, bf16 with room to spare |
| `a100-large` | 80 GB | proven A100 β€” bf16 with offload + cached latents |

First run is slow to start: it downloads the gated Krea 2 RAW + Turbo weights before training.
"""

TRIGGER_HELP = {
    "style": (
        "Your **trigger** is a short descriptive phrase appended to every caption β€” e.g. "
        "*heavy impasto style*, *monochrome ink wash style*. Click **✨ Suggest** to get one "
        "from your images."
    ),
    "object": (
        "Your **trigger** is a rare, unique token for the subject β€” e.g. *b3@rcup*, *sks dog*. "
        "Click **✨ Suggest** to get one from your images."
    ),
    "custom": (
        "Advanced: caption however you like. The **trigger** is whatever token or phrase you "
        "choose; **✨ Add captions** writes a literal description and appends your trigger, "
        "and blank captions fall back to it."
    ),
}
TRIGGER_PLACEHOLDER = {"style": "heavy impasto style", "object": "b3@rcup", "custom": "TOK"}

CAPTION_TIP = (
    "<details><summary>ℹ️ <b>How captioning works</b></summary>\n\n"
    "- For a <b>style</b>, captions describe only the <i>content</i> (subjects, layout, setting) "
    "and end with your style trigger β€” the model then learns the look, not the subjects.\n"
    "- For an <b>object / character</b>, captions describe the scene and tag the subject with your "
    "trigger token.\n"
    "- <b>✨ Auto-caption</b> fills these for you; edit anything you like. Blank captions fall "
    "back to the trigger.\n"
    "- Already have captions? Upload a <code>.txt</code> next to each image with the same name "
    "(e.g. <code>cat.jpg</code> + <code>cat.txt</code>) and they're filled in automatically.\n"
    "</details>"
)


def _paths(files) -> list[str]:
    return [p if isinstance(p, str) else getattr(p, "name", p) for p in (files or [])]


def _split_uploads(files):
    """Split a mixed upload into image paths and a {basename: caption} map from .txt sidecars.

    A .txt file whose name matches an image (e.g. cat.jpg + cat.txt) prefills that caption.
    """
    images, captions = [], {}
    for p in _paths(files):
        ext = os.path.splitext(p)[1].lower()
        base = os.path.splitext(os.path.basename(p))[0]
        if ext == ".txt":
            try:
                with open(p, encoding="utf-8") as f:
                    captions[base] = f.read().strip()
            except Exception:  # noqa: BLE001
                pass
        elif ext in IMAGE_EXTS:
            images.append(p)
    return images, captions


def _signin_state(profile: gr.OAuthProfile | None):
    # The form stays visible but locked (pointer-events:none) until sign-in: OAuth is a full-page
    # redirect, so anything typed before logging in would be wiped on the way back.
    if profile is None:
        return ("", gr.update(elem_classes=["locked"]), gr.update())
    return (
        "",
        gr.update(elem_classes=[]),
        gr.update(info=f"Becomes your model repo `{profile.username}/<name>` (+ a dataset repo). "
                       "Spaces and symbols are turned into dashes automatically."),
    )


def on_concept_change(concept: str):
    # picking a concept reveals the upload area (lora-ease "part by part" reveal)
    return (
        gr.update(visible=True),
        gr.update(placeholder=TRIGGER_PLACEHOLDER.get(concept, "")),
        TRIGGER_HELP.get(concept, ""),
    )


def load_captioning(files, trigger):
    """Reveal the captioning + training UI once images are uploaded; one row per image.

    If a matching `<name>.txt` sidecar was uploaded, its content prefills that image's caption.
    """
    paths, txt_caps = _split_uploads(files)
    n = len(paths)
    if n > MAX_IMAGES:
        raise gr.Error(f"For now, up to {MAX_IMAGES} images are supported (got {n}).")
    updates = [gr.update(visible=n > 0), gr.update(visible=n > 0)]  # captioning_area, post_upload
    for i in range(MAX_IMAGES):
        visible = i < n
        if visible:
            base = os.path.splitext(os.path.basename(paths[i]))[0]
            cap_val = txt_caps.get(base) or (trigger or "")
        else:
            cap_val = None
        updates.append(gr.update(visible=visible))  # row
        updates.append(gr.update(value=paths[i] if visible else None, visible=visible))  # image
        updates.append(gr.update(value=cap_val, visible=visible))  # caption
    return updates


def ai_suggest_trigger(files, concept_type):
    paths, _ = _split_uploads(files)
    if not paths:
        raise gr.Error("Upload images first.")
    return caption.suggest_trigger(paths, concept_type)


def ai_caption_all(files, concept_type, trigger):
    paths, _ = _split_uploads(files)
    if not paths:
        raise gr.Error("Upload images first.")
    trigger = (trigger or "").strip()
    if not trigger:
        trigger = caption.suggest_trigger(paths, concept_type)  # suggest one and reuse it everywhere
    caps = [caption.caption_one(p, concept_type, trigger) for p in paths]
    cap_updates = [gr.update(value=caps[i]) if i < len(caps) else gr.update() for i in range(MAX_IMAGES)]
    return [gr.update(value=trigger), *cap_updates]


def make_caption_one(idx):
    """Build a handler that captions only image `idx` (seeding the trigger if it's empty)."""
    def _caption_one(files, concept_type, trigger):
        paths, _ = _split_uploads(files)
        if idx >= len(paths):
            return gr.update(), gr.update()
        trigger = (trigger or "").strip()
        if not trigger:
            trigger = caption.suggest_trigger(paths, concept_type)
        return gr.update(value=trigger), gr.update(value=caption.caption_one(paths[idx], concept_type, trigger))
    return _caption_one


def gather_dataset(files, *captions):
    paths, _ = _split_uploads(files)
    return [[img, (captions[i] if i < len(captions) else "")] for i, img in enumerate(paths)]


def start_training(
    dataset_rows, concept_type, lora_name, trigger, rank, lora_alpha,
    max_train_steps, learning_rate, lr_scheduler, resolution, repeats, train_batch_size,
    gradient_accumulation_steps, seed, optimizer, use_8bit_adam, cache_latents,
    gradient_checkpointing, offload, quantization, lora_layers, make_gallery, num_gallery_images,
    custom_eval_prompts, flavor, timeout,
    profile: gr.OAuthProfile | None = None, oauth_token: gr.OAuthToken | None = None,
):
    if oauth_token is None or profile is None:
        return "❌ Please **sign in with Hugging Face** first (top-right).", "", ""
    if not dataset_rows:
        return "❌ Upload at least one image.", "", ""
    if not (lora_name or "").strip():
        return "❌ Give your LoRA a name.", "", ""

    image_paths = [r[0] for r in dataset_rows]
    captions = [r[1] for r in dataset_rows]
    params = {
        "concept_type": concept_type, "lora_name": lora_name,
        "instance_prompt": trigger,
        "rank": rank, "lora_alpha": lora_alpha, "max_train_steps": max_train_steps,
        "learning_rate": learning_rate, "lr_scheduler": lr_scheduler, "resolution": resolution,
        "repeats": repeats, "train_batch_size": train_batch_size,
        "gradient_accumulation_steps": gradient_accumulation_steps, "seed": seed,
        "optimizer": optimizer, "use_8bit_adam": bool(use_8bit_adam),
        "cache_latents": bool(cache_latents), "gradient_checkpointing": bool(gradient_checkpointing),
        "offload": bool(offload), "quantization": quantization, "lora_layers": lora_layers,
        "make_gallery": bool(make_gallery), "num_gallery_images": num_gallery_images,
        "custom_eval_prompts": custom_eval_prompts,
        "hf_token": oauth_token.token,
    }
    try:
        res = jobs.submit(params, image_paths, captions, flavor=flavor, timeout=timeout)
    except Exception as e:  # noqa: BLE001
        return f"❌ Submission failed: {e}", "", ""

    status = f"βœ… Job submitted on **{flavor}**, running as **{profile.username}**."
    link = (
        f"**Job:** [{res['job_id']}]({res['url']})  \n"
        f"**Dataset:** `{res['dataset_repo']}`  \n"
        f"**LoRA will be pushed to:** `{res['hub_model_id']}`  \n\n"
        f"Track progress in the **Monitor** tab (job id is prefilled)."
    )
    return status, link, res["job_id"]


def cost_estimate(steps, num_gallery, make_gallery, flavor):
    minutes, dollars = jobs.estimate_cost(steps, num_gallery, make_gallery, flavor)
    sps = jobs.SEC_PER_STEP.get(flavor, 2.35)
    return (
        f"πŸ’Έ **Estimated cost:** ~${dollars:.2f} Β· ~{minutes:.0f} min on `{flavor}` "
        f"(~{sps:.1f}s/step, compiled). _Rough estimate β€” the first run also downloads the "
        f"gated Krea 2 weights (one-time ~2–3 min)._"
    )


def refresh(job_id, oauth_token: gr.OAuthToken | None = None):
    if not (job_id or "").strip():
        return "Enter a job id.", ""
    token = oauth_token.token if oauth_token else ""
    st = jobs.job_status(job_id.strip(), token)
    logs = jobs.job_logs(job_id.strip(), token)
    return f"**Status:** `{st}`", logs[-MAX_LOG:] if len(logs) > MAX_LOG else logs


with gr.Blocks(title="Krea 2 LoRA Trainer") as demo:
    gr.Markdown(
        "# 🎨 Krea 2 LoRA Trainer\n"
        "### Train a high-quality Krea 2 LoRA from your own images\n"
        "Trains on **Krea 2 RAW**, optionally validates on **Turbo**, runs on **HF Jobs**, "
        "pushed to your Hub. You only pay for the job's GPU minutes.",
        elem_id="main_title",
    )
    with gr.Row():
        gr.Column(scale=2, min_width=0)
        with gr.Column(scale=0, min_width=380):
            gr.LoginButton("Sign in with Hugging Face to use the UI", elem_id="login", elem_classes=["login_logout"])
            gr.Markdown("…or paste this to your coding agent:", elem_classes=["agent_hint"])
            gr.Code(f"curl {AGENT_URL}", language="shell", interactive=False,
                    show_label=False, elem_classes=["agent_curl"])
        gr.Column(scale=2, min_width=0)
    banner = gr.Markdown(elem_id="signin_banner")

    with gr.Column(elem_id="main_ui", elem_classes=["locked"]) as main_ui, gr.Tabs():
        with gr.Tab("Train"):
            lora_name = gr.Textbox(
                label="The name of your LoRA", placeholder="e.g. my-impasto-style",
                info="Has to be unique β€” used for your output model repo (you/<name>) and dataset repo.",
            )
            concept_type = gr.Radio(
                CONCEPT_CHOICES, value=None, label="What are you training?",
                info="Drives how images are captioned and what kind of trigger is suggested.",
            )

            with gr.Row(visible=False, equal_height=False) as image_upload:
                with gr.Column(scale=1):
                    images = gr.File(
                        label="Upload your images (4–30 ideal)", file_count="multiple",
                        file_types=["image", ".txt"], interactive=True, height=320,
                    )
                    gr.Markdown(
                        "_Already have captions? Drop a `.txt` next to each image with the same "
                        "name (e.g. `cat.png` + `cat.txt`) and they'll be filled in automatically. "
                        "Otherwise, you'll be able to caption your images here β†’_",
                        elem_id="upload_hint",
                    )
                with gr.Column(scale=3, visible=False, elem_id="captioning_area") as captioning_area:
                    gr.Markdown("## Trigger & captioning")
                    with gr.Row(elem_classes=["trigger_row"], equal_height=True):
                        trigger = gr.Textbox(
                            label="Trigger", placeholder=TRIGGER_PLACEHOLDER["style"],
                            scale=4, interactive=True,
                        )
                        suggest_btn = gr.Button("✨ Suggest", scale=1, min_width=120,
                                                variant="secondary", elem_classes=["suggest_btn"])
                    trigger_help = gr.Markdown(TRIGGER_HELP["style"])
                    autocaption_btn = gr.Button("✨ Add captions with Gemma",
                                                variant="primary")
                    gr.Markdown(CAPTION_TIP)
                    caption_rows, caption_imgs, caption_txts = [], [], []
                    for i in range(MAX_IMAGES):
                        with gr.Column(visible=False, elem_classes=["cap_item"]) as row:
                            img = gr.Image(
                                height=180, interactive=False, show_label=False,
                                container=False, elem_classes=["cap_img"],
                            )
                            with gr.Row(elem_classes=["cap_row"], equal_height=True):
                                cap = gr.Textbox(label=f"Caption {i + 1}", scale=14, interactive=True)
                                cap_btn = gr.Button("✨", scale=0, min_width=46,
                                                    variant="secondary", elem_classes=["cap_one_btn"])
                        cap_btn.click(make_caption_one(i), inputs=[images, concept_type, trigger],
                                      outputs=[trigger, cap])
                        caption_rows.append(row)
                        caption_imgs.append(img)
                        caption_txts.append(cap)

            with gr.Column(visible=False) as post_upload:
                with gr.Accordion("Advanced options", open=False, elem_classes=["accordion"]):
                    with gr.Row():
                        rank = gr.Number(label="LoRA rank", value=32, precision=0,
                                         info="Authors recommend 32; raise it for long runs / high-frequency styles.")
                        lora_alpha = gr.Number(label="LoRA alpha", value=32, precision=0,
                                               info="Keep equal to rank (scale 1.0).")
                    with gr.Row():
                        max_train_steps = gr.Number(label="Training steps", value=1000, precision=0)
                        learning_rate = gr.Number(label="Learning rate", value=3e-4,
                                                  info="3e-4 ~ 7e-4 with constant works well; go higher with cosine.")
                    with gr.Row():
                        lr_scheduler = gr.Dropdown(LR_SCHEDULERS, value="constant", label="LR scheduler")
                        resolution = gr.Number(label="Resolution", value=1024, precision=0)
                    with gr.Row():
                        repeats = gr.Number(label="Dataset repeats", value=1, precision=0)
                        seed = gr.Number(label="Seed", value=0, precision=0)
                    lora_layers = gr.Textbox(
                        label="Target layers (optional)", placeholder="wq,wk,wv,wo,gate",
                        info="Blank = the authors' recommended full set. For long runs, narrow to the "
                             "attention layers (wq,wk,wv,wo,gate) and raise the rank so prompt adherence holds.",
                    )
                    timeout = gr.Textbox(
                        label="Job timeout", value="6h",
                        info="Max job runtime before it's stopped. The first run also downloads the "
                             "gated Krea 2 weights, so leave headroom.",
                    )
                    with gr.Accordion("Memory / performance", open=False):
                        with gr.Row():
                            quantization = gr.Dropdown(QUANT_CHOICES, value="none", label="Quantization")
                            optimizer = gr.Dropdown(OPTIMIZERS, value="adamW", label="Optimizer")
                        with gr.Row():
                            use_8bit_adam = gr.Checkbox(label="8-bit Adam", value=True)
                            cache_latents = gr.Checkbox(label="Cache latents", value=True)
                        with gr.Row():
                            gradient_checkpointing = gr.Checkbox(label="Gradient checkpointing", value=True)
                            offload = gr.Checkbox(label="CPU offload (VAE + text encoder)", value=False)
                        with gr.Row():
                            train_batch_size = gr.Number(label="Batch size", value=1, precision=0)
                            gradient_accumulation_steps = gr.Number(label="Grad accumulation", value=1, precision=0)
                    with gr.Accordion("Preview gallery & README (on Turbo)", open=True,
                                      elem_classes=["accordion"]):
                        make_gallery = gr.Checkbox(
                            label="Generate a preview gallery + rich README", value=True,
                            info="After training, render sample images on Krea 2 Turbo with your LoRA "
                                 "and push a model-card README where each image is captioned by its prompt.",
                        )
                        num_gallery_images = gr.Number(label="Number of samples", value=3, precision=0)
                        custom_eval_prompts = gr.Textbox(
                            label="Custom showcase prompts (optional)", lines=4,
                            placeholder="One prompt per line β€” use <trigger> where the trigger should go.\n"
                                        "Leave blank to auto-generate diverse prompts with the LLM.",
                            info="If blank, the LLM writes diverse showcase prompts from your concept + trigger.",
                        )

                gr.Markdown("### Output & submit")
                flavor = gr.Dropdown(jobs.FLAVORS, value=jobs.DEFAULT_FLAVOR, label="GPU flavor")
                with gr.Accordion("GPU guide", open=False):
                    gr.Markdown(FLAVOR_GUIDE)

                cost_md = gr.Markdown(cost_estimate(1000, 3, True, jobs.DEFAULT_FLAVOR))
                submit_btn = gr.Button("πŸš€ Submit training job", variant="primary", size="lg")
                status = gr.Markdown()
                joblink = gr.Markdown()

        with gr.Tab("Monitor"):
            with gr.Row():
                job_id = gr.Textbox(label="Job id", scale=3)
                refresh_btn = gr.Button("πŸ”„ Refresh", scale=1)
            mon_status = gr.Markdown()
            mon_logs = gr.Textbox(label="Job logs", lines=22, autoscroll=True, max_lines=22)

    dataset_state = gr.State([])

    caption_outputs = [captioning_area, post_upload]
    for r, im, c in zip(caption_rows, caption_imgs, caption_txts):
        caption_outputs += [r, im, c]

    demo.load(_signin_state, inputs=None, outputs=[banner, main_ui, lora_name])
    concept_type.change(
        on_concept_change, inputs=[concept_type], outputs=[image_upload, trigger, trigger_help],
    )
    images.change(load_captioning, inputs=[images, trigger], outputs=caption_outputs)
    suggest_btn.click(ai_suggest_trigger, inputs=[images, concept_type], outputs=[trigger])
    autocaption_btn.click(
        ai_caption_all, inputs=[images, concept_type, trigger], outputs=[trigger, *caption_txts],
    )

    submit_btn.click(
        gather_dataset, inputs=[images, *caption_txts], outputs=dataset_state,
    ).then(
        start_training,
        inputs=[dataset_state, concept_type, lora_name, trigger, rank, lora_alpha,
                max_train_steps, learning_rate, lr_scheduler, resolution, repeats, train_batch_size,
                gradient_accumulation_steps, seed, optimizer, use_8bit_adam, cache_latents,
                gradient_checkpointing, offload, quantization, lora_layers, make_gallery,
                num_gallery_images, custom_eval_prompts, flavor, timeout],
        outputs=[status, joblink, job_id],
    )
    cost_inputs = [max_train_steps, num_gallery_images, make_gallery, flavor]
    for comp in cost_inputs:
        comp.change(cost_estimate, inputs=cost_inputs, outputs=cost_md)

    refresh_btn.click(refresh, inputs=[job_id], outputs=[mon_status, mon_logs])


if __name__ == "__main__":
    demo.queue(default_concurrency_limit=4).launch(theme=THEME, css=CSS)