Name downloads as project + yymmddhhmmss
Browse filesAdd a session project short-name field (default f2klora, max 12
alphanumeric). PNG/WebP/prompt downloads share that stem plus timestamp.
Co-Authored-By: Oz <oz-agent@warp.dev>
app.py
CHANGED
|
@@ -73,9 +73,39 @@ from control_tools import (
|
|
| 73 |
OPENPOSE_KEYPOINT_NAMES,
|
| 74 |
)
|
| 75 |
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
"""Save image without any metadata for privacy."""
|
| 78 |
-
path = f"
|
| 79 |
image.save(path, format="PNG")
|
| 80 |
return path
|
| 81 |
|
|
@@ -90,13 +120,13 @@ def _build_full_prompt(prompt, lora_prompt_text, custom_prompt_text) -> str:
|
|
| 90 |
)
|
| 91 |
|
| 92 |
|
| 93 |
-
def save_webp_from_image(image: Image.Image) -> str:
|
| 94 |
-
path = f"
|
| 95 |
image.convert("RGB").save(path, format="WEBP", quality=90, method=4)
|
| 96 |
return path
|
| 97 |
|
| 98 |
|
| 99 |
-
def save_webp_from_path(path) -> str | None:
|
| 100 |
from image_utils import _gallery_item_path
|
| 101 |
p = _gallery_item_path(path) or (path if isinstance(path, str) else None)
|
| 102 |
if not p:
|
|
@@ -105,14 +135,14 @@ def save_webp_from_path(path) -> str | None:
|
|
| 105 |
img = Image.open(p).convert("RGB")
|
| 106 |
except Exception:
|
| 107 |
return None
|
| 108 |
-
return save_webp_from_image(img)
|
| 109 |
|
| 110 |
|
| 111 |
-
def save_prompt_txt(text: str | None) -> str | None:
|
| 112 |
text = (text or "").strip()
|
| 113 |
if not text:
|
| 114 |
return None
|
| 115 |
-
path = f"
|
| 116 |
with open(path, "w", encoding="utf-8") as f:
|
| 117 |
f.write(text)
|
| 118 |
if not text.endswith("\n"):
|
|
@@ -120,11 +150,30 @@ def save_prompt_txt(text: str | None) -> str | None:
|
|
| 120 |
return path
|
| 121 |
|
| 122 |
|
| 123 |
-
def
|
| 124 |
-
"""
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
return png, webp, prompt_file
|
| 129 |
|
| 130 |
# ββ Model load ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
@@ -255,7 +304,7 @@ def infer(
|
|
| 255 |
base_image, reference_images, prompt, lora_prompt_text, custom_prompt_text,
|
| 256 |
selected_titles, seed, randomize_seed, guidance_scale, steps, upscale_factor,
|
| 257 |
canvas_mode, custom_width, custom_height, canvas_fit_mode, pad_color,
|
| 258 |
-
batch_count, batch_vary, sweep_min, sweep_max,
|
| 259 |
dynamic_loras, *slider_values, progress=gr.Progress(track_tqdm=True),
|
| 260 |
):
|
| 261 |
"""Generator. Streams a list of PNG paths into the output gallery."""
|
|
@@ -270,6 +319,7 @@ def infer(
|
|
| 270 |
|
| 271 |
selected_titles = selected_titles or []
|
| 272 |
batch_count = max(1, int(batch_count))
|
|
|
|
| 273 |
|
| 274 |
base_seed = random.randint(0, MAX_SEED) if randomize_seed else int(seed)
|
| 275 |
seeds, weight_overrides = [], []
|
|
@@ -290,7 +340,10 @@ def infer(
|
|
| 290 |
results = []
|
| 291 |
last_seed_text = ""
|
| 292 |
full_prompt = _build_full_prompt(prompt, lora_prompt_text, custom_prompt_text)
|
| 293 |
-
|
|
|
|
|
|
|
|
|
|
| 294 |
for i in range(batch_count):
|
| 295 |
sliders = list(slider_values)
|
| 296 |
if weight_overrides[i] is not None:
|
|
@@ -308,9 +361,12 @@ def infer(
|
|
| 308 |
canvas_fit_mode, pad_color, dynamic_loras,
|
| 309 |
*sliders, progress=progress,
|
| 310 |
)
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
|
|
|
|
|
|
|
|
|
| 314 |
results.append(png_path)
|
| 315 |
last_seed_text = str(used_seed)
|
| 316 |
# Yield download paths explicitly. Relying only on output_gallery.change
|
|
@@ -319,8 +375,14 @@ def infer(
|
|
| 319 |
yield results, last_seed_text, png_path, webp_path, prompt_file, full_prompt
|
| 320 |
except Exception as e:
|
| 321 |
png = latest_gallery_download_path(results)
|
| 322 |
-
|
| 323 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 324 |
|
| 325 |
|
| 326 |
# ββ Bulk processing (one input image per iteration) βββββββββββββββββββββββββ
|
|
@@ -510,13 +572,26 @@ with gr.Blocks() as demo:
|
|
| 510 |
object_fit="contain", show_label=True,
|
| 511 |
elem_id="output_gallery",
|
| 512 |
)
|
| 513 |
-
|
| 514 |
-
|
| 515 |
-
|
| 516 |
-
|
| 517 |
-
|
| 518 |
-
|
| 519 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 520 |
with gr.Row():
|
| 521 |
download_png_btn = gr.DownloadButton(
|
| 522 |
label="β¬οΈ PNG",
|
|
@@ -546,9 +621,10 @@ with gr.Blocks() as demo:
|
|
| 546 |
send_out_to_base_btn = gr.Button("β© Send β Base", size="sm")
|
| 547 |
send_out_to_ref_btn = gr.Button("β© Send β Reference", size="sm")
|
| 548 |
gr.Markdown(
|
| 549 |
-
"*
|
| 550 |
-
"
|
| 551 |
-
"
|
|
|
|
| 552 |
)
|
| 553 |
|
| 554 |
with gr.Row():
|
|
@@ -1039,13 +1115,19 @@ with gr.Blocks() as demo:
|
|
| 1039 |
# plus the last-run prompt text.
|
| 1040 |
selected_output_state.change(
|
| 1041 |
fn=resolve_download_bundle,
|
| 1042 |
-
inputs=[selected_output_state, output_gallery, last_prompt_state],
|
| 1043 |
outputs=[download_png_btn, download_webp_btn, download_prompt_btn],
|
| 1044 |
show_progress="hidden",
|
| 1045 |
)
|
| 1046 |
output_gallery.change(
|
| 1047 |
fn=resolve_download_bundle,
|
| 1048 |
-
inputs=[selected_output_state, output_gallery, last_prompt_state],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1049 |
outputs=[download_png_btn, download_webp_btn, download_prompt_btn],
|
| 1050 |
show_progress="hidden",
|
| 1051 |
)
|
|
@@ -1072,7 +1154,7 @@ with gr.Blocks() as demo:
|
|
| 1072 |
inputs=[base_image, reference_images, prompt, lora_prompt_display, custom_prompt_display,
|
| 1073 |
lora_selector, seed, randomize_seed, guidance_scale, steps, upscale_factor,
|
| 1074 |
canvas_mode, custom_width, custom_height, canvas_fit_mode, pad_color,
|
| 1075 |
-
batch_count, batch_vary, sweep_min, sweep_max,
|
| 1076 |
dynamic_loras_state] + weight_sliders,
|
| 1077 |
# Download bundle on every generate yield β required because
|
| 1078 |
# gallery.change alone misses the first virgin-session result.
|
|
|
|
| 73 |
OPENPOSE_KEYPOINT_NAMES,
|
| 74 |
)
|
| 75 |
|
| 76 |
+
DEFAULT_PROJECT_NAME = "f2klora"
|
| 77 |
+
MAX_PROJECT_NAME_LEN = 12
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
def sanitize_project_name(name: str | None) -> str:
|
| 81 |
+
"""Alphanumeric only, max 12 chars. Default f2klora."""
|
| 82 |
+
raw = (name or "").strip()
|
| 83 |
+
cleaned = "".join(c for c in raw if c.isalnum())
|
| 84 |
+
cleaned = cleaned[:MAX_PROJECT_NAME_LEN]
|
| 85 |
+
return cleaned or DEFAULT_PROJECT_NAME
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
def make_run_stamp() -> str:
|
| 89 |
+
"""yymmddhhmmss β no separators."""
|
| 90 |
+
return time.strftime("%y%m%d%H%M%S")
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
def make_download_basename(project: str | None, stamp: str | None = None,
|
| 94 |
+
batch_index: int | None = None) -> str:
|
| 95 |
+
stem = f"{sanitize_project_name(project)}{stamp or make_run_stamp()}"
|
| 96 |
+
if batch_index is not None:
|
| 97 |
+
stem = f"{stem}{int(batch_index):02d}"
|
| 98 |
+
return stem
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
def _tmp_named(basename: str, ext: str) -> str:
|
| 102 |
+
ext = ext if ext.startswith(".") else f".{ext}"
|
| 103 |
+
return f"/tmp/{basename}{ext}"
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
def save_simple_image(image: Image.Image, basename: str | None = None) -> str:
|
| 107 |
"""Save image without any metadata for privacy."""
|
| 108 |
+
path = _tmp_named(basename or f"gen{uuid.uuid4().hex[:8]}", ".png")
|
| 109 |
image.save(path, format="PNG")
|
| 110 |
return path
|
| 111 |
|
|
|
|
| 120 |
)
|
| 121 |
|
| 122 |
|
| 123 |
+
def save_webp_from_image(image: Image.Image, basename: str | None = None) -> str:
|
| 124 |
+
path = _tmp_named(basename or f"gen{uuid.uuid4().hex[:8]}", ".webp")
|
| 125 |
image.convert("RGB").save(path, format="WEBP", quality=90, method=4)
|
| 126 |
return path
|
| 127 |
|
| 128 |
|
| 129 |
+
def save_webp_from_path(path, basename: str | None = None) -> str | None:
|
| 130 |
from image_utils import _gallery_item_path
|
| 131 |
p = _gallery_item_path(path) or (path if isinstance(path, str) else None)
|
| 132 |
if not p:
|
|
|
|
| 135 |
img = Image.open(p).convert("RGB")
|
| 136 |
except Exception:
|
| 137 |
return None
|
| 138 |
+
return save_webp_from_image(img, basename=basename)
|
| 139 |
|
| 140 |
|
| 141 |
+
def save_prompt_txt(text: str | None, basename: str | None = None) -> str | None:
|
| 142 |
text = (text or "").strip()
|
| 143 |
if not text:
|
| 144 |
return None
|
| 145 |
+
path = _tmp_named(basename or f"prompt{uuid.uuid4().hex[:8]}", ".txt")
|
| 146 |
with open(path, "w", encoding="utf-8") as f:
|
| 147 |
f.write(text)
|
| 148 |
if not text.endswith("\n"):
|
|
|
|
| 150 |
return path
|
| 151 |
|
| 152 |
|
| 153 |
+
def copy_as_named_png(src_path, basename: str) -> str | None:
|
| 154 |
+
"""Copy an existing PNG to a project+timestamp name for download."""
|
| 155 |
+
from image_utils import _gallery_item_path
|
| 156 |
+
import shutil
|
| 157 |
+
p = _gallery_item_path(src_path) or (src_path if isinstance(src_path, str) else None)
|
| 158 |
+
if not p or not os.path.isfile(p):
|
| 159 |
+
return None
|
| 160 |
+
dest = _tmp_named(basename, ".png")
|
| 161 |
+
if os.path.abspath(p) == os.path.abspath(dest):
|
| 162 |
+
return p
|
| 163 |
+
try:
|
| 164 |
+
shutil.copy2(p, dest)
|
| 165 |
+
return dest
|
| 166 |
+
except Exception:
|
| 167 |
+
return p
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
def resolve_download_bundle(selected_path, gallery_value, last_prompt_text, project_name):
|
| 171 |
+
"""PNG + WebP + prompt txt named project+yymmddhhmmss.(png|webp|txt)."""
|
| 172 |
+
png_src = resolve_output_download_path(selected_path, gallery_value)
|
| 173 |
+
base = make_download_basename(project_name)
|
| 174 |
+
png = copy_as_named_png(png_src, base) if png_src else None
|
| 175 |
+
webp = save_webp_from_path(png_src, basename=base) if png_src else None
|
| 176 |
+
prompt_file = save_prompt_txt(last_prompt_text, basename=base)
|
| 177 |
return png, webp, prompt_file
|
| 178 |
|
| 179 |
# ββ Model load ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 304 |
base_image, reference_images, prompt, lora_prompt_text, custom_prompt_text,
|
| 305 |
selected_titles, seed, randomize_seed, guidance_scale, steps, upscale_factor,
|
| 306 |
canvas_mode, custom_width, custom_height, canvas_fit_mode, pad_color,
|
| 307 |
+
batch_count, batch_vary, sweep_min, sweep_max, project_name,
|
| 308 |
dynamic_loras, *slider_values, progress=gr.Progress(track_tqdm=True),
|
| 309 |
):
|
| 310 |
"""Generator. Streams a list of PNG paths into the output gallery."""
|
|
|
|
| 319 |
|
| 320 |
selected_titles = selected_titles or []
|
| 321 |
batch_count = max(1, int(batch_count))
|
| 322 |
+
project = sanitize_project_name(project_name)
|
| 323 |
|
| 324 |
base_seed = random.randint(0, MAX_SEED) if randomize_seed else int(seed)
|
| 325 |
seeds, weight_overrides = [], []
|
|
|
|
| 340 |
results = []
|
| 341 |
last_seed_text = ""
|
| 342 |
full_prompt = _build_full_prompt(prompt, lora_prompt_text, custom_prompt_text)
|
| 343 |
+
# One stamp for the whole batch; multi-run items get 00/01/... suffix (no hyphen).
|
| 344 |
+
run_stamp = make_run_stamp()
|
| 345 |
+
prompt_base = make_download_basename(project, run_stamp)
|
| 346 |
+
prompt_file = save_prompt_txt(full_prompt, basename=prompt_base)
|
| 347 |
for i in range(batch_count):
|
| 348 |
sliders = list(slider_values)
|
| 349 |
if weight_overrides[i] is not None:
|
|
|
|
| 361 |
canvas_fit_mode, pad_color, dynamic_loras,
|
| 362 |
*sliders, progress=progress,
|
| 363 |
)
|
| 364 |
+
item_base = make_download_basename(
|
| 365 |
+
project, run_stamp,
|
| 366 |
+
batch_index=i if batch_count > 1 else None,
|
| 367 |
+
)
|
| 368 |
+
png_path = save_simple_image(image, basename=item_base)
|
| 369 |
+
webp_path = save_webp_from_image(image, basename=item_base)
|
| 370 |
results.append(png_path)
|
| 371 |
last_seed_text = str(used_seed)
|
| 372 |
# Yield download paths explicitly. Relying only on output_gallery.change
|
|
|
|
| 375 |
yield results, last_seed_text, png_path, webp_path, prompt_file, full_prompt
|
| 376 |
except Exception as e:
|
| 377 |
png = latest_gallery_download_path(results)
|
| 378 |
+
fail_base = make_download_basename(project)
|
| 379 |
+
webp = save_webp_from_path(png, basename=fail_base) if png else None
|
| 380 |
+
png_named = copy_as_named_png(png, fail_base) if png else None
|
| 381 |
+
yield (
|
| 382 |
+
results,
|
| 383 |
+
f"Batch {i+1}/{batch_count} failed: {e}",
|
| 384 |
+
png_named, webp, prompt_file, full_prompt,
|
| 385 |
+
)
|
| 386 |
|
| 387 |
|
| 388 |
# ββ Bulk processing (one input image per iteration) βββββββββββββββββββββββββ
|
|
|
|
| 572 |
object_fit="contain", show_label=True,
|
| 573 |
elem_id="output_gallery",
|
| 574 |
)
|
| 575 |
+
with gr.Row():
|
| 576 |
+
used_seed = gr.Textbox(
|
| 577 |
+
label="π± Seed used (last run)",
|
| 578 |
+
interactive=False,
|
| 579 |
+
lines=1,
|
| 580 |
+
max_lines=1,
|
| 581 |
+
elem_id="used_seed",
|
| 582 |
+
scale=2,
|
| 583 |
+
)
|
| 584 |
+
project_name = gr.Textbox(
|
| 585 |
+
label="Project short name",
|
| 586 |
+
value=DEFAULT_PROJECT_NAME,
|
| 587 |
+
max_lines=1,
|
| 588 |
+
lines=1,
|
| 589 |
+
max_length=MAX_PROJECT_NAME_LEN,
|
| 590 |
+
placeholder=DEFAULT_PROJECT_NAME,
|
| 591 |
+
info="Max 12 letters/digits. Used in download filenames.",
|
| 592 |
+
scale=1,
|
| 593 |
+
elem_id="project_name",
|
| 594 |
+
)
|
| 595 |
with gr.Row():
|
| 596 |
download_png_btn = gr.DownloadButton(
|
| 597 |
label="β¬οΈ PNG",
|
|
|
|
| 621 |
send_out_to_base_btn = gr.Button("β© Send β Base", size="sm")
|
| 622 |
send_out_to_ref_btn = gr.Button("β© Send β Reference", size="sm")
|
| 623 |
gr.Markdown(
|
| 624 |
+
"*Downloads: `project` + `yymmddhhmmss` + `.png/.webp/.txt` "
|
| 625 |
+
f"(default project `{DEFAULT_PROJECT_NAME}`). "
|
| 626 |
+
"Click a gallery thumbnail before Download / Sendβ; otherwise latest. "
|
| 627 |
+
"Prompt file = last run's combined user + LoRA + custom text.*"
|
| 628 |
)
|
| 629 |
|
| 630 |
with gr.Row():
|
|
|
|
| 1115 |
# plus the last-run prompt text.
|
| 1116 |
selected_output_state.change(
|
| 1117 |
fn=resolve_download_bundle,
|
| 1118 |
+
inputs=[selected_output_state, output_gallery, last_prompt_state, project_name],
|
| 1119 |
outputs=[download_png_btn, download_webp_btn, download_prompt_btn],
|
| 1120 |
show_progress="hidden",
|
| 1121 |
)
|
| 1122 |
output_gallery.change(
|
| 1123 |
fn=resolve_download_bundle,
|
| 1124 |
+
inputs=[selected_output_state, output_gallery, last_prompt_state, project_name],
|
| 1125 |
+
outputs=[download_png_btn, download_webp_btn, download_prompt_btn],
|
| 1126 |
+
show_progress="hidden",
|
| 1127 |
+
)
|
| 1128 |
+
project_name.change(
|
| 1129 |
+
fn=resolve_download_bundle,
|
| 1130 |
+
inputs=[selected_output_state, output_gallery, last_prompt_state, project_name],
|
| 1131 |
outputs=[download_png_btn, download_webp_btn, download_prompt_btn],
|
| 1132 |
show_progress="hidden",
|
| 1133 |
)
|
|
|
|
| 1154 |
inputs=[base_image, reference_images, prompt, lora_prompt_display, custom_prompt_display,
|
| 1155 |
lora_selector, seed, randomize_seed, guidance_scale, steps, upscale_factor,
|
| 1156 |
canvas_mode, custom_width, custom_height, canvas_fit_mode, pad_color,
|
| 1157 |
+
batch_count, batch_vary, sweep_min, sweep_max, project_name,
|
| 1158 |
dynamic_loras_state] + weight_sliders,
|
| 1159 |
# Download bundle on every generate yield β required because
|
| 1160 |
# gallery.change alone misses the first virgin-session result.
|