ash123 commited on
Commit
fedc156
1 Parent(s): c0e4c16

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -83
app.py CHANGED
@@ -12,7 +12,7 @@ pipeline, pipeline_params = FlaxStableDiffusionPipeline.from_pretrained(
12
  )
13
 
14
 
15
- def generate_image(prompt: str,negative_prompt:str = "" , inference_steps: int = 25, prng_seed: int = 0, guidance_scale: float = 9):
16
  rng = jax.random.PRNGKey(int(prng_seed))
17
  rng = jax.random.split(rng, jax.device_count())
18
  p_params = replicate(pipeline_params)
@@ -21,18 +21,19 @@ def generate_image(prompt: str,negative_prompt:str = "" , inference_steps: int =
21
  prompt_ids = pipeline.prepare_inputs([prompt] * num_samples)
22
  prompt_ids = shard(prompt_ids)
23
  if negative_prompt == "":
24
- images = pipeline(
25
  prompt_ids=prompt_ids,
26
  params=p_params,
27
  prng_seed=rng,
28
  height=128,
29
  width=128,
30
  num_inference_steps=int(inference_steps),
31
- guidance_scale =float(guidance_scale),
32
  jit=True,
33
  ).images
34
- else:
35
- neg_prompt_ids = pipeline.prepare_inputs([negative_prompt] * num_samples)
 
36
  neg_prompt_ids = shard(neg_prompt_ids)
37
  images = pipeline(
38
  prompt_ids=prompt_ids,
@@ -42,17 +43,19 @@ def generate_image(prompt: str,negative_prompt:str = "" , inference_steps: int =
42
  width=128,
43
  num_inference_steps=int(inference_steps),
44
  neg_prompt_ids=neg_prompt_ids,
45
- guidance_scale =float(guidance_scale),
46
  jit=True,
47
  ).images
48
 
49
  images = images.reshape((num_samples,) + images.shape[-3:])
50
  images = pipeline.numpy_to_pil(images)
51
  return images[0]
 
 
52
  examples = [
53
- ["A watercolor painting of a bird"],
54
- ["A watercolor painting of an otter"]
55
- ]
56
  css = """
57
  .gradio-container {
58
  font-family: 'IBM Plex Sans', sans-serif;
@@ -171,9 +174,6 @@ css = """
171
  .gr-form{
172
  flex: 1 1 50%; border-top-right-radius: 0; border-bottom-right-radius: 0;
173
  }
174
- #prompt-container{
175
- gap: 0;
176
- }
177
  #prompt-text-input, #negative-prompt-text-input{padding: .45rem 0.625rem}
178
  #component-16{border-top-width: 1px!important;margin-top: 1em}
179
  .image_duplication{position: absolute; width: 100px; left: 50px}
@@ -193,39 +193,6 @@ with block as demo:
193
  font-size: 1.75rem;
194
  "
195
  >
196
- <svg
197
- width="0.65em"
198
- height="0.65em"
199
- viewBox="0 0 115 115"
200
- fill="none"
201
- xmlns="http://www.w3.org/2000/svg"
202
- >
203
- <rect width="23" height="23" fill="white"></rect>
204
- <rect y="69" width="23" height="23" fill="white"></rect>
205
- <rect x="23" width="23" height="23" fill="#AEAEAE"></rect>
206
- <rect x="23" y="69" width="23" height="23" fill="#AEAEAE"></rect>
207
- <rect x="46" width="23" height="23" fill="white"></rect>
208
- <rect x="46" y="69" width="23" height="23" fill="white"></rect>
209
- <rect x="69" width="23" height="23" fill="black"></rect>
210
- <rect x="69" y="69" width="23" height="23" fill="black"></rect>
211
- <rect x="92" width="23" height="23" fill="#D9D9D9"></rect>
212
- <rect x="92" y="69" width="23" height="23" fill="#AEAEAE"></rect>
213
- <rect x="115" y="46" width="23" height="23" fill="white"></rect>
214
- <rect x="115" y="115" width="23" height="23" fill="white"></rect>
215
- <rect x="115" y="69" width="23" height="23" fill="#D9D9D9"></rect>
216
- <rect x="92" y="46" width="23" height="23" fill="#AEAEAE"></rect>
217
- <rect x="92" y="115" width="23" height="23" fill="#AEAEAE"></rect>
218
- <rect x="92" y="69" width="23" height="23" fill="white"></rect>
219
- <rect x="69" y="46" width="23" height="23" fill="white"></rect>
220
- <rect x="69" y="115" width="23" height="23" fill="white"></rect>
221
- <rect x="69" y="69" width="23" height="23" fill="#D9D9D9"></rect>
222
- <rect x="46" y="46" width="23" height="23" fill="black"></rect>
223
- <rect x="46" y="115" width="23" height="23" fill="black"></rect>
224
- <rect x="46" y="69" width="23" height="23" fill="black"></rect>
225
- <rect x="23" y="46" width="23" height="23" fill="#D9D9D9"></rect>
226
- <rect x="23" y="115" width="23" height="23" fill="#AEAEAE"></rect>
227
- <rect x="23" y="69" width="23" height="23" fill="black"></rect>
228
- </svg>
229
  <h1 style="font-weight: 900; margin-bottom: 7px;margin-top:5px">
230
  Stable Diffusion Nano Demo
231
  </h1>
@@ -238,69 +205,61 @@ with block as demo:
238
  )
