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
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
import os
import random
import gradio as gr


import cv2
import torch
import numpy as np
from PIL import Image

from transformers import CLIPVisionModelWithProjection
from diffusers.utils import load_image
from diffusers.models import ControlNetModel
# from diffusers.image_processor import IPAdapterMaskProcessor
from insightface.app import FaceAnalysis
# import sys
# import glob
# import os
import io
import spaces

from pipeline_stable_diffusion_xl_instantid import StableDiffusionXLInstantIDPipeline, draw_kps

import pandas as pd
import json
import requests
from PIL import Image
from io import BytesIO


def resize_img(input_image, max_side=1280, min_side=1024, size=None, 
               pad_to_max_side=False, mode=Image.BILINEAR, base_pixel_number=64):
    
    w, h = input_image.size
    if size is not None:
        w_resize_new, h_resize_new = size
    else:
        ratio = min_side / min(h, w)
        w, h = round(ratio*w), round(ratio*h)
        ratio = max_side / max(h, w)
        input_image = input_image.resize([round(ratio*w), round(ratio*h)], mode)
        w_resize_new = (round(ratio * w) // base_pixel_number) * base_pixel_number
        h_resize_new = (round(ratio * h) // base_pixel_number) * base_pixel_number
    input_image = input_image.resize([w_resize_new, h_resize_new], mode)

    if pad_to_max_side:
        res = np.ones([max_side, max_side, 3], dtype=np.uint8) * 255
        offset_x = (max_side - w_resize_new) // 2
        offset_y = (max_side - h_resize_new) // 2
        res[offset_y:offset_y+h_resize_new, offset_x:offset_x+w_resize_new] = np.array(input_image)
        input_image = Image.fromarray(res)
    return input_image

def process_image_by_bbox_larger(input_image, bbox_xyxy, min_bbox_ratio=0.2):
    """
    Process an image based on a bounding box, cropping and resizing as necessary.

    Parameters:
    - input_image: PIL Image object.
    - bbox_xyxy: Tuple (x1, y1, x2, y2) representing the bounding box coordinates.

    Returns:
    - A processed image cropped and resized to 1024x1024 if the bounding box is valid,
      or None if the bounding box does not meet the required size criteria.
    """
    # Constants
    target_size = 1024
    # min_bbox_ratio = 0.2  # Bounding box should be at least 20% of the crop

    # Extract bounding box coordinates
    x1, y1, x2, y2 = bbox_xyxy
    bbox_w = x2 - x1
    bbox_h = y2 - y1

    # Calculate the area of the bounding box
    bbox_area = bbox_w * bbox_h

    # Start with the smallest square crop that allows bbox to be at least 20% of the crop area
    crop_size = max(bbox_w, bbox_h)
    initial_crop_area = crop_size * crop_size
    while (bbox_area / initial_crop_area) < min_bbox_ratio:
        crop_size += 10  # Gradually increase until bbox is at least 20% of the area
        initial_crop_area = crop_size * crop_size

    # Once the minimum condition is satisfied, try to expand the crop further
    max_possible_crop_size = min(input_image.width, input_image.height)
    while crop_size < max_possible_crop_size:
        # Calculate a potential new area
        new_crop_size = crop_size + 10
        new_crop_area = new_crop_size * new_crop_size
        if (bbox_area / new_crop_area) < min_bbox_ratio:
            break  # Stop if expanding further violates the 20% rule
        crop_size = new_crop_size

    # Determine the center of the bounding box
    center_x = (x1 + x2) // 2
    center_y = (y1 + y2) // 2

    # Calculate the crop coordinates centered around the bounding box
    crop_x1 = max(0, center_x - crop_size // 2)
    crop_y1 = max(0, center_y - crop_size // 2)
    crop_x2 = min(input_image.width, crop_x1 + crop_size)
    crop_y2 = min(input_image.height, crop_y1 + crop_size)

    # Ensure the crop is square, adjust if it goes out of image bounds
    if crop_x2 - crop_x1 != crop_y2 - crop_y1:
        side_length = min(crop_x2 - crop_x1, crop_y2 - crop_y1)
        crop_x2 = crop_x1 + side_length
        crop_y2 = crop_y1 + side_length

    # Crop the image
    cropped_image = input_image.crop((crop_x1, crop_y1, crop_x2, crop_y2))

    # Resize the cropped image to 1024x1024
    resized_image = cropped_image.resize((target_size, target_size), Image.LANCZOS)

    return resized_image

def calc_emb_cropped(image, app):
    face_image = image.copy()

    face_info = app.get(cv2.cvtColor(np.array(face_image), cv2.COLOR_RGB2BGR))

    face_info = face_info[0]

    cropped_face_image = process_image_by_bbox_larger(face_image, face_info["bbox"], min_bbox_ratio=0.2)

    return cropped_face_image

def process_benchmark_csv(banchmark_csv_path):
    # Reading the first CSV file into a DataFrame
    df = pd.read_csv(banchmark_csv_path)

    # Drop any unnamed columns
    df = df.loc[:, ~df.columns.str.contains('^Unnamed')]

    # Drop columns with all NaN values
    df.dropna(axis=1, how='all', inplace=True)

    # Drop rows with all NaN values
    df.dropna(axis=0, how='all', inplace=True)

    df = df.loc[df['High resolution'] == 1]

    df.reset_index(drop=True, inplace=True)

    return df

def make_canny_condition(image, min_val=100, max_val=200, w_bilateral=True):
    if w_bilateral:
        image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2GRAY)
        bilateral_filtered_image = cv2.bilateralFilter(image, d=9, sigmaColor=75, sigmaSpace=75)
        image = cv2.Canny(bilateral_filtered_image, min_val, max_val)
    else:
        image = np.array(image)
        image = cv2.Canny(image, min_val, max_val)
    image = image[:, :, None]
    image = np.concatenate([image, image, image], axis=2)
    image = Image.fromarray(image)
    return image


default_negative_prompt = "Logo,Watermark,Text,Ugly,Morbid,Extra fingers,Poorly drawn hands,Mutation,Blurry,Extra limbs,Gross proportions,Missing arms,Mutated hands,Long neck,Duplicate,Mutilated,Mutilated hands,Poorly drawn face,Deformed,Bad anatomy,Cloned face,Malformed limbs,Missing legs,Too many fingers"

# Load face detection and recognition package
app = FaceAnalysis(name='antelopev2', root='./', providers=['CPUExecutionProvider'])
app.prepare(ctx_id=0, det_size=(640, 640))

base_dir = "./instantID_ckpt/checkpoint_174000"
face_adapter = f'{base_dir}/pytorch_model.bin'
controlnet_path = f'{base_dir}/controlnet'
base_model_path = f'briaai/BRIA-2.3'
resolution = 1024

controlnet_lnmks = ControlNetModel.from_pretrained(controlnet_path, torch_dtype=torch.float16)

controlnet_canny = ControlNetModel.from_pretrained("briaai/BRIA-2.3-ControlNet-Canny",
                                                   torch_dtype=torch.float16)

controlnet = [controlnet_lnmks, controlnet_canny]

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

image_encoder = CLIPVisionModelWithProjection.from_pretrained(
        '/home/ubuntu/BRIA-2.3-InstantID/ip_adapter/image_encoder',
        torch_dtype=torch.float16,
    )

pipe = StableDiffusionXLInstantIDPipeline.from_pretrained(
        base_model_path,
        controlnet=controlnet,
        torch_dtype=torch.float16,
        image_encoder=image_encoder # For compatibility issues - needs to be there
    )

pipe = pipe.to(device)

use_native_ip_adapter = True
pipe.use_native_ip_adapter=use_native_ip_adapter

pipe.load_ip_adapter_instantid(face_adapter)

clip_embeds=None


Loras_dict = {
    "":"",
    "Vangogh_Vanilla": "bold, dramatic brush strokes, vibrant colors, swirling patterns, intense, emotionally charged paintings of",
    "Avatar_internlm": "2d anime sketch avatar of",
    # "Tomer_Hanuka_V3": "Fluid lines",
    "Storyboards": "Illustration style for storyboarding",
    "3D_illustration": "3D object illustration, abstract",
    # "beetl_general_death_style_v2": "a pale, dead, unnatural color face with dark circles around the eyes",
    "Characters": "gaming vector Art"
    }

lora_names = Loras_dict.keys()

lora_base_path = "./LoRAs"

def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
    if randomize_seed:
        seed = random.randint(0, 99999999)
    return seed


@spaces.GPU
def generate_image(image_path, prompt, num_steps, guidance_scale, seed, num_images, ip_adapter_scale=0.8, kps_scale=0.6, canny_scale=0.4, lora_name="", lora_scale=0.7, progress=gr.Progress(track_tqdm=True)):
    if image_path is None:
        raise gr.Error(f"Cannot find any input face image! Please upload a face image.")
    
    # img = np.array(Image.open(image_path))[:,:,::-1]
    img = Image.open(image_path)

    face_image_orig = img #Image.open(BytesIO(response.content))
    face_image_cropped = calc_emb_cropped(face_image_orig, app)
    face_image = resize_img(face_image_cropped, max_side=resolution, min_side=resolution)
    # face_image_padded = resize_img(face_image_cropped, max_side=resolution, min_side=resolution, pad_to_max_side=True)
    face_info = app.get(cv2.cvtColor(np.array(face_image), cv2.COLOR_RGB2BGR))
    face_info = sorted(face_info, key=lambda x:(x['bbox'][2]-x['bbox'][0])*(x['bbox'][3]-x['bbox'][1]))[-1] # only use the maximum face
    face_emb = face_info['embedding']
    face_kps = draw_kps(face_image, face_info['kps'])

    if canny_scale>0.0:
        # Convert PIL image to a file-like object
        image_file = io.BytesIO()
        face_image_cropped.save(image_file, format='JPEG')  # Save in the desired format (e.g., 'JPEG' or 'PNG')
        image_file.seek(0)  # Move to the start of the BytesIO stream

        url = "https://engine.prod.bria-api.com/v1/background/remove"

        payload = {}
        files = [
            ('file', ('image_name.jpeg', image_file, 'image/jpeg'))  # Specify file name, file-like object, and MIME type
        ]
        headers = {
        'api_token': 'a10d6386dd6a11ebba800242ac130004'
        }

        response = requests.request("POST", url, headers=headers, data=payload, files=files)

        print(response.text)

        response_json = json.loads(response.content.decode('utf-8'))

        img = requests.get(response_json['result_url'])

        processed_image = Image.open(io.BytesIO(img.content))

        # Assuming `processed_image` is the RGBA image returned
        if processed_image.mode == 'RGBA':
            # Create a white background image
            white_background = Image.new("RGB", processed_image.size, (255, 255, 255))
            # Composite the RGBA image over the white background
            face_image = Image.alpha_composite(white_background.convert('RGBA'), processed_image).convert('RGB')
        else:
            face_image = processed_image.convert('RGB')  # If already RGB, just ensure mode is correct

        canny_img = make_canny_condition(face_image, min_val=20, max_val=40, w_bilateral=True)
                    
    generator = torch.Generator(device=device).manual_seed(seed)
    
    if lora_name != "":
        lora_path = os.path.join(lora_base_path, lora_name, "pytorch_lora_weights.safetensors")
        pipe.load_lora_weights(lora_path)
        pipe.fuse_lora(lora_scale)  
        pipe.enable_lora()

        lora_prefix = Loras_dict[lora_name]

        prompt = f"{lora_prefix} {prompt}"
        

    print("Start inference...")    
    images = pipe(
        prompt = prompt,
        negative_prompt = default_negative_prompt,
        image_embeds = face_emb,
        image = [face_kps, canny_img] if canny_scale>0.0 else face_kps,
        controlnet_conditioning_scale = [kps_scale, canny_scale] if canny_scale>0.0 else kps_scale,
        control_guidance_end = [1.0, 1.0] if canny_scale>0.0 else 1.0,
        ip_adapter_scale = ip_adapter_scale,
        num_inference_steps = num_steps,
        guidance_scale = guidance_scale,
        generator = generator,
        visual_prompt_embds = clip_embeds,
        cross_attention_kwargs = None,
        num_images_per_prompt=num_images,
    ).images #[0]

    if lora_name != "":
        pipe.disable_lora()
        pipe.unfuse_lora()
        pipe.unload_lora_weights()

    return images

### Description
title = r"""
<h1>Bria-2.3 ID preservation</h1>
"""

description = r"""
<b>🤗 Gradio demo</b> for bria ID preservation.<br>

Steps:<br>
1. Upload an image with a face. If multiple faces are detected, we use the largest one. For images with already tightly cropped faces, detection may fail, try images with a larger margin.
2. Click <b>Submit</b> to generate new images of the subject.
"""

Footer = r"""
Enjoy
"""

css = '''
.gradio-container {width: 85% !important}
'''
with gr.Blocks(css=css) as demo:

    # description
    gr.Markdown(title)
    gr.Markdown(description)

    with gr.Row():
        with gr.Column():
            
            # upload face image
            img_file = gr.Image(label="Upload a photo with a face", type="filepath")
            
            # Textbox for entering a prompt
            prompt = gr.Textbox(
            label="Prompt", 
            placeholder="Enter your prompt here", 
            info="Describe what you want to generate or modify in the image."
            )
            
            lora_name = gr.Dropdown(choices=lora_names, label="LoRA", value="",  info="Select a LoRA name from the list, not selecting any will disable LoRA.")

            submit = gr.Button("Submit", variant="primary")

            # use_lcm = gr.Checkbox(
            #     label="Use LCM-LoRA to accelerate sampling", value=False,
            #     info="Reduces sampling steps significantly, but may decrease quality.",
            # )
            
            with gr.Accordion(open=False, label="Advanced Options"):
                num_steps = gr.Slider( 
                    label="Number of sample steps",
                    minimum=1,
                    maximum=100,
                    step=1,
                    value=30,
                )
                guidance_scale = gr.Slider(
                    label="Guidance scale",
                    minimum=0.1,
                    maximum=10.0,
                    step=0.1,
                    value=5.0,
                )
                num_images = gr.Slider(
                    label="Number of output images",
                    minimum=1,
                    maximum=3,
                    step=1,
                    value=1,
                )
                ip_adapter_scale = gr.Slider(
                    label="ip adapter scale",
                    minimum=0.0,
                    maximum=1.0,
                    step=0.01,
                    value=0.8,
                )
                kps_scale = gr.Slider(
                    label="kps control scale",
                    minimum=0.0,
                    maximum=1.0,
                    step=0.01,
                    value=0.6,
                )
                canny_scale = gr.Slider(
                    label="canny control scale",
                    minimum=0.0,
                    maximum=1.0,
                    step=0.01,
                    value=0.4,
                )
                seed = gr.Slider(
                    label="Seed",
                    minimum=0,
                    maximum=99999999,
                    step=1,
                    value=0,
                )
                seed = gr.Slider(
                    label="lora_scale",
                    minimum=0.0,
                    maximum=1.0,
                    step=0.01,
                    value=0.7,
                )
                randomize_seed = gr.Checkbox(label="Randomize seed", value=True)

        with gr.Column():
            gallery = gr.Gallery(label="Generated Images")

        submit.click(
            fn=randomize_seed_fn,
            inputs=[seed, randomize_seed],
            outputs=seed,
            queue=False,
            api_name=False,
        ).then(
            fn=generate_image,
            inputs=[img_file, prompt, num_steps, guidance_scale, seed, num_images, ip_adapter_scale, kps_scale, canny_scale, lora_name],
            outputs=[gallery]
        )

    # use_lcm.input(
    #         fn=toggle_lcm_ui,
    #         inputs=[use_lcm],
    #         outputs=[num_steps, guidance_scale],
    #         queue=False,
    #     )       
    
    # gr.Examples(
    #     examples=get_example(),
    #     inputs=[img_file],
    #     run_on_click=True,
    #     fn=run_example,
    #     outputs=[gallery],
    # )
    
    gr.Markdown(Footer)

demo.launch()