IDKiro commited on
Commit
d166122
·
verified ·
1 Parent(s): 606c3f3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -41
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import base64
2
  from io import BytesIO
3
 
@@ -7,18 +8,25 @@ import torch
7
 
8
  from diffusers import StableDiffusionPipeline, AutoencoderKL, AutoencoderTiny
9
 
10
- device = "cpu" # Linux & Windows
11
- weight_type = torch.float32 # torch.float16 works as well, but pictures seem to be a bit worse
12
 
13
- pipe = StableDiffusionPipeline.from_pretrained("IDKiro/sdxs-512-dreamshaper", torch_dtype=weight_type)
 
 
14
  pipe.to(torch_device=device, torch_dtype=weight_type)
15
 
16
- vae_tiny = AutoencoderTiny.from_pretrained("IDKiro/sdxs-512-dreamshaper", subfolder="vae")
 
 
17
  vae_tiny.to(device, dtype=weight_type)
18
 
19
- vae_large = AutoencoderKL.from_pretrained("IDKiro/sdxs-512-dreamshaper", subfolder="vae_large")
 
 
20
  vae_tiny.to(device, dtype=weight_type)
21
 
 
22
  def pil_image_to_data_url(img, format="PNG"):
23
  buffered = BytesIO()
24
  img.save(buffered, format=format)
@@ -26,11 +34,12 @@ def pil_image_to_data_url(img, format="PNG"):
26
  return f"data:image/{format.lower()};base64,{img_str}"
27
 
28
 
 
29
  def run(
30
  prompt: str,
31
  device_type="GPU",
32
  vae_type=None,
33
- param_dtype='torch.float16',
34
  ) -> PIL.Image.Image:
35
  if vae_type == "tiny vae":
36
  pipe.vae = vae_tiny
