* test(browser): seed resets on a scratch copy and publish them in one commit (#384) The concept and plain resets made hundreds of committed transactions on the live database; on a slow CI disk they queued behind WAL checkpoint syncs and took 30+ s. Each reset now copies the live database into a scratch database (backup API, no syncs), runs the unchanged seeder against it through an API proxy, and replaces the rows of the tables the seed changed in one live transaction. A commit by another live connection meanwhile fails the reset. tests/browser/reset_equivalence.py proves every reset kind leaves the same rows, files and response as the old path (PM_TEST_SEED_ON_LIVE=1). * test(browser): empty media folders by a rename, delete them in the background (#384) With the database commits gone, throttled resets still stalled in rmtree: unlinking the last fixture's files waits for their pending writeback. Each reseed now moves the old output and input folders into <root>/reset-trash and makes empty ones; a background thread deletes the trash, and the server's cleanup stops it and removes the rest before the temp root goes. * fix(test): recheck the live data version under the publish's write lock (#384) A writer whose transaction was open while the seed ran committed before BEGIN IMMEDIATE returned and its rows were overwritten. The publish now rechecks data_version under the lock, and restores foreign_keys and detaches the scratch database on every error path. * test(browser): keep the reset scratch database in /dev/shm when there is one (#384) Throttled runs still waited on the runner's disk while creating and rewriting the scratch database, whose pages were under writeback. * test(browser): keep the scratch on disk when /dev/shm is short, delete in place after cleanup (#384) The largest scratch copy is about 8 MB; Docker's default /dev/shm is 64 MB and shared with the browsers. A reset that empties a folder after the server's cleanup closed its trash deletes in place instead of starting a thread nobody stops. * fix(test): drop the gallery caches only after the reset's rows are published (#384) The seeders drop the Images listing at their end. Run on the scratch copy, that drop came before the publish, so a background walk in between could cache per-file values built from the live database's old rows: CI's inspector tag chip found 0 of 0 images. The proxy now records the drop and the seeder makes it after the publish, as the old reset did. * fix(test): remove /dev/shm reset scratch left by killed browser servers (#384) A server killed outright never runs its cleanup, and tmpfs keeps the scratch folder in memory until reboot: the Python suite's server tests left one per killed server. Scratch folders now carry the owner's pid, and each new one first removes those whose process is gone (POSIX only).
173 lines
5.9 KiB
Python
173 lines
5.9 KiB
Python
"""Deterministic original artwork and saved prompts for workspace visual checks."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
from typing import TYPE_CHECKING
|
|
|
|
from PIL import Image, ImageDraw
|
|
from PIL.PngImagePlugin import PngInfo
|
|
|
|
from utils.hashing import generate_prompt_hash
|
|
from tests.browser.folder_trash import replace_folder
|
|
|
|
if TYPE_CHECKING:
|
|
from py.api import PromptManagerAPI
|
|
|
|
|
|
_POSITIVES = (
|
|
(
|
|
"Amber geometry\nLayered sandstone arches, warm afternoon light, "
|
|
"precise silhouettes."
|
|
),
|
|
(
|
|
"Tidal observatory\nCobalt circles over a quiet sea, "
|
|
"a pale horizon and measured reflections."
|
|
),
|
|
(
|
|
"Forest atlas\nOverlapping emerald triangles, paper textures "
|
|
"and a soft misty background."
|
|
),
|
|
(
|
|
"Rose pavilion\nCoral blocks and floating windows, "
|
|
"balanced shadows on a terracotta plaza."
|
|
),
|
|
"Violet orbit\nConcentric rings, lavender haze and a tiny silver moon.",
|
|
"A study in very long creative context\n"
|
|
+ "Preserve every geometric detail, layered color relationship and quiet edge. "
|
|
* 18
|
|
+ "unbroken-"
|
|
+ "context" * 40,
|
|
)
|
|
_NEGATIVES = (
|
|
("blurred edges, watermark", "flat lighting, clipped highlights", ""),
|
|
("low contrast, noise", "distorted horizon"),
|
|
("oversaturated foliage", "soft geometry"),
|
|
("unbalanced composition",),
|
|
("banding, harsh gradients",),
|
|
("cropped details, illegible marks",),
|
|
)
|
|
|
|
|
|
def _write_art(path: Path, index: int, positive: str, negative: str) -> None:
|
|
sizes = ((720, 480), (480, 720), (560, 560), (900, 400))
|
|
palettes = (
|
|
("#252c46", "#e0a75e", "#b76445"),
|
|
("#153b57", "#64bfc6", "#dfdba8"),
|
|
("#1d4947", "#71ac83", "#d5d8aa"),
|
|
("#523547", "#ef9c87", "#bc6764"),
|
|
("#342b59", "#a79bd6", "#e5c0bf"),
|
|
("#303d4d", "#b1c4c5", "#d6a05f"),
|
|
)
|
|
width, height = sizes[index % len(sizes)]
|
|
background, accent, highlight = palettes[index // 4]
|
|
graph = {
|
|
"1": {"class_type": "CLIPTextEncode", "inputs": {"text": positive}},
|
|
"2": {"class_type": "CLIPTextEncode", "inputs": {"text": negative}},
|
|
"3": {
|
|
"class_type": "KSampler",
|
|
"inputs": {"positive": ["1", 0], "negative": ["2", 0]},
|
|
},
|
|
"4": {"class_type": "SaveImage", "inputs": {"images": ["3", 0]}},
|
|
}
|
|
info = PngInfo()
|
|
info.add_text("prompt", json.dumps(graph))
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
with Image.new("RGB", (width, height), background) as image:
|
|
draw = ImageDraw.Draw(image)
|
|
draw.rectangle((0, height * 3 // 4, width, height), fill=highlight)
|
|
radius = min(width, height) // 3
|
|
center_x, center_y = width // 2 + (index % 3 - 1) * 25, height // 2
|
|
draw.ellipse(
|
|
(
|
|
center_x - radius,
|
|
center_y - radius,
|
|
center_x + radius,
|
|
center_y + radius,
|
|
),
|
|
fill=accent,
|
|
)
|
|
draw.polygon(
|
|
(
|
|
(width // 8, height * 7 // 8),
|
|
(width // 2, height // 4),
|
|
(width * 7 // 8, height * 7 // 8),
|
|
),
|
|
outline=highlight,
|
|
width=8,
|
|
)
|
|
draw.line(
|
|
(width // 6, height // 3, width * 5 // 6, height // 3),
|
|
fill=highlight,
|
|
width=4,
|
|
)
|
|
image.save(path, pnginfo=info)
|
|
stamp = 1_760_000_000 - index * 60
|
|
os.utime(path, (stamp, stamp))
|
|
|
|
|
|
def seed_workspace_visual(api: PromptManagerAPI, root: Path) -> None:
|
|
"""Replace only the owned browser harness records and media roots."""
|
|
with api.db.model.get_connection() as conn:
|
|
for table in (
|
|
"saved_views",
|
|
"image_favorites",
|
|
"media_review_decisions",
|
|
"media_review_items",
|
|
"media_image_tags",
|
|
"media_annotation_bytes",
|
|
"media_annotation_files",
|
|
"image_prompt_pairs",
|
|
"prompt_pair_tags",
|
|
"prompt_pairs",
|
|
"generated_images",
|
|
"prompt_tags",
|
|
"tags",
|
|
"prompts",
|
|
):
|
|
conn.execute(f"DELETE FROM {table}")
|
|
for name in ("output", "input"):
|
|
replace_folder(root / name)
|
|
for parent_index, (positive, negatives) in enumerate(zip(_POSITIVES, _NEGATIVES)):
|
|
category = ("Architecture", "Landscape", "Abstract")[parent_index % 3]
|
|
tags = ["geometric", ("warm", "cool")[parent_index % 2]]
|
|
if parent_index == 5:
|
|
tags.append("long-context-" + "detail" * 28)
|
|
prompt_id = api.db.save_prompt(
|
|
positive,
|
|
category=category,
|
|
tags=tags,
|
|
prompt_hash=generate_prompt_hash(positive),
|
|
rating=parent_index if parent_index else None,
|
|
)
|
|
for variant_index, negative in enumerate(negatives):
|
|
api.db.save_prompt_pair(
|
|
positive,
|
|
negative,
|
|
title=f"Composition {variant_index + 1}",
|
|
category=category,
|
|
tags=tags,
|
|
)
|
|
for offset in range(4):
|
|
index = parent_index * 4 + offset
|
|
directory = ("output", "input")[offset % 2]
|
|
filename = f"study-{index + 1:02}.png"
|
|
if index == 23:
|
|
filename = (
|
|
"a-very-long-geometric-composition-"
|
|
+ "layered-context-" * 9
|
|
+ ".png"
|
|
)
|
|
path = root / directory / category.lower() / filename
|
|
_write_art(path, index, positive, negatives[offset % len(negatives)])
|
|
api.db.link_image_to_prompt(prompt_id, str(path))
|
|
from py.api.thumbnail_settings import ThumbnailSettings
|
|
|
|
# 150 px previews in the default format (#317 thumbnail settings).
|
|
api._generate_gallery_thumbnails_sync(
|
|
api._get_all_output_dirs(), ThumbnailSettings("webp", 80, 150)
|
|
)
|
|
api.invalidate_gallery_cache()
|