rinaldih95 commited on
Commit
336f1df
·
1 Parent(s): 9af16ee

add application file

Browse files
Files changed (1) hide show
  1. app.py +859 -0
app.py ADDED
@@ -0,0 +1,859 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import dotenv
2
+
3
+ dotenv.load_dotenv(override=True)
4
+
5
+ import gradio as gr
6
+
7
+ import os
8
+ import argparse
9
+ import random
10
+ from datetime import datetime
11
+
12
+ import torch
13
+ from torchvision.transforms.functional import to_pil_image, to_tensor
14
+
15
+ from accelerate import Accelerator
16
+
17
+ from omnigen2.pipelines.omnigen2.pipeline_omnigen2 import OmniGen2Pipeline
18
+ from omnigen2.models.transformers.transformer_omnigen2 import OmniGen2Transformer2DModel
19
+ from omnigen2.schedulers.scheduling_flow_match_euler_discrete import FlowMatchEulerDiscreteScheduler
20
+ from omnigen2.schedulers.scheduling_dpmsolver_multistep import DPMSolverMultistepScheduler
21
+ from omnigen2.utils.img_util import create_collage
22
+
23
+ NEGATIVE_PROMPT = "(((deformed))), blurry, over saturation, bad anatomy, disfigured, poorly drawn face, mutation, mutated, (extra_limb), (ugly), (poorly drawn hands), fused fingers, messy drawing, broken legs censor, censored, censor_bar"
24
+ ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
25
+
26
+ pipeline = None
27
+ accelerator = None
28
+ save_images = False
29
+
30
+ def load_pipeline(accelerator, weight_dtype, args):
31
+ pipeline = OmniGen2Pipeline.from_pretrained(
32
+ args.model_path,
33
+ torch_dtype=weight_dtype,
34
+ trust_remote_code=True,
35
+ )
36
+ pipeline.transformer = OmniGen2Transformer2DModel.from_pretrained(
37
+ args.model_path,
38
+ subfolder="transformer",
39
+ torch_dtype=weight_dtype,
40
+ )
41
+ if args.enable_sequential_cpu_offload:
42
+ pipeline.enable_sequential_cpu_offload()
43
+ elif args.enable_model_cpu_offload:
44
+ pipeline.enable_model_cpu_offload()
45
+ else:
46
+ pipeline = pipeline.to(accelerator.device)
47
+ return pipeline
48
+
49
+
50
+ def run(
51
+ instruction,
52
+ width_input,
53
+ height_input,
54
+ scheduler,
55
+ num_inference_steps,
56
+ image_input_1,
57
+ image_input_2,
58
+ image_input_3,
59
+ negative_prompt,
60
+ guidance_scale_input,
61
+ img_guidance_scale_input,
62
+ cfg_range_start,
63
+ cfg_range_end,
64
+ num_images_per_prompt,
65
+ max_input_image_side_length,
66
+ max_pixels,
67
+ seed_input,
68
+ progress=gr.Progress(),
69
+ ):
70
+ input_images = [image_input_1, image_input_2, image_input_3]
71
+ input_images = [img for img in input_images if img is not None]
72
+
73
+ if len(input_images) == 0:
74
+ input_images = None
75
+
76
+ if seed_input == -1:
77
+ seed_input = random.randint(0, 2**16 - 1)
78
+
79
+ generator = torch.Generator(device=accelerator.device).manual_seed(seed_input)
80
+
81
+ def progress_callback(cur_step, timesteps):
82
+ frac = (cur_step + 1) / float(timesteps)
83
+ progress(frac)
84
+
85
+ if scheduler == 'euler':
86
+ pipeline.scheduler = FlowMatchEulerDiscreteScheduler()
87
+ elif scheduler == 'dpmsolver':
88
+ pipeline.scheduler = DPMSolverMultistepScheduler(
89
+ algorithm_type="dpmsolver++",
90
+ solver_type="midpoint",
91
+ solver_order=2,
92
+ prediction_type="flow_prediction",
93
+ )
94
+
95
+ results = pipeline(
96
+ prompt=instruction,
97
+ input_images=input_images,
98
+ width=width_input,
99
+ height=height_input,
100
+ max_input_image_side_length=max_input_image_side_length,
101
+ max_pixels=max_pixels,
102
+ num_inference_steps=num_inference_steps,
103
+ max_sequence_length=1024,
104
+ text_guidance_scale=guidance_scale_input,
105
+ image_guidance_scale=img_guidance_scale_input,
106
+ cfg_range=(cfg_range_start, cfg_range_end),
107
+ negative_prompt=negative_prompt,
108
+ num_images_per_prompt=num_images_per_prompt,
109
+ generator=generator,
110
+ output_type="pil",
111
+ step_func=progress_callback,
112
+ )
113
+
114
+ progress(1.0)
115
+
116
+ vis_images = [to_tensor(image) * 2 - 1 for image in results.images]
117
+ output_image = create_collage(vis_images)
118
+
119
+ if save_images:
120
+ # Create outputs directory if it doesn't exist
121
+ output_dir = os.path.join(ROOT_DIR, "outputs_gradio")
122
+ os.makedirs(output_dir, exist_ok=True)
123
+
124
+ # Generate unique filename with timestamp
125
+ timestamp = datetime.now().strftime("%Y_%m_%d-%H_%M_%S")
126
+
127
+ # Generate unique filename with timestamp
128
+ output_path = os.path.join(output_dir, f"{timestamp}.png")
129
+ # Save the image
130
+ output_image.save(output_path)
131
+
132
+ # Save All Generated Images
133
+ if len(results.images) > 1:
134
+ for i, image in enumerate(results.images):
135
+ image_name, ext = os.path.splitext(output_path)
136
+ image.save(f"{image_name}_{i}{ext}")
137
+ return output_image
138
+
139
+
140
+ def get_example():
141
+ case = [
142
+ [
143
+ "The sun rises slightly, the dew on the rose petals in the garden is clear, a crystal ladybug is crawling to the dew, the background is the early morning garden, macro lens.",
144
+ 1024,
145
+ 1024,
146
+ "euler",
147
+ 50,
148
+ None,
149
+ None,
150
+ None,
151
+ NEGATIVE_PROMPT,
152
+ 3.5,
153
+ 1.0,
154
+ 0.0,
155
+ 1.0,
156
+ 1,
157
+ 2048,
158
+ 1024 * 1024,
159
+ 0,
160
+ ],
161
+ [
162
+ "A snow maiden with pale translucent skin, frosty white lashes, and a soft expression of longing",
163
+ 1024,
164
+ 1024,
165
+ "euler",
166
+ 50,
167
+ None,
168
+ None,
169
+ None,
170
+ NEGATIVE_PROMPT,
171
+ 3.5,
172
+ 1.0,
173
+ 0.0,
174
+ 1.0,
175
+ 1,
176
+ 2048,
177
+ 1024 * 1024,
178
+ 0,
179
+ ],
180
+ [
181
+ "Add a fisherman hat to the woman's head",
182
+ 1024,
183
+ 1024,
184
+ "euler",
185
+ 50,
186
+ os.path.join(ROOT_DIR, "example_images/flux5.png"),
187
+ None,
188
+ None,
189
+ NEGATIVE_PROMPT,
190
+ 5.0,
191
+ 2.0,
192
+ 0.0,
193
+ 1.0,
194
+ 1,
195
+ 2048,
196
+ 1024 * 1024,
197
+ 0,
198
+ ],
199
+ [
200
+ " replace the sword with a hammer.",
201
+ 1024,
202
+ 1024,
203
+ "euler",
204
+ 50,
205
+ os.path.join(
206
+ ROOT_DIR,
207
+ "example_images/d8f8f44c64106e7715c61b5dfa9d9ca0974314c5d4a4a50418acf7ff373432bb.png",
208
+ ),
209
+ None,
210
+ None,
211
+ NEGATIVE_PROMPT,
212
+ 5.0,
213
+ 2.0,
214
+ 0.0,
215
+ 1.0,
216
+ 1,
217
+ 2048,
218
+ 1024 * 1024,
219
+ 0,
220
+ ],
221
+ [
222
+ "Extract the character from the picture and fill the rest of the background with white.",
223
+ # "Transform the sculpture into jade",
224
+ 1024,
225
+ 1024,
226
+ "euler",
227
+ 50,
228
+ os.path.join(
229
+ ROOT_DIR, "example_images/46e79704-c88e-4e68-97b4-b4c40cd29826.png"
230
+ ),
231
+ None,
232
+ None,
233
+ NEGATIVE_PROMPT,
234
+ 5.0,
235
+ 2.0,
236
+ 0.0,
237
+ 1.0,
238
+ 1,
239
+ 2048,
240
+ 1024 * 1024,
241
+ 0,
242
+ ],
243
+ [
244
+ "Make he smile",
245
+ 1024,
246
+ 1024,
247
+ "euler",
248
+ 50,
249
+ os.path.join(
250
+ ROOT_DIR, "example_images/vicky-hladynets-C8Ta0gwPbQg-unsplash.jpg"
251
+ ),
252
+ None,
253
+ None,
254
+ NEGATIVE_PROMPT,
255
+ 5.0,
256
+ 2.0,
257
+ 0.0,
258
+ 1.0,
259
+ 1,
260
+ 2048,
261
+ 1024 * 1024,
262
+ 0,
263
+ ],
264
+ [
265
+ "Change the background to classroom",
266
+ 1024,
267
+ 1024,
268
+ "euler",
269
+ 50,
270
+ os.path.join(ROOT_DIR, "example_images/ComfyUI_temp_mllvz_00071_.png"),
271
+ None,
272
+ None,
273
+ NEGATIVE_PROMPT,
274
+ 5.0,
275
+ 2.0,
276
+ 0.0,
277
+ 1.0,
278
+ 1,
279
+ 2048,
280
+ 1024 * 1024,
281
+ 0,
282
+ ],
283
+ [
284
+ "Raise his hand",
285
+ 1024,
286
+ 1024,
287
+ "euler",
288
+ 50,
289
+ os.path.join(
290
+ ROOT_DIR,
291
+ "example_images/289089159-a6d7abc142419e63cab0a566eb38e0fb6acb217b340f054c6172139b316f6596.png",
292
+ ),
293
+ None,
294
+ None,
295
+ NEGATIVE_PROMPT,
296
+ 5.0,
297
+ 2.0,
298
+ 0.0,
299
+ 1.0,
300
+ 1,
301
+ 2048,
302
+ 1024 * 1024,
303
+ 0,
304
+ ],
305
+ [
306
+ "Generate a photo of an anime-style figurine placed on a desk. The figurine model should be based on the character photo provided in the attachment, accurately replicating the full-body pose, facial expression, and clothing style of the character in the photo, ensuring the entire figurine is fully presented. The overall design should be exquisite and detailed, soft gradient colors and a delicate texture, leaning towards a Japanese anime style, rich in details, with a realistic quality and beautiful visual appeal.",
307
+ 1024,
308
+ 1024,
309
+ "euler",
310
+ 50,
311
+ os.path.join(ROOT_DIR, "example_images/RAL_0315.JPG"),
312
+ None,
313
+ None,
314
+ NEGATIVE_PROMPT,
315
+ 5.0,
316
+ 2.0,
317
+ 0.0,
318
+ 1.0,
319
+ 1,
320
+ 2048,
321
+ 1024 * 1024,
322
+ 0,
323
+ ],
324
+ [
325
+ "Change the dress to blue.",
326
+ 1024,
327
+ 1024,
328
+ "euler",
329
+ 50,
330
+ os.path.join(ROOT_DIR, "example_images/1.png"),
331
+ None,
332
+ None,
333
+ NEGATIVE_PROMPT,
334
+ 5.0,
335
+ 2.0,
336
+ 0.0,
337
+ 1.0,
338
+ 1,
339
+ 2048,
340
+ 1024 * 1024,
341
+ 0,
342
+ ],
343
+ [
344
+ "Remove the cat",
345
+ 1024,
346
+ 1024,
347
+ "euler",
348
+ 50,
349
+ os.path.join(
350
+ ROOT_DIR,
351
+ "example_images/386724677-589d19050d4ea0603aee6831459aede29a24f4d8668c62c049f413db31508a54.png",
352
+ ),
353
+ None,
354
+ None,
355
+ NEGATIVE_PROMPT,
356
+ 5.0,
357
+ 2.0,
358
+ 0.0,
359
+ 1.0,
360
+ 1,
361
+ 2048,
362
+ 1024 * 1024,
363
+ 0,
364
+ ],
365
+ [
366
+ "In a cozy café, the anime figure is sitting in front of a laptop, smiling confidently.",
367
+ 1024,
368
+ 1024,
369
+ "euler",
370
+ 50,
371
+ os.path.join(ROOT_DIR, "example_images/ComfyUI_00254_.png"),
372
+ None,
373
+ None,
374
+ NEGATIVE_PROMPT,
375
+ 5.0,
376
+ 2.0,
377
+ 0.0,
378
+ 1.0,
379
+ 1,
380
+ 2048,
381
+ 1024 * 1024,
382
+ 0,
383
+ ],
384
+ [
385
+ "Create a wedding figure based on the girl in the first image and the man in the second image. Set the background as a wedding hall, with the man dressed in a suit and the girl in a white wedding dress. Ensure that the original faces remain unchanged and are accurately preserved. The man should adopt a realistic style, whereas the girl should maintain their classic anime style.",
386
+ 1024,
387
+ 1024,
388
+ "euler",
389
+ 50,
390
+ os.path.join(ROOT_DIR, "example_images/1_20241127203215.png"),
391
+ os.path.join(ROOT_DIR, "example_images/000050281.jpg"),
392
+ None,
393
+ NEGATIVE_PROMPT,
394
+ 5.0,
395
+ 3.0,
396
+ 0.0,
397
+ 1.0,
398
+ 1,
399
+ 2048,
400
+ 1024 * 1024,
401
+ 0,
402
+ ],
403
+ [
404
+ "Let the girl and the boy get married in the church. ",
405
+ 1024,
406
+ 1024,
407
+ "euler",
408
+ 50,
409
+ os.path.join(ROOT_DIR, "example_images/8FtFUxRzXqaguVRGzkHvN.png"),
410
+ os.path.join(ROOT_DIR, "example_images/01194-20240127001056_1024x1536.png"),
411
+ None,
412
+ NEGATIVE_PROMPT,
413
+ 5.0,
414
+ 3.0,
415
+ 0.0,
416
+ 1.0,
417
+ 1,
418
+ 2048,
419
+ 1024 * 1024,
420
+ 0,
421
+ ],
422
+ [
423
+ "Let the man from image1 and the woman from image2 kiss and hug",
424
+ 1024,
425
+ 1024,
426
+ "euler",
427
+ 50,
428
+ os.path.join(ROOT_DIR, "example_images/1280X1280.png"),
429
+ os.path.join(ROOT_DIR, "example_images/000077066.jpg"),
430
+ None,
431
+ NEGATIVE_PROMPT,
432
+ 5.0,
433
+ 2.0,
434
+ 0.0,
435
+ 1.0,
436
+ 1,
437
+ 2048,
438
+ 1024 * 1024,
439
+ 0,
440
+ ],
441
+ [
442
+ "Please let the person in image 2 hold the toy from the first image in a parking lot.",
443
+ 1024,
444
+ 1024,
445
+ "euler",
446
+ 50,
447
+ os.path.join(ROOT_DIR, "example_images/04.jpg"),
448
+ os.path.join(ROOT_DIR, "example_images/000365954.jpg"),
449
+ None,
450
+ NEGATIVE_PROMPT,
451
+ 5.0,
452
+ 2.0,
453
+ 0.0,
454
+ 1.0,
455
+ 1,
456
+ 2048,
457
+ 1024 * 1024,
458
+ 0,
459
+ ],
460
+ [
461
+ "Make the girl pray in the second image.",
462
+ 1024,
463
+ 682,
464
+ "euler",
465
+ 50,
466
+ os.path.join(ROOT_DIR, "example_images/000440817.jpg"),
467
+ os.path.join(ROOT_DIR, "example_images/000119733.jpg"),
468
+ None,
469
+ NEGATIVE_PROMPT,
470
+ 5.0,
471
+ 2.0,
472
+ 0.0,
473
+ 1.0,
474
+ 1,
475
+ 2048,
476
+ 1024 * 1024,
477
+ 0,
478
+ ],
479
+ [
480
+ "Add the bird from image 1 to the desk in image 2",
481
+ 1024,
482
+ 682,
483
+ "euler",
484
+ 50,
485
+ os.path.join(
486
+ ROOT_DIR,
487
+ "example_images/996e2cf6-daa5-48c4-9ad7-0719af640c17_1748848108409.png",
488
+ ),
489
+ os.path.join(ROOT_DIR, "example_images/00066-10350085.png"),
490
+ None,
491
+ NEGATIVE_PROMPT,
492
+ 5.0,
493
+ 2.0,
494
+ 0.0,
495
+ 1.0,
496
+ 1,
497
+ 2048,
498
+ 1024 * 1024,
499
+ 0,
500
+ ],
501
+ [
502
+ "Replace the apple in the first image with the cat from the second image",
503
+ 1024,
504
+ 780,
505
+ "euler",
506
+ 50,
507
+ os.path.join(ROOT_DIR, "example_images/apple.png"),
508
+ os.path.join(
509
+ ROOT_DIR,
510
+ "example_images/468404374-d52ec1a44aa7e0dc9c2807ce09d303a111c78f34da3da2401b83ce10815ff872.png",
511
+ ),
512
+ None,
513
+ NEGATIVE_PROMPT,
514
+ 5.0,
515
+ 2.0,
516
+ 0.0,
517
+ 1.0,
518
+ 1,
519
+ 2048,
520
+ 1024 * 1024,
521
+ 0,
522
+ ],
523
+ [
524
+ "Replace the woman in the second image with the woman from the first image",
525
+ 1024,
526
+ 747,
527
+ "euler",
528
+ 50,
529
+ os.path.join(
530
+ ROOT_DIR, "example_images/byward-outfitters-B97YFrsITyo-unsplash.jpg"
531
+ ),
532
+ os.path.join(
533
+ ROOT_DIR, "example_images/6652baf6-4096-40ef-a475-425e4c072daf.png"
534
+ ),
535
+ None,
536
+ NEGATIVE_PROMPT,
537
+ 5.0,
538
+ 2.0,
539
+ 0.0,
540
+ 1.0,
541
+ 1,
542
+ 2048,
543
+ 1024 * 1024,
544
+ 0,
545
+ ],
546
+ [
547
+ "The cat is sitting on the table. The bird is perching on the edge of the table.",
548
+ 800,
549
+ 512,
550
+ "euler",
551
+ 50,
552
+ os.path.join(
553
+ ROOT_DIR,
554
+ "example_images/996e2cf6-daa5-48c4-9ad7-0719af640c17_1748848108409.png",
555
+ ),
556
+ os.path.join(
557
+ ROOT_DIR,
558
+ "example_images/468404374-d52ec1a44aa7e0dc9c2807ce09d303a111c78f34da3da2401b83ce10815ff872.png",
559
+ ),
560
+ os.path.join(ROOT_DIR, "example_images/00066-10350085.png"),
561
+ NEGATIVE_PROMPT,
562
+ 5.0,
563
+ 2.0,
564
+ 0.0,
565
+ 1.0,
566
+ 1,
567
+ 2048,
568
+ 1024 * 1024,
569
+ 0,
570
+ ],
571
+ ]
572
+ return case
573
+
574
+
575
+ def run_for_examples(
576
+ instruction,
577
+ width_input,
578
+ height_input,
579
+ scheduler,
580
+ num_inference_steps,
581
+ image_input_1,
582
+ image_input_2,
583
+ image_input_3,
584
+ negative_prompt,
585
+ text_guidance_scale_input,
586
+ image_guidance_scale_input,
587
+ cfg_range_start,
588
+ cfg_range_end,
589
+ num_images_per_prompt,
590
+ max_input_image_side_length,
591
+ max_pixels,
592
+ seed_input,
593
+ ):
594
+ return run(
595
+ instruction,
596
+ width_input,
597
+ height_input,
598
+ scheduler,
599
+ num_inference_steps,
600
+ image_input_1,
601
+ image_input_2,
602
+ image_input_3,
603
+ negative_prompt,
604
+ text_guidance_scale_input,
605
+ image_guidance_scale_input,
606
+ cfg_range_start,
607
+ cfg_range_end,
608
+ num_images_per_prompt,
609
+ max_input_image_side_length,
610
+ max_pixels,
611
+ seed_input,
612
+ )
613
+
614
+ description = """
615
+ ### 💡 Quick Tips for Best Results (see our [github](https://github.com/VectorSpaceLab/OmniGen2?tab=readme-ov-file#-usage-tips) for more details)
616
+ - Image Quality: Use high-resolution images (at least 512x512 recommended).
617
+ - Be Specific: Instead of "Add bird to desk", try "Add the bird from image 1 to the desk in image 2".
618
+ - Use English: English prompts currently yield better results.
619
+ - Adjust image_guidance_scale for better consistency with the reference image:
620
+ - Image Editing: 1.3 - 2.0
621
+ - In-context Generation: 2.0 - 3.0
622
+ """
623
+
624
+ article = """
625
+ ```bibtex
626
+ @article{wu2025omnigen2,
627
+ title={OmniGen2: Exploration to Advanced Multimodal Generation},
628
+ author={Chenyuan Wu and Pengfei Zheng and Ruiran Yan and Shitao Xiao and Xin Luo and Yueze Wang and Wanli Li and Xiyan Jiang and Yexin Liu and Junjie Zhou and Ze Liu and Ziyi Xia and Chaofan Li and Haoge Deng and Jiahao Wang and Kun Luo and Bo Zhang and Defu Lian and Xinlong Wang and Zhongyuan Wang and Tiejun Huang and Zheng Liu},
629
+ journal={arXiv preprint arXiv:2506.18871},
630
+ year={2025}
631
+ }
632
+ ```
633
+ """
634
+
635
+ def main(args):
636
+ # Gradio
637
+ with gr.Blocks() as demo:
638
+ gr.Markdown(
639
+ "# OmniGen2: Exploration to Advanced Multimodal Generation [paper](https://arxiv.org/abs/2506.18871) [code](https://github.com/VectorSpaceLab/OmniGen2)"
640
+ )
641
+ gr.Markdown(description)
642
+ with gr.Row():
643
+ with gr.Column():
644
+ # text prompt
645
+ instruction = gr.Textbox(
646
+ label='Enter your prompt. Use "first/second image" or “第一张图/第二张图” as reference.',
647
+ placeholder="Type your prompt here...",
648
+ )
649
+
650
+ with gr.Row(equal_height=True):
651
+ # input images
652
+ image_input_1 = gr.Image(label="First Image", type="pil")
653
+ image_input_2 = gr.Image(label="Second Image", type="pil")
654
+ image_input_3 = gr.Image(label="Third Image", type="pil")
655
+
656
+ generate_button = gr.Button("Generate Image")
657
+
658
+ negative_prompt = gr.Textbox(
659
+ label="Enter your negative prompt",
660
+ placeholder="Type your negative prompt here...",
661
+ value=NEGATIVE_PROMPT,
662
+ )
663
+
664
+ # slider
665
+ with gr.Row(equal_height=True):
666
+ height_input = gr.Slider(
667
+ label="Height", minimum=256, maximum=1024, value=1024, step=128
668
+ )
669
+ width_input = gr.Slider(
670
+ label="Width", minimum=256, maximum=1024, value=1024, step=128
671
+ )
672
+ with gr.Row(equal_height=True):
673
+ text_guidance_scale_input = gr.Slider(
674
+ label="Text Guidance Scale",
675
+ minimum=1.0,
676
+ maximum=8.0,
677
+ value=5.0,
678
+ step=0.1,
679
+ )
680
+
681
+ image_guidance_scale_input = gr.Slider(
682
+ label="Image Guidance Scale",
683
+ minimum=1.0,
684
+ maximum=3.0,
685
+ value=2.0,
686
+ step=0.1,
687
+ )
688
+ with gr.Row(equal_height=True):
689
+ cfg_range_start = gr.Slider(
690
+ label="CFG Range Start",
691
+ minimum=0.0,
692
+ maximum=1.0,
693
+ value=0.0,
694
+ step=0.1,
695
+ )
696
+
697
+ cfg_range_end = gr.Slider(
698
+ label="CFG Range End",
699
+ minimum=0.0,
700
+ maximum=1.0,
701
+ value=1.0,
702
+ step=0.1,
703
+ )
704
+
705
+ def adjust_end_slider(start_val, end_val):
706
+ return max(start_val, end_val)
707
+
708
+ def adjust_start_slider(end_val, start_val):
709
+ return min(end_val, start_val)
710
+
711
+ cfg_range_start.input(
712
+ fn=adjust_end_slider,
713
+ inputs=[cfg_range_start, cfg_range_end],
714
+ outputs=[cfg_range_end]
715
+ )
716
+
717
+ cfg_range_end.input(
718
+ fn=adjust_start_slider,
719
+ inputs=[cfg_range_end, cfg_range_start],
720
+ outputs=[cfg_range_start]
721
+ )
722
+
723
+ with gr.Row(equal_height=True):
724
+ scheduler_input = gr.Dropdown(
725
+ label="Scheduler",
726
+ choices=["euler", "dpmsolver"],
727
+ value="euler",
728
+ info="The scheduler to use for the model.",
729
+ )
730
+
731
+ num_inference_steps = gr.Slider(
732
+ label="Inference Steps", minimum=20, maximum=100, value=50, step=1
733
+ )
734
+ with gr.Row(equal_height=True):
735
+ num_images_per_prompt = gr.Slider(
736
+ label="Number of images per prompt",
737
+ minimum=1,
738
+ maximum=4,
739
+ value=1,
740
+ step=1,
741
+ )
742
+
743
+ seed_input = gr.Slider(
744
+ label="Seed", minimum=-1, maximum=2147483647, value=0, step=1
745
+ )
746
+ with gr.Row(equal_height=True):
747
+ max_input_image_side_length = gr.Slider(
748
+ label="max_input_image_side_length",
749
+ minimum=256,
750
+ maximum=2048,
751
+ value=2048,
752
+ step=256,
753
+ )
754
+ max_pixels = gr.Slider(
755
+ label="max_pixels",
756
+ minimum=256 * 256,
757
+ maximum=1536 * 1536,
758
+ value=1024 * 1024,
759
+ step=256 * 256,
760
+ )
761
+
762
+ with gr.Column():
763
+ with gr.Column():
764
+ # output image
765
+ output_image = gr.Image(label="Output Image")
766
+ global save_images
767
+ save_images = gr.Checkbox(label="Save generated images", value=False)
768
+
769
+ global accelerator
770
+ global pipeline
771
+
772
+ bf16 = True
773
+ accelerator = Accelerator(mixed_precision="bf16" if bf16 else "no")
774
+ weight_dtype = torch.bfloat16 if bf16 else torch.float32
775
+
776
+ pipeline = load_pipeline(accelerator, weight_dtype, args)
777
+
778
+ # click
779
+ generate_button.click(
780
+ run,
781
+ inputs=[
782
+ instruction,
783
+ width_input,
784
+ height_input,
785
+ scheduler_input,
786
+ num_inference_steps,
787
+ image_input_1,
788
+ image_input_2,
789
+ image_input_3,
790
+ negative_prompt,
791
+ text_guidance_scale_input,
792
+ image_guidance_scale_input,
793
+ cfg_range_start,
794
+ cfg_range_end,
795
+ num_images_per_prompt,
796
+ max_input_image_side_length,
797
+ max_pixels,
798
+ seed_input,
799
+ ],
800
+ outputs=output_image,
801
+ )
802
+
803
+ gr.Examples(
804
+ examples=get_example(),
805
+ fn=run_for_examples,
806
+ inputs=[
807
+ instruction,
808
+ width_input,
809
+ height_input,
810
+ scheduler_input,
811
+ num_inference_steps,
812
+ image_input_1,
813
+ image_input_2,
814
+ image_input_3,
815
+ negative_prompt,
816
+ text_guidance_scale_input,
817
+ image_guidance_scale_input,
818
+ cfg_range_start,
819
+ cfg_range_end,
820
+ num_images_per_prompt,
821
+ max_input_image_side_length,
822
+ max_pixels,
823
+ seed_input,
824
+ ],
825
+ outputs=output_image,
826
+ )
827
+
828
+ gr.Markdown(article)
829
+ # launch
830
+ demo.launch(share=args.share, server_port=args.port, allowed_paths=[ROOT_DIR])
831
+
832
+ def parse_args():
833
+ parser = argparse.ArgumentParser(description="Run the OmniGen2")
834
+ parser.add_argument("--share", action="store_true", help="Share the Gradio app")
835
+ parser.add_argument(
836
+ "--port", type=int, default=7860, help="Port to use for the Gradio app"
837
+ )
838
+ parser.add_argument(
839
+ "--model_path",
840
+ type=str,
841
+ default="OmniGen2/OmniGen2",
842
+ help="Path or HuggingFace name of the model to load."
843
+ )
844
+ parser.add_argument(
845
+ "--enable_model_cpu_offload",
846
+ action="store_true",
847
+ help="Enable model CPU offload."
848
+ )
849
+ parser.add_argument(
850
+ "--enable_sequential_cpu_offload",
851
+ action="store_true",
852
+ help="Enable sequential CPU offload."
853
+ )
854
+ args = parser.parse_args()
855
+ return args
856
+
857
+ if __name__ == "__main__":
858
+ args = parse_args()
859
+ main(args)