|
|
|
from functools import wraps |
|
|
|
_actual_spaces_GPU_imported = False |
|
_GPU_decorator_target = None |
|
|
|
try: |
|
from spaces import GPU as ActualSpacesGPU_real |
|
_GPU_decorator_target = ActualSpacesGPU_real |
|
_actual_spaces_GPU_imported = True |
|
print("INFO (decorators.py): Se import贸 'GPU' correctamente desde 'spaces'.") |
|
except ImportError: |
|
print("ADVERTENCIA (decorators.py): No se pudo importar 'GPU' desde 'spaces'. Se usar谩 un placeholder. " |
|
"Esto es normal si no se ejecuta en un Hugging Face Space con GPU habilitada.") |
|
class PlaceholderGPU: |
|
def __init__(self, duration=100, **kwargs): |
|
self.duration = duration |
|
|
|
|
|
def __call__(self, func): |
|
@wraps(func) |
|
def wrapper(*args, **kwargs): |
|
|
|
return func(*args, **kwargs) |
|
return wrapper |
|
_GPU_decorator_target = PlaceholderGPU |
|
except Exception as e: |
|
print(f"ERROR (decorators.py): Error inesperado al intentar importar/definir GPU: {e}") |
|
|
|
_GPU_decorator_target = lambda duration=100, **kwargs: lambda func: func |
|
|
|
|
|
def gpu_decorator(duration=100): |
|
""" |
|
Decorador que aplica el @spaces.GPU real si est谩 disponible, |
|
o un placeholder funcional en otros casos. |
|
""" |
|
def decorator(func): |
|
|
|
decorated_func = _GPU_decorator_target(duration=duration)(func) |
|
return decorated_func |
|
return decorator |
|
|
|
def was_real_spaces_gpu_imported(): |
|
""" |
|
Permite a otros m贸dulos verificar si el decorador GPU real de HF Spaces fue importado. |
|
""" |
|
return _actual_spaces_GPU_imported |