raubatz Oz commited on
Commit
1604d11
·
1 Parent(s): 293305f

Add notes, known_triggers, and playground compatibility fields

Browse files

Extend the catalog JSON schema with optional notes and a flat
known_triggers list (docs only, not auto-appended), plus
compatible_with_playground. Show selected LoRA repo/trigger details in
the UI and allow optional triggers/notes when saving to the catalog.

Co-Authored-By: Oz <oz-agent@warp.dev>

Files changed (2) hide show
  1. app.py +23 -4
  2. lora_registry.py +115 -10
app.py CHANGED
@@ -500,6 +500,11 @@ with gr.Blocks() as demo:
500
  value=[], label="Active LoRAs — tick one or more",
501
  )
502
  catalog_load_status = gr.Markdown("", visible=True)
 
 
 
 
 
503
  gr.Markdown("#### Weights for selected LoRAs")
504
  weight_sliders = []
505
  with gr.Group():
@@ -555,7 +560,19 @@ with gr.Blocks() as demo:
555
  )
556
  catalog_save_prompt = gr.Textbox(
557
  label="Default prompt (optional)", lines=2,
558
- placeholder="Optional prompt auto-appended when this LoRA is selected",
 
 
 
 
 
 
 
 
 
 
 
 
559
  )
560
  with gr.Row():
561
  catalog_save_btn = gr.Button(
@@ -781,7 +798,7 @@ with gr.Blocks() as demo:
781
  lora_selector.change(
782
  fn=update_weight_sliders,
783
  inputs=[lora_selector, dynamic_loras_state],
784
- outputs=weight_sliders + [lora_prompt_display],
785
  )
786
 
787
  # add_custom_lora is also imported from lora_registry.
@@ -795,12 +812,14 @@ with gr.Blocks() as demo:
795
  catalog_save_select.change(
796
  fn=fill_catalog_save_form,
797
  inputs=[catalog_save_select, dynamic_loras_state],
798
- outputs=[catalog_save_title, catalog_save_weight, catalog_save_prompt],
 
799
  )
800
  catalog_save_btn.click(
801
  fn=save_session_lora_to_catalog,
802
  inputs=[catalog_save_select, catalog_save_title, catalog_save_weight,
803
- catalog_save_prompt, dynamic_loras_state],
 
804
  outputs=[catalog_save_status, lora_selector, catalog_save_select,
805
  catalog_remove_select, dynamic_loras_state],
806
  )
 
500
  value=[], label="Active LoRAs — tick one or more",
501
  )
502
  catalog_load_status = gr.Markdown("", visible=True)
503
+ selected_lora_details = gr.Markdown(
504
+ value="",
505
+ visible=False,
506
+ elem_id="selected_lora_details",
507
+ )
508
  gr.Markdown("#### Weights for selected LoRAs")
509
  weight_sliders = []
510
  with gr.Group():
 
560
  )
561
  catalog_save_prompt = gr.Textbox(
562
  label="Default prompt (optional)", lines=2,
563
+ placeholder="Safe ready-to-go prompt auto-appended when selected",
564
+ )
565
+ catalog_save_triggers = gr.Textbox(
566
+ label="Known triggers (optional)", lines=3,
567
+ placeholder=(
568
+ "One per line or comma-separated. Docs only — not auto-appended.\n"
569
+ "e.g. small penis, large penis, flaccid penis, erect penis"
570
+ ),
571
+ info="Can include mutually exclusive keywords; pick what you need in the prompt.",
572
+ )
573
+ catalog_save_notes = gr.Textbox(
574
+ label="Notes (optional)", lines=2,
575
+ placeholder="Usage notes, caveats, pairing tips…",
576
  )
577
  with gr.Row():
