Update worker_runpod.py
Browse files- worker_runpod.py +28 -2
worker_runpod.py
CHANGED
@@ -3,6 +3,8 @@ import os
|
|
3 |
import runpod
|
4 |
import numpy as np
|
5 |
import torch
|
|
|
|
|
6 |
from diffusers import (AutoencoderKL, CogVideoXDDIMScheduler, DDIMScheduler,
|
7 |
DPMSolverMultistepScheduler,
|
8 |
EulerAncestralDiscreteScheduler, EulerDiscreteScheduler,
|
@@ -22,6 +24,29 @@ tokenxf = os.getenv("HF_API_TOKEN")
|
|
22 |
# Low GPU memory mode
|
23 |
low_gpu_memory_mode = False
|
24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
# Model loading section
|
26 |
model_id = "/content/model"
|
27 |
transformer = CogVideoXTransformer3DModel.from_pretrained_2d(
|
@@ -82,11 +107,12 @@ def generate(input):
|
|
82 |
partial_video_length = values.get("partial_video_length", None)
|
83 |
overlap_video_length = values.get("overlap_video_length", 4)
|
84 |
validation_image_start = values.get("validation_image_start", "asset/1.png")
|
|
|
85 |
validation_image_end = values.get("validation_image_end", None)
|
86 |
|
87 |
generator = torch.Generator(device="cuda").manual_seed(seed)
|
88 |
aspect_ratio_sample_size = {key : [x / 512 * base_resolution for x in ASPECT_RATIO_512[key]] for key in ASPECT_RATIO_512.keys()}
|
89 |
-
start_img = Image.open(
|
90 |
original_width, original_height = start_img.size
|
91 |
closest_size, closest_ratio = get_closest_ratio(original_height, original_width, ratios=aspect_ratio_sample_size)
|
92 |
height, width = [int(x / 16) * 16 for x in closest_size]
|
@@ -97,7 +123,7 @@ def generate(input):
|
|
97 |
else:
|
98 |
# Standard video generation
|
99 |
video_length = int((video_length - 1) // vae.config.temporal_compression_ratio * vae.config.temporal_compression_ratio) + 1 if video_length != 1 else 1
|
100 |
-
input_video, input_video_mask, clip_image = get_image_to_video_latent(
|
101 |
|
102 |
with torch.no_grad():
|
103 |
sample = pipeline(
|
|
|
3 |
import runpod
|
4 |
import numpy as np
|
5 |
import torch
|
6 |
+
import requests
|
7 |
+
import uuid
|
8 |
from diffusers import (AutoencoderKL, CogVideoXDDIMScheduler, DDIMScheduler,
|
9 |
DPMSolverMultistepScheduler,
|
10 |
EulerAncestralDiscreteScheduler, EulerDiscreteScheduler,
|
|
|
24 |
# Low GPU memory mode
|
25 |
low_gpu_memory_mode = False
|
26 |
|
27 |
+
def download_image(url, download_dir="asset"):
|
28 |
+
# Ensure the download directory exists
|
29 |
+
if not os.path.exists(download_dir):
|
30 |
+
os.makedirs(download_dir, exist_ok=True)
|
31 |
+
|
32 |
+
# Generate a random filename with a .png extension
|
33 |
+
filename = f"{uuid.uuid4().hex}.png"
|
34 |
+
file_path = os.path.join(download_dir, filename)
|
35 |
+
|
36 |
+
# Download the image and save it
|
37 |
+
response = requests.get(url, stream=True)
|
38 |
+
if response.status_code == 200:
|
39 |
+
with open(file_path, "wb") as f:
|
40 |
+
for chunk in response.iter_content(1024):
|
41 |
+
f.write(chunk)
|
42 |
+
print(f"Image downloaded to {file_path}")
|
43 |
+
return file_path
|
44 |
+
else:
|
45 |
+
raise Exception(f"Failed to download image from {url}, status code: {response.status_code}")
|
46 |
+
|
47 |
+
# Usage
|
48 |
+
# validation_image_start = values.get("validation_image_start", "https://example.com/path/to/image.png")
|
49 |
+
# downloaded_image_path = download_image(validation_image_start)
|
50 |
# Model loading section
|
51 |
model_id = "/content/model"
|
52 |
transformer = CogVideoXTransformer3DModel.from_pretrained_2d(
|
|
|
107 |
partial_video_length = values.get("partial_video_length", None)
|
108 |
overlap_video_length = values.get("overlap_video_length", 4)
|
109 |
validation_image_start = values.get("validation_image_start", "asset/1.png")
|
110 |
+
downloaded_image_path = download_image(validation_image_start)
|
111 |
validation_image_end = values.get("validation_image_end", None)
|
112 |
|
113 |
generator = torch.Generator(device="cuda").manual_seed(seed)
|
114 |
aspect_ratio_sample_size = {key : [x / 512 * base_resolution for x in ASPECT_RATIO_512[key]] for key in ASPECT_RATIO_512.keys()}
|
115 |
+
start_img = Image.open(downloaded_image_path)
|
116 |
original_width, original_height = start_img.size
|
117 |
closest_size, closest_ratio = get_closest_ratio(original_height, original_width, ratios=aspect_ratio_sample_size)
|
118 |
height, width = [int(x / 16) * 16 for x in closest_size]
|
|
|
123 |
else:
|
124 |
# Standard video generation
|
125 |
video_length = int((video_length - 1) // vae.config.temporal_compression_ratio * vae.config.temporal_compression_ratio) + 1 if video_length != 1 else 1
|
126 |
+
input_video, input_video_mask, clip_image = get_image_to_video_latent(downloaded_image_path, validation_image_end, video_length=video_length, sample_size=sample_size)
|
127 |
|
128 |
with torch.no_grad():
|
129 |
sample = pipeline(
|