File size: 9,060 Bytes
cf550ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3decff2
cf550ac
 
 
 
96f7b06
cf550ac
 
 
 
 
 
 
 
 
 
 
 
 
 
b31aae0
cf550ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
faad375
3decff2
 
 
 
 
 
56ed9bc
 
faad375
 
3decff2
9ecdb2a
3decff2
 
faad375
cf550ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
faad375
cf550ac
 
 
412118f
cf550ac
 
412118f
cf550ac
 
 
 
 
 
 
 
 
 
 
 
 
 
c162f2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
412118f
c162f2f
 
412118f
c162f2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf550ac
 
 
d272f9e
fef9809
cf550ac
 
 
 
 
 
 
 
 
d272f9e
fef9809
cf550ac
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!pip install torch torchvision requests gdown matplotlib opencv-python Pillow==8.0.0 gradio cvzone
import cv2
import gradio as gr
import os
from PIL import Image
import numpy as np
import torch
from torch.autograd import Variable
from torchvision import transforms
import torch.nn.functional as F
import gdown
import matplotlib.pyplot as plt
import warnings
import cv2
import matplotlib.pyplot as plt
import cvzone
import torch
import numpy as np
import requests
from io import BytesIO
import requests
import gradio as gr
import base64
import os
import requests
import io
from PIL import Image
from PIL import ImageOps
from PIL import ImageFilter
from PIL import ImageChops
import json
from openai import OpenAI
warnings.filterwarnings("ignore")

engine_id = "stable-inpainting-512-v2-0"
api_host = os.getenv('API_HOST', 'https://api.stability.ai')
api_key = os.environ['Stability_Key']

if api_key is None:
    raise Exception("Missing Stability API key.")

os.system("git clone https://github.com/xuebinqin/DIS")
os.system("mv DIS/IS-Net/* .")

from data_loader_cache import normalize, im_reader, im_preprocess
from models import *

device = 'cuda' if torch.cuda.is_available() else 'cpu'

if not os.path.exists("saved_models"):
    os.mkdir("saved_models")
    MODEL_PATH_URL = "https://drive.google.com/uc?id=1xRaOTDapXoicYGOk-fQk5eSKOkjZ9Cni"
    gdown.download(MODEL_PATH_URL, "saved_models/isnet.pth", use_cookies=False)

class GOSNormalize(object):
    '''
    Normalize the Image using torch.transforms
    '''
    def __init__(self, mean=[0.485,0.456,0.406], std=[0.229,0.224,0.225]):
        self.mean = mean
        self.std = std

    def __call__(self,image):
        image = normalize(image,self.mean,self.std)
        return image


transform =  transforms.Compose([GOSNormalize([0.5,0.5,0.5],[1.0,1.0,1.0])])

def load_image(im_path, hypar):
    im = im_reader(im_path)
    im, im_shp = im_preprocess(im, hypar["cache_size"])
    im = torch.divide(im,255.0)
    shape = torch.from_numpy(np.array(im_shp))
    return transform(im).unsqueeze(0), shape.unsqueeze(0)


def build_model(hypar,device):
    net = hypar["model"]

    if(hypar["model_digit"]=="half"):
        net.half()
        for layer in net.modules():
            if isinstance(layer, nn.BatchNorm2d):
                layer.float()

    net.to(device)

    if(hypar["restore_model"]!=""):
        net.load_state_dict(torch.load(hypar["model_path"]+"/"+hypar["restore_model"], map_location=device))
        net.to(device)
    net.eval()
    return net

def predict(net,  inputs_val, shapes_val, hypar, device):
    '''
    Given an Image, predict the mask
    '''
    net.eval()

    if(hypar["model_digit"]=="full"):
        inputs_val = inputs_val.type(torch.FloatTensor)
    else:
        inputs_val = inputs_val.type(torch.HalfTensor)


    inputs_val_v = Variable(inputs_val, requires_grad=False).to(device)

    ds_val = net(inputs_val_v)[0]

    pred_val = ds_val[0][0,:,:,:]

    pred_val = torch.squeeze(F.upsample(torch.unsqueeze(pred_val,0),(shapes_val[0][0],shapes_val[0][1]),mode='bilinear'))

    ma = torch.max(pred_val)
    mi = torch.min(pred_val)
    pred_val = (pred_val-mi)/(ma-mi)

    if device == 'cuda': torch.cuda.empty_cache()
    return (pred_val.detach().cpu().numpy()*255).astype(np.uint8)


hypar = {}


hypar["model_path"] ="./saved_models"
hypar["restore_model"] = "isnet.pth"
hypar["interm_sup"] = False

hypar["model_digit"] = "full"
hypar["seed"] = 0

hypar["cache_size"] = [1024, 1024]

hypar["input_size"] = [1024, 1024]
hypar["crop_size"] = [1024, 1024]

hypar["model"] = ISNetDIS()

net = build_model(hypar, device)

def round_to_multiple(number, multiple,limit):
    return max(multiple, min(limit, number - number % multiple))

def getPrompt(desc):
    client = OpenAI(api_key = os.environ['OpenApi_Key'])
    #openai.api_key = os.environ['OpenApi_Key']
    prompt = client.chat.completions.create(
    model="gpt-3.5-turbo",
    messages=[
    {"role": "system", "content": '''You are a bot that converts user inputs into stable diffusion promts for a background replacer. For example:
                               User: Add trees and flowers in the background.
                               Prompt Generated: Trees, Flowers, High quality, Ultra HD
                               Keep the prompt within 20 words'''},
                                {"role": "user", "content": desc},
  ]
).choices[0].message.content
    print(prompt)
    return prompt

