alexnasa commited on
Commit
9e7749e
·
verified ·
1 Parent(s): 25dc058

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +133 -130
app.py CHANGED
@@ -228,143 +228,145 @@ def generate_image(
228
  indexs, # 新增参数
229
  *images_captions_faces, # Combine all unpacked arguments into one tuple
230
  ):
231
- torch.cuda.empty_cache()
232
- num_images = 1
233
-
234
- # Determine the number of images, captions, and faces based on the indexs length
235
- images = list(images_captions_faces[:num_inputs])
236
- captions = list(images_captions_faces[num_inputs:2 * num_inputs])
237
- idips_checkboxes = list(images_captions_faces[2 * num_inputs:3 * num_inputs])
238
- images = [images[i] for i in indexs]
239
- captions = [captions[i] for i in indexs]
240
- idips_checkboxes = [idips_checkboxes[i] for i in indexs]
241
-
242
- print(f"Length of images: {len(images)}")
243
- print(f"Length of captions: {len(captions)}")
244
- print(f"Indexs: {indexs}")
245
 
246
- print(f"Control weight lambda: {control_weight_lambda}")
247
- if control_weight_lambda != "no":
248
- parts = control_weight_lambda.split(',')
249
- new_parts = []
250
- for part in parts:
251
- if ':' in part:
252
- left, right = part.split(':')
253
- values = right.split('/')
254
- # 保存整体值
255
- global_value = values[0]
256
- id_value = values[1]
257
- ip_value = values[2]
258
- new_values = [global_value]
259
- for is_id in idips_checkboxes:
260
- if is_id:
261
- new_values.append(id_value)
262
- else:
263
- new_values.append(ip_value)
264
- new_part = f"{left}:{('/'.join(new_values))}"
265
- new_parts.append(new_part)
266
- else:
267
- new_parts.append(part)
268
- control_weight_lambda = ','.join(new_parts)
269
 
270
- print(f"Control weight lambda: {control_weight_lambda}")
271
-
272
- src_inputs = []
273
- use_words = []
274
- cur_run_time = time.strftime("%m%d-%H%M%S")
275
- tmp_dir_root = f"tmp/gradio_demo/{run_name}"
276
- temp_dir = f"{tmp_dir_root}/{cur_run_time}_{generate_random_string(4)}"
277
- os.makedirs(temp_dir, exist_ok=True)
278
- print(f"Temporary directory created: {temp_dir}")
279
- for i, (image_path, caption) in enumerate(zip(images, captions)):
280
- if image_path:
281
- if caption.startswith("a ") or caption.startswith("A "):
282
- word = caption[2:]
283
- else:
284
- word = caption
285
 
286
- if f"ENT{i+1}" in prompt:
287
- prompt = prompt.replace(f"ENT{i+1}", caption)
288
 
289
- image = resize_keep_aspect_ratio(Image.open(image_path), 768)
290
- save_path = f"{temp_dir}/tmp_resized_input_{i}.png"
291
- image.save(save_path)
292
 
293
- input_image_path = save_path
294
-
295
- src_inputs.append(
296
- {
297
- "image_path": input_image_path,
298
- "caption": caption
299
- }
300
- )
301
- use_words.append((i, word, word))
302
-
303
-
304
- test_sample = dict(
305
- input_images=[], position_delta=[0, -32],
306
- prompt=prompt,
307
- target_height=target_height,
308
- target_width=target_width,
309
- seed=seed,
310
- cond_size=cond_size,
311
- vae_skip_iter=vae_skip_iter,
312
- lora_scale=ip_scale,
313
- control_weight_lambda=control_weight_lambda,
314
- latent_sblora_scale=latent_sblora_scale_str,
315
- condition_sblora_scale=vae_lora_scale,
316
- double_attention=double_attention,
317
- single_attention=single_attention,
318
- )
319
- if len(src_inputs) > 0:
320
- test_sample["modulation"] = [
321
- dict(
322
- type="adapter",
323
- src_inputs=src_inputs,
324
- use_words=use_words,
325
- ),
326
- ]
327
 
