app.py CHANGED
@@ -13,7 +13,7 @@ import cv2
13
  from matplotlib import pyplot as plt
14
  from torchvision import transforms
15
  from diffusers import DiffusionPipeline
16
-
17
  pipe = DiffusionPipeline.from_pretrained(
18
  "patrickvonplaten/new_inpaint_test",
19
  torch_dtype=torch.float16,
@@ -30,11 +30,29 @@ def read_content(file_path: str) -> str:
30
 
31
  return content
32
 
33
- def predict(dict, example_image):
34
- init_image = dict["image"].convert("RGB")
35
- mask = dict["mask"].convert("RGB")
36
- image = pipe(image=init_image, mask_image=mask, example_image=example_image).images[0]
37
- return image, gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
 
40
  css = '''
@@ -75,6 +93,16 @@ css = '''
75
  display: none !important;
76
  }
77
  '''
 
 
 
 
 
 
 
 
 
 
78
 
79
  image_blocks = gr.Blocks(css=css)
80
  with image_blocks as demo:
@@ -83,23 +111,36 @@ with image_blocks as demo:
83
  with gr.Box():
84
  with gr.Row():
85
  with gr.Column():
86
- image = gr.Image(source='upload', tool='sketch', elem_id="image_upload", type="pil", label="Upload")
87
- example = gr.Image(source='upload', elem_id="image_upload", type="pil", label="Upload")
 
 
 
 
 
 
 
 
88
  with gr.Row(elem_id="prompt-container").style(mobile_collapse=False, equal_height=True):
89
- btn = gr.Button("Inpaint!").style(
90
  margin=False,
91
  rounded=(False, True, True, False),
92
- full_width=False,
93
  )
94
- with gr.Column():
95
- image_out = gr.Image(label="Output", elem_id="output-img").style(height=400)
96
  with gr.Group(elem_id="share-btn-container"):
97
- community_icon = gr.HTML(community_icon_html, visible=False)
98
- loading_icon = gr.HTML(loading_icon_html, visible=False)
99
- share_button = gr.Button("Share to community", elem_id="share-btn", visible=False)
100
 
101
-
102
- btn.click(fn=predict, inputs=[image, example], outputs=[image_out, community_icon, loading_icon, share_button])
 
 
 
 
 
 
 
103
  share_button.click(None, [], [], _js=share_js)
104
 
105
 
@@ -113,9 +154,6 @@ with image_blocks as demo:
113
  <div class="acknowledgments">
114
  <p><h4>LICENSE</h4>
115
  The model is licensed with a <a href="https://huggingface.co/spaces/CompVis/stable-diffusion-license" style="text-decoration: underline;" target="_blank">CreativeML Open RAIL-M</a> license. The authors claim no rights on the outputs you generate, you are free to use them and are accountable for their use which must not go against the provisions set in this license. The license forbids you from sharing any content that violates any laws, produce any harm to a person, disseminate any personal information that would be meant for harm, spread misinformation and target vulnerable groups. For the full list of restrictions please <a href="https://huggingface.co/spaces/CompVis/stable-diffusion-license" target="_blank" style="text-decoration: underline;" target="_blank">read the license</a></p>
116
- <p><h4>Biases and content acknowledgment</h4>
117
- Despite how impressive being able to turn text into image is, beware to the fact that this model may output content that reinforces or exacerbates societal biases, as well as realistic faces, pornography and violence. The model was trained on the <a href="https://laion.ai/blog/laion-5b/" style="text-decoration: underline;" target="_blank">LAION-5B dataset</a>, which scraped non-curated image-text-pairs from the internet (the exception being the removal of illegal content) and is meant for research purposes. You can read more in the <a href="https://huggingface.co/CompVis/stable-diffusion-v1-4" style="text-decoration: underline;" target="_blank">model card</a></p>
118
- </div>
119
  """
120
  )
121
 
 
13
  from matplotlib import pyplot as plt
14
  from torchvision import transforms
15
  from diffusers import DiffusionPipeline
16
+ from diffusers.utils import torch_device
17
  pipe = DiffusionPipeline.from_pretrained(
18
  "patrickvonplaten/new_inpaint_test",
19
  torch_dtype=torch.float16,
 
30
 
31
  return content
32
 
33
+ def predict(dict, reference, scale, seed, step):
34
+ width,height=dict["image"].size
35
+ if width<height:
36
+ factor=width/512.0
37
+ width=512
38
+ height=int((height/factor)/8.0)*8
39
+
40
+ else:
41
+ factor=height/512.0
42
+ height=512
43
+ width=int((width/factor)/8.0)*8
44
+ init_image = dict["image"].convert("RGB").resize((width,height))
45
+ mask = dict["mask"].convert("RGB").resize((width,height))
46
+ generator = torch.Generator('cuda').manual_seed(seed) if seed != 0 else None
47
+ output = pipe(
48
+ image=init_image,
49
+ mask_image=mask,
50
+ example_image=reference,
51
+ generator=generator,
52
+ guidance_scale=scale,
53
+ num_inference_steps=step,
54
+ ).images[0]
55
+ return output, gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)
56
 
57
 
58
  css = '''
 
