PeterL1n commited on
Commit
f151301
1 Parent(s): e9028a2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -10
app.py CHANGED
@@ -18,7 +18,7 @@ checkpoints = {
18
  "4-Step" : ["sdxl_lightning_4step_unet.safetensors", 4],
19
  "8-Step" : ["sdxl_lightning_8step_unet.safetensors", 8],
20
  }
21
-
22
 
23
  # Ensure model and scheduler are initialized in GPU-enabled function
24
  if torch.cuda.is_available():
@@ -49,19 +49,17 @@ if SAFETY_CHECKER:
49
  # Function
50
  @spaces.GPU(enable_queue=True)
51
  def generate_image(prompt, ckpt):
 
52
  print(prompt, ckpt)
53
 
54
  checkpoint = checkpoints[ckpt][0]
55
- num_inference_steps = checkpoints[ckpt][1]
56
-
57
- if num_inference_steps==1:
58
- # Ensure sampler uses "trailing" timesteps and "sample" prediction type for 1-step inference.
59
- pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config, timestep_spacing="trailing", prediction_type="sample")
60
- else:
61
- # Ensure sampler uses "trailing" timesteps.
62
- pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config, timestep_spacing="trailing")
63
 
64
- pipe.unet.load_state_dict(load_file(hf_hub_download(repo, checkpoint), device="cuda"))
65
  results = pipe(prompt, num_inference_steps=num_inference_steps, guidance_scale=0)
66
 
67
  if SAFETY_CHECKER:
 
18
  "4-Step" : ["sdxl_lightning_4step_unet.safetensors", 4],
19
  "8-Step" : ["sdxl_lightning_8step_unet.safetensors", 8],
20
  }
21
+ loaded = None
22
 
23
  # Ensure model and scheduler are initialized in GPU-enabled function
24
  if torch.cuda.is_available():
 
49
  # Function
50
  @spaces.GPU(enable_queue=True)
51
  def generate_image(prompt, ckpt):
52
+ global loaded
53
  print(prompt, ckpt)
54
 
55
  checkpoint = checkpoints[ckpt][0]
56
+ num_inference_steps = checkpoints[ckpt][1]
57
+
58
+ if loaded != num_inference_steps:
59
+ pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config, timestep_spacing="trailing", prediction_type="sample" if num_inference_steps==1 else "epsilon")
60
+ pipe.unet.load_state_dict(load_file(hf_hub_download(repo, checkpoint), device="cuda"))
61
+ loaded = num_inference_steps
 
 
62
 
 
63
  results = pipe(prompt, num_inference_steps=num_inference_steps, guidance_scale=0)
64
 
65
  if SAFETY_CHECKER: