Spaces:
Sleeping
Sleeping
| from typing import List, Tuple, Type | |
| from functools import lru_cache | |
| from pydantic import Field | |
| from pydantic_settings import ( | |
| BaseSettings, | |
| PydanticBaseSettingsSource, | |
| SettingsConfigDict, | |
| YamlConfigSettingsSource, | |
| ) | |
| class Settings(BaseSettings): | |
| """Centralized settings. | |
| Priority (highest β lowest): | |
| 1. Environment variables | |
| 2. .env file β secrets only (MISTRAL_API_KEY) | |
| 3. development.yml β non-secret config (model, server, CORS) | |
| """ | |
| model_config = SettingsConfigDict( | |
| env_file=".env", | |
| env_file_encoding="utf-8", | |
| case_sensitive=True, | |
| extra="ignore", | |
| ) | |
| def settings_customise_sources( | |
| cls, | |
| settings_cls: Type[PydanticBaseSettingsSource], | |
| init_settings: PydanticBaseSettingsSource, | |
| env_settings: PydanticBaseSettingsSource, | |
| dotenv_settings: PydanticBaseSettingsSource, | |
| file_secret_settings: PydanticBaseSettingsSource, | |
| ) -> Tuple[PydanticBaseSettingsSource, ...]: | |
| return ( | |
| init_settings, | |
| env_settings, | |
| dotenv_settings, | |
| YamlConfigSettingsSource(settings_cls, yaml_file="development.yml"), | |
| file_secret_settings, | |
| ) | |
| # ββ Mistral (secret in .env, rest in development.yml) βββββββββββββββββββββ | |
| MISTRAL_API_KEY: str | |
| MISTRAL_OCR_MODEL: str = "mistral-ocr-latest" | |
| MISTRAL_TABLE_FORMAT: str = "markdown" | |
| # ββ Server (development.yml) βββββββββββββββββββββββββββββββββββββββββββββββ | |
| APP_NAME: str = "Markdown & Layout Extractor" | |
| HOST: str = "127.0.0.1" | |
| PORT: int = 7860 # HF Spaces requires 7860; development.yml / env var can override | |
| LOG_LEVEL: str = "INFO" | |
| # ββ CORS (development.yml) βββββββββββββββββββββββββββββββββββββββββββββββββ | |
| CORS_ALLOW_ORIGINS: List[str] = Field(default_factory=lambda: ["*"]) | |
| CORS_ALLOW_METHODS: List[str] = Field(default_factory=lambda: ["*"]) | |
| CORS_ALLOW_HEADERS: List[str] = Field(default_factory=lambda: ["*"]) | |
| def get_settings() -> Settings: | |
| """Cached settings instance β call this everywhere instead of instantiating.""" | |
| return Settings() | |
| settings = get_settings() | |