File size: 2,101 Bytes
3b56cc0
97eed01
3b56cc0
97eed01
 
3b56cc0
 
 
97eed01
 
 
 
3b56cc0
 
 
 
 
 
 
 
 
 
 
 
97eed01
 
 
3b56cc0
 
 
 
 
 
 
 
 
97eed01
 
48a2fd6
3b56cc0
97eed01
 
 
3b56cc0
 
97eed01
 
3b56cc0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
import io
import gradio as gr
from huggingface_hub import InferenceClient
from PIL import Image

def edit_image(hf_token, input_image, prompt):
    if not hf_token.strip():
        return None, "Please enter your HF token."
    if input_image is None:
        return None, "Please upload an image."
    if not prompt.strip():
        return None, "Please enter an edit instruction."
    try:
        client = InferenceClient(provider="fal-ai", api_key=hf_token.strip())
        img_bytes = io.BytesIO()
        input_image.save(img_bytes, format="PNG")
        result = client.image_to_image(
            img_bytes.getvalue(),
            prompt=prompt.strip(),
            model="black-forest-labs/FLUX.1-Kontext-dev",
        )
        return result, "Done!"
    except Exception as e:
        return None, f"Error: {str(e)}"

with gr.Blocks(title="FLUX Kontext Image Editor") as demo:
    gr.Markdown("# FLUX.1 Kontext Image Editor")
    gr.Markdown("Edit images with natural language. Fast results in ~10 seconds.")
    with gr.Accordion("Setup - Enter your HF Token", open=True):
        gr.Markdown("""
        1. Get your token at [huggingface.co/settings/tokens](https://huggingface.co/settings/tokens)
        2. Enable **Inference Providers** permission
        3. Accept model license at [FLUX.1 Kontext dev](https://huggingface.co/black-forest-labs/FLUX.1-Kontext-dev)
        4. Paste token below
        """)
        hf_token = gr.Textbox(label="HF Token", placeholder="hf_...", type="password")
    with gr.Row():
        with gr.Column():
            input_img = gr.Image(type="pil", label="Upload Image")
            prompt = gr.Textbox(label="Edit Instruction", placeholder="e.g. make the sky look like a sunset", lines=2)
            run_btn = gr.Button("Edit Image", variant="primary")
        with gr.Column():
            output_img = gr.Image(label="Edited Image")
            status = gr.Textbox(label="Status", interactive=False)
    run_btn.click(fn=edit_image, inputs=[hf_token, input_img, prompt], outputs=[output_img, status])

if __name__ == "__main__":
    demo.launch()