Spaces:
Running
on
Zero
Running
on
Zero
initial
Browse files- app.py +103 -0
- demo_header.html +8 -0
- flux1_inpaint.py +61 -0
- requirements.txt +7 -0
app.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import spaces
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import re
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import flux1_inpaint
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def sanitize_prompt(prompt):
|
| 9 |
+
# Allow only alphanumeric characters, spaces, and basic punctuation
|
| 10 |
+
allowed_chars = re.compile(r"[^a-zA-Z0-9\s.,!?-]")
|
| 11 |
+
sanitized_prompt = allowed_chars.sub("", prompt)
|
| 12 |
+
return sanitized_prompt
|
| 13 |
+
|
| 14 |
+
@spaces.GPU(duration=180)
|
| 15 |
+
def process_images(image, image2=None,prompt="a girl",negative_prompt=None,inpaint_model="black-forest-labs/FLUX.1-schnell",strength=0.75):
|
| 16 |
+
if negative_prompt == None:
|
| 17 |
+
negative_prompt = ""
|
| 18 |
+
# I'm not sure when this happen
|
| 19 |
+
if not isinstance(image, dict):
|
| 20 |
+
if image2 == None:
|
| 21 |
+
print("empty mask")
|
| 22 |
+
return image
|
| 23 |
+
else:
|
| 24 |
+
image = dict({'background': image, 'layers': [image2]})
|
| 25 |
+
|
| 26 |
+
if image2!=None:
|
| 27 |
+
#print("use image2")
|
| 28 |
+
mask = image2
|
| 29 |
+
else:
|
| 30 |
+
if len(image['layers']) == 0:
|
| 31 |
+
print("empty mask")
|
| 32 |
+
return image
|
| 33 |
+
print("use layer")
|
| 34 |
+
mask = image['layers'][0]
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
output = flux1_inpaint.process_image(image["background"],mask,prompt,negative_prompt,inpaint_model,strength)
|
| 38 |
+
|
| 39 |
+
return output
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def read_file(path: str) -> str:
|
| 43 |
+
with open(path, 'r', encoding='utf-8') as f:
|
| 44 |
+
content = f.read()
|
| 45 |
+
|
| 46 |
+
return content
|
| 47 |
+
|
| 48 |
+
css="""
|
| 49 |
+
#col-left {
|
| 50 |
+
margin: 0 auto;
|
| 51 |
+
max-width: 640px;
|
| 52 |
+
}
|
| 53 |
+
#col-right {
|
| 54 |
+
margin: 0 auto;
|
| 55 |
+
max-width: 640px;
|
| 56 |
+
}
|
| 57 |
+
"""
|
| 58 |
+
demo_blocks = gr.Blocks(css=css, elem_id="demo-container")
|
| 59 |
+
with demo_blocks as demo:
|
| 60 |
+
with gr.Column():
|
| 61 |
+
gr.HTML(read_file("demo_header.html"))
|
| 62 |
+
with gr.Row():
|
| 63 |
+
with gr.Column():
|
| 64 |
+
image = gr.ImageEditor(height=1000,sources=['upload','clipboard'],transforms=[],image_mode='RGB', layers=False, elem_id="image_upload", type="pil", label="Upload",brush=gr.Brush(colors=["#999"], color_mode="fixed"))
|
| 65 |
+
with gr.Row(elem_id="prompt-container", equal_height=False):
|
| 66 |
+
with gr.Row():
|
| 67 |
+
prompt = gr.Textbox(label="Prompt",placeholder="Your prompt (what you want in place of what is erased)", elem_id="prompt")
|
| 68 |
+
|
| 69 |
+
btn = gr.Button("Inpaint!", elem_id="run_button")
|
| 70 |
+
negative_prompt = gr.Textbox(label="Negative Prompt",placeholder="negative prompt",value="worst quality", elem_id="negative_prompt")
|
| 71 |
+
|
| 72 |
+
image_mask = gr.Image(sources=['upload','clipboard'], elem_id="mask_upload", type="pil", label="Mask_Upload",height=400, value=None)
|
| 73 |
+
with gr.Accordion(label="Advanced Settings", open=False):
|
| 74 |
+
with gr.Row( equal_height=True):
|
| 75 |
+
strength = gr.Number(value=0.8, minimum=0, maximum=1.0, step=0.01, label="Inpaint strength")
|
| 76 |
+
blur_radius = gr.Number(value=25, minimum=0.0, maximum=50.0, step=1, label="Blur Radius")
|
| 77 |
+
edge_expand = gr.Number(value=8, minimum=0.0, maximum=20.0, step=1, label="Edge Expand")
|
| 78 |
+
with gr.Row(equal_height=True):
|
| 79 |
+
models = ["black-forest-labs/FLUX.1-schnell"]
|
| 80 |
+
inpaint_model = gr.Dropdown(label="modes", choices=models, value="stablediffusionapi/bracingevomix-v2")
|
| 81 |
+
with gr.Column():
|
| 82 |
+
image_out = gr.Image(sources=[],label="Output", elem_id="output-img")
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
btn.click(fn=process_images, inputs=[image, image_mask,prompt,negative_prompt,inpaint_model,strength], outputs =image_out, api_name='infer')
|
| 87 |
+
gr.Examples(
|
| 88 |
+
examples=[["examples/catman.jpg", "examples/catman_mask.jpg","He's wearing a dog face."]]
|
| 89 |
+
,
|
| 90 |
+
#fn=predict,
|
| 91 |
+
inputs=[image,image_mask,prompt],
|
| 92 |
+
cache_examples=False,
|
| 93 |
+
)
|
| 94 |
+
gr.HTML(
|
| 95 |
+
"""
|
| 96 |
+
<div style="text-align: center;">
|
| 97 |
+
<p>Inpaint Code <a href="https://github.com/opencv/opencv/blob/da3debda6d233af90e421e95700c63fc08b83b75/samples/python/inpaint.py" style="text-decoration: underline;" target="_blank">OpenCV inpaint example</a> - Gradio Demo by 🤗 Hugging Face
|
| 98 |
+
</p>
|
| 99 |
+
</div>
|
| 100 |
+
"""
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
demo_blocks.queue(max_size=25).launch(share=False,debug=True)
|
demo_header.html
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<div style="text-align: center;">
|
| 2 |
+
<h1>
|
| 3 |
+
Inpaint
|
| 4 |
+
</h1>
|
| 5 |
+
<p>
|
| 6 |
+
|
| 7 |
+
</p>
|
| 8 |
+
</div>
|
flux1_inpaint.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from diffusers import FluxInpaintPipeline
|
| 3 |
+
from diffusers.utils import load_image
|
| 4 |
+
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import sys
|
| 7 |
+
import numpy as np
|
| 8 |
+
|
| 9 |
+
import json
|
| 10 |
+
import os
|
| 11 |
+
import spaces
|
| 12 |
+
|
| 13 |
+
device = "cuda"
|
| 14 |
+
pipeline_device = 0 if torch.cuda.is_available() else -1 # TODO mix above
|
| 15 |
+
torch_dtype = torch.float16
|
| 16 |
+
debug = True
|
| 17 |
+
@spaces.GPU
|
| 18 |
+
def make_inpaint_condition(image, image_mask):
|
| 19 |
+
image = np.array(image.convert("RGB")).astype(np.float32) / 255.0
|
| 20 |
+
image_mask = np.array(image_mask.convert("L")).astype(np.float32) / 255.0
|
| 21 |
+
|
| 22 |
+
if image.shape[0:1] != image_mask.shape[0:1]:
|
| 23 |
+
print("error image and image_mask must have the same image size")
|
| 24 |
+
return None
|
| 25 |
+
|
| 26 |
+
image[image_mask > 0.5] = -1.0 # set as masked pixel
|
| 27 |
+
image = np.expand_dims(image, 0).transpose(0, 3, 1, 2)
|
| 28 |
+
image = torch.from_numpy(image)
|
| 29 |
+
return image
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
@spaces.GPU
|
| 36 |
+
def process_image(image,mask_image,prompt="a girl",negative_prompt="",model_id="black-forest-labs/FLUX.1-schnell",strength=0.75,seed=0,num_inference_steps=4):
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
#control_image=make_inpaint_condition(image,mask_image)
|
| 41 |
+
#image.save("_control.jpg")
|
| 42 |
+
if image == None:
|
| 43 |
+
return None
|
| 44 |
+
|
| 45 |
+
pipe = FluxInpaintPipeline.from_pretrained(model_id, torch_dtype=torch.bfloat16)
|
| 46 |
+
|
| 47 |
+
#batch_size =1
|
| 48 |
+
generators = []
|
| 49 |
+
generator = torch.Generator(device).manual_seed(seed)
|
| 50 |
+
generators.append(generator)
|
| 51 |
+
|
| 52 |
+
output = pipe(prompt=prompt, image=image, mask_image=mask_image,generator=generator)
|
| 53 |
+
|
| 54 |
+
return output.images[0]
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
if __name__ == "__main__":
|
| 58 |
+
image = Image.open(sys.argv[1])
|
| 59 |
+
mask = Image.open(sys.argv[2])
|
| 60 |
+
output = process_image(image,mask)
|
| 61 |
+
output.save(sys.argv[3])
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
safetensors
|
| 2 |
+
numpy
|
| 3 |
+
torch
|
| 4 |
+
diffusers
|
| 5 |
+
spaces
|
| 6 |
+
accelerate
|
| 7 |
+
transformers
|