adamelliotfields commited on
Commit
5d1fc69
1 Parent(s): ad19934

Add aspect ratios

Browse files
Files changed (1) hide show
  1. app.py +45 -16
app.py CHANGED
@@ -25,6 +25,14 @@ seed_js = """
25
  }
26
  """
27
 
 
 
 
 
 
 
 
 
28
 
29
  def read_file(path: str) -> str:
30
  with open(path, "r", encoding="utf-8") as file:
@@ -34,8 +42,7 @@ def read_file(path: str) -> str:
34
  def random_fn():
35
  prompts = read_file("data/prompts.json")
36
  prompts = json.loads(prompts)
37
- index = random.randint(0, len(prompts) - 1)
38
- return gr.Textbox(value=prompts[index])
39
 
40
 
41
  def generate_fn(*args):
@@ -153,6 +160,20 @@ with gr.Blocks(
153
  maximum=768,
154
  step=16,
155
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
156
  scale = gr.Dropdown(
157
  choices=[(f"{s}x", s) for s in cfg.SCALES],
158
  filterable=False,
@@ -273,32 +294,40 @@ with gr.Blocks(
273
  value="🗑️",
274
  )
275
 
276
- random_btn.click(
277
- fn=random_fn,
278
- inputs=[],
279
- outputs=[prompt],
280
- )
281
 
282
- # update the seed using JavaScript
283
- refresh_btn.click(None, outputs=[seed], js=refresh_seed_js)
284
 
285
- seed.change(
286
- None,
287
- inputs=[seed],
288
- outputs=[],
289
- js=seed_js,
290
- )
291
 
292
  file_format.change(
293
  lambda f: gr.Gallery(format=f),
294
  inputs=[file_format],
295
  outputs=[output_images],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
296
  )
297
 
298
  gr.on(
299
  triggers=[generate_btn.click, prompt.submit],
300
  fn=generate_fn,
301
- api_name="api",
302
  concurrency_limit=5,
303
  outputs=[output_images],
304
  inputs=[
 
25
  }
26
  """
27
 
28
+ aspect_ratio_js = """
29
+ (ar, w, h) => {
30
+ if (!ar) return [w, h];
31
+ const [width, height] = ar.split(",");
32
+ return [parseInt(width), parseInt(height)];
33
+ }
34
+ """
35
+
36
 
37
  def read_file(path: str) -> str:
38
  with open(path, "r", encoding="utf-8") as file:
 
42
  def random_fn():
43
  prompts = read_file("data/prompts.json")
44
  prompts = json.loads(prompts)
45
+ return gr.Textbox(value=random.choice(prompts))
 
46
 
47
 
48
  def generate_fn(*args):
 
160
  maximum=768,
161
  step=16,
162
  )
163
+ aspect_ratio = gr.Dropdown(
164
+ choices=[
165
+ ("Custom", None),
166
+ ("7:9 (448x576)", "448,576"),
167
+ ("3:4 (432x576)", "432,576"),
168
+ ("1:1 (512x512)", "512,512"),
169
+ ("4:3 (576x432)", "576,432"),
170
+ ("9:7 (576x448)", "576,448"),
171
+ ],
172
+ value="448,576",
173
+ filterable=False,
174
+ label="Aspect Ratio",
175
+ min_width=180,
176
+ )
177
  scale = gr.Dropdown(
178
  choices=[(f"{s}x", s) for s in cfg.SCALES],
179
  filterable=False,
 
294
  value="🗑️",
295
  )
296
 
297
+ random_btn.click(random_fn, inputs=[], outputs=[prompt], show_api=False)
 
 
 
 
298
 
299
+ refresh_btn.click(None, inputs=[], outputs=[seed], js=refresh_seed_js)
 
300
 
301
+ seed.change(None, inputs=[seed], outputs=[], js=seed_js)
 
 
 
 
 
302
 
303
  file_format.change(
304
  lambda f: gr.Gallery(format=f),
305
  inputs=[file_format],
306
  outputs=[output_images],
307
+ show_api=False,
308
+ )
309
+
310
+ # input events are only user input; change events are both user and programmatic
311
+ aspect_ratio.input(
312
+ None,
313
+ inputs=[aspect_ratio, width, height],
314
+ outputs=[width, height],
315
+ js=aspect_ratio_js,
316
+ )
317
+
318
+ # show "Custom" aspect ratio when manually changing width or height
319
+ gr.on(
320
+ triggers=[width.input, height.input],
321
+ fn=None,
322
+ inputs=[],
323
+ outputs=[aspect_ratio],
324
+ js="() => { return null; }",
325
  )
326
 
327
  gr.on(
328
  triggers=[generate_btn.click, prompt.submit],
329
  fn=generate_fn,
330
+ api_name="generate",
331
  concurrency_limit=5,
332
  outputs=[output_images],
333
  inputs=[