239
  with gr.Group():
240
  with gr.Box():
241
- with gr.Row(elem_id="prompt-container").style(mobile_collapse=False, equal_height=True):
242
  with gr.Column():
243
  prompt_input = gr.Textbox(
244
  label="Enter your prompt",
245
- show_label=False,
246
  max_lines=1,
247
  placeholder="A watercolor painting of a bird",
248
  elem_id="prompt-text-input",
249
- ).style(
250
- border=(True, False, True, True),
251
- rounded=(True, False, False, True),
252
- container=False,
253
  )
254
  negative = gr.Textbox(
255
  label="Enter your negative prompt",
256
- show_label=False,
257
  max_lines=1,
258
  placeholder="tree",
259
  elem_id="negative-prompt-text-input",
260
- ).style(
261
- border=(True, False, True, True),
262
- rounded=(True, False, False, True),
263
- container=False,
264
  )
265
- btn = gr.Button("Generate image").style(
266
- margin=False,
267
- rounded=(False, True, True, False),
268
- full_width=False,
269
- )
270
 
271
  gallery = gr.Gallery(
272
  label="Generated images", show_label=False, elem_id="gallery"
273
- ).style(grid=[2], height="auto")
274
 
275
  with gr.Group(elem_id="container-advanced-btns"):
276
- #advanced_button = gr.Button("Advanced options", elem_id="advanced-btn")
277
  with gr.Group(elem_id="share-btn-container"):
278
  community_icon = gr.HTML(community_icon_html)
279
  loading_icon = gr.HTML(loading_icon_html)
280
- share_button = gr.Button("Share to community", elem_id="share-btn")
 
281
 
282
  with gr.Accordion("Advanced settings", open=False):
283
- # gr.Markdown("Advanced settings are temporarily unavailable")
284
- # samples = gr.Slider(label="Images", minimum=1, maximum=4, value=4, step=1)
285
- # steps = gr.Slider(label="Steps", minimum=1, maximum=50, value=45, step=1)
286
  inf_steps_input = gr.inputs.Slider(
287
  minimum=1, maximum=100, default=25, step=1, label="Inference Steps"
288
- )
289
  seed_input = gr.inputs.Number(default=0, label="Seed")
290
  guidance_scale = gr.Slider(
291
  label="Guidance Scale", minimum=0, maximum=50, value=9, step=0.1
292
- )
293
 
294
- ex = gr.Examples(examples=examples,
295
- fn=generate_image,
296
- inputs=[prompt_input, negative, inf_steps_input,seed_input,guidance_scale],
297
- outputs=[gallery, community_icon, loading_icon, share_button],
298
- cache_examples=True)
 
 
299
  ex.dataset.headers = [""]
300
- negative.submit(generate_image, inputs=[prompt_input, negative, inf_steps_input,seed_input,guidance_scale], outputs=[gallery], postprocess=False)
301
- prompt_input.submit(generate_image, inputs=[prompt_input, negative, inf_steps_input,seed_input,guidance_scale], outputs=[gallery], postprocess=False)
302
- btn.click(generate_image, inputs=[prompt_input, negative, inf_steps_input,seed_input,guidance_scale], outputs=[gallery], postprocess=False)
303
-
 
 
 
