Preserve edited LoRA prompts across selection changes
Browse filesRemember the LoRA prompt box per selection set (and per title when
split is clean). Adding a LoRA keeps prior edits and appends only the
new default; free-form text is not wiped on tick/untick.
Co-Authored-By: Oz <oz-agent@warp.dev>
- app.py +7 -4
- lora_registry.py +127 -12
app.py
CHANGED
|
@@ -505,9 +505,10 @@ with gr.Blocks() as demo:
|
|
| 505 |
dynamic_loras_state = gr.State({})
|
| 506 |
selected_output_state = gr.State(None)
|
| 507 |
last_prompt_state = gr.State("")
|
| 508 |
-
# Remember per-title LoRA weights across add/remove so existing
|
| 509 |
-
# don't snap back to catalog defaults when the selection changes.
|
| 510 |
lora_weight_memory_state = gr.State({})
|
|
|
|
| 511 |
lora_prev_selected_state = gr.State([])
|
| 512 |
|
| 513 |
with gr.Column(elem_id="col-container"):
|
|
@@ -1027,15 +1028,17 @@ with gr.Blocks() as demo:
|
|
| 1027 |
# update_weight_sliders is the one imported from lora_registry now.
|
| 1028 |
# Use "once" (not always_last): always_last can drop/defer the first tick
|
| 1029 |
# so the new weight slider stays hidden until a second LoRA is selected.
|
| 1030 |
-
# Pass live slider values + memory so existing
|
| 1031 |
_lora_slider_inputs = [
|
| 1032 |
lora_selector, dynamic_loras_state,
|
| 1033 |
lora_weight_memory_state, lora_prev_selected_state,
|
|
|
|
| 1034 |
] + weight_sliders
|
| 1035 |
_lora_slider_outputs = (
|
| 1036 |
weight_sliders
|
| 1037 |
+ [lora_prompt_display, selected_lora_details,
|
| 1038 |
-
lora_weight_memory_state, lora_prev_selected_state
|
|
|
|
| 1039 |
)
|
| 1040 |
lora_selector.change(
|
| 1041 |
fn=update_weight_sliders,
|
|
|
|
| 505 |
dynamic_loras_state = gr.State({})
|
| 506 |
selected_output_state = gr.State(None)
|
| 507 |
last_prompt_state = gr.State("")
|
| 508 |
+
# Remember per-title LoRA weights / prompts across add/remove so existing
|
| 509 |
+
# values don't snap back to catalog defaults when the selection changes.
|
| 510 |
lora_weight_memory_state = gr.State({})
|
| 511 |
+
lora_prompt_memory_state = gr.State({})
|
| 512 |
lora_prev_selected_state = gr.State([])
|
| 513 |
|
| 514 |
with gr.Column(elem_id="col-container"):
|
|
|
|
| 1028 |
# update_weight_sliders is the one imported from lora_registry now.
|
| 1029 |
# Use "once" (not always_last): always_last can drop/defer the first tick
|
| 1030 |
# so the new weight slider stays hidden until a second LoRA is selected.
|
| 1031 |
+
# Pass live slider/prompt values + memory so existing settings survive add/remove.
|
| 1032 |
_lora_slider_inputs = [
|
| 1033 |
lora_selector, dynamic_loras_state,
|
| 1034 |
lora_weight_memory_state, lora_prev_selected_state,
|
| 1035 |
+
lora_prompt_memory_state, lora_prompt_display,
|
| 1036 |
] + weight_sliders
|
| 1037 |
_lora_slider_outputs = (
|
| 1038 |
weight_sliders
|
| 1039 |
+ [lora_prompt_display, selected_lora_details,
|
| 1040 |
+
lora_weight_memory_state, lora_prev_selected_state,
|
| 1041 |
+
lora_prompt_memory_state]
|
| 1042 |
)
|
| 1043 |
lora_selector.change(
|
| 1044 |
fn=update_weight_sliders,
|
lora_registry.py
CHANGED
|
@@ -624,19 +624,129 @@ def format_selected_lora_details(selected_titles, dynamic_loras) -> str:
|
|
| 624 |
return "\n\n".join(blocks)
|
| 625 |
|
| 626 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 627 |
def update_weight_sliders(
|
| 628 |
selected_titles,
|
| 629 |
dynamic_loras,
|
| 630 |
weight_memory=None,
|
| 631 |
prev_selected=None,
|
|
|
|
|
|
|
| 632 |
*current_slider_values,
|
| 633 |
):
|
| 634 |
"""Show/hide weight sliders for the current LoRA selection.
|
| 635 |
|
| 636 |
-
Preserves user-adjusted weights:
|
| 637 |
- Reads live slider values for the previous selection into `weight_memory`
|
| 638 |
- Reuses remembered weights for still-selected (or re-selected) titles
|
| 639 |
- Only new titles fall back to catalog `default_weight`
|
|
|
|
| 640 |
"""
|
| 641 |
selected_styles = []
|
| 642 |
for t in (selected_titles or []):
|
|
@@ -659,6 +769,9 @@ def update_weight_sliders(
|
|
| 659 |
except (TypeError, ValueError):
|
| 660 |
pass
|
| 661 |
|
|
|
|
|
|
|
|
|
|
| 662 |
slider_updates = []
|
| 663 |
for i in range(MAX_LORA_SLOTS):
|
| 664 |
if i < len(selected_styles):
|
|
@@ -685,19 +798,16 @@ def update_weight_sliders(
|
|
| 685 |
value=1.0,
|
| 686 |
))
|
| 687 |
|
| 688 |
-
|
| 689 |
-
|
| 690 |
-
|
| 691 |
-
|
| 692 |
-
if
|
| 693 |
-
combined = "\n\n".join(lora_prompts)
|
| 694 |
lora_prompt_update = gr.update(value=combined, visible=True, interactive=True)
|
| 695 |
else:
|
| 696 |
lora_prompt_update = gr.update(value="", visible=False)
|
| 697 |
|
| 698 |
-
details = format_selected_lora_details(
|
| 699 |
-
[s["title"] for s in selected_styles], dynamic_loras
|
| 700 |
-
)
|
| 701 |
if not details:
|
| 702 |
details = (
|
| 703 |
"*Tick one or more LoRAs above to see full repo paths, "
|
|
@@ -706,8 +816,13 @@ def update_weight_sliders(
|
|
| 706 |
# Always keep the Advanced accordion body populated (no visibility toggle).
|
| 707 |
details_update = gr.update(value=details)
|
| 708 |
|
| 709 |
-
|
| 710 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 711 |
|
| 712 |
|
| 713 |
def _looks_like_local_path(value: str) -> bool:
|
|
|
|
| 624 |
return "\n\n".join(blocks)
|
| 625 |
|
| 626 |
|
| 627 |
+
_PROMPT_SEP = "\n\n"
|
| 628 |
+
_PROMPT_SET_PREFIX = "__set__:"
|
| 629 |
+
|
| 630 |
+
|
| 631 |
+
def _style_default_prompt(style) -> str:
|
| 632 |
+
return (str(style.get("default_prompt") or "")).strip()
|
| 633 |
+
|
| 634 |
+
|
| 635 |
+
def _prompt_set_key(titles) -> str:
|
| 636 |
+
return _PROMPT_SET_PREFIX + "\x1f".join(titles or [])
|
| 637 |
+
|
| 638 |
+
|
| 639 |
+
def _capture_lora_prompts(prev_titles, current_text, prompt_memory):
|
| 640 |
+
"""Persist the on-screen LoRA prompt box into session memory."""
|
| 641 |
+
mem = dict(prompt_memory or {})
|
| 642 |
+
prev = list(prev_titles or [])
|
| 643 |
+
if not prev:
|
| 644 |
+
return mem
|
| 645 |
+
text = "" if current_text is None else str(current_text)
|
| 646 |
+
# Always remember the exact previous selection's combined text.
|
| 647 |
+
mem[_prompt_set_key(prev)] = text
|
| 648 |
+
parts = text.split(_PROMPT_SEP)
|
| 649 |
+
if len(parts) == len(prev):
|
| 650 |
+
for title, part in zip(prev, parts):
|
| 651 |
+
mem[str(title)] = part
|
| 652 |
+
elif len(prev) == 1:
|
| 653 |
+
mem[str(prev[0])] = text
|
| 654 |
+
return mem
|
| 655 |
+
|
| 656 |
+
|
| 657 |
+
def _build_lora_prompt(new_titles, prompt_memory, dynamic_loras, prev_titles=None):
|
| 658 |
+
"""Rebuild the combined LoRA prompt, preserving edits when possible."""
|
| 659 |
+
mem = dict(prompt_memory or {})
|
| 660 |
+
titles = list(new_titles or [])
|
| 661 |
+
if not titles:
|
| 662 |
+
return "", mem
|
| 663 |
+
|
| 664 |
+
set_key = _prompt_set_key(titles)
|
| 665 |
+
if set_key in mem:
|
| 666 |
+
return mem[set_key], mem
|
| 667 |
+
|
| 668 |
+
prev = list(prev_titles or [])
|
| 669 |
+
prev_key = _prompt_set_key(prev) if prev else None
|
| 670 |
+
prev_set = set(prev)
|
| 671 |
+
new_set = set(titles)
|
| 672 |
+
|
| 673 |
+
# Pure add onto a previous (possibly freely-edited) combined prompt.
|
| 674 |
+
if prev_key and prev_key in mem and prev_set and prev_set.issubset(new_set) and new_set != prev_set:
|
| 675 |
+
base = mem[prev_key]
|
| 676 |
+
extras = []
|
| 677 |
+
for title in titles:
|
| 678 |
+
if title in prev_set:
|
| 679 |
+
continue
|
| 680 |
+
if title in mem:
|
| 681 |
+
frag = mem[title]
|
| 682 |
+
else:
|
| 683 |
+
style = get_style_by_title(title, dynamic_loras) or {}
|
| 684 |
+
frag = _style_default_prompt(style)
|
| 685 |
+
mem[title] = frag
|
| 686 |
+
frag = (frag or "").strip()
|
| 687 |
+
if frag:
|
| 688 |
+
extras.append(frag)
|
| 689 |
+
parts = []
|
| 690 |
+
if (base or "").strip():
|
| 691 |
+
parts.append(base.rstrip())
|
| 692 |
+
parts.extend(extras)
|
| 693 |
+
combined = _PROMPT_SEP.join(parts)
|
| 694 |
+
mem[set_key] = combined
|
| 695 |
+
return combined, mem
|
| 696 |
+
|
| 697 |
+
# Pure remove: if the previous combined text still splits cleanly into one
|
| 698 |
+
# fragment per previous title, drop the removed titles' fragments.
|
| 699 |
+
# Otherwise the user free-edited the box — keep that text rather than
|
| 700 |
+
# guessing from stale per-title defaults.
|
| 701 |
+
if prev_key and prev_key in mem and new_set and new_set.issubset(prev_set) and new_set != prev_set:
|
| 702 |
+
prev_text = mem[prev_key]
|
| 703 |
+
prev_parts = prev_text.split(_PROMPT_SEP)
|
| 704 |
+
if len(prev_parts) == len(prev):
|
| 705 |
+
title_to_part = dict(zip(prev, prev_parts))
|
| 706 |
+
parts = []
|
| 707 |
+
for title in titles:
|
| 708 |
+
frag = (title_to_part.get(title) or mem.get(title) or "").strip()
|
| 709 |
+
if frag:
|
| 710 |
+
parts.append(frag)
|
| 711 |
+
combined = _PROMPT_SEP.join(parts)
|
| 712 |
+
else:
|
| 713 |
+
combined = prev_text
|
| 714 |
+
mem[set_key] = combined
|
| 715 |
+
return combined, mem
|
| 716 |
+
|
| 717 |
+
# Default path: per-title memory, else catalog default.
|
| 718 |
+
parts = []
|
| 719 |
+
for title in titles:
|
| 720 |
+
if title in mem and not str(title).startswith(_PROMPT_SET_PREFIX):
|
| 721 |
+
frag = mem[title]
|
| 722 |
+
else:
|
| 723 |
+
style = get_style_by_title(title, dynamic_loras) or {}
|
| 724 |
+
frag = _style_default_prompt(style)
|
| 725 |
+
mem[title] = frag
|
| 726 |
+
frag = (frag or "").strip()
|
| 727 |
+
if frag:
|
| 728 |
+
parts.append(frag)
|
| 729 |
+
combined = _PROMPT_SEP.join(parts)
|
| 730 |
+
mem[set_key] = combined
|
| 731 |
+
return combined, mem
|
| 732 |
+
|
| 733 |
+
|
| 734 |
def update_weight_sliders(
|
| 735 |
selected_titles,
|
| 736 |
dynamic_loras,
|
| 737 |
weight_memory=None,
|
| 738 |
prev_selected=None,
|
| 739 |
+
prompt_memory=None,
|
| 740 |
+
current_lora_prompt=None,
|
| 741 |
*current_slider_values,
|
| 742 |
):
|
| 743 |
"""Show/hide weight sliders for the current LoRA selection.
|
| 744 |
|
| 745 |
+
Preserves user-adjusted weights and LoRA prompt text:
|
| 746 |
- Reads live slider values for the previous selection into `weight_memory`
|
| 747 |
- Reuses remembered weights for still-selected (or re-selected) titles
|
| 748 |
- Only new titles fall back to catalog `default_weight`
|
| 749 |
+
- Same idea for the editable LoRA prompt box (per-title + set memory)
|
| 750 |
"""
|
| 751 |
selected_styles = []
|
| 752 |
for t in (selected_titles or []):
|
|
|
|
| 769 |
except (TypeError, ValueError):
|
| 770 |
pass
|
| 771 |
|
| 772 |
+
# Capture current LoRA prompt box before selection rebuild.
|
| 773 |
+
p_memory = _capture_lora_prompts(prev, current_lora_prompt, prompt_memory)
|
| 774 |
+
|
| 775 |
slider_updates = []
|
| 776 |
for i in range(MAX_LORA_SLOTS):
|
| 777 |
if i < len(selected_styles):
|
|
|
|
| 798 |
value=1.0,
|
| 799 |
))
|
| 800 |
|
| 801 |
+
new_selected = [s["title"] for s in selected_styles]
|
| 802 |
+
combined, p_memory = _build_lora_prompt(
|
| 803 |
+
new_selected, p_memory, dynamic_loras, prev_titles=prev,
|
| 804 |
+
)
|
| 805 |
+
if combined:
|
|
|
|
| 806 |
lora_prompt_update = gr.update(value=combined, visible=True, interactive=True)
|
| 807 |
else:
|
| 808 |
lora_prompt_update = gr.update(value="", visible=False)
|
| 809 |
|
| 810 |
+
details = format_selected_lora_details(new_selected, dynamic_loras)
|
|
|
|
|
|
|
| 811 |
if not details:
|
| 812 |
details = (
|
| 813 |
"*Tick one or more LoRAs above to see full repo paths, "
|
|
|
|
| 816 |
# Always keep the Advanced accordion body populated (no visibility toggle).
|
| 817 |
details_update = gr.update(value=details)
|
| 818 |
|
| 819 |
+
return slider_updates + [
|
| 820 |
+
lora_prompt_update,
|
| 821 |
+
details_update,
|
| 822 |
+
memory,
|
| 823 |
+
new_selected,
|
| 824 |
+
p_memory,
|
| 825 |
+
]
|
| 826 |
|
| 827 |
|
| 828 |
def _looks_like_local_path(value: str) -> bool:
|