File size: 8,814 Bytes
003d203
 
 
94d1b20
0e6c023
b9bde42
aa16383
dcfda89
94d1b20
b9bde42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dc95e97
94d1b20
 
 
 
3aa0053
 
 
 
 
 
 
 
 
aa16383
 
 
dcfda89
 
 
 
aa16383
dcfda89
94d1b20
dcfda89
 
 
 
aa16383
dcfda89
5fb3d3c
 
 
 
 
aa16383
 
 
ba5420f
aa16383
dcfda89
 
 
 
 
30b17a2
 
aa16383
 
dcfda89
aa16383
94d1b20
 
 
 
 
 
 
 
 
 
 
aa16383
94d1b20
9a3b780
aa16383
 
ba5420f
 
 
 
 
 
 
 
 
94d1b20
 
 
 
 
 
 
 
 
 
 
ba5420f
94d1b20
ba5420f
 
 
ccd0549
94d1b20
 
 
 
 
b9bde42
 
 
 
0e6c023
 
 
94d1b20
 
 
 
b9bde42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a181ad3
2ca2e3d
 
94d1b20
 
 
 
 
 
b9bde42
 
 
 
 
 
aa16383
 
 
2ca2e3d
c74d7e5
b9bde42
94d1b20
 
c74d7e5
94d1b20
2ca2e3d
94d1b20
9702cba
94d1b20
aa16383
 
 
dcfda89
 
b9bde42
94d1b20
 
 
 
 
 
 
 
dcfda89
94d1b20
dcfda89
94d1b20
6039455
aa16383
0e6c023
94d1b20
ba5420f
 
 
b9bde42
94d1b20
 
 
 
 
ba5420f
94d1b20
ba5420f
94d1b20
6039455
94d1b20
ba5420f
 
b9bde42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0e6c023
b9bde42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94d1b20
0e6c023
 
cce19ac
 
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import gradio as gr
import spaces
import torch
from loadimg import load_img
from torchvision import transforms
from transformers import AutoModelForImageSegmentation, pipeline
from diffusers import FluxFillPipeline
from PIL import Image, ImageOps

# from sam2.sam2_image_predictor import SAM2ImagePredictor
import numpy as np
from simple_lama_inpainting import SimpleLama
from contextlib import contextmanager


@contextmanager
def float32_high_matmul_precision():
    torch.set_float32_matmul_precision("high")
    try:
        yield
    finally:
        torch.set_float32_matmul_precision("highest")


pipe = FluxFillPipeline.from_pretrained(
    "black-forest-labs/FLUX.1-Fill-dev", torch_dtype=torch.bfloat16
).to("cuda")

birefnet = AutoModelForImageSegmentation.from_pretrained(
    "ZhengPeng7/BiRefNet", trust_remote_code=True
)
birefnet.to("cuda")

transform_image = transforms.Compose(
    [
        transforms.Resize((1024, 1024)),
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
    ]
)


def prepare_image_and_mask(
    image,
    padding_top=0,
    padding_bottom=0,
    padding_left=0,
    padding_right=0,
):
    image = load_img(image).convert("RGB")
    # expand image (left,top,right,bottom)
    background = ImageOps.expand(
        image,
        border=(padding_left, padding_top, padding_right, padding_bottom),
        fill="white",
    )
    mask = Image.new("RGB", image.size, "black")
    mask = ImageOps.expand(
        mask,
        border=(padding_left, padding_top, padding_right, padding_bottom),
        fill="white",
    )
    return background, mask


def outpaint(
    image,
    padding_top=0,
    padding_bottom=0,
    padding_left=0,
    padding_right=0,
    prompt="",
    num_inference_steps=28,
    guidance_scale=50,
):
    background, mask = prepare_image_and_mask(
        image, padding_top, padding_bottom, padding_left, padding_right
    )

    result = pipe(
        prompt=prompt,
        height=background.height,
        width=background.width,
        image=background,
        mask_image=mask,
        num_inference_steps=num_inference_steps,
        guidance_scale=guidance_scale,
    ).images[0]

    result = result.convert("RGBA")

    return result


def inpaint(
    image,
    mask,
    prompt="",
    num_inference_steps=28,
    guidance_scale=50,
):
    background = image.convert("RGB")
    mask = mask.convert("L")

    result = pipe(
        prompt=prompt,
        height=background.height,
        width=background.width,
        image=background,
        mask_image=mask,
        num_inference_steps=num_inference_steps,
        guidance_scale=guidance_scale,
    ).images[0]

    result = result.convert("RGBA")

    return result


def rmbg(image=None, url=None):
    if image is None:
        image = url
    image = load_img(image).convert("RGB")
    image_size = image.size
    input_images = transform_image(image).unsqueeze(0).to("cuda")
    with float32_high_matmul_precision():
        # Prediction
        with torch.no_grad():
            preds = birefnet(input_images)[-1].sigmoid().cpu()
    pred = preds[0].squeeze()
    pred_pil = transforms.ToPILImage()(pred)
    mask = pred_pil.resize(image_size)
    image.putalpha(mask)
    return image


