Veda_Sahaja commited on
Commit
e21e1ed
1 Parent(s): 070bee0

Update space

Browse files
Files changed (1) hide show
  1. app.py +42 -59
app.py CHANGED
@@ -1,7 +1,9 @@
1
  import gradio as gr
2
  import numpy as np
3
  import random
4
- from diffusers import StableDiffusionXLPipeline
 
 
5
  import torch
6
  from typing import Tuple
7
 
@@ -66,39 +68,39 @@ def apply_style(style_name: str, positive: str, negative: str = "") -> Tuple[str
66
  p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
67
  return p.replace("{prompt}", positive), n + negative
68
 
69
- device = "cuda" if torch.cuda.is_available() else "cpu"
 
 
 
 
 
 
 
 
 
 
70
 
71
- if torch.cuda.is_available():
72
- # torch.cuda.max_memory_allocated(device=device)
73
- pipe = StableDiffusionXLPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16, variant="fp16", use_safetensors=True)
74
- # pipe.enable_xformers_memory_efficient_attention()
75
- pipe = pipe.to(device)
76
- # else:
77
- # pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", use_safetensors=True)
78
- # pipe = pipe.to(device)
79
 
80
  MAX_SEED = np.iinfo(np.int32).max
81
  MAX_IMAGE_SIZE = 1024
82
 
83
- def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, style_name=None):
 
84
 
85
- if randomize_seed:
86
- seed = random.randint(0, MAX_SEED)
87
-
88
  generator = torch.Generator().manual_seed(seed)
89
 
90
  prompt, negative_prompt = apply_style(style_name, prompt, negative_prompt)
91
-
92
  image = pipe(
93
- prompt = prompt,
94
  negative_prompt = negative_prompt,
95
- guidance_scale = guidance_scale,
96
- num_inference_steps = num_inference_steps,
97
- width = width,
98
  height = height,
99
  generator = generator
100
- ).images[0]
101
-
102
  return image
103
 
