prithivMLmods commited on
Commit
5439aa4
·
verified ·
1 Parent(s): 10814c3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +124 -119
app.py CHANGED
@@ -12,7 +12,7 @@ import zipfile
12
  from transformers import CLIPTextModel, CLIPTokenizer, T5EncoderModel, T5TokenizerFast
13
 
14
  # Description for the app
15
- DESCRIPTION = """## flux comparator hpc/."""
16
 
17
  # Helper functions
18
  def save_image(img):
@@ -28,24 +28,19 @@ def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
28
  MAX_SEED = np.iinfo(np.int32).max
29
  MAX_IMAGE_SIZE = 2048
30
 
31
- # Load pipelines for both models
32
- # Flux.1-dev-realism
33
- base_model_dev = "prithivMLmods/Flux.1-Merged" # Merge of (black-forest-labs/FLUX.1-dev + black-forest-labs/FLUX.1-schnell)
34
- pipe_dev = DiffusionPipeline.from_pretrained(base_model_dev, torch_dtype=torch.bfloat16)
35
- lora_repo = "strangerzonehf/Flux-Super-Realism-LoRA"
36
- trigger_word = "Super Realism"
37
- pipe_dev.load_lora_weights(lora_repo)
38
- pipe_dev.to("cuda")
39
-
40
- # Flux.1-krea
41
  dtype = torch.bfloat16
42
  device = "cuda" if torch.cuda.is_available() else "cpu"
 
 
43
  taef1 = AutoencoderTiny.from_pretrained("madebyollin/taef1", torch_dtype=dtype).to(device)
44
- # Merge of (black-forest-labs/FLUX.1-dev + https://huggingface.co/black-forest-labs/FLUX.1-Krea-dev)
45
  good_vae = AutoencoderKL.from_pretrained("prithivMLmods/Flux.1-Krea-Merged-Dev", subfolder="vae", torch_dtype=dtype).to(device)
46
  pipe_krea = DiffusionPipeline.from_pretrained("prithivMLmods/Flux.1-Krea-Merged-Dev", torch_dtype=dtype, vae=taef1).to(device)
47
 
48
- # Define the flux_pipe_call_that_returns_an_iterable_of_images for flux.1-krea
 
 
 
49
  @torch.inference_mode()
