lnyan commited on
Commit
e5c3d38
1 Parent(s): cf9f7d8

Update app with cpu backend

Browse files
Files changed (1) hide show
  1. app.py +41 -24
app.py CHANGED
@@ -18,6 +18,15 @@ try:
18
  cuda_available = torch.cuda.is_available()
19
  except:
20
  cuda_available = False
 
 
 
 
 
 
 
 
 
21
 
22
  def load_html():
23
  body, canvaspy = "", ""
@@ -88,12 +97,18 @@ def save_token(token):
88
 
89
  def get_model(token=""):
90
  if "text2img" not in model:
91
- text2img = StableDiffusionPipeline.from_pretrained(
92
- "CompVis/stable-diffusion-v1-4",
93
- revision="fp16",
94
- torch_dtype=torch.float16,
95
- use_auth_token=token,
96
- ).to("cuda")
 
 
 
 
 
 
97
  model["safety_checker"] = text2img.safety_checker
98
  inpaint = StableDiffusionInpaintPipeline(
99
  vae=text2img.vae,
@@ -103,7 +118,7 @@ def get_model(token=""):
103
  scheduler=text2img.scheduler,
104
  safety_checker=text2img.safety_checker,
105
  feature_extractor=text2img.feature_extractor,
106
- ).to("cuda")
107
  save_token(token)
108
  try:
109
  total_memory = torch.cuda.get_device_properties(0).total_memory // (
@@ -130,23 +145,23 @@ def run_outpaint(
130
  state,
131
  ):
132
  base64_str = "base64"
133
- if not cuda_available:
134
- data = base64.b64decode(str(sel_buffer_str))
135
- pil = Image.open(io.BytesIO(data))
136
- sel_buffer = np.array(pil)
137
- sel_buffer[:, :, 3]=255
138
- sel_buffer[:, :, 0]=255
139
- out_pil = Image.fromarray(sel_buffer)
140
- out_buffer = io.BytesIO()
141
- out_pil.save(out_buffer, format="PNG")
142
- out_buffer.seek(0)
143
- base64_bytes = base64.b64encode(out_buffer.read())
144
- base64_str = base64_bytes.decode("ascii")
145
- return (
146
- gr.update(label=str(state + 1), value=base64_str,),
147
- gr.update(label="Prompt"),
148
- state + 1,
149
- )
150
  if True:
151
  text2img, inpaint = get_model()
152
  if enable_safety:
@@ -237,6 +252,8 @@ with blocks as demo:
237
  title = gr.Markdown(
238
  """
239
  **stablediffusion-infinity**: Outpainting with Stable Diffusion on an infinite canvas: [https://github.com/lkwq007/stablediffusion-infinity](https://github.com/lkwq007/stablediffusion-infinity)
 
 
240
  """
241
  )
242
  # frame
 
18
  cuda_available = torch.cuda.is_available()
19
  except:
20
  cuda_available = False
21
+ finally:
22
+ if cuda_available:
23
+ device = "cuda"
24
+ else:
25
+ device = "cpu"
26
+
27
+ if device != "cuda":
28
+ import contextlib
29
+ autocast = contextlib.nullcontext
30
 
31
  def load_html():
32
  body, canvaspy = "", ""
 
97
 
98
  def get_model(token=""):
99
  if "text2img" not in model:
100
+ if device=="cuda":
101
+ text2img = StableDiffusionPipeline.from_pretrained(
102
+ "CompVis/stable-diffusion-v1-4",
103
+ revision="fp16",
104
+ torch_dtype=torch.float16,
105
+ use_auth_token=token,
106
+ ).to(device)
107
+ else:
108
+ text2img = StableDiffusionPipeline.from_pretrained(
109
+ "CompVis/stable-diffusion-v1-4",
110
+ use_auth_token=token,
111
+ ).to(device)
112
  model["safety_checker"] = text2img.safety_checker
113
  inpaint = StableDiffusionInpaintPipeline(
114
  vae=text2img.vae,
 
118
  scheduler=text2img.scheduler,
119
  safety_checker=text2img.safety_checker,
120
  feature_extractor=text2img.feature_extractor,
121
+ ).to(device)
122
  save_token(token)
123
  try:
124
  total_memory = torch.cuda.get_device_properties(0).total_memory // (
 
145
  state,
146
  ):
147
  base64_str = "base64"
148
+ # if not cuda_available:
149
+ # data = base64.b64decode(str(sel_buffer_str))
150
+ # pil = Image.open(io.BytesIO(data))
151
+ # sel_buffer = np.array(pil)
152
+ # sel_buffer[:, :, 3]=255
153
+ # sel_buffer[:, :, 0]=255
154
+ # out_pil = Image.fromarray(sel_buffer)
155
+ # out_buffer = io.BytesIO()
156
+ # out_pil.save(out_buffer, format="PNG")
157
+ # out_buffer.seek(0)
158
+ # base64_bytes = base64.b64encode(out_buffer.read())
159
+ # base64_str = base64_bytes.decode("ascii")
160
+ # return (
161
+ # gr.update(label=str(state + 1), value=base64_str,),
162
+ # gr.update(label="Prompt"),
163
+ # state + 1,
164
+ # )
165
  if True:
166
  text2img, inpaint = get_model()
167
  if enable_safety:
 
252
  title = gr.Markdown(
253
  """
254
  **stablediffusion-infinity**: Outpainting with Stable Diffusion on an infinite canvas: [https://github.com/lkwq007/stablediffusion-infinity](https://github.com/lkwq007/stablediffusion-infinity)
255
+
256
+ **This Space does not have GPU at present, so it's quite slow or can only outputs red squares when the inference cannot be proceeded**
257
  """
258
  )
259
  # frame