Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -153,15 +153,32 @@ except Exception as e:
|
|
| 153 |
|
| 154 |
|
| 155 |
# ---------------------------------------------------------------------------
|
| 156 |
-
# Model configuration
|
| 157 |
# ---------------------------------------------------------------------------
|
| 158 |
|
| 159 |
MODEL_ID = "krea/krea-realtime-video"
|
| 160 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
_pipeline = None
|
| 162 |
_pipeline_error = None
|
| 163 |
_pipeline_lock = threading.Lock()
|
| 164 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
|
| 166 |
def _log(msg):
|
| 167 |
print(f"[KreaRealtimeVideo] {msg}", flush=True)
|
|
@@ -180,6 +197,16 @@ def _runtime_report():
|
|
| 180 |
}
|
| 181 |
|
| 182 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
def _call_from_pretrained_compat(*args, **kwargs):
|
| 184 |
"""
|
| 185 |
Compatibility wrapper because some diffusers/HF Hub combinations
|
|
@@ -216,11 +243,10 @@ def _load_pipeline():
|
|
| 216 |
"""
|
| 217 |
Load the ModularPipeline once at app startup.
|
| 218 |
|
| 219 |
-
For ZeroGPU, this
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
generate() will retry loading under @spaces.GPU.
|
| 224 |
"""
|
| 225 |
global _pipeline, _pipeline_error
|
| 226 |
|
|
@@ -245,7 +271,6 @@ def _load_pipeline():
|
|
| 245 |
|
| 246 |
_log("Skeleton loaded; attaching components ...")
|
| 247 |
|
| 248 |
-
# Primary path: Krea model-card style.
|
| 249 |
try:
|
| 250 |
_load_components_compat(
|
| 251 |
pipe,
|
|
@@ -314,6 +339,196 @@ def _load_pipeline():
|
|
| 314 |
return None
|
| 315 |
|
| 316 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 317 |
# ---------------------------------------------------------------------------
|
| 318 |
# Eager app runtime warm-up
|
| 319 |
# ---------------------------------------------------------------------------
|
|
@@ -323,7 +538,7 @@ if os.environ.get("SKIP_MODEL_LOAD") != "1":
|
|
| 323 |
|
| 324 |
|
| 325 |
# ---------------------------------------------------------------------------
|
| 326 |
-
# Health
|
| 327 |
# ---------------------------------------------------------------------------
|
| 328 |
|
| 329 |
def health():
|
|
@@ -338,10 +553,10 @@ def health():
|
|
| 338 |
"last_error": _pipeline_error or "",
|
| 339 |
"expected_output_type": "video",
|
| 340 |
"runtime": _runtime_report(),
|
|
|
|
| 341 |
}
|
| 342 |
|
| 343 |
|
| 344 |
-
@_spaces_gpu(duration=120, size="xlarge")
|
| 345 |
def warmup_model():
|
| 346 |
"""
|
| 347 |
Manual warm-up button.
|
|
@@ -355,6 +570,7 @@ def warmup_model():
|
|
| 355 |
"status": "error",
|
| 356 |
"error": _pipeline_error or "Pipeline failed to load",
|
| 357 |
"runtime": _runtime_report(),
|
|
|
|
| 358 |
}
|
| 359 |
|
| 360 |
return {
|
|
@@ -363,6 +579,7 @@ def warmup_model():
|
|
| 363 |
"runtime_mode": "zerogpu_compatibility_compile_bypass",
|
| 364 |
"message": "Model loaded and cached in this Space process.",
|
| 365 |
"runtime": _runtime_report(),
|
|
|
|
| 366 |
}
|
| 367 |
|
| 368 |
|
|
@@ -370,29 +587,37 @@ def warmup_model():
|
|
| 370 |
# Generation endpoint — real inference guarded by @spaces.GPU
|
| 371 |
# ---------------------------------------------------------------------------
|
| 372 |
|
| 373 |
-
def _gpu_duration(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 374 |
try:
|
| 375 |
blocks = int(num_blocks)
|
| 376 |
steps = int(num_inference_steps)
|
| 377 |
except Exception:
|
| 378 |
-
blocks =
|
| 379 |
-
steps =
|
| 380 |
|
| 381 |
-
# Model
|
| 382 |
-
# Observed
|
| 383 |
-
#
|
|
|
|
| 384 |
#
|
| 385 |
-
#
|
| 386 |
-
|
| 387 |
-
# 6×4 -> 68s
|
| 388 |
-
# 9×4 -> 92s
|
| 389 |
-
# 9×8 -> 150s cap
|
| 390 |
-
return min(150, max(30, int(20 + blocks * steps * 2)))
|
| 391 |
|
| 392 |
|
| 393 |
@_spaces_gpu(duration=_gpu_duration, size="xlarge")
|
| 394 |
def generate(
|
| 395 |
prompt,
|
|
|
|
|
|
|
| 396 |
num_blocks,
|
| 397 |
num_inference_steps,
|
| 398 |
seed,
|
|
@@ -410,9 +635,10 @@ def generate(
|
|
| 410 |
num_blocks = int(num_blocks)
|
| 411 |
num_inference_steps = int(num_inference_steps)
|
| 412 |
seed = int(seed)
|
|
|
|
| 413 |
|
| 414 |
-
if num_blocks < 1 or num_blocks >
|
| 415 |
-
raise ValueError("num_blocks must be between 1 and
|
| 416 |
|
| 417 |
if num_inference_steps < 1 or num_inference_steps > 8:
|
| 418 |
raise ValueError("num_inference_steps must be between 1 and 8.")
|
|
@@ -425,6 +651,20 @@ def generate(
|
|
| 425 |
except Exception as e:
|
| 426 |
_log(f"Pipeline .to('cuda') warning: {type(e).__name__}: {e}")
|
| 427 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 428 |
frames = []
|
| 429 |
state = PipelineState()
|
| 430 |
|
|
@@ -450,7 +690,7 @@ def generate(
|
|
| 450 |
|
| 451 |
state = pipe(
|
| 452 |
state,
|
| 453 |
-
prompt=[
|
| 454 |
num_inference_steps=num_inference_steps,
|
| 455 |
num_blocks=num_blocks,
|
| 456 |
block_idx=block_idx,
|
|
@@ -494,23 +734,57 @@ with gr.Blocks(title="Krea Realtime Video 14B") as demo:
|
|
| 494 |
"This Space runs **real local inference** for the Krea Realtime 14B "
|
| 495 |
"text-to-video model using Diffusers `ModularPipeline`.\n\n"
|
| 496 |
"⚠️ **ZeroGPU compatibility mode**: `torch.compile` is disabled because "
|
| 497 |
-
"ZeroGPU does not support it.
|
| 498 |
"**Video length** is controlled by the number of blocks. "
|
| 499 |
-
"Roughly: 1 block ≈ ~1 second,
|
|
|
|
|
|
|
|
|
|
| 500 |
)
|
| 501 |
|
| 502 |
with gr.Row():
|
| 503 |
with gr.Column():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 504 |
prompt = gr.Textbox(
|
| 505 |
label="Prompt",
|
| 506 |
placeholder="e.g., a cat sitting on a boat",
|
| 507 |
-
lines=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 508 |
)
|
| 509 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 510 |
num_blocks = gr.Slider(
|
| 511 |
minimum=1,
|
| 512 |
-
maximum=
|
| 513 |
-
value=
|
| 514 |
step=1,
|
| 515 |
label="Video Length / Number of Blocks",
|
| 516 |
)
|
|
@@ -518,29 +792,93 @@ with gr.Blocks(title="Krea Realtime Video 14B") as demo:
|
|
| 518 |
num_inference_steps = gr.Slider(
|
| 519 |
minimum=1,
|
| 520 |
maximum=8,
|
| 521 |
-
value=
|
| 522 |
step=1,
|
| 523 |
label="Inference Steps per Block",
|
| 524 |
)
|
| 525 |
|
| 526 |
seed = gr.Number(value=42, precision=0, label="Seed")
|
| 527 |
|
| 528 |
-
|
| 529 |
-
warmup_btn = gr.Button("Load / Warm up model", variant="secondary")
|
| 530 |
-
generate_btn = gr.Button("Generate Video", variant="primary")
|
| 531 |
-
|
| 532 |
-
warmup_status = gr.JSON(label="Model Status")
|
| 533 |
|
| 534 |
with gr.Column():
|
| 535 |
output_video = gr.Video(label="Generated Video")
|
| 536 |
|
| 537 |
gr.Examples(
|
| 538 |
examples=[
|
| 539 |
-
[
|
| 540 |
-
|
| 541 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 542 |
],
|
| 543 |
-
inputs=[prompt, num_blocks, num_inference_steps, seed],
|
| 544 |
outputs=output_video,
|
| 545 |
fn=generate,
|
| 546 |
cache_examples=False,
|
|
@@ -549,13 +887,34 @@ with gr.Blocks(title="Krea Realtime Video 14B") as demo:
|
|
| 549 |
warmup_btn.click(
|
| 550 |
warmup_model,
|
| 551 |
inputs=None,
|
| 552 |
-
outputs=
|
| 553 |
api_name="warmup",
|
| 554 |
)
|
| 555 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 556 |
generate_btn.click(
|
| 557 |
generate,
|
| 558 |
-
inputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 559 |
outputs=output_video,
|
| 560 |
api_name="generate",
|
| 561 |
)
|
|
@@ -563,7 +922,7 @@ with gr.Blocks(title="Krea Realtime Video 14B") as demo:
|
|
| 563 |
demo.load(
|
| 564 |
lambda: health(),
|
| 565 |
inputs=None,
|
| 566 |
-
outputs=
|
| 567 |
api_name="health",
|
| 568 |
)
|
| 569 |
|
|
|
|
| 153 |
|
| 154 |
|
| 155 |
# ---------------------------------------------------------------------------
|
| 156 |
+
# Model / LoRA configuration
|
| 157 |
# ---------------------------------------------------------------------------
|
| 158 |
|
| 159 |
MODEL_ID = "krea/krea-realtime-video"
|
| 160 |
|
| 161 |
+
KNOWN_LORAS = {
|
| 162 |
+
"Base model": None,
|
| 163 |
+
"Origami": {
|
| 164 |
+
"repo_id": "shauray/Origami_WanLora",
|
| 165 |
+
"prefix": "diffusion_model",
|
| 166 |
+
"weight_name": "origami_000000500.safetensors",
|
| 167 |
+
"adapter_name": "origami",
|
| 168 |
+
"trigger": "[origami]",
|
| 169 |
+
},
|
| 170 |
+
}
|
| 171 |
+
|
| 172 |
_pipeline = None
|
| 173 |
_pipeline_error = None
|
| 174 |
_pipeline_lock = threading.Lock()
|
| 175 |
|
| 176 |
+
_loaded_loras = set()
|
| 177 |
+
_active_lora = None
|
| 178 |
+
_active_lora_label = "Base model"
|
| 179 |
+
_active_lora_strength = 1.0
|
| 180 |
+
_lora_lock = threading.Lock()
|
| 181 |
+
|
| 182 |
|
| 183 |
def _log(msg):
|
| 184 |
print(f"[KreaRealtimeVideo] {msg}", flush=True)
|
|
|
|
| 197 |
}
|
| 198 |
|
| 199 |
|
| 200 |
+
def _lora_report():
|
| 201 |
+
return {
|
| 202 |
+
"active_lora": _active_lora_label,
|
| 203 |
+
"active_adapter": _active_lora,
|
| 204 |
+
"active_strength": _active_lora_strength,
|
| 205 |
+
"loaded_loras": sorted(list(_loaded_loras)),
|
| 206 |
+
"available_loras": list(KNOWN_LORAS.keys()),
|
| 207 |
+
}
|
| 208 |
+
|
| 209 |
+
|
| 210 |
def _call_from_pretrained_compat(*args, **kwargs):
|
| 211 |
"""
|
| 212 |
Compatibility wrapper because some diffusers/HF Hub combinations
|
|
|
|
| 243 |
"""
|
| 244 |
Load the ModularPipeline once at app startup.
|
| 245 |
|
| 246 |
+
For ZeroGPU, this gives the best UX:
|
| 247 |
+
- model warms up when the app starts;
|
| 248 |
+
- generation remains protected by @spaces.GPU;
|
| 249 |
+
- LoRAs can be loaded manually before generation.
|
|
|
|
| 250 |
"""
|
| 251 |
global _pipeline, _pipeline_error
|
| 252 |
|
|
|
|
| 271 |
|
| 272 |
_log("Skeleton loaded; attaching components ...")
|
| 273 |
|
|
|
|
| 274 |
try:
|
| 275 |
_load_components_compat(
|
| 276 |
pipe,
|
|
|
|
| 339 |
return None
|
| 340 |
|
| 341 |
|
| 342 |
+
# ---------------------------------------------------------------------------
|
| 343 |
+
# LoRA helpers
|
| 344 |
+
# ---------------------------------------------------------------------------
|
| 345 |
+
|
| 346 |
+
def _load_lora_if_needed(pipe, lora_label):
|
| 347 |
+
"""
|
| 348 |
+
Load a known LoRA adapter once.
|
| 349 |
+
|
| 350 |
+
This is intentionally not decorated with @spaces.GPU when called through
|
| 351 |
+
the UI load button, so it does not reserve ZeroGPU generation time.
|
| 352 |
+
"""
|
| 353 |
+
global _loaded_loras
|
| 354 |
+
|
| 355 |
+
cfg = KNOWN_LORAS.get(lora_label)
|
| 356 |
+
if not cfg:
|
| 357 |
+
return None
|
| 358 |
+
|
| 359 |
+
adapter_name = cfg["adapter_name"]
|
| 360 |
+
|
| 361 |
+
if adapter_name in _loaded_loras:
|
| 362 |
+
return adapter_name
|
| 363 |
+
|
| 364 |
+
transformer = getattr(pipe, "transformer", None)
|
| 365 |
+
if transformer is None or not hasattr(transformer, "load_lora_adapter"):
|
| 366 |
+
raise RuntimeError("This pipeline transformer does not expose load_lora_adapter().")
|
| 367 |
+
|
| 368 |
+
_log(f"Loading LoRA adapter: {lora_label} ({adapter_name})")
|
| 369 |
+
|
| 370 |
+
transformer.load_lora_adapter(
|
| 371 |
+
cfg["repo_id"],
|
| 372 |
+
prefix=cfg["prefix"],
|
| 373 |
+
weight_name=cfg["weight_name"],
|
| 374 |
+
adapter_name=adapter_name,
|
| 375 |
+
)
|
| 376 |
+
|
| 377 |
+
_loaded_loras.add(adapter_name)
|
| 378 |
+
_log(f"LoRA loaded: {adapter_name}")
|
| 379 |
+
|
| 380 |
+
return adapter_name
|
| 381 |
+
|
| 382 |
+
|
| 383 |
+
def _set_lora(pipe, lora_label, lora_strength, allow_load=True):
|
| 384 |
+
"""
|
| 385 |
+
Activate the selected LoRA, or disable LoRA for base model.
|
| 386 |
+
|
| 387 |
+
If allow_load=False, this function will not download/load a missing adapter.
|
| 388 |
+
This keeps generate() fast and avoids hidden loading inside @spaces.GPU.
|
| 389 |
+
"""
|
| 390 |
+
global _active_lora, _active_lora_label, _active_lora_strength
|
| 391 |
+
|
| 392 |
+
transformer = getattr(pipe, "transformer", None)
|
| 393 |
+
if transformer is None:
|
| 394 |
+
raise RuntimeError("Pipeline has no transformer.")
|
| 395 |
+
|
| 396 |
+
cfg = KNOWN_LORAS.get(lora_label)
|
| 397 |
+
|
| 398 |
+
if not cfg:
|
| 399 |
+
if hasattr(transformer, "disable_lora"):
|
| 400 |
+
transformer.disable_lora()
|
| 401 |
+
elif hasattr(transformer, "set_adapters"):
|
| 402 |
+
try:
|
| 403 |
+
transformer.set_adapters([], adapter_weights=[])
|
| 404 |
+
except Exception:
|
| 405 |
+
pass
|
| 406 |
+
|
| 407 |
+
_active_lora = None
|
| 408 |
+
_active_lora_label = "Base model"
|
| 409 |
+
_active_lora_strength = 1.0
|
| 410 |
+
return ""
|
| 411 |
+
|
| 412 |
+
adapter_name = cfg["adapter_name"]
|
| 413 |
+
|
| 414 |
+
if adapter_name not in _loaded_loras:
|
| 415 |
+
if not allow_load:
|
| 416 |
+
raise RuntimeError(
|
| 417 |
+
f"LoRA '{lora_label}' is selected but not loaded. "
|
| 418 |
+
"Click 'Load selected LoRA' before generating."
|
| 419 |
+
)
|
| 420 |
+
adapter_name = _load_lora_if_needed(pipe, lora_label)
|
| 421 |
+
|
| 422 |
+
if hasattr(transformer, "enable_lora"):
|
| 423 |
+
transformer.enable_lora()
|
| 424 |
+
|
| 425 |
+
if hasattr(transformer, "set_adapters"):
|
| 426 |
+
transformer.set_adapters(
|
| 427 |
+
[adapter_name],
|
| 428 |
+
adapter_weights=[float(lora_strength)],
|
| 429 |
+
)
|
| 430 |
+
elif hasattr(transformer, "set_adapter"):
|
| 431 |
+
transformer.set_adapter(adapter_name)
|
| 432 |
+
|
| 433 |
+
_active_lora = adapter_name
|
| 434 |
+
_active_lora_label = lora_label
|
| 435 |
+
_active_lora_strength = float(lora_strength)
|
| 436 |
+
|
| 437 |
+
return cfg.get("trigger", "").strip()
|
| 438 |
+
|
| 439 |
+
|
| 440 |
+
def load_selected_lora(lora_style, lora_strength):
|
| 441 |
+
"""
|
| 442 |
+
Manual LoRA loading button.
|
| 443 |
+
|
| 444 |
+
Not decorated with @spaces.GPU on purpose:
|
| 445 |
+
loading the adapter should happen before generation and not consume
|
| 446 |
+
the generation reservation window.
|
| 447 |
+
"""
|
| 448 |
+
pipe = _load_pipeline()
|
| 449 |
+
if pipe is None:
|
| 450 |
+
return {
|
| 451 |
+
"status": "error",
|
| 452 |
+
"error": _pipeline_error or "Pipeline failed to load",
|
| 453 |
+
"runtime": _runtime_report(),
|
| 454 |
+
"lora": _lora_report(),
|
| 455 |
+
}
|
| 456 |
+
|
| 457 |
+
if lora_style == "Base model":
|
| 458 |
+
with _lora_lock:
|
| 459 |
+
_set_lora(pipe, "Base model", 1.0, allow_load=False)
|
| 460 |
+
|
| 461 |
+
return {
|
| 462 |
+
"status": "ready",
|
| 463 |
+
"message": "Base model active. No LoRA loaded.",
|
| 464 |
+
"runtime": _runtime_report(),
|
| 465 |
+
"lora": _lora_report(),
|
| 466 |
+
}
|
| 467 |
+
|
| 468 |
+
try:
|
| 469 |
+
with _lora_lock:
|
| 470 |
+
trigger = _set_lora(
|
| 471 |
+
pipe,
|
| 472 |
+
lora_style,
|
| 473 |
+
float(lora_strength),
|
| 474 |
+
allow_load=True,
|
| 475 |
+
)
|
| 476 |
+
|
| 477 |
+
return {
|
| 478 |
+
"status": "ready",
|
| 479 |
+
"message": f"LoRA loaded and activated: {lora_style}",
|
| 480 |
+
"trigger": trigger,
|
| 481 |
+
"runtime": _runtime_report(),
|
| 482 |
+
"lora": _lora_report(),
|
| 483 |
+
}
|
| 484 |
+
|
| 485 |
+
except Exception as e:
|
| 486 |
+
traceback.print_exc()
|
| 487 |
+
return {
|
| 488 |
+
"status": "error",
|
| 489 |
+
"error": f"{type(e).__name__}: {e}",
|
| 490 |
+
"runtime": _runtime_report(),
|
| 491 |
+
"lora": _lora_report(),
|
| 492 |
+
}
|
| 493 |
+
|
| 494 |
+
|
| 495 |
+
def disable_lora():
|
| 496 |
+
"""
|
| 497 |
+
Disable LoRA and return to base model.
|
| 498 |
+
|
| 499 |
+
This does not necessarily remove the adapter from memory; it only disables it.
|
| 500 |
+
Keeping the adapter cached makes switching back faster.
|
| 501 |
+
"""
|
| 502 |
+
pipe = _load_pipeline()
|
| 503 |
+
if pipe is None:
|
| 504 |
+
return {
|
| 505 |
+
"status": "error",
|
| 506 |
+
"error": _pipeline_error or "Pipeline failed to load",
|
| 507 |
+
"runtime": _runtime_report(),
|
| 508 |
+
"lora": _lora_report(),
|
| 509 |
+
}
|
| 510 |
+
|
| 511 |
+
try:
|
| 512 |
+
with _lora_lock:
|
| 513 |
+
_set_lora(pipe, "Base model", 1.0, allow_load=False)
|
| 514 |
+
|
| 515 |
+
return {
|
| 516 |
+
"status": "ready",
|
| 517 |
+
"message": "LoRA disabled. Base model active.",
|
| 518 |
+
"runtime": _runtime_report(),
|
| 519 |
+
"lora": _lora_report(),
|
| 520 |
+
}
|
| 521 |
+
|
| 522 |
+
except Exception as e:
|
| 523 |
+
traceback.print_exc()
|
| 524 |
+
return {
|
| 525 |
+
"status": "error",
|
| 526 |
+
"error": f"{type(e).__name__}: {e}",
|
| 527 |
+
"runtime": _runtime_report(),
|
| 528 |
+
"lora": _lora_report(),
|
| 529 |
+
}
|
| 530 |
+
|
| 531 |
+
|
| 532 |
# ---------------------------------------------------------------------------
|
| 533 |
# Eager app runtime warm-up
|
| 534 |
# ---------------------------------------------------------------------------
|
|
|
|
| 538 |
|
| 539 |
|
| 540 |
# ---------------------------------------------------------------------------
|
| 541 |
+
# Health / warm-up endpoints
|
| 542 |
# ---------------------------------------------------------------------------
|
| 543 |
|
| 544 |
def health():
|
|
|
|
| 553 |
"last_error": _pipeline_error or "",
|
| 554 |
"expected_output_type": "video",
|
| 555 |
"runtime": _runtime_report(),
|
| 556 |
+
"lora": _lora_report(),
|
| 557 |
}
|
| 558 |
|
| 559 |
|
|
|
|
| 560 |
def warmup_model():
|
| 561 |
"""
|
| 562 |
Manual warm-up button.
|
|
|
|
| 570 |
"status": "error",
|
| 571 |
"error": _pipeline_error or "Pipeline failed to load",
|
| 572 |
"runtime": _runtime_report(),
|
| 573 |
+
"lora": _lora_report(),
|
| 574 |
}
|
| 575 |
|
| 576 |
return {
|
|
|
|
| 579 |
"runtime_mode": "zerogpu_compatibility_compile_bypass",
|
| 580 |
"message": "Model loaded and cached in this Space process.",
|
| 581 |
"runtime": _runtime_report(),
|
| 582 |
+
"lora": _lora_report(),
|
| 583 |
}
|
| 584 |
|
| 585 |
|
|
|
|
| 587 |
# Generation endpoint — real inference guarded by @spaces.GPU
|
| 588 |
# ---------------------------------------------------------------------------
|
| 589 |
|
| 590 |
+
def _gpu_duration(
|
| 591 |
+
prompt,
|
| 592 |
+
lora_style,
|
| 593 |
+
lora_strength,
|
| 594 |
+
num_blocks,
|
| 595 |
+
num_inference_steps,
|
| 596 |
+
seed,
|
| 597 |
+
*args,
|
| 598 |
+
**kwargs,
|
| 599 |
+
):
|
| 600 |
try:
|
| 601 |
blocks = int(num_blocks)
|
| 602 |
steps = int(num_inference_steps)
|
| 603 |
except Exception:
|
| 604 |
+
blocks = 9
|
| 605 |
+
steps = 6
|
| 606 |
|
| 607 |
+
# Model and LoRA are expected to be loaded before generation.
|
| 608 |
+
# Observed on this Space:
|
| 609 |
+
# 9 blocks × 4 steps < 75s
|
| 610 |
+
# 9 blocks �� 8 steps < 80s
|
| 611 |
#
|
| 612 |
+
# Keep a safety buffer without over-reserving ZeroGPU.
|
| 613 |
+
return min(120, max(30, int(35 + blocks * steps * 1.2)))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 614 |
|
| 615 |
|
| 616 |
@_spaces_gpu(duration=_gpu_duration, size="xlarge")
|
| 617 |
def generate(
|
| 618 |
prompt,
|
| 619 |
+
lora_style,
|
| 620 |
+
lora_strength,
|
| 621 |
num_blocks,
|
| 622 |
num_inference_steps,
|
| 623 |
seed,
|
|
|
|
| 635 |
num_blocks = int(num_blocks)
|
| 636 |
num_inference_steps = int(num_inference_steps)
|
| 637 |
seed = int(seed)
|
| 638 |
+
lora_strength = float(lora_strength)
|
| 639 |
|
| 640 |
+
if num_blocks < 1 or num_blocks > 12:
|
| 641 |
+
raise ValueError("num_blocks must be between 1 and 12.")
|
| 642 |
|
| 643 |
if num_inference_steps < 1 or num_inference_steps > 8:
|
| 644 |
raise ValueError("num_inference_steps must be between 1 and 8.")
|
|
|
|
| 651 |
except Exception as e:
|
| 652 |
_log(f"Pipeline .to('cuda') warning: {type(e).__name__}: {e}")
|
| 653 |
|
| 654 |
+
# Activate selected LoRA without hidden loading during generation.
|
| 655 |
+
# If user selected a LoRA but did not load it, fail with a clear message.
|
| 656 |
+
with _lora_lock:
|
| 657 |
+
trigger = _set_lora(
|
| 658 |
+
pipe,
|
| 659 |
+
lora_style,
|
| 660 |
+
lora_strength,
|
| 661 |
+
allow_load=False,
|
| 662 |
+
)
|
| 663 |
+
|
| 664 |
+
final_prompt = prompt.strip()
|
| 665 |
+
if trigger and not final_prompt.startswith(trigger):
|
| 666 |
+
final_prompt = f"{trigger} {final_prompt}"
|
| 667 |
+
|
| 668 |
frames = []
|
| 669 |
state = PipelineState()
|
| 670 |
|
|
|
|
| 690 |
|
| 691 |
state = pipe(
|
| 692 |
state,
|
| 693 |
+
prompt=[final_prompt],
|
| 694 |
num_inference_steps=num_inference_steps,
|
| 695 |
num_blocks=num_blocks,
|
| 696 |
block_idx=block_idx,
|
|
|
|
| 734 |
"This Space runs **real local inference** for the Krea Realtime 14B "
|
| 735 |
"text-to-video model using Diffusers `ModularPipeline`.\n\n"
|
| 736 |
"⚠️ **ZeroGPU compatibility mode**: `torch.compile` is disabled because "
|
| 737 |
+
"ZeroGPU does not support it.\n\n"
|
| 738 |
"**Video length** is controlled by the number of blocks. "
|
| 739 |
+
"Roughly: 1 block ≈ ~1 second, 9 blocks ≈ ~9 seconds. "
|
| 740 |
+
"Values above 9 are experimental.\n\n"
|
| 741 |
+
"**LoRA support**: select a style, click **Load selected LoRA**, then generate. "
|
| 742 |
+
"The Origami preset automatically prefixes the prompt with `[origami]`."
|
| 743 |
)
|
| 744 |
|
| 745 |
with gr.Row():
|
| 746 |
with gr.Column():
|
| 747 |
+
model_status = gr.JSON(label="Model Status")
|
| 748 |
+
|
| 749 |
+
with gr.Row():
|
| 750 |
+
warmup_btn = gr.Button("Warm up / Check model", variant="secondary")
|
| 751 |
+
|
| 752 |
+
gr.Markdown("## Prompt")
|
| 753 |
+
|
| 754 |
prompt = gr.Textbox(
|
| 755 |
label="Prompt",
|
| 756 |
placeholder="e.g., a cat sitting on a boat",
|
| 757 |
+
lines=3,
|
| 758 |
+
)
|
| 759 |
+
|
| 760 |
+
gr.Markdown("## Style / LoRA")
|
| 761 |
+
|
| 762 |
+
lora_style = gr.Dropdown(
|
| 763 |
+
choices=list(KNOWN_LORAS.keys()),
|
| 764 |
+
value="Base model",
|
| 765 |
+
label="Style / LoRA",
|
| 766 |
)
|
| 767 |
|
| 768 |
+
lora_strength = gr.Slider(
|
| 769 |
+
minimum=0.0,
|
| 770 |
+
maximum=1.5,
|
| 771 |
+
value=1.0,
|
| 772 |
+
step=0.05,
|
| 773 |
+
label="LoRA Strength",
|
| 774 |
+
)
|
| 775 |
+
|
| 776 |
+
with gr.Row():
|
| 777 |
+
load_lora_btn = gr.Button("Load selected LoRA", variant="secondary")
|
| 778 |
+
disable_lora_btn = gr.Button("Disable LoRA / Use Base Model", variant="secondary")
|
| 779 |
+
|
| 780 |
+
lora_status = gr.JSON(label="LoRA Status")
|
| 781 |
+
|
| 782 |
+
gr.Markdown("## Generation Settings")
|
| 783 |
+
|
| 784 |
num_blocks = gr.Slider(
|
| 785 |
minimum=1,
|
| 786 |
+
maximum=12,
|
| 787 |
+
value=9,
|
| 788 |
step=1,
|
| 789 |
label="Video Length / Number of Blocks",
|
| 790 |
)
|
|
|
|
| 792 |
num_inference_steps = gr.Slider(
|
| 793 |
minimum=1,
|
| 794 |
maximum=8,
|
| 795 |
+
value=6,
|
| 796 |
step=1,
|
| 797 |
label="Inference Steps per Block",
|
| 798 |
)
|
| 799 |
|
| 800 |
seed = gr.Number(value=42, precision=0, label="Seed")
|
| 801 |
|
| 802 |
+
generate_btn = gr.Button("Generate Video", variant="primary")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 803 |
|
| 804 |
with gr.Column():
|
| 805 |
output_video = gr.Video(label="Generated Video")
|
| 806 |
|
| 807 |
gr.Examples(
|
| 808 |
examples=[
|
| 809 |
+
[
|
| 810 |
+
"Astronaut in a jungle, cold color palette, muted colors, detailed, cinematic, 8k",
|
| 811 |
+
"Base model",
|
| 812 |
+
1.0,
|
| 813 |
+
9,
|
| 814 |
+
6,
|
| 815 |
+
42,
|
| 816 |
+
],
|
| 817 |
+
[
|
| 818 |
+
"A tiny wooden boat drifting through a misty lake at sunrise, a curious cat sitting at the front, soft cinematic lighting, calm water reflections",
|
| 819 |
+
"Base model",
|
| 820 |
+
1.0,
|
| 821 |
+
9,
|
| 822 |
+
6,
|
| 823 |
+
123,
|
| 824 |
+
],
|
| 825 |
+
[
|
| 826 |
+
"A futuristic city at sunset, flying vehicles between glass towers, neon reflections, cinematic camera movement, atmospheric haze",
|
| 827 |
+
"Base model",
|
| 828 |
+
1.0,
|
| 829 |
+
9,
|
| 830 |
+
6,
|
| 831 |
+
7,
|
| 832 |
+
],
|
| 833 |
+
[
|
| 834 |
+
"A car racing down a snowy mountain road, dramatic chase shot, powder snow flying behind the wheels, cold blue lighting, high speed motion",
|
| 835 |
+
"Base model",
|
| 836 |
+
1.0,
|
| 837 |
+
9,
|
| 838 |
+
6,
|
| 839 |
+
99,
|
| 840 |
+
],
|
| 841 |
+
[
|
| 842 |
+
"A surreal underwater library, glowing jellyfish floating between bookshelves, slow cinematic dolly shot, dreamlike atmosphere",
|
| 843 |
+
"Base model",
|
| 844 |
+
1.0,
|
| 845 |
+
9,
|
| 846 |
+
6,
|
| 847 |
+
314,
|
| 848 |
+
],
|
| 849 |
+
[
|
| 850 |
+
"a cat sitting on a boat",
|
| 851 |
+
"Origami",
|
| 852 |
+
1.0,
|
| 853 |
+
9,
|
| 854 |
+
6,
|
| 855 |
+
2026,
|
| 856 |
+
],
|
| 857 |
+
[
|
| 858 |
+
"a dragon flying over a mountain village at sunrise, paper-folded geometry, delicate handmade texture, soft shadows",
|
| 859 |
+
"Origami",
|
| 860 |
+
1.0,
|
| 861 |
+
9,
|
| 862 |
+
6,
|
| 863 |
+
777,
|
| 864 |
+
],
|
| 865 |
+
[
|
| 866 |
+
"a small fox walking through a paper forest, handcrafted origami style, warm lantern light, cinematic close-up",
|
| 867 |
+
"Origami",
|
| 868 |
+
0.9,
|
| 869 |
+
9,
|
| 870 |
+
6,
|
| 871 |
+
888,
|
| 872 |
+
],
|
| 873 |
+
],
|
| 874 |
+
inputs=[
|
| 875 |
+
prompt,
|
| 876 |
+
lora_style,
|
| 877 |
+
lora_strength,
|
| 878 |
+
num_blocks,
|
| 879 |
+
num_inference_steps,
|
| 880 |
+
seed,
|
| 881 |
],
|
|
|
|
| 882 |
outputs=output_video,
|
| 883 |
fn=generate,
|
| 884 |
cache_examples=False,
|
|
|
|
| 887 |
warmup_btn.click(
|
| 888 |
warmup_model,
|
| 889 |
inputs=None,
|
| 890 |
+
outputs=model_status,
|
| 891 |
api_name="warmup",
|
| 892 |
)
|
| 893 |
|
| 894 |
+
load_lora_btn.click(
|
| 895 |
+
load_selected_lora,
|
| 896 |
+
inputs=[lora_style, lora_strength],
|
| 897 |
+
outputs=lora_status,
|
| 898 |
+
api_name="load_lora",
|
| 899 |
+
)
|
| 900 |
+
|
| 901 |
+
disable_lora_btn.click(
|
| 902 |
+
disable_lora,
|
| 903 |
+
inputs=None,
|
| 904 |
+
outputs=lora_status,
|
| 905 |
+
api_name="disable_lora",
|
| 906 |
+
)
|
| 907 |
+
|
| 908 |
generate_btn.click(
|
| 909 |
generate,
|
| 910 |
+
inputs=[
|
| 911 |
+
prompt,
|
| 912 |
+
lora_style,
|
| 913 |
+
lora_strength,
|
| 914 |
+
num_blocks,
|
| 915 |
+
num_inference_steps,
|
| 916 |
+
seed,
|
| 917 |
+
],
|
| 918 |
outputs=output_video,
|
| 919 |
api_name="generate",
|
| 920 |
)
|
|
|
|
| 922 |
demo.load(
|
| 923 |
lambda: health(),
|
| 924 |
inputs=None,
|
| 925 |
+
outputs=model_status,
|
| 926 |
api_name="health",
|
| 927 |
)
|
| 928 |
|