@@ -38,12 +47,15 @@ def run(
38
  pipe.vae = vae_large
39
 
40
  if device_type == "CPU":
41
- device = "cpu"
42
- param_dtype = 'torch.float32'
43
  else:
44
  device = "cuda"
45
-
46
- pipe.to(torch_device=device, torch_dtype=torch.float16 if param_dtype == 'torch.float16' else torch.float32)
 
 
 
47
 
48
  result = pipe(
49
  prompt=prompt,
@@ -62,7 +74,7 @@ examples = [
62
  ]
63
 
64
  with gr.Blocks(css="style.css") as demo:
65
- gr.Markdown("# SDXS-512-DreamShaper (only CPU now)")
66
  with gr.Group():
67
  with gr.Row():
68
  with gr.Column(min_width=685):
@@ -75,38 +87,51 @@ with gr.Blocks(css="style.css") as demo:
75
  container=False,
76
  )
77
  run_button = gr.Button("Run", scale=0)
78
-
79
- device_choices = ['GPU','CPU']
80
- device_type = gr.Radio(device_choices, label='Device',
81
- value=device_choices[1],
82
- interactive=False,
83
- info='Only CPU now.')
84
-
85
- vae_choices = ['tiny vae','large vae']
86
- vae_type = gr.Radio(vae_choices, label='Image Decoder Type',
87
- value=vae_choices[0],
88
- interactive=True,
89
- info='To save GPU memory, use tiny vae. For better quality, use large vae.')
90
-
91
- dtype_choices = ['torch.float16','torch.float32']
92
- param_dtype = gr.Radio(dtype_choices,label='torch.weight_type',
93
- value=dtype_choices[0],
94
- interactive=True,
95
- info='To save GPU memory, use torch.float16. For better quality, use torch.float32.')
96
-
97
- download_output = gr.Button("Download output", elem_id="download_output")
98
 
99
- with gr.Column(min_width=512):
100
- result = gr.Image(label="Result", height=512, width=512, elem_id="output_image", show_label=False, show_download_button=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
 
102
- gr.Examples(
103
- examples=examples,
104
- inputs=prompt,
105
- outputs=result,
106
- fn=run
107
- )
108
-
109
- demo.load(None,None,None)
 
 
 
 
 
110
 
111
  inputs = [prompt, device_type, vae_type, param_dtype]
112
  outputs = [result, download_output]
 
1
+ import spaces
2
  import base64
3
  from io import BytesIO
4
 
 
8
 
9
  from diffusers import StableDiffusionPipeline, AutoencoderKL, AutoencoderTiny
10
 
11
+ device = "cuda"
12
+ weight_type = torch.float16
13
 
14
+ pipe = StableDiffusionPipeline.from_pretrained(
15
+ "IDKiro/sdxs-512-dreamshaper", torch_dtype=weight_type
16
+ )
17
  pipe.to(torch_device=device, torch_dtype=weight_type)
18
 
19
+ vae_tiny = AutoencoderTiny.from_pretrained(
20
+ "IDKiro/sdxs-512-dreamshaper", subfolder="vae"
21
+ )
22
  vae_tiny.to(device, dtype=weight_type)
23
 
24
+ vae_large = AutoencoderKL.from_pretrained(
25
+ "IDKiro/sdxs-512-dreamshaper", subfolder="vae_large"
26
+ )
27
  vae_tiny.to(device, dtype=weight_type)
28
 
29
+
30
  def pil_image_to_data_url(img, format="PNG"):
31
  buffered = BytesIO()
32
  img.save(buffered, format=format)
 
34
  return f"data:image/{format.lower()};base64,{img_str}"
35
 
36
 
37
+ @spaces.GPU
38
  def run(
39
  prompt: str,
40
  device_type="GPU",
41
  vae_type=None,
42
+ param_dtype="torch.float16",
43
  ) -> PIL.Image.Image:
44
  if vae_type == "tiny vae":
45
  pipe.vae = vae_tiny
 
47
  pipe.vae = vae_large
48
 
49
  if device_type == "CPU":
50
+ device = "cpu"
51
+ param_dtype = "torch.float32"
52
  else:
53
  device = "cuda"
54
+
55
+ pipe.to(
56
+ torch_device=device,
57
+ torch_dtype=torch.float16 if param_dtype == "torch.float16" else torch.float32,
58
+ )
59
 
60
  result = pipe(
61
  prompt=prompt,
 
74
  ]
75
 
76
  with gr.Blocks(css="style.css") as demo:
77
+ gr.Markdown("# SDXS-512-DreamShaper")
78
  with gr.Group():
79
  with gr.Row():
80
  with gr.Column(min_width=685):
 
87
  container=False,
88
  )
89
  run_button = gr.Button("Run", scale=0)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
 
91
+ device_choices = ["GPU", "CPU"]
92
+ device_type = gr.Radio(
93
+ device_choices,
94
+ label="Device",
95
+ value=device_choices[0],
96
+ interactive=True,
97
+ info="Thanks to the community for the GPU!",
98
+ )
99
+
100
+ vae_choices = ["tiny vae", "large vae"]
101
+ vae_type = gr.Radio(
102
+ vae_choices,
103
+ label="Image Decoder Type",
104
+ value=vae_choices[0],
105
+ interactive=True,
106
+ info="To save GPU memory, use tiny vae. For better quality, use large vae.",
107
+ )
108
+
109
+ dtype_choices = ["torch.float16", "torch.float32"]
110
+ param_dtype = gr.Radio(
111
+ dtype_choices,
112
+ label="torch.weight_type",
113
+ value=dtype_choices[0],
114
+ interactive=True,
115
+ info="To save GPU memory, use torch.float16. For better quality, use torch.float32.",
116
+ )
117
+
118
+ download_output = gr.Button(
119
+ "Download output", elem_id="download_output"
120
+ )
121
 
122
+ with gr.Column(min_width=512):
123
+ result = gr.Image(
124
+ label="Result",
125
+ height=512,
126
+ width=512,
127
+ elem_id="output_image",
128
+ show_label=False,
129
+ show_download_button=True,
130
+ )
131
+
132
+ gr.Examples(examples=examples, inputs=prompt, outputs=result, fn=run)
133
+
134
+ demo.load(None, None, None)
135
 
136
  inputs = [prompt, device_type, vae_type, param_dtype]
137
  outputs = [result, download_output]