Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import gradio as gr
|
2 |
import spaces
|
|
|
3 |
import cv2
|
4 |
from diffusers import AutoPipelineForInpainting
|
5 |
from transformers import pipeline
|
@@ -30,15 +31,10 @@ def get_most_similar_string(target_string, string_array):
|
|
30 |
best_match_ratio = similarity_ratio
|
31 |
return best_match
|
32 |
|
33 |
-
# Load
|
34 |
yoloModel = YOLO('yolov8x-seg.pt')
|
35 |
yoloModel.to('cpu')
|
36 |
|
37 |
-
sdxl = AutoPipelineForInpainting.from_pretrained(
|
38 |
-
"diffusers/stable-diffusion-xl-1.0-inpainting-0.1"
|
39 |
-
)
|
40 |
-
image_captioner = pipeline("image-to-text", model="Abdou/vit-swin-base-224-gpt2-image-captioning")
|
41 |
-
|
42 |
def getClasses(model, img1):
|
43 |
results = model([img1])
|
44 |
out = []
|
@@ -80,11 +76,14 @@ def joinClasses(classes):
|
|
80 |
|
81 |
def getSegments(yoloModel, img1):
|
82 |
classes, image, results1 = getClasses(yoloModel, img1)
|
|
|
|
|
83 |
allMask = joinClasses(classes)
|
84 |
return allMask
|
85 |
|
86 |
@spaces.GPU
|
87 |
-
def getDescript(
|
|
|
88 |
base64_img = image_to_base64(img1)
|
89 |
caption = image_captioner(base64_img)[0]['generated_text']
|
90 |
return caption
|
@@ -105,19 +104,22 @@ def rmGPT(caption, remove_class, change):
|
|
105 |
return ' '.join(arstr)
|
106 |
|
107 |
@spaces.GPU
|
108 |
-
def ChangeOBJ(
|
|
|
|
|
|
|
109 |
size = img1.size
|
110 |
-
image =
|
111 |
return image.resize((size[0], size[1]))
|
112 |
|
113 |
def full_pipeline(image, target, change):
|
114 |
img1 = Image.fromarray(image.astype('uint8'), 'RGB')
|
115 |
allMask = getSegments(yoloModel, img1)
|
116 |
target_to_remove = get_most_similar_string(target, list(allMask.keys()))
|
117 |
-
caption = getDescript(
|
118 |
response = rmGPT(caption, target_to_remove, change)
|
119 |
mask1 = allMask[target_to_remove]
|
120 |
-
remimg = ChangeOBJ(
|
121 |
return remimg, caption, response
|
122 |
|
123 |
iface = gr.Interface(
|
|
|
1 |
import gradio as gr
|
2 |
import spaces
|
3 |
+
import time
|
4 |
import cv2
|
5 |
from diffusers import AutoPipelineForInpainting
|
6 |
from transformers import pipeline
|
|
|
31 |
best_match_ratio = similarity_ratio
|
32 |
return best_match
|
33 |
|
34 |
+
# Load YOLO model on CPU
|
35 |
yoloModel = YOLO('yolov8x-seg.pt')
|
36 |
yoloModel.to('cpu')
|
37 |
|
|
|
|
|
|
|
|
|
|
|
38 |
def getClasses(model, img1):
|
39 |
results = model([img1])
|
40 |
out = []
|
|
|
76 |
|
77 |
def getSegments(yoloModel, img1):
|
78 |
classes, image, results1 = getClasses(yoloModel, img1)
|
79 |
+
im = Image.fromarray(image)
|
80 |
+
im.save('classes.jpg')
|
81 |
allMask = joinClasses(classes)
|
82 |
return allMask
|
83 |
|
84 |
@spaces.GPU
|
85 |
+
def getDescript(img1):
|
86 |
+
image_captioner = pipeline("image-to-text", model="Abdou/vit-swin-base-224-gpt2-image-captioning", device='cuda')
|
87 |
base64_img = image_to_base64(img1)
|
88 |
caption = image_captioner(base64_img)[0]['generated_text']
|
89 |
return caption
|
|
|
104 |
return ' '.join(arstr)
|
105 |
|
106 |
@spaces.GPU
|
107 |
+
def ChangeOBJ(img1, response, mask1):
|
108 |
+
sdxl = AutoPipelineForInpainting.from_pretrained(
|
109 |
+
"diffusers/stable-diffusion-xl-1.0-inpainting-0.1", torch_dtype=torch.float16
|
110 |
+
).to('cuda')
|
111 |
size = img1.size
|
112 |
+
image = sdxl(prompt=response, image=img1, mask_image=mask1).images[0]
|
113 |
return image.resize((size[0], size[1]))
|
114 |
|
115 |
def full_pipeline(image, target, change):
|
116 |
img1 = Image.fromarray(image.astype('uint8'), 'RGB')
|
117 |
allMask = getSegments(yoloModel, img1)
|
118 |
target_to_remove = get_most_similar_string(target, list(allMask.keys()))
|
119 |
+
caption = getDescript(img1)
|
120 |
response = rmGPT(caption, target_to_remove, change)
|
121 |
mask1 = allMask[target_to_remove]
|
122 |
+
remimg = ChangeOBJ(img1, response, mask1)
|
123 |
return remimg, caption, response
|
124 |
|
125 |
iface = gr.Interface(
|