Spaces:
Running
on
Zero
Running
on
Zero
add mask
Browse files
app.py
CHANGED
|
@@ -1,4 +1,7 @@
|
|
| 1 |
import spaces
|
|
|
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import re
|
| 4 |
from PIL import Image
|
|
@@ -22,7 +25,7 @@ def process_images(image, image2=None,prompt="a girl",inpaint_model="black-fores
|
|
| 22 |
if not isinstance(image, dict):
|
| 23 |
if image2 == None:
|
| 24 |
print("empty mask")
|
| 25 |
-
return image
|
| 26 |
else:
|
| 27 |
image = dict({'background': image, 'layers': [image2]})
|
| 28 |
|
|
@@ -37,9 +40,25 @@ def process_images(image, image2=None,prompt="a girl",inpaint_model="black-fores
|
|
| 37 |
mask = image['layers'][0]
|
| 38 |
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
output = flux1_inpaint.process_image(image["background"],mask,prompt,inpaint_model,strength,seed)
|
| 41 |
|
| 42 |
-
return output
|
| 43 |
|
| 44 |
|
| 45 |
def read_file(path: str) -> str:
|
|
@@ -91,12 +110,13 @@ with demo_blocks as demo:
|
|
| 91 |
id_input=gr.Text(label="Name", visible=False)
|
| 92 |
|
| 93 |
with gr.Column():
|
| 94 |
-
image_out = gr.Image(height=800,sources=[],label="Output", elem_id="output-img",format="
|
|
|
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
-
btn.click(fn=process_images, inputs=[image, image_mask,prompt,inpaint_model,strength,seed], outputs =image_out, api_name='infer')
|
| 100 |
gr.Examples(
|
| 101 |
examples=[
|
| 102 |
["images/00547245_99.jpg", "images/00547245_99_mask.jpg","a beautiful girl,eyes closed",0.8,"images/00547245.jpg"],
|
|
|
|
| 1 |
import spaces
|
| 2 |
+
import torch
|
| 3 |
+
from diffusers import FluxInpaintPipeline
|
| 4 |
+
|
| 5 |
import gradio as gr
|
| 6 |
import re
|
| 7 |
from PIL import Image
|
|
|
|
| 25 |
if not isinstance(image, dict):
|
| 26 |
if image2 == None:
|
| 27 |
print("empty mask")
|
| 28 |
+
return image,None
|
| 29 |
else:
|
| 30 |
image = dict({'background': image, 'layers': [image2]})
|
| 31 |
|
|
|
|
| 40 |
mask = image['layers'][0]
|
| 41 |
|
| 42 |
|
| 43 |
+
def process_image(image,mask_image,prompt="a person",model_id="black-forest-labs/FLUX.1-schnell",strength=0.75,seed=0,num_inference_steps=4):
|
| 44 |
+
if image == None:
|
| 45 |
+
return None
|
| 46 |
+
|
| 47 |
+
pipe = FluxInpaintPipeline.from_pretrained(model_id, torch_dtype=torch.bfloat16)
|
| 48 |
+
pipe.to("cuda")
|
| 49 |
+
|
| 50 |
+
generators = []
|
| 51 |
+
generator = torch.Generator("cuda").manual_seed(seed)
|
| 52 |
+
generators.append(generator)
|
| 53 |
+
# more parameter see https://huggingface.co/docs/diffusers/api/pipelines/flux#diffusers.FluxInpaintPipeline
|
| 54 |
+
output = pipe(prompt=prompt, image=image, mask_image=mask_image,generator=generator,strength=strength)
|
| 55 |
+
|
| 56 |
+
return output.images[0]
|
| 57 |
+
|
| 58 |
+
|
| 59 |
output = flux1_inpaint.process_image(image["background"],mask,prompt,inpaint_model,strength,seed)
|
| 60 |
|
| 61 |
+
return output,mask
|
| 62 |
|
| 63 |
|
| 64 |
def read_file(path: str) -> str:
|
|
|
|
| 110 |
id_input=gr.Text(label="Name", visible=False)
|
| 111 |
|
| 112 |
with gr.Column():
|
| 113 |
+
image_out = gr.Image(height=800,sources=[],label="Output", elem_id="output-img",format="webp")
|
| 114 |
+
mask_out = gr.Image(height=800,sources=[],label="Mask", elem_id="mask-img",format="jpeg")
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
+
btn.click(fn=process_images, inputs=[image, image_mask,prompt,inpaint_model,strength,seed], outputs =[image_out,mask_out], api_name='infer')
|
| 120 |
gr.Examples(
|
| 121 |
examples=[
|
| 122 |
["images/00547245_99.jpg", "images/00547245_99_mask.jpg","a beautiful girl,eyes closed",0.8,"images/00547245.jpg"],
|