tennyson commited on
Commit
87c8773
1 Parent(s): de9f302
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,19 @@ 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 +83,16 @@ css = '''
75
  display: none !important;
76
  }
77
  '''
 
 
 
 
 
 
 
 
 
 
78
 
79
  image_blocks = gr.Blocks(css=css)
80
  with image_blocks as demo:
@@ -83,23 +101,39 @@ 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,10 +147,7 @@ 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
 
122
- image_blocks.launch()
 
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
+ init_image = dict["image"].convert("RGB").resize((512,512))
35
+ mask = dict["mask"].convert("RGB").resize((512,512))
36
+ generator = torch.Generator('cuda').manual_seed(seed) if seed != 0 else None
37
+ output = pipe(
38
+ image=init_image,
39
+ mask_image=mask,
40
+ example_image=reference,
41
+ generator=generator,
42
+ guidance_scale=scale,
43
+ num_inference_steps=step,
44
+ ).images[0]
45
+ return output, gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)
46
 
47
 
48
  css = '''
 
83
  display: none !important;
84
  }
85
  '''
86
+ example={}
87
+ for i in range(1,4):
88
+ ex_image_path='examples/image/example_'+str(i)+'.png'
89
+ ex_mask_path='examples/mask/example_'+str(i)+'.png'
90
+ ex_reference_path='examples/reference/example_'+str(i)+'.jpg'
91
+ ex_image=Image.open(ex_image_path)
92
+ ex_mask=Image.open(ex_mask_path)
93
+ ex_reference=Image.open(ex_reference_path)
94
+ example[i]={'image':{'image':ex_image,'mask':ex_mask},'reference':ex_reference}
95
+
96
 
97
  image_blocks = gr.Blocks(css=css)
98
  with image_blocks as demo:
 
101
  with gr.Box():
102
  with gr.Row():
103
  with gr.Column():
104
+ image = gr.Image(source='upload', tool='sketch', elem_id="image_upload", type="pil", label="Source Image")
105
+ reference = gr.Image(source='upload', elem_id="image_upload", type="pil", label="Reference Image")
106
+
107
+ with gr.Column():
108
+ image_out = gr.Image(label="Output", elem_id="output-img").style(height=400)
109
+ guidance = gr.Slider(label="Guidance scale", value=5, maximum=15,interactive=True)
110
+ steps = gr.Slider(label="Steps", value=50, minimum=2, maximum=75, step=1,interactive=True)
111
+
112
+ seed = gr.Slider(0, 10000, label='Seed (0 = random)', value=0, step=1)
113
+
114
  with gr.Row(elem_id="prompt-container").style(mobile_collapse=False, equal_height=True):
115
+ btn = gr.Button("Paint!").style(
116
  margin=False,
117
  rounded=(False, True, True, False),
118
+ full_width=True,
119
  )
 
 
120
  with gr.Group(elem_id="share-btn-container"):
121
  community_icon = gr.HTML(community_icon_html, visible=False)
122
  loading_icon = gr.HTML(loading_icon_html, visible=False)
123
+ share_button = gr.Button("Share to community", elem_id="share-btn", visible=False).style(
124
+ margin=False,
125
+ rounded=(False, True, True, False),
126
+ full_width=True,
127
+ )
128
 
129
+ with gr.Row():
130
+ gr.Examples([
131
+ ['examples/image/example_2.png', 'examples/reference/example_2.jpg',5,50],
132
+ ['examples/image/example_3.png', 'examples/reference/example_3.jpg',5,50],
133
+ ['examples/image/example_1.png', 'examples/reference/example_1.jpg',5,50],
134
+ ], inputs=[image, reference, guidance, steps])
135
+
136
+ btn.click(fn=predict, inputs=[image, reference, guidance, seed, steps], outputs=[image_out, community_icon, loading_icon, share_button])
137
  share_button.click(None, [], [], _js=share_js)
138
 
139
 
 
147
  <div class="acknowledgments">
148
  <p><h4>LICENSE</h4>
149
  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>
 
 
 
150
  """
151
  )
152
 
153
+ image_blocks.launch(server_name='0.0.0.0')
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,10 @@
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
+ <p style="align-items: center; margin-bottom: 7px;">
18
+ This demo currently only supports square source image. It will raise an error if the souce image is not square.
19
  </p>
20
  </div>
21
  </div>