spdraptor commited on
Commit
e76f4a8
1 Parent(s): 7f68b40

Upload 4 files

Browse files
Files changed (4) hide show
  1. app (2).py +53 -0
  2. modules_controlnetSD.py +42 -0
  3. requirements (1).txt +12 -0
  4. utils_inpaint.py +35 -0
app (2).py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #Script added by SPDraptor
2
+ import spaces
3
+ from typing import Optional
4
+ import subprocess
5
+ subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
6
+ import torch
7
+ print("cuda present = ",torch.cuda.is_available())
8
+ import os
9
+ import sys
10
+ import gradio as gr
11
+ from PIL import Image
12
+
13
+ from modules import masking_module
14
+ from modules import controlnetSD
15
+ # import modules.inpaint.utils_inpaint
16
+
17
+
18
+
19
+ DESCRIPTION = "Welcome to Raptor APIs"
20
+
21
+ css = """
22
+ #output {
23
+ height: 500px;
24
+ overflow: auto;
25
+ border: 1px solid #ccc;
26
+ }
27
+ """
28
+
29
+ with gr.Blocks(css=css) as demo:
30
+ gr.Markdown(DESCRIPTION)
31
+ with gr.Tab(label="OBJ_mask"):
32
+ with gr.Row():
33
+ with gr.Column():
34
+ image = gr.Image(label="Input main Picture")
35
+ image_object = gr.Textbox(label="object name")
36
+ mask_btn = gr.Button(value="createMask")
37
+
38
+ with gr.Column():
39
+ output_mask = gr.Image(label="mask")
40
+ mask_btn.click(masking_module.masking_process,inputs=[image,image_object],outputs=output_mask,api_name="masking_step")
41
+ with gr.Tab(label="img_inpaint"):
42
+ with gr.Row():
43
+ with gr.Column():
44
+ org_image = gr.Image(label="Input main Picture")
45
+ mask = gr.Image(label="Input mask Picture")
46
+ prompt = gr.Textbox(label="prompt")
47
+ mask_btn = gr.Button(value="replace")
48
+
49
+ with gr.Column():
50
+ output_img = gr.Image(label="edited_img")
51
+ mask_btn.click(controlnetSD.mask_based_updating2,inputs=[org_image,mask,prompt],outputs=output_img,api_name="/masked_based_img_editing")
52
+
53
+ demo.launch()
modules_controlnetSD.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from utils_inpaint import resize_image_dimensions, make_inpaint_condition
2
+ import torch
3
+ from diffusers import ControlNetModel, StableDiffusionControlNetInpaintPipeline
4
+ import spaces
5
+ import time
6
+ from PIL import Image
7
+ import numpy as np
8
+
9
+ device = torch.device('cuda')
10
+
11
+ @spaces.GPU(duration=20)
12
+ def mask_based_updating2(init_image_file,mask_image_file,prompt,strength=0.9, guidance_scale=9, num_inference_steps=100):
13
+ # load ControlNet
14
+ start_time = time.time()
15
+ controlnet = ControlNetModel.from_pretrained("lllyasviel/control_v11p_sd15_inpaint")
16
+
17
+ # pass ControlNet to the pipeline
18
+ pipeline = StableDiffusionControlNetInpaintPipeline.from_pretrained(
19
+ "fluently/Fluently-v4-inpainting", controlnet=controlnet
20
+ )
21
+ # pipeline.enable_model_cpu_offload()
22
+ pipeline.to(device)
23
+
24
+ init_image = Image.fromarray(init_image_file)
25
+ mask_image = Image.fromarray(mask_image_file)
26
+ init_image = init_image.convert("RGB")
27
+ mask_image = mask_image.convert("1")
28
+ width, height = init_image.size
29
+ width_new, height_new = resize_image_dimensions(original_resolution_wh=init_image.size)
30
+ init_image = init_image.resize((width_new, height_new), Image.LANCZOS)
31
+ mask_image = mask_image.resize((width_new, height_new), Image.NEAREST)
32
+ #image and mask_image should be PIL images.
33
+ #The mask structure is white for inpainting and black for keeping as is
34
+ # image = pipe(prompt=prompt, image=init_image, mask_image=mask_image).images[0]
35
+ control_image = make_inpaint_condition(init_image, mask_image)
36
+ print("para: ",strength, guidance_scale,num_inference_steps)
37
+ negative_prompt = "ugly, deformed, nsfw, disfigured, worst quality, normal quality, low quality, low res, blurry, text, watermark, logo, banner, extra digits, cropped, jpeg artifacts, signature, username, error, sketch, duplicate, ugly, monochrome, horror, geometry, mutation, disgusting, bad anatomy, faint, unrealistic, Cartoon, drawing"
38
+ image = pipeline(prompt=prompt,negative_prompt=negative_prompt, image=init_image, mask_image=mask_image, control_image=control_image,strength = strength, guidance_scale=guidance_scale,num_inference_steps=num_inference_steps).images[0]
39
+ image = image.resize((width, height), Image.LANCZOS)
40
+ print(f'Time taken by inpainting model: {time.time() - start_time}')
41
+ torch.cuda.empty_cache()
42
+ return image
requirements (1).txt ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ tqdm
2
+ einops
3
+ accelerate
4
+ spaces
5
+ timm
6
+ transformers
7
+ diffusers
8
+ samv2
9
+ gradio
10
+ supervision
11
+ opencv-python
12
+ pytest
utils_inpaint.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Tuple
2
+
3
+ def resize_image_dimensions(
4
+ original_resolution_wh: Tuple[int, int],
5
+ maximum_dimension: int = 2048
6
+ ) -> Tuple[int, int]:
7
+ width, height = original_resolution_wh
8
+
9
+ if width <= maximum_dimension and height <= maximum_dimension:
10
+ width = width - (width % 32)
11
+ height = height - (height % 32)
12
+ return width, height
13
+
14
+ if width > height:
15
+ scaling_factor = maximum_dimension / width
16
+ else:
17
+ scaling_factor = maximum_dimension / height
18
+
19
+ new_width = int(width * scaling_factor)
20
+ new_height = int(height * scaling_factor)
21
+
22
+ new_width = new_width - (new_width % 32)
23
+ new_height = new_height - (new_height % 32)
24
+
25
+ return new_width, new_height
26
+
27
+ def make_inpaint_condition(init_image, mask_image):
28
+ init_image = np.array(init_image.convert("RGB")).astype(np.float32) / 255.0
29
+ mask_image = np.array(mask_image.convert("L")).astype(np.float32) / 255.0
30
+
31
+ assert init_image.shape[0:1] == mask_image.shape[0:1], "image and image_mask must have the same image size"
32
+ init_image[mask_image > 0.5] = -1.0 # set as masked pixel
33
+ init_image = np.expand_dims(init_image, 0).transpose(0, 3, 1, 2)
34
+ init_image = torch.from_numpy(init_image)
35
+ return init_image