EmanHassan26 commited on
Commit
7735f13
·
verified ·
1 Parent(s): a1d70a2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -1
app.py CHANGED
@@ -10,6 +10,8 @@ from PIL import Image
10
  import torch
11
  from transformers import AutoProcessor, AutoModelForCausalLM
12
  from diffusers import ControlNetModel, StableDiffusionControlNetPipeline, UniPCMultistepScheduler
 
 
13
 
14
  # Programmatically add the repository to PyTorch Hub's trusted list
15
  #torch.hub.set_dir('/root/.cache/torch/hub') # Optional: ensures clean cache path
@@ -158,7 +160,6 @@ def extract_depth_map(image_path):
158
  ).squeeze()
159
 
160
  depth_output = prediction.cpu().numpy()
161
-
162
  # Normalize pixel depth values into a visible 0-255 map array
163
  depth_min, depth_max = depth_output.min(), depth_output.max()
164
  normalized_depth = (255 * (depth_output - depth_min) / (depth_max - depth_min)).astype(np.uint8)
@@ -186,6 +187,8 @@ def generate_input_image(image_path,input_choice,seg_input_promt):
186
  print(f"image_path: '{image_path}'")
187
  print(f"input_choice: '{input_choice}'")
188
  image_path = image_path[0]
 
 
189
  # ["Edge image","Segmented image","Depth Image"]
190
  if input_choice == "Edge image" :
191
  conditioning_image = generate_edgeImage(image_path)
@@ -193,6 +196,8 @@ def generate_input_image(image_path,input_choice,seg_input_promt):
193
  conditioning_image = generate_segmt_img(image_path,seg_input_promt)
194
  else:
195
  conditioning_image = extract_depth_map(image_path)
 
 
196
  return conditioning_image
197
 
198
  @spaces.GPU
@@ -218,6 +223,8 @@ def generate_output_image(prompt,conditioning_image,input_choice):
218
  num_inference_steps=20,
219
  guidance_scale=8.0
220
  ).images[0]
 
 
221
  return generated_output
222
 
223
 
 
10
  import torch
11
  from transformers import AutoProcessor, AutoModelForCausalLM
12
  from diffusers import ControlNetModel, StableDiffusionControlNetPipeline, UniPCMultistepScheduler
13
+ import gc
14
+ import torch
15
 
16
  # Programmatically add the repository to PyTorch Hub's trusted list
17
  #torch.hub.set_dir('/root/.cache/torch/hub') # Optional: ensures clean cache path
 
160
  ).squeeze()
161
 
162
  depth_output = prediction.cpu().numpy()
 
163
  # Normalize pixel depth values into a visible 0-255 map array
164
  depth_min, depth_max = depth_output.min(), depth_output.max()
165
  normalized_depth = (255 * (depth_output - depth_min) / (depth_max - depth_min)).astype(np.uint8)
 
187
  print(f"image_path: '{image_path}'")
188
  print(f"input_choice: '{input_choice}'")
189
  image_path = image_path[0]
190
+ gc.collect()
191
+ torch.cuda.empty_cache()
192
  # ["Edge image","Segmented image","Depth Image"]
193
  if input_choice == "Edge image" :
194
  conditioning_image = generate_edgeImage(image_path)
 
196
  conditioning_image = generate_segmt_img(image_path,seg_input_promt)
197
  else:
198
  conditioning_image = extract_depth_map(image_path)
199
+ gc.collect()
200
+ torch.cuda.empty_cache()
201
  return conditioning_image
202
 
203
  @spaces.GPU
 
223
  num_inference_steps=20,
224
  guidance_scale=8.0
225
  ).images[0]
226
+ gc.collect()
227
+ torch.cuda.empty_cache()
228
  return generated_output
229
 
230