Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -1,50 +1,67 @@
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
-
|
4 |
import spaces
|
5 |
import torch
|
6 |
import random
|
7 |
from PIL import Image
|
8 |
-
|
9 |
from pipeline_flux_kontext import FluxKontextPipeline
|
10 |
from diffusers import FluxTransformer2DModel
|
11 |
from diffusers.utils import load_image
|
12 |
-
|
13 |
from huggingface_hub import hf_hub_download
|
14 |
|
15 |
-
|
16 |
-
kontext_path = hf_hub_download(repo_id="diffusers/kontext-v2", filename="dev-opt-2-a-3.safetensors")
|
17 |
-
|
18 |
MAX_SEED = np.iinfo(np.int32).max
|
19 |
|
20 |
-
|
21 |
-
pipe = FluxKontextPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", transformer=transformer, torch_dtype=torch.bfloat16).to("cuda")
|
22 |
|
23 |
@spaces.GPU
|
24 |
-
def infer(input_image, prompt, seed=42, randomize_seed=False, guidance_scale=2.5, progress=gr.Progress(track_tqdm=True)):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
if randomize_seed:
|
27 |
seed = random.randint(0, MAX_SEED)
|
28 |
|
29 |
input_image = input_image.convert("RGB")
|
30 |
-
# original_width, original_height = input_image.size
|
31 |
-
|
32 |
-
# if original_width >= original_height:
|
33 |
-
# new_width = 1024
|
34 |
-
# new_height = int(original_height * (new_width / original_width))
|
35 |
-
# new_height = round(new_height / 64) * 64
|
36 |
-
# else:
|
37 |
-
# new_height = 1024
|
38 |
-
# new_width = int(original_width * (new_height / original_height))
|
39 |
-
# new_width = round(new_width / 64) * 64
|
40 |
-
|
41 |
-
#input_image_resized = input_image.resize((new_width, new_height), Image.LANCZOS)
|
42 |
image = pipe(
|
43 |
image=input_image,
|
44 |
prompt=prompt,
|
45 |
guidance_scale=guidance_scale,
|
46 |
-
# width=new_width,
|
47 |
-
# height=new_height,
|
48 |
generator=torch.Generator().manual_seed(seed),
|
49 |
).images[0]
|
50 |
return image, seed, gr.update(visible=True)
|
@@ -60,8 +77,8 @@ with gr.Blocks(css=css) as demo:
|
|
60 |
|
61 |
with gr.Column(elem_id="col-container"):
|
62 |
gr.Markdown(f"""# FLUX.1 Kontext [dev]
|
|
|
63 |
""")
|
64 |
-
|
65 |
with gr.Row():
|
66 |
with gr.Column():
|
67 |
input_image = gr.Image(label="Upload the image for editing", type="pil")
|
@@ -75,7 +92,7 @@ with gr.Blocks(css=css) as demo:
|
|
75 |
)
|
76 |
run_button = gr.Button("Run", scale=0)
|
77 |
with gr.Accordion("Advanced Settings", open=False):
|
78 |
-
|
79 |
seed = gr.Slider(
|
80 |
label="Seed",
|
81 |
minimum=0,
|
@@ -94,16 +111,23 @@ with gr.Blocks(css=css) as demo:
|
|
94 |
value=2.5,
|
95 |
)
|
96 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
with gr.Column():
|
98 |
result = gr.Image(label="Result", show_label=False, interactive=False)
|
99 |
reuse_button = gr.Button("Reuse this image", visible=False)
|
100 |
|
101 |
|
102 |
-
|
103 |
gr.on(
|
104 |
triggers=[run_button.click, prompt.submit],
|
105 |
fn = infer,
|
106 |
-
inputs = [input_image, prompt, seed, randomize_seed, guidance_scale],
|
107 |
outputs = [result, seed, reuse_button]
|
108 |
)
|
109 |
reuse_button.click(
|
@@ -112,4 +136,4 @@ with gr.Blocks(css=css) as demo:
|
|
112 |
outputs = [input_image]
|
113 |
)
|
114 |
|
115 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
|
|
3 |
import spaces
|
4 |
import torch
|
5 |
import random
|
6 |
from PIL import Image
|
|
|
7 |
from pipeline_flux_kontext import FluxKontextPipeline
|
8 |
from diffusers import FluxTransformer2DModel
|
9 |
from diffusers.utils import load_image
|
|
|
10 |
from huggingface_hub import hf_hub_download
|
11 |
|
|
|
|
|
|
|
12 |
MAX_SEED = np.iinfo(np.int32).max
|
13 |
|
14 |
+
pipe = FluxKontextPipeline.from_pretrained("black-forest-labs/FLUX.1-Kontext-dev", revision="refs/pr/2", torch_dtype=torch.bfloat16).to("cuda")
|
|
|
15 |
|
16 |
@spaces.GPU
|
17 |
+
def infer(input_image, prompt, seed=42, randomize_seed=False, guidance_scale=2.5, steps=28, progress=gr.Progress(track_tqdm=True)):
|
18 |
+
"""
|
19 |
+
Perform image editing using the FLUX.1 Kontext pipeline.
|
20 |
+
|
21 |
+
This function takes an input image and a text prompt to generate a modified version
|
22 |
+
of the image based on the provided instructions. It uses the FLUX.1 Kontext model
|
23 |
+
for contextual image editing tasks.
|
24 |
+
|
25 |
+
Args:
|
26 |
+
input_image (PIL.Image.Image): The input image to be edited. Will be converted
|
27 |
+
to RGB format if not already in that format.
|
28 |
+
prompt (str): Text description of the desired edit to apply to the image.
|
29 |
+
Examples: "Remove glasses", "Add a hat", "Change background to beach".
|
30 |
+
seed (int, optional): Random seed for reproducible generation. Defaults to 42.
|
31 |
+
Must be between 0 and MAX_SEED (2^31 - 1).
|
32 |
+
randomize_seed (bool, optional): If True, generates a random seed instead of
|
33 |
+
using the provided seed value. Defaults to False.
|
34 |
+
guidance_scale (float, optional): Controls how closely the model follows the
|
35 |
+
prompt. Higher values mean stronger adherence to the prompt but may reduce
|
36 |
+
image quality. Range: 1.0-10.0. Defaults to 2.5.
|
37 |
+
steps (int, optional): Controls how many steps to run the diffusion model for.
|
38 |
+
Range: 1-30. Defaults to 28.
|
39 |
+
progress (gr.Progress, optional): Gradio progress tracker for monitoring
|
40 |
+
generation progress. Defaults to gr.Progress(track_tqdm=True).
|
41 |
+
|
42 |
+
Returns:
|
43 |
+
tuple: A 3-tuple containing:
|
44 |
+
- PIL.Image.Image: The generated/edited image
|
45 |
+
- int: The seed value used for generation (useful when randomize_seed=True)
|
46 |
+
- gr.update: Gradio update object to make the reuse button visible
|
47 |
|
48 |
+
Example:
|
49 |
+
>>> edited_image, used_seed, button_update = infer(
|
50 |
+
... input_image=my_image,
|
51 |
+
... prompt="Add sunglasses",
|
52 |
+
... seed=123,
|
53 |
+
... randomize_seed=False,
|
54 |
+
... guidance_scale=2.5
|
55 |
+
... )
|
56 |
+
"""
|
57 |
if randomize_seed:
|
58 |
seed = random.randint(0, MAX_SEED)
|
59 |
|
60 |
input_image = input_image.convert("RGB")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
image = pipe(
|
62 |
image=input_image,
|
63 |
prompt=prompt,
|
64 |
guidance_scale=guidance_scale,
|
|
|
|
|
65 |
generator=torch.Generator().manual_seed(seed),
|
66 |
).images[0]
|
67 |
return image, seed, gr.update(visible=True)
|
|
|
77 |
|
78 |
with gr.Column(elem_id="col-container"):
|
79 |
gr.Markdown(f"""# FLUX.1 Kontext [dev]
|
80 |
+
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)
|
81 |
""")
|
|
|
82 |
with gr.Row():
|
83 |
with gr.Column():
|
84 |
input_image = gr.Image(label="Upload the image for editing", type="pil")
|
|
|
92 |
)
|
93 |
run_button = gr.Button("Run", scale=0)
|
94 |
with gr.Accordion("Advanced Settings", open=False):
|
95 |
+
|
96 |
seed = gr.Slider(
|
97 |
label="Seed",
|
98 |
minimum=0,
|
|
|
111 |
value=2.5,
|
112 |
)
|
113 |
|
114 |
+
steps = gr.Slider(
|
115 |
+
label="Steps",
|
116 |
+
minimum=1,
|
117 |
+
maximum=30,
|
118 |
+
value=28,
|
119 |
+
step=1
|
120 |
+
)
|
121 |
+
|
122 |
with gr.Column():
|
123 |
result = gr.Image(label="Result", show_label=False, interactive=False)
|
124 |
reuse_button = gr.Button("Reuse this image", visible=False)
|
125 |
|
126 |
|
|
|
127 |
gr.on(
|
128 |
triggers=[run_button.click, prompt.submit],
|
129 |
fn = infer,
|
130 |
+
inputs = [input_image, prompt, seed, randomize_seed, guidance_scale, steps],
|
131 |
outputs = [result, seed, reuse_button]
|
132 |
)
|
133 |
reuse_button.click(
|
|
|
136 |
outputs = [input_image]
|
137 |
)
|
138 |
|
139 |
+
demo.launch(mcp_server=True)
|