Use direct autocorr inference for hosted demo

This commit is contained in:
Meta Vens 2026-04-26 19:21:01 +01:00
commit 728e08472b
12 changed files with 326 additions and 23 deletions

View file

@ -470,6 +470,46 @@ def test_gui_hosted_config_endpoint_exposes_demo_limits(monkeypatch, tmp_path: P
assert payload["ui"]["showQueuePanel"] is True
def test_hosted_job_options_use_direct_autocorr_and_skip_rerank() -> None:
from repixelizer.gui import HostedDemoConfig, _normalize_job_options
config = HostedDemoConfig(
hosted_demo=True,
show_queue_panel=True,
max_upload_bytes=1_048_576,
max_input_dimension=2048,
max_output_dimension=256,
default_steps=32,
max_steps=48,
queue_capacity=10,
heartbeat_interval_seconds=10,
stale_after_seconds=30,
phase_field_preview_stride=4,
spool_dir=Path("spool"),
)
options = _normalize_job_options(
config,
source_width=1254,
source_height=1254,
target_size=None,
target_width=None,
target_height=None,
phase_x=None,
phase_y=None,
steps=None,
seed=7,
device="auto",
strip_background=False,
skip_phase_rerank=False,
)
assert options["target_size"] is None
assert options["lattice_inference_mode"] == "autocorr"
assert options["max_inferred_target_size"] == 256
assert options["skip_phase_rerank"] is True
def test_gui_queue_panel_defaults_off_for_local_runs_and_can_be_forced(monkeypatch, tmp_path: Path) -> None:
from repixelizer.gui import create_app
@ -554,6 +594,51 @@ def test_gui_queue_rejects_eleventh_waiting_job(monkeypatch, tmp_path: Path) ->
assert "Queue is full" in getattr(rejected, "detail", str(rejected))
def test_gui_hosted_jobs_dispatch_direct_autocorr_pipeline(monkeypatch, tmp_path: Path) -> None:
from repixelizer.gui import create_app
release = threading.Event()
calls: list[dict[str, object]] = []
def fake_run_pipeline_rgba(*args, **kwargs):
calls.append(kwargs)
release.wait(timeout=2.0)
monkeypatch.setenv("REPIXELIZER_HOSTED_DEMO", "1")
monkeypatch.setenv("REPIXELIZER_SPOOL_DIR", str(tmp_path / "spool"))
monkeypatch.setattr("repixelizer.gui.run_pipeline_rgba", fake_run_pipeline_rgba)
upload = _png_bytes()
app = create_app()
create_job = _route_endpoint(app, "/api/jobs", "POST")
created = asyncio.run(
create_job(
image=UploadFile(filename="tiny.png", file=io.BytesIO(upload)),
target_size=None,
target_width=None,
target_height=None,
phase_x=None,
phase_y=None,
steps=None,
seed=7,
device="auto",
strip_background=False,
skip_phase_rerank=False,
)
)
deadline = time.time() + 2.0
while not calls and time.time() < deadline:
time.sleep(0.05)
release.set()
assert created.status_code == 200
assert calls
assert calls[0]["lattice_inference_mode"] == "autocorr"
assert calls[0]["max_inferred_target_size"] == 256
assert calls[0]["enable_phase_rerank"] is False
assert calls[0]["target_size"] is None
def test_gui_canceling_queued_job_cleans_spool_file(monkeypatch, tmp_path: Path) -> None:
from repixelizer.gui import create_app

View file

@ -15,6 +15,7 @@ from repixelizer.inference import (
_hint_target_sizes_from_autocorr,
_resolve_candidate_dims_from_autocorr,
_top_candidates_by_size,
infer_autocorr_lattice,
infer_fixed_lattice,
infer_lattice,
)
@ -135,6 +136,14 @@ def test_badge_autocorr_consensus_stays_in_canonical_family() -> None:
assert result.top_candidates[0].target_width in {125, 126}
def test_direct_badge_autocorr_consensus_stays_in_canonical_family() -> None:
rgba = load_rgba(Path("tests/fixtures/real/ai-badge-cleaned.png"))
result = infer_autocorr_lattice(rgba, device="cpu")
assert result.target_width in {125, 126}
assert result.top_candidates
assert all(candidate.target_width == result.target_width for candidate in result.top_candidates)
def test_top_candidates_are_diversified_by_size() -> None:
candidates = [
InferenceCandidate(target_width=113, target_height=113, phase_x=0.0, phase_y=0.0, score=0.90, breakdown={}),

View file

@ -470,6 +470,70 @@ def test_pipeline_fixed_target_and_phase_skip_search_inference(tmp_path: Path, m
assert result.inference.phase_y == -0.2
def test_pipeline_autocorr_mode_skips_size_search(tmp_path: Path, monkeypatch) -> None:
source = np.zeros((6, 6, 4), dtype=np.float32)
source[..., 3] = 1.0
input_path = tmp_path / "input.png"
output_path = tmp_path / "output.png"
from repixelizer.io import save_rgba
save_rgba(input_path, source)
inference = InferenceResult(
target_width=3,
target_height=3,
phase_x=0.2,
phase_y=-0.2,
confidence=1.0,
top_candidates=[
InferenceCandidate(target_width=3, target_height=3, phase_x=0.2, phase_y=-0.2, score=0.9, breakdown={})
],
)
class DummyAnalysis:
edge_map = np.zeros((6, 6), dtype=np.float32)
def fail_infer(*args, **kwargs):
raise AssertionError("searched lattice inference should not run in autocorr mode")
def fake_infer_autocorr(*args, **kwargs):
assert kwargs["max_target_size"] == 5
return inference
def fake_optimize_phase_field(source_rgba, inference, analysis, steps, seed, device, solver_params=None):
rgba = np.zeros((inference.target_height, inference.target_width, 4), dtype=np.float32)
rgba[..., 3] = 1.0
return SolverArtifacts(
target_rgba=rgba,
uv_field=np.zeros((inference.target_height, inference.target_width, 2), dtype=np.float32),
guidance_strength=np.zeros((inference.target_height, inference.target_width), dtype=np.float32),
initial_rgba=rgba.copy(),
loss_history=[],
)
monkeypatch.setattr("repixelizer.pipeline.infer_lattice", fail_infer)
monkeypatch.setattr("repixelizer.pipeline.infer_autocorr_lattice", fake_infer_autocorr)
monkeypatch.setattr("repixelizer.pipeline.analyze_phase_field_source", lambda *args, **kwargs: DummyAnalysis())
monkeypatch.setattr("repixelizer.pipeline.optimize_phase_field", fake_optimize_phase_field)
result = run_pipeline(
input_path,
output_path,
enable_phase_rerank=False,
lattice_inference_mode="autocorr",
max_inferred_target_size=5,
steps=0,
device="cpu",
)
assert output_path.exists()
assert result.inference.target_width == 3
assert result.inference.target_height == 3
assert result.inference.phase_x == 0.2
assert result.inference.phase_y == -0.2
def test_phase_rerank_can_accept_low_confidence_size_jump_with_strong_support(monkeypatch) -> None:
source = np.zeros((16, 16, 4), dtype=np.float32)
candidate_a = InferenceCandidate(target_width=16, target_height=16, phase_x=0.0, phase_y=0.0, score=0.91, breakdown={})