alexnasa commited on
Commit
207f73a
·
verified ·
1 Parent(s): 38d2535

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -7
app.py CHANGED
@@ -22,15 +22,16 @@ import torch
22
  from safetensors.torch import load_file
23
  from huggingface_hub import hf_hub_download
24
  from diffusers import DiffusionPipeline
 
25
 
26
-
27
  def save_state_to_file(state):
28
  filename = "state.pkl"
29
  with open(filename, "wb") as f:
30
  pickle.dump(state, f)
31
  return filename
32
 
33
-
34
  def load_state_from_file(filename):
35
  with open(filename, "rb") as f:
36
  state = pickle.load(f)
@@ -47,10 +48,10 @@ blur_value = None
47
  allowed_res_max = 1.0
48
 
49
  # Device configuration
50
- device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
51
  print(f"Using device: {device}")
52
 
53
-
54
  def weight_population(layer_type, resolution, depth, value):
55
  # Check if layer_type exists, if not, create it
56
  if layer_type not in weights:
@@ -67,6 +68,7 @@ def weight_population(layer_type, resolution, depth, value):
67
  # Add/Modify the value at the specified depth (which can be a string)
68
  weights[layer_type][resolution][depth] = value
69
 
 
70
  def resize_image_with_aspect(image, res_range_min=128, res_range_max=1024):
71
  # Get the original width and height of the image
72
  width, height = image.size
@@ -89,6 +91,7 @@ def resize_image_with_aspect(image, res_range_min=128, res_range_max=1024):
89
 
90
  return resized_image
91
 
 
92
  def reconstruct(input_img, caption):
93
  global weights
94
  weights = {}
@@ -187,6 +190,7 @@ def reconstruct(input_img, caption):
187
 
188
  return image_np, caption, 12, [caption, real_image_initial_latents.detach(), inversed_latents, weights]
189
 
 
190
  class AttnReplaceProcessor(AttnProcessor2_0):
191
 
192
  def __init__(self, replace_all, layer_type, layer_count, blur_sigma=None):
@@ -294,6 +298,7 @@ class AttnReplaceProcessor(AttnProcessor2_0):
294
 
295
  return hidden_states
296
 
 
297
  def replace_attention_processor(unet, clear=False, blur_sigma=None):
298
  attention_count = 0
299
 
@@ -314,7 +319,7 @@ def replace_attention_processor(unet, clear=False, blur_sigma=None):
314
  else:
315
  module.processor = AttnReplaceProcessor(False, layer_type, attention_count, blur_sigma=blur_sigma)
316
 
317
-
318
  def apply_prompt(meta_data, new_prompt):
319
 
320
  caption, real_image_initial_latents, inversed_latents, _ = meta_data
@@ -362,7 +367,7 @@ def apply_prompt(meta_data, new_prompt):
362
 
363
  return image_np
364
 
365
-
366
  def on_image_change(filepath):
367
  # Extract the filename without extension
368
  filename = os.path.splitext(os.path.basename(filepath))[0]
@@ -395,7 +400,7 @@ def on_image_change(filepath):
395
 
396
  return filepath, img, meta_data_raw, num_inference_steps, scale_value, scale_value
397
 
398
-
399
  def update_value(value, layer_type, resolution, depth):
400
  global weights
401
  weights[layer_type][resolution][depth] = value
@@ -422,6 +427,7 @@ def adjust_ends(values, adjustment):
422
 
423
  max_scale_value = 16
424
 
 
425
  def update_scale(scale):
426
  global weights
427
 
 
22
  from safetensors.torch import load_file
23
  from huggingface_hub import hf_hub_download
24
  from diffusers import DiffusionPipeline
25
+ import spaces
26
 
27
+ @spaces.GPU()
28
  def save_state_to_file(state):
29
  filename = "state.pkl"
30
  with open(filename, "wb") as f:
31
  pickle.dump(state, f)
32
  return filename
33
 
34
+ @spaces.GPU()
35
  def load_state_from_file(filename):
36
  with open(filename, "rb") as f:
37
  state = pickle.load(f)
 
48
  allowed_res_max = 1.0
49
 
50
  # Device configuration
51
+ device = "cuda"
52
  print(f"Using device: {device}")
53
 
54
+ @spaces.GPU()
55
  def weight_population(layer_type, resolution, depth, value):
56
  # Check if layer_type exists, if not, create it
57
  if layer_type not in weights:
 
68
  # Add/Modify the value at the specified depth (which can be a string)
69
  weights[layer_type][resolution][depth] = value
70
 
71
+ @spaces.GPU()
72
  def resize_image_with_aspect(image, res_range_min=128, res_range_max=1024):
73
  # Get the original width and height of the image
74
  width, height = image.size
 
91
 
92
  return resized_image
93
 
94
+ @spaces.GPU()
95
  def reconstruct(input_img, caption):
96
  global weights
97
  weights = {}
 
190
 
191
  return image_np, caption, 12, [caption, real_image_initial_latents.detach(), inversed_latents, weights]
192
 
193
+ @spaces.GPU()
194
  class AttnReplaceProcessor(AttnProcessor2_0):
195
 
196
  def __init__(self, replace_all, layer_type, layer_count, blur_sigma=None):
 
298
 
299
  return hidden_states
300
 
301
+ @spaces.GPU()
302
  def replace_attention_processor(unet, clear=False, blur_sigma=None):
303
  attention_count = 0
304
 
 
319
  else:
320
  module.processor = AttnReplaceProcessor(False, layer_type, attention_count, blur_sigma=blur_sigma)
321
 
322
+ @spaces.GPU()
323
  def apply_prompt(meta_data, new_prompt):
324
 
325
  caption, real_image_initial_latents, inversed_latents, _ = meta_data
 
367
 
368
  return image_np
369
 
370
+ @spaces.GPU()
371
  def on_image_change(filepath):
372
  # Extract the filename without extension
373
  filename = os.path.splitext(os.path.basename(filepath))[0]
 
400
 
401
  return filepath, img, meta_data_raw, num_inference_steps, scale_value, scale_value
402
 
403
+ @spaces.GPU()
404
  def update_value(value, layer_type, resolution, depth):
405
  global weights
406
  weights[layer_type][resolution][depth] = value
 
427
 
428
  max_scale_value = 16
429
 
430
+ @spaces.GPU()
431
  def update_scale(scale):
432
  global weights
433