Spaces:
Running
on
Zero
Running
on
Zero
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 kontext_pipeline 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", filename="kontext.safetensors")
|
17 |
+
|
18 |
+
MAX_SEED = np.iinfo(np.int32).max
|
19 |
+
|
20 |
+
transformer = FluxTransformer2DModel.from_single_file(kontext_path, torch_dtype=torch.bfloat16)
|
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 |
+
|
31 |
+
original_width, original_height = input_image.size
|
32 |
+
|
33 |
+
if original_width >= original_height:
|
34 |
+
new_width = 1024
|
35 |
+
new_height = int(original_height * (new_width / original_width))
|
36 |
+
else:
|
37 |
+
new_height = 1024
|
38 |
+
new_width = int(original_width * (new_height / original_height))
|
39 |
+
|
40 |
+
input_image_resized = input_image.resize((new_width, new_height), Image.LANCZOS)
|
41 |
+
|
42 |
+
image = pipe(
|
43 |
+
image=input_image_resized, # Use the resized image
|
44 |
+
prompt=prompt,
|
45 |
+
guidance_scale=guidance_scale,
|
46 |
+
generator=torch.Generator().manual_seed(seed),
|
47 |
+
).images[0]
|
48 |
+
return image, seed
|
49 |
+
|
50 |
+
css="""
|
51 |
+
#col-container {
|
52 |
+
margin: 0 auto;
|
53 |
+
max-width: 520px;
|
54 |
+
}
|
55 |
+
"""
|
56 |
+
|
57 |
+
with gr.Blocks(css=css) as demo:
|
58 |
+
|
59 |
+
with gr.Column(elem_id="col-container"):
|
60 |
+
gr.Markdown(f"""# FLUX.1 Kontext [dev]
|
61 |
+
""")
|
62 |
+
|
63 |
+
input_image = gr.Image(label="Upload the image for editing", type="pil")
|
64 |
+
with gr.Row():
|
65 |
+
|
66 |
+
prompt = gr.Text(
|
67 |
+
label="Prompt",
|
68 |
+
show_label=False,
|
69 |
+
max_lines=1,
|
70 |
+
placeholder="Enter your prompt for editing (e.g., 'Remove glasses', 'Add a hat')",
|
71 |
+
container=False,
|
72 |
+
)
|
73 |
+
|
74 |
+
run_button = gr.Button("Run", scale=0)
|
75 |
+
|
76 |
+
result = gr.Image(label="Result", show_label=False)
|
77 |
+
|
78 |
+
with gr.Accordion("Advanced Settings", open=False):
|
79 |
+
|
80 |
+
seed = gr.Slider(
|
81 |
+
label="Seed",
|
82 |
+
minimum=0,
|
83 |
+
maximum=MAX_SEED,
|
84 |
+
step=1,
|
85 |
+
value=0,
|
86 |
+
)
|
87 |
+
|
88 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
89 |
+
|
90 |
+
guidance_scale = gr.Slider(
|
91 |
+
label="Guidance Scale",
|
92 |
+
minimum=1,
|
93 |
+
maximum=10,
|
94 |
+
step=0.1,
|
95 |
+
value=2.5,
|
96 |
+
)
|
97 |
+
|
98 |
+
gr.on(
|
99 |
+
triggers=[run_button.click, prompt.submit],
|
100 |
+
fn = infer,
|
101 |
+
inputs = [input_image, prompt, seed, randomize_seed, guidance_scale],
|
102 |
+
outputs = [result, seed]
|
103 |
+
)
|
104 |
+
|
105 |
+
demo.launch()
|