File size: 55,054 Bytes
b0e6b3f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5367584
 
b0e6b3f
 
 
 
82881d0
b0e6b3f
 
 
 
109dbfb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b0e6b3f
 
37efa91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b0e6b3f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79af5ce
b0e6b3f
 
 
 
 
 
79af5ce
 
b0e6b3f
 
 
 
 
 
 
c43c199
b0e6b3f
 
c43c199
b0e6b3f
14199d9
 
 
 
 
 
 
 
 
 
 
 
79af5ce
b0e6b3f
79af5ce
 
 
 
 
14199d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0e2099d
 
 
 
14199d9
 
 
 
 
 
 
 
 
79af5ce
 
 
 
 
 
 
 
 
 
 
c43c199
79af5ce
c43c199
79af5ce
 
 
 
b0e6b3f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65527a1
 
 
 
b0e6b3f
 
 
37efa91
b0e6b3f
 
65527a1
 
 
b0e6b3f
109dbfb
b0e6b3f
 
 
 
109dbfb
 
 
 
 
 
 
 
 
b0e6b3f
 
 
5367584
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0e2099d
 
 
b0e6b3f
 
 
 
 
 
5367584
 
 
b0e6b3f
5367584
 
b0e6b3f
 
 
79af5ce
b0e6b3f
 
 
 
 
 
 
 
5367584
b0e6b3f
 
 
 
 
5367584
b0e6b3f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5367584
b0e6b3f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c43c199
 
 
 
a9e72e4
 
 
 
 
 
 
 
b0e6b3f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5367584
b0e6b3f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5367584
b0e6b3f
 
 
 
 
5367584
79af5ce
b0e6b3f
 
 
 
0e2099d
5367584
b0e6b3f
 
 
 
0e2099d
5367584
 
 
b0e6b3f
0e2099d
5367584
 
 
 
 
 
 
b0e6b3f
5367584
 
 
79af5ce
5367584
 
79af5ce
 
5367584
79af5ce
5367584
 
79af5ce
b0e6b3f
 
 
 
 
 
5367584
 
 
 
 
 
 
b0e6b3f
 
5367584
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b0e6b3f
 
5367584
b0e6b3f
 
 
 
 
 
 
 
 
 
 
79af5ce
 
 
 
 
 
 
 
 
 
b0e6b3f
79af5ce
b0e6b3f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37efa91
 
 
 
 
 
b0e6b3f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109dbfb
b0e6b3f
 
109dbfb
 
b0e6b3f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65527a1
 
 
 
 
 
 
 
 
b0e6b3f
 
 
 
65527a1
 
 
b0e6b3f
 
 
 
 
 
109dbfb
 
65527a1
109dbfb
 
b0e6b3f
 
a9e72e4
14199d9
 
 
 
 
 
 
a9e72e4
b0e6b3f
 
 
 
c43c199
 
16b8421
 
 
 
 
 
 
c43c199
 
16b8421
 
 
 
 
 
b0e6b3f
 
 
65527a1
 
b0e6b3f
 
 
37efa91
b0e6b3f
 
 
 
 
 
 
109dbfb
 
b0e6b3f
 
65527a1
 
 
 
 
 
 
b0e6b3f
65527a1
 
 
 
 
 
 
b0e6b3f
 
 
109dbfb
 
 
 
 
 
 
 
 
 
 
 
b0e6b3f
 
 
109dbfb
 
 
b0e6b3f
 
 
 
 
 
37efa91
 
c43c199
 
b0e6b3f
 
109dbfb
b0e6b3f
 
109dbfb
b0e6b3f
 
c43c199
 
109dbfb
 
b0e6b3f
37efa91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109dbfb
 
65527a1
 
b0e6b3f
109dbfb
b0e6b3f
 
 
 
109dbfb
b0e6b3f
 
109dbfb
b0e6b3f
 
 
 
 
 
65527a1
 
 
 
b0e6b3f
 
 
37efa91
b0e6b3f
 
65527a1
 
 
b0e6b3f
109dbfb
b0e6b3f
 
 
 
109dbfb
 
 
 
65527a1
 
109dbfb
 
b0e6b3f
109dbfb
b0e6b3f
109dbfb
 
 
 
 
 
 
 
 
b0e6b3f
109dbfb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b0e6b3f
 
 
109dbfb
 
 
 
b0e6b3f
 
 
109dbfb
b0e6b3f
 
 
65527a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b0e6b3f
 
65527a1
b0e6b3f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109dbfb
 
 
 
 
 
 
 
 
 
 
b0e6b3f
109dbfb
 
 
 
b0e6b3f
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
"""Shared lora library, stored in a Hugging Face storage bucket.

The list lives in `hf://buckets/<owner>/<bucket>/library.json` and holds two
sections, "normal" and "nsfw". Each entry keeps a name, the link, its trigger
words and a strength. A bucket is mutable object storage rather than a git
repo, so the file is simply overwritten on every change and survives a restart
of the Space.

Writing needs an `HF_TOKEN` with write access. When the bucket is mounted into
the Space as a volume, set `LORA_LIBRARY_DIR` to the mount path and the file is
read and written directly, with no token involved.

Used from app.py with a single line inside `with gr.Blocks(...) as demo:`

    lora_library.library_tab(lora_box=user_loras_text, prompt_box=prompt)

or, for a build with fixed lora slots,

    lora_library.library_tab(lora_slots=lora_refs, scale_slots=lora_scales,
                             prompt_box=prompt)
"""

from __future__ import annotations

import json
import os
import re
import time
from contextvars import ContextVar
from threading import Lock

import gradio as gr
import requests

BUCKET = os.environ.get("LORA_LIBRARY_BUCKET", "amisima/wan-2.2-lora-storage")
LIBRARY_FILE = os.environ.get("LORA_LIBRARY_FILE", "library.json")
LOCAL_DIR = os.environ.get("LORA_LIBRARY_DIR", "").strip()
# Fallback for a Space whose huggingface_hub is too old to speak to buckets.
DATASET_REPO = os.environ.get("LORA_LIBRARY_REPO", BUCKET)
# Anyone holding this key may remove any entry. Set it in the Space secrets.
ADMIN_KEY = os.environ.get("LORA_LIBRARY_ADMIN_KEY", "").strip()

_OWNER_JS = """
() => {
  try {
    var k = window.localStorage.getItem('lora_library_owner_v1');
    if (!k) {
      k = 'u' + Math.random().toString(36).slice(2, 10) + Date.now().toString(36);
      window.localStorage.setItem('lora_library_owner_v1', k);
    }
    return k;
  } catch (e) { return ''; }
}
"""

