Spaces:
Running on Zero
Running on Zero
Download src/ui/cpu_fallback.py from QUD-Technologies/quranic-universal-aligner: direct link, hf CLI and curl.
- Browser
- Download file 2.7 kB
-
https://huggingface.co/spaces/QUD-Technologies/quranic-universal-aligner/resolve/main/src/ui/cpu_fallback.py
- Command line
-
hf download hf://spaces/QUD-Technologies/quranic-universal-aligner/src/ui/cpu_fallback.py
-
curl -L -o cpu_fallback.py https://huggingface.co/spaces/QUD-Technologies/quranic-universal-aligner/resolve/main/src/ui/cpu_fallback.py
2.7 kB
| """Finish on the CPU when ZeroGPU has nothing left. | |
| The API already does this (`_run` in `src/api/services/align.py`). The UI used | |
| to make the visitor do it by hand, from a Device radio next to the model | |
| picker: a refusal, a warning telling them to switch, and a second click. Both | |
| surfaces now behave the same way, which leaves that radio nothing to control. | |
| The retry is a real second run — the first one died inside the lease — so the | |
| caller's progress bar goes back to the queue stage. That is what happened. | |
| """ | |
| from __future__ import annotations | |
| import gradio as gr | |
| from src.core.zero_gpu import GpuBypassError, QuotaExhaustedError | |
| from src.pipeline import progress | |
| #: Long enough to read a two-clause sentence without chasing it. | |
| _NOTICE_SECONDS = 12 | |
| def _is_anonymous(exc) -> bool: | |
| """Whether ZeroGPU refused an unsigned-in caller rather than an account.""" | |
| raw = str(exc).lower() | |
| return "unlogged user" in raw or "signup for free" in raw | |
| def _reset_hint(exc) -> str: | |
| # ZeroGPU reports 0:00:00 when the refusal is the daily runs limit rather | |
| # than a seconds budget, and there is no countdown to quote. | |
| if exc.reset_time == "0:00:00": | |
| return " It resets about 24 hours after your first GPU use today." | |
| if exc.reset_time: | |
| return f" It resets in {exc.reset_time}." | |
| return "" | |
| def notice(exc) -> str: | |
| """What the visitor is told when their alignment moves to the CPU.""" | |
| if _is_anonymous(exc): | |
| return ("Anonymous GPU quota is used up, so this is running on the CPU " | |
| f"— slower on long audio.{_reset_hint(exc)} " | |
| "Sign in with Hugging Face for a larger allowance.") | |
| return ("Your GPU quota is used up, so this is running on the CPU — slower " | |
| f"on long audio.{_reset_hint(exc)} " | |
| "Hugging Face PRO raises the daily allowance.") | |
| def stream(fn, *args, **kwargs): | |
| """``progress.stream`` for *fn* on the GPU, moving to the CPU on a refusal. | |
| Yields exactly what ``progress.stream`` yields, so callers keep their | |
| existing loop. A quota refusal, a recording past the GPU limit, a scheduler failure | |
| or an aborted task is retried; any other GPU failure is a real fault and must not | |
| hide behind a slow run. | |
| """ | |
| try: | |
| yield from progress.stream(fn, *args, device="GPU", **kwargs) | |
| return | |
| except QuotaExhaustedError as exhausted: | |
| gr.Info(notice(exhausted), duration=_NOTICE_SECONDS) | |
| except GpuBypassError as bypass: | |
| print(f"[GPU] running on CPU: {bypass}", flush=True) | |
| gr.Info(bypass.notice, duration=_NOTICE_SECONDS) | |
| yield from progress.stream(fn, *args, device="CPU", **kwargs) | |