itberrios commited on
Commit
261c2c3
1 Parent(s): 519a004
Files changed (3) hide show
  1. __pycache__/model.cpython-310.pyc +0 -0
  2. app.py +16 -25
  3. model.py +14 -5
__pycache__/model.cpython-310.pyc CHANGED
Binary files a/__pycache__/model.cpython-310.pyc and b/__pycache__/model.cpython-310.pyc differ
 
app.py CHANGED
@@ -6,18 +6,12 @@ from collections import defaultdict
6
  import streamlit as st
7
  from streamlit_drawable_canvas import st_canvas
8
 
9
- import torch
10
- from transformers import AutoImageProcessor, Mask2FormerForUniversalSegmentation
11
- from diffusers import StableDiffusionInpaintPipeline
12
-
13
  import matplotlib as mpl
14
 
15
- from model import segment_image, inpaint
16
 
17
 
18
  # define utils and helpers
19
- DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'
20
-
21
  def closest_number(n, m=8):
22
  """ Obtains closest number to n that is divisble by m """
23
  return int(n/m) * m
@@ -96,7 +90,10 @@ def get_mask(image, edit_method, height, width):
96
 
97
  if __name__ == '__main__':
98
 
99
- st.title("Stable Edit - Edit your photos with Stable Diffusion!")
 
 
 
100
 
101
  sf = st.text_input("Please enter resizing scale factor to downsize image (default=2)", value="2")
102
  try:
@@ -126,22 +123,16 @@ if __name__ == '__main__':
126
 
127
  # get inpainted images
128
  prompt = st.text_input("Please enter prompt for image inpainting", value="")
129
- seed = st.text_input("(Optional) enter seed to change inpainting result (default=0)", value="0")
130
- try:
131
- seed = int(seed)
132
- except:
133
- st.write("Invalid seed! Defaultign to 0, please re-enter above to change it")
134
- seed = 0
135
-
136
- st.write("Inpainting Images, patience is a virtue :)")
137
-
138
- images = inpaint(image, mask, width, height, prompt=prompt, seed=seed, guidance_scale=17.5, num_samples=3)
139
-
140
- # display all images
141
- st.write("Original Image")
142
- st.image(image)
143
- for i, img in enumerate(images, 1):
144
- st.write(f"result: {i}")
145
- st.image(img)
146
 
147
 
 
6
  import streamlit as st
7
  from streamlit_drawable_canvas import st_canvas
8
 
 
 
 
 
9
  import matplotlib as mpl
10
 
11
+ from model import device, segment_image, inpaint
12
 
13
 
14
  # define utils and helpers
 
 
15
  def closest_number(n, m=8):
16
  """ Obtains closest number to n that is divisble by m """
17
  return int(n/m) * m
 
90
 
91
  if __name__ == '__main__':
92
 
93
+ st.title("Stable Edit")
94
+ st.title("Edit your photos with Stable Diffusion!")
95
+
96
+ st.write(f"Device found: {device}")
97
 
98
  sf = st.text_input("Please enter resizing scale factor to downsize image (default=2)", value="2")
99
  try:
 
123
 
124
  # get inpainted images
125
  prompt = st.text_input("Please enter prompt for image inpainting", value="")
126
+
127
+ if prompt: # and isinstance(seed, int):
128
+ st.write("Inpainting Images, patience is a virtue and this will take a while to run on a CPU :)")
129
+ images = inpaint(image, mask, width, height, prompt=prompt, seed=0, guidance_scale=17.5, num_samples=3)
130
+
131
+ # display all images
132
+ st.write("Original Image")
133
+ st.image(image)
134
+ for i, img in enumerate(images, 1):
135
+ st.write(f"result: {i}")
136
+ st.image(img)
 
 
 
 
 
 
137
 
138
 
model.py CHANGED
@@ -1,6 +1,6 @@
1
  import torch
2
  from transformers import AutoImageProcessor, Mask2FormerForUniversalSegmentation
3
- from diffusers import StableDiffusionInpaintPipeline
4
 
5
 
6
  device = 'cuda' if torch.cuda.is_available() else 'cpu'
@@ -30,7 +30,7 @@ def segment_image(image):
30
 
31
  return seg_prediction, segment_labels
32
 
33
- # Image inpainting
34
  # get Stable Diffusion model for image inpainting
35
  if device == 'cuda':
36
  pipe = StableDiffusionInpaintPipeline.from_pretrained(
@@ -39,9 +39,18 @@ if device == 'cuda':
39
  ).to(device)
40
  else:
41
  pipe = StableDiffusionInpaintPipeline.from_pretrained(
42
- "runwayml/stable-diffusion-inpainting"
43
- ).to(device)
44
-
 
 
 
 
 
 
 
 
 
45
 
46
  def inpaint(image, mask, W, H, prompt="", seed=0, guidance_scale=17.5, num_samples=3):
47
  """ Uses Stable Diffusion model to inpaint image
 
1
  import torch
2
  from transformers import AutoImageProcessor, Mask2FormerForUniversalSegmentation
3
+ from diffusers import StableDiffusionInpaintPipeline # , DiffusionPipeline
4
 
5
 
6
  device = 'cuda' if torch.cuda.is_available() else 'cpu'
 
30
 
31
  return seg_prediction, segment_labels
32
 
33
+ # Image inpainting pipeline
34
  # get Stable Diffusion model for image inpainting
35
  if device == 'cuda':
36
  pipe = StableDiffusionInpaintPipeline.from_pretrained(
 
39
  ).to(device)
40
  else:
41
  pipe = StableDiffusionInpaintPipeline.from_pretrained(
42
+ "runwayml/stable-diffusion-inpainting",
43
+ torch_dtype=torch.bfloat16,
44
+ device_map="auto"
45
+ )
46
+
47
+ # pipe = StableDiffusionInpaintPipeline.from_pretrained( # DiffusionPipeline.from_pretrained(
48
+ # "runwayml/stable-diffusion-inpainting",
49
+ # revision="fp16",
50
+ # torch_dtype=torch.bfloat16,
51
+ # # device_map="auto" # use for Hugging face spaces
52
+ # )
53
+ # pipe.to(device) # use for local environment
54
 
55
  def inpaint(image, mask, W, H, prompt="", seed=0, guidance_scale=17.5, num_samples=3):
56
  """ Uses Stable Diffusion model to inpaint image