Spaces:
Running on Zero
Running on Zero
Show related CPU LoRA matches for short keywords
Browse files- app.py +41 -10
- lora_cpu_picker.py +54 -0
app.py
CHANGED
|
@@ -25,7 +25,7 @@ from PIL import Image, ImageDraw, ImageFilter
|
|
| 25 |
|
| 26 |
import gradio as gr
|
| 27 |
from prompt_assistant import assistant_choices, prepare_request, resolve_writer, writer_result
|
| 28 |
-
from lora_cpu_picker import rank_prompt_loras
|
| 29 |
from economy_prompt import (ECONOMY_SPACE, ECONOMY_TOKENS, ECONOMY_NOTE, is_economy,
|
| 30 |
economy_instruction, economy_prompt, economy_status)
|
| 31 |
from end_frame_color import match_end_frame_preview
|
|
@@ -7833,10 +7833,16 @@ def _wan_cpu_current_picks(state, values):
|
|
| 7833 |
|
| 7834 |
def _wan_cpu_picker_panel():
|
| 7835 |
return gr.CheckboxGroup(choices=[], value=[], visible=False, interactive=True,
|
| 7836 |
-
label="CPU-picked LoRAs", info="
|
| 7837 |
|
| 7838 |
|
| 7839 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7840 |
"""Find compatible matches for vacant slots using catalogue text and file metadata."""
|
| 7841 |
from concurrent.futures import ThreadPoolExecutor
|
| 7842 |
|
|
@@ -7885,6 +7891,8 @@ def _wan_cpu_lora_choices(prompt, library, adapter_values, ranked=None):
|
|
| 7885 |
|
| 7886 |
candidates = []
|
| 7887 |
ranked = rank_prompt_loras(prompt, _library_pool(library)) if ranked is None else ranked
|
|
|
|
|
|
|
| 7888 |
for item in ranked:
|
| 7889 |
candidate = describe(item)
|
| 7890 |
if candidate is not None:
|
|
@@ -7903,13 +7911,17 @@ def _wan_cpu_lora_choices(prompt, library, adapter_values, ranked=None):
|
|
| 7903 |
for (item, _, keys), infos in zip(candidates, candidate_info):
|
| 7904 |
slots = built_slots if item.get("builtin") else custom_slots
|
| 7905 |
sizes = [int(info.get("size") or 0) for info in infos]
|
| 7906 |
-
|
|
|
|
|
|
|
| 7907 |
or any(size > LORA_MAX_FILE_BYTES for size in sizes) or used_bytes + sum(sizes) > LORA_MAX_RUN_BYTES):
|
| 7908 |
continue
|
| 7909 |
choices.append((slots.pop(0), item))
|
|
|
|
|
|
|
| 7910 |
used_sources.update(keys)
|
| 7911 |
used_bytes += sum(sizes)
|
| 7912 |
-
if len(choices) >=
|
| 7913 |
break
|
| 7914 |
return choices
|
| 7915 |
|
|
@@ -10187,10 +10199,20 @@ with gr.Blocks(title="Wan 2.2 Studio | NEW AI Scene Planner · 64 Clips · 3 Aud
|
|
| 10187 |
prompt_basis = "\n".join(value for value in (idea_text, scene_basis) if value).strip() or prompt_basis
|
| 10188 |
values = list(adapter_values)
|
| 10189 |
updates = [gr.update() for _ in values]
|
| 10190 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10191 |
if not choices:
|
| 10192 |
-
|
| 10193 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10194 |
summary = _show_active_loras(library, state, *values)
|
| 10195 |
return [note, gr.update(), *updates[3:], *(gr.update() for _ in _TRIGGER_OUTPUTS), *summary]
|
| 10196 |
|
|
@@ -10208,13 +10230,22 @@ with gr.Blocks(title="Wan 2.2 Studio | NEW AI Scene Planner · 64 Clips · 3 Aud
|
|
| 10208 |
slot = (item["url"], item.get("high") or "(None)", row["high"],
|
| 10209 |
item.get("low") or "(None)", row["low"])
|
| 10210 |
refs = (slot[0], slot[1], slot[3])
|
| 10211 |
-
_write_cpu_lora_slot(values, updates, index, slot)
|
| 10212 |
key = _pick_key(item)
|
| 10213 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10214 |
if key not in keys:
|
| 10215 |
page.append(item)
|
| 10216 |
keys.add(key)
|
| 10217 |
state.update(page=page, cpu_picks=picks)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10218 |
note = f"Added {len(choices)} LoRA{'s' if len(choices) != 1 else ''} from your prompt."
|
| 10219 |
return [note, *_cpu_lora_result(prompt_text, sheet_text, library, state, values, updates)]
|
| 10220 |
|
|
|
|
| 25 |
|
| 26 |
import gradio as gr
|
| 27 |
from prompt_assistant import assistant_choices, prepare_request, resolve_writer, writer_result
|
| 28 |
+
from lora_cpu_picker import rank_prompt_loras, rank_wan_prompt_suggestions
|
| 29 |
from economy_prompt import (ECONOMY_SPACE, ECONOMY_TOKENS, ECONOMY_NOTE, is_economy,
|
| 30 |
economy_instruction, economy_prompt, economy_status)
|
| 31 |
from end_frame_color import match_end_frame_preview
|
|
|
|
| 7833 |
|
| 7834 |
def _wan_cpu_picker_panel():
|
| 7835 |
return gr.CheckboxGroup(choices=[], value=[], visible=False, interactive=True,
|
| 7836 |
+
label="CPU-picked LoRAs", info="Tick a suggestion to use it. Untick to remove it.")
|
| 7837 |
|
| 7838 |
|
| 7839 |
+
def _wan_cpu_model_key(item):
|
| 7840 |
+
page = str(item.get("page") or item.get("url") or "")
|
| 7841 |
+
match = re.match(r"https?://(?:www\.)?civitai\.(?:com|red|green|work)/models/(\d+)(?:[/?#]|$)", page, re.I)
|
| 7842 |
+
return ("civitai", match.group(1)) if match else None
|
| 7843 |
+
|
| 7844 |
+
|
| 7845 |
+
def _wan_cpu_lora_choices(prompt, library, adapter_values, ranked=None, limit=2):
|
| 7846 |
"""Find compatible matches for vacant slots using catalogue text and file metadata."""
|
| 7847 |
from concurrent.futures import ThreadPoolExecutor
|
| 7848 |
|
|
|
|
| 7891 |
|
| 7892 |
candidates = []
|
| 7893 |
ranked = rank_prompt_loras(prompt, _library_pool(library)) if ranked is None else ranked
|
| 7894 |
+
used_models = {key for item in ranked if (key := _wan_cpu_model_key(item))
|
| 7895 |
+
and any(str(item.get(field) or "").casefold() in used_refs for field in ("url", "high", "low"))}
|
| 7896 |
for item in ranked:
|
| 7897 |
candidate = describe(item)
|
| 7898 |
if candidate is not None:
|
|
|
|
| 7911 |
for (item, _, keys), infos in zip(candidates, candidate_info):
|
| 7912 |
slots = built_slots if item.get("builtin") else custom_slots
|
| 7913 |
sizes = [int(info.get("size") or 0) for info in infos]
|
| 7914 |
+
model_key = _wan_cpu_model_key(item)
|
| 7915 |
+
if (not slots or model_key in used_models or keys & used_sources
|
| 7916 |
+
or any(info.get("status") == "unavailable" for info in infos)
|
| 7917 |
or any(size > LORA_MAX_FILE_BYTES for size in sizes) or used_bytes + sum(sizes) > LORA_MAX_RUN_BYTES):
|
| 7918 |
continue
|
| 7919 |
choices.append((slots.pop(0), item))
|
| 7920 |
+
if model_key:
|
| 7921 |
+
used_models.add(model_key)
|
| 7922 |
used_sources.update(keys)
|
| 7923 |
used_bytes += sum(sizes)
|
| 7924 |
+
if len(choices) >= limit:
|
| 7925 |
break
|
| 7926 |
return choices
|
| 7927 |
|
|
|
|
| 10199 |
prompt_basis = "\n".join(value for value in (idea_text, scene_basis) if value).strip() or prompt_basis
|
| 10200 |
values = list(adapter_values)
|
| 10201 |
updates = [gr.update() for _ in values]
|
| 10202 |
+
pool = _library_pool(library) if prompt_basis.strip() else []
|
| 10203 |
+
strong = rank_prompt_loras(prompt_basis, pool)
|
| 10204 |
+
choices = _wan_cpu_lora_choices(prompt_basis, library, values, ranked=strong) if strong else []
|
| 10205 |
+
related = rank_wan_prompt_suggestions(prompt_basis, pool) if not choices else []
|
| 10206 |
+
suggestions_only = bool(related and not choices)
|
| 10207 |
+
if suggestions_only:
|
| 10208 |
+
choices = _wan_cpu_lora_choices(prompt_basis, library, values, ranked=related, limit=4)
|
| 10209 |
if not choices:
|
| 10210 |
+
if not prompt_basis.strip():
|
| 10211 |
+
note = "Write or paste a prompt first."
|
| 10212 |
+
elif strong or related:
|
| 10213 |
+
note = "Found related LoRAs, but none fit the available slots, file checks and size limits."
|
| 10214 |
+
else:
|
| 10215 |
+
note = "No clear LoRA match in the library for this prompt."
|
| 10216 |
summary = _show_active_loras(library, state, *values)
|
| 10217 |
return [note, gr.update(), *updates[3:], *(gr.update() for _ in _TRIGGER_OUTPUTS), *summary]
|
| 10218 |
|
|
|
|
| 10230 |
slot = (item["url"], item.get("high") or "(None)", row["high"],
|
| 10231 |
item.get("low") or "(None)", row["low"])
|
| 10232 |
refs = (slot[0], slot[1], slot[3])
|
|
|
|
| 10233 |
key = _pick_key(item)
|
| 10234 |
+
record = dict(slot=index, item=item, refs=_wan_lora_slot_refs(refs), enabled=not suggestions_only)
|
| 10235 |
+
picks[key] = record
|
| 10236 |
+
if suggestions_only:
|
| 10237 |
+
record["saved"] = slot
|
| 10238 |
+
continue
|
| 10239 |
+
_write_cpu_lora_slot(values, updates, index, slot)
|
| 10240 |
if key not in keys:
|
| 10241 |
page.append(item)
|
| 10242 |
keys.add(key)
|
| 10243 |
state.update(page=page, cpu_picks=picks)
|
| 10244 |
+
if suggestions_only:
|
| 10245 |
+
note = f"Found {len(choices)} related LoRA{'s' if len(choices) != 1 else ''}. Tick a suggestion to use it."
|
| 10246 |
+
trigger_updates = [gr.update() for _ in _TRIGGER_OUTPUTS]
|
| 10247 |
+
trigger_updates[2] = state
|
| 10248 |
+
return [note, gr.update(), *updates[3:], *trigger_updates, *_show_active_loras(library, state, *values)]
|
| 10249 |
note = f"Added {len(choices)} LoRA{'s' if len(choices) != 1 else ''} from your prompt."
|
| 10250 |
return [note, *_cpu_lora_result(prompt_text, sheet_text, library, state, values, updates)]
|
| 10251 |
|
lora_cpu_picker.py
CHANGED
|
@@ -120,3 +120,57 @@ def rank_prompt_loras(prompt, items):
|
|
| 120 |
ranked.append(((1, max(map(len, hits))), item))
|
| 121 |
ranked.sort(key=lambda entry: entry[0], reverse=True)
|
| 122 |
return [item for _, item in ranked]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
ranked.append(((1, max(map(len, hits))), item))
|
| 121 |
ranked.sort(key=lambda entry: entry[0], reverse=True)
|
| 122 |
return [item for _, item in ranked]
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
_WAN_BUST_ALIASES = {
|
| 126 |
+
"tit": "tits", "tits": "tits", "cici": "tits", "tsitsi": "tits", "цици": "tits",
|
| 127 |
+
"boob": "boobs", "boobs": "boobs",
|
| 128 |
+
"breast": "breasts", "breasts": "breasts", "гърда": "breasts", "гърди": "breasts",
|
| 129 |
+
"gardi": "breasts", "gurdi": "breasts", "gyrdi": "breasts",
|
| 130 |
+
"bust": "bust", "бюст": "bust", "byust": "bust", "biust": "bust",
|
| 131 |
+
}
|
| 132 |
+
_WAN_BUST_UNRELATED = frozenset(
|
| 133 |
+
"crotch crotchboobs quadboob triboob lick licking squeeze squeezing play suck sucking "
|
| 134 |
+
"grope groping fondle fondling grab grabbing motorboat motorboating blowjob handjob "
|
| 135 |
+
"sex penetration porn topless nude nipples fetish flashing stripping fuck fucking titfuck "
|
| 136 |
+
"titfucking titpounding pounding".split()
|
| 137 |
+
)
|
| 138 |
+
_WAN_BUST_FEATURES = frozenset(
|
| 139 |
+
"best big bounce bouncing expansion full huge large natural physics round shaking shape size "
|
| 140 |
+
"slider small torpedo".split()
|
| 141 |
+
)
|
| 142 |
+
_WAN_OTHER_BUST = re.compile(
|
| 143 |
+
r"\b(?:bust (?:through|out|open|up|down|of)|(?:bronze|marble|sculpted|stone|plaster) bust)\b"
|
| 144 |
+
)
|
| 145 |
+
_WAN_BIRD_TITS = re.compile(r"\b(?:great|blue|coal|crested|long tailed) tits?\b")
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
def rank_wan_prompt_suggestions(prompt, items):
|
| 149 |
+
"""Suggest title-related WAN adapters for an explicit broad term, without applying them."""
|
| 150 |
+
requested = set()
|
| 151 |
+
for clause in _positive_clauses(prompt):
|
| 152 |
+
clause = _WAN_BIRD_TITS.sub(" ", _WAN_OTHER_BUST.sub(" ", clause))
|
| 153 |
+
requested.update(_WAN_BUST_ALIASES[word] for word in _words(clause) if word in _WAN_BUST_ALIASES)
|
| 154 |
+
if not requested:
|
| 155 |
+
return []
|
| 156 |
+
|
| 157 |
+
ranked = []
|
| 158 |
+
for item in items:
|
| 159 |
+
if not isinstance(item, dict):
|
| 160 |
+
continue
|
| 161 |
+
name = str(item.get("name") or item.get("title") or "")
|
| 162 |
+
title, _, metadata = name.partition(" · ")
|
| 163 |
+
words = set(_words(title))
|
| 164 |
+
anatomy = {_WAN_BUST_ALIASES[word] for word in words if word in _WAN_BUST_ALIASES}
|
| 165 |
+
if not anatomy or words & _WAN_BUST_UNRELATED:
|
| 166 |
+
continue
|
| 167 |
+
# An anatomy word in a title is not enough when the rest names a different concept.
|
| 168 |
+
anatomy_title = all(word in _WAN_BUST_ALIASES for phrase in _title_phrases(item) for word in phrase)
|
| 169 |
+
if not anatomy_title and not words & _WAN_BUST_FEATURES:
|
| 170 |
+
continue
|
| 171 |
+
# Saved library titles may name both modes. The actual version's base model wins.
|
| 172 |
+
base_words = _words(item.get("base_model") or metadata.partition(" · ")[0])
|
| 173 |
+
i2v = "i2v" in (base_words or words)
|
| 174 |
+
ranked.append(((bool(anatomy & requested), i2v), item))
|
| 175 |
+
ranked.sort(key=lambda row: row[0], reverse=True)
|
| 176 |
+
return [item for _, item in ranked]
|