Cleanup/code quality #1

Merged
froxxxy merged 9 commits from cleanup/code-quality into main 2026-07-18 17:26:22 +02:00
5 changed files with 38 additions and 15 deletions
Showing only changes of commit 5a3118f590 - Show all commits
+5 -1
View File
@@ -9,6 +9,8 @@ from __future__ import annotations
import os import os
from pathlib import Path from pathlib import Path
from .env_utils import env_bool
def _int_env(name: str, default: int) -> int: def _int_env(name: str, default: int) -> int:
try: try:
@@ -55,13 +57,15 @@ MAX_FILTER_ATTEMPTS = _int_env("MAX_FILTER_ATTEMPTS", 8)
# Longest edge after preprocess (ImageMagick). Keeps rembg/gmic sane on # Longest edge after preprocess (ImageMagick). Keeps rembg/gmic sane on
# large camera JPEGs. # large camera JPEGs.
MAX_EDGE_PX = _int_env("MAX_EDGE_PX", 2000) MAX_EDGE_PX = _int_env("MAX_EDGE_PX", 2000)
# ImageMagick preprocess step timeout (resize + orient).
PREPROCESS_TIMEOUT = _int_env("PREPROCESS_TIMEOUT", 120)
# Generous on purpose: the very first rembg call also downloads the model, # Generous on purpose: the very first rembg call also downloads the model,
# which can take a while depending on the link. # which can take a while depending on the link.
REMBG_TIMEOUT = _int_env("REMBG_TIMEOUT", 600) REMBG_TIMEOUT = _int_env("REMBG_TIMEOUT", 600)
# birefnet-general OOMs on the ~8GB event box; u2net + alpha matting fits # birefnet-general OOMs on the ~8GB event box; u2net + alpha matting fits
# and is the practical equivalent of `rembg i -a`. # and is the practical equivalent of `rembg i -a`.
REMBG_MODEL = os.environ.get("REMBG_MODEL", "u2net") REMBG_MODEL = os.environ.get("REMBG_MODEL", "u2net")
REMBG_ALPHA = os.environ.get("REMBG_ALPHA", "1").strip().lower() in {"1", "true", "yes", "on"} REMBG_ALPHA = env_bool("REMBG_ALPHA", True)
NICE_LEVEL = _int_env("NICE_LEVEL", 18) NICE_LEVEL = _int_env("NICE_LEVEL", 18)
# gmic CLI binary, override for local dev if not on PATH. rembg has no # gmic CLI binary, override for local dev if not on PATH. rembg has no
+12
View File
@@ -0,0 +1,12 @@
"""Shared environment-variable parsing."""
from __future__ import annotations
import os
def env_bool(name: str, default: bool) -> bool:
raw = os.environ.get(name)
if raw is None:
return default
return raw.strip().lower() in {"1", "true", "yes", "on"}
+11
View File
@@ -0,0 +1,11 @@
"""Shared logging setup for entry-point modules."""
from __future__ import annotations
import logging
_LOG_FORMAT = "%(asctime)s %(levelname)s [%(name)s] %(message)s"
def configure_logging(level: int = logging.INFO) -> None:
logging.basicConfig(level=level, format=_LOG_FORMAT)
+8 -7
View File
@@ -14,8 +14,9 @@ from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates from fastapi.templating import Jinja2Templates
from . import config, pipeline, remix from . import config, pipeline, remix
from .logging_config import configure_logging
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s [%(name)s] %(message)s") configure_logging()
logger = logging.getLogger("livef12.web") logger = logging.getLogger("livef12.web")
app = FastAPI(title=config.SITE_TITLE) app = FastAPI(title=config.SITE_TITLE)
@@ -63,15 +64,15 @@ def _safe_job_file(job_id: str, filename: str) -> Path:
def list_jobs() -> list[dict[str, Any]]: def list_jobs() -> list[dict[str, Any]]:
jobs = [] jobs = []
for job_id in pipeline.list_job_ids(): for job_id in pipeline.list_job_ids():
status = pipeline.read_status(job_id) or {} data = pipeline.read_meta(job_id) or {}
manifest = pipeline.read_manifest(job_id) or {} variants = data.get("variants") or []
jobs.append( jobs.append(
{ {
"job_id": job_id, "job_id": job_id,
"status": status.get("status", "unknown"), "status": data.get("status", "unknown"),
"created_at": status.get("created_at") or manifest.get("created_at") or "", "created_at": data.get("created_at") or "",
"variant_count": len(manifest.get("variants", [])), "variant_count": len(variants),
"thumbnail": (manifest.get("variants") or [{}])[-1].get("file") if manifest.get("variants") else None, "thumbnail": variants[-1].get("file") if variants else None,
} }
) )
jobs.sort(key=lambda j: j["created_at"] or j["job_id"], reverse=True) jobs.sort(key=lambda j: j["created_at"] or j["job_id"], reverse=True)
+2 -7
View File
@@ -18,12 +18,7 @@ import sys
import traceback import traceback
from pathlib import Path from pathlib import Path
from .env_utils import env_bool
def _env_bool(name: str, default: bool) -> bool:
raw = os.environ.get(name)
if raw is None:
return default
return raw.strip().lower() in {"1", "true", "yes", "on"}
def main() -> int: def main() -> int:
@@ -42,7 +37,7 @@ def main() -> int:
output_path.parent.mkdir(parents=True, exist_ok=True) output_path.parent.mkdir(parents=True, exist_ok=True)
model = os.environ.get("REMBG_MODEL", "u2net") model = os.environ.get("REMBG_MODEL", "u2net")
alpha = _env_bool("REMBG_ALPHA", True) alpha = env_bool("REMBG_ALPHA", True)
session = new_session(model) session = new_session(model)
output_path.write_bytes( output_path.write_bytes(
remove( remove(