Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,83 +1,71 @@
|
|
| 1 |
-
from
|
| 2 |
|
| 3 |
-
import
|
| 4 |
import random
|
| 5 |
-
import
|
|
|
|
| 6 |
import gradio as gr
|
|
|
|
|
|
|
| 7 |
import spaces
|
| 8 |
import torch
|
| 9 |
-
from PIL import Image
|
| 10 |
from diffusers import FluxInpaintPipeline
|
| 11 |
-
|
| 12 |
-
torch.cuda.empty_cache()
|
| 13 |
|
| 14 |
MARKDOWN = """
|
| 15 |
# FLUX Inpainting
|
| 16 |
-
Model used FLUX.1-schnell.
|
| 17 |
"""
|
| 18 |
|
| 19 |
MAX_SEED = np.iinfo(np.int32).max
|
| 20 |
-
IMAGE_SIZE =
|
| 21 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
def remove_background(image: Image.Image, threshold: int = 50) -> Image.Image:
|
| 25 |
-
image = image.convert("RGBA")
|
| 26 |
-
data = image.getdata()
|
| 27 |
-
new_data = []
|
| 28 |
-
for item in data:
|
| 29 |
-
avg = sum(item[:3]) / 3
|
| 30 |
-
if avg < threshold:
|
| 31 |
-
new_data.append((0, 0, 0, 0))
|
| 32 |
-
else:
|
| 33 |
-
new_data.append(item)
|
| 34 |
-
|
| 35 |
-
image.putdata(new_data)
|
| 36 |
-
return image
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
# EXAMPLES = [
|
| 40 |
-
# [
|
| 41 |
-
# {
|
| 42 |
-
# "background": Image.open(requests.get("https://media.roboflow.com/spaces/doge-2-image.png", stream=True).raw),
|
| 43 |
-
# "layers": [remove_background(Image.open(requests.get("https://media.roboflow.com/spaces/doge-2-mask-2.png", stream=True).raw))],
|
| 44 |
-
# "composite": Image.open(requests.get("https://media.roboflow.com/spaces/doge-2-composite-2.png", stream=True).raw),
|
| 45 |
-
# },
|
| 46 |
-
# "little lion",
|
| 47 |
-
# 42,
|
| 48 |
-
# False,
|
| 49 |
-
# 0.85,
|
| 50 |
-
# 30
|
| 51 |
-
# ],
|
| 52 |
-
# [
|
| 53 |
-
# {
|
| 54 |
-
# "background": Image.open(requests.get("https://media.roboflow.com/spaces/doge-2-image.png", stream=True).raw),
|
| 55 |
-
# "layers": [remove_background(Image.open(requests.get("https://media.roboflow.com/spaces/doge-2-mask-3.png", stream=True).raw))],
|
| 56 |
-
# "composite": Image.open(requests.get("https://media.roboflow.com/spaces/doge-2-composite-3.png", stream=True).raw),
|
| 57 |
-
# },
|
| 58 |
-
# "tribal tattoos",
|
| 59 |
-
# 42,
|
| 60 |
-
# False,
|
| 61 |
-
# 0.85,
|
| 62 |
-
# 30
|
| 63 |
-
# ]
|
| 64 |
-
# ]
|
| 65 |
-
|
| 66 |
-
pipe = FluxInpaintPipeline.from_pretrained(
|
| 67 |
"black-forest-labs/FLUX.1-schnell", torch_dtype=torch.bfloat16).to(DEVICE)
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
original_resolution_wh: Tuple[int, int],
|
| 72 |
maximum_dimension: int = IMAGE_SIZE
|
| 73 |
) -> Tuple[int, int]:
|
| 74 |
width, height = original_resolution_wh
|
| 75 |
|
| 76 |
-
# if width <= maximum_dimension and height <= maximum_dimension:
|
| 77 |
-
# width = width - (width % 32)
|
| 78 |
-
# height = height - (height % 32)
|
| 79 |
-
# return width, height
|
| 80 |
-
|
| 81 |
if width > height:
|
| 82 |
scaling_factor = maximum_dimension / width
|
| 83 |
else:
|
|
@@ -92,77 +80,191 @@ def resize_image_dimensions(
|
|
| 92 |
return new_width, new_height
|
| 93 |
|
| 94 |
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
def process(
|
|
|
|
| 97 |
input_image_editor: dict,
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
| 99 |
seed_slicer: int,
|
| 100 |
randomize_seed_checkbox: bool,
|
| 101 |
strength_slider: float,
|
| 102 |
-
num_inference_steps_slider: int
|
| 103 |
-
progress=gr.Progress(track_tqdm=True)
|
| 104 |
):
|
| 105 |
-
if not
|
| 106 |
-
gr.Info("Please enter
|
| 107 |
return None, None
|
| 108 |
|
| 109 |
-
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
if not image:
|
| 113 |
gr.Info("Please upload an image.")
|
| 114 |
return None, None
|
| 115 |
|
| 116 |
-
if not
|
| 117 |
-
gr.Info("Please draw a mask
|
| 118 |
return None, None
|
| 119 |
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
|
| 124 |
-
if
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
|
| 142 |
with gr.Blocks() as demo:
|
|
|
|
| 143 |
gr.Markdown(MARKDOWN)
|
| 144 |
with gr.Row():
|
| 145 |
with gr.Column():
|
| 146 |
input_image_editor_component = gr.ImageEditor(
|
| 147 |
label='Image',
|
| 148 |
-
type='
|
| 149 |
sources=["upload", "webcam"],
|
| 150 |
image_mode='RGB',
|
| 151 |
layers=False,
|
| 152 |
brush=gr.Brush(colors=["#FFFFFF"], color_mode="fixed"))
|
| 153 |
|
| 154 |
with gr.Row():
|
| 155 |
-
|
| 156 |
-
label="
|
| 157 |
show_label=False,
|
| 158 |
max_lines=1,
|
| 159 |
-
placeholder="Enter
|
| 160 |
container=False,
|
| 161 |
)
|
| 162 |
submit_button_component = gr.Button(
|
| 163 |
value='Submit', variant='primary', scale=0)
|
| 164 |
|
| 165 |
with gr.Accordion("Advanced Settings", open=False):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
seed_slicer_component = gr.Slider(
|
| 167 |
label="Seed",
|
| 168 |
minimum=0,
|
|
@@ -177,9 +279,9 @@ with gr.Blocks() as demo:
|
|
| 177 |
with gr.Row():
|
| 178 |
strength_slider_component = gr.Slider(
|
| 179 |
label="Strength",
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
minimum=0,
|
| 184 |
maximum=1,
|
| 185 |
step=0.01,
|
|
@@ -188,10 +290,10 @@ with gr.Blocks() as demo:
|
|
| 188 |
|
| 189 |
num_inference_steps_slider_component = gr.Slider(
|
| 190 |
label="Number of inference steps",
|
| 191 |
-
|
| 192 |
-
|
| 193 |
minimum=1,
|
| 194 |
-
maximum=
|
| 195 |
step=1,
|
| 196 |
value=20,
|
| 197 |
)
|
|
@@ -201,31 +303,36 @@ with gr.Blocks() as demo:
|
|
| 201 |
with gr.Accordion("Debug", open=False):
|
| 202 |
output_mask_component = gr.Image(
|
| 203 |
type='pil', image_mode='RGB', label='Input mask', format="png")
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
|
|
|
| 223 |
|
| 224 |
submit_button_component.click(
|
| 225 |
fn=process,
|
| 226 |
inputs=[
|
|
|
|
| 227 |
input_image_editor_component,
|
| 228 |
-
|
|
|
|
|
|
|
|
|
|
| 229 |
seed_slicer_component,
|
| 230 |
randomize_seed_checkbox_component,
|
| 231 |
strength_slider_component,
|
|
@@ -236,5 +343,245 @@ with gr.Blocks() as demo:
|
|
| 236 |
output_mask_component
|
| 237 |
]
|
| 238 |
)
|
| 239 |
-
|
| 240 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from functools import partial
|
| 2 |
|
| 3 |
+
import cv2
|
| 4 |
import random
|
| 5 |
+
from typing import Tuple, Optional
|
| 6 |
+
|
| 7 |
import gradio as gr
|
| 8 |
+
import numpy as np
|
| 9 |
+
import requests
|
| 10 |
import spaces
|
| 11 |
import torch
|
| 12 |
+
from PIL import Image, ImageFilter
|
| 13 |
from diffusers import FluxInpaintPipeline
|
| 14 |
+
from gradio_client import Client, handle_file
|
|
|
|
| 15 |
|
| 16 |
MARKDOWN = """
|
| 17 |
# FLUX Inpainting
|
| 18 |
+
Model used is FLUX.1-schnell.
|
| 19 |
"""
|
| 20 |
|
| 21 |
MAX_SEED = np.iinfo(np.int32).max
|
| 22 |
+
IMAGE_SIZE = 1024
|
| 23 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 24 |
+
PIPE = FluxInpaintPipeline.from_pretrained(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
"black-forest-labs/FLUX.1-schnell", torch_dtype=torch.bfloat16).to(DEVICE)
|
| 26 |
+
CLIENT = Client("SkalskiP/florence-sam-masking")
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
EXAMPLES = [
|
| 30 |
+
[
|
| 31 |
+
{
|
| 32 |
+
"background": Image.open(requests.get("https://media.roboflow.com/spaces/doge-2-image.png", stream=True).raw),
|
| 33 |
+
"layers": [Image.open(requests.get("https://media.roboflow.com/spaces/doge-2-mask-2-removebg.png", stream=True).raw)],
|
| 34 |
+
"composite": Image.open(requests.get("https://media.roboflow.com/spaces/doge-2-composite-2.png", stream=True).raw),
|
| 35 |
+
},
|
| 36 |
+
"little lion",
|
| 37 |
+
"",
|
| 38 |
+
5,
|
| 39 |
+
5,
|
| 40 |
+
42,
|
| 41 |
+
False,
|
| 42 |
+
0.85,
|
| 43 |
+
20
|
| 44 |
+
],
|
| 45 |
+
[
|
| 46 |
+
{
|
| 47 |
+
"background": Image.open(requests.get("https://media.roboflow.com/spaces/doge-5.jpeg", stream=True).raw),
|
| 48 |
+
"layers": None,
|
| 49 |
+
"composite": None
|
| 50 |
+
},
|
| 51 |
+
"big blue eyes",
|
| 52 |
+
"eyes",
|
| 53 |
+
10,
|
| 54 |
+
5,
|
| 55 |
+
42,
|
| 56 |
+
False,
|
| 57 |
+
0.9,
|
| 58 |
+
20
|
| 59 |
+
]
|
| 60 |
+
]
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def calculate_image_dimensions_for_flux(
|
| 64 |
original_resolution_wh: Tuple[int, int],
|
| 65 |
maximum_dimension: int = IMAGE_SIZE
|
| 66 |
) -> Tuple[int, int]:
|
| 67 |
width, height = original_resolution_wh
|
| 68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
if width > height:
|
| 70 |
scaling_factor = maximum_dimension / width
|
| 71 |
else:
|
|
|
|
| 80 |
return new_width, new_height
|
| 81 |
|
| 82 |
|
| 83 |
+
def is_mask_empty(image: Image.Image) -> bool:
|
| 84 |
+
gray_img = image.convert("L")
|
| 85 |
+
pixels = list(gray_img.getdata())
|
| 86 |
+
return all(pixel == 0 for pixel in pixels)
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
def process_mask(
|
| 90 |
+
mask: Image.Image,
|
| 91 |
+
mask_inflation: Optional[int] = None,
|
| 92 |
+
mask_blur: Optional[int] = None
|
| 93 |
+
) -> Image.Image:
|
| 94 |
+
"""
|
| 95 |
+
Inflates and blurs the white regions of a mask.
|
| 96 |
+
Args:
|
| 97 |
+
mask (Image.Image): The input mask image.
|
| 98 |
+
mask_inflation (Optional[int]): The number of pixels to inflate the mask by.
|
| 99 |
+
mask_blur (Optional[int]): The radius of the Gaussian blur to apply.
|
| 100 |
+
Returns:
|
| 101 |
+
Image.Image: The processed mask with inflated and/or blurred regions.
|
| 102 |
+
"""
|
| 103 |
+
if mask_inflation and mask_inflation > 0:
|
| 104 |
+
mask_array = np.array(mask)
|
| 105 |
+
kernel = np.ones((mask_inflation, mask_inflation), np.uint8)
|
| 106 |
+
mask_array = cv2.dilate(mask_array, kernel, iterations=1)
|
| 107 |
+
mask = Image.fromarray(mask_array)
|
| 108 |
+
|
| 109 |
+
if mask_blur and mask_blur > 0:
|
| 110 |
+
mask = mask.filter(ImageFilter.GaussianBlur(radius=mask_blur))
|
| 111 |
+
|
| 112 |
+
return mask
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
def set_client_for_session(request: gr.Request):
|
| 116 |
+
try:
|
| 117 |
+
x_ip_token = request.headers['x-ip-token']
|
| 118 |
+
return Client("SkalskiP/florence-sam-masking", headers={"X-IP-Token": x_ip_token})
|
| 119 |
+
except:
|
| 120 |
+
return CLIENT
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
@spaces.GPU(duration=50)
|
| 124 |
+
def run_flux(
|
| 125 |
+
image: Image.Image,
|
| 126 |
+
mask: Image.Image,
|
| 127 |
+
prompt: str,
|
| 128 |
+
seed_slicer: int,
|
| 129 |
+
randomize_seed_checkbox: bool,
|
| 130 |
+
strength_slider: float,
|
| 131 |
+
num_inference_steps_slider: int,
|
| 132 |
+
resolution_wh: Tuple[int, int],
|
| 133 |
+
) -> Image.Image:
|
| 134 |
+
print("Running FLUX...")
|
| 135 |
+
width, height = resolution_wh
|
| 136 |
+
if randomize_seed_checkbox:
|
| 137 |
+
seed_slicer = random.randint(0, MAX_SEED)
|
| 138 |
+
generator = torch.Generator().manual_seed(seed_slicer)
|
| 139 |
+
return PIPE(
|
| 140 |
+
prompt=prompt,
|
| 141 |
+
image=image,
|
| 142 |
+
mask_image=mask,
|
| 143 |
+
width=width,
|
| 144 |
+
height=height,
|
| 145 |
+
strength=strength_slider,
|
| 146 |
+
generator=generator,
|
| 147 |
+
num_inference_steps=num_inference_steps_slider
|
| 148 |
+
).images[0]
|
| 149 |
+
|
| 150 |
+
|
| 151 |
def process(
|
| 152 |
+
client,
|
| 153 |
input_image_editor: dict,
|
| 154 |
+
inpainting_prompt_text: str,
|
| 155 |
+
masking_prompt_text: str,
|
| 156 |
+
mask_inflation_slider: int,
|
| 157 |
+
mask_blur_slider: int,
|
| 158 |
seed_slicer: int,
|
| 159 |
randomize_seed_checkbox: bool,
|
| 160 |
strength_slider: float,
|
| 161 |
+
num_inference_steps_slider: int
|
|
|
|
| 162 |
):
|
| 163 |
+
if not inpainting_prompt_text:
|
| 164 |
+
gr.Info("Please enter inpainting text prompt.")
|
| 165 |
return None, None
|
| 166 |
|
| 167 |
+
image_path = input_image_editor['background']
|
| 168 |
+
mask_path = input_image_editor['layers'][0]
|
| 169 |
+
|
| 170 |
+
image = Image.open(image_path)
|
| 171 |
+
mask = Image.open(mask_path)
|
| 172 |
|
| 173 |
if not image:
|
| 174 |
gr.Info("Please upload an image.")
|
| 175 |
return None, None
|
| 176 |
|
| 177 |
+
if is_mask_empty(mask) and not masking_prompt_text:
|
| 178 |
+
gr.Info("Please draw a mask or enter a masking prompt.")
|
| 179 |
return None, None
|
| 180 |
|
| 181 |
+
if not is_mask_empty(mask) and masking_prompt_text:
|
| 182 |
+
gr.Info("Both mask and masking prompt are provided. Please provide only one.")
|
| 183 |
+
return None, None
|
| 184 |
|
| 185 |
+
if is_mask_empty(mask):
|
| 186 |
+
print("Generating mask...")
|
| 187 |
+
mask = client.predict(
|
| 188 |
+
image_input=handle_file(image_path),
|
| 189 |
+
text_input=masking_prompt_text,
|
| 190 |
+
api_name="/process_image")
|
| 191 |
+
mask = Image.open(mask)
|
| 192 |
+
print("Mask generated.")
|
| 193 |
+
|
| 194 |
+
width, height = calculate_image_dimensions_for_flux(original_resolution_wh=image.size)
|
| 195 |
+
image = image.resize((width, height), Image.LANCZOS)
|
| 196 |
+
mask = mask.resize((width, height), Image.LANCZOS)
|
| 197 |
+
mask = process_mask(mask, mask_inflation=mask_inflation_slider, mask_blur=mask_blur_slider)
|
| 198 |
+
image = run_flux(
|
| 199 |
+
image=image,
|
| 200 |
+
mask=mask,
|
| 201 |
+
prompt=inpainting_prompt_text,
|
| 202 |
+
seed_slicer=seed_slicer,
|
| 203 |
+
randomize_seed_checkbox=randomize_seed_checkbox,
|
| 204 |
+
strength_slider=strength_slider,
|
| 205 |
+
num_inference_steps_slider=num_inference_steps_slider,
|
| 206 |
+
resolution_wh=(width, height)
|
| 207 |
+
)
|
| 208 |
+
return image, mask
|
| 209 |
+
|
| 210 |
+
|
| 211 |
+
process_example = partial(process, client=CLIENT)
|
| 212 |
|
| 213 |
|
| 214 |
with gr.Blocks() as demo:
|
| 215 |
+
client_component = gr.State()
|
| 216 |
gr.Markdown(MARKDOWN)
|
| 217 |
with gr.Row():
|
| 218 |
with gr.Column():
|
| 219 |
input_image_editor_component = gr.ImageEditor(
|
| 220 |
label='Image',
|
| 221 |
+
type='filepath',
|
| 222 |
sources=["upload", "webcam"],
|
| 223 |
image_mode='RGB',
|
| 224 |
layers=False,
|
| 225 |
brush=gr.Brush(colors=["#FFFFFF"], color_mode="fixed"))
|
| 226 |
|
| 227 |
with gr.Row():
|
| 228 |
+
inpainting_prompt_text_component = gr.Text(
|
| 229 |
+
label="Inpainting prompt",
|
| 230 |
show_label=False,
|
| 231 |
max_lines=1,
|
| 232 |
+
placeholder="Enter text to generate inpainting",
|
| 233 |
container=False,
|
| 234 |
)
|
| 235 |
submit_button_component = gr.Button(
|
| 236 |
value='Submit', variant='primary', scale=0)
|
| 237 |
|
| 238 |
with gr.Accordion("Advanced Settings", open=False):
|
| 239 |
+
masking_prompt_text_component = gr.Text(
|
| 240 |
+
label="Masking prompt",
|
| 241 |
+
show_label=False,
|
| 242 |
+
max_lines=1,
|
| 243 |
+
placeholder="Enter text to generate masking",
|
| 244 |
+
container=False,
|
| 245 |
+
)
|
| 246 |
+
|
| 247 |
+
with gr.Row():
|
| 248 |
+
mask_inflation_slider_component = gr.Slider(
|
| 249 |
+
label="Mask inflation",
|
| 250 |
+
info="Adjusts the amount of mask edge expansion before "
|
| 251 |
+
"inpainting.",
|
| 252 |
+
minimum=0,
|
| 253 |
+
maximum=20,
|
| 254 |
+
step=1,
|
| 255 |
+
value=5,
|
| 256 |
+
)
|
| 257 |
+
|
| 258 |
+
mask_blur_slider_component = gr.Slider(
|
| 259 |
+
label="Mask blur",
|
| 260 |
+
info="Controls the intensity of the Gaussian blur applied to "
|
| 261 |
+
"the mask edges.",
|
| 262 |
+
minimum=0,
|
| 263 |
+
maximum=20,
|
| 264 |
+
step=1,
|
| 265 |
+
value=5,
|
| 266 |
+
)
|
| 267 |
+
|
| 268 |
seed_slicer_component = gr.Slider(
|
| 269 |
label="Seed",
|
| 270 |
minimum=0,
|
|
|
|
| 279 |
with gr.Row():
|
| 280 |
strength_slider_component = gr.Slider(
|
| 281 |
label="Strength",
|
| 282 |
+
info="Indicates extent to transform the reference `image`. "
|
| 283 |
+
"Must be between 0 and 1. `image` is used as a starting "
|
| 284 |
+
"point and more noise is added the higher the `strength`.",
|
| 285 |
minimum=0,
|
| 286 |
maximum=1,
|
| 287 |
step=0.01,
|
|
|
|
| 290 |
|
| 291 |
num_inference_steps_slider_component = gr.Slider(
|
| 292 |
label="Number of inference steps",
|
| 293 |
+
info="The number of denoising steps. More denoising steps "
|
| 294 |
+
"usually lead to a higher quality image at the",
|
| 295 |
minimum=1,
|
| 296 |
+
maximum=50,
|
| 297 |
step=1,
|
| 298 |
value=20,
|
| 299 |
)
|
|
|
|
| 303 |
with gr.Accordion("Debug", open=False):
|
| 304 |
output_mask_component = gr.Image(
|
| 305 |
type='pil', image_mode='RGB', label='Input mask', format="png")
|
| 306 |
+
gr.Examples(
|
| 307 |
+
fn=process_example,
|
| 308 |
+
examples=EXAMPLES,
|
| 309 |
+
inputs=[
|
| 310 |
+
input_image_editor_component,
|
| 311 |
+
inpainting_prompt_text_component,
|
| 312 |
+
masking_prompt_text_component,
|
| 313 |
+
mask_inflation_slider_component,
|
| 314 |
+
mask_blur_slider_component,
|
| 315 |
+
seed_slicer_component,
|
| 316 |
+
randomize_seed_checkbox_component,
|
| 317 |
+
strength_slider_component,
|
| 318 |
+
num_inference_steps_slider_component
|
| 319 |
+
],
|
| 320 |
+
outputs=[
|
| 321 |
+
output_image_component,
|
| 322 |
+
output_mask_component
|
| 323 |
+
],
|
| 324 |
+
run_on_click=False
|
| 325 |
+
)
|
| 326 |
|
| 327 |
submit_button_component.click(
|
| 328 |
fn=process,
|
| 329 |
inputs=[
|
| 330 |
+
client_component,
|
| 331 |
input_image_editor_component,
|
| 332 |
+
inpainting_prompt_text_component,
|
| 333 |
+
masking_prompt_text_component,
|
| 334 |
+
mask_inflation_slider_component,
|
| 335 |
+
mask_blur_slider_component,
|
| 336 |
seed_slicer_component,
|
| 337 |
randomize_seed_checkbox_component,
|
| 338 |
strength_slider_component,
|
|
|
|
| 343 |
output_mask_component
|
| 344 |
]
|
| 345 |
)
|
| 346 |
+
demo.load(set_client_for_session, None, client_component)
|
| 347 |
+
|
| 348 |
+
demo.launch(debug=False, show_error=True)
|
| 349 |
+
|
| 350 |
+
|
| 351 |
+
|
| 352 |
+
# from typing import Tuple
|
| 353 |
+
|
| 354 |
+
# import requests
|
| 355 |
+
# import random
|
| 356 |
+
# import numpy as np
|
| 357 |
+
# import gradio as gr
|
| 358 |
+
# import spaces
|
| 359 |
+
# import torch
|
| 360 |
+
# from PIL import Image
|
| 361 |
+
# from diffusers import FluxInpaintPipeline
|
| 362 |
+
|
| 363 |
+
# torch.cuda.empty_cache()
|
| 364 |
+
|
| 365 |
+
|
| 366 |
+
# MAX_SEED = np.iinfo(np.int32).max
|
| 367 |
+
# IMAGE_SIZE = 512
|
| 368 |
+
# DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 369 |
+
|
| 370 |
+
|
| 371 |
+
# def remove_background(image: Image.Image, threshold: int = 50) -> Image.Image:
|
| 372 |
+
# image = image.convert("RGBA")
|
| 373 |
+
# data = image.getdata()
|
| 374 |
+
# new_data = []
|
| 375 |
+
# for item in data:
|
| 376 |
+
# avg = sum(item[:3]) / 3
|
| 377 |
+
# if avg < threshold:
|
| 378 |
+
# new_data.append((0, 0, 0, 0))
|
| 379 |
+
# else:
|
| 380 |
+
# new_data.append(item)
|
| 381 |
+
|
| 382 |
+
# image.putdata(new_data)
|
| 383 |
+
# return image
|
| 384 |
+
|
| 385 |
+
|
| 386 |
+
# # EXAMPLES = [
|
| 387 |
+
# # [
|
| 388 |
+
# # {
|
| 389 |
+
# # "background": Image.open(requests.get("https://media.roboflow.com/spaces/doge-2-image.png", stream=True).raw),
|
| 390 |
+
# # "layers": [remove_background(Image.open(requests.get("https://media.roboflow.com/spaces/doge-2-mask-2.png", stream=True).raw))],
|
| 391 |
+
# # "composite": Image.open(requests.get("https://media.roboflow.com/spaces/doge-2-composite-2.png", stream=True).raw),
|
| 392 |
+
# # },
|
| 393 |
+
# # "little lion",
|
| 394 |
+
# # 42,
|
| 395 |
+
# # False,
|
| 396 |
+
# # 0.85,
|
| 397 |
+
# # 30
|
| 398 |
+
# # ],
|
| 399 |
+
# # [
|
| 400 |
+
# # {
|
| 401 |
+
# # "background": Image.open(requests.get("https://media.roboflow.com/spaces/doge-2-image.png", stream=True).raw),
|
| 402 |
+
# # "layers": [remove_background(Image.open(requests.get("https://media.roboflow.com/spaces/doge-2-mask-3.png", stream=True).raw))],
|
| 403 |
+
# # "composite": Image.open(requests.get("https://media.roboflow.com/spaces/doge-2-composite-3.png", stream=True).raw),
|
| 404 |
+
# # },
|
| 405 |
+
# # "tribal tattoos",
|
| 406 |
+
# # 42,
|
| 407 |
+
# # False,
|
| 408 |
+
# # 0.85,
|
| 409 |
+
# # 30
|
| 410 |
+
# # ]
|
| 411 |
+
# # ]
|
| 412 |
+
|
| 413 |
+
# pipe = FluxInpaintPipeline.from_pretrained(
|
| 414 |
+
# "black-forest-labs/FLUX.1-schnell", torch_dtype=torch.bfloat16).to(DEVICE)
|
| 415 |
+
|
| 416 |
+
|
| 417 |
+
# def resize_image_dimensions(
|
| 418 |
+
# original_resolution_wh: Tuple[int, int],
|
| 419 |
+
# maximum_dimension: int = IMAGE_SIZE
|
| 420 |
+
# ) -> Tuple[int, int]:
|
| 421 |
+
# width, height = original_resolution_wh
|
| 422 |
+
|
| 423 |
+
# # if width <= maximum_dimension and height <= maximum_dimension:
|
| 424 |
+
# # width = width - (width % 32)
|
| 425 |
+
# # height = height - (height % 32)
|
| 426 |
+
# # return width, height
|
| 427 |
+
|
| 428 |
+
# if width > height:
|
| 429 |
+
# scaling_factor = maximum_dimension / width
|
| 430 |
+
# else:
|
| 431 |
+
# scaling_factor = maximum_dimension / height
|
| 432 |
+
|
| 433 |
+
# new_width = int(width * scaling_factor)
|
| 434 |
+
# new_height = int(height * scaling_factor)
|
| 435 |
+
|
| 436 |
+
# new_width = new_width - (new_width % 32)
|
| 437 |
+
# new_height = new_height - (new_height % 32)
|
| 438 |
+
|
| 439 |
+
# return new_width, new_height
|
| 440 |
+
|
| 441 |
+
|
| 442 |
+
# @spaces.GPU(duration=100)
|
| 443 |
+
# def process(
|
| 444 |
+
# input_image_editor: dict,
|
| 445 |
+
# input_text: str,
|
| 446 |
+
# seed_slicer: int,
|
| 447 |
+
# randomize_seed_checkbox: bool,
|
| 448 |
+
# strength_slider: float,
|
| 449 |
+
# num_inference_steps_slider: int,
|
| 450 |
+
# progress=gr.Progress(track_tqdm=True)
|
| 451 |
+
# ):
|
| 452 |
+
# if not input_text:
|
| 453 |
+
# gr.Info("Please enter a text prompt.")
|
| 454 |
+
# return None, None
|
| 455 |
+
|
| 456 |
+
# image = input_image_editor['background']
|
| 457 |
+
# mask = input_image_editor['layers'][0]
|
| 458 |
+
|
| 459 |
+
# if not image:
|
| 460 |
+
# gr.Info("Please upload an image.")
|
| 461 |
+
# return None, None
|
| 462 |
+
|
| 463 |
+
# if not mask:
|
| 464 |
+
# gr.Info("Please draw a mask on the image.")
|
| 465 |
+
# return None, None
|
| 466 |
+
|
| 467 |
+
# width, height = resize_image_dimensions(original_resolution_wh=image.size)
|
| 468 |
+
# resized_image = image.resize((width, height), Image.LANCZOS)
|
| 469 |
+
# resized_mask = mask.resize((width, height), Image.LANCZOS)
|
| 470 |
+
|
| 471 |
+
# if randomize_seed_checkbox:
|
| 472 |
+
# seed_slicer = random.randint(0, MAX_SEED)
|
| 473 |
+
# generator = torch.Generator().manual_seed(seed_slicer)
|
| 474 |
+
# with torch.no_grad(), torch.autocast("cuda"):
|
| 475 |
+
# result = pipe(
|
| 476 |
+
# prompt=input_text,
|
| 477 |
+
# image=resized_image,
|
| 478 |
+
# mask_image=resized_mask,
|
| 479 |
+
# width=width,
|
| 480 |
+
# height=height,
|
| 481 |
+
# strength=strength_slider,
|
| 482 |
+
# generator=generator,
|
| 483 |
+
# num_inference_steps=num_inference_steps_slider
|
| 484 |
+
# ).images[0]
|
| 485 |
+
# torch.cuda.empty_cache()
|
| 486 |
+
# return result, resized_mask
|
| 487 |
+
|
| 488 |
+
|
| 489 |
+
# with gr.Blocks() as demo:
|
| 490 |
+
# gr.Markdown(MARKDOWN)
|
| 491 |
+
# with gr.Row():
|
| 492 |
+
# with gr.Column():
|
| 493 |
+
# input_image_editor_component = gr.ImageEditor(
|
| 494 |
+
# label='Image',
|
| 495 |
+
# type='pil',
|
| 496 |
+
# sources=["upload", "webcam"],
|
| 497 |
+
# image_mode='RGB',
|
| 498 |
+
# layers=False,
|
| 499 |
+
# brush=gr.Brush(colors=["#FFFFFF"], color_mode="fixed"))
|
| 500 |
+
|
| 501 |
+
# with gr.Row():
|
| 502 |
+
# input_text_component = gr.Text(
|
| 503 |
+
# label="Prompt",
|
| 504 |
+
# show_label=False,
|
| 505 |
+
# max_lines=1,
|
| 506 |
+
# placeholder="Enter your prompt",
|
| 507 |
+
# container=False,
|
| 508 |
+
# )
|
| 509 |
+
# submit_button_component = gr.Button(
|
| 510 |
+
# value='Submit', variant='primary', scale=0)
|
| 511 |
+
|
| 512 |
+
# with gr.Accordion("Advanced Settings", open=False):
|
| 513 |
+
# seed_slicer_component = gr.Slider(
|
| 514 |
+
# label="Seed",
|
| 515 |
+
# minimum=0,
|
| 516 |
+
# maximum=MAX_SEED,
|
| 517 |
+
# step=1,
|
| 518 |
+
# value=42,
|
| 519 |
+
# )
|
| 520 |
+
|
| 521 |
+
# randomize_seed_checkbox_component = gr.Checkbox(
|
| 522 |
+
# label="Randomize seed", value=True)
|
| 523 |
+
|
| 524 |
+
# with gr.Row():
|
| 525 |
+
# strength_slider_component = gr.Slider(
|
| 526 |
+
# label="Strength",
|
| 527 |
+
# # info="Indicates extent to transform the reference `image`. "
|
| 528 |
+
# # "Must be between 0 and 1. `image` is used as a starting "
|
| 529 |
+
# # "point and more noise is added the higher the `strength`.",
|
| 530 |
+
# minimum=0,
|
| 531 |
+
# maximum=1,
|
| 532 |
+
# step=0.01,
|
| 533 |
+
# value=0.85,
|
| 534 |
+
# )
|
| 535 |
+
|
| 536 |
+
# num_inference_steps_slider_component = gr.Slider(
|
| 537 |
+
# label="Number of inference steps",
|
| 538 |
+
# # info="The number of denoising steps. More denoising steps "
|
| 539 |
+
# # "usually lead to a higher quality image at the",
|
| 540 |
+
# minimum=1,
|
| 541 |
+
# maximum=20,
|
| 542 |
+
# step=1,
|
| 543 |
+
# value=20,
|
| 544 |
+
# )
|
| 545 |
+
# with gr.Column():
|
| 546 |
+
# output_image_component = gr.Image(
|
| 547 |
+
# type='pil', image_mode='RGB', label='Generated image', format="png")
|
| 548 |
+
# with gr.Accordion("Debug", open=False):
|
| 549 |
+
# output_mask_component = gr.Image(
|
| 550 |
+
# type='pil', image_mode='RGB', label='Input mask', format="png")
|
| 551 |
+
# # with gr.Row():
|
| 552 |
+
# # gr.Examples(
|
| 553 |
+
# # fn=process,
|
| 554 |
+
# # examples=EXAMPLES,
|
| 555 |
+
# # inputs=[
|
| 556 |
+
# # input_image_editor_component,
|
| 557 |
+
# # input_text_component,
|
| 558 |
+
# # seed_slicer_component,
|
| 559 |
+
# # randomize_seed_checkbox_component,
|
| 560 |
+
# # strength_slider_component,
|
| 561 |
+
# # num_inference_steps_slider_component
|
| 562 |
+
# # ],
|
| 563 |
+
# # outputs=[
|
| 564 |
+
# # output_image_component,
|
| 565 |
+
# # output_mask_component
|
| 566 |
+
# # ],
|
| 567 |
+
# # run_on_click=True,
|
| 568 |
+
# # cache_examples=True
|
| 569 |
+
# # )
|
| 570 |
+
|
| 571 |
+
# submit_button_component.click(
|
| 572 |
+
# fn=process,
|
| 573 |
+
# inputs=[
|
| 574 |
+
# input_image_editor_component,
|
| 575 |
+
# input_text_component,
|
| 576 |
+
# seed_slicer_component,
|
| 577 |
+
# randomize_seed_checkbox_component,
|
| 578 |
+
# strength_slider_component,
|
| 579 |
+
# num_inference_steps_slider_component
|
| 580 |
+
# ],
|
| 581 |
+
# outputs=[
|
| 582 |
+
# output_image_component,
|
| 583 |
+
# output_mask_component
|
| 584 |
+
# ]
|
| 585 |
+
# )
|
| 586 |
+
|
| 587 |
+
# demo.launch(share=True)
|