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 |
+
import spaces
|
6 |
+
import torch
|
7 |
+
from huggingface_hub import hf_hub_download, HfApi
|
8 |
+
from diffusers import FluxPriorReduxPipeline, FluxPipeline
|
9 |
+
from diffusers.utils import load_image
|
10 |
+
import os
|
11 |
+
api = HfApi(
|
12 |
+
token=os.getenv('HF_TOKEN'), # Token is not persisted on the machine.
|
13 |
+
)
|
14 |
+
MAX_SEED = np.iinfo(np.int32).max
|
15 |
+
MAX_IMAGE_SIZE = 2048
|
16 |
+
|
17 |
+
pipe = FluxPipeline.from_pretrained(
|
18 |
+
"black-forest-labs/FLUX.1-dev",
|
19 |
+
torch_dtype=torch.bfloat16,
|
20 |
+
token=os.getenv('HF_TOKEN'),
|
21 |
+
).to("cuda")
|
22 |
+
pipe.load_lora_weights(hf_hub_download("ByteDance/Hyper-SD", "Hyper-FLUX.1-dev-8steps-lora.safetensors"), lora_scale=0.125)
|
23 |
+
pipe.fuse_lora(lora_scale=0.125)
|
24 |
+
pipe.to(device="cuda", dtype=torch.bfloat16)
|
25 |
+
|
26 |
+
pipe_prior_redux = FluxPriorReduxPipeline.from_pretrained(
|
27 |
+
"ostris/Flex.1-alpha-Redux",
|
28 |
+
text_encoder=pipe.text_encoder,
|
29 |
+
tokenizer=pipe.tokenizer,
|
30 |
+
text_encoder_2=pipe.text_encoder_2,
|
31 |
+
tokenizer_2=pipe.tokenizer_2,
|
32 |
+
torch_dtype=torch.bfloat16
|
33 |
+
).to("cuda")
|
34 |
+
|
35 |
+
examples = [[Image.open("mona_lisa.jpg"), "pink hair, at the beach", None, "", 0.035, 1., 1., 1., 1., 0, False],
|
36 |
+
[Image.open("1665_Girl_with_a_Pearl_Earring.jpg"), "", Image.open("dali_example.jpg"), "", 0.08, .4, .6, .33, 1., 1912857110, False]]
|
37 |
+
|
38 |
+
@spaces.GPU
|
39 |
+
def infer(control_image, prompt, image_2, prompt_2, reference_scale= 0.03 ,
|
40 |
+
prompt_embeds_scale_1 =1, prompt_embeds_scale_2 =1, pooled_prompt_embeds_scale_1 =1, pooled_prompt_embeds_scale_2 =1,
|
41 |
+
seed=42, randomize_seed=False, width=1024, height=1024,
|
42 |
+
guidance_scale=3.5, num_inference_steps=28, progress=gr.Progress(track_tqdm=True)):
|
43 |
+
if randomize_seed:
|
44 |
+
seed = random.randint(0, MAX_SEED)
|
45 |
+
if image_2 is not None:
|
46 |
+
pipe_prior_output = pipe_prior_redux([control_image, image_2],
|
47 |
+
prompt=[prompt, prompt_2],
|
48 |
+
prompt_embeds_scale = [prompt_embeds_scale_1, prompt_embeds_scale_2],
|
49 |
+
pooled_prompt_embeds_scale = [pooled_prompt_embeds_scale_1, pooled_prompt_embeds_scale_2])
|
50 |
+
else:
|
51 |
+
pipe_prior_output = pipe_prior_redux(control_image, prompt=prompt, prompt_embeds_scale = [prompt_embeds_scale_1],
|
52 |
+
pooled_prompt_embeds_scale = [pooled_prompt_embeds_scale_1])
|
53 |
+
cond_size = 729
|
54 |
+
hidden_size = 4096
|
55 |
+
max_sequence_length = 512
|
56 |
+
full_attention_size = max_sequence_length + hidden_size + cond_size
|
57 |
+
attention_mask = torch.zeros(
|
58 |
+
(full_attention_size, full_attention_size), device="cuda", dtype=torch.bfloat16
|
59 |
+
)
|
60 |
+
bias = torch.log(
|
61 |
+
torch.tensor(reference_scale, dtype=torch.bfloat16, device="cuda").clamp(min=1e-5, max=1)
|
62 |
+
)
|
63 |
+
attention_mask[:, max_sequence_length : max_sequence_length + cond_size] = bias
|
64 |
+
joint_attention_kwargs=dict(attention_mask=attention_mask)
|
65 |
+
images = pipe(
|
66 |
+
guidance_scale=guidance_scale,
|
67 |
+
width=width,
|
68 |
+
height=height,
|
69 |
+
num_inference_steps=num_inference_steps,
|
70 |
+
generator=torch.Generator("cpu").manual_seed(seed),
|
71 |
+
joint_attention_kwargs=joint_attention_kwargs,
|
72 |
+
**pipe_prior_output,
|
73 |
+
).images[0]
|
74 |
+
return images, seed
|
75 |
+
|
76 |
+
css="""
|
77 |
+
#col-container {
|
78 |
+
margin: 0 auto;
|
79 |
+
max-width: 960px;
|
80 |
+
}
|
81 |
+
"""
|
82 |
+
|
83 |
+
with gr.Blocks(css=css) as demo:
|
84 |
+
|
85 |
+
with gr.Column(elem_id="col-container"):
|
86 |
+
gr.Markdown(f"""# ⚡️ Fast FLUX.1 Redux [dev] ⚡️
|
87 |
+
An adapter for FLUX [dev] to create image variations combined with ByteDance [
|
88 |
+
Hyper FLUX 8 Steps LoRA](https://huggingface.co/ByteDance/Hyper-SD) 🏎️
|
89 |
+
Now with added support:
|
90 |
+
- prompt input
|
91 |
+
- attention masking for improved prompt adherence
|
92 |
+
- multiple image interpolation
|
93 |
+
|
94 |
+
[[non-commercial license](https://huggingface.co/black-forest-labs/FLUX.1-dev/blob/main/LICENSE.md)] [[blog](https://blackforestlabs.ai/announcing-black-forest-labs/)] [[model](https://huggingface.co/black-forest-labs/FLUX.1-dev)]
|
95 |
+
""")
|
96 |
+
with gr.Row():
|
97 |
+
with gr.Column():
|
98 |
+
input_image = gr.Image(label="Image to create variations", type="pil")
|
99 |
+
prompt = gr.Text(
|
100 |
+
label="Prompt",
|
101 |
+
show_label=False,
|
102 |
+
max_lines=1,
|
103 |
+
placeholder="Enter your prompt",
|
104 |
+
container=False,
|
105 |
+
)
|
106 |
+
reference_scale = gr.Slider(
|
107 |
+
info="lower to enhance prompt adherence",
|
108 |
+
label="Masking Scale",
|
109 |
+
minimum=0.01,
|
110 |
+
maximum=0.08,
|
111 |
+
step=0.001,
|
112 |
+
value=0.03,
|
113 |
+
)
|
114 |
+
run_button = gr.Button("Run")
|
115 |
+
with gr.Column():
|
116 |
+
image_2 = gr.Image(label="2nd image to create interpolated variations", type="pil")
|
117 |
+
prompt_2 = gr.Text(
|
118 |
+
label="2nd Prompt",
|
119 |
+
show_label=False,
|
120 |
+
max_lines=1,
|
121 |
+
placeholder="Enter your prompt",
|
122 |
+
container=False,
|
123 |
+
)
|
124 |
+
|
125 |
+
result = gr.Image(label="Result", show_label=False)
|
126 |
+
|
127 |
+
with gr.Accordion("Advanced Settings", open=False):
|
128 |
+
|
129 |
+
seed = gr.Slider(
|
130 |
+
label="Seed",
|
131 |
+
minimum=0,
|
132 |
+
maximum=MAX_SEED,
|
133 |
+
step=1,
|
134 |
+
value=0,
|
135 |
+
)
|
136 |
+
|
137 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
138 |
+
|
139 |
+
with gr.Row():
|
140 |
+
prompt_embeds_scale_1 = gr.Slider(
|
141 |
+
label="prompt embeds scale 1st image",
|
142 |
+
minimum=0,
|
143 |
+
maximum=1.5,
|
144 |
+
step=0.01,
|
145 |
+
value=1,
|
146 |
+
)
|
147 |
+
prompt_embeds_scale_2 = gr.Slider(
|
148 |
+
label="prompt embeds scale 2nd image",
|
149 |
+
minimum=0,
|
150 |
+
maximum=1.5,
|
151 |
+
step=0.01,
|
152 |
+
value=1,
|
153 |
+
)
|
154 |
+
pooled_prompt_embeds_scale_1 = gr.Slider(
|
155 |
+
label="pooled prompt embeds scale 1nd image",
|
156 |
+
minimum=0,
|
157 |
+
maximum=1.5,
|
158 |
+
step=0.01,
|
159 |
+
value=1,
|
160 |
+
)
|
161 |
+
pooled_prompt_embeds_scale_2 = gr.Slider(
|
162 |
+
label="pooled prompt embeds scale 2nd image",
|
163 |
+
minimum=0,
|
164 |
+
maximum=1.5,
|
165 |
+
step=0.01,
|
166 |
+
value=1,
|
167 |
+
)
|
168 |
+
|
169 |
+
with gr.Row():
|
170 |
+
|
171 |
+
width = gr.Slider(
|
172 |
+
label="Width",
|
173 |
+
minimum=256,
|
174 |
+
maximum=MAX_IMAGE_SIZE,
|
175 |
+
step=32,
|
176 |
+
value=1024,
|
177 |
+
)
|
178 |
+
|
179 |
+
height = gr.Slider(
|
180 |
+
label="Height",
|
181 |
+
minimum=256,
|
182 |
+
maximum=MAX_IMAGE_SIZE,
|
183 |
+
step=32,
|
184 |
+
value=1024,
|
185 |
+
)
|
186 |
+
|
187 |
+
with gr.Row():
|
188 |
+
|
189 |
+
guidance_scale = gr.Slider(
|
190 |
+
label="Guidance Scale",
|
191 |
+
minimum=1,
|
192 |
+
maximum=15,
|
193 |
+
step=0.1,
|
194 |
+
value=3.5,
|
195 |
+
)
|
196 |
+
|
197 |
+
num_inference_steps = gr.Slider(
|
198 |
+
label="Number of inference steps",
|
199 |
+
minimum=1,
|
200 |
+
maximum=30,
|
201 |
+
step=1,
|
202 |
+
value=8,
|
203 |
+
)
|
204 |
+
|
205 |
+
gr.Examples(
|
206 |
+
examples=examples,
|
207 |
+
inputs=[input_image, prompt, image_2, prompt_2, reference_scale, prompt_embeds_scale_1, prompt_embeds_scale_2, pooled_prompt_embeds_scale_1, pooled_prompt_embeds_scale_2, seed, randomize_seed],
|
208 |
+
outputs=[result, seed],
|
209 |
+
fn=infer,
|
210 |
+
)
|
211 |
+
|
212 |
+
gr.on(
|
213 |
+
triggers=[run_button.click],
|
214 |
+
fn = infer,
|
215 |
+
inputs = [input_image, prompt, image_2, prompt_2, reference_scale, prompt_embeds_scale_1, prompt_embeds_scale_2, pooled_prompt_embeds_scale_1, pooled_prompt_embeds_scale_2, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
216 |
+
outputs = [result, seed]
|
217 |
+
)
|
218 |
+
|
219 |
+
demo.launch()
|
220 |
+
|
221 |
+
|