def inference2(image: Image,prompt):
  image_path = image
  image_tensor, orig_size = load_image(image_path, hypar)
  mask = predict(net, image_tensor, orig_size, hypar, device)

  pil_mask = Image.fromarray(mask).convert("L")
  ogimg = Image.open(image)

  new_width1 = round_to_multiple(ogimg.width, 64,704)
  new_height1 = round_to_multiple(ogimg.height, 64,704)
  init_img = ogimg.resize((new_width1, new_height1))

  new_height = round_to_multiple(int(init_img.height * 1.45), 64,1024)
  new_width = init_img.width

  new_img = Image.new('RGB', (new_width, new_height), (1, 1, 1))

  new_img.paste(init_img, (0, int(new_height * 0.2)))

  mask_img = pil_mask.resize((new_width1, new_height1))
  mask_img2 = mask_img


  output_buffer = io.BytesIO()
  new_img.save(output_buffer, format="PNG")

  resized_image_bytes = output_buffer.getvalue()

  new_img = Image.new('RGB', (new_width, new_height), (1, 1, 1))

  new_img.paste(mask_img, (0, int(new_height * 0.2)))


  output_buffer2 = io.BytesIO()
  new_img.save(output_buffer2, format="PNG")

  resized_image_bytes2 = output_buffer2.getvalue()

  response = requests.post(
    f"{api_host}/v1/generation/{engine_id}/image-to-image/masking",
    headers={
        "Accept": 'application/json',
        "Authorization": f"Bearer {api_key}"
    },
    files={
        'init_image': resized_image_bytes,
        'mask_image': resized_image_bytes2,
    },
    data={
        "mask_source": "MASK_IMAGE_BLACK",
        "text_prompts[0][text]": getPrompt(prompt),
        "text_prompts[0][weight]": 1,
        "text_prompts[1][text]": "text, distorted text, distortion, bottles, bluriness",
        "text_prompts[1][weight]": -1,
        "cfg_scale": 9,
        "clip_guidance_preset": "FAST_GREEN",
        "samples": 2,
        "steps": 40,
        "style_preset": "photographic",
    }
)

  if response.status_code != 200:
      raise Exception("Non-200 response: " + str(response.text))

  data = response.json()

  image_data1 = data["artifacts"][0]["base64"]

  image_bytes = base64.b64decode(image_data1)
  generated_image = Image.open(io.BytesIO(image_bytes))

  generated_imaget = generated_image.resize((int(generated_image.width*0.8),int(generated_image.height*0.8)))

  generated_image.paste(generated_imaget, (int(new_height * 0.08), int(new_height * 0.1)))

  output_buffer = io.BytesIO()
  generated_image.save(output_buffer, format="PNG")

  resized_image_bytes = output_buffer.getvalue()

  response = requests.post(
    f"{api_host}/v1/generation/{engine_id}/image-to-image/masking",
    headers={
        "Accept": 'application/json',
        "Authorization": f"Bearer {api_key}"
    },
    files={
        'init_image': resized_image_bytes,
        'mask_image': resized_image_bytes2,
    },
    data={
        "mask_source": "MASK_IMAGE_BLACK",
        "text_prompts[0][text]": getPrompt(prompt),
        "text_prompts[0][weight]": 1,
        "text_prompts[1][text]": "white, patches, patch, text, distorted text, distortion, bluriness ",
        "text_prompts[1][weight]": -1,
        "cfg_scale": 9,
        "clip_guidance_preset": "FAST_GREEN",
        "samples": 2,
        "steps": 50,
        "style_preset": "photographic",
        #"sampler": "DDPM"
    }
)

  if response.status_code != 200:
      raise Exception("Non-200 response: " + str(response.text))

  data = response.json()

  image_data1 = data["artifacts"][0]["base64"]

  image_bytes = base64.b64decode(image_data1)
  generated_image = Image.open(io.BytesIO(image_bytes))

  im_rgb = Image.open(image).convert("RGB")
  im_rgba = im_rgb.copy()
  im_rgba.putalpha(pil_mask)
  im_rgba = im_rgba.resize((int(new_width1*1),int(new_height1*1)), Image.ANTIALIAS)
  generated_image.paste(im_rgba, (0, int(new_height * 0.2)), im_rgba)

  image_data2 = data["artifacts"][1]["base64"]

  image_bytes = base64.b64decode(image_data2)
  generated_image2 = Image.open(io.BytesIO(image_bytes))

  im_rgb = Image.open(image).convert("RGB")
  im_rgba = im_rgb.copy()
  im_rgba.putalpha(pil_mask)
  im_rgba = im_rgba.resize((int(new_width1*1),int(new_height1*1)), Image.ANTIALIAS)
  generated_image2.paste(im_rgba, (0, int(new_height * 0.2)), im_rgba)

  return generated_image,generated_image2

interface = gr.Interface(
    fn=inference2,
    inputs=[gr.Image(type='filepath'),
            gr.Textbox(label = 'prompt')],
    outputs=["image","image"],
    title="For best results, use square images and make sure the subject is in the centre",
    allow_flagging='never',
    theme="default",
    cache_examples=False,
    ).launch( debug=True, share=False)