_CIVITAI_DOWNLOAD_RE = re.compile(r"/api/download/models/(\d+)")
_CIVITAI_PAGE_RE = re.compile(r"civitai\.(?:com|red)/models/(\d+)")
_CIVITAI_HOST_RE = re.compile(r"https?://(?:www\.)?(civitai\.(?:com|red))")

# version id -> model page url. Filled in as entries are looked at so a library
# of 50 loras does not hit the API 50 times on every refresh.
_CIVITAI_PAGE_CACHE: dict[str, str] = {}


def civitai_page_url(url: str, resolve: bool = True) -> str:
    """Model page for a civitai link.

    A download link (`/api/download/models/<versionId>`) says nothing about
    which model it belongs to, so the version has to be resolved through the
    API to get `modelId`. With `resolve=False` only links that are already a
    model page, or that were resolved earlier, come back — so redrawing a big
    library never blocks on the network. Returns "" when unknown.
    """
    url = (url or "").strip().strip('"').strip("'")
    if not url:
        return ""

    host_match = _CIVITAI_HOST_RE.match(url)
    if not host_match:
        return ""
    host = host_match.group(1)

    # Already a model page.
    if _CIVITAI_PAGE_RE.search(url):
        return url

    match = _CIVITAI_DOWNLOAD_RE.search(url)
    if not match:
        return ""
    version_id = match.group(1)
    if version_id in _CIVITAI_PAGE_CACHE:
        return _CIVITAI_PAGE_CACHE[version_id]
    if not resolve:
        return ""

    headers = {"User-Agent": "Mozilla/5.0"}
    token = os.environ.get("CIVITAI_TOKEN", "").strip()
    if token:
        headers["Authorization"] = f"Bearer {token}"
    for api_host in (host, "civitai.com", "civitai.red"):
        try:
            response = requests.get(
                f"https://{api_host}/api/v1/model-versions/{version_id}",
                headers=headers, timeout=15,
            )
            response.raise_for_status()
            data = response.json()
        except Exception:  # noqa: BLE001
            continue
        model_id = data.get("modelId") or (data.get("model") or {}).get("id")
        if not model_id:
            continue
        page = f"https://{host}/models/{model_id}?modelVersionId={version_id}"
        _CIVITAI_PAGE_CACHE[version_id] = page
        return page

    _CIVITAI_PAGE_CACHE[version_id] = ""
    return ""


def _entry_page_url(item: dict, resolve: bool = False) -> str:
    """Model page for a library entry, preferring the one saved with it."""
    saved = (item.get("page") or "").strip()
    if saved:
        return saved
    return civitai_page_url(item.get("url") or "", resolve=resolve)


def _links_markdown(entries: list[dict], heading: str, resolve: bool = False) -> str:
    """A numbered list of 🔗 links matching the tick boxes above it."""
    rows, missing = [], 0
    for index, item in enumerate(entries, start=1):
        page = _entry_page_url(item, resolve=resolve)
        name = str(item.get("name") or "").strip() or f"lora {index}"
        if page:
            rows.append(f"{index}. [🔗 {name}]({page})")
        else:
            rows.append(f"{index}. {name}")
            missing += 1
    if not rows:
        return ""
    text = f"**{heading} — open the model page on civitai**  \n" + "  \n".join(rows)
    if missing:
        text += (
            f"  \n<sub>{missing} without a model page yet — press "
            "**🔗 fetch model links**.</sub>"
        )
    return text
_HF_URL_RE = re.compile(r"^https?://(?:www\.)?huggingface\.co/(?:datasets/)?([^/\s]+)/([^/\s?#]+)")


# ---------------------------------------------------------------------------
# storage
# ---------------------------------------------------------------------------

def _token() -> str:
    return (os.environ.get("HF_TOKEN") or os.environ.get("HUGGINGFACE_HUB_TOKEN") or "").strip()


def _bucket_path() -> str:
    return f"buckets/{BUCKET.strip('/')}/{LIBRARY_FILE}"


_mount_cache: dict[str, str] = {}


def _runtime_volume_paths() -> list[str]:
    """Ask the Hub where this Space has its volumes mounted.

    A bucket attached in the Space settings lands at whatever mount path was
    typed in there, so guessing is hopeless. The plain REST endpoint is used
    rather than the Python client, because the client on an older Space does
    not know about volumes at all.
    """
    space_id = os.environ.get("SPACE_ID") or os.environ.get("SPACE_REPO_ID") or ""
    if not space_id:
        return []
    headers = {"User-Agent": "Mozilla/5.0"}
    token = _token()
    if token:
        headers["Authorization"] = f"Bearer {token}"
    try:
        response = requests.get(
            f"https://huggingface.co/api/spaces/{space_id}/runtime",
            headers=headers, timeout=20,
        )
        response.raise_for_status()
        data = response.json()
    except Exception as error:  # noqa: BLE001
        print(f"[lora-library] could not read the Space runtime: "
              f"{type(error).__name__}: {error}", flush=True)
        return []

    wanted = BUCKET.strip("/").lower()
    preferred, others = [], []

    def walk(node):
        if isinstance(node, dict):
            mount = node.get("mountPath") or node.get("mount_path")
            if isinstance(mount, str) and mount.startswith("/"):
                source = str(node.get("source") or node.get("repoId") or "").strip("/").lower()
                (preferred if source.endswith(wanted) else others).append(mount)
            for value in node.values():
                walk(value)
        elif isinstance(node, list):
            for value in node:
                walk(value)

    walk(data)
    return preferred + others


def _mount_table_paths() -> list[str]:
    """Mount points that look like an attached volume rather than the OS."""
    system = ("/proc", "/sys", "/dev", "/run", "/etc", "/usr", "/lib", "/bin",
              "/sbin", "/boot", "/var/lib", "/var/run")
    found = []
    try:
        with open("/proc/mounts", "r", encoding="utf-8", errors="ignore") as handle:
            for line in handle:
                parts = line.split()
                if len(parts) < 3:
                    continue
                point, fstype = parts[1], parts[2]
                if point == "/" or any(point.startswith(prefix) for prefix in system):
                    continue
                if fstype in ("nfs", "nfs4", "fuse", "virtiofs", "9p") or fstype.startswith("fuse"):
                    found.insert(0, point)
                elif point.count("/") == 1:
                    found.append(point)
    except Exception:  # noqa: BLE001
        return []
    return found


def _volume_mount_path() -> str:
    if "path" in _mount_cache:
        return str(_mount_cache["path"])
    _mount_cache["path"] = ""
    for path in _runtime_volume_paths() + _mount_table_paths():
        if path and os.path.isdir(path):
            _mount_cache["path"] = path
            print(f"[lora-library] using the volume mounted at {path}", flush=True)
            break
    return str(_mount_cache["path"])


