6Morpheus6 commited on
Commit
2290da3
·
verified ·
1 Parent(s): ff83c1d

UI improvement

Browse files

- Fixed height
- Increase width
- Download button
- Clear cache
- Code fix for follow up generations

Files changed (1) hide show
  1. app.py +49 -26
app.py CHANGED
@@ -1,6 +1,8 @@
 
1
  import gradio as gr
2
  import torch
3
  import devicetorch
 
4
  from diffusers import StableDiffusionXLPipeline, StableDiffusionPipeline, LCMScheduler
5
  from diffusers.schedulers import TCDScheduler
6
 
@@ -60,35 +62,41 @@ def generate_image(
60
 
61
  pipe = pipe_sdxl if mode == "sdxl" else pipe_sd15
62
 
63
- if loaded != (ckpt + mode):
64
- pipe.load_lora_weights(
65
  "wangfuyun/PCM_Weights", weight_name=checkpoint, subfolder=mode
66
  )
67
- loaded = ckpt + mode
68
-
69
- if ckpt == "LCM-Like LoRA":
70
- pipe.scheduler = LCMScheduler()
71
- else:
72
- pipe.scheduler = TCDScheduler(
73
- num_train_timesteps=1000,
74
- beta_start=0.00085,
75
- beta_end=0.012,
76
- beta_schedule="scaled_linear",
77
- timestep_spacing="trailing",
78
- )
79
 
80
  results = pipe(
81
  prompt, num_inference_steps=num_inference_steps, guidance_scale=guidance_scale
82
  )
83
 
 
 
 
 
 
84
  # if SAFETY_CHECKER:
85
  # images, has_nsfw_concepts = check_nsfw_images(results.images)
86
  # if any(has_nsfw_concepts):
87
  # gr.Warning("NSFW content detected.")
88
  # return Image.new("RGB", (512, 512))
89
  # return images[0]
90
- return results.images[0]
91
 
 
 
92
 
93
  def update_steps(ckpt):
94
  num_inference_steps = checkpoints[ckpt][1]
@@ -99,7 +107,14 @@ def update_steps(ckpt):
99
 
100
  css = """
101
  .gradio-container {
102
- max-width: 60rem !important;
 
 
 
 
 
 
 
103
  }
104
  """
105
  with gr.Blocks(css=css) as demo:
@@ -108,14 +123,12 @@ with gr.Blocks(css=css) as demo:
108
  # Phased Consistency Model
109
 
110
  Phased Consistency Model (PCM) is an image generation technique that addresses the limitations of the Latent Consistency Model (LCM) in high-resolution and text-conditioned image generation.
111
- PCM outperforms LCM across various generation settings and achieves state-of-the-art results in both image and video generation.
112
-
113
- [[paper](https://huggingface.co/papers/2405.18407)] [[arXiv](https://arxiv.org/abs/2405.18407)] [[code](https://github.com/G-U-N/Phased-Consistency-Model)] [[project page](https://g-u-n.github.io/projects/pcm)]
114
  """
115
  )
116
  with gr.Group():
117
  with gr.Row():
118
- prompt = gr.Textbox(label="Prompt", scale=8)
119
  ckpt = gr.Dropdown(
120
  label="Select inference steps",
121
  choices=list(checkpoints.keys()),
@@ -137,10 +150,12 @@ PCM outperforms LCM across various generation settings and achieves state-of-the
137
  show_progress=False,
138
  )
139
 
 
140
  submit_sdxl = gr.Button("Run on SDXL", scale=1)
141
  submit_sd15 = gr.Button("Run on SD15", scale=1)
142
 
143
- img = gr.Image(label="PCM Image")
 
144
  gr.Examples(
145
  examples=[
146
  [" astronaut walking on the moon", "4-Step", 4],
@@ -171,7 +186,7 @@ PCM outperforms LCM across various generation settings and achieves state-of-the
171
  ],
172
  ],
173
  inputs=[prompt, ckpt, steps],
174
- outputs=[img],
175
  fn=generate_image,
176
  #cache_examples="lazy",
177
  )
@@ -180,14 +195,22 @@ PCM outperforms LCM across various generation settings and achieves state-of-the
180
  fn=generate_image,
181
  triggers=[ckpt.change, prompt.submit, submit_sdxl.click],
182
  inputs=[prompt, ckpt, steps],
183
- outputs=[img],
184
- )
 
 
 
 
185
  gr.on(
186
  fn=lambda *args: generate_image(*args, mode="sd15"),
187
  triggers=[submit_sd15.click],
188
  inputs=[prompt, ckpt, steps],
189
- outputs=[img],
190
- )
 
 
 
 
191
 
192
 
193
  demo.queue(api_open=False).launch(show_api=False)
 
1
+ import os
2
  import gradio as gr
3
  import torch
4
  import devicetorch
5
+ import tempfile
6
  from diffusers import StableDiffusionXLPipeline, StableDiffusionPipeline, LCMScheduler
7
  from diffusers.schedulers import TCDScheduler
8
 
 
62
 
63
  pipe = pipe_sdxl if mode == "sdxl" else pipe_sd15
64
 
65
+ pipe.load_lora_weights(
 
66
  "wangfuyun/PCM_Weights", weight_name=checkpoint, subfolder=mode
67
  )
68
+
69
+
70
+ if ckpt == "LCM-Like LoRA":
71
+ pipe.scheduler = LCMScheduler()
72
+ else:
73
+ pipe.scheduler = TCDScheduler(
74
+ num_train_timesteps=1000,
75
+ beta_start=0.00085,
76
+ beta_end=0.012,
77
+ beta_schedule="scaled_linear",
78
+ timestep_spacing="trailing",
79
+ )
80
 
81
  results = pipe(
82
  prompt, num_inference_steps=num_inference_steps, guidance_scale=guidance_scale
83
  )
84
 
85
+
86
+ gradio_temp_dir = os.environ.get('GRADIO_TEMP_DIR', tempfile.gettempdir())
87
+ temp_file_path = os.path.join(gradio_temp_dir, "image.png")
88
+ results.images[0].save(temp_file_path, format="PNG")
89
+
90
  # if SAFETY_CHECKER:
91
  # images, has_nsfw_concepts = check_nsfw_images(results.images)
92
  # if any(has_nsfw_concepts):
93
  # gr.Warning("NSFW content detected.")
94
  # return Image.new("RGB", (512, 512))
95
  # return images[0]
96
+ return results.images[0], temp_file_path
97
 
98
+ def clear_cache():
99
+ devicetorch.empty_cache(torch)
100
 
101
  def update_steps(ckpt):
102
  num_inference_steps = checkpoints[ckpt][1]
 
107
 
108
  css = """
109
  .gradio-container {
110
+ max-width: 95vw !important;
111
+ margin: auto !important
112
+ }
113
+ .img img {
114
+ height: 70vh !important
115
+ }
116
+ #row-height {
117
+ height: 65px !important
118
  }
119
  """
120
  with gr.Blocks(css=css) as demo:
 
123
  # Phased Consistency Model
124
 
125
  Phased Consistency Model (PCM) is an image generation technique that addresses the limitations of the Latent Consistency Model (LCM) in high-resolution and text-conditioned image generation.
126
+ PCM outperforms LCM across various generation settings and achieves state-of-the-art results in both image and video generation.     [[paper](https://huggingface.co/papers/2405.18407)] [[arXiv](https://arxiv.org/abs/2405.18407)] [[code](https://github.com/G-U-N/Phased-Consistency-Model)] [[project page](https://g-u-n.github.io/projects/pcm)]
 
 
127
  """
128
  )
129
  with gr.Group():
130
  with gr.Row():
131
+ prompt = gr.Textbox(label="Prompt", scale=4)
132
  ckpt = gr.Dropdown(
133
  label="Select inference steps",
134
  choices=list(checkpoints.keys()),
 
150
  show_progress=False,
151
  )
152
 
153
+ with gr.Row():
154
  submit_sdxl = gr.Button("Run on SDXL", scale=1)
155
  submit_sd15 = gr.Button("Run on SD15", scale=1)
156
 
157
+ img = gr.Image(label="PCM Image", elem_classes="img")
158
+ download_image = gr.File(label="Download Image", file_count="single", interactive=False, elem_id="row-height")
159
  gr.Examples(
160
  examples=[
161
  [" astronaut walking on the moon", "4-Step", 4],
 
186
  ],
187
  ],
188
  inputs=[prompt, ckpt, steps],
189
+ outputs=[img, download_image],
190
  fn=generate_image,
191
  #cache_examples="lazy",
192
  )
 
195
  fn=generate_image,
196
  triggers=[ckpt.change, prompt.submit, submit_sdxl.click],
197
  inputs=[prompt, ckpt, steps],
198
+ outputs=[img, download_image],
199
+ ).then(
200
+ fn=clear_cache,
201
+ inputs=[],
202
+ outputs=None
203
+ )
204
  gr.on(
205
  fn=lambda *args: generate_image(*args, mode="sd15"),
206
  triggers=[submit_sd15.click],
207
  inputs=[prompt, ckpt, steps],
208
+ outputs=[img, download_image],
209
+ ).then(
210
+ fn=clear_cache,
211
+ inputs=[],
212
+ outputs=None
213
+ )
214
 
215
 
216
  demo.queue(api_open=False).launch(show_api=False)