Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,6 @@
|
|
| 1 |
import os
|
| 2 |
-
import sys
|
| 3 |
-
import uuid
|
| 4 |
-
import shutil
|
| 5 |
-
import time
|
| 6 |
-
import gradio as gr
|
| 7 |
import torch
|
|
|
|
| 8 |
from diffusers import StableVideoDiffusionPipeline
|
| 9 |
from PIL import Image
|
| 10 |
import numpy as np
|
|
@@ -12,127 +8,72 @@ import cv2
|
|
| 12 |
import tempfile
|
| 13 |
from diffusers.utils import export_to_video
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
torch_dtype=
|
| 22 |
variant="fp16"
|
| 23 |
)
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
Wan-Animate supports two modes:
|
| 85 |
-
|
| 86 |
-
* Move Mode: Use the movements extracted from the input video to drive the character in the input image
|
| 87 |
-
|
| 88 |
-
* Mix Mode: Use the character in the input image to replace the character in the input video
|
| 89 |
-
|
| 90 |
-
Currently, the following restrictions apply to inputs:
|
| 91 |
-
|
| 92 |
-
* Video file size: Less than 200MB
|
| 93 |
-
|
| 94 |
-
* Video resolution: The shorter side must be greater than 200, and the longer side must be less than 2048
|
| 95 |
-
|
| 96 |
-
* Video duration: 2s to 30s
|
| 97 |
-
|
| 98 |
-
* Video aspect ratio: 1:3 to 3:1
|
| 99 |
-
|
| 100 |
-
* Video formats: mp4, avi, mov
|
| 101 |
-
|
| 102 |
-
* Image file size: Less than 5MB
|
| 103 |
-
|
| 104 |
-
* Image resolution: The shorter side must be greater than 200, and the longer side must be less than 4096
|
| 105 |
-
|
| 106 |
-
* Image formats: jpg, png, jpeg, webp, bmp
|
| 107 |
-
|
| 108 |
-
Current, the inference quality has two variants. You can use our open-source code for more flexible configuration.
|
| 109 |
-
|
| 110 |
-
* wan-pro: 25fps, 720p
|
| 111 |
-
|
| 112 |
-
* wan-std: 15fps, 720p
|
| 113 |
-
""")
|
| 114 |
-
|
| 115 |
-
with gr.Row():
|
| 116 |
-
with gr.Column():
|
| 117 |
-
ref_img = gr.Image(label="Reference Image (изображение)", type="numpy", sources=["upload"])
|
| 118 |
-
video = gr.Video(label="Template Video (шаблонное видео)", sources=["upload"])
|
| 119 |
-
with gr.Row():
|
| 120 |
-
model_id = gr.Dropdown(label="Mode (режим)", choices=["wan2.2-animate-move", "wan2.2-animate-mix"], value="wan2.2-animate-move")
|
| 121 |
-
model = gr.Dropdown(label="Inference Quality (качество)", choices=["wan-pro", "wan-std"], value="wan-pro")
|
| 122 |
-
run_button = gr.Button("Generate Video (генерировать)")
|
| 123 |
-
|
| 124 |
-
with gr.Column():
|
| 125 |
-
output_video = gr.Video(label="Output Video (результат)")
|
| 126 |
-
output_status = gr.Textbox(label="Status (статус)")
|
| 127 |
-
|
| 128 |
-
run_button.click(
|
| 129 |
-
fn=app.predict,
|
| 130 |
-
inputs=[ref_img, video, model_id, model],
|
| 131 |
-
outputs=[output_video, output_status]
|
| 132 |
-
)
|
| 133 |
-
|
| 134 |
-
demo.queue(default_concurrency_limit=1)
|
| 135 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
| 136 |
-
|
| 137 |
-
if __name__ == "__main__":
|
| 138 |
-
start_app()
|
|
|
|
| 1 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import torch
|
| 3 |
+
import gradio as gr
|
| 4 |
from diffusers import StableVideoDiffusionPipeline
|
| 5 |
from PIL import Image
|
| 6 |
import numpy as np
|
|
|
|
| 8 |
import tempfile
|
| 9 |
from diffusers.utils import export_to_video
|
| 10 |
|
| 11 |
+
pipe = None
|
| 12 |
+
def load():
|
| 13 |
+
global pipe
|
| 14 |
+
if pipe is None:
|
| 15 |
+
pipe = StableVideoDiffusionPipeline.from_pretrained(
|
| 16 |
+
"stabilityai/stable-video-diffusion-img2vid-xt",
|
| 17 |
+
torch_dtype=torch.float16,
|
| 18 |
variant="fp16"
|
| 19 |
)
|
| 20 |
+
pipe.to("cuda")
|
| 21 |
+
gr.Info("Модель на GPU — генерация 30–60 сек")
|
| 22 |
+
return pipe
|
| 23 |
+
|
| 24 |
+
def run(ref_img, video, mode, quality, prog=gr.Progress()):
|
| 25 |
+
pipe = load()
|
| 26 |
+
prog(0, desc="Подготовка...")
|
| 27 |
+
img = Image.fromarray(ref_img).convert("RGB").resize((576, 320))
|
| 28 |
+
|
| 29 |
+
cap = cv2.VideoCapture(video)
|
| 30 |
+
n = int(cap.get(7)); cap.release()
|
| 31 |
+
hint = f" ({n} кадров)"
|
| 32 |
+
|
| 33 |
+
steps = 25 if quality == "wan-pro" else 15
|
| 34 |
+
frames = 25 if quality == "wan-pro" else 14
|
| 35 |
+
noise = 0.1 if mode == "wan2.2-animate-mix" else 0.02
|
| 36 |
+
|
| 37 |
+
def cb(step, *_):
|
| 38 |
+
prog((step+1)/steps, desc=f"Шаг {step+1}/{steps}")
|
| 39 |
+
prog(0.1, desc="Генерация...")
|
| 40 |
+
|
| 41 |
+
out = pipe(
|
| 42 |
+
img,
|
| 43 |
+
num_inference_steps=steps,
|
| 44 |
+
num_frames=frames,
|
| 45 |
+
decode_chunk_size=2,
|
| 46 |
+
noise_aug_strength=noise,
|
| 47 |
+
callback_on_step_end=cb
|
| 48 |
+
).frames[0]
|
| 49 |
+
|
| 50 |
+
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
|
| 51 |
+
export_to_video(out, tmp.name, fps=7)
|
| 52 |
+
return tmp.name, "Готово!" + hint
|
| 53 |
+
|
| 54 |
+
with gr.Blocks() as demo:
|
| 55 |
+
gr.Markdown("# Wan2.2-Animate (GPU)")
|
| 56 |
+
with gr.Accordion("Инструкция", open=False):
|
| 57 |
+
gr.Markdown("Загрузи фото + видео → выбери режим → жми Generate")
|
| 58 |
+
|
| 59 |
+
with gr.Row():
|
| 60 |
+
with gr.Column():
|
| 61 |
+
img = gr.Image(label="Фото", type="numpy")
|
| 62 |
+
vid = gr.Video(label="Видео")
|
| 63 |
+
with gr.Row():
|
| 64 |
+
mode = gr.Dropdown(["wan2.2-animate-move", "wan2.2-animate-mix"],
|
| 65 |
+
label="Режим", value="wan2.2-animate-move")
|
| 66 |
+
qual = gr.Dropdown(["wan-pro", "wan-std"], label="Качество", value="wan-pro")
|
| 67 |
+
btn = gr.Button("Generate Video")
|
| 68 |
+
with gr.Column():
|
| 69 |
+
out = gr.Video(label="Результат")
|
| 70 |
+
stat = gr.Textbox(label="Статус")
|
| 71 |
+
|
| 72 |
+
btn.click(run, [img, vid, mode, qual], [out, stat])
|
| 73 |
+
|
| 74 |
+
demo.queue(max_size=2).launch(
|
| 75 |
+
server_name="0.0.0.0",
|
| 76 |
+
server_port=7860,
|
| 77 |
+
share=True, # ← ФИКС 1
|
| 78 |
+
enable_queue=True
|
| 79 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|