kayte0342 commited on
Commit
759eb6f
·
verified ·
1 Parent(s): 41ab360

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -29
app.py CHANGED
@@ -306,24 +306,21 @@ css = '''
306
  #title{text-align: center}
307
  #title h1{font-size: 3em; display:inline-flex; align-items:center}
308
  #title img{width: 100px; margin-right: 0.5em}
309
- #gallery .grid-wrap{height: 10vh}
310
  #lora_list{background: var(--block-background-fill);padding: 0 1em .3em; font-size: 90%}
311
  .card_internal{display: flex;height: 100px;margin-top: .5em}
312
  .card_internal img{margin-right: 1em}
313
  .styler{--form-gap-width: 0px !important}
314
  #progress{height:30px}
315
- #progress .generating{display:none}
316
  .progress-container {width: 100%;height: 30px;background-color: #f0f0f0;border-radius: 15px;overflow: hidden;margin-bottom: 20px}
317
  .progress-bar {height: 100%;background-color: #4f46e5;width: calc(var(--current) / var(--total) * 100%);transition: width 0.5s ease-in-out}
318
  '''
319
- font=[gr.themes.GoogleFont("Source Sans Pro"), "Arial", "sans-serif"]
320
- # Begin Gradio Blocks
321
  with gr.Blocks(theme=gr.themes.Soft(font=font), css=css, delete_cache=(60, 60)) as app:
322
  title = gr.HTML(
323
  """<h1><img src="https://huggingface.co/spaces/kayte0342/test/resolve/main/DA4BE61E-A0BD-4254-A1B6-AD3C05D18A9C%20(1).png?download=true" alt="LoRA"> FLUX LoRA Kayte's Space</h1>""",
324
  elem_id="title",
325
  )
326
-
327
  # Hidden textbox to store the JSON string of selected indices
328
  selected_indices_hidden = gr.Textbox(value="[]", visible=False)
329
 
@@ -336,17 +333,15 @@ with gr.Blocks(theme=gr.themes.Soft(font=font), css=css, delete_cache=(60, 60))
336
  with gr.Row():
337
  with gr.Column():
338
  selected_info = gr.Markdown("")
339
- # Create a custom container for LoRA selection with checkboxes
340
  lora_selection_container = gr.Column()
341
- # We'll collect individual checkbox components in a list for later use
342
  lora_checkbox_list = []
343
  for idx, lora in enumerate(loras):
344
  with gr.Row():
345
  gr.Image(value=lora["image"], label=lora["title"], height=100)
346
  checkbox = gr.Checkbox(label="Select", value=False, elem_id=f"lora_checkbox_{idx}")
347
  lora_checkbox_list.append(checkbox)
348
- # Add a hidden update button (invisible) to update the hidden state; it can be triggered programmatically.
349
- update_selection_btn = gr.Button("Update LoRA Selection", visible=False)
350
  with gr.Column():
351
  progress_bar = gr.Markdown(elem_id="progress", visible=False)
352
  result = gr.Image(label="Generated Image")
@@ -368,19 +363,25 @@ with gr.Blocks(theme=gr.themes.Soft(font=font), css=css, delete_cache=(60, 60))
368
  seed = gr.Slider(label="Seed", minimum=0, maximum=2**32-1, step=1, value=0, randomize=True)
369
  lora_scale = gr.Slider(label="LoRA Scale", minimum=0, maximum=3, step=0.01, value=0.95)
370
 
371
- # Function to combine checkbox states into a JSON list of selected indices.
372
  def combine_selections(*checkbox_values):
373
  selected_indices = [i for i, v in enumerate(checkbox_values) if v]
374
  return json.dumps(selected_indices)
375
 
376
- # When the (invisible) update button is clicked, update the hidden state.
377
- update_selection_btn.click(
 
 
378
  combine_selections,
379
  inputs=lora_checkbox_list,
380
  outputs=selected_indices_hidden
 
 
 
 
381
  )
382
 
383
- # Also update the selected_info Markdown when the hidden state changes.
384
  def update_info(selected_json):
385
  selected_indices = json.loads(selected_json)
386
  if selected_indices:
@@ -388,26 +389,11 @@ with gr.Blocks(theme=gr.themes.Soft(font=font), css=css, delete_cache=(60, 60))
388
  else:
389
  info = "No LoRAs selected."
390
  return info
391
- selected_info.change(
392
  update_info,
393
  inputs=selected_indices_hidden,
394
  outputs=selected_info
395
  )
