Spaces:
Runtime error
Runtime error
ESVIAL: cuenta anonimo|pro + freno 38 min
Browse files- mcp_tools/run.py +85 -3
mcp_tools/run.py
CHANGED
|
@@ -3,9 +3,11 @@ MCP Tool: run
|
|
| 3 |
Unified video generation task submission and execution interface.
|
| 4 |
"""
|
| 5 |
|
|
|
|
| 6 |
import time
|
| 7 |
import uuid
|
| 8 |
import threading
|
|
|
|
| 9 |
from .common import (
|
| 10 |
_TASK_DEFINITIONS,
|
| 11 |
_TASKS_DB,
|
|
@@ -14,6 +16,31 @@ from .common import (
|
|
| 14 |
from .error_schema import make_validation_error
|
| 15 |
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
def handle_run(params: dict) -> dict:
|
| 18 |
"""
|
| 19 |
Unified MiniMax H3 video generation task execution interface.
|
|
@@ -27,6 +54,14 @@ def handle_run(params: dict) -> dict:
|
|
| 27 |
[GLOBAL OPTIONAL PARAMETERS]
|
| 28 |
- seed (int): Random seed (-1 for random seed, >=0 for deterministic reproduction). Default: -1.
|
| 29 |
- zero_gpu_duration (int): GPU execution time quota allocation in seconds on HuggingFace ZeroGPU spaces (default: 120).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
- loras (list[dict]): List of LoRA configurations (e.g., [{"source": "Hugging Face", "id_or_url": "repo/lora.safetensors", "scale": 1.0}]).
|
| 31 |
- h3_controlnets (list[dict]): List of H3 ControlNet configurations (e.g., [{"video": "https://.../pose.mp4", "strength": 1.0}]).
|
| 32 |
- h3_guides (list[dict]): List of keyframe guide configurations (e.g., [{"image": "https://.../keyframe.png", "time_seconds": 1.5}]). Supported across all H3 tasks (FL2VA & Ref2VA).
|
|
@@ -41,7 +76,8 @@ def handle_run(params: dict) -> dict:
|
|
| 41 |
"height": 768,
|
| 42 |
"duration": 5.0,
|
| 43 |
"seed": -1,
|
| 44 |
-
"zero_gpu_duration": 120
|
|
|
|
| 45 |
}
|
| 46 |
"""
|
| 47 |
if not isinstance(params, dict):
|
|
@@ -68,6 +104,39 @@ def handle_run(params: dict) -> dict:
|
|
| 68 |
missing_fields=missing,
|
| 69 |
)
|
| 70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
task_id = f"v_task_{uuid.uuid4().hex[:10]}"
|
| 72 |
created_at = int(time.time())
|
| 73 |
|
|
@@ -76,19 +145,32 @@ def handle_run(params: dict) -> dict:
|
|
| 76 |
"status": "queued",
|
| 77 |
"progress": 0,
|
| 78 |
"created_at": created_at,
|
|
|
|
| 79 |
}
|
| 80 |
|
| 81 |
async_exec = params.get("async_execution", False)
|
| 82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
if async_exec:
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
t.start()
|
| 86 |
return {
|
| 87 |
"status": "queued",
|
| 88 |
"task_id": task_id,
|
|
|
|
| 89 |
"poll_interval_ms": 2000,
|
| 90 |
"message": "Task queued successfully. Poll get_task_status for results.",
|
| 91 |
}
|
| 92 |
else:
|
| 93 |
-
|
| 94 |
return _TASKS_DB[task_id]
|
|
|
|
| 3 |
Unified video generation task submission and execution interface.
|
| 4 |
"""
|
| 5 |
|
| 6 |
+
import os
|
| 7 |
import time
|
| 8 |
import uuid
|
| 9 |
import threading
|
| 10 |
+
import contextvars
|
| 11 |
from .common import (
|
| 12 |
_TASK_DEFINITIONS,
|
| 13 |
_TASKS_DB,
|
|
|
|
| 16 |
from .error_schema import make_validation_error
|
| 17 |
|
| 18 |
|
| 19 |
+
# ---------------------------------------------------------------- freno PRO
|
| 20 |
+
# Tope de GPU que ESTE Space puede gastar de la cuota PRO de la cuenta.
|
| 21 |
+
# 2280 s = 38 min, por debajo de los 40 min diarios, para no tocar creditos.
|
| 22 |
+
# OJO: el contador es de este Space y vuelve a cero si el Space se reinicia.
|
| 23 |
+
_TOPE_PRO_S = int(os.environ.get("ESVIAL_TOPE_PRO_S", "2280"))
|
| 24 |
+
_pro_lock = threading.Lock()
|
| 25 |
+
_pro_segundos = [0.0]
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def _pro_gastado() -> int:
|
| 29 |
+
with _pro_lock:
|
| 30 |
+
return int(_pro_segundos[0])
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def _anota_pro(tarea: dict) -> None:
|
| 34 |
+
"""Suma al contador lo que de verdad gasto la toma."""
|
| 35 |
+
res = (tarea or {}).get("result") or {}
|
| 36 |
+
try:
|
| 37 |
+
gasto = float(res.get("execution_time_seconds") or 0.0)
|
| 38 |
+
except (TypeError, ValueError):
|
| 39 |
+
gasto = 0.0
|
| 40 |
+
with _pro_lock:
|
| 41 |
+
_pro_segundos[0] += gasto
|
| 42 |
+
|
| 43 |
+
|
| 44 |
def handle_run(params: dict) -> dict:
|
| 45 |
"""
|
| 46 |
Unified MiniMax H3 video generation task execution interface.
|
|
|
|
| 54 |
[GLOBAL OPTIONAL PARAMETERS]
|
| 55 |
- seed (int): Random seed (-1 for random seed, >=0 for deterministic reproduction). Default: -1.
|
| 56 |
- zero_gpu_duration (int): GPU execution time quota allocation in seconds on HuggingFace ZeroGPU spaces (default: 120).
|
| 57 |
+
- cuenta (str): "anonimo" (default) o "pro".
|
| 58 |
+
"anonimo" -> la toma corre en un hilo nuevo, que NO hereda los contextvars de
|
| 59 |
+
Gradio, asi que spaces.GPU no ve quien llamo y usa la cuota gratis
|
| 60 |
+
compartida del Space.
|
| 61 |
+
"pro" -> se copia el contexto de la llamada al hilo, la identidad viaja, y
|
| 62 |
+
ZeroGPU cobra a la cuota de la cuenta que llamo.
|
| 63 |
+
NO hay modo automatico: pasar a "pro" siempre es una decision explicita de quien
|
| 64 |
+
manda la toma. Ademas hay un freno: cada Space no gasta mas de 38 min en "pro".
|
| 65 |
- loras (list[dict]): List of LoRA configurations (e.g., [{"source": "Hugging Face", "id_or_url": "repo/lora.safetensors", "scale": 1.0}]).
|
| 66 |
- h3_controlnets (list[dict]): List of H3 ControlNet configurations (e.g., [{"video": "https://.../pose.mp4", "strength": 1.0}]).
|
| 67 |
- h3_guides (list[dict]): List of keyframe guide configurations (e.g., [{"image": "https://.../keyframe.png", "time_seconds": 1.5}]). Supported across all H3 tasks (FL2VA & Ref2VA).
|
|
|
|
| 76 |
"height": 768,
|
| 77 |
"duration": 5.0,
|
| 78 |
"seed": -1,
|
| 79 |
+
"zero_gpu_duration": 120,
|
| 80 |
+
"cuenta": "anonimo"
|
| 81 |
}
|
| 82 |
"""
|
| 83 |
if not isinstance(params, dict):
|
|
|
|
| 104 |
missing_fields=missing,
|
| 105 |
)
|
| 106 |
|
| 107 |
+
# --- de que bolsa de GPU sale la toma -----------------------------------
|
| 108 |
+
cuenta = str(params.get("cuenta", "anonimo")).strip().lower()
|
| 109 |
+
if cuenta in ("anonymous", "anonimo", "anónimo", ""):
|
| 110 |
+
cuenta = "anonimo"
|
| 111 |
+
if cuenta not in ("anonimo", "pro"):
|
| 112 |
+
return make_validation_error(
|
| 113 |
+
"Invalid 'cuenta'. Must be 'anonimo' or 'pro'.",
|
| 114 |
+
invalid_fields={"cuenta": "anonimo | pro"},
|
| 115 |
+
)
|
| 116 |
+
es_pro = (cuenta == "pro")
|
| 117 |
+
|
| 118 |
+
if es_pro:
|
| 119 |
+
gastado = _pro_gastado()
|
| 120 |
+
queda = _TOPE_PRO_S - gastado
|
| 121 |
+
if queda <= 0:
|
| 122 |
+
return make_validation_error(
|
| 123 |
+
"Freno de seguridad: este Space ya gasto %d s de GPU en modo 'pro' "
|
| 124 |
+
"(tope %d s = %d min). Usa cuenta='anonimo', u otro Space, o espera "
|
| 125 |
+
"a que se reinicie la cuota diaria."
|
| 126 |
+
% (gastado, _TOPE_PRO_S, _TOPE_PRO_S // 60),
|
| 127 |
+
invalid_fields={"cuenta": "tope pro alcanzado en este Space"},
|
| 128 |
+
)
|
| 129 |
+
try:
|
| 130 |
+
pedido = int(params.get("zero_gpu_duration", 120) or 120)
|
| 131 |
+
except (TypeError, ValueError):
|
| 132 |
+
pedido = 120
|
| 133 |
+
if pedido > queda:
|
| 134 |
+
return make_validation_error(
|
| 135 |
+
"Freno de seguridad: pides %d s pero en modo 'pro' solo quedan %d s "
|
| 136 |
+
"en este Space (tope %d s)." % (pedido, queda, _TOPE_PRO_S),
|
| 137 |
+
invalid_fields={"zero_gpu_duration": "quedan %d s" % queda},
|
| 138 |
+
)
|
| 139 |
+
|
| 140 |
task_id = f"v_task_{uuid.uuid4().hex[:10]}"
|
| 141 |
created_at = int(time.time())
|
| 142 |
|
|
|
|
| 145 |
"status": "queued",
|
| 146 |
"progress": 0,
|
| 147 |
"created_at": created_at,
|
| 148 |
+
"modo": cuenta,
|
| 149 |
}
|
| 150 |
|
| 151 |
async_exec = params.get("async_execution", False)
|
| 152 |
|
| 153 |
+
def _correr():
|
| 154 |
+
try:
|
| 155 |
+
_execute_imagegen_pipeline(task_id, params)
|
| 156 |
+
finally:
|
| 157 |
+
if es_pro:
|
| 158 |
+
_anota_pro(_TASKS_DB.get(task_id, {}))
|
| 159 |
+
|
| 160 |
if async_exec:
|
| 161 |
+
if es_pro:
|
| 162 |
+
ctx = contextvars.copy_context() # la identidad viaja al hilo
|
| 163 |
+
t = threading.Thread(target=ctx.run, args=(_correr,), daemon=True)
|
| 164 |
+
else:
|
| 165 |
+
t = threading.Thread(target=_correr, daemon=True)
|
| 166 |
t.start()
|
| 167 |
return {
|
| 168 |
"status": "queued",
|
| 169 |
"task_id": task_id,
|
| 170 |
+
"modo": cuenta,
|
| 171 |
"poll_interval_ms": 2000,
|
| 172 |
"message": "Task queued successfully. Poll get_task_status for results.",
|
| 173 |
}
|
| 174 |
else:
|
| 175 |
+
_correr()
|
| 176 |
return _TASKS_DB[task_id]
|