Spaces:
Runtime error
Runtime error
Commit
·
ca1f90f
1
Parent(s):
3b14a4f
Enviando nova versão
Browse files- app.py +67 -9
- app_v1.py +85 -0
- requirements.txt +1 -1
app.py
CHANGED
@@ -1,17 +1,28 @@
|
|
|
|
|
|
|
|
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 = 'cpu'
|
10 |
device = torch.device(device_name)
|
11 |
|
12 |
processor = CLIPSegProcessor.from_pretrained("CIDAS/clipseg-rd64-refined")
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
|
17 |
def numpy_to_pil(images):
|
@@ -33,7 +44,7 @@ def get_mask(text, image):
|
|
33 |
text=[text], images=[image], padding="max_length", return_tensors="pt"
|
34 |
).to(device)
|
35 |
|
36 |
-
outputs =
|
37 |
mask = torch.sigmoid(outputs.logits).cpu().detach().unsqueeze(-1).numpy()
|
38 |
|
39 |
mask_pil = numpy_to_pil(mask)[0].resize(image.size)
|
@@ -41,17 +52,64 @@ def get_mask(text, image):
|
|
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 |
-
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
mask = mask_image.convert('L')
|
52 |
|
53 |
-
PIL.Image.composite(
|
54 |
-
return (
|
55 |
|
56 |
|
57 |
def inference(prompt, negative_prompt, obj2mask, image_numpy):
|
@@ -66,8 +124,8 @@ def inference(prompt, negative_prompt, obj2mask, image_numpy):
|
|
66 |
with gr.Blocks() as demo:
|
67 |
with gr.Row():
|
68 |
with gr.Column():
|
69 |
-
prompt = gr.Textbox(label="Prompt", value="cinematic,
|
70 |
-
negative_prompt = gr.Textbox(label="Negative Prompt", value="
|
71 |
mask = gr.Textbox(label="Mask", value="shoe")
|
72 |
intput_img = gr.Image()
|
73 |
run = gr.Button(value="Generate")
|
|
|
1 |
+
import io
|
2 |
+
from io import BytesIO
|
3 |
+
|
4 |
import gradio as gr
|
5 |
+
import requests
|
6 |
import torch
|
7 |
from transformers import CLIPSegProcessor, CLIPSegForImageSegmentation
|
8 |
from diffusers import StableDiffusionInpaintPipeline
|
9 |
from PIL import Image, ImageOps
|
10 |
import PIL
|
11 |
+
import replicate
|
12 |
+
import os
|
13 |
|
14 |
# cuda cpu
|
15 |
device_name = 'cpu'
|
16 |
device = torch.device(device_name)
|
17 |
|
18 |
processor = CLIPSegProcessor.from_pretrained("CIDAS/clipseg-rd64-refined")
|
19 |
+
model_clip = CLIPSegForImageSegmentation.from_pretrained("CIDAS/clipseg-rd64-refined").to(device)
|
20 |
+
|
21 |
+
|
22 |
+
os.environ['REPLICATE_API_TOKEN'] = '16ea7157b65a155892e29298b6ddac479a12e819'
|
23 |
+
model_name = 'cjwbw/stable-diffusion-v2-inpainting'
|
24 |
+
model = replicate.models.get(model_name)
|
25 |
+
version = model.versions.get("f9bb0632bfdceb83196e85521b9b55895f8ff3d1d3b487fd1973210c0eb30bec")
|
26 |
|
27 |
|
28 |
def numpy_to_pil(images):
|
|
|
44 |
text=[text], images=[image], padding="max_length", return_tensors="pt"
|
45 |
).to(device)
|
46 |
|
47 |
+
outputs = model_clip(**inputs)
|
48 |
mask = torch.sigmoid(outputs.logits).cpu().detach().unsqueeze(-1).numpy()
|
49 |
|
50 |
mask_pil = numpy_to_pil(mask)[0].resize(image.size)
|
|
|
52 |
return mask_pil
|
53 |
|
54 |
|
55 |
+
def image_to_byte_array(image: Image) -> bytes:
|
56 |
+
# BytesIO is a file-like buffer stored in memory
|
57 |
+
imgByteArr = io.BytesIO()
|
58 |
+
# image.save expects a file-like as a argument
|
59 |
+
image.save(imgByteArr, format='PNG')
|
60 |
+
# Turn the BytesIO object back into a bytes object
|
61 |
+
#imgByteArr = imgByteArr.getvalue()
|
62 |
+
return imgByteArr
|
63 |
+
|
64 |
+
|
65 |
def predict(prompt, negative_prompt, image, obj2mask):
|
66 |
mask = get_mask(obj2mask, image)
|
67 |
image = image.convert("RGB").resize((512, 512))
|
68 |
mask_image = mask.convert("RGB").resize((512, 512))
|
69 |
mask_image = ImageOps.invert(mask_image)
|
70 |
+
# open("/home/tobias/WorkspageBE/replicate/tenis.png", "rb")
|
71 |
+
# io.BufferedReader(image_to_byte_array(image))
|
72 |
+
inputs = {
|
73 |
+
# Input prompt
|
74 |
+
'prompt': prompt,
|
75 |
+
|
76 |
+
# Inital image to generate variations of. Supproting images size with
|
77 |
+
# 512x512
|
78 |
+
'image': image_to_byte_array(image),
|
79 |
+
|
80 |
+
# Black and white image to use as mask for inpainting over the image
|
81 |
+
# provided. Black pixels are inpainted and white pixels are preserved
|
82 |
+
'mask': image_to_byte_array(mask_image),
|
83 |
+
|
84 |
+
# Prompt strength when using init image. 1.0 corresponds to full
|
85 |
+
# destruction of information in init image
|
86 |
+
'prompt_strength': 0.8,
|
87 |
+
|
88 |
+
# Number of images to output. Higher number of outputs may OOM.
|
89 |
+
# Range: 1 to 8
|
90 |
+
'num_outputs': 1,
|
91 |
+
|
92 |
+
# Number of denoising steps
|
93 |
+
# Range: 1 to 500
|
94 |
+
'num_inference_steps': 50,
|
95 |
+
|
96 |
+
# Scale for classifier-free guidance
|
97 |
+
# Range: 1 to 20
|
98 |
+
'guidance_scale': 7.5,
|
99 |
+
|
100 |
+
# Random seed. Leave blank to randomize the seed
|
101 |
+
# 'seed': ...,
|
102 |
+
}
|
103 |
+
|
104 |
+
output = version.predict(**inputs)
|
105 |
+
|
106 |
+
response = requests.get(output[0])
|
107 |
+
img_final = Image.open(BytesIO(response.content))
|
108 |
+
|
109 |
mask = mask_image.convert('L')
|
110 |
|
111 |
+
PIL.Image.composite(img_final, image, mask)
|
112 |
+
return (img_final)
|
113 |
|
114 |
|
115 |
def inference(prompt, negative_prompt, obj2mask, image_numpy):
|
|
|
124 |
with gr.Blocks() as demo:
|
125 |
with gr.Row():
|
126 |
with gr.Column():
|
127 |
+
prompt = gr.Textbox(label="Prompt", value="cinematic, advertisement, sharpe focus, ad, ads")
|
128 |
+
negative_prompt = gr.Textbox(label="Negative Prompt", value="text, written")
|
129 |
mask = gr.Textbox(label="Mask", value="shoe")
|
130 |
intput_img = gr.Image()
|
131 |
run = gr.Button(value="Generate")
|
app_v1.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 = 'cpu'
|
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()
|
requirements.txt
CHANGED
@@ -4,4 +4,4 @@ diffusers
|
|
4 |
gradio
|
5 |
torch
|
6 |
transformers
|
7 |
-
|
|
|
4 |
gradio
|
5 |
torch
|
6 |
transformers
|
7 |
+
replicate
|