File size: 796 Bytes
b5e0c74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from collections.abc import Callable
from typing import TypeVar

from app.core.config import settings


F = TypeVar("F", bound=Callable)


def gpu(duration: int | None = None) -> Callable[[F], F]:
    if not _should_use_zerogpu():
        return _identity

    try:
        import spaces
    except ImportError:
        return _identity

    return spaces.GPU(duration=duration or settings.ZEROGPU_DURATION_SECONDS)


def _identity(func: F) -> F:
    return func


def _should_use_zerogpu() -> bool:
    if os.getenv("DISABLE_ZEROGPU", "").lower() in {"1", "true", "yes"}:
        return False
    if os.getenv("ENABLE_ZEROGPU", "").lower() in {"1", "true", "yes"}:
        return True
    return bool(os.getenv("SPACE_ID"))


def is_enabled() -> bool:
    return _should_use_zerogpu()