396
-
397
- # Also, when the Generate button is clicked, update the hidden state from the checkboxes.
398
- generate_button.click(
399
- combine_selections,
400
- inputs=lora_checkbox_list,
401
- outputs=selected_indices_hidden
402
- )
403
-
404
- # Finally, trigger the generation function (run_lora). Note that run_lora should be modified to parse the JSON string.
405
- gr.on(
406
- triggers=[generate_button.click, prompt.submit],
407
- fn=run_lora, # Make sure run_lora begins by parsing the JSON: selected_indices = json.loads(selected_indices_hidden)
408
- inputs=[prompt, input_image, image_strength, cfg_scale, steps, selected_indices_hidden, randomize_seed, seed, width, height, lora_scale],
409
- outputs=[result, seed, progress_bar]
410
- )
411
 
412
  app.queue()
413
  app.launch()
 
306
  #title{text-align: center}
307
  #title h1{font-size: 3em; display:inline-flex; align-items:center}
308
  #title img{width: 100px; margin-right: 0.5em}
 
309
  #lora_list{background: var(--block-background-fill);padding: 0 1em .3em; font-size: 90%}
310
  .card_internal{display: flex;height: 100px;margin-top: .5em}
311
  .card_internal img{margin-right: 1em}
312
  .styler{--form-gap-width: 0px !important}
313
  #progress{height:30px}
 
314
  .progress-container {width: 100%;height: 30px;background-color: #f0f0f0;border-radius: 15px;overflow: hidden;margin-bottom: 20px}
315
  .progress-bar {height: 100%;background-color: #4f46e5;width: calc(var(--current) / var(--total) * 100%);transition: width 0.5s ease-in-out}
316
  '''
317
+ font = [gr.themes.GoogleFont("Source Sans Pro"), "Arial", "sans-serif"]
318
+
319
  with gr.Blocks(theme=gr.themes.Soft(font=font), css=css, delete_cache=(60, 60)) as app:
320
  title = gr.HTML(
321
  """<h1><img src="https://huggingface.co/spaces/kayte0342/test/resolve/main/DA4BE61E-A0BD-4254-A1B6-AD3C05D18A9C%20(1).png?download=true" alt="LoRA"> FLUX LoRA Kayte's Space</h1>""",
322
  elem_id="title",
323
  )
 
324
  # Hidden textbox to store the JSON string of selected indices
325
  selected_indices_hidden = gr.Textbox(value="[]", visible=False)
326
 
 
333
  with gr.Row():
334
  with gr.Column():
335
  selected_info = gr.Markdown("")
336
+ # Build a custom layout for LoRA selection with checkboxes.
337
  lora_selection_container = gr.Column()
 
338
  lora_checkbox_list = []
339
  for idx, lora in enumerate(loras):
340
  with gr.Row():
341
  gr.Image(value=lora["image"], label=lora["title"], height=100)
342
  checkbox = gr.Checkbox(label="Select", value=False, elem_id=f"lora_checkbox_{idx}")
343
  lora_checkbox_list.append(checkbox)
344
+ gr.Markdown("[Check the list of FLUX LoRas](https://huggingface.co/models?other=base_model:adapter:black-forest-labs/FLUX.1-dev)", elem_id="lora_list")
 
345
  with gr.Column():
346
  progress_bar = gr.Markdown(elem_id="progress", visible=False)
347
  result = gr.Image(label="Generated Image")
 
363
  seed = gr.Slider(label="Seed", minimum=0, maximum=2**32-1, step=1, value=0, randomize=True)
364
  lora_scale = gr.Slider(label="LoRA Scale", minimum=0, maximum=3, step=0.01, value=0.95)
365
 
366
+ # Function to combine the checkbox values into a JSON string.
367
  def combine_selections(*checkbox_values):
368
  selected_indices = [i for i, v in enumerate(checkbox_values) if v]
369
  return json.dumps(selected_indices)
370
 
371
+ # Chain the update: When the Generate button is clicked,
372
+ # first update the hidden state with combine_selections,
373
+ # then run run_lora using the updated hidden state.
374
+ generate_button.click(
375
  combine_selections,
376
  inputs=lora_checkbox_list,
377
  outputs=selected_indices_hidden
378
+ ).then(
379
+ run_lora,
380
+ inputs=[prompt, input_image, image_strength, cfg_scale, steps, selected_indices_hidden, randomize_seed, seed, width, height, lora_scale],
381
+ outputs=[result, seed, progress_bar]
382
  )
383
 
384
+ # Optionally, update the selected_info markdown when the hidden state changes.
385
  def update_info(selected_json):
386
  selected_indices = json.loads(selected_json)
387
  if selected_indices:
 
389
  else:
390
  info = "No LoRAs selected."
391
  return info
392
+ selected_indices_hidden.change(
393
  update_info,
394
  inputs=selected_indices_hidden,
395
  outputs=selected_info
396
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
397
 
398
  app.queue()
399
  app.launch()