ags / utils /secrets_utils.py
Mark Cuizon
fix: restore Hugging Face Space build and startup reliability
c9a0fae
Raw History Blame Contribute Delete
1.06 kB
"""Safe access helpers for Streamlit secrets on Hugging Face Spaces."""
from __future__ import annotations
from typing import Any, Optional
def secrets_available() -> bool:
"""Return True when Streamlit secrets are configured."""
try:
import streamlit as st
if not hasattr(st, "secrets"):
return False
_ = st.secrets.keys()
return True
except Exception:
return False
def has_secret(key: str) -> bool:
"""Check whether a top-level secret key exists without raising."""
try:
import streamlit as st
if not hasattr(st, "secrets"):
return False
return key in st.secrets
except Exception:
return False
def get_secret_section(section: str) -> Optional[Any]:
"""Return a secrets section/namespace, or None if unavailable."""
try:
import streamlit as st
if not hasattr(st, "secrets") or section not in st.secrets:
return None
return st.secrets[section]
except Exception:
return None