cometa21 commited on
Commit
09e67c2
·
verified ·
1 Parent(s): 482fb9a

Create lora_loader.py

Browse files
Files changed (1) hide show
  1. lora_loader.py +296 -0
lora_loader.py ADDED
@@ -0,0 +1,296 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ LoRA Loader for WAN 2.2 I2V - multi-repo.
3
+ Baseline: lkzd7/WAN2.2_LoraSet_NSFW. Plus curated I2V act/helper LoRAs from
4
+ Lythiga/WAN2.2_I2V_Lora, rolechar/rc_wan2.2_i2v_loraset_nsfw_private,
5
+ lopi999/Wan2.2-I2V_General-NSFW-LoRA. T2V / lightning / furry / dance / male-char filtered out.
6
+ """
7
+ import os
8
+ import urllib.parse
9
+ import re
10
+ from huggingface_hub import hf_hub_download, list_repo_files
11
+
12
+ LORA_REPO = "lkzd7/WAN2.2_LoraSet_NSFW"
13
+ HF_TOKEN = os.environ.get("HF_TOKEN")
14
+
15
+ PINNED_REVISIONS = {
16
+ "lkzd7/WAN2.2_LoraSet_NSFW": "ccfa867ce4b1",
17
+ "Lythiga/WAN2.2_I2V_Lora": "67ef542e6faa",
18
+ "rolechar/rc_wan2.2_i2v_loraset_nsfw_private": "4855a2f216ac",
19
+ "lopi999/Wan2.2-I2V_General-NSFW-LoRA": "aeef17d7fa51",
20
+ }
21
+
22
+ LORA_FILES = [
23
+ "Blink_Squatting_Cowgirl_Position_I2V_HIGH.safetensors",
24
+ "Blink_Squatting_Cowgirl_Position_I2V_LOW.safetensors",
25
+ "PENISLORA_22_i2v_HIGH_e320.safetensors",
26
+ "PENISLORA_22_i2v_LOW_e496.safetensors",
27
+ "Pornmaster_wan 2.2_14b_I2V_bukkake_v1.4_high_noise.safetensors",
28
+ "Pornmaster_wan 2.2_14b_I2V_bukkake_v1.4_low_noise.safetensors",
29
+ "W22_Multiscene_Photoshoot_Softcore_i2v_HN.safetensors",
30
+ "W22_Multiscene_Photoshoot_Softcore_i2v_LN.safetensors",
31
+ "WAN-2.2-I2V-Double-Blowjob-HIGH-v1.safetensors",
32
+ "WAN-2.2-I2V-Double-Blowjob-LOW-v1.safetensors",
33
+ "WAN-2.2-I2V-HandjobBlowjobCombo-HIGH-v1.safetensors",
34
+ "WAN-2.2-I2V-HandjobBlowjobCombo-LOW-v1.safetensors",
35
+ "WAN-2.2-I2V-SensualTeasingBlowjob-HIGH-v1.safetensors",
36
+ "WAN-2.2-I2V-SensualTeasingBlowjob-LOW-v1.safetensors",
37
+ "iGOON_Blink_Blowjob_I2V_HIGH.safetensors",
38
+ "iGOON_Blink_Blowjob_I2V_LOW.safetensors",
39
+ "iGoon - Blink_Front_Doggystyle_I2V_HIGH.safetensors",
40
+ "iGoon - Blink_Front_Doggystyle_I2V_LOW.safetensors",
41
+ "iGoon - Blink_Missionary_I2V_HIGH.safetensors",
42
+ "iGoon - Blink_Missionary_I2V_LOW v2.safetensors",
43
+ "iGoon - Blink_Missionary_I2V_LOW.safetensors",
44
+ "iGoon%20-%20Blink_Back_Doggystyle_HIGH.safetensors",
45
+ "iGoon%20-%20Blink_Back_Doggystyle_LOW.safetensors",
46
+ "iGoon%20-%20Blink_Facial_I2V_HIGH.safetensors",
47
+ "iGoon%20-%20Blink_Facial_I2V_LOW.safetensors",
48
+ "iGoon_Blink_Missionary_I2V_HIGH v2.safetensors",
49
+ "iGoon_Blink_Titjob_I2V_HIGH.safetensors",
50
+ "iGoon_Blink_Titjob_I2V_LOW.safetensors",
51
+ "lips-bj_high_noise.safetensors",
52
+ "lips-bj_low_noise.safetensors",
53
+ "mql_casting_sex_doggy_kneel_diagonally_behind_vagina_wan22_i2v_v1_high_noise.safetensors",
54
+ "mql_casting_sex_doggy_kneel_diagonally_behind_vagina_wan22_i2v_v1_low_noise.safetensors",
55
+ "mql_casting_sex_reverse_cowgirl_lie_front_vagina_wan22_i2v_v1_high_noise.safetensors",
56
+ "mql_casting_sex_reverse_cowgirl_lie_front_vagina_wan22_i2v_v1_low_noise.safetensors",
57
+ "mql_casting_sex_spoon_wan22_i2v_v1_high_noise.safetensors",
58
+ "mql_casting_sex_spoon_wan22_i2v_v1_low_noise.safetensors",
59
+ "mql_massage_tits_wan22_i2v_v1_high_noise.safetensors",
60
+ "mql_massage_tits_wan22_i2v_v1_low_noise.safetensors",
61
+ "mql_panties_aside_wan22_i2v_v1_high_noise.safetensors",
62
+ "mql_panties_aside_wan22_i2v_v1_low_noise.safetensors",
63
+ "sfbehind_v2.1_high_noise.safetensors",
64
+ "sfbehind_v2.1_low_noise.safetensors",
65
+ "sid3l3g_transition_v2.0_H.safetensors",
66
+ "sid3l3g_transition_v2.0_L.safetensors",
67
+ "wan2.2_i2v_high_ulitmate_pussy_asshole.safetensors",
68
+ "wan2.2_i2v_low_ulitmate_pussy_asshole.safetensors",
69
+ "wan22-mouthfull-140epoc-high-k3nk.safetensors",
70
+ "wan22-mouthfull-152epoc-low-k3nk.safetensors",
71
+ ]
72
+
73
+ LORA_PAIRS = {}
74
+ for f in LORA_FILES:
75
+ name = urllib.parse.unquote(f).replace(".safetensors", "")
76
+ is_high = bool(re.search(r'(high|HN|_H\b)', name, re.IGNORECASE))
77
+ is_low = bool(re.search(r'(low|LN|_L\b)', name, re.IGNORECASE))
78
+ group = re.sub(r'[\s_-]*(high|low|noise|HN|LN)([\s_-]*noise)?[\s_-]*(v?\d+(\.\d+)?)?\s*$', '', name, flags=re.IGNORECASE).strip()
79
+ group = re.sub(r'[\s_]+$', '', group)
80
+ LORA_PAIRS.setdefault(group, {"HIGH": None, "LOW": None})
81
+ if is_high:
82
+ LORA_PAIRS[group]["HIGH"] = (LORA_REPO, f)
83
+ elif is_low:
84
+ LORA_PAIRS[group]["LOW"] = (LORA_REPO, f)
85
+
86
+ L = "Lythiga/WAN2.2_I2V_Lora"
87
+ R = "rolechar/rc_wan2.2_i2v_loraset_nsfw_private"
88
+ P = "lopi999/Wan2.2-I2V_General-NSFW-LoRA"
89
+
90
+ EXTRA = {
91
+ "Ultimate Deepthroat": (L, "wan22-ultimatedeepthroat-i2v-102epoc-high-k3nk.safetensors", "wan22-ultimatedeepthroat-I2V-101epoc-low-k3nk.safetensors"),
92
+ "BBC Deepthroat": (L, "wan22-bbcdeepthroat-115epoc-high-k3nk.safetensors", "wan22-bbcdeepthroat-155epoc-low-720-k3nk.safetensors"),
93
+ "Throat V2": (L, "Wan22_ThroatV2_High.safetensors", "Wan22_ThroatV2_Low.safetensors"),
94
+ "JFJ Deepthroat": (L, "jfj-deepthroat-W22-I2V-HN.safetensors", None),
95
+ "BBC Blowjob Extreme": (L, "BBC Blowjobs Extreme_high_noise.safetensors", "BBC Blowjobs Extreme_low_noise.safetensors"),
96
+ "POV Blowjob": (L, "Pov_Blowjob_Wan2.2_high_I2V_v1.0.safetensors", None),
97
+ "Cunnilingus": (L, "wan22-cunilingus-I2V-106epoc-high.safetensors", "wan22-cunilingus-I2V-72epoc-low.safetensors"),
98
+ "Reverse Cowgirl v2": (L, "wan22.r3v3rs3_c0wg1rl-14b-High-i2v_e70.safetensors", "wan22.r3v3rs3_c0wg1rl-14b-Low-i2v_e70.safetensors"),
99
+ "Doggy Front View v2": (L, "doggy_style_sex_front_view_Wan2.2_I2V_high_v1.0.safetensors", "doggy_style_sex_front_view_Wan2.2_I2V_Low_v1.0.safetensors"),
100
+ "Fucked From Behind": (L, "derpfuckedfrombehind_000002125_high_noise.safetensors", "derpfuckedfrombehind_000002125_low_noise.safetensors"),
101
+ "Prone Bone": (L, "Pronebone_high_noise.safetensors", "Pronebone_low_noise.safetensors"),
102
+ "Reverse Suspended Congress": (L, "reverse_suspended_congress_I2V_high.safetensors", "reverse_suspended_congress_I2V_low.safetensors"),
103
+ "POV Missionary": (L, "wan2.2_i2v_highnoise_pov_missionary_v1.0.safetensors", "wan2.2_i2v_lownoise_pov_missionary_v1.0.safetensors"),
104
+ "Mating Press": (L, "mating_press_high.safetensors", "mating_press_low.safetensors"),
105
+ "Standing Upright Sex": (L, "Upright_sex_Wan2.2_14B_TI2V_HIGH_v1.0.safetensors", "Upright_sex_Wan2.2_14B_TI2V_LOW_v1.0.safetensors"),
106
+ "Oral Insertion": (L, "wan2.2-i2v-high-oral-insertion-v1.0.safetensors", "wan2.2-i2v-low-oral-insertion-v1.0.safetensors"),
107
+ "Handjob": (L, "WAN-2.2-I2V-Handjob-HIGH-v1.safetensors", "WAN-2.2-I2V-Handjob-LOW-v1.safetensors"),
108
+ "Balls Sucking": (L, "WAN-2.2-I2V_Balls_sucking_High_noise.safetensors", "WAN-2.2-I2V_Balls_sucking_Low_noise.safetensors"),
109
+ "Fingering": (R, "Sensual_fingering_v1_high_noise.safetensors", "Sensual_fingering_v1_low_noise.safetensors"),
110
+ "Facesplash Cumshot": (L, "wan22-f4c3spl4sh-100epoc-high-k3nk.safetensors", "wan22-f4c3spl4sh-154epoc-low-k3nk.safetensors"),
111
+ "Creampie CRM": (L, "Creampie CRM-FULL-EPOCH-80-HIGH.safetensors", "Creampie CRM-FULL-EPOCH-80-LOW.safetensors"),
112
+ "Pornmaster Creampie": (L, "Pornmaster_wan 2.2_14b_I2V_Creampie_v1_high_noise.safetensors", "Pornmaster_wan 2.2_14b_I2V_Creampie_v1_low_noise.safetensors"),
113
+ "Cum (generic)": (L, "Wan22_Cum_high_noise_1.V1.safetensors", "Wan22_Cum_low_noise_1.V1.safetensors"),
114
+ "French Kiss": (L, "WAN2.2-FrenchKiss_HighNoise.safetensors", "WAN2.2-FrenchKiss_LowNoise.safetensors"),
115
+ "Twerking": (L, "Twerking_I2V_high_noise.safetensors", "Twerking_I2V_low_noise.safetensors"),
116
+ "Ahegao Drooling": (R, "middlefinger_drooling_ahegao_high.safetensors", "middlefinger_drooling_ahegao_low.safetensors"),
117
+ "General NSFW Booster": (P, "NSFW-22-H-e8.safetensors", "NSFW-22-L-e8.safetensors"),
118
+ "Penetration Insert": (L, "PenInsert_high_noise.safetensors", "PenInsert_low_noise.safetensors"),
119
+ "Biggest Cock (size)": (L, "WAN-2.2-I2V_BiggestCock_high_noise_V1.safetensors", "WAN-2.2-I2V_BiggestCock_low_noise_V1.safetensors"),
120
+ "Doggy Slider": (L, "I2V_doggyslider_high.safetensors", "I2V_doggyslider_low.safetensors"),
121
+ "Pussy Helper": (R, "wan2.2_i2v_ulitmate_pussy_helper_high.safetensors", "wan2.2_i2v_ulitmate_pussy_helper_low.safetensors"),
122
+ "FOV Slider (camera)": (L, "wan2.2-i2v-high-sex-fov-slider-v1.0.safetensors", "wan2.2-i2v-low-sex-fov-slider-v1.0.safetensors"),
123
+ "Smashcut (camera)": (R, "wan2.2-i2v-sex-smashcut-v1.0-high.safetensors", "wan2.2-i2v-sex-smashcut-v1.0-low.safetensors"),
124
+ }
125
+ for label, (repo, hi, lo) in EXTRA.items():
126
+ LORA_PAIRS.setdefault(label, {"HIGH": None, "LOW": None})
127
+ if hi:
128
+ LORA_PAIRS[label]["HIGH"] = (repo, hi)
129
+ if lo:
130
+ LORA_PAIRS[label]["LOW"] = (repo, lo)
131
+
132
+
133
+ def get_lora_choices():
134
+ choices = ["(None)"]
135
+ for group in sorted(LORA_PAIRS.keys()):
136
+ p = LORA_PAIRS[group]
137
+ if p["HIGH"] and p["LOW"]:
138
+ choices.append(group)
139
+ elif p["HIGH"]:
140
+ choices.append(f"{group} (HIGH only)")
141
+ elif p["LOW"]:
142
+ choices.append(f"{group} (LOW only)")
143
+ return choices
144
+
145
+
146
+ def parse_repo_link(link: str):
147
+ if not link:
148
+ return None
149
+ link = link.strip().strip('"').strip("'")
150
+ m = re.match(r"https?://huggingface\.co/(?:models/)?([^/]+)/([^/]+)", link)
151
+ if m:
152
+ return f"{m.group(1)}/{m.group(2)}"
153
+ if not link.startswith("http") and link.count("/") == 1:
154
+ return link
155
+ return None
156
+
157
+
158
+ def parse_hf_file_link(link: str):
159
+ if not link:
160
+ return None, None
161
+ link = link.strip().strip('"').strip("'")
162
+ m = re.match(
163
+ r"https?://huggingface\.co/([^/]+)/([^/]+)/(?:resolve|blob)/[^/]+/(.+)",
164
+ link,
165
+ )
166
+ if m:
167
+ return f"{m.group(1)}/{m.group(2)}", urllib.parse.unquote(m.group(3).split("?")[0])
168
+ if not link.startswith("http") and link.count("/") >= 2:
169
+ parts = link.split("/")
170
+ return f"{parts[0]}/{parts[1]}", "/".join(parts[2:])
171
+ return None, None
172
+
173
+
174
+ def list_loras_from_repo(repo_link: str):
175
+ repo_id = parse_repo_link(repo_link)
176
+ if not repo_id:
177
+ return ["(None)"]
178
+ try:
179
+ files = list_repo_files(repo_id, token=HF_TOKEN, revision=PINNED_REVISIONS.get(repo_id))
180
+ safes = sorted([f for f in files if f.lower().endswith(".safetensors")])
181
+ return ["(None)"] + safes if safes else ["(None)"]
182
+ except Exception as e:
183
+ print(f"list_loras_from_repo error: {e}")
184
+ return ["(None)"]
185
+
186
+
187
+ def download_lora(group_name):
188
+ if not group_name:
189
+ return None, None
190
+ clean_name = re.sub(r'\s*$$HIGH only$$|\s*$$LOW only$$', '', group_name)
191
+ if clean_name not in LORA_PAIRS:
192
+ return None, None
193
+ pair = LORA_PAIRS[clean_name]
194
+ high_path, low_path = None, None
195
+ if pair["HIGH"]:
196
+ repo, fn = pair["HIGH"]
197
+ high_path = hf_hub_download(repo, fn, token=HF_TOKEN, revision=PINNED_REVISIONS.get(repo))
198
+ if pair["LOW"]:
199
+ repo, fn = pair["LOW"]
200
+ low_path = hf_hub_download(repo, fn, token=HF_TOKEN, revision=PINNED_REVISIONS.get(repo))
201
+ return high_path, low_path
202
+
203
+
204
+ def download_repo_file(repo_link: str, filename: str):
205
+ if not filename or filename == "(None)":
206
+ return None
207
+ repo_from_file, weight = parse_hf_file_link(filename)
208
+ if repo_from_file and weight:
209
+ return hf_hub_download(
210
+ repo_from_file, weight, token=HF_TOKEN,
211
+ revision=PINNED_REVISIONS.get(repo_from_file),
212
+ )
213
+ repo_id = parse_repo_link(repo_link)
214
+ if not repo_id:
215
+ raise ValueError(f"Invalid repo/file: repo={repo_link}, file={filename}")
216
+ return hf_hub_download(
217
+ repo_id, filename, token=HF_TOKEN,
218
+ revision=PINNED_REVISIONS.get(repo_id),
219
+ )
220
+
221
+
222
+ def _load_path(pipe, path, adapter_name, into_t2=False):
223
+ kwargs = {"adapter_name": adapter_name}
224
+ if into_t2:
225
+ kwargs["load_into_transformer_2"] = True
226
+ pipe.load_lora_weights(path, **kwargs)
227
+ return adapter_name
228
+
229
+
230
+ def load_lora_to_pipe(pipe, group_name, adapter_name="lora", high_scale=1.0, low_scale=1.0):
231
+ if not group_name or group_name == "(None)":
232
+ return [], []
233
+ high_path, low_path = download_lora(group_name)
234
+ names, weights = [], []
235
+ if high_path:
236
+ n = f"{adapter_name}_high"
237
+ _load_path(pipe, high_path, n, into_t2=False)
238
+ names.append(n)
239
+ weights.append(float(high_scale))
240
+ if low_path:
241
+ n = f"{adapter_name}_low"
242
+ _load_path(pipe, low_path, n, into_t2=True)
243
+ names.append(n)
244
+ weights.append(float(low_scale))
245
+ if names:
246
+ print(f"Loaded preset LoRA: {group_name} scales={weights}")
247
+ return names, weights
248
+
249
+
250
+ def load_lora_slots(pipe, slots):
251
+ all_names, all_weights = [], []
252
+ for idx, slot in enumerate(slots or []):
253
+ if not slot:
254
+ continue
255
+ preset = slot.get("preset")
256
+ if preset and preset != "(None)":
257
+ names, weights = load_lora_to_pipe(
258
+ pipe, preset, adapter_name=f"lora_{idx}",
259
+ high_scale=slot.get("high_scale", 1.0),
260
+ low_scale=slot.get("low_scale", 1.0),
261
+ )
262
+ all_names.extend(names)
263
+ all_weights.extend(weights)
264
+ continue
265
+ repo = slot.get("repo")
266
+ fh = slot.get("file_high")
267
+ fl = slot.get("file_low")
268
+ hs = float(slot.get("high_scale", 1.0) or 1.0)
269
+ ls = float(slot.get("low_scale", 1.0) or 1.0)
270
+ if fh and fh != "(None)":
271
+ path = download_repo_file(repo, fh)
272
+ n = f"link_{idx}_high"
273
+ _load_path(pipe, path, n, into_t2=False)
274
+ all_names.append(n)
275
+ all_weights.append(hs)
276
+ print(f"Loaded link HIGH: {fh} scale={hs}")
277
+ if fl and fl != "(None)":
278
+ path = download_repo_file(repo, fl)
279
+ n = f"link_{idx}_low"
280
+ _load_path(pipe, path, n, into_t2=True)
281
+ all_names.append(n)
282
+ all_weights.append(ls)
283
+ print(f"Loaded link LOW: {fl} scale={ls}")
284
+ if all_names:
285
+ pipe.set_adapters(all_names, adapter_weights=all_weights)
286
+ print(f"Active adapters: {list(zip(all_names, all_weights))}")
287
+ return True
288
+ return False
289
+
290
+
291
+ def unload_lora(pipe):
292
+ try:
293
+ pipe.unload_lora_weights()
294
+ except Exception:
295
+ pass
296
+