Verify API key against SHA-256 hash (no write token needed)
Browse filesCo-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Files changed (2)
- api_key.sha256Added+1
- app.py+18-4
153c868180dec7f5640ffc8de289d49fffb6d57bec40d5e8abdc2c37059e0e22a5 unmodified lines678910116 unmodified lines18192021222324252627282921 unmodified lines515253545556575 unmodified lines- the `api_key` input of the /transcribe endpoint."""import hmacimport osimport time6 unmodified linesfrom transformers import pipelineMODEL_ID = "MediaTek-Research/Breeze-ASR-25"API_KEY = os.environ.get("API_KEY", "")MAX_AUDIO_SECONDS = int(os.environ.get("MAX_AUDIO_SECONDS", "600"))RATE_LIMIT_PER_MIN = int(os.environ.get("RATE_LIMIT_PER_MIN", "20"))if not API_KEY:print("WARNING: API_KEY secret is not set - every request will be rejected.")# Load on CPU at startup; ZeroGPU moves it to the GPU inside @spaces.GPU calls.asr = pipeline(21 unmodified linesdef _check_auth(api_key: str | None, request: gr.Request | None) -> None:supplied = _extract_key(api_key, request)if not API_KEY or not hmac.compare_digest(supplied.encode(), API_KEY.encode()):time.sleep(1) # slow down brute-force attemptsraise gr.Error("Unauthorized: invalid or missing API key.", print_exception=False)5 unmodified lines67891011126 unmodified lines19202122232425262728293031323334353637383940414221 unmodified lines64656667686970715 unmodified lines- the `api_key` input of the /transcribe endpoint."""import hashlibimport hmacimport osimport time6 unmodified linesfrom transformers import pipelineMODEL_ID = "MediaTek-Research/Breeze-ASR-25"MAX_AUDIO_SECONDS = int(os.environ.get("MAX_AUDIO_SECONDS", "600"))RATE_LIMIT_PER_MIN = int(os.environ.get("RATE_LIMIT_PER_MIN", "20"))def _load_key_hash() -> str:"""SHA-256 of the API key: from the API_KEY secret if set, else api_key.sha256 in the repo."""if os.environ.get("API_KEY"):return hashlib.sha256(os.environ["API_KEY"].encode()).hexdigest()try:with open(os.path.join(os.path.dirname(__file__), "api_key.sha256")) as f:return f.read().strip().lower()except OSError:return ""API_KEY_HASH = _load_key_hash()if not API_KEY_HASH:print("WARNING: no API key configured - every request will be rejected.")# Load on CPU at startup; ZeroGPU moves it to the GPU inside @spaces.GPU calls.asr = pipeline(21 unmodified linesdef _check_auth(api_key: str | None, request: gr.Request | None) -> None:supplied = _extract_key(api_key, request)supplied_hash = hashlib.sha256(supplied.encode()).hexdigest()if not API_KEY_HASH or not supplied or not hmac.compare_digest(supplied_hash, API_KEY_HASH):time.sleep(1) # slow down brute-force attemptsraise gr.Error("Unauthorized: invalid or missing API key.", print_exception=False)
api_key.sha256Added+1
app.py+18-4