Spaces:
Runtime error
Runtime error
Create deforum_engine.py
Browse files- deforum_engine.py +129 -0
deforum_engine.py
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch, os, uuid, zipfile, cv2, gc, random
|
| 2 |
+
import numpy as np
|
| 3 |
+
from diffusers import AutoPipelineForImage2Image, LCMScheduler, EulerAncestralDiscreteScheduler, DDIMScheduler, DPMSolverMultistepScheduler
|
| 4 |
+
from diffusers.models import AutoencoderTiny
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import utils
|
| 7 |
+
|
| 8 |
+
class DeforumRunner:
|
| 9 |
+
def __init__(self, device="cpu"):
|
| 10 |
+
self.device = device
|
| 11 |
+
self.pipe = None
|
| 12 |
+
self.stop_requested = False
|
| 13 |
+
self.current_config = (None, None, None)
|
| 14 |
+
|
| 15 |
+
def load_model(self, model_id, lora_id, scheduler_name):
|
| 16 |
+
if (model_id, lora_id, scheduler_name) == self.current_config and self.pipe: return
|
| 17 |
+
print(f"Loading: {model_id}...")
|
| 18 |
+
if self.pipe: del self.pipe; gc.collect()
|
| 19 |
+
|
| 20 |
+
# 1. Load Pipeline
|
| 21 |
+
try:
|
| 22 |
+
self.pipe = AutoPipelineForImage2Image.from_pretrained(
|
| 23 |
+
model_id, safety_checker=None, torch_dtype=torch.float32
|
| 24 |
+
)
|
| 25 |
+
except Exception as e:
|
| 26 |
+
print(f"Standard load failed: {e}. Trying explicit VAE fallback...")
|
| 27 |
+
# Fallback for SDXS or non-standard Repos
|
| 28 |
+
self.pipe = AutoPipelineForImage2Image.from_pretrained(
|
| 29 |
+
model_id, safety_checker=None, torch_dtype=torch.float32, use_safetensors=False
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
# 2. Handle LoRA
|
| 33 |
+
if lora_id and lora_id.lower() != "none":
|
| 34 |
+
try:
|
| 35 |
+
self.pipe.load_lora_weights(lora_id)
|
| 36 |
+
self.pipe.fuse_lora()
|
| 37 |
+
print(f"LoRA {lora_id} fused.")
|
| 38 |
+
except Exception as e: print(f"LoRA failed: {e}")
|
| 39 |
+
|
| 40 |
+
# 3. Handle Scheduler
|
| 41 |
+
config = self.pipe.scheduler.config
|
| 42 |
+
if scheduler_name == "LCM": self.pipe.scheduler = LCMScheduler.from_config(config)
|
| 43 |
+
elif scheduler_name == "Euler A": self.pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(config)
|
| 44 |
+
elif scheduler_name == "DDIM": self.pipe.scheduler = DDIMScheduler.from_config(config)
|
| 45 |
+
elif scheduler_name == "DPM++ 2M": self.pipe.scheduler = DPMSolverMultistepScheduler.from_config(config)
|
| 46 |
+
|
| 47 |
+
self.pipe.to(self.device)
|
| 48 |
+
self.pipe.enable_attention_slicing()
|
| 49 |
+
self.current_config = (model_id, lora_id, scheduler_name)
|
| 50 |
+
|
| 51 |
+
def stop(self): self.stop_requested = True
|
| 52 |
+
|
| 53 |
+
def render(self, prompts, neg, max_f, w, h, z_s, a_s, tx_s, ty_s, str_s, noi_s,
|
| 54 |
+
fps, steps, cfg, cadence, color_mode, border_mode, seed_beh, init_img,
|
| 55 |
+
model_id, lora_id, scheduler_name):
|
| 56 |
+
|
| 57 |
+
self.stop_requested = False
|
| 58 |
+
self.load_model(model_id, lora_id, scheduler_name)
|
| 59 |
+
|
| 60 |
+
# Schedules
|
| 61 |
+
keys = ['z','a','tx','ty','str','noi']
|
| 62 |
+
vals = [z_s, a_s, tx_s, ty_s, str_s, noi_s]
|
| 63 |
+
sched = {k: utils.parse_weight_string(v, max_f) for k, v in zip(keys, vals)}
|
| 64 |
+
|
| 65 |
+
run_id = uuid.uuid4().hex[:6]
|
| 66 |
+
os.makedirs(f"out_{run_id}", exist_ok=True)
|
| 67 |
+
|
| 68 |
+
# Initial State
|
| 69 |
+
prev_img = init_img.resize((w, h), Image.LANCZOS) if init_img else Image.new("RGB", (w, h), (127, 127, 127))
|
| 70 |
+
color_anchor = prev_img.copy()
|
| 71 |
+
frames = []
|
| 72 |
+
|
| 73 |
+
base_seed = random.randint(0, 2**32-1)
|
| 74 |
+
print(f"Run {run_id} | Model: {model_id} | Seed: {base_seed}")
|
| 75 |
+
|
| 76 |
+
for i in range(max_f):
|
| 77 |
+
if self.stop_requested: break
|
| 78 |
+
|
| 79 |
+
# Seed Logic
|
| 80 |
+
f_seed = base_seed if seed_beh == "fixed" else (random.randint(0, 2**32-1) if seed_beh == "random" else base_seed + i)
|
| 81 |
+
random.seed(f_seed); np.random.seed(f_seed); torch.manual_seed(f_seed)
|
| 82 |
+
|
| 83 |
+
# 1. WARP
|
| 84 |
+
args = {'angle':sched['a'][i], 'zoom':sched['z'][i], 'tx':sched['tx'][i], 'ty':sched['ty'][i]}
|
| 85 |
+
warped = utils.anim_frame_warp_2d(prev_img, args, border_mode)
|
| 86 |
+
|
| 87 |
+
# 2. DIFFUSE (Cadence)
|
| 88 |
+
if i % cadence == 0:
|
| 89 |
+
# Color & Noise
|
| 90 |
+
inp = utils.maintain_colors(warped, color_anchor, color_mode)
|
| 91 |
+
inp = utils.add_noise(inp, sched['noi'][i])
|
| 92 |
+
|
| 93 |
+
# Prompt
|
| 94 |
+
curr_prompt = prompts[max(k for k in prompts.keys() if k <= i)]
|
| 95 |
+
|
| 96 |
+
# Strength (Ensure non-zero steps)
|
| 97 |
+
strength = sched['str'][i]
|
| 98 |
+
if steps * strength < 1.0: strength = 1.1 / steps
|
| 99 |
+
|
| 100 |
+
# Generate
|
| 101 |
+
gen = self.pipe(
|
| 102 |
+
prompt=curr_prompt, negative_prompt=neg, image=inp,
|
| 103 |
+
num_inference_steps=steps, strength=strength, guidance_scale=cfg,
|
| 104 |
+
width=w, height=h
|
| 105 |
+
).images[0]
|
| 106 |
+
|
| 107 |
+
if color_mode != 'None': gen = utils.maintain_colors(gen, color_anchor, color_mode)
|
| 108 |
+
prev_img = gen
|
| 109 |
+
else:
|
| 110 |
+
# Turbo
|
| 111 |
+
gen = warped
|
| 112 |
+
prev_img = warped
|
| 113 |
+
|
| 114 |
+
frames.append(gen)
|
| 115 |
+
yield gen, None, None, f"Frame {i+1}/{max_f}"
|
| 116 |
+
|
| 117 |
+
# Save
|
| 118 |
+
vid_p = f"out_{run_id}/video.mp4"
|
| 119 |
+
out = cv2.VideoWriter(vid_p, cv2.VideoWriter_fourcc(*'mp4v'), fps, (w, h))
|
| 120 |
+
for f in frames: out.write(cv2.cvtColor(np.array(f), cv2.COLOR_RGB2BGR))
|
| 121 |
+
out.release()
|
| 122 |
+
|
| 123 |
+
zip_p = f"out_{run_id}/frames.zip"
|
| 124 |
+
with zipfile.ZipFile(zip_p, 'w') as zf:
|
| 125 |
+
for j, f in enumerate(frames):
|
| 126 |
+
buf = io.BytesIO(); f.save(buf, "PNG"); zf.writestr(f"{j:05d}.png", buf.getvalue())
|
| 127 |
+
import io # Re-import locally for safety
|
| 128 |
+
|
| 129 |
+
yield frames[-1], vid_p, zip_p, "Done"
|