93
  display: none !important;
94
  }
95
  '''
96
+ example={}
97
+ for i in range(1,4):
98
+ ex_image_path='examples/image/example_'+str(i)+'.png'
99
+ ex_mask_path='examples/mask/example_'+str(i)+'.png'
100
+ ex_reference_path='examples/reference/example_'+str(i)+'.jpg'
101
+ ex_image=Image.open(ex_image_path)
102
+ ex_mask=Image.open(ex_mask_path)
103
+ ex_reference=Image.open(ex_reference_path)
104
+ example[i]={'image':{'image':ex_image,'mask':ex_mask},'reference':ex_reference}
105
+
106
 
107
  image_blocks = gr.Blocks(css=css)
108
  with image_blocks as demo:
 
111
  with gr.Box():
112
  with gr.Row():
113
  with gr.Column():
114
+ image = gr.Image(source='upload', tool='sketch', elem_id="image_upload", type="pil", label="Source Image")
115
+ reference = gr.Image(source='upload', elem_id="image_upload", type="pil", label="Reference Image")
116
+
117
+ with gr.Column():
118
+ image_out = gr.Image(label="Output", elem_id="output-img").style(height=400)
119
+ guidance = gr.Slider(label="Guidance scale", value=5, maximum=15,interactive=True)
120
+ steps = gr.Slider(label="Steps", value=50, minimum=2, maximum=75, step=1,interactive=True)
121
+
122
+ seed = gr.Slider(0, 10000, label='Seed (0 = random)', value=0, step=1)
123
+
124
  with gr.Row(elem_id="prompt-container").style(mobile_collapse=False, equal_height=True):
125
+ btn = gr.Button("Paint!").style(
126
  margin=False,
127
  rounded=(False, True, True, False),
128
+ full_width=True,
129
  )
 
 
130
  with gr.Group(elem_id="share-btn-container"):
131
+ community_icon = gr.HTML(community_icon_html, visible=True)
132
+ loading_icon = gr.HTML(loading_icon_html, visible=True)
133
+ share_button = gr.Button("Share to community", elem_id="share-btn", visible=True)
134
 
135
+
136
+ with gr.Row():
137
+ gr.Examples([
138
+ ['examples/image/example_2.png', 'examples/reference/example_2.jpg',5,50],
139
+ ['examples/image/example_3.png', 'examples/reference/example_3.jpg',5,50],
140
+ ['examples/image/example_1.png', 'examples/reference/example_1.jpg',5,50],
141
+ ], inputs=[image, reference, guidance, steps])
142
+
143
+ btn.click(fn=predict, inputs=[image, reference, guidance, seed, steps], outputs=[image_out, community_icon, loading_icon, share_button])
144
  share_button.click(None, [], [], _js=share_js)
145
 
146
 
 
154
  <div class="acknowledgments">
155
  <p><h4>LICENSE</h4>
156
  The model is licensed with a <a href="https://huggingface.co/spaces/CompVis/stable-diffusion-license" style="text-decoration: underline;" target="_blank">CreativeML Open RAIL-M</a> license. The authors claim no rights on the outputs you generate, you are free to use them and are accountable for their use which must not go against the provisions set in this license. The license forbids you from sharing any content that violates any laws, produce any harm to a person, disseminate any personal information that would be meant for harm, spread misinformation and target vulnerable groups. For the full list of restrictions please <a href="https://huggingface.co/spaces/CompVis/stable-diffusion-license" target="_blank" style="text-decoration: underline;" target="_blank">read the license</a></p>
 
 
 
157
  """
158
  )
159
 
examples/image/example_1.png ADDED
examples/image/example_2.png ADDED
examples/image/example_3.png ADDED
examples/mask/example_1.png ADDED
examples/mask/example_2.png ADDED
examples/mask/example_3.png ADDED
examples/reference/example_1.jpg ADDED
examples/reference/example_2.jpg ADDED
examples/reference/example_3.jpg ADDED
header.html CHANGED
@@ -12,7 +12,7 @@
12
  </div>
13
  <div>
14
  <p style="align-items: center; margin-bottom: 7px;">
15
- Paint by Example, add a input and draw a mask for what you want to replace with a example image
16
  </p>
17
  </div>
18
  </div>
 
12
  </div>
13
  <div>
14
  <p style="align-items: center; margin-bottom: 7px;">
15
+ Paint by Example, upload a source image and draw a mask for what you want to replace with a example image.
16
  </p>
17
  </div>
18
  </div>
init_image.png DELETED
Binary file (407 kB)
 
mask_image.png DELETED
Binary file (11.9 kB)