File size: 1,680 Bytes
b2c2f23 bf292d9 b2c2f23 bf292d9 b2c2f23 bf292d9 b2c2f23 bf292d9 b2c2f23 bf292d9 b2c2f23 bf292d9 b2c2f23 |
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 |
# app/config.py
import os
from functools import lru_cache
# v2-first import, fallback to v1
try:
from pydantic_settings import BaseSettings, SettingsConfigDict # pydantic v2
from pydantic import AnyUrl, Field
IS_V2 = True
except Exception:
from pydantic import BaseSettings, AnyUrl # pydantic v1
from pydantic import Field
IS_V2 = False
class Settings(BaseSettings):
# Read from env: set this in your Space Secrets as AMQP_URL
AMQP_URL: AnyUrl = Field(..., description="amqps://user:pass@host:5671/%2F?heartbeat=30")
RABBIT_INSTANCE_NAME: str = "prod"
RABBIT_EXCHANGE_TYPE: str = "topic"
RABBIT_ROUTING_KEY: str = ""
RABBIT_PREFETCH: int = 1
SERVICE_ID: str = "gradllm"
USE_TLS: bool = True
# Optional: prefix->type map, e.g. {"llmStartSession": "topic"}
EXCHANGE_TYPES: dict[str, str] = {}
if IS_V2:
# pydantic v2
model_config = SettingsConfigDict(
case_sensitive=True,
env_file=".env", # for local dev; ignored on HF unless you commit it
env_file_encoding="utf-8",
)
else:
# pydantic v1
class Config:
case_sensitive = True
env_file = ".env"
env_file_encoding = "utf-8"
@lru_cache
def get_settings() -> Settings:
try:
return Settings()
except Exception as e:
# Friendlier message when AMQP_URL is missing/invalid
raise RuntimeError(
"AMQP_URL is not set or invalid. In your Hugging Face Space, add a Secret "
"named AMQP_URL (e.g. amqps://user:pass@host:5671/%2F?heartbeat=30)."
) from e
settings = get_settings()
|