chatvns / app /runtime_auth.py
liamxdev's picture
Upload folder using huggingface_hub
34b531b verified
Raw
History Blame Contribute Delete
1.09 kB
from __future__ import annotations
from contextvars import ContextVar
import os
_gemini_api_key: ContextVar[str] = ContextVar("gemini_api_key", default="")
_hf_api_key: ContextVar[str] = ContextVar("hf_api_key", default="")
def set_runtime_api_keys(*, gemini_api_key: str = "", hf_api_key: str = "") -> None:
_gemini_api_key.set(gemini_api_key.strip())
_hf_api_key.set(hf_api_key.strip())
def get_gemini_api_key() -> str:
runtime_key = _gemini_api_key.get()
if runtime_key or _requires_user_keys():
return runtime_key
return os.getenv("GEMINI_API_KEY", "").strip()
def get_hf_api_key() -> str:
runtime_key = _hf_api_key.get()
if runtime_key or _requires_user_keys():
return runtime_key
return (
os.getenv("HF_API_KEY")
or os.getenv("HG_API_KEY")
or os.getenv("HUGGINGFACEHUB_API_TOKEN")
or ""
).strip()
def has_hf_api_key() -> bool:
return bool(get_hf_api_key())
def _requires_user_keys() -> bool:
return os.getenv("REQUIRE_USER_API_KEYS", "").strip().lower() in {"1", "true", "yes"}