Spaces:
Running on Zero
Running on Zero
Download src/api/services/faults.py from QUD-Technologies/quranic-universal-aligner: direct link, hf CLI and curl.
- Browser
- Download file 4.48 kB
-
https://huggingface.co/spaces/QUD-Technologies/quranic-universal-aligner/resolve/main/src/api/services/faults.py
- Command line
-
hf download hf://spaces/QUD-Technologies/quranic-universal-aligner/src/api/services/faults.py
-
curl -L -o faults.py https://huggingface.co/spaces/QUD-Technologies/quranic-universal-aligner/resolve/main/src/api/services/faults.py
4.48 kB
| """Translate pipeline exceptions into the typed API errors in ``src.api.errors``. | |
| One place decides what each runtime failure means to a caller, so /api/v1 and | |
| the legacy adapters can never disagree about a status code or an error code. | |
| """ | |
| from __future__ import annotations | |
| from src.api.errors import ( | |
| AlignmentFailedError, | |
| CpuRateLimitedError, | |
| GpuQuotaError, | |
| GpuUnavailableError, | |
| ValidationError, | |
| ) | |
| from src.core.zero_gpu import GpuQueueTimeoutError, QuotaExhaustedError | |
| # ZeroGPU emits "0:00:00" when the daily cap is blown (as opposed to a | |
| # short-term rate limit, where it emits a real wait). "Resets in 0:00:00" would | |
| # confuse users, so that case gets a daily-cap hint instead. | |
| _DAILY_CAP_SENTINEL = "0:00:00" | |
| _ANONYMOUS_MARKERS = ("unlogged user", "signup for free") | |
| def _reset_suffix(reset_time) -> str: | |
| if reset_time and reset_time != _DAILY_CAP_SENTINEL: | |
| return f" Resets in {reset_time}." | |
| if reset_time == _DAILY_CAP_SENTINEL: | |
| return " Daily limit — resets ~24h after your first GPU use today." | |
| return "" | |
| def quota_error(exc: QuotaExhaustedError) -> GpuQuotaError: | |
| """Build the typed quota error, preserving production's exact wording. | |
| ZeroGPU's real error strings (verified from Space logs, Apr 2026): | |
| * anonymous: "Unlogged user is runnning out of daily ZeroGPU quotas. | |
| Signup for free on .../join or login on .../login ..." | |
| * logged: "User is runnning out of daily ZeroGPU quotas. | |
| Visit .../subscribe/pro ..." | |
| Neither carries a reset timestamp — HF does not expose the daily-reset time | |
| anywhere (huggingface_hub#2842) — so ``reset_time`` is usually None. | |
| """ | |
| raw_lower = str(exc).lower() | |
| suffix = _reset_suffix(exc.reset_time) | |
| anonymous = any(marker in raw_lower for marker in _ANONYMOUS_MARKERS) | |
| if anonymous: | |
| message = ( | |
| f"Anonymous GPU quota exhausted for this IP.{suffix} " | |
| "Sign in at https://huggingface.co/login for more quota, " | |
| "or retry with device=CPU." | |
| ) | |
| else: | |
| message = ( | |
| f"GPU quota exhausted for your account.{suffix} " | |
| "Upgrade at https://huggingface.co/subscribe/pro for more quota, " | |
| "or retry with device=CPU." | |
| ) | |
| return GpuQuotaError(message, anonymous=anonymous, reset_time=exc.reset_time) | |
| def gpu_queue_timeout_error(exc: GpuQueueTimeoutError) -> GpuUnavailableError: | |
| return GpuUnavailableError( | |
| "Hugging Face could not grant a GPU slot after repeated attempts. Retry shortly." | |
| ) | |
| def unalignable_error(exc: ValueError) -> AlignmentFailedError: | |
| """The pipeline ran but could not produce an alignment. | |
| ``src/pipeline/post_stages.py`` re-raises the SDK's input failures — an | |
| audio that anchors to no chapter, a verse range that does not exist — as a | |
| bare ``ValueError`` carrying the message meant for a person. Without this | |
| it reaches the surfaces untyped: /api/v1 renders 500 "the server failed", | |
| and the legacy adapter never catches it at all, breaking its promise that | |
| errors arrive as HTTP 200 with an ``error`` key. | |
| """ | |
| return AlignmentFailedError(str(exc)) | |
| def cpu_rate_limit_error(exc) -> CpuRateLimitedError: | |
| """Wrap a CpuRateLimitError / CpuActiveLimitError.""" | |
| return CpuRateLimitedError( | |
| str(exc), | |
| code=getattr(exc, "error_code", None), | |
| retry_after_s=getattr(exc, "retry_after_s", 30), | |
| ) | |
| def audio_too_long_error(exc) -> ValidationError: | |
| """The recording is past the length limit; its message already says so.""" | |
| return ValidationError(str(exc)) | |
| def unreadable_audio_error(exc) -> ValidationError: | |
| """The upload is empty or no decoder could read it: the caller's input, not a fault.""" | |
| return ValidationError("The audio file is empty or could not be read.") | |
| def cpu_rate_limit_after_quota_error(exc) -> CpuRateLimitedError: | |
| """The CPU fallback was refused as well. | |
| A caller who asked for GPU and gets a CPU limit back needs to be told why | |
| they are seeing one at all. The retry timing is the only actionable thing | |
| either failure carries, so the CPU error's status and code win. | |
| """ | |
| limited = cpu_rate_limit_error(exc) | |
| return CpuRateLimitedError( | |
| f"The GPU could not take this request and the CPU fallback is rate limited. {limited}", | |
| code=limited.code, | |
| retry_after_s=limited.retry_after_s, | |
| ) | |