mrbeliever commited on
Commit
04793b9
·
verified ·
1 Parent(s): 5c740f6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -37
app.py CHANGED
@@ -6,30 +6,29 @@ import json
6
  import gradio as gr
7
  import numpy as np
8
  from PIL import Image
9
- import spaces
10
  import torch
11
  from diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler
12
 
13
- if not torch.cuda.is_available():
14
- DESCRIPTION += "\n<p>Running on CPU 🥶 This demo may not work on CPU.</p>"
15
 
16
  MAX_SEED = np.iinfo(np.int32).max
17
- CACHE_EXAMPLES = torch.cuda.is_available() and os.getenv("CACHE_EXAMPLES", "1") == "1"
18
  MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "4096"))
19
  USE_TORCH_COMPILE = os.getenv("USE_TORCH_COMPILE", "0") == "1"
20
  ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD", "0") == "1"
21
 
22
- device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 
23
 
24
- if torch.cuda.is_available():
25
- pipe = StableDiffusionXLPipeline.from_pretrained(
26
- "sd-community/sdxl-flash",
27
- torch_dtype=torch.float16,
28
- use_safetensors=True,
29
- add_watermarker=False
30
- )
31
- pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
32
- pipe.to("cuda")
33
 
34
  def save_image(img):
35
  unique_name = str(uuid.uuid4()) + ".png"
@@ -41,7 +40,6 @@ def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
41
  seed = random.randint(0, MAX_SEED)
42
  return seed
43
 
44
- @spaces.GPU(duration=30, queue=False)
45
  def generate(
46
  prompt: str,
47
  negative_prompt: str = "",
@@ -55,23 +53,22 @@ def generate(
55
  use_resolution_binning: bool = True,
56
  progress=gr.Progress(track_tqdm=True),
57
  ):
58
- pipe.to(device)
59
  seed = int(randomize_seed_fn(seed, randomize_seed))
60
- generator = torch.Generator().manual_seed(seed)
61
 
62
  options = {
63
- "prompt":prompt,
64
- "negative_prompt":negative_prompt,
65
- "width":width,
66
- "height":height,
67
- "guidance_scale":guidance_scale,
68
- "num_inference_steps":num_inference_steps,
69
- "generator":generator,
70
- "use_resolution_binning":use_resolution_binning,
71
- "output_type":"pil",
72
-
73
  }
74
-
 
75
  images = pipe(**options).images
76
 
77
  image_paths = [save_image(img) for img in images]
@@ -79,17 +76,15 @@ def generate(
79
 
80
 
81
  css = '''
82
- .gradio-container{max-width: 700px !important}
83
- h1{text-align:center}
84
- footer {
85
- visibility: hidden
86
- }
87
  '''
 
88
  with gr.Blocks(css=css) as demo:
89
-
90
  gr.Markdown("""
91
  <div style="text-align: center; font-weight: bold; font-size: 2em;">
92
- Womener AI
93
  </div>
94
  """)
95
 
@@ -154,7 +149,6 @@ with gr.Blocks(css=css) as demo:
154
  value=8,
155
  )
156
 
157
-
158
  use_negative_prompt.change(
159
  fn=lambda x: gr.update(visible=x),
160
  inputs=use_negative_prompt,
@@ -185,4 +179,4 @@ with gr.Blocks(css=css) as demo:
185
  )
186
 
187
  if __name__ == "__main__":
188
- demo.queue(max_size=50).launch()
 
6
  import gradio as gr
7
  import numpy as np
8
  from PIL import Image
 
9
  import torch
10
  from diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler
11
 
12
+ DESCRIPTION = "A Stable Diffusion XL demo running on CPU."
 
13
 
14
  MAX_SEED = np.iinfo(np.int32).max
15
+ CACHE_EXAMPLES = os.getenv("CACHE_EXAMPLES", "1") == "1"
16
  MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "4096"))
17
  USE_TORCH_COMPILE = os.getenv("USE_TORCH_COMPILE", "0") == "1"
18
  ENABLE_CPU_OFFLOAD = os.getenv("ENABLE_CPU_OFFLOAD", "0") == "1"
19
 
20
+ # Set device to CPU explicitly
21
+ device = torch.device("cpu")
22
 
23
+ # Load pipeline and scheduler for CPU
24
+ pipe = StableDiffusionXLPipeline.from_pretrained(
25
+ "sd-community/sdxl-flash",
26
+ torch_dtype=torch.float32, # Use float32 for CPU
27
+ use_safetensors=True,
28
+ add_watermarker=False
29
+ )
30
+ pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
31
+ pipe.to(device) # Move the model to CPU
32
 
33
  def save_image(img):
34
  unique_name = str(uuid.uuid4()) + ".png"
 
40
  seed = random.randint(0, MAX_SEED)
41
  return seed
42
 
 
43
  def generate(
44
  prompt: str,
45
  negative_prompt: str = "",
 
53
  use_resolution_binning: bool = True,
54
  progress=gr.Progress(track_tqdm=True),
55
  ):
 
56
  seed = int(randomize_seed_fn(seed, randomize_seed))
57
+ generator = torch.Generator(device=device).manual_seed(seed)
58
 
59
  options = {
60
+ "prompt": prompt,
61
+ "negative_prompt": negative_prompt if use_negative_prompt else None,
62
+ "width": width,
63
+ "height": height,
64
+ "guidance_scale": guidance_scale,
65
+ "num_inference_steps": num_inference_steps,
66
+ "generator": generator,
67
+ "use_resolution_binning": use_resolution_binning,
68
+ "output_type": "pil",
 
69
  }
70
+
71
+ # Generate images
72
  images = pipe(**options).images
73
 
74
  image_paths = [save_image(img) for img in images]
 
76
 
77
 
78
  css = '''
79
+ .gradio-container { max-width: 700px !important; }
80
+ h1 { text-align: center; }
81
+ footer { visibility: hidden; }
 
 
82
  '''
83
+
84
  with gr.Blocks(css=css) as demo:
 
85
  gr.Markdown("""
86
  <div style="text-align: center; font-weight: bold; font-size: 2em;">
87
+ Womener AI (CPU Mode)
88
  </div>
89
  """)
90
 
 
149
  value=8,
150
  )
151
 
 
152
  use_negative_prompt.change(
153
  fn=lambda x: gr.update(visible=x),
154
  inputs=use_negative_prompt,
 
179
  )
180
 
181
  if __name__ == "__main__":
182
+ demo.queue(max_size=50).launch()