fffiloni commited on
Commit
13edd1a
·
verified ·
1 Parent(s): cf5b2d5

Update gradio_app.py

Browse files
Files changed (1) hide show
  1. gradio_app.py +44 -19
gradio_app.py CHANGED
@@ -11,6 +11,10 @@ from attn_ctrl.attention_control import (AttentionStore,
11
  register_temporal_self_attention_flip_control,
12
  )
13
  from torch.cuda.amp import autocast
 
 
 
 
14
 
15
  # Set up device
16
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
@@ -64,6 +68,7 @@ register_temporal_self_attention_flip_control(pipe.unet, controller, controller_
64
  def cuda_memory_cleanup():
65
  torch.cuda.empty_cache()
66
  torch.cuda.ipc_collect()
 
67
 
68
  def check_outputs_folder(folder_path):
69
  if os.path.exists(folder_path) and os.path.isdir(folder_path):
@@ -82,7 +87,7 @@ def check_outputs_folder(folder_path):
82
  @torch.no_grad()
83
  def infer(frame1_path, frame2_path):
84
  seed = 42
85
- num_inference_steps = 10
86
  noise_injection_steps = 0
87
  noise_injection_ratio = 0.5
88
  weighted_average = False
@@ -92,30 +97,50 @@ def infer(frame1_path, frame2_path):
92
  generator = generator.manual_seed(seed)
93
 
94
  frame1 = load_image(frame1_path)
95
- frame1 = frame1.resize((512, 288))
96
 
97
  frame2 = load_image(frame2_path)
98
- frame2 = frame2.resize((512, 288))
99
 
 
100
  cuda_memory_cleanup()
101
 
102
- with autocast():
103
- frames = pipe(image1=frame1, image2=frame2,
104
- num_inference_steps=num_inference_steps,
105
- generator=generator,
106
- weighted_average=weighted_average,
107
- noise_injection_steps=noise_injection_steps,
108
- noise_injection_ratio=noise_injection_ratio,
109
- ).frames[0]
110
-
111
- frames = [frame.cpu() for frame in frames]
112
-
113
- out_dir = "result"
114
- check_outputs_folder(out_dir)
115
- os.makedirs(out_dir, exist_ok=True)
116
- out_path = "result/video_result.gif"
117
 
118
- return "done"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
 
120
  @torch.no_grad()
121
  def load_model():
 
11
  register_temporal_self_attention_flip_control,
12
  )
13
  from torch.cuda.amp import autocast
14
+ import gc
15
+
16
+ # Set PYTORCH_CUDA_ALLOC_CONF
17
+ os.environ['PYTORCH_CUDA_ALLOC_CONF'] = 'expandable_segments:True'
18
 
19
  # Set up device
20
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 
68
  def cuda_memory_cleanup():
69
  torch.cuda.empty_cache()
70
  torch.cuda.ipc_collect()
71
+ gc.collect()
72
 
73
  def check_outputs_folder(folder_path):
74
  if os.path.exists(folder_path) and os.path.isdir(folder_path):
 
87
  @torch.no_grad()
88
  def infer(frame1_path, frame2_path):
89
  seed = 42
90
+ num_inference_steps = 5 # Reduced from 10
91
  noise_injection_steps = 0
92
  noise_injection_ratio = 0.5
93
  weighted_average = False
 
97
  generator = generator.manual_seed(seed)
98
 
99
  frame1 = load_image(frame1_path)
100
+ frame1 = frame1.resize((256, 144)) # Reduced from (512, 288)
101
 
102
  frame2 = load_image(frame2_path)
103
+ frame2 = frame2.resize((256, 144)) # Reduced from (512, 288)
104
 
105
+ # Clear CUDA cache
106
  cuda_memory_cleanup()
107
 
108
+ # Move model to CPU and clear CUDA cache
109
+ pipe.to("cpu")
110
+ cuda_memory_cleanup()
 
 
 
 
 
 
 
 
 
 
 
 
111
 
112
+ # Move model back to GPU
113
+ pipe.to(device)
114
+
115
+ try:
116
+ with autocast(device_type='cuda', dtype=torch.float16):
117
+ frames = pipe(
118
+ image1=frame1,
119
+ image2=frame2,
120
+ num_inference_steps=num_inference_steps,
121
+ generator=generator,
122
+ weighted_average=weighted_average,
123
+ noise_injection_steps=noise_injection_steps,
124
+ noise_injection_ratio=noise_injection_ratio,
125
+ ).frames[0]
126
+
127
+ frames = [frame.cpu() for frame in frames]
128
+
129
+ out_dir = "result"
130
+ check_outputs_folder(out_dir)
131
+ os.makedirs(out_dir, exist_ok=True)
132
+ out_path = "result/video_result.gif"
133
+
134
+ return "done"
135
+ except RuntimeError as e:
136
+ if "CUDA out of memory" in str(e):
137
+ return "Error: CUDA out of memory. Try reducing the image size or using fewer inference steps."
138
+ else:
139
+ return f"An error occurred: {str(e)}"
140
+ finally:
141
+ # Move model back to CPU and clear CUDA cache
142
+ pipe.to("cpu")
143
+ cuda_memory_cleanup()
144
 
145
  @torch.no_grad()
146
  def load_model():