Spaces:
Sleeping
Sleeping
| # app/config.py | |
| """ | |
| Application configuration using Pydantic BaseSettings. | |
| All configuration MUST be provided via environment variables or the .env file. | |
| """ | |
| import json | |
| from pydantic import Field, field_validator | |
| from pydantic_settings import BaseSettings, SettingsConfigDict | |
| from typing import List, Optional, Any | |
| class Settings(BaseSettings): | |
| # ----------------------------- | |
| # Security / Access | |
| # ----------------------------- | |
| EMBED_API_KEY: str = Field(..., validation_alias="EMBED_API_KEY") | |
| HUGGING_FACE_TOKEN: str = Field(..., validation_alias="HUGGING_FACE_TOKEN") | |
| # ----------------------------- | |
| # Model & Device Settings | |
| # ----------------------------- | |
| MODEL_NAME: str = Field(..., validation_alias="MODEL_NAME") | |
| DEVICE: Optional[str] = Field(..., validation_alias="DEVICE") # Can be empty in .env for auto-detection | |
| BATCH_SIZE: int = Field(8, validation_alias="BATCH_SIZE") | |
| MAX_LENGTH: int = Field(1024, validation_alias="MAX_LENGTH") | |
| # ----------------------------- | |
| # Server & Reliability | |
| # ----------------------------- | |
| RETRY_ATTEMPTS: int = Field(3, validation_alias="RETRY_ATTEMPTS") | |
| RETRY_BACKOFF_SECONDS: float = Field(2.0, validation_alias="RETRY_BACKOFF_SECONDS") | |
| HOST: str = Field("0.0.0.0", validation_alias="HOST") | |
| PORT: int = Field(7860, validation_alias="PORT") | |
| WORKERS: int = Field(1, validation_alias="WORKERS") | |
| # ----------------------------- | |
| # Logging | |
| # ----------------------------- | |
| LOG_LEVEL: str = Field("INFO", validation_alias="LOG_LEVEL") | |
| # Pydantic Settings Configuration | |
| model_config = SettingsConfigDict( | |
| env_file=".env", | |
| env_file_encoding="utf-8", | |
| extra="ignore" | |
| ) | |
| # Load settings (Strict mode: will raise error if any key is missing) | |
| try: | |
| settings = Settings() | |
| # ------------------------------------------------------------------ | |
| # Explicitly set HF environment variables so all HF-based libraries | |
| # (transformers, huggingface_hub, etc.) pick up the token globally. | |
| # If this not done then hugging face token will not work | |
| # ------------------------------------------------------------------ | |
| import os | |
| if settings.HUGGING_FACE_TOKEN: | |
| os.environ["HF_TOKEN"] = settings.HUGGING_FACE_TOKEN | |
| os.environ["HUGGINGFACE_HUB_TOKEN"] = settings.HUGGING_FACE_TOKEN | |
| except Exception as e: | |
| import sys | |
| print(f"\n[CRITICAL ERROR] Configuration failed to load from environment/.env:") | |
| print(f"Missing or invalid keys: {e}") | |
| print("\nPlease ensure your .env file is complete according to .env.example\n") | |
| sys.exit(1) | |