50
  def flux_pipe_call_that_returns_an_iterable_of_images(
51
  self,
@@ -167,7 +162,7 @@ def flux_pipe_call_that_returns_an_iterable_of_images(
167
 
168
  pipe_krea.flux_pipe_call_that_returns_an_iterable_of_images = flux_pipe_call_that_returns_an_iterable_of_images.__get__(pipe_krea)
169
 
170
- # Helper functions for flux.1-krea
171
  def calculate_shift(
172
  image_seq_len,
173
  base_seq_len: int = 256,
@@ -203,67 +198,48 @@ def retrieve_timesteps(
203
  timesteps = scheduler.timesteps
204
  return timesteps, num_inference_steps
205
 
206
- # Styles for flux.1-dev-realism
207
- style_list = [
208
- {"name": "3840 x 2160", "prompt": "hyper-realistic 8K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic", "negative_prompt": ""},
209
- {"name": "2560 x 1440", "prompt": "hyper-realistic 4K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic", "negative_prompt": ""},
210
- {"name": "HD+", "prompt": "hyper-realistic 2K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic", "negative_prompt": ""},
211
- {"name": "Style Zero", "prompt": "{prompt}", "negative_prompt": ""},
212
- ]
213
-
214
- styles = {k["name"]: (k["prompt"], k["negative_prompt"]) for k in style_list}
215
- DEFAULT_STYLE_NAME = "Style Zero"
216
- STYLE_NAMES = list(styles.keys())
217
-
218
- def apply_style(style_name: str, positive: str) -> Tuple[str, str]:
219
- p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
220
- return p.replace("{prompt}", positive), n
221
 
222
- # Generation function for flux.1-dev-realism
223
  @spaces.GPU
224
- def generate_dev(
225
  prompt: str,
226
- negative_prompt: str = "",
227
- use_negative_prompt: bool = False,
228
  seed: int = 0,
229
  width: int = 1024,
230
  height: int = 1024,
231
- guidance_scale: float = 3,
232
  randomize_seed: bool = False,
233
- style_name: str = DEFAULT_STYLE_NAME,
234
- num_inference_steps: int = 30,
235
  num_images: int = 1,
236
  zip_images: bool = False,
237
  progress=gr.Progress(track_tqdm=True),
238
  ):
239
- positive_prompt, style_negative_prompt = apply_style(style_name, prompt)
240
-
241
- if use_negative_prompt:
242
- final_negative_prompt = style_negative_prompt + " " + negative_prompt
243
- else:
244
- final_negative_prompt = style_negative_prompt
245
-
246
- final_negative_prompt = final_negative_prompt.strip()
247
-
248
- if trigger_word:
249
- positive_prompt = f"{trigger_word} {positive_prompt}"
250
-
251
- seed = int(randomize_seed_fn(seed, randomize_seed))
252
- generator = torch.Generator(device="cuda").manual_seed(seed)
253
 
254
  start_time = time.time()
255
 
256
- images = pipe_dev(
257
- prompt=positive_prompt,
258
- negative_prompt=final_negative_prompt if final_negative_prompt else None,
259
- width=width,
260
- height=height,
261
- guidance_scale=guidance_scale,
262
- num_inference_steps=num_inference_steps,
263
- num_images_per_prompt=num_images,
264
- generator=generator,
265
- output_type="pil",
266
- ).images
 
 
267
 
268
  end_time = time.time()
269
  duration = end_time - start_time
@@ -280,39 +256,38 @@ def generate_dev(
280
 
281
  return image_paths, seed, f"{duration:.2f}", zip_path
282
 
283
- # Generation function for flux.1-krea
284
  @spaces.GPU
285
- def generate_krea(
286
  prompt: str,
 
287
  seed: int = 0,
288
  width: int = 1024,
289
  height: int = 1024,
290
- guidance_scale: float = 4.5,
291
  randomize_seed: bool = False,
292
- num_inference_steps: int = 28,
293
  num_images: int = 1,
294
  zip_images: bool = False,
295
  progress=gr.Progress(track_tqdm=True),
296
  ):
297
  if randomize_seed:
298
  seed = random.randint(0, MAX_SEED)
299
- generator = torch.Generator().manual_seed(seed)
300
 
301
  start_time = time.time()
302
 
303
- images = []
304
- for _ in range(num_images):
305
- final_img = list(pipe_krea.flux_pipe_call_that_returns_an_iterable_of_images(
306
- prompt=prompt,
307
- guidance_scale=guidance_scale,
308
- num_inference_steps=num_inference_steps,
309
- width=width,
310
- height=height,
311
- generator=generator,
312
- output_type="pil",
313
- good_vae=good_vae,
314
- ))[-1] # Take the final image only
315
- images.append(final_img)
316
 
317
  end_time = time.time()
318
  duration = end_time - start_time
@@ -329,7 +304,7 @@ def generate_krea(
329
 
330
  return image_paths, seed, f"{duration:.2f}", zip_path
331
 
332
- # Main generation function to handle model choice
333
  @spaces.GPU
334
  def generate(
335
  model_choice: str,
@@ -339,33 +314,31 @@ def generate(
339
  seed: int = 0,
340
  width: int = 1024,
341
  height: int = 1024,
342
- guidance_scale: float = 3,
343
  randomize_seed: bool = False,
344
- style_name: str = DEFAULT_STYLE_NAME,
345
- num_inference_steps: int = 30,
346
  num_images: int = 1,
347
  zip_images: bool = False,
348
  progress=gr.Progress(track_tqdm=True),
349
  ):
350
- if model_choice == "flux.1-dev-merged":
351
- return generate_dev(
352
  prompt=prompt,
353
- negative_prompt=negative_prompt,
354
- use_negative_prompt=use_negative_prompt,
355
  seed=seed,
356
  width=width,
357
  height=height,
358
  guidance_scale=guidance_scale,
359
  randomize_seed=randomize_seed,
360
- style_name=style_name,
361
  num_inference_steps=num_inference_steps,
362
  num_images=num_images,
363
  zip_images=zip_images,
364
  progress=progress,
365
  )
366
- elif model_choice == "flux.1-krea-merged-dev":
367
- return generate_krea(
 
368
  prompt=prompt,
 
369
  seed=seed,
370
  width=width,
371
  height=height,
@@ -379,12 +352,12 @@ def generate(
379
  else:
380
  raise ValueError("Invalid model choice")
381
 
382
- # Examples
383
  examples = [
384
- "An attractive young woman with blue eyes lying face down on the bed, in the style of animated gifs, light white and light amber, jagged edges, the snapshot aesthetic, timeless beauty, goosepunk, sunrays shine upon it --no freckles --chaos 65 --ar 1:2 --profile yruxpc2 --stylize 750 --v 6.1",
385
- "Headshot of handsome young man, wearing dark gray sweater with buttons and big shawl collar, brown hair and short beard, serious look on his face, black background, soft studio lighting, portrait photography --ar 85:128 --v 6.0 --style",
386
- "Purple Dreamy, a medium-angle shot of a young woman with long brown hair, wearing a pair of eye-level glasses, stands in front of a backdrop of purple and white lights.",
387
- "High-resolution photograph, woman, UHD, photorealistic, shot on a Sony A7III --chaos 20 --ar 1:2 --style raw --stylize 250"
388
  ]
389
 
390
  css = '''
@@ -415,21 +388,23 @@ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
415
  result = gr.Gallery(label="Result", columns=1, show_label=False, preview=True)
416
 
417
  with gr.Row():
418
- # Model choice radio button above additional options
419
  model_choice = gr.Radio(
420
- choices=["flux.1-dev-merged", "flux.1-krea-merged-dev"],
421
  label="Select Model",
422
- value="flux.1-dev-merged"
423
  )
424
 
425
  with gr.Accordion("Additional Options", open=False):
426
- style_selection = gr.Dropdown(
427
- label="Quality Style (for flux.1-dev-realism only)",
428
- choices=STYLE_NAMES,
429
- value=DEFAULT_STYLE_NAME,
430
- interactive=True,
 
 
 
 
431
  )
432
- use_negative_prompt = gr.Checkbox(label="Use negative prompt (for flux.1-dev-realism only)", value=False)
433
  negative_prompt = gr.Text(
434
  label="Negative prompt",
435
  max_lines=1,
@@ -461,7 +436,7 @@ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
461
  )
462
  guidance_scale = gr.Slider(
463
  label="Guidance Scale",
464
- minimum=0.1,
465
  maximum=20.0,
466
  step=0.1,
467
  value=3.5,
@@ -469,7 +444,7 @@ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
469
  num_inference_steps = gr.Slider(
470
  label="Number of inference steps",
471
  minimum=1,
472
- maximum=40,
473
  step=1,
474
  value=28,
475
  )
@@ -487,26 +462,48 @@ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
487
  generation_time = gr.Textbox(label="Generation time (seconds)", interactive=False)
488
  zip_file = gr.File(label="Download ZIP")
489
 
490
- gr.Examples(
491
- examples=examples,
492
- inputs=prompt,
493
- outputs=[result, seed_display, generation_time, zip_file],
494
- fn=generate,
495
- cache_examples=False,
 
 
 
496
  )
497
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
498
  use_negative_prompt.change(
499
  fn=lambda x: gr.update(visible=x),
500
  inputs=use_negative_prompt,
501
- outputs=negative_prompt,
502
- api_name=False,
503
  )
504
 
 
505
  gr.on(
506
- triggers=[
507
- prompt.submit,
508
- run_button.click,
509
- ],
510
  fn=generate,
511
  inputs=[
512
  model_choice,
@@ -518,7 +515,6 @@ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
518
  height,
519
  guidance_scale,
520
  randomize_seed,
521
- style_selection,
522
  num_inference_steps,
523
  num_images,
524
  zip_images,
@@ -527,5 +523,14 @@ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
527
  api_name="run",
528
  )
529
 
 
 
 
 
 
 
 
 
 
530
  if __name__ == "__main__":
531
  demo.queue(max_size=30).launch(mcp_server=True, ssr_mode=False, show_error=True)
 
12
  from transformers import CLIPTextModel, CLIPTokenizer, T5EncoderModel, T5TokenizerFast
13
 
14
  # Description for the app
15
+ DESCRIPTION = """## flux-krea vs qwen"""
16
 
17
  # Helper functions
18
  def save_image(img):
 
28
  MAX_SEED = np.iinfo(np.int32).max
29
  MAX_IMAGE_SIZE = 2048
30
 
31
+ # Load pipelines
 
 
 
 
 
 
 
 
 
32
  dtype = torch.bfloat16
33
  device = "cuda" if torch.cuda.is_available() else "cpu"
34
+
35
+ # Flux.1-krea pipeline
36
  taef1 = AutoencoderTiny.from_pretrained("madebyollin/taef1", torch_dtype=dtype).to(device)
 
37
  good_vae = AutoencoderKL.from_pretrained("prithivMLmods/Flux.1-Krea-Merged-Dev", subfolder="vae", torch_dtype=dtype).to(device)
38
  pipe_krea = DiffusionPipeline.from_pretrained("prithivMLmods/Flux.1-Krea-Merged-Dev", torch_dtype=dtype, vae=taef1).to(device)
39
 
40
+ # Qwen/Qwen-Image pipeline
41
+ pipe_qwen = DiffusionPipeline.from_pretrained("Qwen/Qwen-Image", torch_dtype=dtype).to(device)
42
+
43
+ # Define custom flux_pipe_call for Flux.1-krea
44
  @torch.inference_mode()
45
  def flux_pipe_call_that_returns_an_iterable_of_images(
46
  self,
 
162
 
163
  pipe_krea.flux_pipe_call_that_returns_an_iterable_of_images = flux_pipe_call_that_returns_an_iterable_of_images.__get__(pipe_krea)
164
 
165
+ # Helper functions for Flux.1-krea
166
  def calculate_shift(
167
  image_seq_len,
168
  base_seq_len: int = 256,
 
198
  timesteps = scheduler.timesteps
199
  return timesteps, num_inference_steps
200
 
201
+ # Aspect ratios
202
+ aspect_ratios = {
203
+ "1:1": (1328, 1328),
204
+ "16:9": (1664, 928),
205
+ "9:16": (928, 1664),
206
+ "4:3": (1472, 1140),
207
+ "3:4": (1140, 1472)
208
+ }
 
 
 
 
 
 
 
209
 
210
+ # Generation function for Flux.1-krea
211
  @spaces.GPU
212
+ def generate_krea(
213
  prompt: str,
 
 
214
  seed: int = 0,
215
  width: int = 1024,
216
  height: int = 1024,
217
+ guidance_scale: float = 4.5,
218
  randomize_seed: bool = False,
219
+ num_inference_steps: int = 28,
 
220
  num_images: int = 1,
221
  zip_images: bool = False,
222
  progress=gr.Progress(track_tqdm=True),
223
  ):
224
+ if randomize_seed:
225
+ seed = random.randint(0, MAX_SEED)
226
+ generator = torch.Generator(device).manual_seed(seed)
 
 
 
 
 
 
 
 
 
 
 
227
 
228
  start_time = time.time()
229
 
230
+ images = []
231
+ for _ in range(num_images):
232
+ final_img = list(pipe_krea.flux_pipe_call_that_returns_an_iterable_of_images(
233
+ prompt=prompt,
234
+ guidance_scale=guidance_scale,
235
+ num_inference_steps=num_inference_steps,
236
+ width=width,
237
+ height=height,
238
+ generator=generator,
239
+ output_type="pil",
240
+ good_vae=good_vae,
241
+ ))[-1] # Take the final image only
242
+ images.append(final_img)
243
 
244
  end_time = time.time()
245
  duration = end_time - start_time
 
256
 
257
  return image_paths, seed, f"{duration:.2f}", zip_path
258
 
259
+ # Generation function for Qwen/Qwen-Image
260
  @spaces.GPU
261
+ def generate_qwen(
262
  prompt: str,
263
+ negative_prompt: str = "",
264
  seed: int = 0,
265
  width: int = 1024,
266
  height: int = 1024,
267
+ guidance_scale: float = 4.0,
268
  randomize_seed: bool = False,
269
+ num_inference_steps: int = 50,
270
  num_images: int = 1,
271
  zip_images: bool = False,
272
  progress=gr.Progress(track_tqdm=True),
273
  ):
274
  if randomize_seed:
275
  seed = random.randint(0, MAX_SEED)
276
+ generator = torch.Generator(device).manual_seed(seed)
277
 
278
  start_time = time.time()
279
 
280
+ images = pipe_qwen(
281
+ prompt=prompt,
282
+ negative_prompt=negative_prompt if negative_prompt else None,
283
+ height=height,
284
+ width=width,
285
+ guidance_scale=guidance_scale,
286
+ num_inference_steps=num_inference_steps,
287
+ num_images_per_prompt=num_images,
288
+ generator=generator,
289
+ output_type="pil",
290
+ ).images
 
 
291
 
292
  end_time = time.time()
293
  duration = end_time - start_time
 
304
 
305
  return image_paths, seed, f"{duration:.2f}", zip_path
306
 
307
+ # Main generation function
308
  @spaces.GPU
309
  def generate(
310
  model_choice: str,
 
314
  seed: int = 0,
315
  width: int = 1024,
316
  height: int = 1024,
317
+ guidance_scale: float = 3.5,
318
  randomize_seed: bool = False,
319
+ num_inference_steps: int = 28,
 
320
  num_images: int = 1,
321
  zip_images: bool = False,
322
  progress=gr.Progress(track_tqdm=True),
323
  ):
324
+ if model_choice == "Flux.1-krea":
325
+ return generate_krea(
326
  prompt=prompt,
 
 
327
  seed=seed,
328
  width=width,
329
  height=height,
330
  guidance_scale=guidance_scale,
331
  randomize_seed=randomize_seed,
 
332
  num_inference_steps=num_inference_steps,
333
  num_images=num_images,
334
  zip_images=zip_images,
335
  progress=progress,
336
  )
337
+ elif model_choice == "Qwen Image":
338
+ final_negative_prompt = negative_prompt if use_negative_prompt else ""
339
+ return generate_qwen(
340
  prompt=prompt,
341
+ negative_prompt=final_negative_prompt,
342
  seed=seed,
343
  width=width,
344
  height=height,
 
352
  else:
353
  raise ValueError("Invalid model choice")
354
 
355
+ # Examples
356
  examples = [
357
+ "An attractive young woman with blue eyes lying face down on the bed, light white and light amber, timeless beauty, sunrays shine upon it",
358
+ "Headshot of handsome young man, wearing dark gray sweater, brown hair and short beard, serious look, black background, soft studio lighting",
359
+ "A medium-angle shot of a young woman with long brown hair, wearing glasses, standing in front of purple and white lights",
360
+ "High-resolution photograph of a woman, photorealistic, vibrant colors"
361
  ]
362
 
363
  css = '''
 
388
  result = gr.Gallery(label="Result", columns=1, show_label=False, preview=True)
389
 
390
  with gr.Row():
 
391
  model_choice = gr.Radio(
392
+ choices=["Flux.1-krea", "Qwen Image"],
393
  label="Select Model",
394
+ value="Flux.1-krea"
395
  )
396
 
397
  with gr.Accordion("Additional Options", open=False):
398
+ aspect_ratio = gr.Dropdown(
399
+ label="Aspect Ratio",
400
+ choices=list(aspect_ratios.keys()),
401
+ value="1:1",
402
+ )
403
+ use_negative_prompt = gr.Checkbox(
404
+ label="Use negative prompt (Qwen Image only)",
405
+ value=False,
406
+ visible=False
407
  )
 
408
  negative_prompt = gr.Text(
409
  label="Negative prompt",
410
  max_lines=1,
 
436
  )
437
  guidance_scale = gr.Slider(
438
  label="Guidance Scale",
439
+ minimum=0.0,
440
  maximum=20.0,
441
  step=0.1,
442
  value=3.5,
 
444
  num_inference_steps = gr.Slider(
445
  label="Number of inference steps",
446
  minimum=1,
447
+ maximum=100,
448
  step=1,
449
  value=28,
450
  )
 
462
  generation_time = gr.Textbox(label="Generation time (seconds)", interactive=False)
463
  zip_file = gr.File(label="Download ZIP")
464
 
465
+ # Update aspect ratio
466
+ def set_dimensions(ar):
467
+ w, h = aspect_ratios[ar]
468
+ return gr.update(value=w), gr.update(value=h)
469
+
470
+ aspect_ratio.change(
471
+ fn=set_dimensions,
472
+ inputs=aspect_ratio,
473
+ outputs=[width, height]
474
  )
475
 
476
+ # Update model-specific settings
477
+ def update_settings(mc):
478
+ if mc == "Flux.1-krea":
479
+ return (
480
+ gr.update(value=28),
481
+ gr.update(value=3.5),
482
+ gr.update(visible=False)
483
+ )
484
+ elif mc == "Qwen Image":
485
+ return (
486
+ gr.update(value=50),
487
+ gr.update(value=4.0),
488
+ gr.update(visible=True)
489
+ )
490
+
491
+ model_choice.change(
492
+ fn=update_settings,
493
+ inputs=model_choice,
494
+ outputs=[num_inference_steps, guidance_scale, use_negative_prompt]
495
+ )
496
+
497
+ # Negative prompt visibility
498
  use_negative_prompt.change(
499
  fn=lambda x: gr.update(visible=x),
500
  inputs=use_negative_prompt,
501
+ outputs=negative_prompt
 
502
  )
503
 
504
+ # Run button and prompt submit
505
  gr.on(
506
+ triggers=[prompt.submit, run_button.click],
 
 
 
507
  fn=generate,
508
  inputs=[
509
  model_choice,
 
515
  height,
516
  guidance_scale,
517
  randomize_seed,
 
518
  num_inference_steps,
519
  num_images,
520
  zip_images,
 
523
  api_name="run",
524
  )
525
 
526
+ # Examples
527
+ gr.Examples(
528
+ examples=examples,
529
+ inputs=prompt,
530
+ outputs=[result, seed_display, generation_time, zip_file],
531
+ fn=generate,
532
+ cache_examples=False,
533
+ )
534
+
535
  if __name__ == "__main__":
536
  demo.queue(max_size=30).launch(mcp_server=True, ssr_mode=False, show_error=True)