""" Configuración central y variables de entorno del nodo worker. 100% compatible con la implementación de Go. """ import os import time import socket import logging from pathlib import Path # Compatibilidad retroactiva con huggingface_hub >= 0.25 en Hugging Face Spaces try: import huggingface_hub if not hasattr(huggingface_hub, "HfFolder"): class _HfFolderCompat: @staticmethod def get_token(): return os.getenv("HF_TOKEN") @staticmethod def save_token(token): pass @staticmethod def delete_token(): pass huggingface_hub.HfFolder = _HfFolderCompat except Exception: pass # Parche de compatibilidad para gradio_client (evita TypeError: argument of type 'bool' is not iterable en Gradio 4.44.x) try: import gradio_client.utils as client_utils _orig_get_type = client_utils.get_type def _safe_get_type(schema): if isinstance(schema, bool): return "boolean" return _orig_get_type(schema) client_utils.get_type = _safe_get_type _orig_json_schema = client_utils._json_schema_to_python_type def _safe_json_schema_to_python_type(schema, defs=None): if isinstance(schema, bool): return "boolean" return _orig_json_schema(schema, defs) client_utils._json_schema_to_python_type = _safe_json_schema_to_python_type except Exception: pass # Parche de compatibilidad para Starlette >= 1.0 (TemplateResponse signature mismatch en Gradio 4.44.x) try: from starlette.templating import Jinja2Templates _orig_template_response = Jinja2Templates.TemplateResponse def _safe_template_response(self, *args, **kwargs): if len(args) >= 2 and isinstance(args[0], str) and isinstance(args[1], dict): name = args[0] context = args[1] request = context.get("request") if request is not None: return _orig_template_response(self, request, name, context, *args[2:], **kwargs) elif len(args) == 1 and isinstance(args[0], str) and "context" in kwargs: name = args[0] context = kwargs["context"] request = context.get("request") if request is not None: kwargs["name"] = name kwargs["request"] = request return _orig_template_response(self, **kwargs) return _orig_template_response(self, *args, **kwargs) Jinja2Templates.TemplateResponse = _safe_template_response except Exception: pass # Configuración de Logging logging.basicConfig( level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s", datefmt="%H:%M:%S", ) logger = logging.getLogger("omnivision") # URL del Manager / Coordinador MANAGER_URL = os.getenv("MANAGER_URL", "").strip() if MANAGER_URL and not MANAGER_URL.startswith("http://") and not MANAGER_URL.startswith("https://"): MANAGER_URL = f"https://{MANAGER_URL}" # Token secreto de autenticación WORKER_SECRET = os.getenv("WORKER_SECRET", "").strip() # Identificador único del nodo WORKER_ID = os.getenv("WORKER_ID", "").strip() if not WORKER_ID: try: hostname = socket.gethostname() or "omnivision-node" except Exception: hostname = "omnivision-node" WORKER_ID = f"{hostname}-{int(time.time()) % 10000}" # Directorio temporal de trabajo WORK_DIR = os.getenv("WORK_DIR", "").strip() if not WORK_DIR: WORK_DIR = os.path.join(os.path.expanduser("~"), "omnivision_media") os.makedirs(WORK_DIR, exist_ok=True) # Puerto del servidor PORT = int(os.getenv("PORT", "7860")) # Tareas concurrentes máximas (por defecto 1, ampliable a 2, 3 o 4) try: MAX_JOBS = int(os.getenv("MAX_JOBS", "1").strip()) if MAX_JOBS < 1: MAX_JOBS = 1 except Exception: MAX_JOBS = 1 # Versión del servicio VERSION = "2.4.1" # URL pública del nodo para el Watchdog (auto-detectada en Hugging Face Spaces o configurable) SPACE_URL = os.getenv("SPACE_URL", "").strip() if not SPACE_URL: space_host = os.getenv("SPACE_HOST", "").strip() if space_host: SPACE_URL = f"https://{space_host}" else: space_id = os.getenv("SPACE_ID", "").strip() if space_id: sanitized = space_id.replace("/", "-").lower() SPACE_URL = f"https://{sanitized}.hf.space" elif not SPACE_URL.startswith("http://") and not SPACE_URL.startswith("https://"): SPACE_URL = f"https://{SPACE_URL}" # Marca de inicio START_TIME = time.time()