304
  share_button.click(
305
  None,
306
  [],
@@ -317,13 +276,14 @@ with block as demo:
317
  )
318
  with gr.Accordion(label="License", open=False):
319
  gr.HTML(
320
- """<div class="acknowledgments">
 
321
  <p><h4>LICENSE</h4>
322
- The model is licensed with a <a href="https://huggingface.co/stabilityai/stable-diffusion-2/blob/main/LICENSE-MODEL" style="text-decoration: underline;" target="_blank">CreativeML OpenRAIL++</a> license. The authors claim no rights on the outputs you generate, you are free to use them and are accountable for their use which must not go against the provisions set in this license. The license forbids you from sharing any content that violates any laws, produce any harm to a person, disseminate any personal information that would be meant for harm, spread misinformation and target vulnerable groups. For the full list of restrictions please <a href="https://huggingface.co/spaces/CompVis/stable-diffusion-license" target="_blank" style="text-decoration: underline;" target="_blank">read the license</a></p>
323
  <p><h4>Biases and content acknowledgment</h4>
324
- Despite how impressive being able to turn text into image is, beware to the fact that this model may output content that reinforces or exacerbates societal biases, as well as realistic faces, pornography and violence. The model was trained on the <a href="https://laion.ai/blog/laion-5b/" style="text-decoration: underline;" target="_blank">LAION-5B dataset</a>, which scraped non-curated image-text-pairs from the internet (the exception being the removal of illegal content) and is meant for research purposes. You can read more in the <a href="https://huggingface.co/CompVis/stable-diffusion-v1-4" style="text-decoration: underline;" target="_blank">model card</a></p>
325
  </div>
326
  """
327
  )
328
-
329
- demo.launch()
 
12
  )
13
 
14
 
15
+ def generate_image(prompt: str, negative_prompt: str = "", inference_steps: int = 25, prng_seed: int = 0, guidance_scale: float = 9):
16
  rng = jax.random.PRNGKey(int(prng_seed))
17
  rng = jax.random.split(rng, jax.device_count())
18
  p_params = replicate(pipeline_params)
 
21
  prompt_ids = pipeline.prepare_inputs([prompt] * num_samples)
22
  prompt_ids = shard(prompt_ids)
23
  if negative_prompt == "":
24
+ images = pipeline(
25
  prompt_ids=prompt_ids,
26
  params=p_params,
27
  prng_seed=rng,
28
  height=128,
29
  width=128,
30
  num_inference_steps=int(inference_steps),
31
+ guidance_scale=float(guidance_scale),
32
  jit=True,
33
  ).images
34
+ else:
35
+ neg_prompt_ids = pipeline.prepare_inputs(
36
+ [negative_prompt] * num_samples)
37
  neg_prompt_ids = shard(neg_prompt_ids)
38
  images = pipeline(
39
  prompt_ids=prompt_ids,
 
43
  width=128,
44
  num_inference_steps=int(inference_steps),
45
  neg_prompt_ids=neg_prompt_ids,
46
+ guidance_scale=float(guidance_scale),
47
  jit=True,
48
  ).images
49
 
50
  images = images.reshape((num_samples,) + images.shape[-3:])
51
  images = pipeline.numpy_to_pil(images)
52
  return images[0]
