Update app.py
Browse files
app.py
CHANGED
|
@@ -148,16 +148,31 @@ def load_char_loras_from_repo():
|
|
| 148 |
|
| 149 |
# 🌟 拉取 trigger word 元数据(key = 完整仓库路径,如 moss/CN/xxx.safetensors)
|
| 150 |
lora_meta = {}
|
|
|
|
| 151 |
try:
|
| 152 |
r = requests.get(LORA_META_URL, timeout=10)
|
| 153 |
if r.ok:
|
| 154 |
lora_meta = r.json().get("items", {}) or {}
|
|
|
|
| 155 |
print(f"✅ 已加载 trigger 元数据: {len(lora_meta)} 条")
|
| 156 |
else:
|
| 157 |
print(f"⚠️ trigger 元数据拉取失败: HTTP {r.status_code}")
|
| 158 |
except Exception as e:
|
| 159 |
print(f"⚠️ trigger 元数据拉取失败: {e}")
|
| 160 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
try:
|
| 162 |
files = list_repo_files(repo_id=MODEL_CONFIG['char_lora_repo'], token=os.environ.get("HF_TOKEN"))
|
| 163 |
for engine, cfg in MODEL_CONFIG.get("models", {}).items():
|
|
@@ -174,19 +189,31 @@ def load_char_loras_from_repo():
|
|
| 174 |
webp_path = f.replace(".safetensors", ".webp")
|
| 175 |
webp_url = f"https://huggingface.co/{MODEL_CONFIG['char_lora_repo']}/resolve/main/{webp_path}"
|
| 176 |
|
| 177 |
-
# 🌟 合并 title / trigger
|
| 178 |
-
info =
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
title = (info.get("title") or "").strip() or filename.replace(".safetensors", "")
|
| 184 |
|
| 185 |
new_char_choices[engine][filename] = {
|
| 186 |
"path": f,
|
| 187 |
"webp": webp_url,
|
| 188 |
"title": title,
|
| 189 |
-
"trigger":
|
| 190 |
}
|
| 191 |
|
| 192 |
CHAR_LORA_CHOICES.clear()
|
|
|
|
| 148 |
|
| 149 |
# 🌟 拉取 trigger word 元数据(key = 完整仓库路径,如 moss/CN/xxx.safetensors)
|
| 150 |
lora_meta = {}
|
| 151 |
+
meta_ok = False
|
| 152 |
try:
|
| 153 |
r = requests.get(LORA_META_URL, timeout=10)
|
| 154 |
if r.ok:
|
| 155 |
lora_meta = r.json().get("items", {}) or {}
|
| 156 |
+
meta_ok = True
|
| 157 |
print(f"✅ 已加载 trigger 元数据: {len(lora_meta)} 条")
|
| 158 |
else:
|
| 159 |
print(f"⚠️ trigger 元数据拉取失败: HTTP {r.status_code}")
|
| 160 |
except Exception as e:
|
| 161 |
print(f"⚠️ trigger 元数据拉取失败: {e}")
|
| 162 |
|
| 163 |
+
# 多形态索引:仓库全路径 / 去前缀路径 / 纯文件名,任一命中即可
|
| 164 |
+
meta_index = {}
|
| 165 |
+
for k, v in (lora_meta or {}).items():
|
| 166 |
+
meta_index[k] = v
|
| 167 |
+
meta_index[k.lstrip("./")] = v
|
| 168 |
+
meta_index[os.path.basename(k)] = v
|
| 169 |
+
|
| 170 |
+
# 上一次成功扫描的结果(元数据拉取失败时兜底,避免全站触发词瞬间消失)
|
| 171 |
+
old_flat = {}
|
| 172 |
+
for _eng, _m in CHAR_LORA_CHOICES.items():
|
| 173 |
+
for _fn, _v in (_m or {}).items():
|
| 174 |
+
old_flat[_fn] = _v
|
| 175 |
+
|
| 176 |
try:
|
| 177 |
files = list_repo_files(repo_id=MODEL_CONFIG['char_lora_repo'], token=os.environ.get("HF_TOKEN"))
|
| 178 |
for engine, cfg in MODEL_CONFIG.get("models", {}).items():
|
|
|
|
| 189 |
webp_path = f.replace(".safetensors", ".webp")
|
| 190 |
webp_url = f"https://huggingface.co/{MODEL_CONFIG['char_lora_repo']}/resolve/main/{webp_path}"
|
| 191 |
|
| 192 |
+
# 🌟 合并 title / trigger
|
| 193 |
+
info = (
|
| 194 |
+
meta_index.get(f)
|
| 195 |
+
or meta_index.get(relative_path)
|
| 196 |
+
or meta_index.get(filename)
|
| 197 |
+
or {}
|
| 198 |
+
)
|
| 199 |
+
if not info and not meta_ok:
|
| 200 |
+
info = old_flat.get(filename, {}) # 元数据失败 → 复用上次缓存
|
| 201 |
+
|
| 202 |
+
# trigger:数组原样透传(只 trim / 去空),字符串原样透传
|
| 203 |
+
# ⚠️ 绝不 join 成 ", ",也绝不在后端按逗号拆分 —— 拆分只由前端 normalizeTriggers 负责
|
| 204 |
+
raw_trigger = info.get("trigger", "")
|
| 205 |
+
if isinstance(raw_trigger, (list, tuple)):
|
| 206 |
+
triggers = [str(t).strip() for t in raw_trigger if str(t).strip()]
|
| 207 |
+
else:
|
| 208 |
+
triggers = str(raw_trigger or "").strip()
|
| 209 |
+
|
| 210 |
title = (info.get("title") or "").strip() or filename.replace(".safetensors", "")
|
| 211 |
|
| 212 |
new_char_choices[engine][filename] = {
|
| 213 |
"path": f,
|
| 214 |
"webp": webp_url,
|
| 215 |
"title": title,
|
| 216 |
+
"trigger": triggers, # list 或 str,前端都能吃
|
| 217 |
}
|
| 218 |
|
| 219 |
CHAR_LORA_CHOICES.clear()
|