def _mount_candidates() -> list:
    """Every folder an attached bucket could be sitting in, best guess first."""
    bucket_name = BUCKET.strip("/").split("/")[-1]
    candidates = [LOCAL_DIR] if LOCAL_DIR else []
    candidates.append(_volume_mount_path())
    candidates += [
        f"/bucket/{bucket_name}", "/bucket",
        f"/buckets/{bucket_name}", "/buckets",
        f"/mnt/bucket/{bucket_name}", "/mnt/bucket",
        f"/data/{bucket_name}",
        f"/mnt/data/{bucket_name}", "/mnt/data",
        f"/storage/{bucket_name}", "/storage",
        "/data",
    ]
    # Plus every top-level folder that is not part of the OS. A bucket can be mounted at any
    # path the Space owner types in, so guessing names is hopeless - it is cheaper to look at
    # what is actually there and let the "already holds the file" rule below pick the winner.
    system = {"bin", "boot", "dev", "etc", "home", "lib", "lib32", "lib64", "libx32",
              "media", "opt", "proc", "root", "run", "sbin", "srv", "sys", "tmp", "usr", "var"}
    try:
        for entry in sorted(os.listdir("/")):
            if entry.lower() not in system and not entry.startswith("."):
                candidates.append(f"/{entry}")
    except Exception:  # noqa: BLE001
        pass

    out = []
    for directory in candidates:
        if directory and directory not in out and os.path.isdir(directory):
            out.append(directory)
    return out


def storage_report() -> str:
    """Everything the library can see about where it might live. For when the list comes back
    empty and the mount path in the Space settings needs checking against reality."""
    lines = [
        f"**bucket setting:** `{BUCKET}`",
        f"**file name:** `{LIBRARY_FILE}`",
        f"**LORA_LIBRARY_DIR:** `{LOCAL_DIR or '(not set)'}`",
        f"**HF_TOKEN present:** {'yes' if _token() else 'no'}",
        f"**backend in use:** {_active['backend'] or '(none yet)'}"
        + (f" - {_active['detail']}" if _active["detail"] else ""),
        "",
        f"**mount paths the Hub reports:** {_runtime_volume_paths() or '(none)'}",
        f"**mount points in /proc/mounts:** {_mount_table_paths() or '(none)'}",
        "",
        "**folders checked, in order:**",
    ]
    for directory in _mount_candidates():
        path = os.path.join(directory, LIBRARY_FILE)
        marks = []
        marks.append("readable" if os.access(directory, os.R_OK) else "NOT readable")
        marks.append("writable" if os.access(directory, os.W_OK) else "not writable")
        if os.path.exists(path):
            try:
                marks.append(f"**has {LIBRARY_FILE}, {os.path.getsize(path) / 1024:.1f} kB**")
            except OSError:
                marks.append(f"**has {LIBRARY_FILE}**")
        else:
            marks.append(f"no {LIBRARY_FILE}")
        try:
            inside = sorted(os.listdir(directory))[:8]
        except Exception:  # noqa: BLE001
            inside = ["(cannot list)"]
        lines.append(f"- `{directory}` - {', '.join(marks)}  \n  <sub>contains: "
                     f"{', '.join(inside) or '(empty)'}</sub>")

    lines += ["", "**what each source said on the last read:**"]
    for name, _reader, _writer in _BACKENDS:
        lines.append(f"- {name}: {_last_read.get(name, '(not tried)')}")

    chosen = _mounted_file()
    lines += ["", f"**would read from:** `{chosen or '(no mounted folder found)'}`"]
    if chosen and not os.path.exists(chosen):
        lines.append("<sub>that file does not exist yet, so the mounted bucket looks empty. "
                     "If the folder above is not where your bucket is mounted, set "
                     "`LORA_LIBRARY_DIR` to the right path in the Space settings.</sub>")
    return "\n".join(lines)


def _mounted_file(require_write: bool = False) -> str:
    """The library file on a locally mounted bucket, or "" when there is none.

    Two rules, both learned the hard way. A folder that already holds the file wins, because
    a Space can have several writable folders - persistent storage, a scratch mount, the bucket -
    and only one of them is the shared list; taking whichever came first meant reading an empty
    file forever and no amount of refreshing helped. And reading only needs the folder to be
    readable: demanding write access made a read-only or oddly-permissioned mount look absent,
    which dropped the whole thing through to the empty dataset fallback."""
    folders = _mount_candidates()
    for directory in folders:
        path = os.path.join(directory, LIBRARY_FILE)
        if os.path.exists(path) and (not require_write or os.access(directory, os.W_OK)):
            return path
    for directory in folders:
        if os.access(directory, os.W_OK):
            return os.path.join(directory, LIBRARY_FILE)
    return ""


def _hub_has_buckets() -> bool:
    """Buckets need huggingface_hub 1.5 or newer; older Spaces have to fall back."""
    try:
        from huggingface_hub import HfFileSystem  # noqa: F401
    except Exception:  # noqa: BLE001
        return False
    try:
        from importlib.metadata import version as _version

        parts = _version("huggingface_hub").split(".")
        return (int(parts[0]), int(parts[1])) >= (1, 5)
    except Exception:  # noqa: BLE001
        return True


def _filesystem():
    from huggingface_hub import HfFileSystem

    token = _token()
    return HfFileSystem(token=token) if token else HfFileSystem()


def _empty() -> dict:
    return {"normal": [], "nsfw": []}


def _clean(data) -> dict:
    out = _empty()
    if not isinstance(data, dict):
        return out
    for section in ("normal", "nsfw"):
        for item in data.get(section) or []:
            if not isinstance(item, dict):
                continue
            url = str(item.get("url") or "").strip()
            if not url:
                continue
            try:
                strength = float(item.get("strength", 1.0))
            except (TypeError, ValueError):
                strength = 1.0
            try:
                low_strength = float(item.get("low_strength", strength))
            except (TypeError, ValueError):
                low_strength = strength
            out[section].append({
                "name": str(item.get("name") or "").strip() or url.split("/")[-1][:60],
                "url": url,
                "page": str(item.get("page") or "").strip(),
                "trigger": str(item.get("trigger") or "").strip(),
                "strength": strength,
                "high": str(item.get("high") or "").strip(),
                "low": str(item.get("low") or "").strip(),
                "low_strength": low_strength,
                "added": str(item.get("added") or ""),
                "owner": str(item.get("owner") or ""),
            })
    return out


def _may_delete(item: dict, owner: str) -> bool:
    owner = (owner or "").strip()
    if not owner:
        return False
    if ADMIN_KEY and owner == ADMIN_KEY:
        return True
    return bool(item.get("owner")) and item.get("owner") == owner


# Every backend is tried in turn, and whichever one answers is remembered so the
# reader and the writer never end up looking in two different places.
_active = {"backend": "", "detail": ""}
# Each callback must save the library it actually read. A different visitor's
# refresh must not turn a failed read into permission to overwrite storage.
_library_read_snapshot = ContextVar("lora_library_read_snapshot", default=None)
_library_save_lock = Lock()


