patrickvonplaten commited on
Commit
a00529a
1 Parent(s): ae4902b
Files changed (5) hide show
  1. README.md +1 -1
  2. __pycache__/app.cpython-310.pyc +0 -0
  3. app.py +1 -7
  4. header.html +4 -8
  5. inpainting.py +0 -194
README.md CHANGED
@@ -1,5 +1,5 @@
1
  ---
2
- title: Runway Inpainting
3
  emoji: 🔥
4
  colorFrom: green
5
  colorTo: pink
 
1
  ---
2
+ title: SDXL Inpainting
3
  emoji: 🔥
4
  colorFrom: green
5
  colorTo: pink
__pycache__/app.cpython-310.pyc CHANGED
Binary files a/__pycache__/app.cpython-310.pyc and b/__pycache__/app.cpython-310.pyc differ
 
app.py CHANGED
@@ -98,15 +98,9 @@ with image_blocks as demo:
98
  gr.HTML(
99
  """
100
  <div class="footer">
101
- <p>Model by <a href="https://huggingface.co/runwayml" style="text-decoration: underline;" target="_blank">RunwayML</a> - Gradio Demo by 🤗 Hugging Face
102
  </p>
103
  </div>
104
- <div class="acknowledgments">
105
- <p><h4>LICENSE</h4>
106
- 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>
107
- <p><h4>Biases and content acknowledgment</h4>
108
- 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>
109
- </div>
110
  """
111
  )
112
 
 
98
  gr.HTML(
99
  """
100
  <div class="footer">
101
+ <p>Model by <a href="https://huggingface.co/diffusers" style="text-decoration: underline;" target="_blank">RunwayML</a> - Gradio Demo by 🤗 Hugging Face
102
  </p>
103
  </div>
 
 
 
 
 
 
104
  """
105
  )
106
 
header.html CHANGED
@@ -8,17 +8,13 @@
8
  ">
9
  <img
10
  src="https://aeiljuispo.cloudimg.io/v7/https://s3.amazonaws.com/moonup/production/uploads/1665970599545-634cb15a4abe8405758d2e7e.jpeg"
11
- alt="RunwayML" width="64px">
12
  <h1 style="font-weight: 900; align-items: center; margin-bottom: 7px; margin-top: 20px;">
13
- RunwayML Stable Diffusion Inpainting 🎨
14
  </h1>
15
  </div>
16
  <div>
17
  <p style="align-items: center; margin-bottom: 7px;">
18
- Stable Diffusion Inpainting, add a mask and text prompt for what you want to replace <br>For faster
19
- generation you can try
20
- <a href="https://app.runwayml.com/video-tools/teams/akhaliq/ai-tools/erase-and-replace"
21
- style="text-decoration: underline;" target="_blank">erase and replace tool on Runway</a>
22
- </p>
23
  </div>
24
- </div>
 
8
  ">
9
  <img
10
  src="https://aeiljuispo.cloudimg.io/v7/https://s3.amazonaws.com/moonup/production/uploads/1665970599545-634cb15a4abe8405758d2e7e.jpeg"
11
+ alt="Diffusers" width="64px">
12
  <h1 style="font-weight: 900; align-items: center; margin-bottom: 7px; margin-top: 20px;">
13
+ Stable Diffusion XL Inpainting 🎨
14
  </h1>
15
  </div>
16
  <div>
17
  <p style="align-items: center; margin-bottom: 7px;">
18
+ Stable Diffusion XL Inpainting, add a mask and text prompt for what you want to replace
 
 
 
 
19
  </div>