53
+
54
+
55
  examples = [
56
+ ["A watercolor painting of a bird"],
57
+ ["A watercolor painting of an otter"]
58
+ ]
59
  css = """
60
  .gradio-container {
61
  font-family: 'IBM Plex Sans', sans-serif;
 
174
  .gr-form{
175
  flex: 1 1 50%; border-top-right-radius: 0; border-bottom-right-radius: 0;
176
  }
 
 
 
177
  #prompt-text-input, #negative-prompt-text-input{padding: .45rem 0.625rem}
178
  #component-16{border-top-width: 1px!important;margin-top: 1em}
179
  .image_duplication{position: absolute; width: 100px; left: 50px}
 
193
  font-size: 1.75rem;
194
  "
195
  >
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
196
  <h1 style="font-weight: 900; margin-bottom: 7px;margin-top:5px">
197
  Stable Diffusion Nano Demo
198
  </h1>
 
205
  )
206
  with gr.Group():
207
  with gr.Box():
208
+ with gr.Row():
209
  with gr.Column():
210
  prompt_input = gr.Textbox(
211
  label="Enter your prompt",
 
212
  max_lines=1,
213
  placeholder="A watercolor painting of a bird",
214
  elem_id="prompt-text-input",
 
 
 
 
215
  )
216
  negative = gr.Textbox(
217
  label="Enter your negative prompt",
 
218
  max_lines=1,
219
  placeholder="tree",
220
  elem_id="negative-prompt-text-input",
 
 
 
 
221
  )
222
+ btn = gr.Button("Generate image")
 
 
 
 
223
 
224
  gallery = gr.Gallery(
225
  label="Generated images", show_label=False, elem_id="gallery"
226
+ ).style(container=True, height="auto")
227
 
228
  with gr.Group(elem_id="container-advanced-btns"):
229
+ # advanced_button = gr.Button("Advanced options", elem_id="advanced-btn")
230
  with gr.Group(elem_id="share-btn-container"):
231
  community_icon = gr.HTML(community_icon_html)
232
  loading_icon = gr.HTML(loading_icon_html)
233
+ share_button = gr.Button(
234
+ "Share to community", elem_id="share-btn")
235
 
236
  with gr.Accordion("Advanced settings", open=False):
237
+ # gr.Markdown("Advanced settings are temporarily unavailable")
238
+ # samples = gr.Slider(label="Images", minimum=1, maximum=4, value=4, step=1)
239
+ # steps = gr.Slider(label="Steps", minimum=1, maximum=50, value=45, step=1)
240
  inf_steps_input = gr.inputs.Slider(
241
  minimum=1, maximum=100, default=25, step=1, label="Inference Steps"
242
+ )
243
  seed_input = gr.inputs.Number(default=0, label="Seed")
244
  guidance_scale = gr.Slider(
245
  label="Guidance Scale", minimum=0, maximum=50, value=9, step=0.1
246
+ )
247
 
248
+ ex = gr.Examples(examples=examples,
249
+ fn=generate_image,
250
+ inputs=[prompt_input, negative,
251
+ inf_steps_input, seed_input, guidance_scale],
252
+ outputs=[gallery, community_icon,
253
+ loading_icon, share_button],
254
+ cache_examples=False)
255
  ex.dataset.headers = [""]
256
+ negative.submit(generate_image, inputs=[
257
+ prompt_input, negative, inf_steps_input, seed_input, guidance_scale], outputs=[gallery], postprocess=False)
258
+ prompt_input.submit(generate_image, inputs=[
259
+ prompt_input, negative, inf_steps_input, seed_input, guidance_scale], outputs=[gallery], postprocess=False)
260
+ btn.click(generate_image, inputs=[prompt_input, negative, inf_steps_input,
261
+ seed_input, guidance_scale], outputs=[gallery], postprocess=False)
262
+
263
  share_button.click(
264
  None,
265
  [],
 
276
  )
277
  with gr.Accordion(label="License", open=False):
278
  gr.HTML(
279
+ """
280
+ <div class="acknowledgments">
281
  <p><h4>LICENSE</h4>
282
+ The model is licensed with a <a href="https://huggingface.co/stabilityai/stable-diffusion-2/blob/main/LICENSE-MODEL" style="text-decoration: underline;" target="_blank">CreativeML OpenRAIL++</a> license. The authors claim no rights on the outputs you generate, you are free to use them and are accountable for their use which must not go against the provisions set in this license. The license forbids you from sharing any content that violates any laws, produce any harm to a person, disseminate any personal information that would be meant for harm, spread misinformation and target vulnerable groups. For the full list of restrictions please <a href="https://huggingface.co/spaces/CompVis/stable-diffusion-license" target="_blank" style="text-decoration: underline;" target="_blank">read the license</a></p>
283
  <p><h4>Biases and content acknowledgment</h4>
284
+ Despite how impressive being able to turn text into image is, beware to the fact that this model may output content that reinforces or exacerbates societal biases, as well as realistic faces, pornography and violence. The model was trained on the <a href="https://laion.ai/blog/laion-5b/" style="text-decoration: underline;" target="_blank">LAION-5B dataset</a>, which scraped non-curated image-text-pairs from the internet (the exception being the removal of illegal content) and is meant for research purposes. You can read more in the <a href="https://huggingface.co/CompVis/stable-diffusion-v1-4" style="text-decoration: underline;" target="_blank">model card</a></p>
285
  </div>
286
  """
287
  )
288
+
289
+ demo.launch()