Spaces:
Runtime error
Runtime error
Upload 8 files
Browse files- README.md +13 -11
- app.py +299 -464
- optimization.py +28 -45
- requirements.txt +5 -8
README.md
CHANGED
|
@@ -1,12 +1,14 @@
|
|
| 1 |
-
---
|
| 2 |
-
title: FLUX.
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo: gray
|
| 6 |
-
sdk: gradio
|
| 7 |
-
sdk_version: 5.29.1
|
| 8 |
-
app_file: app.py
|
| 9 |
-
pinned: true
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
| 12 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: FLUX.1 Kontext
|
| 3 |
+
emoji: ⚡
|
| 4 |
+
colorFrom: green
|
| 5 |
+
colorTo: gray
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 5.29.1
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: true
|
| 10 |
+
license: mit
|
| 11 |
+
short_description: 'Kontext image editing on FLUX[dev] '
|
| 12 |
+
---
|
| 13 |
+
|
| 14 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
CHANGED
|
@@ -1,464 +1,299 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
import
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
import
|
| 16 |
-
|
| 17 |
-
import
|
| 18 |
-
import
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
"
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
)
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
]
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
.
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
with gr.Row():
|
| 301 |
-
prompt = gr.Text(
|
| 302 |
-
label="Prompt",
|
| 303 |
-
show_label=False,
|
| 304 |
-
max_lines=2,
|
| 305 |
-
placeholder="Enter your prompt",
|
| 306 |
-
container=False,
|
| 307 |
-
scale=3
|
| 308 |
-
)
|
| 309 |
-
|
| 310 |
-
run_button = gr.Button("Run", scale=1)
|
| 311 |
-
|
| 312 |
-
with gr.Accordion("Input image(s) (optional)", open=False):
|
| 313 |
-
input_images = gr.Gallery(
|
| 314 |
-
label="Input Image(s)",
|
| 315 |
-
type="pil",
|
| 316 |
-
columns=3,
|
| 317 |
-
rows=1,
|
| 318 |
-
)
|
| 319 |
-
input_image = gr.Image(label="Upload the image for editing", type="pil")
|
| 320 |
-
|
| 321 |
-
mode_choice = gr.Radio(
|
| 322 |
-
label="Mode",
|
| 323 |
-
choices=["Distilled (4 steps)", "Base (50 steps)"],
|
| 324 |
-
value="Distilled (4 steps)",
|
| 325 |
-
)
|
| 326 |
-
|
| 327 |
-
with gr.Accordion("Advanced Settings", open=False):
|
| 328 |
-
|
| 329 |
-
prompt_upsampling = gr.Checkbox(
|
| 330 |
-
label="Prompt Upsampling",
|
| 331 |
-
value=False,
|
| 332 |
-
info="Automatically enhance the prompt using a VLM"
|
| 333 |
-
)
|
| 334 |
-
|
| 335 |
-
seed = gr.Slider(
|
| 336 |
-
label="Seed",
|
| 337 |
-
minimum=0,
|
| 338 |
-
maximum=MAX_SEED,
|
| 339 |
-
step=1,
|
| 340 |
-
value=0,
|
| 341 |
-
)
|
| 342 |
-
|
| 343 |
-
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 344 |
-
|
| 345 |
-
with gr.Row():
|
| 346 |
-
|
| 347 |
-
width = gr.Slider(
|
| 348 |
-
label="Width",
|
| 349 |
-
minimum=256,
|
| 350 |
-
maximum=MAX_IMAGE_SIZE,
|
| 351 |
-
step=8,
|
| 352 |
-
value=1024,
|
| 353 |
-
)
|
| 354 |
-
|
| 355 |
-
height = gr.Slider(
|
| 356 |
-
label="Height",
|
| 357 |
-
minimum=256,
|
| 358 |
-
maximum=MAX_IMAGE_SIZE,
|
| 359 |
-
step=8,
|
| 360 |
-
value=1024,
|
| 361 |
-
)
|
| 362 |
-
|
| 363 |
-
with gr.Row():
|
| 364 |
-
|
| 365 |
-
num_inference_steps = gr.Slider(
|
| 366 |
-
label="Number of inference steps",
|
| 367 |
-
minimum=1,
|
| 368 |
-
maximum=100,
|
| 369 |
-
step=1,
|
| 370 |
-
value=4,
|
| 371 |
-
)
|
| 372 |
-
|
| 373 |
-
guidance_scale = gr.Slider(
|
| 374 |
-
label="Guidance scale",
|
| 375 |
-
minimum=0.0,
|
| 376 |
-
maximum=10.0,
|
| 377 |
-
step=0.1,
|
| 378 |
-
value=1.0,
|
| 379 |
-
)
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
with gr.Column():
|
| 383 |
-
result = gr.Image(label="Result", show_label=False)
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
gr.Examples(
|
| 387 |
-
examples=examples,
|
| 388 |
-
fn=infer,
|
| 389 |
-
inputs=[prompt],
|
| 390 |
-
outputs=[result, seed],
|
| 391 |
-
cache_examples=True,
|
| 392 |
-
cache_mode="lazy"
|
| 393 |
-
)
|
| 394 |
-
|
| 395 |
-
gr.Examples(
|
| 396 |
-
examples=examples_images,
|
| 397 |
-
fn=infer,
|
| 398 |
-
inputs=[prompt, input_images],
|
| 399 |
-
outputs=[result, seed],
|
| 400 |
-
cache_examples=True,
|
| 401 |
-
cache_mode="lazy"
|
| 402 |
-
)
|
| 403 |
-
|
| 404 |
-
# Auto-update dimensions when images are uploaded
|
| 405 |
-
input_images.upload(
|
| 406 |
-
fn=update_dimensions_from_image,
|
| 407 |
-
inputs=[input_images],
|
| 408 |
-
outputs=[width, height]
|
| 409 |
-
)
|
| 410 |
-
|
| 411 |
-
# Auto-update steps when mode changes
|
| 412 |
-
mode_choice.change(
|
| 413 |
-
fn=update_steps_from_mode,
|
| 414 |
-
inputs=[mode_choice],
|
| 415 |
-
outputs=[num_inference_steps, guidance_scale]
|
| 416 |
-
)
|
| 417 |
-
|
| 418 |
-
gr.on(
|
| 419 |
-
triggers=[run_button.click, prompt.submit],
|
| 420 |
-
fn=infer,
|
| 421 |
-
inputs=[prompt, input_images, mode_choice, seed, randomize_seed, width, height, num_inference_steps, guidance_scale, prompt_upsampling],
|
| 422 |
-
outputs=[result, seed]
|
| 423 |
-
)
|
| 424 |
-
|
| 425 |
-
with gr.Row(visible=False):
|
| 426 |
-
download_button = gr.DownloadButton(elem_id="download_btn", interactive = True)
|
| 427 |
-
info_debug = gr.HTML(value = "")
|
| 428 |
-
prompt_debug = gr.Text(
|
| 429 |
-
max_lines=2,
|
| 430 |
-
container=False,
|
| 431 |
-
scale=3
|
| 432 |
-
)
|
| 433 |
-
input_images_debug = gr.Gallery(
|
| 434 |
-
label="Input Image(s)",
|
| 435 |
-
type="pil",
|
| 436 |
-
columns=3,
|
| 437 |
-
rows=1,
|
| 438 |
-
)
|
| 439 |
-
gr.Examples(
|
| 440 |
-
examples=[
|
| 441 |
-
["A dog", "woman1.webp"]
|
| 442 |
-
],
|
| 443 |
-
fn=infer_example,
|
| 444 |
-
inputs=[prompt, input_image],
|
| 445 |
-
outputs=[seed, download_button, info_debug],
|
| 446 |
-
run_on_click=True,
|
| 447 |
-
cache_examples=True,
|
| 448 |
-
cache_mode='lazy'
|
| 449 |
-
)
|
| 450 |
-
number_debug=gr.Slider(minimum=1, maximum=50, step=1, value=50)
|
| 451 |
-
|
| 452 |
-
def handle_field_debug_change(prompt_debug_data, input_images_debug_data, number_debug_data):
|
| 453 |
-
prompt_debug_value[0] = prompt_debug_data
|
| 454 |
-
input_images_debug_value[0] = input_images_debug_data
|
| 455 |
-
number_debug_value[0] = number_debug_data
|
| 456 |
-
return []
|
| 457 |
-
|
| 458 |
-
inputs_debug=[prompt_debug, input_images_debug, number_debug]
|
| 459 |
-
|
| 460 |
-
prompt_debug.change(fn=handle_field_debug_change, inputs=inputs_debug, outputs=[])
|
| 461 |
-
input_images_debug.change(fn=handle_field_debug_change, inputs=inputs_debug, outputs=[])
|
| 462 |
-
number_debug.change(fn=handle_field_debug_change, inputs=inputs_debug, outputs=[])
|
| 463 |
-
|
| 464 |
-
demo.launch()
|
|
|
|
| 1 |
+
# PyTorch 2.8 (temporary hack)
|
| 2 |
+
import os
|
| 3 |
+
os.system('pip install --upgrade --pre --extra-index-url https://download.pytorch.org/whl/nightly/cu126 "torch<2.9" spaces')
|
| 4 |
+
|
| 5 |
+
# Actual demo code
|
| 6 |
+
try:
|
| 7 |
+
import spaces
|
| 8 |
+
except:
|
| 9 |
+
class spaces():
|
| 10 |
+
def GPU(*args, **kwargs):
|
| 11 |
+
def decorator(function):
|
| 12 |
+
return lambda *dummy_args, **dummy_kwargs: function(*dummy_args, **dummy_kwargs)
|
| 13 |
+
return decorator
|
| 14 |
+
|
| 15 |
+
import gradio as gr
|
| 16 |
+
import numpy as np
|
| 17 |
+
import torch
|
| 18 |
+
import random
|
| 19 |
+
import os
|
| 20 |
+
from datetime import datetime
|
| 21 |
+
|
| 22 |
+
from PIL import Image
|
| 23 |
+
import tempfile
|
| 24 |
+
import zipfile
|
| 25 |
+
import shutil
|
| 26 |
+
from pathlib import Path
|
| 27 |
+
|
| 28 |
+
from diffusers import FluxKontextPipeline
|
| 29 |
+
from diffusers.utils import load_image
|
| 30 |
+
|
| 31 |
+
from optimization import optimize_pipeline_
|
| 32 |
+
|
| 33 |
+
MAX_SEED = np.iinfo(np.int32).max
|
| 34 |
+
|
| 35 |
+
pipe = FluxKontextPipeline.from_pretrained("yuvraj108c/FLUX.1-Kontext-dev", torch_dtype=torch.bfloat16).to("cuda")
|
| 36 |
+
optimize_pipeline_(pipe, image=Image.new("RGB", (512, 512)), prompt='prompt')
|
| 37 |
+
|
| 38 |
+
input_image_debug_value = [None]
|
| 39 |
+
prompt_debug_value = [None]
|
| 40 |
+
number_debug_value = [None]
|
| 41 |
+
def save_on_path(img: Image, filename: str, format_: str = None) -> Path:
|
| 42 |
+
"""
|
| 43 |
+
Save `img` in a unique temporary folder under the given `filename`
|
| 44 |
+
and return its absolute path.
|
| 45 |
+
"""
|
| 46 |
+
# 1) unique temporary folder
|
| 47 |
+
tmp_dir = Path(tempfile.mkdtemp(prefix="pil_tmp_"))
|
| 48 |
+
|
| 49 |
+
# 2) full path of the future file
|
| 50 |
+
file_path = tmp_dir / filename
|
| 51 |
+
|
| 52 |
+
# 3) save
|
| 53 |
+
img.save(file_path, format=format_ or img.format)
|
| 54 |
+
|
| 55 |
+
return file_path
|
| 56 |
+
|
| 57 |
+
@spaces.GPU(duration=40)
|
| 58 |
+
def infer(
|
| 59 |
+
input_image,
|
| 60 |
+
prompt,
|
| 61 |
+
seed = 42,
|
| 62 |
+
randomize_seed = False,
|
| 63 |
+
guidance_scale = 2.5,
|
| 64 |
+
steps = 28,
|
| 65 |
+
width = -1,
|
| 66 |
+
height = -1,
|
| 67 |
+
progress=gr.Progress(track_tqdm=True)
|
| 68 |
+
):
|
| 69 |
+
"""
|
| 70 |
+
Perform image editing using the FLUX.1 Kontext pipeline.
|
| 71 |
+
|
| 72 |
+
This function takes an input image and a text prompt to generate a modified version
|
| 73 |
+
of the image based on the provided instructions. It uses the FLUX.1 Kontext model
|
| 74 |
+
for contextual image editing tasks.
|
| 75 |
+
|
| 76 |
+
Args:
|
| 77 |
+
input_image (PIL.Image.Image): The input image to be edited. Will be converted
|
| 78 |
+
to RGB format if not already in that format.
|
| 79 |
+
prompt (str): Text description of the desired edit to apply to the image.
|
| 80 |
+
Examples: "Remove glasses", "Add a hat", "Change background to beach".
|
| 81 |
+
seed (int, optional): Random seed for reproducible generation. Defaults to 42.
|
| 82 |
+
Must be between 0 and MAX_SEED (2^31 - 1).
|
| 83 |
+
randomize_seed (bool, optional): If True, generates a random seed instead of
|
| 84 |
+
using the provided seed value. Defaults to False.
|
| 85 |
+
guidance_scale (float, optional): Controls how closely the model follows the
|
| 86 |
+
prompt. Higher values mean stronger adherence to the prompt but may reduce
|
| 87 |
+
image quality. Range: 1.0-10.0. Defaults to 2.5.
|
| 88 |
+
steps (int, optional): Controls how many steps to run the diffusion model for.
|
| 89 |
+
Range: 1-30. Defaults to 28.
|
| 90 |
+
progress (gr.Progress, optional): Gradio progress tracker for monitoring
|
| 91 |
+
generation progress. Defaults to gr.Progress(track_tqdm=True).
|
| 92 |
+
|
| 93 |
+
Returns:
|
| 94 |
+
tuple: A 3-tuple containing:
|
| 95 |
+
- PIL.Image.Image: The generated/edited image
|
| 96 |
+
- int: The seed value used for generation (useful when randomize_seed=True)
|
| 97 |
+
- gr.update: Gradio update object to make the reuse button visible
|
| 98 |
+
|
| 99 |
+
Example:
|
| 100 |
+
>>> edited_image, used_seed, button_update = infer(
|
| 101 |
+
... input_image=my_image,
|
| 102 |
+
... prompt="Add sunglasses",
|
| 103 |
+
... seed=123,
|
| 104 |
+
... randomize_seed=False,
|
| 105 |
+
... guidance_scale=2.5
|
| 106 |
+
... )
|
| 107 |
+
"""
|
| 108 |
+
if randomize_seed:
|
| 109 |
+
seed = random.randint(0, MAX_SEED)
|
| 110 |
+
|
| 111 |
+
if input_image:
|
| 112 |
+
input_image = input_image.convert("RGB")
|
| 113 |
+
image = pipe(
|
| 114 |
+
image=input_image,
|
| 115 |
+
prompt=prompt,
|
| 116 |
+
guidance_scale=guidance_scale,
|
| 117 |
+
width = input_image.size[0] if width == -1 else width,
|
| 118 |
+
height = input_image.size[1] if height == -1 else height,
|
| 119 |
+
num_inference_steps=steps,
|
| 120 |
+
generator=torch.Generator().manual_seed(seed),
|
| 121 |
+
).images[0]
|
| 122 |
+
else:
|
| 123 |
+
image = pipe(
|
| 124 |
+
prompt=prompt,
|
| 125 |
+
guidance_scale=guidance_scale,
|
| 126 |
+
num_inference_steps=steps,
|
| 127 |
+
generator=torch.Generator().manual_seed(seed),
|
| 128 |
+
).images[0]
|
| 129 |
+
|
| 130 |
+
image_filename = datetime.now().strftime("%Y-%m-%d_%H-%M-%S.%f") + '.webp'
|
| 131 |
+
path = save_on_path(image, image_filename, format_="WEBP")
|
| 132 |
+
return path, gr.update(value=path, visible=True), seed, gr.update(visible=True)
|
| 133 |
+
|
| 134 |
+
def infer_example(input_image, prompt):
|
| 135 |
+
number=1
|
| 136 |
+
if input_image_debug_value[0] is not None or prompt_debug_value[0] is not None or number_debug_value[0] is not None:
|
| 137 |
+
input_image=input_image_debug_value[0]
|
| 138 |
+
prompt=prompt_debug_value[0]
|
| 139 |
+
number=number_debug_value[0]
|
| 140 |
+
#input_image_debug_value[0]=prompt_debug_value[0]=prompt_debug_value[0]=None
|
| 141 |
+
gallery = []
|
| 142 |
+
try:
|
| 143 |
+
for i in range(number):
|
| 144 |
+
print("Generating #" + str(i + 1) + " image...")
|
| 145 |
+
seed = random.randint(0, MAX_SEED)
|
| 146 |
+
image, download_button, seed, _ = infer(input_image, prompt, seed, True)
|
| 147 |
+
gallery.append(image)
|
| 148 |
+
except:
|
| 149 |
+
print("Error")
|
| 150 |
+
zip_path = export_images_to_zip(gallery)
|
| 151 |
+
return gallery, seed, zip_path
|
| 152 |
+
|
| 153 |
+
def export_images_to_zip(gallery) -> str:
|
| 154 |
+
"""
|
| 155 |
+
Bundle compiled_transformer_1 and compiled_transformer_2 into a zip file and return the file path.
|
| 156 |
+
"""
|
| 157 |
+
|
| 158 |
+
tmp_zip = tempfile.NamedTemporaryFile(suffix=".zip", delete=False)
|
| 159 |
+
tmp_zip.close()
|
| 160 |
+
|
| 161 |
+
with zipfile.ZipFile(tmp_zip.name, "w", compression=zipfile.ZIP_DEFLATED) as zf:
|
| 162 |
+
for i in range(len(gallery)):
|
| 163 |
+
image_path = gallery[i]
|
| 164 |
+
zf.write(image_path, arcname=os.path.basename(image_path))
|
| 165 |
+
|
| 166 |
+
print(str(len(gallery)) + " images zipped")
|
| 167 |
+
return tmp_zip.name
|
| 168 |
+
|
| 169 |
+
css="""
|
| 170 |
+
#col-container {
|
| 171 |
+
margin: 0 auto;
|
| 172 |
+
max-width: 960px;
|
| 173 |
+
}
|
| 174 |
+
"""
|
| 175 |
+
|
| 176 |
+
with gr.Blocks(css=css) as demo:
|
| 177 |
+
|
| 178 |
+
with gr.Column(elem_id="col-container"):
|
| 179 |
+
gr.Markdown(f"""# FLUX.1 Kontext [dev]
|
| 180 |
+
Image editing and manipulation model guidance-distilled from FLUX.1 Kontext [pro], [[blog]](https://bfl.ai/announcements/flux-1-kontext-dev) [[model]](https://huggingface.co/black-forest-labs/FLUX.1-Kontext-dev)
|
| 181 |
+
""")
|
| 182 |
+
with gr.Row():
|
| 183 |
+
with gr.Column():
|
| 184 |
+
input_image = gr.Image(label="Upload the image for editing", type="pil")
|
| 185 |
+
with gr.Row():
|
| 186 |
+
prompt = gr.Text(
|
| 187 |
+
label="Prompt",
|
| 188 |
+
show_label=False,
|
| 189 |
+
max_lines=1,
|
| 190 |
+
placeholder="Enter your prompt for editing (e.g., 'Remove glasses', 'Add a hat')",
|
| 191 |
+
container=False,
|
| 192 |
+
)
|
| 193 |
+
run_button = gr.Button(value="🚀 Edit", variant = "primary", scale=0)
|
| 194 |
+
with gr.Accordion("Advanced Settings", open=False):
|
| 195 |
+
|
| 196 |
+
seed = gr.Slider(
|
| 197 |
+
label="Seed",
|
| 198 |
+
minimum=0,
|
| 199 |
+
maximum=MAX_SEED,
|
| 200 |
+
step=1,
|
| 201 |
+
value=0,
|
| 202 |
+
)
|
| 203 |
+
|
| 204 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 205 |
+
|
| 206 |
+
guidance_scale = gr.Slider(
|
| 207 |
+
label="Guidance Scale",
|
| 208 |
+
minimum=1,
|
| 209 |
+
maximum=10,
|
| 210 |
+
step=0.1,
|
| 211 |
+
value=2.5,
|
| 212 |
+
)
|
| 213 |
+
|
| 214 |
+
steps = gr.Slider(
|
| 215 |
+
label="Steps",
|
| 216 |
+
minimum=1,
|
| 217 |
+
maximum=30,
|
| 218 |
+
value=30,
|
| 219 |
+
step=1
|
| 220 |
+
)
|
| 221 |
+
|
| 222 |
+
width = gr.Slider(
|
| 223 |
+
label="Output width",
|
| 224 |
+
info="-1 = original width",
|
| 225 |
+
minimum=-1,
|
| 226 |
+
maximum=1024,
|
| 227 |
+
value=-1,
|
| 228 |
+
step=1
|
| 229 |
+
)
|
| 230 |
+
|
| 231 |
+
height = gr.Slider(
|
| 232 |
+
label="Output height",
|
| 233 |
+
info="-1 = original height",
|
| 234 |
+
minimum=-1,
|
| 235 |
+
maximum=1024,
|
| 236 |
+
value=-1,
|
| 237 |
+
step=1
|
| 238 |
+
)
|
| 239 |
+
|
| 240 |
+
with gr.Column():
|
| 241 |
+
result = gr.Image(label="Result", show_label=False, interactive=False)
|
| 242 |
+
download_button = gr.DownloadButton(elem_id="download_btn", visible=False)
|
| 243 |
+
reuse_button = gr.Button("Reuse this image", visible=False)
|
| 244 |
+
|
| 245 |
+
with gr.Row(visible=False):
|
| 246 |
+
download_button = gr.DownloadButton(elem_id="download_btn", interactive = True)
|
| 247 |
+
result_gallery = gr.Gallery(interactive = False, elem_id = "gallery1")
|
| 248 |
+
gr.Examples(
|
| 249 |
+
examples=[
|
| 250 |
+
["monster.png", "Make this monster ride a skateboard on the beach"]
|
| 251 |
+
],
|
| 252 |
+
inputs=[input_image, prompt],
|
| 253 |
+
outputs=[result_gallery, seed, download_button],
|
| 254 |
+
fn=infer_example,
|
| 255 |
+
run_on_click=True,
|
| 256 |
+
cache_examples=True,
|
| 257 |
+
cache_mode='lazy'
|
| 258 |
+
)
|
| 259 |
+
prompt_debug=gr.Textbox()
|
| 260 |
+
input_image_debug=gr.Image(type="pil")
|
| 261 |
+
number_debug=gr.Slider(minimum=1, maximum=50, step=1, value=50)
|
| 262 |
+
|
| 263 |
+
gr.Examples(
|
| 264 |
+
label = "Examples from demo",
|
| 265 |
+
examples=[
|
| 266 |
+
["flowers.png", "turn the flowers into sunflowers"],
|
| 267 |
+
["monster.png", "make this monster ride a skateboard on the beach"],
|
| 268 |
+
["cat.png", "make this cat happy"]
|
| 269 |
+
],
|
| 270 |
+
inputs=[input_image, prompt],
|
| 271 |
+
outputs=[result, download_button, seed],
|
| 272 |
+
fn=infer
|
| 273 |
+
)
|
| 274 |
+
|
| 275 |
+
def handle_field_debug_change(input_image_debug_data, prompt_debug_data, number_debug_data):
|
| 276 |
+
prompt_debug_value[0] = prompt_debug_data
|
| 277 |
+
input_image_debug_value[0] = input_image_debug_data
|
| 278 |
+
number_debug_value[0] = number_debug_data
|
| 279 |
+
return []
|
| 280 |
+
|
| 281 |
+
inputs_debug=[input_image_debug, prompt_debug, number_debug]
|
| 282 |
+
|
| 283 |
+
input_image_debug.upload(fn=handle_field_debug_change, inputs=inputs_debug, outputs=[])
|
| 284 |
+
prompt_debug.change(fn=handle_field_debug_change, inputs=inputs_debug, outputs=[])
|
| 285 |
+
number_debug.change(fn=handle_field_debug_change, inputs=inputs_debug, outputs=[])
|
| 286 |
+
|
| 287 |
+
gr.on(
|
| 288 |
+
triggers=[run_button.click, prompt.submit],
|
| 289 |
+
fn = infer,
|
| 290 |
+
inputs = [input_image, prompt, seed, randomize_seed, guidance_scale, steps, width, height],
|
| 291 |
+
outputs = [result, download_button, seed, reuse_button]
|
| 292 |
+
)
|
| 293 |
+
reuse_button.click(
|
| 294 |
+
fn = lambda image: image,
|
| 295 |
+
inputs = [result],
|
| 296 |
+
outputs = [input_image]
|
| 297 |
+
)
|
| 298 |
+
|
| 299 |
+
demo.launch(mcp_server=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
optimization.py
CHANGED
|
@@ -4,35 +4,23 @@
|
|
| 4 |
from typing import Any
|
| 5 |
from typing import Callable
|
| 6 |
from typing import ParamSpec
|
|
|
|
| 7 |
import spaces
|
| 8 |
import torch
|
| 9 |
-
from
|
| 10 |
-
|
| 11 |
-
from
|
|
|
|
|
|
|
| 12 |
|
| 13 |
P = ParamSpec('P')
|
| 14 |
|
| 15 |
-
|
|
|
|
| 16 |
|
| 17 |
TRANSFORMER_DYNAMIC_SHAPES = {
|
| 18 |
-
'
|
| 19 |
-
|
| 20 |
-
1: TRANSFORMER_IMAGE_DIM,
|
| 21 |
-
},
|
| 22 |
-
'image_rotary_emb': (
|
| 23 |
-
{0: TRANSFORMER_IMAGE_DIM + 512},
|
| 24 |
-
{0: TRANSFORMER_IMAGE_DIM + 512},
|
| 25 |
-
),
|
| 26 |
-
},
|
| 27 |
-
'single': {
|
| 28 |
-
'hidden_states': {
|
| 29 |
-
1: TRANSFORMER_IMAGE_DIM + 512,
|
| 30 |
-
},
|
| 31 |
-
'image_rotary_emb': (
|
| 32 |
-
{0: TRANSFORMER_IMAGE_DIM + 512},
|
| 33 |
-
{0: TRANSFORMER_IMAGE_DIM + 512},
|
| 34 |
-
),
|
| 35 |
-
},
|
| 36 |
}
|
| 37 |
|
| 38 |
INDUCTOR_CONFIGS = {
|
|
@@ -44,34 +32,29 @@ INDUCTOR_CONFIGS = {
|
|
| 44 |
'triton.cudagraphs': True,
|
| 45 |
}
|
| 46 |
|
|
|
|
| 47 |
def optimize_pipeline_(pipeline: Callable[P, Any], *args: P.args, **kwargs: P.kwargs):
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
'single': pipeline.transformer.single_transformer_blocks,
|
| 52 |
-
}
|
| 53 |
|
| 54 |
-
|
| 55 |
-
def compile_block(blocks_kind: str):
|
| 56 |
-
block = blocks[blocks_kind][0]
|
| 57 |
-
with spaces.aoti_capture(block) as call:
|
| 58 |
pipeline(*args, **kwargs)
|
| 59 |
|
| 60 |
-
dynamic_shapes =
|
| 61 |
-
dynamic_shapes |= TRANSFORMER_DYNAMIC_SHAPES
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
)
|
| 70 |
|
| 71 |
-
return
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
weights = ZeroGPUWeights(block.state_dict())
|
| 77 |
-
block.forward = ZeroGPUCompiledModel(archive_file, weights)
|
|
|
|
| 4 |
from typing import Any
|
| 5 |
from typing import Callable
|
| 6 |
from typing import ParamSpec
|
| 7 |
+
|
| 8 |
import spaces
|
| 9 |
import torch
|
| 10 |
+
from torch.utils._pytree import tree_map_only
|
| 11 |
+
|
| 12 |
+
from optimization_utils import capture_component_call
|
| 13 |
+
from optimization_utils import aoti_compile
|
| 14 |
+
|
| 15 |
|
| 16 |
P = ParamSpec('P')
|
| 17 |
|
| 18 |
+
|
| 19 |
+
TRANSFORMER_HIDDEN_DIM = torch.export.Dim('hidden', min=4096, max=8212)
|
| 20 |
|
| 21 |
TRANSFORMER_DYNAMIC_SHAPES = {
|
| 22 |
+
'hidden_states': {1: TRANSFORMER_HIDDEN_DIM},
|
| 23 |
+
'img_ids': {0: TRANSFORMER_HIDDEN_DIM},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
}
|
| 25 |
|
| 26 |
INDUCTOR_CONFIGS = {
|
|
|
|
| 32 |
'triton.cudagraphs': True,
|
| 33 |
}
|
| 34 |
|
| 35 |
+
|
| 36 |
def optimize_pipeline_(pipeline: Callable[P, Any], *args: P.args, **kwargs: P.kwargs):
|
| 37 |
|
| 38 |
+
@spaces.GPU(duration=1500)
|
| 39 |
+
def compile_transformer():
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
with capture_component_call(pipeline, 'transformer') as call:
|
|
|
|
|
|
|
|
|
|
| 42 |
pipeline(*args, **kwargs)
|
| 43 |
|
| 44 |
+
dynamic_shapes = tree_map_only((torch.Tensor, bool), lambda t: None, call.kwargs)
|
| 45 |
+
dynamic_shapes |= TRANSFORMER_DYNAMIC_SHAPES
|
| 46 |
+
|
| 47 |
+
pipeline.transformer.fuse_qkv_projections()
|
| 48 |
|
| 49 |
+
exported = torch.export.export(
|
| 50 |
+
mod=pipeline.transformer,
|
| 51 |
+
args=call.args,
|
| 52 |
+
kwargs=call.kwargs,
|
| 53 |
+
dynamic_shapes=dynamic_shapes,
|
| 54 |
+
)
|
|
|
|
| 55 |
|
| 56 |
+
return aoti_compile(exported, INDUCTOR_CONFIGS)
|
| 57 |
|
| 58 |
+
transformer_config = pipeline.transformer.config
|
| 59 |
+
pipeline.transformer = compile_transformer()
|
| 60 |
+
pipeline.transformer.config = transformer_config # pyright: ignore[reportAttributeAccessIssue]
|
|
|
|
|
|
requirements.txt
CHANGED
|
@@ -1,8 +1,5 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
accelerate
|
| 4 |
-
safetensors
|
| 5 |
-
|
| 6 |
-
torchao
|
| 7 |
-
kernels
|
| 8 |
-
spaces==0.43.0
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
git+https://github.com/huggingface/diffusers.git
|
| 3 |
+
accelerate
|
| 4 |
+
safetensors
|
| 5 |
+
sentencepiece
|
|
|
|
|
|
|
|
|