Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
Auto-sync from GitHub Actions
Browse files- core/counter.py +1 -0
- mcp_server.py +56 -3
- ui_nicegui.py +12 -0
core/counter.py
CHANGED
|
@@ -365,6 +365,7 @@ async def add_bad_case(query: str, platform: str = '', settings: dict | None = N
|
|
| 365 |
entry['platform'] = platform
|
| 366 |
if settings:
|
| 367 |
entry['settings'] = settings
|
|
|
|
| 368 |
_memory_bad_cases.insert(0, entry)
|
| 369 |
if len(_memory_bad_cases) > MAX_BAD_CASES:
|
| 370 |
_memory_bad_cases[:] = _memory_bad_cases[:MAX_BAD_CASES]
|
|
|
|
| 365 |
entry['platform'] = platform
|
| 366 |
if settings:
|
| 367 |
entry['settings'] = settings
|
| 368 |
+
print(f'[Counter] bad_case 上报: {json.dumps(entry, ensure_ascii=False)}')
|
| 369 |
_memory_bad_cases.insert(0, entry)
|
| 370 |
if len(_memory_bad_cases) > MAX_BAD_CASES:
|
| 371 |
_memory_bad_cases[:] = _memory_bad_cases[:MAX_BAD_CASES]
|
mcp_server.py
CHANGED
|
@@ -276,10 +276,52 @@ JSON array sorted by aggregated NPMI score (descending). Each result:
|
|
| 276 |
- wiki: only if include_wiki=True
|
| 277 |
"""
|
| 278 |
tagger = await DanbooruTagger.get_instance()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 279 |
results = await asyncio.to_thread(
|
| 280 |
tagger.get_related,
|
| 281 |
-
|
| 282 |
-
set(
|
| 283 |
limit,
|
| 284 |
show_nsfw,
|
| 285 |
)
|
|
@@ -303,4 +345,15 @@ JSON array sorted by aggregated NPMI score (descending). Each result:
|
|
| 303 |
item["wiki"] = r.wiki
|
| 304 |
output.append(item)
|
| 305 |
|
| 306 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 276 |
- wiki: only if include_wiki=True
|
| 277 |
"""
|
| 278 |
tagger = await DanbooruTagger.get_instance()
|
| 279 |
+
|
| 280 |
+
# ── 检查标签是否存在,不存在则尝试 search_tags 纠错 ──────────────────
|
| 281 |
+
valid_tags = []
|
| 282 |
+
invalid_tags = []
|
| 283 |
+
for t in tags:
|
| 284 |
+
if t in tagger._name_to_idx:
|
| 285 |
+
valid_tags.append(t)
|
| 286 |
+
else:
|
| 287 |
+
invalid_tags.append(t)
|
| 288 |
+
|
| 289 |
+
corrections = {}
|
| 290 |
+
if invalid_tags:
|
| 291 |
+
for bad_tag in invalid_tags:
|
| 292 |
+
try:
|
| 293 |
+
req = SearchRequest(
|
| 294 |
+
query=bad_tag,
|
| 295 |
+
top_k=5,
|
| 296 |
+
limit=5,
|
| 297 |
+
popularity_weight=0.15,
|
| 298 |
+
use_segmentation=False,
|
| 299 |
+
target_layers=['英文']
|
| 300 |
+
)
|
| 301 |
+
resp = await asyncio.to_thread(tagger.search, req)
|
| 302 |
+
if resp.results:
|
| 303 |
+
corrections[bad_tag] = resp.results[0].tag
|
| 304 |
+
except Exception:
|
| 305 |
+
pass
|
| 306 |
+
|
| 307 |
+
if not valid_tags and not corrections:
|
| 308 |
+
return json.dumps({
|
| 309 |
+
"error": "所有传入的标签均不存在于标签表中",
|
| 310 |
+
"invalid_tags": invalid_tags,
|
| 311 |
+
}, ensure_ascii=False, indent=2)
|
| 312 |
+
|
| 313 |
+
# 用纠错后的标签替换无效标签
|
| 314 |
+
corrected_tags = []
|
| 315 |
+
for t in tags:
|
| 316 |
+
if t in valid_tags:
|
| 317 |
+
corrected_tags.append(t)
|
| 318 |
+
elif t in corrections:
|
| 319 |
+
corrected_tags.append(corrections[t])
|
| 320 |
+
|
| 321 |
results = await asyncio.to_thread(
|
| 322 |
tagger.get_related,
|
| 323 |
+
corrected_tags,
|
| 324 |
+
set(corrected_tags),
|
| 325 |
limit,
|
| 326 |
show_nsfw,
|
| 327 |
)
|
|
|
|
| 345 |
item["wiki"] = r.wiki
|
| 346 |
output.append(item)
|
| 347 |
|
| 348 |
+
payload = output
|
| 349 |
+
if corrections:
|
| 350 |
+
correction_notes = [
|
| 351 |
+
f"{bad} → {good}" for bad, good in corrections.items()
|
| 352 |
+
]
|
| 353 |
+
payload = {
|
| 354 |
+
"correction_note": "标签拼写错误,已经纠错: " + ", ".join(correction_notes),
|
| 355 |
+
"corrections": corrections,
|
| 356 |
+
"results": output,
|
| 357 |
+
}
|
| 358 |
+
|
| 359 |
+
return json.dumps(payload, ensure_ascii=False, indent=2)
|
ui_nicegui.py
CHANGED
|
@@ -1040,6 +1040,18 @@ class DanbooruSearchUI:
|
|
| 1040 |
if not query:
|
| 1041 |
return
|
| 1042 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1043 |
# 搜索前保存配置
|
| 1044 |
self._save_config()
|
| 1045 |
|
|
|
|
| 1040 |
if not query:
|
| 1041 |
return
|
| 1042 |
|
| 1043 |
+
# 搜索前校验数值参数
|
| 1044 |
+
_err_fields = []
|
| 1045 |
+
if self.input_top_k and (self.input_top_k.value is None or str(self.input_top_k.value).strip() == ''):
|
| 1046 |
+
_err_fields.append('Top K')
|
| 1047 |
+
if self.input_limit and (self.input_limit.value is None or str(self.input_limit.value).strip() == ''):
|
| 1048 |
+
_err_fields.append('返回数量')
|
| 1049 |
+
if self.input_weight and (self.input_weight.value is None or str(self.input_weight.value).strip() == ''):
|
| 1050 |
+
_err_fields.append('热度权重')
|
| 1051 |
+
if _err_fields:
|
| 1052 |
+
ui.notify(f'请填写:{"、".join(_err_fields)}', type='negative', timeout=3000)
|
| 1053 |
+
return
|
| 1054 |
+
|
| 1055 |
# 搜索前保存配置
|
| 1056 |
self._save_config()
|
| 1057 |
|