benibraz commited on
Commit
be22e58
1 Parent(s): 300ca95

Enhance video generation error handling and memory management

Browse files
Files changed (1) hide show
  1. app.py +42 -24
app.py CHANGED
@@ -17,6 +17,7 @@ import cv2
17
  from PIL import Image
18
  import tempfile
19
  import os
 
20
 
21
  # Load Hugging Face token if needed
22
  hf_token = os.getenv("HF_TOKEN")
@@ -198,24 +199,34 @@ def generate_video_from_text(
198
  def gradio_progress_callback(self, step, timestep, kwargs):
199
  progress((step + 1) / num_inference_steps)
200
 
201
- with torch.no_grad():
202
- images = pipeline(
203
- num_inference_steps=num_inference_steps,
204
- num_images_per_prompt=1,
205
- guidance_scale=guidance_scale,
206
- generator=generator,
207
- output_type="pt",
208
- height=height,
209
- width=width,
210
- num_frames=num_frames,
211
- frame_rate=frame_rate,
212
- **sample,
213
- is_video=True,
214
- vae_per_channel_normalize=True,
215
- conditioning_method=ConditioningMethod.FIRST_FRAME,
216
- mixed_precision=True,
217
- callback_on_step_end=gradio_progress_callback,
218
- ).images
 
 
 
 
 
 
 
 
 
 
219
 
220
  output_path = tempfile.mktemp(suffix=".mp4")
221
  print(images.shape)
@@ -257,7 +268,9 @@ def generate_video_from_image(
257
  if not image_path:
258
  raise gr.Error("Please provide an input image.", duration=5)
259
 
260
- media_items = load_image_to_tensor_with_resize(image_path, height, width).to(device).detach()
 
 
261
 
262
  sample = {
263
  "prompt": prompt,
@@ -271,6 +284,7 @@ def generate_video_from_image(
271
 
272
  def gradio_progress_callback(self, step, timestep, kwargs):
273
  progress((step + 1) / num_inference_steps)
 
274
  try:
275
  with torch.no_grad():
276
  images = pipeline(
@@ -301,13 +315,17 @@ def generate_video_from_image(
301
  for frame in video_np[..., ::-1]:
302
  out.write(frame)
303
  out.release()
 
 
 
 
 
 
304
  finally:
305
- del media_items
306
- del images
307
- del video_np
308
- gc.collect()
309
  torch.cuda.empty_cache()
310
-
 
311
  return output_path
312
 
313
 
 
17
  from PIL import Image
18
  import tempfile
19
  import os
20
+ import gc
21
 
22
  # Load Hugging Face token if needed
23
  hf_token = os.getenv("HF_TOKEN")
 
199
  def gradio_progress_callback(self, step, timestep, kwargs):
200
  progress((step + 1) / num_inference_steps)
201
 
202
+ try:
203
+ with torch.no_grad():
204
+ images = pipeline(
205
+ num_inference_steps=num_inference_steps,
206
+ num_images_per_prompt=1,
207
+ guidance_scale=guidance_scale,
208
+ generator=generator,
209
+ output_type="pt",
210
+ height=height,
211
+ width=width,
212
+ num_frames=num_frames,
213
+ frame_rate=frame_rate,
214
+ **sample,
215
+ is_video=True,
216
+ vae_per_channel_normalize=True,
217
+ conditioning_method=ConditioningMethod.FIRST_FRAME,
218
+ mixed_precision=True,
219
+ callback_on_step_end=gradio_progress_callback,
220
+ ).images
221
+ except Exception as e:
222
+ raise gr.Error(
223
+ f"An error occurred while generating the video. Please try again. Error: {e}",
224
+ duration=5,
225
+ )
226
+ finally:
227
+ pipeline.to("cpu")
228
+ torch.cuda.empty_cache()
229
+ gc.collect()
230
 
231
  output_path = tempfile.mktemp(suffix=".mp4")
232
  print(images.shape)
 
268
  if not image_path:
269
  raise gr.Error("Please provide an input image.", duration=5)
270
 
271
+ media_items = (
272
+ load_image_to_tensor_with_resize(image_path, height, width).to(device).detach()
273
+ )
274
 
275
  sample = {
276
  "prompt": prompt,
 
284
 
285
  def gradio_progress_callback(self, step, timestep, kwargs):
286
  progress((step + 1) / num_inference_steps)
287
+
288
  try:
289
  with torch.no_grad():
290
  images = pipeline(
 
315
  for frame in video_np[..., ::-1]:
316
  out.write(frame)
317
  out.release()
318
+ except Exception as e:
319
+ raise gr.Error(
320
+ f"An error occurred while generating the video. Please try again. Error: {e}",
321
+ duration=5,
322
+ )
323
+
324
  finally:
325
+ pipeline.to("cpu")
 
 
 
326
  torch.cuda.empty_cache()
327
+ gc.collect()
328
+
329
  return output_path
330
 
331