Shalmoni commited on
Commit
f01e490
·
verified ·
1 Parent(s): 623b9fe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -9
app.py CHANGED
@@ -227,6 +227,7 @@ def generate_storyboard_with_llm(user_prompt: str, n_shots: int, default_fps: in
227
  # =========================
228
  # IMAGE GEN (ZeroGPU) — SD1.5 text2img + img2img chaining
229
  # =========================
 
230
  from diffusers import StableDiffusionPipeline, StableDiffusionImg2ImgPipeline
231
 
232
  SD_MODEL = os.getenv("SD_MODEL", "runwayml/stable-diffusion-v1-5")
@@ -234,25 +235,35 @@ _sd_t2i = None
234
  _sd_i2i = None
235
 
236
  def _lazy_sd_pipes():
237
- """Load SD once without low_cpu_mem_usage to avoid offload_state_dict kwarg; reuse modules for img2img."""
 
 
 
 
 
 
238
  global _sd_t2i, _sd_i2i
239
  if _sd_t2i is not None and _sd_i2i is not None:
240
  return _sd_t2i, _sd_i2i
241
 
242
- dtype = torch.float16 if torch.cuda.is_available() else torch.float32
 
243
 
 
244
  _sd_t2i = StableDiffusionPipeline.from_pretrained(
245
  SD_MODEL,
246
- dtype=dtype, # (`torch_dtype` is deprecated, use `dtype`)
247
- safety_checker=None,
248
  feature_extractor=None,
249
  use_safetensors=True,
250
- low_cpu_mem_usage=False # <-- critical: prevents passing offload_state_dict
 
251
  )
252
- if torch.cuda.is_available():
 
253
  _sd_t2i = _sd_t2i.to("cuda")
254
 
255
- # Build img2img from already-loaded modules (avoids another from_pretrained call)
256
  _sd_i2i = StableDiffusionImg2ImgPipeline(
257
  vae=_sd_t2i.vae,
258
  text_encoder=_sd_t2i.text_encoder,
@@ -262,12 +273,11 @@ def _lazy_sd_pipes():
262
  safety_checker=None,
263
  feature_extractor=None
264
  )
265
- if torch.cuda.is_available():
266
  _sd_i2i = _sd_i2i.to("cuda")
267
 
268
  return _sd_t2i, _sd_i2i
269
 
270
-
271
  def _save_keyframe(pid: str, shot_id: int, img: Image.Image) -> str:
272
  pdir = project_dir(pid)
273
  out = os.path.join(pdir, "keyframes", f"shot_{shot_id:02d}.png")
 
227
  # =========================
228
  # IMAGE GEN (ZeroGPU) — SD1.5 text2img + img2img chaining
229
  # =========================
230
+ # --- replace the whole _lazy_sd_pipes() with this ---
231
  from diffusers import StableDiffusionPipeline, StableDiffusionImg2ImgPipeline
232
 
233
  SD_MODEL = os.getenv("SD_MODEL", "runwayml/stable-diffusion-v1-5")
 
235
  _sd_i2i = None
236
 
237
  def _lazy_sd_pipes():
238
+ """
239
+ Load SD once with settings that avoid the `offload_state_dict` kwarg issue:
240
+ - use `torch_dtype` (not `dtype`)
241
+ - set `low_cpu_mem_usage=False`
242
+ - disable safety checker at load time
243
+ - build Img2Img from the already-loaded modules (no second from_pretrained)
244
+ """
245
  global _sd_t2i, _sd_i2i
246
  if _sd_t2i is not None and _sd_i2i is not None:
247
  return _sd_t2i, _sd_i2i
248
 
249
+ use_cuda = torch.cuda.is_available()
250
+ dtype = torch.float16 if use_cuda else torch.float32
251
 
252
+ # Important: torch_dtype + low_cpu_mem_usage=False to avoid offload_state_dict
253
  _sd_t2i = StableDiffusionPipeline.from_pretrained(
254
  SD_MODEL,
255
+ torch_dtype=dtype, # <-- correct kwarg
256
+ safety_checker=None, # disable safety checker at load
257
  feature_extractor=None,
258
  use_safetensors=True,
259
+ low_cpu_mem_usage=False, # <-- critical to prevent passing offload_state_dict
260
+ revision="fp16" if use_cuda else None # helps pick fp16 weights on GPU
261
  )
262
+
263
+ if use_cuda:
264
  _sd_t2i = _sd_t2i.to("cuda")
265
 
266
+ # Build img2img from the same components (avoid another from_pretrained call)
267
  _sd_i2i = StableDiffusionImg2ImgPipeline(
268
  vae=_sd_t2i.vae,
269
  text_encoder=_sd_t2i.text_encoder,
 
273
  safety_checker=None,
274
  feature_extractor=None
275
  )
276
+ if use_cuda:
277
  _sd_i2i = _sd_i2i.to("cuda")
278
 
279
  return _sd_t2i, _sd_i2i
280
 
 
281
  def _save_keyframe(pid: str, shot_id: int, img: Image.Image) -> str:
282
  pdir = project_dir(pid)
283
  out = os.path.join(pdir, "keyframes", f"shot_{shot_id:02d}.png")