328
- json_dump(test_sample, f"{temp_dir}/test_sample.json", 'utf-8')
329
- assert single_attention == True
330
- target_size = int(round((target_width * target_height) ** 0.5) // 16 * 16)
331
- print(test_sample)
332
 
333
- model.config["train"]["dataset"]["val_condition_size"] = cond_size
334
- model.config["train"]["dataset"]["val_target_size"] = target_size
335
 
336
- if control_weight_lambda == "no":
337
- control_weight_lambda = None
338
- if vae_skip_iter == "no":
339
- vae_skip_iter = None
340
- use_condition_sblora_control = True
341
- use_latent_sblora_control = True
342
- image = generate_from_test_sample(
343
- test_sample, model.pipe, model.config,
344
- num_images=num_images,
345
- target_height=target_height,
346
- target_width=target_width,
347
- seed=seed,
348
- store_attn_map=store_attn_map,
349
- vae_skip_iter=vae_skip_iter, # 使用新的参数
350
- control_weight_lambda=control_weight_lambda, # 传递新的参数
351
- double_attention=double_attention, # 新增参数
352
- single_attention=single_attention, # 新增参数
353
- ip_scale=ip_scale,
354
- use_latent_sblora_control=use_latent_sblora_control,
355
- latent_sblora_scale=latent_sblora_scale_str,
356
- use_condition_sblora_control=use_condition_sblora_control,
357
- condition_sblora_scale=vae_lora_scale,
358
- )
359
- if isinstance(image, list):
360
- num_cols = 2
361
- num_rows = int(math.ceil(num_images / num_cols))
362
- image = image_grid(image, num_rows, num_cols)
363
-
364
- save_path = f"{temp_dir}/tmp_result.png"
365
- image.save(save_path)
366
-
367
- return image
 
 
368
 
369
 
370
 
@@ -557,4 +559,5 @@ with gr.Blocks() as demo:
557
  vlm_btn_2.click(vlm_img_caption, inputs=[image_2], outputs=[caption_2])
558
 
559
 
560
- demo.queue().launch(share=True)
 
 
228
  indexs, # 新增参数
229
  *images_captions_faces, # Combine all unpacked arguments into one tuple
230
  ):
231
+ # torch.cuda.empty_cache()
232
+ # num_images = 1
233
+
234
+ # # Determine the number of images, captions, and faces based on the indexs length
235
+ # images = list(images_captions_faces[:num_inputs])
236
+ # captions = list(images_captions_faces[num_inputs:2 * num_inputs])
237
+ # idips_checkboxes = list(images_captions_faces[2 * num_inputs:3 * num_inputs])
238
+ # images = [images[i] for i in indexs]
239
+ # captions = [captions[i] for i in indexs]
240
+ # idips_checkboxes = [idips_checkboxes[i] for i in indexs]
241
+
242
+ # print(f"Length of images: {len(images)}")
243
+ # print(f"Length of captions: {len(captions)}")
244
+ # print(f"Indexs: {indexs}")
245
 
246
+ # print(f"Control weight lambda: {control_weight_lambda}")
247
+ # if control_weight_lambda != "no":
248
+ # parts = control_weight_lambda.split(',')
249
+ # new_parts = []
250
+ # for part in parts:
251
+ # if ':' in part:
252
+ # left, right = part.split(':')
253
+ # values = right.split('/')
254
+ # # 保存整体值
255
+ # global_value = values[0]
256
+ # id_value = values[1]
257
+ # ip_value = values[2]
258
+ # new_values = [global_value]
259
+ # for is_id in idips_checkboxes:
260
+ # if is_id:
261
+ # new_values.append(id_value)
262
+ # else:
263
+ # new_values.append(ip_value)
264
+ # new_part = f"{left}:{('/'.join(new_values))}"
265
+ # new_parts.append(new_part)
266
+ # else:
267
+ # new_parts.append(part)
268
+ # control_weight_lambda = ','.join(new_parts)
269
 
270
+ # print(f"Control weight lambda: {control_weight_lambda}")
271
+
272
+ # src_inputs = []
273
+ # use_words = []
274
+ # cur_run_time = time.strftime("%m%d-%H%M%S")
275
+ # tmp_dir_root = f"tmp/gradio_demo/{run_name}"
276
+ # temp_dir = f"{tmp_dir_root}/{cur_run_time}_{generate_random_string(4)}"
277
+ # os.makedirs(temp_dir, exist_ok=True)
278
+ # print(f"Temporary directory created: {temp_dir}")
279
+ # for i, (image_path, caption) in enumerate(zip(images, captions)):
280
+ # if image_path:
281
+ # if caption.startswith("a ") or caption.startswith("A "):
282
+ # word = caption[2:]
283
+ # else:
284
+ # word = caption
285
 