578
  catalog_save_btn = gr.Button(
 
798
  lora_selector.change(
799
  fn=update_weight_sliders,
800
  inputs=[lora_selector, dynamic_loras_state],
801
+ outputs=weight_sliders + [lora_prompt_display, selected_lora_details],
802
  )
803
 
804
  # add_custom_lora is also imported from lora_registry.
 
812
  catalog_save_select.change(
813
  fn=fill_catalog_save_form,
814
  inputs=[catalog_save_select, dynamic_loras_state],
815
+ outputs=[catalog_save_title, catalog_save_weight, catalog_save_prompt,
816
+ catalog_save_triggers, catalog_save_notes],
817
  )
818
  catalog_save_btn.click(
819
  fn=save_session_lora_to_catalog,
820
  inputs=[catalog_save_select, catalog_save_title, catalog_save_weight,
821
+ catalog_save_prompt, dynamic_loras_state,
822
+ catalog_save_triggers, catalog_save_notes],
823
  outputs=[catalog_save_status, lora_selector, catalog_save_select,
824
  catalog_remove_select, dynamic_loras_state],
825
  )
lora_registry.py CHANGED
@@ -271,6 +271,21 @@ def _compute_entry_sha256(entry: dict) -> str | None:
271
  return _file_sha256(path)
272
 
273
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
274
  def _normalize_catalog_entry(raw) -> dict | None:
275
  if not isinstance(raw, dict):
276
  return None
@@ -301,6 +316,12 @@ def _normalize_catalog_entry(raw) -> dict | None:
301
  else:
302
  sha = None
303
 
 
 
 
 
 
 
304
  return {
305
  "image": raw.get("image") or _DEFAULT_LORA_IMAGE,
306
  "title": title,
@@ -311,6 +332,13 @@ def _normalize_catalog_entry(raw) -> dict | None:
311
  "default_weight": default_weight,
312
  "admin_approved": _as_bool(raw.get("admin_approved"), False),
313
  "active": active,
 
 
 
 
 
 
 
314
  "sha256": sha,
315
  }
316
 
@@ -325,7 +353,14 @@ def _serialize_entry(e: dict) -> dict:
325
  "default_weight": float(e.get("default_weight", 1.0)),
326
  "admin_approved": bool(e.get("admin_approved", False)),
327
  "active": bool(e.get("active", True)),
 
328
  }
 
 
 
 
 
 
329
  if e.get("sha256"):
330
  out["sha256"] = e["sha256"]
331
  if e.get("image") and e["image"] != _DEFAULT_LORA_IMAGE:
@@ -365,7 +400,7 @@ def _write_catalog_file(entries: list[dict], path: str | None = None) -> str:
365
  catalog_path = Path(path or PERSISTENT_LORA_CATALOG_PATH)
366
  catalog_path.parent.mkdir(parents=True, exist_ok=True)
367
  payload = {
368
- "version": 2,
369
  "loras": [_serialize_entry(e) for e in entries],
370
  }
371
  tmp_path = catalog_path.with_suffix(catalog_path.suffix + ".tmp")
@@ -415,7 +450,10 @@ def load_external_catalog(path: str | None = None) -> list[dict]:
415
  global EXTERNAL_LORA_STYLES, LORA_STYLES
416
  with _CATALOG_LOCK:
417
  EXTERNAL_LORA_STYLES = _seed_catalog_if_needed(path)
418
- LORA_STYLES = [e for e in EXTERNAL_LORA_STYLES if e.get("active", True)]
 
 
 
