Spaces:
Paused
Paused
Download core/config.py from TeamError404/nexus: direct link, hf CLI and curl.
- Browser
- Download file 5.11 kB
-
https://huggingface.co/spaces/TeamError404/nexus/resolve/main/core/config.py
- Command line
-
hf download hf://spaces/TeamError404/nexus/core/config.py
-
curl -L -o config.py https://huggingface.co/spaces/TeamError404/nexus/resolve/main/core/config.py
5.11 kB
| """ | |
| Application configuration loaded from environment variables. | |
| Uses pydantic-settings for type-safe config with .env file support. | |
| """ | |
| import os | |
| from pydantic_settings import BaseSettings | |
| from pydantic import field_validator | |
| from functools import lru_cache | |
| class Settings(BaseSettings): | |
| """Application settings from environment variables.""" | |
| # App | |
| APP_NAME: str = "Nexus Master Gateway" | |
| APP_VERSION: str = "1.0.0" | |
| DEBUG: bool = False | |
| API_V1_STR: str = "/api/v1" | |
| # Super Admin Configuration | |
| SUPER_ADMIN_EMAIL: str = "admin@3sc.com" | |
| SUPER_ADMIN_NAME: str = "Nexus Super Admin" | |
| # Database | |
| DATABASE_URL: str | None = None | |
| DB_USER: str = "postgres" | |
| DB_PASS: str = "postgres" | |
| DB_HOST: str = "localhost" | |
| DB_PORT: int = 5432 | |
| DB_NAME: str = "customer_portal" | |
| def async_database_url(self) -> str: | |
| """Constructs or ensures the +asyncpg prefix exists for SQLAlchemy.""" | |
| # Priority: Explicit environment variable DATABASE_URL (Cleaned) | |
| url = self.DATABASE_URL.strip() if self.DATABASE_URL else None | |
| if url: | |
| if "localhost" in url or "127.0.0.1" in url: | |
| # If we are in the cloud (Linux) but hitting localhost, something is wrong | |
| import platform | |
| if platform.system() != "Windows": | |
| print("--- WARNING: CLOUD ENVIRONMENT DETECTED LOCALHOST DB ---") | |
| if url.startswith("postgresql://"): | |
| return url.replace("postgresql://", "postgresql+asyncpg://", 1) | |
| return url | |
| # Fallback: Construct from individual parts | |
| return f"postgresql+asyncpg://{self.DB_USER}:{self.DB_PASS}@{self.DB_HOST}:{self.DB_PORT}/{self.DB_NAME}" | |
| def clean_redis_url(self) -> str: | |
| """Returns a sanitized Redis URL (no trailing newlines or spaces).""" | |
| return self.REDIS_URL.strip().replace("\n", "").replace("\r", "") | |
| def database_host(self) -> str: | |
| """Returns the hostname of the database for diagnostic logs.""" | |
| if self.DATABASE_URL: | |
| try: | |
| from urllib.parse import urlparse | |
| return urlparse(self.DATABASE_URL).hostname or "unspecified" | |
| except: return "error-parsing" | |
| return self.DB_HOST | |
| REDIS_URL: str = "redis://localhost:6379" | |
| CACHE_REDIS_URL: str = "redis://localhost:6379" | |
| # JWT Auth | |
| SECRET_KEY: str = "your-secret-key-change-in-production" | |
| ALGORITHM: str = "HS256" | |
| ACCESS_TOKEN_EXPIRE_MINUTES: int = 30 | |
| REFRESH_TOKEN_EXPIRE_DAYS: int = 7 | |
| # AI Settings (100% Ollama-Only Transition) | |
| AI_PROVIDER: str = "ollama" | |
| AI_MODEL: str = "kimi-k2.5:cloud" | |
| # AI Providers (Hybrid Switch) | |
| AI_IMAGE_PROVIDER: str = "local" | |
| AI_TEXT_PROVIDER: str = "ollama" | |
| # Decoupled Embeddings | |
| AI_TEXT_EMBEDDING_PROVIDER: str = "google" # options: "google", "ollama", "openai" | |
| AI_TEXT_EMBEDDING_MODEL: str = "models/embedding-001" | |
| AI_TEXT_EMBEDDING_DIM: int = 768 | |
| AI_IMAGE_EMBEDDING_MODEL: str = "clip-ViT-B-32" | |
| AI_IMAGE_EMBEDDING_DIM: int = 512 | |
| # Vector Safety | |
| AI_FORCE_DIMENSION_COMPATIBILITY: bool = True | |
| # Deprecated (Keeping for soft migration/compat) | |
| AI_EMBEDDING_MODEL: str = "nomic-embed-text:latest" | |
| AI_EMBEDDING_DIM: int = 768 | |
| OLLAMA_BASE_URL: str = "http://host.docker.internal:11434" | |
| def sanitize_ollama_url(cls, v: str) -> str: | |
| """Removes trailing slashes to prevent double-slash API paths.""" | |
| if v and v.endswith("/"): | |
| return v.rstrip("/") | |
| return v | |
| # Cloud AI Providers | |
| GROQ_API_KEY: str | None = None | |
| OPENAI_API_KEY: str | None = None | |
| ANTHROPIC_API_KEY: str | None = None | |
| GOOGLE_API_KEY: str | None = None | |
| # LangGraph Settings | |
| LANGCHAIN_TRACING_V2: bool = False | |
| LANGCHAIN_API_KEY: str | None = None | |
| LANGCHAIN_PROJECT: str = "customer-portal" | |
| # Storage & Logging Settings | |
| STORAGE_BACKEND: str = "local" # options: "local", "s3" | |
| UPLOAD_DIR: str = "uploads" | |
| LOG_DIR: str = "logs" | |
| # S3 Configuration (Required if STORAGE_BACKEND="s3") | |
| S3_ACCESS_KEY: str | None = None | |
| S3_SECRET_KEY: str | None = None | |
| S3_BUCKET: str | None = None | |
| S3_REGION: str = "us-east-1" | |
| S3_ENDPOINT_URL: str | None = None # For Cloudflare R2 or Minio | |
| class Config: | |
| case_sensitive = True | |
| env_file = ".env" | |
| extra = "ignore" # Add this to prevent future validation errors from extra env vars | |
| def get_settings() -> Settings: | |
| return Settings() | |
| def get_redis_settings() -> "RedisSettings": | |
| """Returns hardened RedisSettings for Upstash/Cloud stability.""" | |
| from arq.connections import RedisSettings | |
| settings = get_settings() | |
| rs = RedisSettings.from_dsn(settings.clean_redis_url) | |
| rs.conn_timeout = 60 | |
| rs.conn_retries = 20 | |
| rs.socket_keepalive = True | |
| rs.retry_on_timeout = True | |
| return rs | |