kadirnar commited on
Commit
8f357bc
1 Parent(s): a36b097

Upload 5 files

Browse files
Files changed (4) hide show
  1. app.py +65 -1
  2. requirements.txt +2 -1
  3. utils/image2image.py +1 -0
  4. utils/inpaint.py +53 -0
app.py CHANGED
@@ -1,6 +1,7 @@
 
1
  from utils.image2image import stable_diffusion_img2img
2
  from utils.text2image import stable_diffusion_text2img
3
-
4
  import gradio as gr
5
 
6
  stable_model_list = [
@@ -10,6 +11,12 @@ stable_model_list = [
10
  "stabilityai/stable-diffusion-2-1",
11
  "stabilityai/stable-diffusion-2-1-base"
12
  ]
 
 
 
 
 
 
13
  stable_prompt_list = [
14
  "a photo of a man.",
15
  "a photo of a girl."
@@ -127,6 +134,50 @@ with app:
127
 
128
  image2image_predict = gr.Button(value='Generator')
129
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130
 
131
  with gr.Tab('Generator'):
132
  with gr.Column():
@@ -159,4 +210,17 @@ with app:
159
  outputs = [output_image],
160
  )
161
 
 
 
 
 
 
 
 
 
 
 
 
 
 
162
  app.launch()
 
1
+
2
  from utils.image2image import stable_diffusion_img2img
3
  from utils.text2image import stable_diffusion_text2img
4
+ from utils.inpaint import stable_diffusion_inpaint
5
  import gradio as gr
6
 
7
  stable_model_list = [
 
11
  "stabilityai/stable-diffusion-2-1",
12
  "stabilityai/stable-diffusion-2-1-base"
13
  ]
14
+
15
+ stable_inpiant_model_list = [
16
+ "stabilityai/stable-diffusion-2-inpainting",
17
+ "runwayml/stable-diffusion-inpainting"
18
+ ]
19
+
20
  stable_prompt_list = [
21
  "a photo of a man.",
22
  "a photo of a girl."
 
134
 
135
  image2image_predict = gr.Button(value='Generator')
136
 
137
+ with gr.Tab('Inpaint'):
138
+ inpaint_image_file = gr.Image(
139
+ source="upload",
140
+ type="numpy",
141
+ tool="sketch",
142
+ elem_id="source_container"
143
+ )
144
+
145
+ inpaint_model_id = gr.Dropdown(
146
+ choices=stable_inpiant_model_list,
147
+ value=stable_inpiant_model_list[0],
148
+ label='Inpaint Model Id'
149
+ )
150
+
151
+ inpaint_prompt = gr.Textbox(
152
+ lines=1,
153
+ value=stable_prompt_list[0],
154
+ label='Prompt'
155
+ )
156
+
157
+ inpaint_negative_prompt = gr.Textbox(
158
+ lines=1,
159
+ value=stable_negative_prompt_list[0],
160
+ label='Negative Prompt'
161
+ )
162
+
163
+ with gr.Accordion("Advanced Options", open=False):
164
+ inpaint_guidance_scale = gr.Slider(
165
+ minimum=0.1,
166
+ maximum=15,
167
+ step=0.1,
168
+ value=7.5,
169
+ label='Guidance Scale'
170
+ )
171
+
172
+ inpaint_num_inference_step = gr.Slider(
173
+ minimum=1,
174
+ maximum=100,
175
+ step=1,
176
+ value=50,
177
+ label='Num Inference Step'
178
+ )
179
+
180
+ inpaint_predict = gr.Button(value='Generator')
181
 
182
  with gr.Tab('Generator'):
183
  with gr.Column():
 
210
  outputs = [output_image],
211
  )
212
 
213
+ inpaint_predict.click(
214
+ fn = stable_diffusion_inpaint,
215
+ inputs = [
216
+ inpaint_image_file,
217
+ inpaint_model_id,
218
+ inpaint_prompt,
219
+ inpaint_negative_prompt,
220
+ inpaint_guidance_scale,
221
+ inpaint_num_inference_step,
222
+ ],
223
+ outputs = [output_image],
224
+ )
225
+
226
  app.launch()
requirements.txt CHANGED
@@ -2,4 +2,5 @@ transformers
2
  bitsandbytes==0.35.0
3
  xformers
4
  controlnet_aux
5
- diffusers
 
 
2
  bitsandbytes==0.35.0
3
  xformers
4
  controlnet_aux
5
+ diffusers
6
+ imageio
utils/image2image.py CHANGED
@@ -1,4 +1,5 @@
1
  from diffusers import StableDiffusionImg2ImgPipeline, DDIMScheduler
 
2
  from PIL import Image
3
  import torch
4
 
 
1
  from diffusers import StableDiffusionImg2ImgPipeline, DDIMScheduler
2
+ from IPython.display import display
3
  from PIL import Image
4
  import torch
5
 
utils/inpaint.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from diffusers import DiffusionPipeline, DDIMScheduler
2
+ from PIL import Image
3
+ import imageio
4
+ import torch
5
+
6
+ # https://huggingface.co/spaces/Manjushri/SD-2.0-Inpainting-CPU/blob/main/app.py
7
+
8
+ def resize(height,img):
9
+ baseheight = height
10
+ img = Image.open(img)
11
+ hpercent = (baseheight/float(img.size[1]))
12
+ wsize = int((float(img.size[0])*float(hpercent)))
13
+ img = img.resize((wsize,baseheight), Image.Resampling.LANCZOS)
14
+ return img
15
+
16
+ def img_preprocces(source_img, prompt, negative_prompt):
17
+ imageio.imwrite("data.png", source_img["image"])
18
+ imageio.imwrite("data_mask.png", source_img["mask"])
19
+ src = resize(512, "data.png")
20
+ src.save("src.png")
21
+ mask = resize(512, "data_mask.png")
22
+ mask.save("mask.png")
23
+ return src, mask
24
+
25
+ def stable_diffusion_inpaint(
26
+ image_path:str,
27
+ model_path:str,
28
+ prompt:str,
29
+ negative_prompt:str,
30
+ guidance_scale:int,
31
+ num_inference_step:int,
32
+ ):
33
+
34
+ image, mask_image = img_preprocces(image_path, prompt, negative_prompt)
35
+ pipe = DiffusionPipeline.from_pretrained(
36
+ model_path,
37
+ revision="fp16",
38
+ torch_dtype=torch.float16,
39
+ )
40
+ pipe.to('cuda')
41
+ pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config)
42
+ pipe.enable_xformers_memory_efficient_attention()
43
+
44
+ output = pipe(
45
+ prompt = prompt,
46
+ image = image,
47
+ mask_image=mask_image,
48
+ negative_prompt = negative_prompt,
49
+ num_inference_steps = num_inference_step,
50
+ guidance_scale = guidance_scale,
51
+ ).images
52
+
53
+ return output