# def mask_generation(image=None, d=None):
#     # use bfloat16 for the entire notebook
#     # torch.autocast("cuda", dtype=torch.bfloat16).__enter__()
#     # # turn on tfloat32 for Ampere GPUs (https://pytorch.org/docs/stable/notes/cuda.html#tensorfloat-32-tf32-on-ampere-devices)
#     # if torch.cuda.get_device_properties(0).major >= 8:
#     #     torch.backends.cuda.matmul.allow_tf32 = True
#     #     torch.backends.cudnn.allow_tf32 = True
#     d = eval(d)  # convert this to dictionary
#     with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16):
#         predictor = SAM2ImagePredictor.from_pretrained("facebook/sam2.1-hiera-large")
#         predictor.set_image(image)
#         input_point = np.array(d["input_points"])
#         input_label = np.array(d["input_labels"])
#         masks, scores, logits = predictor.predict(
#             point_coords=input_point,
#             point_labels=input_label,
#             multimask_output=True,
#         )
#     sorted_ind = np.argsort(scores)[::-1]
#     masks = masks[sorted_ind]
#     scores = scores[sorted_ind]
#     logits = logits[sorted_ind]

#     out = []
#     for i in range(len(masks)):
#         m = Image.fromarray(masks[i] * 255).convert("L")
#         comp = Image.composite(image, m, m)
#         out.append((comp, f"image {i}"))

#     return out


def erase(image=None, mask=None):
    simple_lama = SimpleLama()
    image = load_img(image)
    mask = load_img(mask).convert("L")
    return simple_lama(image, mask)


# Initialize Whisper model
whisper = pipeline(
    task="automatic-speech-recognition",
    model="openai/whisper-large-v3",
    chunk_length_s=30,
    device="cuda" if torch.cuda.is_available() else "cpu",
)


def transcribe(audio, task="transcribe"):
    if audio is None:
        raise gr.Error("No audio file submitted!")

    text = whisper(
        audio, batch_size=8, generate_kwargs={"task": task}, return_timestamps=True
    )["text"]
    return text


@spaces.GPU(duration=120)
def main(*args):
    api_num = args[0]
    args = args[1:]
    if api_num == 1:
        return rmbg(*args)
    elif api_num == 2:
        return outpaint(*args)
    elif api_num == 3:
        return inpaint(*args)
    # elif api_num == 4:
    #     return mask_generation(*args)
    elif api_num == 5:
        return erase(*args)
    elif api_num == 6:
        return transcribe(*args)


rmbg_tab = gr.Interface(
    fn=main,
    inputs=[
        gr.Number(1, interactive=False),
        "image",
        gr.Text("", label="url"),
    ],
    outputs=["image"],
    api_name="rmbg",
    examples=[[1, "./assets/Inpainting mask.png", ""]],
    cache_examples=False,
    description="pass an image or a url of an image",
)

outpaint_tab = gr.Interface(
    fn=main,
    inputs=[
        gr.Number(2, interactive=False),
        gr.Image(label="image", type="pil"),
        gr.Number(label="padding top"),
        gr.Number(label="padding bottom"),
        gr.Number(label="padding left"),
        gr.Number(label="padding right"),
        gr.Text(label="prompt"),
        gr.Number(value=50, label="num_inference_steps"),
        gr.Number(value=28, label="guidance_scale"),
    ],
    outputs=["image"],
    api_name="outpainting",
    examples=[[2, "./assets/rocket.png", 100, 0, 0, 0, "", 50, 28]],
    cache_examples=False,
)


inpaint_tab = gr.Interface(
    fn=main,
    inputs=[
        gr.Number(3, interactive=False),
        gr.Image(label="image", type="pil"),
        gr.Image(label="mask", type="pil"),
        gr.Text(label="prompt"),
        gr.Number(value=50, label="num_inference_steps"),
        gr.Number(value=28, label="guidance_scale"),
    ],
    outputs=["image"],
    api_name="inpaint",
    examples=[[3, "./assets/rocket.png", "./assets/Inpainting mask.png"]],
    cache_examples=False,
    description="it is recommended that you use https://github.com/la-voliere/react-mask-editor when creating an image mask in JS and then inverse it before sending it to this space",
)


# sam2_tab = gr.Interface(
#     main,
#     inputs=[
#         gr.Number(4, interactive=False),
#         gr.Image(type="pil"),
#         gr.Text(),
#     ],
#     outputs=gr.Gallery(),
#     examples=[
#         [
#             4,
#             "./assets/truck.jpg",
#             '{"input_points": [[500, 375], [1125, 625]], "input_labels": [1, 0]}',
#         ]
#     ],
#     api_name="sam2",
#     cache_examples=False,
# )

erase_tab = gr.Interface(
    main,
    inputs=[
        gr.Number(5, interactive=False),
        gr.Image(type="pil"),
        gr.Image(type="pil"),
    ],
    outputs=gr.Image(),
    examples=[
        [
            5,
            "./assets/rocket.png",
            "./assets/Inpainting mask.png",
        ]
    ],
    api_name="erase",
    cache_examples=False,
)

transcribe_tab = gr.Interface(
    fn=main,
    inputs=[
        gr.Number(6, interactive=False),
        gr.Audio(type="filepath"),
        gr.Radio(["transcribe", "translate"], label="Task", value="transcribe"),
    ],
    outputs="text",
    api_name="transcribe",
    description="Upload an audio file to extract text using Whisper Large V3",
)

demo = gr.TabbedInterface(
    [
        rmbg_tab,
        outpaint_tab,
        inpaint_tab,
        #  sam2_tab,
        erase_tab,
        transcribe_tab,
    ],
    [
        "remove background",
        "outpainting",
        "inpainting",
        #  "sam2",
        "erase",
        "transcribe",
    ],
    title="Utilities that require GPU",
)


demo.launch()