286
+ # if f"ENT{i+1}" in prompt:
287
+ # prompt = prompt.replace(f"ENT{i+1}", caption)
288
 
289
+ # image = resize_keep_aspect_ratio(Image.open(image_path), 768)
290
+ # save_path = f"{temp_dir}/tmp_resized_input_{i}.png"
291
+ # image.save(save_path)
292
 
293
+ # input_image_path = save_path
294
+
295
+ # src_inputs.append(
296
+ # {
297
+ # "image_path": input_image_path,
298
+ # "caption": caption
299
+ # }
300
+ # )
301
+ # use_words.append((i, word, word))
302
+
303
+
304
+ # test_sample = dict(
305
+ # input_images=[], position_delta=[0, -32],
306
+ # prompt=prompt,
307
+ # target_height=target_height,
308
+ # target_width=target_width,
309
+ # seed=seed,
310
+ # cond_size=cond_size,
311
+ # vae_skip_iter=vae_skip_iter,
312
+ # lora_scale=ip_scale,
313
+ # control_weight_lambda=control_weight_lambda,
314
+ # latent_sblora_scale=latent_sblora_scale_str,
315
+ # condition_sblora_scale=vae_lora_scale,
316
+ # double_attention=double_attention,
317
+ # single_attention=single_attention,
318
+ # )
319
+ # if len(src_inputs) > 0:
320
+ # test_sample["modulation"] = [
321
+ # dict(
322
+ # type="adapter",
323
+ # src_inputs=src_inputs,
324
+ # use_words=use_words,
325
+ # ),
326
+ # ]
327
 
328
+ # json_dump(test_sample, f"{temp_dir}/test_sample.json", 'utf-8')
329
+ # assert single_attention == True
330
+ # target_size = int(round((target_width * target_height) ** 0.5) // 16 * 16)
331
+ # print(test_sample)
332
 
333
+ # model.config["train"]["dataset"]["val_condition_size"] = cond_size
334
+ # model.config["train"]["dataset"]["val_target_size"] = target_size
335
 
336
+ # if control_weight_lambda == "no":
337
+ # control_weight_lambda = None
338
+ # if vae_skip_iter == "no":
339
+ # vae_skip_iter = None
340
+ # use_condition_sblora_control = True
341
+ # use_latent_sblora_control = True
342
+ # image = generate_from_test_sample(
343
+ # test_sample, model.pipe, model.config,
344
+ # num_images=num_images,
345
+ # target_height=target_height,
346
+ # target_width=target_width,
347
+ # seed=seed,
348
+ # store_attn_map=store_attn_map,
349
+ # vae_skip_iter=vae_skip_iter, # 使用新的参数
350
+ # control_weight_lambda=control_weight_lambda, # 传递新的参数
351
+ # double_attention=double_attention, # 新增参数
352
+ # single_attention=single_attention, # 新增参数
353
+ # ip_scale=ip_scale,
354
+ # use_latent_sblora_control=use_latent_sblora_control,
355
+ # latent_sblora_scale=latent_sblora_scale_str,
356
+ # use_condition_sblora_control=use_condition_sblora_control,
357
+ # condition_sblora_scale=vae_lora_scale,
358
+ # )
359
+ # if isinstance(image, list):
360
+ # num_cols = 2
361
+ # num_rows = int(math.ceil(num_images / num_cols))
362
+ # image = image_grid(image, num_rows, num_cols)
363
+
364
+ # save_path = f"{temp_dir}/tmp_result.png"
365
+ # image.save(save_path)
366
+
367
+ # return image
368
+
369
+ return None
370
 
371
 
372
 
 
559
  vlm_btn_2.click(vlm_img_caption, inputs=[image_2], outputs=[caption_2])
560
 
561
 
562
+ demo.queue()
563
+ demo.launch(share=True)