Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,45 +12,28 @@ MAX_SEED = np.iinfo(np.int32).max
|
|
| 12 |
|
| 13 |
pipe = FluxKontextPipeline.from_pretrained("black-forest-labs/FLUX.1-Kontext-dev", torch_dtype=torch.bfloat16).to("cuda")
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
@spaces.GPU
|
| 16 |
-
def infer(input_image, prompt,
|
| 17 |
"""
|
| 18 |
-
Perform image editing using the FLUX.1 Kontext pipeline.
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
of the image based on the provided instructions. It uses the FLUX.1 Kontext model
|
| 22 |
-
for contextual image editing tasks.
|
| 23 |
|
| 24 |
Args:
|
| 25 |
input_image (PIL.Image.Image): The input image to be edited. Will be converted
|
| 26 |
to RGB format if not already in that format.
|
| 27 |
prompt (str): Text description of the desired edit to apply to the image.
|
| 28 |
Examples: "Remove glasses", "Add a hat", "Change background to beach".
|
| 29 |
-
seed (int, optional): Random seed for reproducible generation. Defaults to 42.
|
| 30 |
-
Must be between 0 and MAX_SEED (2^31 - 1).
|
| 31 |
-
randomize_seed (bool, optional): If True, generates a random seed instead of
|
| 32 |
-
using the provided seed value. Defaults to False.
|
| 33 |
-
guidance_scale (float, optional): Controls how closely the model follows the
|
| 34 |
-
prompt. Higher values mean stronger adherence to the prompt but may reduce
|
| 35 |
-
image quality. Range: 1.0-10.0. Defaults to 2.5.
|
| 36 |
-
steps (int, optional): Controls how many steps to run the diffusion model for.
|
| 37 |
-
Range: 1-30. Defaults to 28.
|
| 38 |
-
progress (gr.Progress, optional): Gradio progress tracker for monitoring
|
| 39 |
-
generation progress. Defaults to gr.Progress(track_tqdm=True).
|
| 40 |
|
| 41 |
Returns:
|
| 42 |
tuple: A 2-tuple containing:
|
| 43 |
- tuple of (PIL.Image.Image, PIL.Image.Image): The original image, generated/edited image
|
| 44 |
-
- int: The seed value used for generation (useful when randomize_seed=True)
|
| 45 |
-
|
| 46 |
-
Example:
|
| 47 |
-
>>> edited_image, used_seed, button_update = infer(
|
| 48 |
-
... input_image=my_image,
|
| 49 |
-
... prompt="Add sunglasses",
|
| 50 |
-
... seed=123,
|
| 51 |
-
... randomize_seed=False,
|
| 52 |
-
... guidance_scale=2.5
|
| 53 |
-
... )
|
| 54 |
"""
|
| 55 |
if randomize_seed:
|
| 56 |
seed = random.randint(0, MAX_SEED)
|
|
@@ -75,80 +58,34 @@ def infer(input_image, prompt, seed=42, randomize_seed=False, guidance_scale=2.5
|
|
| 75 |
).images[0]
|
| 76 |
return (input_image, image), seed
|
| 77 |
|
| 78 |
-
css="""
|
| 79 |
-
#col-container {
|
| 80 |
-
margin: 0 auto;
|
| 81 |
-
max-width: 960px;
|
| 82 |
-
}
|
| 83 |
-
"""
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
with gr.Column(elem_id="col-container"):
|
| 88 |
-
gr.Markdown(f"""# FLUX.1 Kontext [dev]
|
| 89 |
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)
|
| 90 |
-
|
| 91 |
-
with gr.Row():
|
| 92 |
-
with gr.Column():
|
| 93 |
-
input_image = gr.Image(label="Upload the image for editing", type="pil")
|
| 94 |
-
with gr.Row():
|
| 95 |
-
prompt = gr.Text(
|
| 96 |
-
label="Prompt",
|
| 97 |
-
show_label=False,
|
| 98 |
-
max_lines=1,
|
| 99 |
-
placeholder="Enter your prompt for editing (e.g., 'Remove glasses', 'Add a hat')",
|
| 100 |
-
container=False,
|
| 101 |
-
)
|
| 102 |
-
run_button = gr.Button("Run", scale=0)
|
| 103 |
-
with gr.Accordion("Advanced Settings", open=False):
|
| 104 |
-
|
| 105 |
-
seed = gr.Slider(
|
| 106 |
-
label="Seed",
|
| 107 |
-
minimum=0,
|
| 108 |
-
maximum=MAX_SEED,
|
| 109 |
-
step=1,
|
| 110 |
-
value=0,
|
| 111 |
-
)
|
| 112 |
-
|
| 113 |
-
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 114 |
-
|
| 115 |
-
guidance_scale = gr.Slider(
|
| 116 |
-
label="Guidance Scale",
|
| 117 |
-
minimum=1,
|
| 118 |
-
maximum=10,
|
| 119 |
-
step=0.1,
|
| 120 |
-
value=2.5,
|
| 121 |
-
)
|
| 122 |
-
|
| 123 |
-
steps = gr.Slider(
|
| 124 |
-
label="Steps",
|
| 125 |
-
minimum=1,
|
| 126 |
-
maximum=30,
|
| 127 |
-
value=28,
|
| 128 |
-
step=1
|
| 129 |
-
)
|
| 130 |
-
|
| 131 |
-
with gr.Column():
|
| 132 |
-
result = gr.ImageSlider(label="Result", show_label=False, interactive=False)
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
examples = gr.Examples(
|
| 136 |
-
examples=[
|
| 137 |
-
["flowers.png", "turn the flowers into sunflowers"],
|
| 138 |
-
["monster.png", "make this monster ride a skateboard on the beach"],
|
| 139 |
-
["cat.png", "make this cat happy"]
|
| 140 |
-
],
|
| 141 |
-
inputs=[input_image, prompt],
|
| 142 |
-
outputs=[result, seed],
|
| 143 |
-
fn=infer,
|
| 144 |
-
cache_examples="lazy"
|
| 145 |
-
)
|
| 146 |
-
|
| 147 |
-
gr.on(
|
| 148 |
-
triggers=[run_button.click, prompt.submit],
|
| 149 |
-
fn = infer,
|
| 150 |
-
inputs = [input_image, prompt, seed, randomize_seed, guidance_scale, steps],
|
| 151 |
-
outputs = [result, seed]
|
| 152 |
-
)
|
| 153 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
demo.launch(mcp_server=True)
|
|
|
|
| 12 |
|
| 13 |
pipe = FluxKontextPipeline.from_pretrained("black-forest-labs/FLUX.1-Kontext-dev", torch_dtype=torch.bfloat16).to("cuda")
|
| 14 |
|
| 15 |
+
seed=42
|
| 16 |
+
randomize_seed=False
|
| 17 |
+
guidance_scale=2.5
|
| 18 |
+
steps=28,
|
| 19 |
+
|
| 20 |
@spaces.GPU
|
| 21 |
+
def infer(input_image, prompt, progress=gr.Progress(track_tqdm=True)):
|
| 22 |
"""
|
| 23 |
+
Perform image editing using the FLUX.1 Kontext pipeline. This function takes an input image and a text
|
| 24 |
+
prompt to generate a modified version of the image based on the provided instructions. It uses the FLUX.1
|
| 25 |
+
Kontext model for contextual image editing tasks.
|
|
|
|
|
|
|
| 26 |
|
| 27 |
Args:
|
| 28 |
input_image (PIL.Image.Image): The input image to be edited. Will be converted
|
| 29 |
to RGB format if not already in that format.
|
| 30 |
prompt (str): Text description of the desired edit to apply to the image.
|
| 31 |
Examples: "Remove glasses", "Add a hat", "Change background to beach".
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
Returns:
|
| 34 |
tuple: A 2-tuple containing:
|
| 35 |
- tuple of (PIL.Image.Image, PIL.Image.Image): The original image, generated/edited image
|
| 36 |
+
- int: The seed value used for generation (useful when randomize_seed=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
"""
|
| 38 |
if randomize_seed:
|
| 39 |
seed = random.randint(0, MAX_SEED)
|
|
|
|
| 58 |
).images[0]
|
| 59 |
return (input_image, image), seed
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
+
title = f"""# FLUX.1 Kontext [dev]
|
|
|
|
|
|
|
|
|
|
| 63 |
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)
|
| 64 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
+
demo = gr.Interface(
|
| 67 |
+
fn=infer,
|
| 68 |
+
inputs=[
|
| 69 |
+
gr.Image(label="Upload the image for editing", type="pil"),
|
| 70 |
+
gr.Text(
|
| 71 |
+
label="Prompt",
|
| 72 |
+
show_label=False,
|
| 73 |
+
max_lines=1,
|
| 74 |
+
placeholder="Enter your prompt for editing (e.g., 'Remove glasses', 'Add a hat')",
|
| 75 |
+
container=False,
|
| 76 |
+
)
|
| 77 |
+
],
|
| 78 |
+
outputs=[
|
| 79 |
+
gr.ImageSlider(label="Result", show_label=False)
|
| 80 |
+
],
|
| 81 |
+
examples=[
|
| 82 |
+
["flowers.png", "turn the flowers into sunflowers"],
|
| 83 |
+
["monster.png", "make this monster ride a skateboard on the beach"],
|
| 84 |
+
["cat.png", "make this cat happy"]
|
| 85 |
+
],
|
| 86 |
+
cache_examples=True,
|
| 87 |
+
cache_mode="lazy",
|
| 88 |
+
title=title
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
demo.launch(mcp_server=True)
|