Spaces:
Sleeping
Sleeping
Update app_gradio.py
Browse files- app_gradio.py +63 -47
app_gradio.py
CHANGED
|
@@ -117,47 +117,67 @@ pipe = TextToVideoSDPipelineModded.from_pretrained(
|
|
| 117 |
@torch.no_grad()
|
| 118 |
def process_video(num_frames, num_seeds, generator, exp_dir, load_name, caption, lambda_):
|
| 119 |
pipe_inversion.to(device)
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
latents = id_latents.repeat(num_seeds, 1, 1, 1, 1)
|
| 122 |
generator = [torch.Generator(device="cuda").manual_seed(i) for i in range(num_seeds)]
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
gifs = []
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
|
|
|
| 147 |
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
|
| 155 |
-
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
| 157 |
|
| 158 |
return gifs
|
| 159 |
|
| 160 |
def generate_output(image, apply_filter, prompt: str, num_seeds: int = 3, lambda_value: float = 0.5, progress=gr.Progress(track_tqdm=True)) -> List[str]:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
if prompt is None:
|
| 162 |
raise gr.Error("You forgot to describe the motion !")
|
| 163 |
"""Main function to generate output GIFs"""
|
|
@@ -175,21 +195,17 @@ def generate_output(image, apply_filter, prompt: str, num_seeds: int = 3, lambda
|
|
| 175 |
|
| 176 |
image.save(temp_image_path)
|
| 177 |
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
except Exception as e:
|
| 190 |
-
torch.cuda.empty_cache() # Clear CUDA cache in case of failure
|
| 191 |
-
gc.collect()
|
| 192 |
-
raise gr.Error(f"Video processing failed: {str(e)}") from e
|
| 193 |
|
| 194 |
if apply_filter:
|
| 195 |
try:
|
|
|
|
| 117 |
@torch.no_grad()
|
| 118 |
def process_video(num_frames, num_seeds, generator, exp_dir, load_name, caption, lambda_):
|
| 119 |
pipe_inversion.to(device)
|
| 120 |
+
try:
|
| 121 |
+
id_latents = invert(pipe_inversion, inv, load_name).to(device, dtype=dtype)
|
| 122 |
+
except Exception as e:
|
| 123 |
+
torch.cuda.empty_cache() # Clear CUDA cache in case of failure
|
| 124 |
+
gc.collect()
|
| 125 |
+
raise gr.Error(f"Invert latents failed: {str(e)}") from e
|
| 126 |
+
|
| 127 |
latents = id_latents.repeat(num_seeds, 1, 1, 1, 1)
|
| 128 |
generator = [torch.Generator(device="cuda").manual_seed(i) for i in range(num_seeds)]
|
| 129 |
+
|
| 130 |
+
try:
|
| 131 |
+
video_frames = pipe(
|
| 132 |
+
prompt=caption,
|
| 133 |
+
negative_prompt="",
|
| 134 |
+
num_frames=num_frames,
|
| 135 |
+
num_inference_steps=25,
|
| 136 |
+
inv_latents=latents,
|
| 137 |
+
guidance_scale=9,
|
| 138 |
+
generator=generator,
|
| 139 |
+
lambda_=lambda_,
|
| 140 |
+
).frames
|
| 141 |
+
except Exception as e:
|
| 142 |
+
torch.cuda.empty_cache()
|
| 143 |
+
gc.collect()
|
| 144 |
+
raise RuntimeError(f"Failed to process video: {e}") from e
|
| 145 |
+
|
| 146 |
gifs = []
|
| 147 |
+
try:
|
| 148 |
+
for seed in range(num_seeds):
|
| 149 |
+
vid_name = f"{exp_dir}/mp4_logs/vid_{os.path.basename(load_name)[:-4]}-rand{seed}.mp4"
|
| 150 |
+
gif_name = f"{exp_dir}/gif_logs/vid_{os.path.basename(load_name)[:-4]}-rand{seed}.gif"
|
| 151 |
+
|
| 152 |
+
os.makedirs(os.path.dirname(vid_name), exist_ok=True)
|
| 153 |
+
os.makedirs(os.path.dirname(gif_name), exist_ok=True)
|
| 154 |
+
|
| 155 |
+
video_path = export_to_video(video_frames[seed], output_video_path=vid_name)
|
| 156 |
+
VideoFileClip(vid_name).write_gif(gif_name)
|
| 157 |
+
|
| 158 |
+
with Image.open(gif_name) as im:
|
| 159 |
+
frames = load_frames(im)
|
| 160 |
|
| 161 |
+
frames_collect = np.empty((0, 1024, 1024), int)
|
| 162 |
+
for frame in frames:
|
| 163 |
+
frame = cv2.resize(frame, (1024, 1024))[:, :, :3]
|
| 164 |
+
frame = cv2.cvtColor(255 - frame, cv2.COLOR_RGB2GRAY)
|
| 165 |
+
_, frame = cv2.threshold(255 - frame, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
|
| 166 |
+
frames_collect = np.append(frames_collect, [frame], axis=0)
|
| 167 |
|
| 168 |
+
save_gif(frames_collect, gif_name)
|
| 169 |
+
gifs.append(gif_name)
|
| 170 |
+
except Exception as e:
|
| 171 |
+
torch.cuda.empty_cache()
|
| 172 |
+
raise RuntimeError(f"Failed during GIF generation: {e}") from e
|
| 173 |
|
| 174 |
return gifs
|
| 175 |
|
| 176 |
def generate_output(image, apply_filter, prompt: str, num_seeds: int = 3, lambda_value: float = 0.5, progress=gr.Progress(track_tqdm=True)) -> List[str]:
|
| 177 |
+
gc.collect()
|
| 178 |
+
torch.cuda.empty_cache()
|
| 179 |
+
torch.cuda.ipc_collect()
|
| 180 |
+
|
| 181 |
if prompt is None:
|
| 182 |
raise gr.Error("You forgot to describe the motion !")
|
| 183 |
"""Main function to generate output GIFs"""
|
|
|
|
| 195 |
|
| 196 |
image.save(temp_image_path)
|
| 197 |
|
| 198 |
+
|
| 199 |
+
# Attempt to process video
|
| 200 |
+
generated_gifs = process_video(
|
| 201 |
+
num_frames=10,
|
| 202 |
+
num_seeds=num_seeds,
|
| 203 |
+
generator=None,
|
| 204 |
+
exp_dir=exp_dir,
|
| 205 |
+
load_name=temp_image_path,
|
| 206 |
+
caption=prompt,
|
| 207 |
+
lambda_=1 - lambda_value
|
| 208 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
|
| 210 |
if apply_filter:
|
| 211 |
try:
|