Files
masi 26845081bc feat: prefer the machine you own, and keep the cluster card as overflow
The box list is read in order and the first one that can take the work draws it, so
the 4070 Super at home goes first and the shared 3090 covers for it when that machine
is off, asleep, or busy with a game. Reversing the two entries is all it would take
to change that.
2026-10-04 15:54:13 -03:00

91 lines
4.9 KiB
Python

"""Settings from the environment and small shared helpers."""
import hashlib
import json
import os
from pathlib import Path
HERE = Path(__file__).resolve().parent
DATA = Path(os.environ.get("DATA_DIR", "/data"))
DB = DATA / "illustrator.db"
IMAGES = DATA / "images"
STYLES = DATA / "styles"
CHARACTERS = DATA / "characters"
# Language models, tried in order: a request goes to the first provider that can serve it
# (vision requests skip providers without a vision model) and falls through to the next when
# that provider fails. Override the whole list with LLM_PROVIDERS (a JSON array of objects
# like the defaults below); keys are read from the environment variable named in "key_env".
# api "anthropic": an Anthropic Messages endpoint (MiniMax through Headroom)
# api "ollama": Ollama's /api/chat (Ollama Cloud)
# "think": "off" is passed through as a request asking the model not to reason out loud.
DEFAULT_PROVIDERS = [
{"name": "minimax", "api": "anthropic", "key_env": "MINIMAX_API_KEY",
"url": os.environ.get("MINIMAX_URL", "http://100.117.115.61:8789/v1/messages"),
"text_model": os.environ.get("MINIMAX_TEXT_MODEL", "MiniMax-M2.7"),
"vision_model": os.environ.get("MINIMAX_VISION_MODEL", "MiniMax-M3"),
# M2.7 thinks out loud, and a long cast list spent the whole budget reasoning and
# returned no text at all. Asked not to, it answers in a fraction of the time.
"think": "off"},
{"name": "ollama-cloud", "api": "ollama", "key_env": "OLLAMA_API_KEY",
"url": os.environ.get("OLLAMA_URL", "https://ollama.com/api/chat"),
"text_model": os.environ.get("OLLAMA_MODEL", "glm-5.3-flash"),
"vision_model": os.environ.get("OLLAMA_VISION_MODEL", os.environ.get("OLLAMA_MODEL", "glm-5.3-flash"))},
]
def _providers():
listed = json.loads(os.environ["LLM_PROVIDERS"]) if os.environ.get("LLM_PROVIDERS") else DEFAULT_PROVIDERS
out = []
for p in listed:
if p.get("api") not in ("anthropic", "ollama") or not p.get("url") or not p.get("text_model"):
raise ValueError(f"LLM provider {p.get('name')!r} needs api (anthropic|ollama), url and text_model")
key_value = os.environ.get(p.get("key_env", ""), "")
if key_value: # a provider without a key is simply not configured
out.append({**p, "key": key_value})
if not out:
raise RuntimeError("no LLM provider configured: set MINIMAX_API_KEY and/or OLLAMA_API_KEY")
return out
LLM_PROVIDERS = _providers()
COMFYUI_URL = os.environ.get("COMFYUI_URL", "http://comfyui.comfyui.svc.cluster.local:8188")
# Every box the renders may be handed to, in order of preference: the first that can take the
# work draws it. One is the normal case and COMFYUI_URL is then the only entry; a second
# machine is overflow rather than a second code path, since the dispatcher is what steps over
# a box that is down or out of memory.
COMFYUI_URLS = [u.strip() for u in os.environ.get("COMFYUI_URLS", COMFYUI_URL).split(",") if u.strip()]
DISPATCHER = os.environ.get("DISPATCHER", "/app/comfyui/scripts/generate_asset.py")
READER_JS = Path(os.environ.get("READER_JS", HERE / "reader.js"))
# Style presets: a ConfigMap of its own in the pod (keys cannot hold folders).
STYLE_LIBRARY = Path(os.environ.get("STYLE_LIBRARY", HERE / "style-library"))
# Benchmark and research pages served at /research (copied to the data volume by deploy.sh).
RESEARCH = DATA / "research"
# Shared secret for every endpoint but /healthz: the service is reachable from the internet.
ACCESS_KEY = os.environ["ACCESS_KEY"]
# The owner: the master ACCESS_KEY signs in as this reader (existing snippet and links).
OWNER = os.environ.get("OWNER", "masi")
# Who may sign in with Gitea; removing a name revokes that person's key at once.
ALLOWED_USERS = {u.strip().lower() for u in os.environ.get("ALLOWED_USERS", OWNER).split(",") if u.strip()}
# Gitea OAuth2: the public URL for the browser redirect, the internal one for server calls.
GITEA_URL = os.environ.get("GITEA_URL", "https://git.marcospaulo.dev.br")
GITEA_INTERNAL_URL = os.environ.get("GITEA_INTERNAL_URL", "http://gitea-http.gitea.svc.cluster.local:3000")
GITEA_CLIENT_ID = os.environ.get("GITEA_CLIENT_ID", "")
GITEA_CLIENT_SECRET = os.environ.get("GITEA_CLIENT_SECRET", "")
PUBLIC_URL = os.environ.get("PUBLIC_URL", "https://illustrate.marcospaulo.dev.br")
# Kavita, for reading chapters server-side (prefetch and pre-reading). Optional.
KAVITA_URL = os.environ.get("KAVITA_URL", "http://kavita.kavita.svc.cluster.local:5000")
KAVITA_USER = os.environ.get("KAVITA_USER", "")
KAVITA_PASSWORD = os.environ.get("KAVITA_PASSWORD", "")
WEBP_QUALITY = 85 # ~10x smaller than the PNG, no visible loss on a phone
MAX_TEXT = 14_000 # characters of chapter text sent to the LLM
def key(*parts):
return hashlib.sha256("\x1f".join(parts).encode()).hexdigest()[:32]
for folder in (IMAGES, STYLES, CHARACTERS):
folder.mkdir(parents=True, exist_ok=True)