GradLLM / config.py
johnbridges's picture
.
b2c2f23
raw
history blame
1.68 kB
# 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()