Files
reTraining/backend/config.py
T

61 lines
2.3 KiB
Python

"""Paths and settings. Everything is environment-driven so the same image runs
locally and in Docker without code changes (REQ-072)."""
import os
from dotenv import load_dotenv
load_dotenv()
# huggingface_hub reads HF_TOKEN / HUGGING_FACE_HUB_TOKEN; accept either name in
# .env so pasting a token under the obvious name just works.
if os.environ.get("HF_TOKEN") and not os.environ.get("HUGGING_FACE_HUB_TOKEN"):
os.environ["HUGGING_FACE_HUB_TOKEN"] = os.environ["HF_TOKEN"]
REPO_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DATA_DIR = os.path.abspath(os.environ.get("APP_DATA_DIR", os.path.join(REPO_ROOT, "data")))
PROJECTS_DIR = os.path.join(DATA_DIR, "projects")
DB_PATH = os.path.join(DATA_DIR, "app.db")
# Where the video archive is mounted. Projects store a path relative to nothing —
# they store an absolute one — but this is the default the UI starts browsing from.
VIDEO_ROOT = os.path.abspath(os.environ.get("VIDEO_ARCHIVE", os.path.join(DATA_DIR, "archive")))
def archive_host_paths() -> dict:
"""Where the archive sits on the host — for the copy-path buttons (REQ-172).
ponytail: relative VIDEO_ARCHIVE_HOST (./data/archive) cannot be resolved
inside the container; set it absolute in .env or the buttons stay hidden."""
import re
host = os.environ.get("VIDEO_ARCHIVE_HOST", "")
if os.path.isabs(host):
linux = host.rstrip("/")
elif VIDEO_ROOT != "/videos": # bare-metal: VIDEO_ROOT is already the host path
linux = VIDEO_ROOT
else:
return {"linux": None, "windows": None}
if re.match(r"^[A-Za-z]:[\\/]", linux):
return {"linux": None, "windows": linux.replace("/", "\\")}
distro = os.environ.get("WSL_DISTRO_NAME", "")
windows = f"\\\\wsl.localhost\\{distro}\\" + linux.lstrip("/").replace("/", "\\") if distro else None
return {"linux": linux, "windows": windows}
# Vite dev server needs cross-origin access; in Docker nginx proxies /api and
# this is irrelevant.
CORS_ORIGINS = [
origin.strip()
for origin in os.environ.get("CORS_ORIGINS", "http://localhost:5173").split(",")
if origin.strip()
]
def ensure_dirs() -> None:
os.makedirs(PROJECTS_DIR, exist_ok=True)
def project_dir(slug: str) -> str:
return os.path.join(PROJECTS_DIR, slug)