Cursor Agent AHAD commited on
Load .env automatically for Supabase and backend settings
Browse filesCo-authored-by: AHAD <ahad-m@users.noreply.github.com>
Files changed (2)
2 unmodified lines3456789103 unmodified lines1415161718192012 unmodified lines33343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778792 unmodified linesfrom pathlib import Pathdef _resolve_constants_path() -> str:env_path = os.getenv("DIAC_CONSTANTS_PATH") or os.getenv("CONSTANTS_PATH")if env_path:return env_path if env_path.endswith("/") else f"{env_path}/"3 unmodified linesdef _resolve_asr_device() -> str:env = os.getenv("ASR_DEVICE", "cpu").lower()if env == "auto":try:import torch12 unmodified linesclass Settings:def __init__(self) -> None:self.model_name: str = (os.getenv("MODEL_NAME")or os.getenv("DIAC_MODEL_ID")or "rufaelfekadu/diac-transformer-text-asr-tashkeela-clartts")self.constants_path: str = _resolve_constants_path()self.api_key: str | None = os.getenv("API_KEY") or os.getenv("DIAC_API_KEY")self.port: int = int(os.getenv("PORT", "8000"))self.max_chars: int = int(os.getenv("DIAC_MAX_CHARS", "5000"))self.max_batch_size: int = int(os.getenv("DIAC_MAX_BATCH_SIZE", "100"))self.log_level: str = os.getenv("LOG_LEVEL", "INFO").upper()self.asr_model_name: str = os.getenv("ASR_MODEL_NAME","sashat/whisper-medium-ClassicalAr",)self.asr_device: str = _resolve_asr_device()self.max_audio_bytes: int = int(os.getenv("MAX_AUDIO_MB", "25")) * 1024 * 1024self.allowed_audio_extensions: set[str] = {ext.strip().lower()for ext in os.getenv("ALLOWED_AUDIO_EXTENSIONS",".wav,.mp3,.flac,.ogg,.m4a,.aac,.webm",).split(",")if ext.strip()}self.supabase_url: str | None = os.getenv("SUPABASE_URL") or Noneself.supabase_service_role_key: str | None = (os.getenv("SUPABASE_SERVICE_ROLE_KEY") or None)self.supabase_storage_bucket: str = os.getenv("SUPABASE_STORAGE_BUCKET", "audio-recordings")self.supabase_table_name: str = os.getenv("SUPABASE_TABLE_NAME", "diacritization_records")cors_raw = os.getenv("CORS_ORIGINS","http://localhost:5173,http://localhost:3000,http://127.0.0.1:5173",)self.cors_origins: list[str] = [origin.strip() for origin in cors_raw.split(",") if origin.strip()]2 unmodified lines3456789101112131415161718192021222324252627282930313233343 unmodified lines3839404142434412 unmodified lines575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001012 unmodified linesfrom pathlib import Pathdef _load_env_file() -> None:"""Load project-root .env so Supabase and other settings work without manual export."""try:from dotenv import load_dotenvexcept ImportError:returnproject_root = Path(__file__).resolve().parents[2]env_path = project_root / ".env"if env_path.exists():load_dotenv(env_path, override=False)_load_env_file()def _env(name: str, default: str | None = None) -> str | None:value = os.getenv(name, default)if value is None:return Nonevalue = value.strip()return value or Nonedef _resolve_constants_path() -> str:env_path = _env("DIAC_CONSTANTS_PATH") or _env("CONSTANTS_PATH")if env_path:return env_path if env_path.endswith("/") else f"{env_path}/"3 unmodified linesdef _resolve_asr_device() -> str:env = (_env("ASR_DEVICE", "cpu") or "cpu").lower()if env == "auto":try:import torch12 unmodified linesclass Settings:def __init__(self) -> None:self.model_name: str = (_env("MODEL_NAME")or _env("DIAC_MODEL_ID")or "rufaelfekadu/diac-transformer-text-asr-tashkeela-clartts")self.constants_path: str = _resolve_constants_path()self.api_key: str | None = _env("API_KEY") or _env("DIAC_API_KEY")self.port: int = int(_env("PORT", "8000") or "8000")self.max_chars: int = int(_env("DIAC_MAX_CHARS", "5000") or "5000")self.max_batch_size: int = int(_env("DIAC_MAX_BATCH_SIZE", "100") or "100")self.log_level: str = (_env("LOG_LEVEL", "INFO") or "INFO").upper()self.asr_model_name: str = _env("ASR_MODEL_NAME","sashat/whisper-medium-ClassicalAr",) or "sashat/whisper-medium-ClassicalAr"self.asr_device: str = _resolve_asr_device()self.max_audio_bytes: int = int(_env("MAX_AUDIO_MB", "25") or "25") * 1024 * 1024self.allowed_audio_extensions: set[str] = {ext.strip().lower()for ext in (_env("ALLOWED_AUDIO_EXTENSIONS",".wav,.mp3,.flac,.ogg,.m4a,.aac,.webm",) or "").split(",")if ext.strip()}self.supabase_url: str | None = _env("SUPABASE_URL")self.supabase_service_role_key: str | None = _env("SUPABASE_SERVICE_ROLE_KEY")self.supabase_storage_bucket: str = (_env("SUPABASE_STORAGE_BUCKET", "audio-recordings") or "audio-recordings")self.supabase_table_name: str = (_env("SUPABASE_TABLE_NAME", "diacritization_records") or "diacritization_records")cors_raw = _env("CORS_ORIGINS","http://localhost:5173,http://localhost:3000,http://127.0.0.1:5173",) or ""self.cors_origins: list[str] = [origin.strip() for origin in cors_raw.split(",") if origin.strip()]
37 unmodified lines38394041424337 unmodified linesexcept Exception as exc:logger.exception("Failed to load model at startup: %s", exc)raiseyield37 unmodified lines3839404142434445464748495037 unmodified linesexcept Exception as exc:logger.exception("Failed to load model at startup: %s", exc)raiseif settings.supabase_enabled:logger.info("Supabase save enabled (bucket=%s)", settings.supabase_storage_bucket)else:logger.warning("Supabase save disabled. Set SUPABASE_URL and SUPABASE_SERVICE_ROLE_KEY in .env")yield
backend/app/config.py+46-24
backend/app/main.py+7