104
  examples = [
@@ -120,15 +122,15 @@ else:
120
  power_device = "CPU"
121
 
122
  with gr.Blocks(css=css) as demo:
123
-
124
  with gr.Column(elem_id="col-container"):
125
  gr.Markdown(f"""
126
  # Text-to-Image Gradio Template
127
  Currently running on {power_device}.
128
  """)
129
-
130
  with gr.Row():
131
-
132
  prompt = gr.Text(
133
  label="Prompt",
134
  show_label=False,
@@ -136,74 +138,55 @@ with gr.Blocks(css=css) as demo:
136
  placeholder="Enter your prompt",
137
  container=False,
138
  )
139
-
140
  run_button = gr.Button("Run", scale=0)
141
-
142
  result = gr.Image(label="Result", show_label=False)
143
 
144
  with gr.Accordion("Advanced Settings", open=False):
145
  negative_prompt = gr.Textbox(
146
  label="Negative prompt",
147
- show_label=False,
148
  max_lines=1,
149
  placeholder="Enter a negative prompt",
150
- elem_id="negative-prompt-text-input",
151
- value="low-quality, text, blurry, fuzziness"
152
  )
153
-
154
  style_selection = gr.Radio(
155
  show_label=True, container=True, interactive=True,
156
  choices=STYLE_NAMES,
157
  value=DEFAULT_STYLE_NAME,
158
  label='Image Style'
159
  )
160
-
161
- seed = gr.Slider(
162
- label="Seed",
163
- minimum=0,
164
- maximum=MAX_SEED,
165
- step=1,
166
- value=0,
167
- )
168
-
169
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
170
-
171
  with gr.Row():
172
-
173
  width = gr.Slider(
174
  label="Width",
175
  minimum=256,
176
  maximum=MAX_IMAGE_SIZE,
177
  step=32,
178
- value=512,
179
  )
180
-
181
  height = gr.Slider(
182
  label="Height",
183
  minimum=256,
184
  maximum=MAX_IMAGE_SIZE,
185
  step=32,
186
- value=512,
187
  )
188
-
189
  with gr.Row():
190
-
191
  guidance_scale = gr.Slider(
192
  label="Guidance scale",
193
  minimum=0.0,
194
- maximum=10.0,
195
  step=0.1,
196
- value=0.0,
197
  )
198
-
199
- num_inference_steps = gr.Slider(
200
- label="Number of inference steps",
201
- minimum=1,
202
- maximum=12,
203
- step=1,
204
- value=2,
205
- )
206
-
207
  gr.Examples(
208
  examples = examples,
209
  inputs = [prompt]
@@ -211,7 +194,7 @@ with gr.Blocks(css=css) as demo:
211
 
212
  run_button.click(
213
  fn = infer,
214
- inputs = [prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, style_selection],
215
  outputs = [result]
216
  )
217
 
 
1
  import gradio as gr
2
  import numpy as np
3
  import random
4
+ from diffusers import StableDiffusionXLPipeline, LCMScheduler, UNet2DConditionModel, EulerDiscreteScheduler
5
+ from safetensors.torch import load_file
6
+ from huggingface_hub import hf_hub_download
7
  import torch
8
  from typing import Tuple
9
 
 
68
  p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
69
  return p.replace("{prompt}", positive), n + negative
70
 
71
+ base = "stabilityai/stable-diffusion-xl-base-1.0"
72
+ repo = "ByteDance/SDXL-Lightning"
73
+ ckpt = "sdxl_lightning_4step_unet.safetensors" # Use the correct ckpt for your step setting!
74
+
75
+ # Load model.
76
+ unet = UNet2DConditionModel.from_config(base, subfolder="unet").to("cuda", torch.float16)
77
+ unet.load_state_dict(load_file(hf_hub_download(repo, ckpt), device="cuda"))
78
+ pipe = StableDiffusionXLPipeline.from_pretrained(base, unet=unet, torch_dtype=torch.float16, variant="fp16").to("cuda")
79
+
80
+ # Ensure sampler uses "trailing" timesteps.
81
+ pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config, timestep_spacing="trailing")
82
 
 
 
 
 
 
 
 
 
83
 
84
  MAX_SEED = np.iinfo(np.int32).max
85
  MAX_IMAGE_SIZE = 1024
86
 
87
+ def infer(prompt, negative_prompt, width, height, guidance_scale, style_name=None):
88
+ seed = random.randint(0,4294967295)
89
 
 
 
 
90
  generator = torch.Generator().manual_seed(seed)
91
 
92
  prompt, negative_prompt = apply_style(style_name, prompt, negative_prompt)
93
+
94
  image = pipe(
95
+ prompt = prompt,
96
  negative_prompt = negative_prompt,
97
+ guidance_scale = guidance_scale,
98
+ # num_inference_steps = num_inference_steps,
99
+ width = width,
100
  height = height,
101
  generator = generator
102
+ ).images[0]
103
+
104
  return image
105
 
106
  examples = [
 
122
  power_device = "CPU"
123
 
124
  with gr.Blocks(css=css) as demo:
125
+
126
  with gr.Column(elem_id="col-container"):
127
  gr.Markdown(f"""
128
  # Text-to-Image Gradio Template
129
  Currently running on {power_device}.
130
  """)
131
+
132
  with gr.Row():
133
+
134
  prompt = gr.Text(
135
  label="Prompt",
136
  show_label=False,
 
138
  placeholder="Enter your prompt",
139
  container=False,
140
  )
141
+
142
  run_button = gr.Button("Run", scale=0)
143
+
144
  result = gr.Image(label="Result", show_label=False)
145
 
146
  with gr.Accordion("Advanced Settings", open=False):
147
  negative_prompt = gr.Textbox(
148
  label="Negative prompt",
149
+ # show_label=False,
150
  max_lines=1,
151
  placeholder="Enter a negative prompt",
152
+ elem_id="negative-prompt-text-input"
 
153
  )
154
+
155
  style_selection = gr.Radio(
156
  show_label=True, container=True, interactive=True,
157
  choices=STYLE_NAMES,
158
  value=DEFAULT_STYLE_NAME,
159
  label='Image Style'
160
  )
161
+
 
 
 
 
 
 
 
 
 
 
162
  with gr.Row():
163
+
164
  width = gr.Slider(
165
  label="Width",
166
  minimum=256,
167
  maximum=MAX_IMAGE_SIZE,
168
  step=32,
169
+ value=1024,
170
  )
171
+
172
  height = gr.Slider(
173
  label="Height",
174
  minimum=256,
175
  maximum=MAX_IMAGE_SIZE,
176
  step=32,
177
+ value=1024,
178
  )
179
+
180
  with gr.Row():
181
+
182
  guidance_scale = gr.Slider(
183
  label="Guidance scale",
184
  minimum=0.0,
185
+ maximum=50.0,
186
  step=0.1,
187
+ value=5,
188
  )
189
+
 
 
 
 
 
 
 
 
190
  gr.Examples(
191
  examples = examples,
192
  inputs = [prompt]
 
194
 
195
  run_button.click(
196
  fn = infer,
197
+ inputs = [prompt, negative_prompt, width, height, guidance_scale, style_selection],
198
  outputs = [result]
199
  )
200