Spaces:
Running
on
Zero
Running
on
Zero
Update Gradio app with multiple files
Browse files
app.py
CHANGED
|
@@ -172,36 +172,20 @@ def update_selection(evt: gr.SelectData, aspect_ratio):
|
|
| 172 |
lora_repo = selected_lora["repo"]
|
| 173 |
updated_text = f"### Selected: [{lora_repo}](https://huggingface.co/{lora_repo}) ✨"
|
| 174 |
|
| 175 |
-
# Get model card
|
| 176 |
-
|
| 177 |
try:
|
| 178 |
model_card = ModelCard.load(lora_repo)
|
| 179 |
widget_data = model_card.data.get("widget", [])
|
| 180 |
if widget_data and len(widget_data) > 0:
|
| 181 |
-
# Get
|
| 182 |
-
|
| 183 |
-
examples_html += '<h4 style="margin-bottom: 8px; font-size: 0.9em;">Sample Images:</h4>'
|
| 184 |
-
examples_html += '<div style="display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px;">'
|
| 185 |
-
|
| 186 |
-
for i, example in enumerate(widget_data[:4]):
|
| 187 |
if "output" in example and "url" in example["output"]:
|
| 188 |
image_url = f"https://huggingface.co/{lora_repo}/resolve/main/{example['output']['url']}"
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
<div style="text-align: center;">
|
| 192 |
-
<img src="{image_url}" style="width: 100%; height: auto; border-radius: 4px;" />
|
| 193 |
-
<p style="font-size: 0.7em; margin: 2px 0;">{caption[:30]}{'...' if len(caption) > 30 else ''}</p>
|
| 194 |
-
</div>
|
| 195 |
-
'''
|
| 196 |
-
|
| 197 |
-
examples_html += '</div></div>'
|
| 198 |
-
model_card_html = examples_html
|
| 199 |
except Exception as e:
|
| 200 |
print(f"Could not load model card for {lora_repo}: {e}")
|
| 201 |
-
model_card_html = ""
|
| 202 |
-
|
| 203 |
-
# Combine selected info with model card examples
|
| 204 |
-
full_info = updated_text + model_card_html
|
| 205 |
|
| 206 |
# Update aspect ratio if specified in LoRA config
|
| 207 |
if "aspect" in selected_lora:
|
|
@@ -216,9 +200,10 @@ def update_selection(evt: gr.SelectData, aspect_ratio):
|
|
| 216 |
|
| 217 |
return (
|
| 218 |
gr.update(placeholder=new_placeholder),
|
| 219 |
-
|
| 220 |
evt.index,
|
| 221 |
aspect_ratio,
|
|
|
|
| 222 |
)
|
| 223 |
|
| 224 |
def handle_speed_mode(speed_mode):
|
|
@@ -480,17 +465,17 @@ def add_custom_lora(custom_lora):
|
|
| 480 |
loras.append(new_item)
|
| 481 |
existing_item_index = len(loras) - 1 # Get the actual index after adding
|
| 482 |
|
| 483 |
-
return gr.update(visible=True, value=card), gr.update(visible=True), gr.Gallery(selected_index=None), f"Custom: {path}", existing_item_index, trigger_word
|
| 484 |
except Exception as e:
|
| 485 |
full_traceback = traceback.format_exc()
|
| 486 |
print(f"Full traceback:\n{full_traceback}")
|
| 487 |
gr.Warning(f"Invalid LoRA: either you entered an invalid link, or a non-Qwen-Image LoRA, this was the issue: {e}")
|
| 488 |
return gr.update(visible=True, value=f"Invalid LoRA: either you entered an invalid link, a non-Qwen-Image LoRA"), gr.update(visible=True), gr.update(), "", None, ""
|
| 489 |
else:
|
| 490 |
-
return gr.update(visible=False), gr.update(visible=False), gr.update(), "", None, ""
|
| 491 |
|
| 492 |
def remove_custom_lora():
|
| 493 |
-
return gr.update(visible=False), gr.update(visible=False), gr.update(), "", None, ""
|
| 494 |
|
| 495 |
run_lora.zerogpu = True
|
| 496 |
|
|
@@ -526,6 +511,7 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css, delete_cache=(60, 60)) as app:
|
|
| 526 |
with gr.Row():
|
| 527 |
with gr.Column():
|
| 528 |
selected_info = gr.Markdown("")
|
|
|
|
| 529 |
gallery = gr.Gallery(
|
| 530 |
[(item["image"], item["title"]) for item in loras],
|
| 531 |
label="LoRA Gallery",
|
|
@@ -590,7 +576,7 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css, delete_cache=(60, 60)) as app:
|
|
| 590 |
gallery.select(
|
| 591 |
update_selection,
|
| 592 |
inputs=[aspect_ratio],
|
| 593 |
-
outputs=[prompt, selected_info, selected_index, aspect_ratio]
|
| 594 |
)
|
| 595 |
|
| 596 |
speed_mode.change(
|
|
@@ -602,12 +588,12 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css, delete_cache=(60, 60)) as app:
|
|
| 602 |
custom_lora.input(
|
| 603 |
add_custom_lora,
|
| 604 |
inputs=[custom_lora],
|
| 605 |
-
outputs=[custom_lora_info, custom_lora_button, gallery, selected_info, selected_index, prompt]
|
| 606 |
)
|
| 607 |
|
| 608 |
custom_lora_button.click(
|
| 609 |
remove_custom_lora,
|
| 610 |
-
outputs=[custom_lora_info, custom_lora_button, gallery, selected_info, selected_index, custom_lora]
|
| 611 |
)
|
| 612 |
|
| 613 |
gr.on(
|
|
|
|
| 172 |
lora_repo = selected_lora["repo"]
|
| 173 |
updated_text = f"### Selected: [{lora_repo}](https://huggingface.co/{lora_repo}) ✨"
|
| 174 |
|
| 175 |
+
# Get model card examples
|
| 176 |
+
examples_list = []
|
| 177 |
try:
|
| 178 |
model_card = ModelCard.load(lora_repo)
|
| 179 |
widget_data = model_card.data.get("widget", [])
|
| 180 |
if widget_data and len(widget_data) > 0:
|
| 181 |
+
# Get examples from widget data
|
| 182 |
+
for example in widget_data[:4]:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
if "output" in example and "url" in example["output"]:
|
| 184 |
image_url = f"https://huggingface.co/{lora_repo}/resolve/main/{example['output']['url']}"
|
| 185 |
+
prompt_text = example.get("text", "")
|
| 186 |
+
examples_list.append([prompt_text])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
except Exception as e:
|
| 188 |
print(f"Could not load model card for {lora_repo}: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
|
| 190 |
# Update aspect ratio if specified in LoRA config
|
| 191 |
if "aspect" in selected_lora:
|
|
|
|
| 200 |
|
| 201 |
return (
|
| 202 |
gr.update(placeholder=new_placeholder),
|
| 203 |
+
updated_text,
|
| 204 |
evt.index,
|
| 205 |
aspect_ratio,
|
| 206 |
+
gr.Examples(examples=examples_list, inputs=[prompt], label="Sample Prompts")
|
| 207 |
)
|
| 208 |
|
| 209 |
def handle_speed_mode(speed_mode):
|
|
|
|
| 465 |
loras.append(new_item)
|
| 466 |
existing_item_index = len(loras) - 1 # Get the actual index after adding
|
| 467 |
|
| 468 |
+
return gr.update(visible=True, value=card), gr.update(visible=True), gr.Gallery(selected_index=None), f"Custom: {path}", existing_item_index, trigger_word, gr.Examples(examples=[], inputs=[prompt], label="Sample Prompts")
|
| 469 |
except Exception as e:
|
| 470 |
full_traceback = traceback.format_exc()
|
| 471 |
print(f"Full traceback:\n{full_traceback}")
|
| 472 |
gr.Warning(f"Invalid LoRA: either you entered an invalid link, or a non-Qwen-Image LoRA, this was the issue: {e}")
|
| 473 |
return gr.update(visible=True, value=f"Invalid LoRA: either you entered an invalid link, a non-Qwen-Image LoRA"), gr.update(visible=True), gr.update(), "", None, ""
|
| 474 |
else:
|
| 475 |
+
return gr.update(visible=False), gr.update(visible=False), gr.update(), "", None, "", gr.Examples(examples=[], inputs=[prompt], label="Sample Prompts", visible=False)
|
| 476 |
|
| 477 |
def remove_custom_lora():
|
| 478 |
+
return gr.update(visible=False), gr.update(visible=False), gr.update(), "", None, "", gr.Examples(examples=[], inputs=[prompt], label="Sample Prompts", visible=False), gr.Examples(examples=[], inputs=[prompt], label="Sample Prompts", visible=False)
|
| 479 |
|
| 480 |
run_lora.zerogpu = True
|
| 481 |
|
|
|
|
| 511 |
with gr.Row():
|
| 512 |
with gr.Column():
|
| 513 |
selected_info = gr.Markdown("")
|
| 514 |
+
examples_component = gr.Examples(examples=[], inputs=[prompt], label="Sample Prompts", visible=False)
|
| 515 |
gallery = gr.Gallery(
|
| 516 |
[(item["image"], item["title"]) for item in loras],
|
| 517 |
label="LoRA Gallery",
|
|
|
|
| 576 |
gallery.select(
|
| 577 |
update_selection,
|
| 578 |
inputs=[aspect_ratio],
|
| 579 |
+
outputs=[prompt, selected_info, selected_index, aspect_ratio, examples_component]
|
| 580 |
)
|
| 581 |
|
| 582 |
speed_mode.change(
|
|
|
|
| 588 |
custom_lora.input(
|
| 589 |
add_custom_lora,
|
| 590 |
inputs=[custom_lora],
|
| 591 |
+
outputs=[custom_lora_info, custom_lora_button, gallery, selected_info, selected_index, prompt, examples_component]
|
| 592 |
)
|
| 593 |
|
| 594 |
custom_lora_button.click(
|
| 595 |
remove_custom_lora,
|
| 596 |
+
outputs=[custom_lora_info, custom_lora_button, gallery, selected_info, selected_index, custom_lora, examples_component]
|
| 597 |
)
|
| 598 |
|
| 599 |
gr.on(
|