File size: 2,000 Bytes
1676aa7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
ZeroGPU integration helper.

Hugging Face's free tier now only offers ZeroGPU hardware for compute
Spaces (CPU Basic requires a paid plan as of mid-2026). ZeroGPU has a hard
platform requirement: a Space will fail to start if it contains ZERO
functions decorated with @spaces.GPU, even if none of them strictly need
CUDA to run.

This module provides `gpu_decorator`, which is:
  - the real `spaces.GPU` decorator when the `spaces` package is present
    (i.e. when actually running on a Hugging Face Space)
  - a transparent no-op decorator everywhere else (local dev, this sandbox,
    CI) so the exact same code runs unmodified off-platform

Per Hugging Face's own documentation, `spaces.GPU` is itself "designed to
be effect-free in non-ZeroGPU environments" — this wrapper extends that
same safety to environments where the `spaces` package isn't installed
at all.

Our models are intentionally small enough that they do not need GPU
acceleration to produce a convincing demo; this decorator exists to
satisfy the platform requirement, not because the workload demands it.
"""

from __future__ import annotations
import logging

logger = logging.getLogger("zero_gpu")

try:
    import spaces  # type: ignore
    gpu_decorator = spaces.GPU
    ZERO_GPU_AVAILABLE = True
    logger.info("Running with real Hugging Face `spaces` module — @spaces.GPU is active.")
except ImportError:
    ZERO_GPU_AVAILABLE = False

    def gpu_decorator(*decorator_args, **decorator_kwargs):
        """No-op replacement for spaces.GPU so code runs unmodified off-Spaces."""
        def wrapper(func):
            return func
        # Support both @gpu_decorator and @gpu_decorator(duration=30) call styles
        if len(decorator_args) == 1 and callable(decorator_args[0]) and not decorator_kwargs:
            return decorator_args[0]
        return wrapper

    logger.info("`spaces` module not found (expected in local/sandbox environments) — "
                "@spaces.GPU calls are no-ops here.")