multimodalart HF Staff commited on
Commit
d05e8db
·
verified ·
1 Parent(s): ea367b1

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. README.md +27 -7
  2. app.py +186 -0
  3. requirements.txt +6 -0
README.md CHANGED
@@ -1,13 +1,33 @@
1
  ---
2
- title: Flux Cmo Compositional
3
- emoji: 🐠
4
- colorFrom: yellow
5
- colorTo: indigo
6
  sdk: gradio
7
  sdk_version: 6.20.0
8
- python_version: '3.12'
9
  app_file: app.py
10
- pinned: false
 
 
11
  ---
12
 
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: FLUX.1-dev CMO Compositional
3
+ emoji: 🎨
4
+ colorFrom: gray
5
+ colorTo: purple
6
  sdk: gradio
7
  sdk_version: 6.20.0
 
8
  app_file: app.py
9
+ short_description: FLUX.1-dev fine-tuned with CMO for compositional generation
10
+ python_version: "3.12"
11
+ startup_duration_timeout: 30m
12
  ---
13
 
14
+ # FLUX.1-dev CMO Compositional Text-to-Image
15
+
16
+ This Space demonstrates **Correlation-Weighted Multi-Reward Optimization (CMO)** applied to FLUX.1-dev.
17
+ CMO improves compositional text-to-image generation by analyzing the correlation structure among
18
+ concept-wise rewards (object existence, attributes, numeracy, size, spatial relations) and adaptively
19
+ weighting conflicting concepts during training.
20
+
21
+ ## Model
22
+
23
+ - **Base:** [black-forest-labs/FLUX.1-dev](https://huggingface.co/black-forest-labs/FLUX.1-dev)
24
+ - **LoRA adapter:** [Bruece/FLUX.1-dev-CMO](https://huggingface.co/Bruece/FLUX.1-dev-CMO)
25
+
26
+ ## Usage
27
+
28
+ Enter a compositional prompt (e.g. "A red apple is on the left of the yellow banana") and click Run.
29
+ The model is loaded in bfloat16 with a tiny VAE for fast preview.
30
+
31
+ ## License
32
+
33
+ FLUX.1-dev carries a non-commercial license. See [LICENSE](https://huggingface.co/black-forest-labs/FLUX.1-dev/blob/main/LICENSE.md).
app.py ADDED
@@ -0,0 +1,186 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ os.environ.setdefault("PYTORCH_CUDA_ALLOC_CONF", "expandable_segments:True")
4
+
5
+ import spaces # MUST come before any CUDA-touching import
6
+ import torch
7
+ import gradio as gr
8
+ import random
9
+ import numpy as np
10
+ from diffusers import DiffusionPipeline, AutoencoderTiny, AutoencoderKL
11
+ from peft import PeftModel
12
+
13
+ dtype = torch.bfloat16
14
+ device = "cuda"
15
+
16
+ # Tiny VAE for fast preview, good VAE for final output (same pattern as official FLUX.1-dev space)
17
+ taef1 = AutoencoderTiny.from_pretrained("madebyollin/taef1", torch_dtype=dtype).to(device)
18
+ good_vae = AutoencoderKL.from_pretrained(
19
+ "black-forest-labs/FLUX.1-dev", subfolder="vae", torch_dtype=dtype
20
+ ).to(device)
21
+
22
+ # Load base FLUX.1-dev with tiny VAE, then apply CMO LoRA adapter
23
+ BASE_MODEL = "black-forest-labs/FLUX.1-dev"
24
+ CMO_LORA = "Bruece/FLUX.1-dev-CMO"
25
+
26
+ pipe = DiffusionPipeline.from_pretrained(
27
+ BASE_MODEL, torch_dtype=dtype, vae=taef1
28
+ ).to(device)
29
+
30
+ # Load and merge the CMO LoRA adapter
31
+ pipe.transformer = PeftModel.from_pretrained(pipe.transformer, CMO_LORA)
32
+ pipe.transformer = pipe.transformer.merge_and_unload()
33
+ pipe = pipe.to(device)
34
+
35
+ torch.cuda.empty_cache()
36
+
37
+ MAX_SEED = np.iinfo(np.int32).max
38
+ MAX_IMAGE_SIZE = 2048
39
+
40
+
41
+ def _estimate(prompt, num_inference_steps, *args, **kwargs):
42
+ return min(180, 30 + int(num_inference_steps * 4))
43
+
44
+
45
+ @spaces.GPU(duration=_estimate)
46
+ def generate(
47
+ prompt: str,
48
+ seed: int = 42,
49
+ randomize_seed: bool = True,
50
+ width: int = 1024,
51
+ height: int = 1024,
52
+ guidance_scale: float = 4.5,
53
+ num_inference_steps: int = 40,
54
+ progress: gr.Progress = gr.Progress(track_tqdm=True),
55
+ ):
56
+ """Generate an image from a text prompt using FLUX.1-dev fine-tuned with CMO.
57
+
58
+ CMO (Correlation-Weighted Multi-Reward Optimization) improves compositional
59
+ text-to-image generation by adaptively weighting conflicting concept rewards
60
+ (object existence, attributes, numeracy, size, spatial relations).
61
+
62
+ Args:
63
+ prompt: Text description of the image to generate.
64
+ seed: RNG seed for reproducibility.
65
+ randomize_seed: If True, pick a random seed each run.
66
+ width: Output image width in pixels.
67
+ height: Output image height in pixels.
68
+ guidance_scale: Classifier-free guidance scale.
69
+ num_inference_steps: Number of denoising steps.
70
+ """
71
+ if randomize_seed:
72
+ seed = random.randint(0, MAX_SEED)
73
+ seed = int(seed)
74
+ generator = torch.Generator().manual_seed(seed)
75
+ image = pipe(
76
+ prompt=prompt,
77
+ height=height,
78
+ width=width,
79
+ num_inference_steps=num_inference_steps,
80
+ guidance_scale=guidance_scale,
81
+ generator=generator,
82
+ ).images[0]
83
+ return image, seed
84
+
85
+
86
+ CSS = """
87
+ #col-container { max-width: 1100px; margin: 0 auto; }
88
+ .dark .gradio-container { color: var(--body-text-color); }
89
+ """
90
+
91
+ EXAMPLES = [
92
+ ["A red apple is on the left of the yellow banana"],
93
+ ["Two Tyrannosaurus rexes engaged in a boxing match"],
94
+ ["a photo of a black kite and a green bear"],
95
+ ["A brown cow wearing yellow sunglasses in a pastel chalk drawing"],
96
+ ["a cat holding a sign that says hello world"],
97
+ ["The green plant was on top of the white nightstand"],
98
+ ]
99
+
100
+ with gr.Blocks(theme=gr.themes.Citrus(), css=CSS) as demo:
101
+ with gr.Column(elem_id="col-container"):
102
+ gr.Markdown(
103
+ """# FLUX.1-dev CMO — Compositional Text-to-Image
104
+ FLUX.1-dev fine-tuned with **Correlation-Weighted Multi-Reward Optimization (CMO)** for improved compositional generation.
105
+ [[Paper](https://huggingface.co/papers/2603.18528)] [[Code](https://github.com/TheDarkKnight-21th/CMO)] [[Model](https://huggingface.co/Bruece/FLUX.1-dev-CMO)]
106
+ """
107
+ )
108
+ with gr.Row():
109
+ prompt = gr.Textbox(
110
+ label="Prompt",
111
+ show_label=False,
112
+ max_lines=1,
113
+ placeholder="Enter your prompt (e.g. 'A red apple is on the left of the yellow banana')",
114
+ container=False,
115
+ scale=4,
116
+ )
117
+ run_button = gr.Button("Run", variant="primary", scale=1)
118
+
119
+ result = gr.Image(label="Result", show_label=False)
120
+
121
+ with gr.Accordion("Advanced settings", open=False):
122
+ seed = gr.Slider(
123
+ label="Seed",
124
+ minimum=0,
125
+ maximum=MAX_SEED,
126
+ step=1,
127
+ value=42,
128
+ )
129
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
130
+ with gr.Row():
131
+ width = gr.Slider(
132
+ label="Width",
133
+ minimum=256,
134
+ maximum=MAX_IMAGE_SIZE,
135
+ step=32,
136
+ value=1024,
137
+ )
138
+ height = gr.Slider(
139
+ label="Height",
140
+ minimum=256,
141
+ maximum=MAX_IMAGE_SIZE,
142
+ step=32,
143
+ value=1024,
144
+ )
145
+ with gr.Row():
146
+ guidance_scale = gr.Slider(
147
+ label="Guidance Scale",
148
+ minimum=1,
149
+ maximum=15,
150
+ step=0.1,
151
+ value=4.5,
152
+ )
153
+ num_inference_steps = gr.Slider(
154
+ label="Number of inference steps",
155
+ minimum=1,
156
+ maximum=50,
157
+ step=1,
158
+ value=40,
159
+ )
160
+
161
+ gr.Examples(
162
+ examples=EXAMPLES,
163
+ fn=generate,
164
+ inputs=[prompt],
165
+ outputs=[result, seed],
166
+ cache_examples=True,
167
+ cache_mode="lazy",
168
+ )
169
+
170
+ gr.on(
171
+ triggers=[run_button.click, prompt.submit],
172
+ fn=generate,
173
+ inputs=[
174
+ prompt,
175
+ seed,
176
+ randomize_seed,
177
+ width,
178
+ height,
179
+ guidance_scale,
180
+ num_inference_steps,
181
+ ],
182
+ outputs=[result, seed],
183
+ api_name="generate",
184
+ )
185
+
186
+ demo.launch(mcp_server=True)
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ diffusers
2
+ peft
3
+ accelerate
4
+ torchvision
5
+ safetensors
6
+ sentencepiece