smooth_gen / app.py
alrobles's picture
Create app.py
956168c verified
Raw History Blame Contribute Delete
29 kB
"""
Stop-motion Studio — un solo archivo, tres capacidades:
A) Fluidez: dedupe de poses -> deflicker -> RIFE (lo que ya funcionó)
B) Limpieza: detecta manos (YOLO) y/o zona rectangular -> LaMa inpaint
C) Transiciones: FLF2V generativo entre el final de un clip y el inicio
del siguiente, encadenando N clips en una secuencia.
Orden recomendado: B (limpiar) -> A (suavizar) -> C (encadenar).
"""
import os
import shutil
import subprocess
import sys
import tempfile
import uuid
from pathlib import Path
import gradio as gr
import numpy as np
from PIL import Image
from imageio_ffmpeg import get_ffmpeg_exe
try:
import spaces
HAS_SPACES = True
except Exception:
HAS_SPACES = False
try:
import torch
HAS_TORCH = True
except Exception:
HAS_TORCH = False
try:
import cv2
HAS_CV2 = True
except Exception:
HAS_CV2 = False
FFMPEG = get_ffmpeg_exe()
WORK = Path(tempfile.gettempdir()) / "smstudio"
WORK.mkdir(parents=True, exist_ok=True)
MAX_SIDE = 1280
THUMB = (96, 54)
MAX_POSES = 400
MAX_OUT_FRAMES = 1500
MAX_SEQ_CLIPS = 6
RIFE_SPACE = "X-HighVoltage-X/RIFE-Video-Frame-Interpolation"
RIFE_WEIGHTS = ("r3gm/RIFE", "RIFEv4.26_0921.zip")
FLF_SPACE = "dream2589632147/Dream-wan2-2-fp8da-aoti-preview-2"
HAND_MODEL = ("Bingsu/adetailer", "hand_yolov8n.pt")
# ==========================================================================
# Vídeo I/O
# ==========================================================================
def extract_frames(video_path, out_dir, max_side=MAX_SIDE, fps=None):
out_dir = Path(out_dir)
out_dir.mkdir(parents=True, exist_ok=True)
vf = ("scale=w='if(gte(iw,ih),min({0},iw),-2)':"
"h='if(gte(iw,ih),-2,min({0},ih))'").format(max_side)
if fps:
vf = "fps={},{}".format(fps, vf)
cmd = [FFMPEG, "-y", "-i", str(video_path), "-vf", vf,
"-fps_mode", "passthrough", "-pix_fmt", "rgb24",
str(out_dir / "%05d.png")]
p = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
frames = sorted(out_dir.glob("*.png"))
if p.returncode != 0 or not frames:
raise gr.Error("No pude decodificar el vídeo. Recomplímelo a H.264 "
"(iPhone: Atajos > Codificar multimedia > HD 1080p).\n\n"
+ p.stderr.decode(errors="ignore")[-400:])
return frames
def encode_png_sequence(sequence_dir, fps, out_path):
cmd = [FFMPEG, "-y", "-framerate", str(fps),
"-i", str(Path(sequence_dir) / "%05d.png"),
"-c:v", "libx264", "-preset", "fast", "-crf", "16",
"-pix_fmt", "yuv420p", "-movflags", "+faststart", str(out_path)]
p = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if p.returncode != 0:
raise gr.Error("Fallo al montar el vídeo:\n"
+ p.stderr.decode(errors="ignore")[-500:])
def encode_numpy_frames(frames, fps, out_path):
h, w = frames[0].shape[:2]
cmd = [FFMPEG, "-y", "-f", "rawvideo", "-pix_fmt", "rgb24",
"-s", "{}x{}".format(w, h), "-r", str(fps), "-i", "-",
"-c:v", "libx264", "-preset", "fast", "-crf", "16",
"-pix_fmt", "yuv420p", "-movflags", "+faststart", str(out_path)]
p = subprocess.Popen(cmd, stdin=subprocess.PIPE,
stdout=subprocess.DEVNULL, stderr=subprocess.PIPE)
try:
for fr in frames:
p.stdin.write(fr.tobytes())
p.stdin.close()
except BrokenPipeError:
pass
err = p.stderr.read().decode(errors="ignore")[-500:]
if p.wait() != 0:
raise gr.Error("Fallo al codificar:\n" + err)
def read_pngs(paths):
out = []
for p in paths:
with Image.open(p) as im:
out.append(np.asarray(im.convert("RGB"), dtype=np.uint8))
return out
def write_pngs(frames, out_dir):
out_dir = Path(out_dir)
out_dir.mkdir(parents=True, exist_ok=True)
for i, fr in enumerate(frames, start=1):
Image.fromarray(fr).save(out_dir / ("%05d.png" % i))
return sorted(out_dir.glob("*.png"))
def thumb(path):
with Image.open(path) as im:
g = im.convert("L").resize(THUMB, Image.BILINEAR)
return np.asarray(g, dtype=np.float32)
def dedupe_poses(frames, threshold):
kept = [frames[0]]
last = thumb(frames[0])
for f in frames[1:]:
t = thumb(f)
if float(np.abs(t - last).mean()) > threshold:
kept.append(f)
last = t
return kept
def deflicker_frames(kept, out_dir):
out_dir = Path(out_dir)
out_dir.mkdir(parents=True, exist_ok=True)
lums = []
for p in kept:
with Image.open(p) as im:
lums.append(float(np.asarray(im.convert("L"), np.float32).mean()))
target = float(np.median(lums))
result = []
for i, p in enumerate(kept):
with Image.open(p) as im:
arr = np.asarray(im.convert("RGB"), np.float32)
if lums[i] > 1.0:
arr = np.clip(arr * min(1.25, max(0.80, target / lums[i])), 0, 255)
dst = out_dir / ("%05d.png" % (i + 1))
Image.fromarray(arr.astype(np.uint8)).save(dst)
result.append(dst)
return result
# ==========================================================================
# RIFE
# ==========================================================================
_NET = None
def _rife_pkg_dir():
base = Path(tempfile.gettempdir()) / "rife_pkg"
if (base / "train_log" / "IFNet_HDv3.py").exists():
return base
from huggingface_hub import snapshot_download
src = Path(snapshot_download(RIFE_SPACE, repo_type="space",
allow_patterns=["model/*", "train_log/*"]))
base.mkdir(parents=True, exist_ok=True)
for sub in ("model", "train_log"):
s = src / sub
if s.exists() and not (base / sub).exists():
shutil.copytree(s, base / sub)
return base
def _load_net():
global _NET
if _NET is not None:
return _NET
import zipfile
from huggingface_hub import hf_hub_download
base = _rife_pkg_dir()
if str(base) not in sys.path:
sys.path.insert(0, str(base))
from train_log.IFNet_HDv3 import IFNet
zip_path = hf_hub_download(RIFE_WEIGHTS[0], RIFE_WEIGHTS[1])
wf = Path(tempfile.gettempdir()) / "rife_w" / "flownet.pkl"
if not wf.exists():
wf.parent.mkdir(parents=True, exist_ok=True)
with zipfile.ZipFile(zip_path) as z:
for name in z.namelist():
if name.endswith("flownet.pkl"):
with z.open(name) as s, open(wf, "wb") as d:
shutil.copyfileobj(s, d)
break
state = torch.load(str(wf), map_location="cpu")
if any("module." in k for k in state):
state = {k.replace("module.", ""): v for k, v in state.items()
if "module." in k}
net = IFNet()
res = net.load_state_dict(state, strict=False)
if len(res.missing_keys) > 3:
raise RuntimeError("Pesos RIFE incompletos")
net.eval()
_NET = net
return net
def _interp_worker(frames, mult):
net = _load_net()
device = "cuda" if torch.cuda.is_available() else "cpu"
if device == "cpu":
raise RuntimeError("Sin GPU")
net = net.to(device)
h, w = frames[0].shape[:2]
ph, pw = ((h - 1) // 64 + 1) * 64, ((w - 1) // 64 + 1) * 64
def to_t(fr):
t = torch.from_numpy(fr).to(device).permute(2, 0, 1).float().unsqueeze(0) / 255.0
return torch.nn.functional.pad(t, (0, pw - w, 0, ph - h))
out = []
with torch.inference_mode():
for i in range(len(frames) - 1):
t0, t1 = to_t(frames[i]), to_t(frames[i + 1])
out.append(frames[i])
for k in range(1, mult):
_, _, merged = net(torch.cat((t0, t1), 1), k / mult, [16, 8, 4, 2, 1])
fr = merged[-1][0][:, :h, :w].permute(1, 2, 0) * 255.0
out.append(fr.clamp(0, 255).byte().cpu().numpy())
del t0, t1
out.append(frames[-1])
if torch.cuda.is_available():
torch.cuda.empty_cache()
return out
# ==========================================================================
# Limpieza: YOLO manos + LaMa
# ==========================================================================
_YOLO = None
_LAMA = None
def _get_yolo():
global _YOLO
if _YOLO is None:
from huggingface_hub import hf_hub_download
from ultralytics import YOLO
_YOLO = YOLO(hf_hub_download(HAND_MODEL[0], HAND_MODEL[1]))
return _YOLO
def _get_lama():
global _LAMA
if _LAMA is not None:
return _LAMA
subprocess.run([sys.executable, "-m", "pip", "install", "--no-deps", "-q",
"simple-lama-inpainting==0.1.2"], check=False)
try:
from simple_lama_inpainting import SimpleLama
dev = "cuda" if torch.cuda.is_available() else "cpu"
try:
_LAMA = ("lama", SimpleLama(device=torch.device(dev)))
except TypeError:
m = SimpleLama()
if hasattr(m, "model"):
m.model.to(dev)
_LAMA = ("lama", m)
except Exception:
_LAMA = ("cv2", None)
return _LAMA
def _inpaint(arr, mask, engine):
kind, model = engine
if kind == "lama":
out = model(Image.fromarray(arr), Image.fromarray(mask))
return np.asarray(out.convert("RGB"), dtype=np.uint8)
bgr = cv2.cvtColor(arr, cv2.COLOR_RGB2BGR)
fixed = cv2.inpaint(bgr, mask, 3, cv2.INPAINT_TELEA)
return cv2.cvtColor(fixed, cv2.COLOR_BGR2RGB)
def _clean_worker(frames, use_hands, dilate_px, rect, ext_mask):
if not HAS_CV2:
raise RuntimeError("cv2 no disponible")
yolo = _get_yolo() if use_hands else None
engine = _get_lama()
out, touched = [], 0
for arr in frames:
mask = np.zeros(arr.shape[:2], np.uint8)
if rect is not None:
mask = np.maximum(mask, rect)
if ext_mask is not None:
if ext_mask.shape != mask.shape:
ext_mask = cv2.resize(ext_mask, (mask.shape[1], mask.shape[0]),
interpolation=cv2.INTER_NEAREST)
mask = np.maximum(mask, ext_mask)
if yolo is not None:
for r in yolo.predict(arr, verbose=False, conf=0.25):
if r.boxes is None:
continue
for b in r.boxes.xyxy.cpu().numpy().astype(int):
x1, y1, x2, y2 = b
mask[max(0, y1):y2, max(0, x1):x2] = 255
if dilate_px and dilate_px > 0:
k = np.ones((int(dilate_px) * 2 + 1,) * 2, np.uint8)
mask = cv2.dilate(mask, k)
if int(mask.max()) == 0:
out.append(arr)
continue
out.append(_inpaint(arr, mask, engine))
touched += 1
return out, touched
if HAS_SPACES:
_clean_gpu = spaces.GPU(duration=120)(_clean_worker)
_interp_gpu = spaces.GPU(duration=120)(_interp_worker)
else:
_clean_gpu = _clean_worker
_interp_gpu = _interp_worker
# ==========================================================================
# Puente generativo (FLF2V por API)
# ==========================================================================
def _client(space_id, token="", request=None):
"""Orden de búsqueda del token:
1) campo de la UI 2) sesión del navegador (x-ip-token) 3) secreto.
"""
from gradio_client import Client
tok = (token or "").strip()
if not tok and request is not None:
try:
tok = request.headers.get("x-ip-token", "") or ""
except Exception:
tok = ""
if not tok:
tok = os.environ.get("HF_TOKEN", "") or ""
if tok:
return Client(space_id.strip(), headers={"x-ip-token": tok})
return Client(space_id.strip())
def _pick_endpoint(cli):
"""Elige el endpoint de generación. Primero candidatos conocidos,
luego heurística: un endpoint con 2 imágenes + prompt."""
try:
info = cli.view_api(return_format="dict")
named = info.get("named_endpoints", {}) or {}
except Exception:
return None
for cand in ("/generate_video", "/flf2v", "/generate", "/predict"):
if cand in named:
return cand
for name, meta in named.items():
pn = [(p.get("parameter_name") or p.get("name") or "").lower()
for p in meta.get("parameters", [])]
has_first = any("first" in n for n in pn)
has_last = any(("last" in n) or n.endswith("_end") for n in pn)
has_prompt = any("prompt" in n and "negative" not in n for n in pn)
if has_first and has_last and has_prompt:
return name
return None
def _build_args(cli, api, start_png, end_png, prompt, duration):
"""Construye la lista COMPLETA de argumentos posicionales según el schema
del endpoint. Así funciona con endpoints de 3 o de 24 parámetros."""
from gradio_client import handle_file
info = cli.view_api(return_format="dict")
params = info["named_endpoints"][api].get("parameters", [])
args = []
for p in params:
pn = (p.get("parameter_name") or p.get("name") or "").lower()
if "first" in pn:
args.append(handle_file(start_png))
elif "last" in pn:
args.append(handle_file(end_png))
elif "negative" in pn:
args.append(p.get("parameter_default"))
elif "prompt" in pn:
args.append(prompt)
elif "generation_mode" in pn or pn in ("mode", "task"):
args.append("First + Last Frame")
elif "duration" in pn:
args.append(float(duration))
elif "safe_mode" in pn:
args.append(True)
elif p.get("parameter_has_default"):
args.append(p.get("parameter_default"))
else:
args.append(None)
return args
def _find_video(obj):
if isinstance(obj, str):
low = obj.lower()
if low.endswith((".mp4", ".webm", ".mov", ".gif")):
return obj
return None
if isinstance(obj, dict):
for v in obj.values():
r = _find_video(v)
if r:
return r
return None
if isinstance(obj, (list, tuple)):
for v in obj:
r = _find_video(v)
if r:
return r
return None
def call_flf2v(space_id, token, start_png, end_png, prompt, api_override,
request=None, duration=3.5):
cli = _client(space_id, token, request)
api = (api_override or "").strip() or _pick_endpoint(cli)
if not api:
raise gr.Error(
"No encontré un endpoint de generación en ese Space. Abre su "
"pestaña 'Use via API' y pega el nombre exacto en el campo API."
)
args = _build_args(cli, api, start_png, end_png, prompt, duration)
res = cli.predict(*args, api_name=api)
vid = _find_video(res)
if not vid:
raise gr.Error("El Space respondió pero no devolvió un vídeo.")
return vid
def _median_frame(paths):
stack = np.stack([np.asarray(Image.open(p).convert("RGB"), np.float32)
for p in paths])
return Image.fromarray(np.median(stack, axis=0).astype(np.uint8))
# ==========================================================================
# Tarea A: fluidez
# ==========================================================================
def task_fluidez(video, sensitivity, pose_fps, mult, do_deflicker, do_rife,
progress=gr.Progress()):
if video is None:
raise gr.Error("Sube un vídeo.")
job = WORK / uuid.uuid4().hex[:10]
frames = extract_frames(video, job / "raw")
progress(0.25, desc="Detectando poses")
thr = 0.5 + (100.0 - float(sensitivity)) * 0.15
kept = dedupe_poses(frames, thr)
if len(kept) < 3:
raise gr.Error("Solo {} poses. Sube la sensibilidad.".format(len(kept)))
if len(kept) > MAX_POSES:
raise gr.Error("{} poses, demasiadas. Baja la sensibilidad.".format(len(kept)))
if do_deflicker:
progress(0.4, desc="Igualando exposición")
kept = deflicker_frames(kept, job / "poses")
else:
pose_dir = job / "poses"
pose_dir.mkdir(parents=True, exist_ok=True)
for i, p in enumerate(kept):
shutil.copy(p, pose_dir / ("%05d.png" % (i + 1)))
progress(0.5, desc="Montando poses")
poses_video = job / "poses.mp4"
encode_png_sequence(job / "poses", pose_fps, poses_video)
final, done, warn = poses_video, False, ""
if do_rife:
paths = sorted((job / "poses").glob("*.png"))
if len(paths) * int(mult) > MAX_OUT_FRAMES:
warn = "Interpolación omitida: demasiados frames."
else:
progress(0.6, desc="Interpolando (RIFE)")
try:
out = _interp_gpu(read_pngs(paths), int(mult))
final = job / "final.mp4"
encode_numpy_frames(out, float(pose_fps) * int(mult), final)
done = True
except Exception as exc:
warn = "RIFE no disponible ({}). Usa el vídeo de poses.".format(
type(exc).__name__)
fps_txt = "{} fps".format(int(pose_fps) * int(mult)) if done else "{} fps".format(pose_fps)
info = ("### Hecho\n- Frames leídos: **{}**\n- Poses: **{}**\n"
"- Salida: **{}**\n- Deflicker: **{}** | RIFE x{}: **{}**\n{}"
).format(len(frames), len(kept), fps_txt,
"sí" if do_deflicker else "no", mult,
"sí" if done else "no",
("\n\n> ⚠️ " + warn) if warn else "")
return str(final), str(poses_video), info
# ==========================================================================
# Tarea B: limpieza
# ==========================================================================
def task_limpieza(video, mode, dilate_px, x1, y1, x2, y2, mask_img,
progress=gr.Progress()):
if video is None:
raise gr.Error("Sube un vídeo.")
job = WORK / uuid.uuid4().hex[:10]
frames = extract_frames(video, job / "raw")
if len(frames) > 480:
raise gr.Error("Demasiados frames ({}). Usa un clip más corto."
.format(len(frames)))
arrs = read_pngs(frames)
h, w = arrs[0].shape[:2]
rect = None
if x2 > x1 and y2 > y1:
rect = np.zeros((h, w), np.uint8)
rect[int(y1 * h / 100):int(y2 * h / 100),
int(x1 * w / 100):int(x2 * w / 100)] = 255
ext = None
if mask_img is not None:
ext = np.asarray(Image.fromarray(mask_img).convert("L"), np.uint8)
_, ext = cv2.threshold(ext, 127, 255, cv2.THRESH_BINARY)
use_hands = mode in ("Manos (auto)", "Manos + rectángulo")
if rect is not None and mode == "Manos (auto)":
rect = None
if rect is not None:
use_hands = mode in ("Manos (auto)", "Manos + rectángulo")
progress(0.2, desc="Detectando e inpaintando")
try:
out, touched = _clean_gpu(arrs, use_hands, int(dilate_px), rect, ext)
except Exception as exc:
raise gr.Error("Falló la limpieza ({}). Prueba con menos frames o "
"sin detección automática.".format(exc))
progress(0.8, desc="Codificando")
paths = write_pngs(out, job / "clean")
cleaned = job / "clean.mp4"
encode_png_sequence(job / "clean", 30, cleaned)
preview = job / "preview.png"
Image.fromarray(out[len(out) // 2]).save(preview)
engine = _get_lama()[0] if _LAMA else "?"
info = ("### Limpieza\n- Frames: **{}**\n- Frames modificados: **{}**\n"
"- Motor de inpaint: **{}**\n- Manos automáticas: **{}**\n\n"
"> El vídeo se reencoda a **30 fps**; si tu clip tenía otra "
"cadencia, pásalo luego por la pestaña Fluidez."
).format(len(frames), touched, engine, "sí" if use_hands else "no")
return str(cleaned), str(preview), info
# ==========================================================================
# Tarea C: transiciones generativas + secuencia
# ==========================================================================
def _clip_frames(video, pose_fps, sensitivity):
"""Devuelve los frames PNG de un clip ya ritmados a pose_fps."""
job = WORK / uuid.uuid4().hex[:10]
frames = extract_frames(video, job / "raw")
thr = 0.5 + (100.0 - float(sensitivity)) * 0.15
kept = dedupe_poses(frames, thr)
return kept
def task_secuencia(videos, sensitivity, pose_fps, smooth, mult, space_id, token,
api_override, prompt, progress=gr.Progress(),
request: gr.Request = None):
clips = [v for v in (videos or []) if v]
if len(clips) < 2:
raise gr.Error("Sube al menos 2 clips.")
if len(clips) > MAX_SEQ_CLIPS:
raise gr.Error("Máximo {} clips.".format(MAX_SEQ_CLIPS))
job = WORK / uuid.uuid4().hex[:10]
job.mkdir(parents=True, exist_ok=True)
n_joints = len(clips) - 1
log = []
segments = []
for idx, clip in enumerate(clips):
progress(idx / (len(clips) * 2.0), desc="Analizando clip {}".format(idx + 1))
pose_paths = _clip_frames(clip, pose_fps, sensitivity)
arrs = read_pngs(pose_paths)
segments.append(arrs)
log.append("clip {}: {} poses".format(idx + 1, len(pose_paths)))
transitions = []
for idx in range(n_joints):
progress(0.5 + idx / (n_joints * 4.0),
desc="Generando transición {} -> {}".format(idx + 1, idx + 2))
pa = _clip_frames(clips[idx], pose_fps, sensitivity)
pb = _clip_frames(clips[idx + 1], pose_fps, sensitivity)
start_img = job / ("start_%d.png" % idx)
end_img = job / ("end_%d.png" % idx)
_median_frame(pa[-3:] if len(pa) >= 3 else pa).save(start_img)
_median_frame(pb[:3]).save(end_img)
try:
gen = call_flf2v(space_id, token, str(start_img), str(end_img),
prompt, api_override, request)
except Exception as exc:
raise gr.Error(
"Falló la transición {} ({}). Revisa el Space y el token; "
"el resto del pipeline sigue disponible.".format(idx + 1, exc))
gen_frames = extract_frames(gen, job / ("gen_%d" % idx), fps=pose_fps)
if len(gen_frames) < 2:
raise gr.Error("La transición {} salió vacía.".format(idx + 1))
transitions.append(read_pngs(gen_frames))
log.append("transición {}: {} frames a {} fps".format(
idx + 1, len(gen_frames), pose_fps))
# concatenar con fundido de 3 frames en las uniones
seq = []
for i, seg in enumerate(segments):
if i == 0:
seq.extend(seg)
continue
prev, tr = transitions[i - 1], None
seq.extend(tr if tr else [])
seg2 = list(seg)
if len(seq) >= 3 and len(seg2) >= 3:
n = 3
tail = seq[-n:]
head = seg2[:n]
blend = []
for k in range(n):
a = 1.0 - (k + 1) / (n + 1.0)
base = tail[k].astype(np.float32) * a + head[k].astype(np.float32) * (1 - a)
blend.append(base.astype(np.uint8))
seq[-n:] = blend
seq.extend(seg2)
else:
seq.extend(seg2)
seq = segments[0] + transitions[0] if n_joints == 1 else seq
if n_joints == 1:
seq = list(segments[0]) + list(transitions[0]) + list(segments[1])
progress(0.9, desc="Montando secuencia")
final_fps = float(pose_fps)
final_frames = seq
if smooth and int(mult) > 1:
try:
final_frames = _interp_gpu(seq, int(mult))
final_fps = float(pose_fps) * int(mult)
except Exception as exc:
log.append("RIFE omitido: {}".format(type(exc).__name__))
out = job / "secuencia.mp4"
encode_numpy_frames(final_frames, final_fps, out)
info = ("### Secuencia\n- Clips: **{}**\n- Transiciones generadas: **{}**\n"
"- Fps de salida: **{}**\n\n{}\n").format(
len(clips), n_joints, int(final_fps), "\n".join("- " + l for l in log))
return str(out), info
# ==========================================================================
# UI
# ==========================================================================
with gr.Blocks(title="Stop-motion Studio") as demo:
gr.Markdown("# 🎬 Stop-motion Studio\n"
"**Orden recomendado:** 1) Limpieza → 2) Fluidez → 3) Secuencia")
with gr.Tab("1 · Limpieza (manos / objetos)"):
with gr.Row():
with gr.Column():
l_video = gr.Video(label="Vídeo")
l_mode = gr.Radio(["Manos (auto)", "Manos + rectángulo",
"Solo rectángulo"],
value="Manos (auto)", label="Qué quitar")
l_dilate = gr.Slider(0, 30, value=8, step=1,
label="Expandir máscara (px)")
with gr.Accordion("Zona rectangular (si aplica)", open=False):
l_x1 = gr.Slider(0, 100, value=0, label="x1 %")
l_y1 = gr.Slider(0, 100, value=0, label="y1 %")
l_x2 = gr.Slider(0, 100, value=0, label="x2 %")
l_y2 = gr.Slider(0, 100, value=0, label="y2 %")
l_mask = gr.Image(label="Máscara extra (opcional, blanco = borrar)",
image_mode="L")
l_run = gr.Button("Limpiar", variant="primary")
with gr.Column():
l_out = gr.Video(label="Vídeo limpio")
l_prev = gr.Image(label="Fotograma de muestra")
l_info = gr.Markdown()
l_run.click(task_limpieza,
[l_video, l_mode, l_dilate, l_x1, l_y1, l_x2, l_y2, l_mask],
[l_out, l_prev, l_info])
with gr.Tab("2 · Fluidez"):
with gr.Row():
with gr.Column():
f_video = gr.Video(label="Vídeo")
f_sens = gr.Slider(40, 99, value=85, step=1,
label="Sensibilidad al cambio de pose")
f_fps = gr.Dropdown([6, 8, 10, 12], value=8,
label="Fps de la secuencia de poses")
f_mult = gr.Radio([2, 4, 6, 8], value=6, label="Multiplicador RIFE")
f_deflick = gr.Checkbox(value=True, label="Igualar exposición")
f_rife = gr.Checkbox(value=True, label="Interpolar con RIFE")
f_run = gr.Button("Procesar", variant="primary")
with gr.Column():
f_out = gr.Video(label="Vídeo final")
f_poses = gr.Video(label="Solo poses")
f_info = gr.Markdown()
f_run.click(task_fluidez,
[f_video, f_sens, f_fps, f_mult, f_deflick, f_rife],
[f_out, f_poses, f_info])
with gr.Tab("3 · Secuencia con transiciones"):
gr.Markdown("Sube 2-6 clips. Entre cada par se genera un tramo con "
"first+last frame y todo se encadena a la misma cadencia.")
with gr.Row():
with gr.Column():
s_videos = gr.File(file_count="multiple",
file_types=["video"],
label="Clips (en orden)")
s_sens = gr.Slider(40, 99, value=85, step=1,
label="Sensibilidad al cambio de pose")
s_fps = gr.Dropdown([6, 8, 10, 12], value=8,
label="Fps base de la secuencia")
s_smooth = gr.Checkbox(value=True, label="Suavizar secuencia con RIFE")
s_mult = gr.Radio([2, 4, 6], value=4, label="Multiplicador RIFE")
with gr.Accordion("Generador (API)", open=True):
s_space = gr.Textbox(value=FLF_SPACE, label="Space generativo")
s_token = gr.Textbox(type="password", label="HF token (tu cuenta PRO)")
s_api = gr.Textbox(label="Endpoint (vacío = autodetectar)",
placeholder="/generate_video")
s_prompt = gr.Textbox(
value=("continuación suave del movimiento, misma cámara, "
"misma iluminación, mismo personaje"),
label="Prompt de la transición")
s_run = gr.Button("Generar secuencia", variant="primary")
with gr.Column():
s_out = gr.Video(label="Secuencia final")
s_info = gr.Markdown()
s_run.click(task_secuencia,
[s_videos, s_sens, s_fps, s_smooth, s_mult, s_space,
s_token, s_api, s_prompt],
[s_out, s_info])
demo.queue(max_size=8).launch()