def _library_storage_data(data):
    """Do not turn a damaged stored library into an apparently new empty one."""
    if not isinstance(data, dict):
        raise ValueError("the stored library must be a JSON object")
    for section in ("normal", "nsfw"):
        entries = data.get(section, [])
        if not isinstance(entries, list) or any(
                not isinstance(item, dict) or not str(item.get("url") or "").strip()
                for item in entries):
            raise ValueError(f"the stored {section} section must contain library entries")
    return _clean(data)
# What each backend said on the last read. Kept so the report can show why a source was
# skipped instead of silently falling through to the next one.
_last_read = {}


def _read_mounted():
    mounted = _mounted_file()
    if not mounted:
        return None
    try:
        handle = open(mounted, "r", encoding="utf-8")
    except FileNotFoundError:
        return _empty()
    with handle:
        return _library_storage_data(json.load(handle))


def _write_mounted(payload: str) -> None:
    mounted = _mounted_file(require_write=True)
    if not mounted:
        raise RuntimeError("no mounted bucket")
    with open(mounted, "w", encoding="utf-8") as handle:
        handle.write(payload)


def _read_bucket():
    if not _hub_has_buckets():
        return None  # Unsupported reader; the private Hub reader is the fallback.
    filesystem = _filesystem()
    path = _bucket_path()
    if not filesystem.exists(path):
        return _empty()
    with filesystem.open(path, "r") as handle:
        return _library_storage_data(json.loads(handle.read()))


def _write_bucket(payload: str) -> None:
    if not _hub_has_buckets():
        raise RuntimeError("huggingface_hub is too old for buckets (needs 1.5+)")
    if not _token():
        raise RuntimeError("no HF_TOKEN")
    filesystem = _filesystem()
    with filesystem.open(_bucket_path(), "w") as handle:
        handle.write(payload)


def _read_dataset():
    url = (f"https://huggingface.co/datasets/{DATASET_REPO}/resolve/main/"
           f"{LIBRARY_FILE}?cb={int(time.time())}")
    headers = {"User-Agent": "Mozilla/5.0"}
    token = _token()
    if token:
        headers["Authorization"] = f"Bearer {token}"
    response = requests.get(url, headers=headers, timeout=25)
    if response.status_code == 404:
        return _empty()
    response.raise_for_status()
    return _library_storage_data(response.json())


def _write_dataset(payload: str) -> None:
    if not _token():
        raise RuntimeError("no HF_TOKEN")
    import io

    from huggingface_hub import HfApi

    api = HfApi(token=_token())
    api.create_repo(repo_id=DATASET_REPO, repo_type="dataset", exist_ok=True, private=True)
    api.upload_file(
        path_or_fileobj=io.BytesIO(payload.encode("utf-8")),
        path_in_repo=LIBRARY_FILE,
        repo_id=DATASET_REPO,
        repo_type="dataset",
        commit_message="update lora library",
    )


def _mounted_name() -> str:
    mounted = _mounted_file()
    if not mounted:
        return "mounted bucket"
    where = os.path.dirname(mounted)
    if not os.path.exists(mounted):
        return (f"**{where}** - there is no {LIBRARY_FILE} in that folder, so this is a fresh "
                f"empty list, not `{BUCKET}`. check where the bucket is mounted in the Space "
                f"settings.")
    try:
        size = os.path.getsize(mounted)
    except OSError:
        size = 0
    return f"**{where}/{LIBRARY_FILE}** ({size / 1024:.1f} kB)"


# ---------------------------------------------------------------------------
# buckets from a Space whose huggingface_hub is pinned below 1.5
# ---------------------------------------------------------------------------
# Some builds pin an old huggingface_hub on purpose (diffusers / ComfyUI
# compatibility) and upgrading it in place would break the Space. A modern copy
# is installed into a directory of its own and driven from a subprocess, so the
# running app never imports it.

_ISOLATED_DIR = os.environ.get("LORA_LIBRARY_HUB_DIR", "/tmp/lora_library_hub")
_isolated_state: dict[str, object] = {}


def _isolated_ready() -> bool:
    if "ok" in _isolated_state:
        return bool(_isolated_state["ok"])
    _isolated_state["ok"] = False
    if os.path.isdir(os.path.join(_ISOLATED_DIR, "huggingface_hub")):
        _isolated_state["ok"] = True
        return True
    print("[lora-library] installing a private huggingface_hub for bucket access…", flush=True)
    import subprocess
    import sys

    try:
        subprocess.run(
            [sys.executable, "-m", "pip", "install", "--no-cache-dir", "--quiet",
             "--target", _ISOLATED_DIR, "huggingface_hub>=1.5"],
            check=True, timeout=600,
        )
        _isolated_state["ok"] = os.path.isdir(os.path.join(_ISOLATED_DIR, "huggingface_hub"))
    except Exception as error:  # noqa: BLE001
        print(f"[lora-library] private huggingface_hub install failed: "
              f"{type(error).__name__}: {error}", flush=True)
    return bool(_isolated_state["ok"])


def _isolated_run(mode: str, payload_path: str = "") -> str:
    import subprocess
    import sys

    if not _isolated_ready():
        raise RuntimeError("could not install a huggingface_hub new enough for buckets")
    script = (
        "import sys, json, os\n"
        f"sys.path.insert(0, {_ISOLATED_DIR!r})\n"
        "from huggingface_hub import HfFileSystem\n"
        "token = os.environ.get('HF_TOKEN') or os.environ.get('HUGGINGFACE_HUB_TOKEN') or None\n"
        "fs = HfFileSystem(token=token)\n"
        f"path = {_bucket_path()!r}\n"
        f"mode = {mode!r}\n"
        f"payload_path = {payload_path!r}\n"
        "if mode == 'read':\n"
        "    if fs.exists(path):\n"
        "        with fs.open(path, 'r') as handle:\n"
        "            sys.stdout.write(handle.read())\n"
        "    else:\n"
        "        sys.stdout.write('{}')\n"
        "else:\n"
        "    with open(payload_path, 'r', encoding='utf-8') as handle:\n"
        "        body = handle.read()\n"
        "    with fs.open(path, 'w') as handle:\n"
        "        handle.write(body)\n"
        "    sys.stdout.write('ok')\n"
    )
    environment = dict(os.environ)
    environment.pop("PYTHONPATH", None)
    result = subprocess.run(
        [sys.executable, "-c", script],
        capture_output=True, text=True, timeout=180, env=environment,
    )
    if result.returncode != 0:
        raise RuntimeError((result.stderr or "").strip().splitlines()[-1:] or "subprocess failed")
    return result.stdout


def _read_isolated():
    return _library_storage_data(json.loads(_isolated_run("read")))


