Spaces:
Running on Zero
Running on Zero
Fall back to an anonymous conditioner call when the forwarded ZeroGPU token is refused
Browse files
README.md
CHANGED
|
@@ -185,14 +185,18 @@ on is cold and a cold one pays the lazy 72.16 GiB `PIPE.to("cuda")` inside its f
|
|
| 185 |
|
| 186 |
Two cards are booked per request: this Space's denoise loop and the conditioner's forward. ZeroGPU attributes a
|
| 187 |
booking to the `X-IP-Token` header of the request that triggered it, so this Space forwards the caller's token to the
|
| 188 |
-
conditioner (`gradio_client.Client(..., headers={"X-IP-Token": ...})`,
|
| 189 |
-
path and the `/generate` API path alike).
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
|
| 197 |
## Secrets
|
| 198 |
|
|
|
|
| 185 |
|
| 186 |
Two cards are booked per request: this Space's denoise loop and the conditioner's forward. ZeroGPU attributes a
|
| 187 |
booking to the `X-IP-Token` header of the request that triggered it, so this Space forwards the caller's token to the
|
| 188 |
+
conditioner (`gradio_client.Client(..., headers={"X-IP-Token": ...})`, off the `gr.Request` gradio injects — the UI
|
| 189 |
+
path and the `/generate` API path alike). When ZeroGPU honours it, one user's request bills as one request across both
|
| 190 |
+
halves and no org token is spent on it.
|
| 191 |
+
|
| 192 |
+
It is best effort, though: ZeroGPU answers `401` for a proxy token it will not honour, which `spaces` surfaces as
|
| 193 |
+
`Expired ZeroGPU proxy token`, and a token minted for one Space arriving at another is exactly that case on the API
|
| 194 |
+
path. So a refused token falls back to calling the conditioner with no token at all, on an IP-based quota.
|
| 195 |
+
|
| 196 |
+
That fallback is a working path rather than a degraded one, because the conditioner is sized for it. An unattributed
|
| 197 |
+
caller may book at most 120 credits at a time and an `xlarge` booking costs **twice** its seconds, i.e. 60 s — so the
|
| 198 |
+
conditioner books the encode (45 s) and a prompt upsample (60 s) as **two separate calls**, each under the ceiling,
|
| 199 |
+
where one combined booking would be refused outright.
|
| 200 |
|
| 201 |
## Secrets
|
| 202 |
|
app.py
CHANGED
|
@@ -346,6 +346,30 @@ def ip_token_of(request) -> str | None:
|
|
| 346 |
return token
|
| 347 |
|
| 348 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 349 |
def probe(path: str) -> tuple[float | None, float | None]:
|
| 350 |
"""`(video seconds, audio seconds)` of a media file, either being `None` when the stream is absent."""
|
| 351 |
import av
|
|
@@ -457,7 +481,8 @@ def encode_remote(prompt, references, canvas, num_frames, rewrite_prompt=False,
|
|
| 457 |
from gradio_client import handle_file
|
| 458 |
from safetensors import safe_open
|
| 459 |
|
| 460 |
-
path, plan =
|
|
|
|
| 461 |
prompt=prompt,
|
| 462 |
media=[handle_file(path) for _, path in references],
|
| 463 |
kinds=",".join(kind for kind, _ in references),
|
|
|
|
| 346 |
return token
|
| 347 |
|
| 348 |
|
| 349 |
+
LOG_TAG = "ref2va"
|
| 350 |
+
|
| 351 |
+
|
| 352 |
+
def call_conditioner(ip_token, **arguments):
|
| 353 |
+
"""One conditioner call, on the caller's ZeroGPU identity when that is accepted and anonymously when it is not.
|
| 354 |
+
|
| 355 |
+
Forwarding is best effort. ZeroGPU's `/schedule` answers `401` for a proxy token it will not honour — which is
|
| 356 |
+
what a token minted for *this* Space looks like when it arrives at the conditioner — and `spaces` surfaces that as
|
| 357 |
+
`Expired ZeroGPU proxy token`. So the forwarded call is tried first, and a rejected token falls back to no token
|
| 358 |
+
at all rather than failing the request. The conditioner's own bookings are sized to fit the unattributed ceiling,
|
| 359 |
+
so the fallback is a working path and not a degraded one.
|
| 360 |
+
"""
|
| 361 |
+
api_name = arguments.pop("api_name")
|
| 362 |
+
if ip_token is not None:
|
| 363 |
+
try:
|
| 364 |
+
return conditioner(ip_token).predict(**arguments, api_name=api_name)
|
| 365 |
+
except Exception as error:
|
| 366 |
+
if "proxy token" not in str(error):
|
| 367 |
+
raise
|
| 368 |
+
print(f"[{LOG_TAG}] the forwarded ZeroGPU token was refused ({error}); retrying anonymously", flush=True)
|
| 369 |
+
CLIENTS.pop(ip_token, None)
|
| 370 |
+
return conditioner(None).predict(**arguments, api_name=api_name)
|
| 371 |
+
|
| 372 |
+
|
| 373 |
def probe(path: str) -> tuple[float | None, float | None]:
|
| 374 |
"""`(video seconds, audio seconds)` of a media file, either being `None` when the stream is absent."""
|
| 375 |
import av
|
|
|
|
| 481 |
from gradio_client import handle_file
|
| 482 |
from safetensors import safe_open
|
| 483 |
|
| 484 |
+
path, plan = call_conditioner(
|
| 485 |
+
ip_token,
|
| 486 |
prompt=prompt,
|
| 487 |
media=[handle_file(path) for _, path in references],
|
| 488 |
kinds=",".join(kind for kind, _ in references),
|