Spaces:
Runtime error
Runtime error
Commit
·
884a837
1
Parent(s):
2fb212a
Commit Inicial
Browse files
app.py
ADDED
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import CLIPSegProcessor, CLIPSegForImageSegmentation
|
4 |
+
from diffusers import StableDiffusionInpaintPipeline
|
5 |
+
from PIL import Image, ImageOps
|
6 |
+
import PIL
|
7 |
+
|
8 |
+
# cuda cpu
|
9 |
+
device_name = 'cuda'
|
10 |
+
device = torch.device(device_name)
|
11 |
+
|
12 |
+
processor = CLIPSegProcessor.from_pretrained("CIDAS/clipseg-rd64-refined")
|
13 |
+
model = CLIPSegForImageSegmentation.from_pretrained("CIDAS/clipseg-rd64-refined").to(device)
|
14 |
+
inpainting_pipeline = StableDiffusionInpaintPipeline.from_pretrained("stabilityai/stable-diffusion-2-inpainting").to(device)
|
15 |
+
|
16 |
+
|
17 |
+
def numpy_to_pil(images):
|
18 |
+
if images.ndim == 3:
|
19 |
+
images = images[None, ...]
|
20 |
+
images = (images * 255).round().astype("uint8")
|
21 |
+
|
22 |
+
if images.shape[-1] == 1:
|
23 |
+
# special case for grayscale (single channel) images
|
24 |
+
pil_images = [Image.fromarray(image.squeeze(), mode="L") for image in images]
|
25 |
+
else:
|
26 |
+
pil_images = [Image.fromarray(image) for image in images]
|
27 |
+
|
28 |
+
return pil_images
|
29 |
+
|
30 |
+
|
31 |
+
def get_mask(text, image):
|
32 |
+
inputs = processor(
|
33 |
+
text=[text], images=[image], padding="max_length", return_tensors="pt"
|
34 |
+
).to(device)
|
35 |
+
|
36 |
+
outputs = model(**inputs)
|
37 |
+
mask = torch.sigmoid(outputs.logits).cpu().detach().unsqueeze(-1).numpy()
|
38 |
+
|
39 |
+
mask_pil = numpy_to_pil(mask)[0].resize(image.size)
|
40 |
+
#mask_pil.show()
|
41 |
+
return mask_pil
|
42 |
+
|
43 |
+
|
44 |
+
def predict(prompt, negative_prompt, image, obj2mask):
|
45 |
+
mask = get_mask(obj2mask, image)
|
46 |
+
image = image.convert("RGB").resize((512, 512))
|
47 |
+
mask_image = mask.convert("RGB").resize((512, 512))
|
48 |
+
mask_image = ImageOps.invert(mask_image)
|
49 |
+
images = inpainting_pipeline(prompt=prompt, negative_prompt=negative_prompt, image=image,
|
50 |
+
mask_image=mask_image).images
|
51 |
+
mask = mask_image.convert('L')
|
52 |
+
|
53 |
+
PIL.Image.composite(images[0], image, mask)
|
54 |
+
return (images[0])
|
55 |
+
|
56 |
+
|
57 |
+
def inference(prompt, negative_prompt, obj2mask, image_numpy):
|
58 |
+
generator = torch.Generator()
|
59 |
+
generator.manual_seed(int(52362))
|
60 |
+
|
61 |
+
image = numpy_to_pil(image_numpy)[0].convert("RGB").resize((512, 512))
|
62 |
+
img = predict(prompt, negative_prompt, image, obj2mask)
|
63 |
+
return img
|
64 |
+
|
65 |
+
|
66 |
+
with gr.Blocks() as demo:
|
67 |
+
with gr.Row():
|
68 |
+
with gr.Column():
|
69 |
+
prompt = gr.Textbox(label="Prompt", value="cinematic, landscape, sharpe focus")
|
70 |
+
negative_prompt = gr.Textbox(label="Negative Prompt", value="illustration, 3d render")
|
71 |
+
mask = gr.Textbox(label="Mask", value="shoe")
|
72 |
+
intput_img = gr.Image()
|
73 |
+
run = gr.Button(value="Generate")
|
74 |
+
with gr.Column():
|
75 |
+
output_img = gr.Image()
|
76 |
+
|
77 |
+
run.click(
|
78 |
+
inference,
|
79 |
+
inputs=[prompt, negative_prompt, mask, intput_img
|
80 |
+
],
|
81 |
+
outputs=output_img,
|
82 |
+
)
|
83 |
+
|
84 |
+
demo.queue(concurrency_count=1)
|
85 |
+
demo.launch()
|