File size: 2,736 Bytes
ec855e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# 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)