Spaces:
Running on Zero

File size: 3,013 Bytes
0a96270
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# SPDX-License-Identifier: Apache-2.0
"""Public error responses for the website and direct Space requests."""

from __future__ import annotations

import re


class RequestError(ValueError):
    """A validation failure with a stable public error code."""

    def __init__(self, code: str, message: str):
        super().__init__(message)
        self.code = code


def error_response(code: str, message: str) -> dict:
    """Return a JSON-safe error without exposing server exception details."""

    return {"error": {"code": code, "message": message}}


def gpu_failure(message: str, title: str = "") -> tuple[str, str] | None:
    """Recognize explicit platform failures; return only safe, fixed messages."""

    text = f"{title} {message}".lower()
    gpu_context = re.search(r"\b(?:zero\s*gpu|gpu)\b", text) is not None

    # Keep memory, queue and per-call duration failures distinct from quota.
    if re.search(r"\bcuda (?:error:\s*)?out of memory\b", text):
        return "gpu_memory", "GPU memory was insufficient for this job. Try a smaller batch or shorter inputs."
    if re.search(r"\bqueue (?:is )?full\b", text):
        return "queue_full", "The inference queue is full. Please try again later."
    if ("zerogpu illegal duration" in text or
            re.search(r"requested gpu duration.{0,120}(?:larger|greater|exceeds).{0,80}(?:maximum|allowed|limit)", text)):
        return "gpu_duration_limit", "This job requires more GPU time than your ZeroGPU tier permits per request. Split the batch or use a smaller input."
    # The UI may expose “ZeroGPU quota exceeded” only in Error.title, while
    # Error.message says the runs limit was exceeded. A runs limit is not
    # a remaining-seconds limit, and reducing sequence length is not a fix.
    if (re.search(r"\bruns?\s+limit\b", text) and
            re.search(r"\b(?:exceed(?:ed|s)?|reached|exhausted)\b", text) and
            (gpu_context or "authenticate with a hugging face token for more quota" in text)):
        return "gpu_runs_limit", "The ZeroGPU run limit has been reached. Sign in for your account allowance, or wait for the limit to reset."
    if gpu_context and (
        re.search(r"\bquota\s+(?:exceeded|exhausted)\b|\bexceed(?:ed|s)? your .{0,40}gpu quota\b|\bout of (?:zero\s*gpu |gpu )?quota\b|\b(?:insufficient|not enough)\s+(?:zero\s*gpu|gpu)\s+quota\b", text)
    ):
        return "gpu_quota", "Your ZeroGPU allowance is insufficient for this job. Sign in, try a smaller request, or wait for your allowance to reset."
    if gpu_context and any(term in text for term in ("timed out", "time limit exceeded", "runtime exceeded", "duration exceeded")):
        return "gpu_timeout", "The GPU job timed out. Split the batch or try again later."
    if ("authenticate with a hugging face token" in text and
            re.search(r"\bquota\b", text) and gpu_context):
        return "gpu_quota", "Your ZeroGPU allowance is insufficient for this job. Sign in or wait for your allowance to reset."
    return None