xinglilu commited on
Commit
18a4b45
·
verified ·
1 Parent(s): 89f50c6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -14
app.py CHANGED
@@ -3,25 +3,20 @@ import numpy as np
3
  import random
4
  from diffusers import DiffusionPipeline
5
  import torch
6
-
7
- # Check for GPU availability and set the device accordingly
8
- device = "cuda" if torch.cuda.is_available() else "cpu"
9
-
10
- # Load the diffusion pipeline based on the availability of GPU
11
- if device == "cuda":
12
- pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", torch_dtype=torch.float16, variant="fp16", use_safetensors=True)
13
- pipe.enable_xformers_memory_efficient_attention()
14
- pipe = pipe.to(device)
15
- else:
16
- pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", use_safetensors=True)
17
- pipe = pipe.to(device)
18
 
19
  # Constants
20
  MAX_SEED = np.iinfo(np.int32).max
21
  MAX_IMAGE_SIZE = 1024
22
 
23
- # Inference function
 
 
 
24
  def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
 
 
 
25
  if randomize_seed:
26
  seed = random.randint(0, MAX_SEED)
27
 
@@ -55,7 +50,7 @@ css="""
55
  """
56
 
57
  # Determine the power device (GPU or CPU) for display purposes
58
- power_device = "GPU" if device == "cuda" else "CPU"
59
 
60
  # Gradio UI setup
61
  with gr.Blocks(css=css) as demo:
 
3
  import random
4
  from diffusers import DiffusionPipeline
5
  import torch
6
+ import spaces
 
 
 
 
 
 
 
 
 
 
 
7
 
8
  # Constants
9
  MAX_SEED = np.iinfo(np.int32).max
10
  MAX_IMAGE_SIZE = 1024
11
 
12
+ # Load the diffusion pipeline without immediately moving it to GPU
13
+ pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", torch_dtype=torch.float16, use_safetensors=True)
14
+
15
+ @spaces.GPU
16
  def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
17
+ device = "cuda" if torch.cuda.is_available() else "cpu"
18
+ pipe.to(device)
19
+
20
  if randomize_seed:
21
  seed = random.randint(0, MAX_SEED)
22
 
 
50
  """
51
 
52
  # Determine the power device (GPU or CPU) for display purposes
53
+ power_device = "GPU" if torch.cuda.is_available() else "CPU"
54
 
55
  # Gradio UI setup
56
  with gr.Blocks(css=css) as demo: