jiuface commited on
Commit
8e5b8b6
·
verified ·
1 Parent(s): 521316d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -49
app.py CHANGED
@@ -68,44 +68,6 @@ class calculateDuration:
68
  print(f"Elapsed time: {self.elapsed_time:.6f} seconds")
69
 
70
 
71
- def generate_image(orginal_image, prompt, adapter_names, steps, seed, image_strength, cfg_scale, width, height, progress):
72
-
73
-
74
- gr.Info("Start to generate images ...")
75
- with calculateDuration(f"Make a new generator: {seed}"):
76
- # txt2img_pipe.to(device)
77
- generator = torch.Generator(device=device).manual_seed(seed)
78
-
79
- with calculateDuration("Generating image"):
80
- # Generate image
81
- joint_attention_kwargs = {"scale": 1}
82
- if orginal_image:
83
- generated_image = img2img_pipe(
84
- prompt=prompt,
85
- image=orginal_image,
86
- strength=image_strength,
87
- num_inference_steps=steps,
88
- guidance_scale=cfg_scale,
89
- width=width,
90
- height=height,
91
- generator=generator,
92
- joint_attention_kwargs=joint_attention_kwargs
93
- ).images[0]
94
- else:
95
- generated_image = txt2img_pipe(
96
- prompt=prompt,
97
- num_inference_steps=steps,
98
- guidance_scale=cfg_scale,
99
- width=width,
100
- height=height,
101
- max_sequence_length=512,
102
- generator=generator,
103
- joint_attention_kwargs=joint_attention_kwargs
104
- ).images[0]
105
- torch.cuda.empty_cache()
106
- progress(99, "Generate image success!")
107
- return generated_image
108
-
109
 
110
  def upload_image_to_r2(image, account_id, access_key, secret_key, bucket_name):
111
  with calculateDuration("Upload images"):
@@ -152,8 +114,6 @@ def generate_random_4_digit_string():
152
  def run_lora(prompt, image_url, lora_strings_json, image_strength, cfg_scale, steps, randomize_seed, seed, width, height, upload_to_r2, account_id, access_key, secret_key, bucket, progress=gr.Progress(track_tqdm=True)):
153
  print("run_lora", prompt, lora_strings_json, cfg_scale, steps, width, height)
154
  gr.Info("Starting process")
155
-
156
- torch.cuda.empty_cache()
157
  img2img_model = False
158
  orginal_image = None
159
  if image_url:
@@ -214,18 +174,46 @@ def run_lora(prompt, image_url, lora_strings_json, image_strength, cfg_scale, s
214
  img2img_pipe.set_adapters(lora_names, adapter_weights=adapter_weights)
215
  else:
216
  txt2img_pipe.set_adapters(lora_names, adapter_weights=adapter_weights)
217
-
218
- print(txt2img_pipe.get_active_adapters())
219
 
220
  # Generate image
221
  error_message = ""
222
  try:
223
- print("Start applying for zeroGPU resources")
224
- final_image = generate_image(orginal_image, prompt, adapter_names, steps, seed, image_strength, cfg_scale, width, height, progress)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
225
  except Exception as e:
226
  error_message = str(e)
227
  gr.Error(error_message)
228
- print("Run error", e)
229
  final_image = None
230
 
231
  if final_image:
@@ -236,12 +224,9 @@ def run_lora(prompt, image_url, lora_strings_json, image_strength, cfg_scale, s
236
  result = {"status": "success", "message": "Image generated but not uploaded"}
237
  else:
238
  result = {"status": "failed", "message": error_message}
239
-
240
- txt2img_pipe.delete_adapters(lora_names)
241
-
242
  gr.Info("Completed!")
243
  progress(100, "Completed!")
244
- torch.cuda.empty_cache()
245
  return final_image, seed, json.dumps(result)
246
 
247
  # Gradio interface
 
68
  print(f"Elapsed time: {self.elapsed_time:.6f} seconds")
69
 
70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
 
72
  def upload_image_to_r2(image, account_id, access_key, secret_key, bucket_name):
73
  with calculateDuration("Upload images"):
 
114
  def run_lora(prompt, image_url, lora_strings_json, image_strength, cfg_scale, steps, randomize_seed, seed, width, height, upload_to_r2, account_id, access_key, secret_key, bucket, progress=gr.Progress(track_tqdm=True)):
115
  print("run_lora", prompt, lora_strings_json, cfg_scale, steps, width, height)
116
  gr.Info("Starting process")
 
 
117
  img2img_model = False
118
  orginal_image = None
119
  if image_url:
 
174
  img2img_pipe.set_adapters(lora_names, adapter_weights=adapter_weights)
175
  else:
176
  txt2img_pipe.set_adapters(lora_names, adapter_weights=adapter_weights)
 
 
177
 
178
  # Generate image
179
  error_message = ""
180
  try:
181
+ gr.Info("Start to generate images ...")
182
+ with calculateDuration(f"Make a new generator: {seed}"):
183
+ generator = torch.Generator(device=device).manual_seed(seed)
184
+
185
+ with calculateDuration("Generating image"):
186
+ # Generate image
187
+ joint_attention_kwargs = {"scale": 1}
188
+
189
+ if orginal_image:
190
+ final_image = img2img_pipe(
191
+ prompt=prompt,
192
+ image=orginal_image,
193
+ strength=image_strength,
194
+ num_inference_steps=steps,
195
+ guidance_scale=cfg_scale,
196
+ width=width,
197
+ height=height,
198
+ generator=generator,
199
+ joint_attention_kwargs=joint_attention_kwargs
200
+ ).images[0]
201
+ else:
202
+ final_image = txt2img_pipe(
203
+ prompt=prompt,
204
+ num_inference_steps=steps,
205
+ guidance_scale=cfg_scale,
206
+ width=width,
207
+ height=height,
208
+ max_sequence_length=512,
209
+ generator=generator,
210
+ joint_attention_kwargs=joint_attention_kwargs
211
+ ).images[0]
212
+
213
  except Exception as e:
214
  error_message = str(e)
215
  gr.Error(error_message)
216
+ print("fatal error", e)
217
  final_image = None
218
 
219
  if final_image:
 
224
  result = {"status": "success", "message": "Image generated but not uploaded"}
225
  else:
226
  result = {"status": "failed", "message": error_message}
227
+
 
 
228
  gr.Info("Completed!")
229
  progress(100, "Completed!")
 
230
  return final_image, seed, json.dumps(result)
231
 
232
  # Gradio interface