Spaces:
Running on Zero
Running on Zero
File size: 2,698 Bytes
35d8fb6 58dde1b 35d8fb6 58dde1b 35d8fb6 58dde1b 35d8fb6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | """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)
|