Spaces:
Runtime error
Runtime error
Commit
·
aa5e404
1
Parent(s):
920b90f
oldisgold
Browse files- options/Video_model/Model.py +11 -78
options/Video_model/Model.py
CHANGED
|
@@ -1,85 +1,18 @@
|
|
| 1 |
import torch
|
| 2 |
-
from PIL import Image
|
| 3 |
-
import os
|
| 4 |
from diffusers import StableVideoDiffusionPipeline
|
| 5 |
-
from .
|
| 6 |
-
from
|
| 7 |
-
from glob import glob
|
| 8 |
-
from typing import Optional
|
| 9 |
-
|
| 10 |
-
# Define paths and device
|
| 11 |
-
svd_path = 'stabilityai/stable-video-diffusion-img2vid-xt-1-1'
|
| 12 |
-
lora_repo_path = 'RED-AIGC/TDD'
|
| 13 |
-
lora_weight_name = 'svd-xt-1-1_tdd_lora_weights.safetensors'
|
| 14 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 15 |
|
| 16 |
-
# Initialize the noise scheduler and pipeline
|
| 17 |
-
noise_scheduler = TDDSVDStochasticIterativeScheduler(
|
| 18 |
-
num_train_timesteps=250, sigma_min=0.002, sigma_max=700.0,
|
| 19 |
-
sigma_data=1.0, s_noise=1.0, rho=7, clip_denoised=False
|
| 20 |
-
)
|
| 21 |
pipeline = StableVideoDiffusionPipeline.from_pretrained(
|
| 22 |
-
|
| 23 |
-
)
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
# Video function definition
|
| 27 |
-
def Video(
|
| 28 |
-
image: Image,
|
| 29 |
-
seed: Optional[int] = 1,
|
| 30 |
-
randomize_seed: bool = False,
|
| 31 |
-
num_inference_steps: int = 4,
|
| 32 |
-
eta: float = 0.3,
|
| 33 |
-
min_guidance_scale: float = 1.0,
|
| 34 |
-
max_guidance_scale: float = 1.0,
|
| 35 |
-
fps: int = 7,
|
| 36 |
-
width: int = 512,
|
| 37 |
-
height: int = 512,
|
| 38 |
-
num_frames: int = 25,
|
| 39 |
-
motion_bucket_id: int = 127,
|
| 40 |
-
output_folder: str = "outputs_gradio",
|
| 41 |
-
):
|
| 42 |
-
# Set the eta value in the scheduler
|
| 43 |
-
pipeline.scheduler.set_eta(eta)
|
| 44 |
|
| 45 |
-
|
| 46 |
-
if randomize_seed:
|
| 47 |
-
seed = random.randint(0, 2**64 - 1)
|
| 48 |
-
generator = torch.manual_seed(seed)
|
| 49 |
-
|
| 50 |
-
# Ensure the image is converted to a format that the model can use
|
| 51 |
image = Image.fromarray(image)
|
| 52 |
-
|
| 53 |
-
base_count = len(glob(os.path.join(output_folder, "*.mp4")))
|
| 54 |
-
video_path = os.path.join(output_folder, f"{base_count:06d}.mp4")
|
| 55 |
-
|
| 56 |
-
# Perform computation with appropriate dtype based on device
|
| 57 |
-
# if device == "cuda":
|
| 58 |
-
# # Use float16 for GPU
|
| 59 |
-
# with torch.autocast(device_type='cuda', dtype=torch.float16):
|
| 60 |
-
# frames = pipeline(
|
| 61 |
-
# image, height=height, width=width,
|
| 62 |
-
# num_inference_steps=num_inference_steps,
|
| 63 |
-
# min_guidance_scale=min_guidance_scale,
|
| 64 |
-
# max_guidance_scale=max_guidance_scale,
|
| 65 |
-
# num_frames=num_frames, fps=fps, motion_bucket_id=motion_bucket_id,
|
| 66 |
-
# generator=generator,
|
| 67 |
-
# ).frames[0]
|
| 68 |
-
# else:
|
| 69 |
-
# Use bfloat16 for CPU as it's supported in torch.autocast
|
| 70 |
-
# with torch.autocast(device_type='cpu', dtype=torch.bfloat16):
|
| 71 |
-
frames = pipeline(
|
| 72 |
-
image, height=height, width=width,
|
| 73 |
-
num_inference_steps=num_inference_steps,
|
| 74 |
-
min_guidance_scale=min_guidance_scale,
|
| 75 |
-
max_guidance_scale=max_guidance_scale,
|
| 76 |
-
num_frames=num_frames, fps=fps, motion_bucket_id=motion_bucket_id,
|
| 77 |
-
generator=generator,
|
| 78 |
-
).frames[0]
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
# Save the generated video
|
| 82 |
-
save_video(frames, video_path, fps=fps, quality=5.0)
|
| 83 |
-
torch.manual_seed(seed)
|
| 84 |
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import torch
|
|
|
|
|
|
|
| 2 |
from diffusers import StableVideoDiffusionPipeline
|
| 3 |
+
from diffusers.utils import load_image, export_to_video
|
| 4 |
+
from PIL import Image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
pipeline = StableVideoDiffusionPipeline.from_pretrained(
|
| 7 |
+
"stabilityai/stable-video-diffusion-img2vid-xt-1-1", torch_dtype=torch.float16, variant="fp16"
|
| 8 |
+
)
|
| 9 |
+
pipeline.enable_model_cpu_offload()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
def Video(image):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
image = Image.fromarray(image)
|
| 13 |
+
image = image.resize((1024, 576))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
generator = torch.manual_seed(42)
|
| 16 |
+
frames = pipeline(image, decode_chunk_size=8, generator=generator).frames[0]
|
| 17 |
+
export_to_video(frames, "generated.mp4", fps=7)
|
| 18 |
+
return "generated.mp4"
|