jiuface commited on
Commit
98795d7
1 Parent(s): c23b09b
Files changed (1) hide show
  1. app.py +15 -15
app.py CHANGED
@@ -54,11 +54,15 @@ class calculateDuration:
54
 
55
  def __enter__(self):
56
  self.start_time = time.time()
 
 
57
  return self
58
 
59
  def __exit__(self, exc_type, exc_value, traceback):
60
  self.end_time = time.time()
61
  self.elapsed_time = self.end_time - self.start_time
 
 
62
  if self.activity_name:
63
  print(f"Elapsed time for {self.activity_name}: {self.elapsed_time:.6f} seconds")
64
  else:
@@ -113,7 +117,6 @@ def upload_image_to_r2(image, account_id, access_key, secret_key, bucket_name):
113
  with calculateDuration("Upload image"):
114
  print("upload_image_to_r2", account_id, access_key, secret_key, bucket_name)
115
  connectionUrl = f"https://{account_id}.r2.cloudflarestorage.com"
116
-
117
  s3 = boto3.client(
118
  's3',
119
  endpoint_url=connectionUrl,
@@ -121,7 +124,6 @@ def upload_image_to_r2(image, account_id, access_key, secret_key, bucket_name):
121
  aws_access_key_id=access_key,
122
  aws_secret_access_key=secret_key
123
  )
124
-
125
  current_time = datetime.now().strftime("%Y/%m/%d/%H%M%S")
126
  image_file = f"generated_images/{current_time}_{random.randint(0, MAX_SEED)}.png"
127
  buffer = BytesIO()
@@ -155,9 +157,7 @@ def run_flux(
155
  seed_slicer = random.randint(0, MAX_SEED)
156
  generator = torch.Generator().manual_seed(seed_slicer)
157
 
158
- with calculateDuration("run pipe"):
159
- print("start to run pipe", prompt)
160
-
161
  with torch.inference_mode():
162
  generated_image = pipe(
163
  prompt=prompt,
@@ -236,7 +236,7 @@ def process(
236
  bucket:str,
237
  progress=gr.Progress(track_tqdm=True)
238
  ):
239
-
240
  result = {"status": "false", "message": ""}
241
  if not image_url:
242
  gr.Info("please enter image url for inpaiting")
@@ -249,28 +249,29 @@ def process(
249
  return None, json.dumps(result)
250
 
251
 
252
- with calculateDuration("load image"):
253
  image = load_image(image_url)
254
  mask = load_image(mask_url)
255
 
256
- if not image or not mask:
257
- gr.Info("Please upload an image & mask by url.")
258
- result["message"] = "can not load image"
259
- return None, json.dumps(result)
260
 
261
  # generate
262
- with calculateDuration("resize & process mask"):
263
  width, height = calculate_image_dimensions_for_flux(original_resolution_wh=image.size)
264
  image = image.resize((width, height), Image.LANCZOS)
265
  mask = mask.resize((width, height), Image.LANCZOS)
266
  mask = process_mask(mask, mask_inflation=mask_inflation_slider, mask_blur=mask_blur_slider)
267
 
268
  control_image = generate_control_image(image, mask, width, height)
269
- clear_cuda_cache()
270
 
271
  load_loras(lora_strings_json=lora_strings_json)
272
 
273
  try:
 
274
  generated_image = run_flux(
275
  image=image,
276
  mask=mask,
@@ -299,8 +300,7 @@ def process(
299
  result = {"status": "success", "message": "upload image success", "url": url}
300
  else:
301
  result = {"status": "success", "message": "Image generated but not uploaded"}
302
-
303
- clear_cuda_cache()
304
  final_images = []
305
  final_images.append(image)
306
  final_images.append(mask)
 
54
 
55
  def __enter__(self):
56
  self.start_time = time.time()
57
+ self.start_time_formatted = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(self.start_time))
58
+ print(f"Activity: {self.activity_name}, Start time: {self.start_time_formatted}")
59
  return self
60
 
61
  def __exit__(self, exc_type, exc_value, traceback):
62
  self.end_time = time.time()
63
  self.elapsed_time = self.end_time - self.start_time
64
+ self.end_time_formatted = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(self.end_time))
65
+
66
  if self.activity_name:
67
  print(f"Elapsed time for {self.activity_name}: {self.elapsed_time:.6f} seconds")
68
  else:
 
117
  with calculateDuration("Upload image"):
118
  print("upload_image_to_r2", account_id, access_key, secret_key, bucket_name)
119
  connectionUrl = f"https://{account_id}.r2.cloudflarestorage.com"
 
120
  s3 = boto3.client(
121
  's3',
122
  endpoint_url=connectionUrl,
 
124
  aws_access_key_id=access_key,
125
  aws_secret_access_key=secret_key
126
  )
 
127
  current_time = datetime.now().strftime("%Y/%m/%d/%H%M%S")
128
  image_file = f"generated_images/{current_time}_{random.randint(0, MAX_SEED)}.png"
129
  buffer = BytesIO()
 
157
  seed_slicer = random.randint(0, MAX_SEED)
158
  generator = torch.Generator().manual_seed(seed_slicer)
159
 
160
+ with calculateDuration("Run pipe"):
 
 
161
  with torch.inference_mode():
162
  generated_image = pipe(
163
  prompt=prompt,
 
236
  bucket:str,
237
  progress=gr.Progress(track_tqdm=True)
238
  ):
239
+ print("process", image_url, mask_url, inpainting_prompt_text, lora_strings_json)
240
  result = {"status": "false", "message": ""}
241
  if not image_url:
242
  gr.Info("please enter image url for inpaiting")
 
249
  return None, json.dumps(result)
250
 
251
 
252
+ with calculateDuration("Load image"):
253
  image = load_image(image_url)
254
  mask = load_image(mask_url)
255
 
256
+ if not image or not mask:
257
+ gr.Info("Please upload an image & mask by url.")
258
+ result["message"] = "can not load image"
259
+ return None, json.dumps(result)
260
 
261
  # generate
262
+ with calculateDuration("Resize & process mask"):
263
  width, height = calculate_image_dimensions_for_flux(original_resolution_wh=image.size)
264
  image = image.resize((width, height), Image.LANCZOS)
265
  mask = mask.resize((width, height), Image.LANCZOS)
266
  mask = process_mask(mask, mask_inflation=mask_inflation_slider, mask_blur=mask_blur_slider)
267
 
268
  control_image = generate_control_image(image, mask, width, height)
269
+ # clear_cuda_cache()
270
 
271
  load_loras(lora_strings_json=lora_strings_json)
272
 
273
  try:
274
+ print("Start applying for zeroGPU resources ...")
275
  generated_image = run_flux(
276
  image=image,
277
  mask=mask,
 
300
  result = {"status": "success", "message": "upload image success", "url": url}
301
  else:
302
  result = {"status": "success", "message": "Image generated but not uploaded"}
303
+
 
304
  final_images = []
305
  final_images.append(image)
306
  final_images.append(mask)