multimodalart HF staff commited on
Commit
e2c1d93
β€’
1 Parent(s): 7a09818

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -19
app.py CHANGED
@@ -7,6 +7,7 @@ import spaces
7
  from diffusers import DiffusionPipeline
8
  import copy
9
  import random
 
10
 
11
  # Load LoRAs from JSON file
12
  with open('loras.json', 'r') as f:
@@ -19,6 +20,23 @@ pipe.to("cuda")
19
 
20
  MAX_SEED = 2**32-1
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  def update_selection(evt: gr.SelectData):
23
  selected_lora = loras[evt.index]
24
  new_placeholder = f"Type a prompt for {selected_lora['title']}"
@@ -40,30 +58,34 @@ def run_lora(prompt, cfg_scale, steps, selected_index, randomize_seed, seed, wid
40
  trigger_word = selected_lora["trigger_word"]
41
 
42
  # Load LoRA weights
43
- if "weights" in selected_lora:
44
- pipe.load_lora_weights(lora_path, weight_name=selected_lora["weights"])
45
- else:
46
- pipe.load_lora_weights(lora_path)
 
47
 
48
  # Set random seed for reproducibility
49
- if randomize_seed:
50
- seed = random.randint(0, MAX_SEED)
51
- generator = torch.Generator(device="cuda").manual_seed(seed)
52
-
53
- # Generate image
54
- image = pipe(
55
- prompt=f"{prompt} {trigger_word}",
56
- num_inference_steps=steps,
57
- guidance_scale=cfg_scale,
58
- width=width,
59
- height=height,
60
- generator=generator,
61
- joint_attention_kwargs={"scale": lora_scale},
62
- ).images[0]
 
 
63
 
64
  yield image, seed
65
 
66
- pipe.unload_lora_weights()
 
67
 
68
 
69
  css = '''
 
7
  from diffusers import DiffusionPipeline
8
  import copy
9
  import random
10
+ import time
11
 
12
  # Load LoRAs from JSON file
13
  with open('loras.json', 'r') as f:
 
20
 
21
  MAX_SEED = 2**32-1
22
 
23
+ class calculateDuration:
24
+ def __init__(self, activity_name=""):
25
+ self.activity_name = activity_name
26
+
27
+ def __enter__(self):
28
+ self.start_time = time.time()
29
+ return self
30
+
31
+ def __exit__(self, exc_type, exc_value, traceback):
32
+ self.end_time = time.time()
33
+ self.elapsed_time = self.end_time - self.start_time
34
+ if self.activity_name:
35
+ print(f"Elapsed time for {self.activity_name}: {self.elapsed_time:.6f} seconds")
36
+ else:
37
+ print(f"Elapsed time: {self.elapsed_time:.6f} seconds")
38
+
39
+
40
  def update_selection(evt: gr.SelectData):
41
  selected_lora = loras[evt.index]
42
  new_placeholder = f"Type a prompt for {selected_lora['title']}"
 
58
  trigger_word = selected_lora["trigger_word"]
59
 
60
  # Load LoRA weights
61
+ with calculateDuration("Loading LoRA weights"):
62
+ if "weights" in selected_lora:
63
+ pipe.load_lora_weights(lora_path, weight_name=selected_lora["weights"])
64
+ else:
65
+ pipe.load_lora_weights(lora_path)
66
 
67
  # Set random seed for reproducibility
68
+ with calculateDuration("Randomizing seed"):
69
+ if randomize_seed:
70
+ seed = random.randint(0, MAX_SEED)
71
+ generator = torch.Generator(device="cuda").manual_seed(seed)
72
+
73
+ with calculateDuration("Generating image"):
74
+ # Generate image
75
+ image = pipe(
76
+ prompt=f"{prompt} {trigger_word}",
77
+ num_inference_steps=steps,
78
+ guidance_scale=cfg_scale,
79
+ width=width,
80
+ height=height,
81
+ generator=generator,
82
+ joint_attention_kwargs={"scale": lora_scale},
83
+ ).images[0]
84
 
85
  yield image, seed
86
 
87
+ with calculateDuration("Unloading weights"):
88
+ pipe.unload_lora_weights()
89
 
90
 
91
  css = '''