Spaces:
Running
on
Zero
Running
on
Zero
NikhilJoson
commited on
Commit
•
6a4771c
1
Parent(s):
8d16ec9
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,107 @@
|
|
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import spaces
|
3 |
import gradio as gr
|
4 |
+
from diffusers import FluxInpaintPipeline
|
5 |
+
import random
|
6 |
+
import numpy as np
|
7 |
|
8 |
+
MARKDOWN = """
|
9 |
+
# FLUX.1 Inpainting 🎨
|
10 |
+
Shoutout to [Black Forest Labs](https://huggingface.co/black-forest-labs) team for
|
11 |
+
creating this amazing model, and a big thanks to [Gothos](https://github.com/Gothos)
|
12 |
+
for taking it to the next level by enabling inpainting with the FLUX.
|
13 |
+
"""
|
14 |
|
15 |
+
MAX_SEED = np.iinfo(np.int32).max
|
16 |
+
DEVICE = "cuda" #if torch.cuda.is_available() else "cpu"
|
17 |
+
|
18 |
+
pipe = FluxInpaintPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell", torch_dtype=torch.bfloat16).to(DEVICE)
|
19 |
+
|
20 |
+
@spaces.GPU()
|
21 |
+
def process(input_image_editor, input_text, strength, seed, randomize_seed, num_inference_steps, guidance_scale=3.5, progress=gr.Progress(track_tqdm=True)):
|
22 |
+
if not input_text:
|
23 |
+
raise gr.Error("Please enter a text prompt.")
|
24 |
+
|
25 |
+
image = input_image_editor['background']
|
26 |
+
|
27 |
+
if not image:
|
28 |
+
raise gr.Error("Please upload an image.")
|
29 |
+
|
30 |
+
width, height = image.size
|
31 |
+
|
32 |
+
if randomize_seed:
|
33 |
+
seed = random.randint(0, MAX_SEED)
|
34 |
+
|
35 |
+
generator = torch.Generator(device=DEVICE).manual_seed(seed)
|
36 |
+
|
37 |
+
result = pipe(prompt=input_text, image=image, mask_image=mask_image, width=width, height=height,
|
38 |
+
strength=strength, num_inference_steps=num_inference_steps, generator=generator,
|
39 |
+
guidance_scale=guidance_scale).images[0]
|
40 |
+
|
41 |
+
return result, mask_image, seed
|
42 |
+
|
43 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
44 |
+
gr.Markdown(MARKDOWN)
|
45 |
+
with gr.Row():
|
46 |
+
with gr.Column(scale=1):
|
47 |
+
input_image_component = gr.ImageEditor(
|
48 |
+
label='Image',
|
49 |
+
type='pil',
|
50 |
+
sources=["upload", "webcam"],
|
51 |
+
image_mode='RGB',
|
52 |
+
layers=False,
|
53 |
+
brush=gr.Brush(colors=["#FFFFFF"], color_mode="fixed"))
|
54 |
+
input_text_component = gr.Text(
|
55 |
+
label="Prompt",
|
56 |
+
show_label=False,
|
57 |
+
max_lines=1,
|
58 |
+
placeholder="Enter your prompt",
|
59 |
+
container=False,
|
60 |
+
)
|
61 |
+
with gr.Accordion("Advanced Settings", open=False):
|
62 |
+
strength_slider = gr.Slider(
|
63 |
+
minimum=0.0,
|
64 |
+
maximum=1.0,
|
65 |
+
value=0.7,
|
66 |
+
step=0.01,
|
67 |
+
label="Strength"
|
68 |
+
)
|
69 |
+
num_inference_steps = gr.Slider(
|
70 |
+
minimum=1,
|
71 |
+
maximum=100,
|
72 |
+
value=30,
|
73 |
+
step=1,
|
74 |
+
label="Number of inference steps"
|
75 |
+
)
|
76 |
+
guidance_scale = gr.Slider(
|
77 |
+
label="Guidance Scale",
|
78 |
+
minimum=1,
|
79 |
+
maximum=15,
|
80 |
+
step=0.1,
|
81 |
+
value=3.5,
|
82 |
+
)
|
83 |
+
seed_number = gr.Number(
|
84 |
+
label="Seed",
|
85 |
+
value=42,
|
86 |
+
precision=0
|
87 |
+
)
|
88 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
89 |
+
with gr.Accordion("Upload a mask", open=False):
|
90 |
+
uploaded_mask_component = gr.Image(label="Already made mask (black pixels will be preserved, white pixels will be redrawn)", sources=["upload"], type="pil")
|
91 |
+
submit_button_component = gr.Button(
|
92 |
+
value='Inpaint', variant='primary')
|
93 |
+
with gr.Column(scale=1):
|
94 |
+
output_image_component = gr.Image(
|
95 |
+
type='pil', image_mode='RGB', label='Generated Image')
|
96 |
+
output_mask_component = gr.Image(
|
97 |
+
type='pil', image_mode='RGB', label='Generated Mask')
|
98 |
+
with gr.Accordion("Debug Info", open=False):
|
99 |
+
output_seed = gr.Number(label="Used Seed")
|
100 |
+
|
101 |
+
submit_button_component.click(
|
102 |
+
fn=process,
|
103 |
+
inputs=[input_image_component, input_text_component, strength_slider, seed_number, randomize_seed, num_inference_steps, guidance_scale],
|
104 |
+
outputs=[output_image_component, output_mask_component, output_seed]
|
105 |
+
)
|
106 |
+
|
107 |
+
demo.launch(debug=False, show_error=True)
|