def _write_isolated(payload: str) -> None:
    import tempfile

    if not _token():
        raise RuntimeError("no HF_TOKEN")
    handle = tempfile.NamedTemporaryFile("w", suffix=".json", delete=False, encoding="utf-8")
    handle.write(payload)
    handle.close()
    try:
        _isolated_run("write", handle.name)
    finally:
        os.unlink(handle.name)


_BACKENDS = [
    ("mounted bucket", _read_mounted, _write_mounted),
    (f"bucket {BUCKET}", _read_bucket, _write_bucket),
    (f"bucket {BUCKET} (private hub)", _read_isolated, _write_isolated),
    (f"dataset {DATASET_REPO}", _read_dataset, _write_dataset),
]


def load_library() -> dict:
    """Read from the backend that last worked, or find one that does."""
    _library_read_snapshot.set(None)
    ordered = _BACKENDS
    if _active["backend"]:
        ordered = ([b for b in _BACKENDS if b[0] == _active["backend"]]
                   + [b for b in _BACKENDS if b[0] != _active["backend"]])
    first_error = ""
    failed = set()
    fallback = None
    for name, reader, _writer in ordered:
        try:
            data = reader()
        except Exception as error:  # noqa: BLE001
            _last_read[name] = f"failed - {type(error).__name__}: {error}"
            failed.add(name)
            first_error = first_error or f"{name}: {type(error).__name__}: {error}"
            print(f"[lora-library] read from {name} failed: {error}", flush=True)
            continue
        if data is None:
            _last_read[name] = "not available here"
            if name == _active["backend"]:
                failed.add(name)
                first_error = first_error or f"{name}: the previous storage is unavailable"
            continue
        _last_read[name] = f"read {len(data['normal'])} ordinary, {len(data['nsfw'])} nsfw"
        if (data["normal"] or data["nsfw"]
                or (name == _active["backend"] and _active.get("empty_confirmed"))):
            if failed:
                # A fallback may be stale. Display it without making it writable
                # or replacing the primary source for the next refresh.
                _active["detail"] = first_error
                return data
            _active["backend"], _active["detail"] = name, ""
            if data["normal"] or data["nsfw"]:
                _active["empty_confirmed"] = False
            _library_read_snapshot.set((name, _clean(data), failed))
            return data
        # Keep WAN's nonempty fallback search, but an empty alternative must
        # never authorize writes when another storage backend could not be read.
        if fallback is None:
            fallback = (name, data)
    if fallback is not None and not failed:
        _active["backend"], _active["detail"] = fallback[0], ""
        _active["empty_confirmed"] = False
        _library_read_snapshot.set((fallback[0], _clean(fallback[1]), failed))
        return fallback[1]
    _active["detail"] = first_error
    return _empty()


def save_library(library: dict) -> str:
    """Write, then read back to prove it landed. Returns "" or a message."""
    snapshot = _library_read_snapshot.get()
    if snapshot is None:
        return ("**nothing was saved.** The library could not be read reliably. "
                "Refresh and retry after storage is available; existing entries were left untouched.")
    source, before, failed = snapshot
    wanted = _clean(library)
    payload = json.dumps(wanted, ensure_ascii=False, indent=1)

    problems = []
    ordered = ([b for b in _BACKENDS if b[0] == source]
               + [b for b in _BACKENDS if b[0] != source and b[0] not in failed])
    with _library_save_lock:
        for name, reader, writer in ordered:
            try:
                current = reader()
                if current is None or _clean(current) != before:
                    problems.append(f"{name}: contents changed or differ from the library read; refresh and retry")
                    # A changed source means this edit is stale, even if a backup still matches.
                    if name == source:
                        break
                    continue
            except Exception as error:  # noqa: BLE001
                problems.append(f"{name}: {type(error).__name__}: {error}")
                if name == source:
                    break
                continue
            try:
                writer(payload)
            except Exception as error:  # noqa: BLE001
                problems.append(f"{name}: {type(error).__name__}: {error}")
                continue
            try:
                written = reader()
            except Exception as error:  # noqa: BLE001
                problems.append(f"{name}: written, but could not be read back ({error})")
                continue
            if written is None or _clean(written) != wanted:
                problems.append(f"{name}: written, but the saved contents could not be verified")
                continue
            _active["backend"], _active["detail"] = name, ""
            _active["empty_confirmed"] = not (written["normal"] or written["nsfw"])
            _library_read_snapshot.set((name, _clean(written), set()))
            return ""

    detail = "; ".join(problems) or "no storage backend is available"
    _library_read_snapshot.set(None)
    _active["detail"] = detail
    return (f"**nothing was saved.** {detail}\n\n"
            f"Easiest fix: attach the `{BUCKET}` bucket to this Space under "
            "*Settings → Storage Buckets* (any mount path will do) — then no token is needed "
            "at all. Otherwise add an `HF_TOKEN` with write access under "
            "*Settings → Variables and secrets*.")


def storage_note() -> str:
    if _active["backend"] and not _active["detail"]:
        name = _mounted_name() if _active["backend"] == "mounted bucket" else _active["backend"]
        line = f"storage: {name}"
        if _active["backend"] != "mounted bucket":
            # Anything other than the mounted bucket means the mount was not found or could
            # not be read, and that is almost always why the list comes back empty. Say where
            # it looked, so the mount path in the Space settings can be checked against it.
            looked = ", ".join(f"`{d}`" for d in _mount_candidates()) or "nothing"
            line += (f"  \n<sub>the attached bucket was not used. folders checked: {looked}. "
                     f"if your bucket is mounted somewhere else, set `LORA_LIBRARY_DIR` to that "
                     f"path in the Space settings.</sub>")
        return line
    if _active["detail"]:
        return f"storage: not working yet - {_active['detail']}"
    return "storage: not contacted yet"


# ---------------------------------------------------------------------------
# reading a link
# ---------------------------------------------------------------------------

def lookup_link(url: str):
    """(name, trigger words) for a civitai / huggingface link."""
    url = (url or "").strip().strip('"').strip("'")
    if not url:
        return "", ""

    match = _CIVITAI_DOWNLOAD_RE.search(url)
    if match:
        headers = {"User-Agent": "Mozilla/5.0"}
        civitai_token = os.environ.get("CIVITAI_TOKEN", "").strip()
        if civitai_token:
            headers["Authorization"] = f"Bearer {civitai_token}"
        for host in ("civitai.red", "civitai.com"):
            try:
                response = requests.get(
                    f"https://{host}/api/v1/model-versions/{match.group(1)}",
                    headers=headers, timeout=20,
                )
                response.raise_for_status()
                data = response.json()
            except Exception:  # noqa: BLE001
                continue
            model_name = (data.get("model") or {}).get("name") or ""
            version_name = data.get("name") or ""
            label = f"{model_name} · {version_name}".strip(" ·")
            model_id = data.get("modelId") or (data.get("model") or {}).get("id")
            if model_id:
                page_host = "civitai.red" if "civitai.red" in url else "civitai.com"
                _CIVITAI_PAGE_CACHE[match.group(1)] = (
                    f"https://{page_host}/models/{model_id}?modelVersionId={match.group(1)}"
                )
            words = [w for w in (data.get("trainedWords") or []) if w]
            return label, ", ".join(words[:8])
        return "", ""

    hf_match = _HF_URL_RE.match(url)
    if hf_match:
        return f"{hf_match.group(1)}/{hf_match.group(2)}", ""

    return url.split("?")[0].split("/")[-1], ""