419
  print(
420
  f"[lora_registry] Catalog: {len(LORA_STYLES)} active / "
421
  f"{len(EXTERNAL_LORA_STYLES)} total from "
@@ -440,7 +478,10 @@ def reload_catalog_from_disk(path: str | None = None) -> list[dict]:
440
  EXTERNAL_LORA_STYLES = entries
441
  else:
442
  EXTERNAL_LORA_STYLES = _seed_catalog_if_needed(str(catalog_path))
443
- LORA_STYLES = [e for e in EXTERNAL_LORA_STYLES if e.get("active", True)]
 
 
 
444
  print(
445
  f"[lora_registry] Reloaded catalog: {len(LORA_STYLES)} active / "
446
  f"{len(EXTERNAL_LORA_STYLES)} total from {catalog_path}"
@@ -483,7 +524,11 @@ load_external_catalog()
483
 
484
 
485
  def _active_catalog() -> list[dict]:
486
- return [e for e in EXTERNAL_LORA_STYLES if e.get("active", True)]
 
 
 
 
487
 
488
 
489
  def _norm_repo_key(repo: str | None) -> str:
@@ -551,6 +596,34 @@ def get_style_by_adapter_name(adapter_name, dynamic_loras):
551
  return None
552
 
553
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
554
  def update_weight_sliders(selected_titles, dynamic_loras):
555
  selected_styles = [
556
  get_style_by_title(t, dynamic_loras)
@@ -579,7 +652,10 @@ def update_weight_sliders(selected_titles, dynamic_loras):
579
  else:
580
  lora_prompt_update = gr.update(value="", visible=False)
581
 
582
- return slider_updates + [lora_prompt_update]
 
 
 
583
 
584
 
585
  def _looks_like_local_path(value: str) -> bool:
@@ -788,6 +864,9 @@ def add_custom_lora(repo_id, weight_name, adapter_name, dynamic_loras_state):
788
  "default_weight": 1.0,
789
  "admin_approved": False,
790
  "active": True,
 
 
 
791
  "sha256": sha,
792
  "session_only": True,
793
  }
@@ -878,7 +957,10 @@ def remove_lora(selected_title, dynamic_loras_state, currently_selected):
878
  dynamic_loras,
879
  )
880
  EXTERNAL_LORA_STYLES = list(entries)
881
- LORA_STYLES[:] = [e for e in EXTERNAL_LORA_STYLES if e.get("active", True)]
 
 
 
882
 
883
  new_sel = [t for t in (currently_selected or []) if t != selected_title]
884
  choices = [s["title"] for s in get_selectable_styles(dynamic_loras)]
@@ -899,6 +981,8 @@ def save_session_lora_to_catalog(
899
  default_weight,
900
  default_prompt,
901
  dynamic_loras_state,
 
 
902
  ):
903
  dynamic_loras = dict(dynamic_loras_state or {})
904
  save_choices = session_custom_lora_titles(dynamic_loras)
@@ -939,6 +1023,10 @@ def save_session_lora_to_catalog(
939
  weight = 1.0
940
 
941
  prompt = (default_prompt or "").strip() or source.get("default_prompt") or None
 
 
 
 
942
  adapter_base = _sanitize_name(
943
  title, fallback=_sanitize_name(source.get("adapter_name", "custom"))
944
  )
@@ -973,6 +1061,12 @@ def save_session_lora_to_catalog(
973
  prev_approved = bool(entries[existing_idx].get("admin_approved", False))
974
  prev_active = bool(entries[existing_idx].get("active", True))
975
 
 
 
 
 
 
 
976
  entry = {
977
  "image": source.get("image") or _DEFAULT_LORA_IMAGE,
978
  "title": title,
@@ -983,6 +1077,9 @@ def save_session_lora_to_catalog(
983
  "default_weight": weight,
984
  "admin_approved": prev_approved,
985
  "active": prev_active,
 
 
 
986
  "sha256": sha,
987
  }
988
 
@@ -999,7 +1096,10 @@ def save_session_lora_to_catalog(
999
  return _fail(f"❌ Failed to write catalog: {e}")
1000
 
1001
  EXTERNAL_LORA_STYLES = list(entries)
1002
- LORA_STYLES[:] = [e for e in EXTERNAL_LORA_STYLES if e.get("active", True)]
 
 
 
1003
 
1004
  if source_key is not None:
1005
  dynamic_loras.pop(source_key, None)
@@ -1008,8 +1108,9 @@ def save_session_lora_to_catalog(
1008
  save_choices = session_custom_lora_titles(dynamic_loras)
1009
  remove_choices = save_choices + removable_catalog_titles()
1010
  hash_note = f", sha256={sha[:12]}…" if sha else ""
 
1011
  return (
1012
- f"✅ {action} '{title}' → {path} (weight={weight}{hash_note})",
1013
  gr.update(choices=selector_choices),
1014
  gr.update(choices=save_choices, value=None),
1015
  gr.update(choices=remove_choices, value=None),
@@ -1018,18 +1119,22 @@ def save_session_lora_to_catalog(
1018
 
1019
 
1020
  def fill_catalog_save_form(selected_title, dynamic_loras_state):
 
1021
  if not selected_title:
1022
- return gr.update(), gr.update(), gr.update()
1023
  style = None
1024
  for s in (dynamic_loras_state or {}).values():
1025
  if s.get("title") == selected_title:
1026
  style = s
1027
  break
1028
  if not style:
1029
- return gr.update(), gr.update(), gr.update()
1030
  suggested = selected_title.removeprefix("Custom: ").strip() or selected_title
 
1031
  return (
1032
  gr.update(value=suggested),
1033
  gr.update(value=float(style.get("default_weight", 1.0))),
1034
  gr.update(value=style.get("default_prompt") or ""),
 
 
1035
  )
 
271
  return _file_sha256(path)
272
 
273
 
274
+ def _parse_trigger_list(value) -> list[str]:
275
+ """Normalize known_triggers from JSON list or free text (commas/newlines)."""
276
+ if value is None:
277
+ return []
278
+ if isinstance(value, list):
279
+ out = []
280
+ for item in value:
281
+ s = str(item).strip()
282
+ if s:
283
+ out.append(s)
284
+ return out
285
+ text = str(value).replace(",", "\n")
286
+ return [line.strip() for line in text.splitlines() if line.strip()]
287
+
288
+
289
  def _normalize_catalog_entry(raw) -> dict | None:
290
  if not isinstance(raw, dict):
291
  return None
 
316
  else:
317
  sha = None
318
 
319
+ notes = raw.get("notes")
320
+ if isinstance(notes, str):
321
+ notes = notes.strip() or None
322
+ else:
323
+ notes = None
324
+
325
  return {
326
  "image": raw.get("image") or _DEFAULT_LORA_IMAGE,
327
  "title": title,
 
332
  "default_weight": default_weight,
333
  "admin_approved": _as_bool(raw.get("admin_approved"), False),
334
  "active": active,
335
+ "compatible_with_playground": _as_bool(
336
+ raw.get("compatible_with_playground"), True
337
+ ),
338
+ "notes": notes,
339
+ "known_triggers": _parse_trigger_list(
340
+ raw.get("known_triggers") or raw.get("triggers")
341
+ ),
342
  "sha256": sha,
343
  }
344
 
 
353
  "default_weight": float(e.get("default_weight", 1.0)),
354
  "admin_approved": bool(e.get("admin_approved", False)),
355
  "active": bool(e.get("active", True)),
356
+ "compatible_with_playground": bool(e.get("compatible_with_playground", True)),
357
  }
358
+ notes = (e.get("notes") or "").strip() if isinstance(e.get("notes"), str) else e.get("notes")
359
+ if notes:
360
+ out["notes"] = notes
361
+ triggers = _parse_trigger_list(e.get("known_triggers"))
362
+ if triggers:
363
+ out["known_triggers"] = triggers
364
  if e.get("sha256"):
365
  out["sha256"] = e["sha256"]
366
  if e.get("image") and e["image"] != _DEFAULT_LORA_IMAGE:
 
400
  catalog_path = Path(path or PERSISTENT_LORA_CATALOG_PATH)
401
  catalog_path.parent.mkdir(parents=True, exist_ok=True)
402
  payload = {
403
+ "version": 3,
404
  "loras": [_serialize_entry(e) for e in entries],
405
  }
406
  tmp_path = catalog_path.with_suffix(catalog_path.suffix + ".tmp")
 
450
  global EXTERNAL_LORA_STYLES, LORA_STYLES
451
  with _CATALOG_LOCK:
452
  EXTERNAL_LORA_STYLES = _seed_catalog_if_needed(path)
453
+ LORA_STYLES = [
454
+ e for e in EXTERNAL_LORA_STYLES
455
+ if e.get("active", True) and e.get("compatible_with_playground", True)
456
+ ]
457
  print(
458
  f"[lora_registry] Catalog: {len(LORA_STYLES)} active / "
459
  f"{len(EXTERNAL_LORA_STYLES)} total from "
 
478
  EXTERNAL_LORA_STYLES = entries
479
  else:
480
  EXTERNAL_LORA_STYLES = _seed_catalog_if_needed(str(catalog_path))
481
+ LORA_STYLES = [
482
+ e for e in EXTERNAL_LORA_STYLES
483
+ if e.get("active", True) and e.get("compatible_with_playground", True)
484
+ ]
485
  print(
486
  f"[lora_registry] Reloaded catalog: {len(LORA_STYLES)} active / "
487
  f"{len(EXTERNAL_LORA_STYLES)} total from {catalog_path}"
 
524
 
525
 
526
  def _active_catalog() -> list[dict]:
527
+ """Entries shown in the main selector: active and playground-compatible."""
528
+ return [
529
+ e for e in EXTERNAL_LORA_STYLES
530
+ if e.get("active", True) and e.get("compatible_with_playground", True)
531
+ ]
532
 
533
 
534
  def _norm_repo_key(repo: str | None) -> str:
 
596
  return None
597
 
598
 
599
+ def format_selected_lora_details(selected_titles, dynamic_loras) -> str:
600
+ """Plain-text detail block for currently ticked LoRAs (repo/triggers/notes)."""
601
+ styles = [
602
+ get_style_by_title(t, dynamic_loras)
603
+ for t in (selected_titles or [])
604
+ if get_style_by_title(t, dynamic_loras)
605
+ ]
606
+ if not styles:
607
+ return ""
608
+ blocks = []
609
+ for s in styles:
610
+ lines = [
611
+ f"**{s['title']}**",
612
+ f"- repo: `{s.get('repo')}`",
613
+ f"- weights: `{s.get('weights')}`",
614
+ f"- default weight: {s.get('default_weight', 1.0)}",
615
+ ]
616
+ if s.get("default_prompt"):
617
+ lines.append(f"- default prompt: {s['default_prompt']}")
618
+ triggers = _parse_trigger_list(s.get("known_triggers"))
619
+ if triggers:
620
+ lines.append("- known triggers: " + ", ".join(f"`{t}`" for t in triggers))
621
+ if s.get("notes"):
622
+ lines.append(f"- notes: {s['notes']}")
623
+ blocks.append("\n".join(lines))
624
+ return "\n\n".join(blocks)
625
+
626
+
627
  def update_weight_sliders(selected_titles, dynamic_loras):
628
  selected_styles = [
629
  get_style_by_title(t, dynamic_loras)
 
652
  else:
653
  lora_prompt_update = gr.update(value="", visible=False)
654
 
655
+ details = format_selected_lora_details(selected_titles, dynamic_loras)
656
+ details_update = gr.update(value=details, visible=bool(details))
657
+
658
+ return slider_updates + [lora_prompt_update, details_update]
659
 
660
 
661
  def _looks_like_local_path(value: str) -> bool:
 
864
  "default_weight": 1.0,
865
  "admin_approved": False,
866
  "active": True,
867
+ "compatible_with_playground": True,
868
+ "notes": None,
869
+ "known_triggers": [],
870
  "sha256": sha,
871
  "session_only": True,
872
  }
 
957
  dynamic_loras,
958
  )
959
  EXTERNAL_LORA_STYLES = list(entries)
960
+ LORA_STYLES[:] = [
961
+ e for e in EXTERNAL_LORA_STYLES
962
+ if e.get("active", True) and e.get("compatible_with_playground", True)
963
+ ]
964
 
965
  new_sel = [t for t in (currently_selected or []) if t != selected_title]
966
  choices = [s["title"] for s in get_selectable_styles(dynamic_loras)]
 
981
  default_weight,
982
  default_prompt,
983
  dynamic_loras_state,
984
+ known_triggers=None,
985
+ notes=None,
986
  ):
987
  dynamic_loras = dict(dynamic_loras_state or {})
988
  save_choices = session_custom_lora_titles(dynamic_loras)
 
1023
  weight = 1.0
1024
 
1025
  prompt = (default_prompt or "").strip() or source.get("default_prompt") or None
1026
+ triggers = _parse_trigger_list(known_triggers)
1027
+ if not triggers:
1028
+ triggers = _parse_trigger_list(source.get("known_triggers"))
1029
+ note_text = (notes or "").strip() or source.get("notes") or None
1030
  adapter_base = _sanitize_name(
1031
  title, fallback=_sanitize_name(source.get("adapter_name", "custom"))
1032
  )
 
1061
  prev_approved = bool(entries[existing_idx].get("admin_approved", False))
1062
  prev_active = bool(entries[existing_idx].get("active", True))
1063
 
1064
+ prev_compatible = True
1065
+ if existing_idx is not None:
1066
+ prev_compatible = bool(
1067
+ entries[existing_idx].get("compatible_with_playground", True)
1068
+ )
1069
+
1070
  entry = {
1071
  "image": source.get("image") or _DEFAULT_LORA_IMAGE,
1072
  "title": title,
 
1077
  "default_weight": weight,
1078
  "admin_approved": prev_approved,
1079
  "active": prev_active,
1080
+ "compatible_with_playground": prev_compatible,
1081
+ "notes": note_text,
1082
+ "known_triggers": triggers,
1083
  "sha256": sha,
1084
  }
1085
 
 
1096
  return _fail(f"❌ Failed to write catalog: {e}")
1097
 
1098
  EXTERNAL_LORA_STYLES = list(entries)
1099
+ LORA_STYLES[:] = [
1100
+ e for e in EXTERNAL_LORA_STYLES
1101
+ if e.get("active", True) and e.get("compatible_with_playground", True)
1102
+ ]
1103
 
1104
  if source_key is not None:
1105
  dynamic_loras.pop(source_key, None)
 
1108
  save_choices = session_custom_lora_titles(dynamic_loras)
1109
  remove_choices = save_choices + removable_catalog_titles()
1110
  hash_note = f", sha256={sha[:12]}…" if sha else ""
1111
+ trig_note = f", {len(triggers)} trigger(s)" if triggers else ""
1112
  return (
1113
+ f"✅ {action} '{title}' → {path} (weight={weight}{hash_note}{trig_note})",
1114
  gr.update(choices=selector_choices),
1115
  gr.update(choices=save_choices, value=None),
1116
  gr.update(choices=remove_choices, value=None),
 
1119
 
1120
 
1121
  def fill_catalog_save_form(selected_title, dynamic_loras_state):
1122
+ empty = gr.update(), gr.update(), gr.update(), gr.update(), gr.update()
1123
  if not selected_title:
1124
+ return empty
1125
  style = None
1126
  for s in (dynamic_loras_state or {}).values():
1127
  if s.get("title") == selected_title:
1128
  style = s
1129
  break
1130
  if not style:
1131
+ return empty
1132
  suggested = selected_title.removeprefix("Custom: ").strip() or selected_title
1133
+ triggers = _parse_trigger_list(style.get("known_triggers"))
1134
  return (
1135
  gr.update(value=suggested),
1136
  gr.update(value=float(style.get("default_weight", 1.0))),
1137
  gr.update(value=style.get("default_prompt") or ""),
1138
+ gr.update(value="\n".join(triggers)),
1139
+ gr.update(value=style.get("notes") or ""),
1140
  )