hf-image-api / config.py
kajalan's picture
Upload 14 files (#1)
5738ae5
Raw History Blame Contribute Delete
3 kB
"""
config.py β€” All runtime settings loaded from environment / .env file.
"""
from pydantic_settings import BaseSettings
from pydantic import Field
import os
class Settings(BaseSettings):
# ── Server ────────────────────────────────────────────────────────────────
host: str = Field("0.0.0.0", env="HOST")
port: int = Field(8000, env="PORT")
debug: bool = Field(False, env="DEBUG")
# ── Model ─────────────────────────────────────────────────────────────────
# Best CPU models (small β†’ fast):
# "nota-ai/bk-sdm-small" (~500 MB, very fast on CPU)
# "runwayml/stable-diffusion-v1-5" (~4 GB, standard quality)
# "stabilityai/stable-diffusion-2-1-base" (~5 GB, higher quality)
model_id: str = Field(
"nota-ai/bk-sdm-small",
env="MODEL_ID",
)
model_cache_dir: str = Field("./model_cache", env="MODEL_CACHE_DIR")
# Load the pipeline at startup (recommended β€” avoids cold-start on first job)
auto_load_model: bool = Field(True, env="AUTO_LOAD_MODEL")
# ── CPU Optimisation ──────────────────────────────────────────────────────
# Number of CPU threads for PyTorch inference (0 = use all logical cores)
cpu_threads: int = Field(0, env="CPU_THREADS")
# Enable attention slicing (reduces peak RAM at slight speed cost)
enable_attention_slicing: bool = Field(True, env="ENABLE_ATTENTION_SLICING")
# Default inference steps (DPM++ scheduler works well at 20 steps on CPU)
default_steps: int = Field(20, env="DEFAULT_STEPS")
max_steps: int = Field(50, env="MAX_STEPS")
# Default image dimensions (512Γ—512 is ideal for SD v1.x on CPU)
default_width: int = Field(512, env="DEFAULT_WIDTH")
default_height: int = Field(512, env="DEFAULT_HEIGHT")
max_width: int = Field(768, env="MAX_WIDTH")
max_height: int = Field(768, env="MAX_HEIGHT")
# Default guidance scale
default_guidance_scale: float = Field(7.5, env="DEFAULT_GUIDANCE_SCALE")
# ── Storage ───────────────────────────────────────────────────────────────
outputs_dir: str = Field("./outputs", env="OUTPUTS_DIR")
# Auto-delete completed jobs after N seconds (0 = never)
job_ttl_seconds: int = Field(0, env="JOB_TTL_SECONDS")
class Config:
env_file = ".env"
env_file_encoding = "utf-8"
settings = Settings()
# Resolve absolute output path
os.makedirs(settings.outputs_dir, exist_ok=True)
os.makedirs(settings.model_cache_dir, exist_ok=True)