# ---------------------------------------------------------------------------
# helpers
# ---------------------------------------------------------------------------

def _labels(entries: list[dict], owner: str = "") -> list[str]:
    labels = []
    for index, item in enumerate(entries, start=1):
        mark = "🟢" if _may_delete(item, owner) else "🔒"
        label = f"{mark} {index}. {item['name']}  ·  strength {item['strength']:g}"
        if item.get("trigger"):
            label += f"  ·  trigger: {item['trigger']}"
        labels.append(label[:220])
    return labels


def _picked(entries: list[dict], labels: list[str], chosen) -> list[dict]:
    index_by_label = {label: i for i, label in enumerate(labels)}
    out = []
    for label in chosen or []:
        index = index_by_label.get(label)
        if index is not None and index < len(entries):
            out.append(entries[index])
    return out


def _autofind(predicate):
    try:
        from gradio.context import Context

        root = getattr(Context, "root_block", None)
        if root is None:
            return None
        for component in root.blocks.values():
            if isinstance(component, gr.Textbox):
                label = str(getattr(component, "label", "") or "")
                if predicate(label.lower()):
                    return component
    except Exception:  # noqa: BLE001
        return None
    return None


def _is_lora_box(label: str) -> bool:
    return "lora" in label and "search" not in label and "civitai" not in label


def _is_prompt_box(label: str) -> bool:
    if "prompt" not in label:
        return False
    return not any(word in label for word in
                   ("negative", "segment", "relay", "scene", "enhance", "search"))


def _with_triggers(prompt_text: str, triggers: list[str]) -> str:
    text = prompt_text or ""
    missing = [t for t in triggers if t and t.lower() not in text.lower()]
    if not missing:
        return text
    return f"{text.rstrip().rstrip(',')}, {', '.join(missing)}".strip(" ,")


# ---------------------------------------------------------------------------
# UI
# ---------------------------------------------------------------------------