20
+ </div>
inpainting.py DELETED
@@ -1,194 +0,0 @@
1
- import inspect
2
- from typing import List, Optional, Union
3
-
4
- import numpy as np
5
- import torch
6
-
7
- import PIL
8
- from diffusers import AutoencoderKL, DDIMScheduler, DiffusionPipeline, PNDMScheduler, UNet2DConditionModel
9
- from diffusers.pipelines.stable_diffusion import StableDiffusionSafetyChecker
10
- from tqdm.auto import tqdm
11
- from transformers import CLIPFeatureExtractor, CLIPTextModel, CLIPTokenizer
12
-
13
-
14
- def preprocess_image(image):
15
- w, h = image.size
16
- w, h = map(lambda x: x - x % 32, (w, h)) # resize to integer multiple of 32
17
- image = image.resize((w, h), resample=PIL.Image.LANCZOS)
18
- image = np.array(image).astype(np.float32) / 255.0
19
- image = image[None].transpose(0, 3, 1, 2)
20
- image = torch.from_numpy(image)
21
- return 2.0 * image - 1.0
22
-
23
-
24
- def preprocess_mask(mask):
25
- mask = mask.convert("L")
26
- w, h = mask.size
27
- w, h = map(lambda x: x - x % 32, (w, h)) # resize to integer multiple of 32
28
- mask = mask.resize((w // 8, h // 8), resample=PIL.Image.NEAREST)
29
- mask = np.array(mask).astype(np.float32) / 255.0
30
- mask = np.tile(mask, (4, 1, 1))
31
- mask = mask[None].transpose(0, 1, 2, 3) # what does this step do?
32
- mask = 1 - mask # repaint white, keep black
33
- mask = torch.from_numpy(mask)
34
- return mask
35
-
36
- class StableDiffusionInpaintingPipeline(DiffusionPipeline):
37
- def __init__(
38
- self,
39
- vae: AutoencoderKL,
40
- text_encoder: CLIPTextModel,
41
- tokenizer: CLIPTokenizer,
42
- unet: UNet2DConditionModel,
43
- scheduler: Union[DDIMScheduler, PNDMScheduler],
44
- safety_checker: StableDiffusionSafetyChecker,
45
- feature_extractor: CLIPFeatureExtractor,
46
- ):
47
- super().__init__()
48
- scheduler = scheduler.set_format("pt")
49
- self.register_modules(
50
- vae=vae,
51
- text_encoder=text_encoder,
52
- tokenizer=tokenizer,
53
- unet=unet,
54
- scheduler=scheduler,
55
- safety_checker=safety_checker,
56
- feature_extractor=feature_extractor,
57
- )
58
-
59
- @torch.no_grad()
60
- def __call__(
61
- self,
62
- prompt: Union[str, List[str]],
63
- init_image: torch.FloatTensor,
64
- mask_image: torch.FloatTensor,
65
- strength: float = 0.8,
66
- num_inference_steps: Optional[int] = 50,
67
- guidance_scale: Optional[float] = 7.5,
68
- eta: Optional[float] = 0.0,
69
- generator: Optional[torch.Generator] = None,
70
- output_type: Optional[str] = "pil",
71
- ):
72
-
73
- if isinstance(prompt, str):
74
- batch_size = 1
75
- elif isinstance(prompt, list):
76
- batch_size = len(prompt)
77
- else:
78
- raise ValueError(f"`prompt` has to be of type `str` or `list` but is {type(prompt)}")
79
-
80
- if strength < 0 or strength > 1:
81
- raise ValueError(f"The value of strength should in [0.0, 1.0] but is {strength}")
82
-
83
- # set timesteps
84
- accepts_offset = "offset" in set(inspect.signature(self.scheduler.set_timesteps).parameters.keys())
85
- extra_set_kwargs = {}
86
- offset = 0
87
- if accepts_offset:
88
- offset = 1
89
- extra_set_kwargs["offset"] = 1
90
-
91
- self.scheduler.set_timesteps(num_inference_steps, **extra_set_kwargs)
92
-
93
- # preprocess image
94
- init_image = preprocess_image(init_image).to(self.device)
95
-
96
- # encode the init image into latents and scale the latents
97
- init_latent_dist = self.vae.encode(init_image).latent_dist
98
- init_latents = init_latent_dist.sample(generator=generator)
99
- init_latents = 0.18215 * init_latents
100
-
101
- # prepare init_latents noise to latents
102
- init_latents = torch.cat([init_latents] * batch_size)
103
- init_latents_orig = init_latents
104
-
105
- # preprocess mask
106
- mask = preprocess_mask(mask_image).to(self.device)
107
- mask = torch.cat([mask] * batch_size)
108
-
109
- # check sizes
110
- if not mask.shape == init_latents.shape:
111
- raise ValueError(f"The mask and init_image should be the same size!")
112
-
113
- # get the original timestep using init_timestep
114
- init_timestep = int(num_inference_steps * strength) + offset
115
- init_timestep = min(init_timestep, num_inference_steps)
116
- timesteps = self.scheduler.timesteps[-init_timestep]
117
- timesteps = torch.tensor([timesteps] * batch_size, dtype=torch.long, device=self.device)
118
-
119
- # add noise to latents using the timesteps
120
- noise = torch.randn(init_latents.shape, generator=generator, device=self.device)
121
- init_latents = self.scheduler.add_noise(init_latents, noise, timesteps)
122
-
123
- # get prompt text embeddings
124
- text_input = self.tokenizer(
125
- prompt,
126
- padding="max_length",
127
- max_length=self.tokenizer.model_max_length,
128
- truncation=True,
129
- return_tensors="pt",
130
- )
131
- text_embeddings = self.text_encoder(text_input.input_ids.to(self.device))[0]
132
-
133
- # here `guidance_scale` is defined analog to the guidance weight `w` of equation (2)
134
- # of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1`
135
- # corresponds to doing no classifier free guidance.
136
- do_classifier_free_guidance = guidance_scale > 1.0
137
- # get unconditional embeddings for classifier free guidance
138
- if do_classifier_free_guidance:
139
- max_length = text_input.input_ids.shape[-1]
140
- uncond_input = self.tokenizer(
141
- [""] * batch_size, padding="max_length", max_length=max_length, return_tensors="pt"
142
- )
143
- uncond_embeddings = self.text_encoder(uncond_input.input_ids.to(self.device))[0]
144
-
145
- # For classifier free guidance, we need to do two forward passes.
146
- # Here we concatenate the unconditional and text embeddings into a single batch
147
- # to avoid doing two forward passes
148
- text_embeddings = torch.cat([uncond_embeddings, text_embeddings])
149
-
150
- # prepare extra kwargs for the scheduler step, since not all schedulers have the same signature
151
- # eta (η) is only used with the DDIMScheduler, it will be ignored for other schedulers.
152
- # eta corresponds to η in DDIM paper: https://arxiv.org/abs/2010.02502
153
- # and should be between [0, 1]
154
- accepts_eta = "eta" in set(inspect.signature(self.scheduler.step).parameters.keys())
155
- extra_step_kwargs = {}
156
- if accepts_eta:
157
- extra_step_kwargs["eta"] = eta
158
-
159
- latents = init_latents
160
- t_start = max(num_inference_steps - init_timestep + offset, 0)
161
- for i, t in tqdm(enumerate(self.scheduler.timesteps[t_start:])):
162
- # expand the latents if we are doing classifier free guidance
163
- latent_model_input = torch.cat([latents] * 2) if do_classifier_free_guidance else latents
164
-
165
- # predict the noise residual
166
- noise_pred = self.unet(latent_model_input, t, encoder_hidden_states=text_embeddings)["sample"]
167
-
168
- # perform guidance
169
- if do_classifier_free_guidance:
170
- noise_pred_uncond, noise_pred_text = noise_pred.chunk(2)
171
- noise_pred = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)
172
-
173
- # compute the previous noisy sample x_t -> x_t-1
174
- latents = self.scheduler.step(noise_pred, t, latents, **extra_step_kwargs)["prev_sample"]
175
-
176
- # masking
177
- init_latents_proper = self.scheduler.add_noise(init_latents_orig, noise, t)
178
- latents = (init_latents_proper * mask) + (latents * (1 - mask))
179
-
180
- # scale and decode the image latents with vae
181
- latents = 1 / 0.18215 * latents
182
- image = self.vae.decode(latents).sample
183
-
184
- image = (image / 2 + 0.5).clamp(0, 1)
185
- image = image.cpu().permute(0, 2, 3, 1).numpy()
186
-
187
- # run safety checker
188
- safety_cheker_input = self.feature_extractor(self.numpy_to_pil(image), return_tensors="pt").to(self.device)
189
- image, has_nsfw_concept = self.safety_checker(images=image, clip_input=safety_cheker_input.pixel_values)
190
-
191
- if output_type == "pil":
192
- image = self.numpy_to_pil(image)
193
-
194
- return {"sample": image, "nsfw_content_detected": has_nsfw_concept}