Spaces:
Running
on
Zero
Running
on
Zero
| """ | |
| Utilities for integrating Hugging Face Spaces ZeroGPU without breaking | |
| local or non-ZeroGPU environments. | |
| """ | |
| from typing import Callable, TypeVar | |
| T = TypeVar("T", bound=Callable) | |
| # Default values in case the spaces package is unavailable (e.g., local runs). | |
| ZERO_GPU_AVAILABLE = False | |
| try: | |
| import spaces # type: ignore | |
| gpu_decorator = spaces.GPU # pragma: no cover | |
| ZERO_GPU_AVAILABLE = True | |
| except Exception: | |
| def gpu_decorator(*decorator_args, **decorator_kwargs): | |
| """ | |
| No-op replacement for spaces.GPU so code can run without the package | |
| or outside of a ZeroGPU Space. | |
| """ | |
| def wrapper(func: T) -> T: | |
| return func | |
| # Support both bare @gpu_decorator and @gpu_decorator(...) | |
| if decorator_args and callable(decorator_args[0]) and not decorator_kwargs: | |
| return decorator_args[0] | |
| return wrapper | |