def library_tab(lora_box=None, prompt_box=None, lora_slots=None, scale_slots=None,
                pair_slots=None, label: str = "📚 lora library"):
    """Draw the library tab. Hand it either a multi-line lora textbox
    (`lora_box`), a list of fixed slots (`lora_slots` + `scale_slots`), or a
    list of high/low pairs (`pair_slots`), each one a tuple of
    (repo, high, high_strength, low, low_strength)."""
    pairs = [tuple(p) for p in (pair_slots or [])]
    slots = [] if pairs else list(lora_slots or [])
    scales = [] if pairs else list(scale_slots or [])
    if not pairs and not slots and lora_box is None:
        lora_box = _autofind(_is_lora_box)
    if prompt_box is None:
        prompt_box = _autofind(_is_prompt_box)

    has_pairs = bool(pairs)
    has_slots = (not has_pairs) and bool(slots)
    has_box = (not has_pairs) and (not has_slots) and lora_box is not None
    has_prompt = prompt_box is not None

    library = load_library()

    with gr.Tab(label):
        gr.Markdown(
            f"a shared list, kept in the `{BUCKET}` bucket, so it survives a restart. "
            "tick what you want and it goes straight into the lora "
            + ("slots" if (has_slots or has_pairs) else "box") + ", trigger words and all.  \n"
            "🟢 = added by you, so you can remove it. 🔒 = somebody else's, and "
            "only they can remove it."
        )

        storage_line = gr.Markdown(storage_note())
        with gr.Accordion("🔎 where is this list coming from?", open=False):
            report_button = gr.Button("🔎 check the storage")
            report_box = gr.Markdown(
                "Press the button. It lists every folder the library looked in, whether your "
                "bucket is really there, and which file it would read."
            )
            report_button.click(lambda: storage_report(), None, report_box)

        state = gr.State(library)
        normal_labels = gr.State(_labels(library["normal"]))
        nsfw_labels = gr.State(_labels(library["nsfw"]))

        with gr.Accordion(f"📋 ordinary — {len(library['normal'])}",
                          open=False) as normal_box:
            normal_pick = gr.CheckboxGroup(
                choices=_labels(library["normal"]), value=[],
                label="ordinary", show_label=False, interactive=True,
            )
            with gr.Accordion("🔗 the links", open=False):
                normal_links = gr.Markdown(_links_markdown(library["normal"], "ordinary"))

        with gr.Accordion(f"🔞 nsfw — {len(library['nsfw'])}",
                          open=False) as nsfw_box:
            nsfw_pick = gr.CheckboxGroup(
                choices=_labels(library["nsfw"]), value=[],
                label="nsfw", show_label=False, interactive=True,
            )
            with gr.Accordion("🔗 the links", open=False):
                nsfw_links = gr.Markdown(_links_markdown(library["nsfw"], "nsfw"))

        with gr.Row():
            add_button = gr.Button(
                "➕ add the ticked ones to the lora "
                + ("slots" if (has_slots or has_pairs) else "box"),
                variant="primary",
            )
            refresh_button = gr.Button("🔄 refresh")
            links_button = gr.Button("🔗 fetch model links")

        use_triggers = gr.Checkbox(
            value=True, label="append the trigger words to the prompt",
        )
        status = gr.Markdown("")

        with gr.Accordion("➕ add a lora to the library", open=False):
            new_url = gr.Textbox(label="link (civitai / huggingface / direct .safetensors)", lines=1)
            lookup_button = gr.Button("🔎 read the name and triggers off the link")
            new_name = gr.Textbox(label="name", lines=1)
            new_trigger = gr.Textbox(label="trigger words", lines=1)
            if has_pairs:
                with gr.Row():
                    new_high = gr.Textbox(label="high file (optional)", lines=1)
                    new_low = gr.Textbox(label="low file (optional)", lines=1)
                with gr.Row():
                    new_strength = gr.Number(value=1.0, label="high strength", precision=2)
                    new_low_strength = gr.Number(value=1.0, label="low strength", precision=2)
                new_nsfw = gr.Checkbox(value=False, label="nsfw")
            else:
                new_high = gr.Textbox(value="", visible=False)
                new_low = gr.Textbox(value="", visible=False)
                with gr.Row():
                    new_strength = gr.Number(value=1.0, label="strength", precision=2)
                    new_nsfw = gr.Checkbox(value=False, label="nsfw")
                new_low_strength = gr.Number(value=1.0, visible=False)
            save_button = gr.Button("💾 save to the library", variant="primary")

        with gr.Accordion("🗑 remove", open=False):
            gr.Markdown(
                "you can only remove the entries **you** added — those are the 🟢 ones. "
                "🔒 belongs to somebody else and is left alone."
            )
            delete_button = gr.Button("remove the ticked ones that are mine")

        with gr.Accordion("🔑 my key", open=False):
            owner_box = gr.Textbox(
                value="", label="this browser's key", lines=1,
                info="made automatically and kept in this browser. copy it into "
                     "another browser to keep ownership of what you added.",
            )

        # ------------------------------------------------------------ events

        def _pack(data, message, owner=""):
            normal = _labels(data["normal"], owner)
            nsfw = _labels(data["nsfw"], owner)
            return (
                data, normal, nsfw,
                gr.update(choices=normal, value=[]),
                gr.update(choices=nsfw, value=[]),
                message,
                storage_note(),
                _links_markdown(data["normal"], "ordinary"),
                _links_markdown(data["nsfw"], "nsfw"),
                gr.update(label=f"📋 ordinary — {len(data['normal'])}"),
                gr.update(label=f"🔞 nsfw — {len(data['nsfw'])}"),
            )

        def _refresh(owner=""):
            data = load_library()
            return _pack(data, f"loaded: {len(data['normal'])} ordinary, "
                               f"{len(data['nsfw'])} nsfw", owner)

        refresh_targets = [state, normal_labels, nsfw_labels, normal_pick, nsfw_pick,
                           status, storage_line, normal_links, nsfw_links,
                           normal_box, nsfw_box]
        refresh_button.click(_refresh, [owner_box], refresh_targets)
        owner_box.change(_refresh, [owner_box], refresh_targets)

        def _fetch_links(owner=""):
            """Resolve every download link to its model page and write the
            result back, so it is looked up once and then kept."""
            data = load_library()
            resolved = 0
            for section in ("normal", "nsfw"):
                for item in data[section]:
                    if (item.get("page") or "").strip():
                        continue
                    page = civitai_page_url(item.get("url") or "", resolve=True)
                    if page:
                        item["page"] = page
                        resolved += 1
            message = f"model links found: {resolved}" if resolved else "nothing new to look up."
            if resolved:
                error = save_library(data)
                if error:
                    message = f"{message} (not saved — {error})"
                else:
                    data = load_library()
            return _pack(data, message, owner)

        links_button.click(_fetch_links, [owner_box], refresh_targets)

        lookup_button.click(lambda url: lookup_link(url), new_url, [new_name, new_trigger])

        def _save(data, url, name, trigger, strength, nsfw, high="", low="",
                  low_strength=None, owner=""):
            url = (url or "").strip().strip('"').strip("'")
            owner = (owner or "").strip()
            # Start from what storage actually holds, so two people adding a lora
            # at the same time do not overwrite each other.
            data = load_library()
            if not url:
                return _pack(data, "paste a link first.", owner)
            for existing in data["normal"] + data["nsfw"]:
                if existing["url"] == url:
                    return _pack(data, f"already on the list: {existing['name']}", owner)
            if not (name or "").strip():
                name = lookup_link(url)[0] or url.split("?")[0].split("/")[-1]
            try:
                strength = float(strength)
            except (TypeError, ValueError):
                strength = 1.0
            try:
                low_strength = float(low_strength)
            except (TypeError, ValueError):
                low_strength = strength
            data["nsfw" if nsfw else "normal"].append({
                "name": name.strip(),
                "url": url,
                "page": civitai_page_url(url),
                "trigger": (trigger or "").strip(),
                "strength": strength,
                "high": (high or "").strip(),
                "low": (low or "").strip(),
                "low_strength": low_strength,
                "added": time.strftime("%Y-%m-%d"),
                "owner": owner,
            })
            error = save_library(data)
            # Show what came back out of storage, not what went in.
            return _pack(load_library() if not error else data,
                         error or f"added and saved: {name.strip()}", owner)

        save_button.click(
            _save,
            [state, new_url, new_name, new_trigger, new_strength, new_nsfw,
             new_high, new_low, new_low_strength, owner_box],
            refresh_targets,
        )

        def _delete(data, normal, nsfw, normal_chosen, nsfw_chosen, owner=""):
            data = _clean(data)
            owner = (owner or "").strip()
            chosen = (_picked(data["normal"], normal, normal_chosen)
                      + _picked(data["nsfw"], nsfw, nsfw_chosen))
            if not chosen:
                return _pack(data, "nothing is ticked.", owner)
            if not owner:
                return _pack(data, "no key for this browser, so nothing can be "
                                   "removed. open **🔑 my key** and paste yours in.", owner)

            fresh = load_library()
            by_url = {}
            for section in ("normal", "nsfw"):
                for item in fresh[section]:
                    by_url[item["url"]] = item

            doomed, refused = set(), []
            for item in chosen:
                current = by_url.get(item["url"], item)
                if _may_delete(current, owner):
                    doomed.add(item["url"])
                else:
                    refused.append(current.get("name") or item["url"])

            if not doomed:
                return _pack(fresh, "none of those are yours, so nothing was "
                                    f"removed: {', '.join(refused)}", owner)

            for section in ("normal", "nsfw"):
                fresh[section] = [i for i in fresh[section] if i["url"] not in doomed]
            error = save_library(fresh)
            message = error or f"removed: {len(doomed)}"
            if not error and refused:
                message += f"  \nleft alone (not yours): {', '.join(refused)}"
            return _pack(load_library() if not error else data, message, owner)

        delete_button.click(
            _delete,
            [state, normal_labels, nsfw_labels, normal_pick, nsfw_pick, owner_box],
            refresh_targets,
        )

        # ------------------------------------------- adding to high/low pairs

        if has_pairs:
            flat_pairs = [component for row in pairs for component in row]
            add_inputs = [state, normal_labels, nsfw_labels, normal_pick, nsfw_pick, use_triggers]
            add_outputs = [status]
            if has_prompt:
                add_inputs.append(prompt_box)
                add_outputs.append(prompt_box)
            add_outputs = add_outputs + flat_pairs

            def _add_pairs(data, normal, nsfw, normal_chosen, nsfw_chosen, triggers_on,
                           prompt_text=""):
                data = _clean(data)
                chosen = (_picked(data["normal"], normal, normal_chosen)
                          + _picked(data["nsfw"], nsfw, nsfw_chosen))
                blank = [gr.update() for _ in flat_pairs]
                if not chosen:
                    return tuple(["nothing is ticked."]
                                 + ([gr.update()] if has_prompt else []) + blank)

                used = chosen[:len(pairs)]
                spare = chosen[len(pairs):]

                updates = []
                for index in range(len(pairs)):
                    if index >= len(used):
                        updates += [gr.update() for _ in pairs[index]]
                        continue
                    item = used[index]
                    high_file = item.get("high") or ""
                    low_file = item.get("low") or ""
                    row = [gr.update(value=item["url"])]
                    row.append(gr.update(choices=["(None)", high_file], value=high_file)
                               if high_file else gr.update())
                    row.append(gr.update(value=item["strength"]))
                    row.append(gr.update(choices=["(None)", low_file], value=low_file)
                               if low_file else gr.update())
                    row.append(gr.update(value=item.get("low_strength", item["strength"])))
                    # A slot may have fewer than five components; keep them lined up.
                    updates += row[:len(pairs[index])]
                    updates += [gr.update() for _ in range(len(pairs[index]) - len(row))]

                message = f"**in the slots:** {', '.join(i['name'] for i in used)}"
                if any(not (i.get("high") or i.get("low")) for i in used):
                    message += ("  \nno file names were saved for some of them — press "
                                "**📥 Load files** in the slot to fill the High/Low boxes.")
                if spare:
                    message += (f"  \nno room for: {', '.join(i['name'] for i in spare)} "
                                f"(there are {len(pairs)} slots)")
                trigger_words = [i["trigger"] for i in used if i.get("trigger")]

                out = [message]
                if has_prompt:
                    out.append(gr.update(value=_with_triggers(prompt_text, trigger_words))
                               if triggers_on and trigger_words else gr.update())
                elif trigger_words:
                    out[0] += f"  \n**trigger words for the prompt:** {', '.join(trigger_words)}"
                return tuple(out + updates)

            add_button.click(_add_pairs, add_inputs, add_outputs)

        # ------------------------------------------- adding to the lora slots

        elif has_slots:
            add_inputs = [state, normal_labels, nsfw_labels, normal_pick, nsfw_pick, use_triggers]
            add_outputs = [status]
            if has_prompt:
                add_inputs.append(prompt_box)
                add_outputs.append(prompt_box)
            add_outputs = add_outputs + slots + scales

            def _add_slots(data, normal, nsfw, normal_chosen, nsfw_chosen, triggers_on,
                           prompt_text=""):
                data = _clean(data)
                chosen = (_picked(data["normal"], normal, normal_chosen)
                          + _picked(data["nsfw"], nsfw, nsfw_chosen))
                blank = [gr.update() for _ in slots] + [gr.update() for _ in scales]
                if not chosen:
                    return tuple(["nothing is ticked."]
                                 + ([gr.update()] if has_prompt else []) + blank)

                used = chosen[:len(slots)]
                spare = chosen[len(slots):]
                ref_updates = [
                    gr.update(value=used[i]["url"]) if i < len(used) else gr.update()
                    for i in range(len(slots))
                ]
                scale_updates = [
                    gr.update(value=used[i]["strength"]) if i < len(used) else gr.update()
                    for i in range(len(scales))
                ]

                message = f"**in the slots:** {', '.join(i['name'] for i in used)}"
                if spare:
                    message += (f"  \nno room for: {', '.join(i['name'] for i in spare)} "
                                f"(there are {len(slots)} slots)")
                trigger_words = [i["trigger"] for i in used if i.get("trigger")]

                out = [message]
                if has_prompt:
                    out.append(gr.update(value=_with_triggers(prompt_text, trigger_words))
                               if triggers_on and trigger_words else gr.update())
                elif trigger_words:
                    out[0] += f"  \n**trigger words for the prompt:** {', '.join(trigger_words)}"
                return tuple(out + ref_updates + scale_updates)

            add_button.click(_add_slots, add_inputs, add_outputs)

        # --------------------------------------------- adding to the lora box

        else:
            add_inputs = [state, normal_labels, nsfw_labels, normal_pick, nsfw_pick, use_triggers]
            add_outputs = [status]
            if has_box:
                add_inputs.append(lora_box)
                add_outputs.append(lora_box)
            if has_prompt:
                add_inputs.append(prompt_box)
                add_outputs.append(prompt_box)

            def _add_box(data, normal, nsfw, normal_chosen, nsfw_chosen, triggers_on, *boxes):
                data = _clean(data)
                chosen = (_picked(data["normal"], normal, normal_chosen)
                          + _picked(data["nsfw"], nsfw, nsfw_chosen))

                position = 0
                lora_text = boxes[position] if has_box else ""
                if has_box:
                    position += 1
                prompt_text = boxes[position] if has_prompt else ""

                if not chosen:
                    out = ["nothing is ticked."]
                    out += [gr.update()] * (int(has_box) + int(has_prompt))
                    return tuple(out) if len(out) > 1 else out[0]

                text = (lora_text or "").rstrip()
                added, skipped, trigger_words = [], [], []
                for item in chosen:
                    if item["url"] in text:
                        skipped.append(item["name"])
                    else:
                        text = f"{text}\n{item['url']} | {item['strength']:g}".strip()
                        added.append(item["name"])
                    if item.get("trigger"):
                        trigger_words.append(item["trigger"])

                message = ""
                if added:
                    message += f"**added:** {', '.join(added)}"
                if skipped:
                    message += f"  \nalready in the box: {', '.join(skipped)}"
                if trigger_words and not has_prompt:
                    message += f"  \n**trigger words for the prompt:** {', '.join(trigger_words)}"
                if not has_box:
                    lines = "\n".join(f"{i['url']} | {i['strength']:g}" for i in chosen)
                    message = f"copy these lines into the lora box:\n\n```\n{lines}\n```\n\n{message}"

                out = [message]
                if has_box:
                    out.append(gr.update(value=text))
                if has_prompt:
                    out.append(gr.update(value=_with_triggers(prompt_text, trigger_words))
                               if triggers_on and trigger_words else gr.update())
                return tuple(out) if len(out) > 1 else out[0]

            add_button.click(_add_box, add_inputs, add_outputs)

        try:
            from gradio.context import Context

            root = getattr(Context, "root_block", None)
            if root is not None:
                event = None
                for keyword in ("js", "_js"):
                    try:
                        event = root.load(None, None, [owner_box], **{keyword: _OWNER_JS})
                        break
                    except TypeError:
                        continue
                if event is not None and hasattr(event, "then"):
                    event.then(_refresh, [owner_box], refresh_targets)
                else:
                    root.load(_refresh, [owner_box], refresh_targets)
        except Exception:  # noqa: BLE001
            try:
                root.load(_refresh, [owner_box], refresh_targets)
            except Exception:  # noqa: BLE001
                pass

    return state