Spaces:
Running
Running
Upload cli.py with huggingface_hub
Browse files
cli.py
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
cli.py
|
| 3 |
+
โโโโโโ
|
| 4 |
+
ๅฝไปค่ก้้
ๅฑ๏ผๅฏ้๏ผใ
|
| 5 |
+
|
| 6 |
+
ๅญๅฝไปค๏ผ
|
| 7 |
+
search ่ฏญไนๆ็ดขๆ ็ญพ
|
| 8 |
+
related ๅบไบๅ
ฑ็ฐ่กจๆฅๅ
ณ่ๆจ่
|
| 9 |
+
|
| 10 |
+
็จๆณ๏ผ
|
| 11 |
+
python cli.py search "็ฝ่ฒๆฐดๆๆ็ๅฅณๅญฉ" --limit 10 --no-nsfw
|
| 12 |
+
python cli.py related "white_serafuku,sailor_collar" --limit 20
|
| 13 |
+
python cli.py related "white_serafuku,sailor_collar" --no-nsfw --show-sources
|
| 14 |
+
"""
|
| 15 |
+
|
| 16 |
+
import argparse
|
| 17 |
+
import asyncio
|
| 18 |
+
|
| 19 |
+
from core.engine import DanbooruTagger
|
| 20 |
+
from core.models import SearchRequest
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
# โโ search โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 24 |
+
|
| 25 |
+
async def cmd_search(args):
|
| 26 |
+
tagger = await DanbooruTagger.get_instance()
|
| 27 |
+
request = SearchRequest(
|
| 28 |
+
query=args.query,
|
| 29 |
+
top_k=args.top_k,
|
| 30 |
+
limit=args.limit,
|
| 31 |
+
popularity_weight=args.weight,
|
| 32 |
+
show_nsfw=not args.no_nsfw,
|
| 33 |
+
use_segmentation=not args.no_seg,
|
| 34 |
+
)
|
| 35 |
+
resp = await asyncio.to_thread(tagger.search, request)
|
| 36 |
+
|
| 37 |
+
print(f"\n{'='*60}")
|
| 38 |
+
print(f"ๆฅ่ฏข๏ผ{args.query} | ๅ
ฑ {len(resp.results)} ๆก็ปๆ")
|
| 39 |
+
print(f"{'='*60}")
|
| 40 |
+
print(f"ๆจ่ Prompt๏ผ\n {resp.tags_sfw if args.no_nsfw else resp.tags_all}\n")
|
| 41 |
+
|
| 42 |
+
for r in resp.results:
|
| 43 |
+
nsfw_mark = "๐ด" if r.nsfw == '1' else "๐ข"
|
| 44 |
+
print(f" {nsfw_mark} [{r.final_score:.3f}] {r.tag:<30} {r.cn_name[:20]:<20} {r.category}")
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
# โโ related โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 48 |
+
|
| 49 |
+
async def cmd_related(args):
|
| 50 |
+
seed_tags = [t.strip() for t in args.tags.split(',') if t.strip()]
|
| 51 |
+
if not seed_tags:
|
| 52 |
+
print("้่ฏฏ๏ผ่ฏทๆไพ่ณๅฐไธไธช็งๅญๆ ็ญพ๏ผๅคไธชๆ ็ญพไปฅ้ๅทๅ้ใ")
|
| 53 |
+
return
|
| 54 |
+
|
| 55 |
+
tagger = await DanbooruTagger.get_instance()
|
| 56 |
+
results = await asyncio.to_thread(
|
| 57 |
+
tagger.get_related,
|
| 58 |
+
seed_tags,
|
| 59 |
+
set(seed_tags), # exclude ็งๅญๆ ็ญพ่ช่บซ
|
| 60 |
+
args.limit,
|
| 61 |
+
not args.no_nsfw,
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
if not results:
|
| 65 |
+
print("ๆชๆพๅฐๅ
ณ่ๆจ่๏ผๅ
ฑ็ฐ่กจๅฏ่ฝๆชๅ ่ฝฝ๏ผๆ็งๅญๆ ็ญพไธๅจๅบไธญ๏ผใ")
|
| 66 |
+
return
|
| 67 |
+
|
| 68 |
+
print(f"\n{'='*60}")
|
| 69 |
+
print(f"็งๅญๆ ็ญพ๏ผ{', '.join(seed_tags)} | ๅ
ฑ {len(results)} ๆกๆจ่")
|
| 70 |
+
print(f"{'='*60}")
|
| 71 |
+
|
| 72 |
+
# ่พๅบ้ๅทๅ้็ๆ ็ญพไธฒ๏ผๆนไพฟ็ดๆฅๅคๅถไฝฟ็จ๏ผ
|
| 73 |
+
tag_list = [r.tag for r in results if not (r.nsfw == '1' and args.no_nsfw)]
|
| 74 |
+
print(f"ๆจ่ๆ ็ญพ๏ผ\n {', '.join(tag_list)}\n")
|
| 75 |
+
|
| 76 |
+
# ๆ็ป่กจ
|
| 77 |
+
for r in results:
|
| 78 |
+
if r.nsfw == '1' and args.no_nsfw:
|
| 79 |
+
continue
|
| 80 |
+
nsfw_mark = "๐ด" if r.nsfw == '1' else "๐ข"
|
| 81 |
+
sources_str = f" โ {', '.join(r.sources)}" if args.show_sources else ""
|
| 82 |
+
print(
|
| 83 |
+
f" {nsfw_mark} [{r.cooc_score:.3f}] {r.tag:<30}"
|
| 84 |
+
f" {r.cn_name[:20]:<20} {r.category}"
|
| 85 |
+
f" (ๅ
ฑ็ฐ:{r.cooc_count:,}){sources_str}"
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
# โโ ๅ
ฅๅฃ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 90 |
+
|
| 91 |
+
async def main():
|
| 92 |
+
parser = argparse.ArgumentParser(
|
| 93 |
+
description="Danbooru Tag CLI Searcher",
|
| 94 |
+
formatter_class=argparse.RawDescriptionHelpFormatter,
|
| 95 |
+
epilog="""
|
| 96 |
+
็คบไพ๏ผ
|
| 97 |
+
python cli.py search "็ฝ่ฒๆฐดๆๆ็ๅฅณๅญฉ" --limit 10
|
| 98 |
+
python cli.py related "white_serafuku,sailor_collar"
|
| 99 |
+
python cli.py related "white_serafuku" --limit 30 --no-nsfw --show-sources
|
| 100 |
+
""",
|
| 101 |
+
)
|
| 102 |
+
sub = parser.add_subparsers(dest='cmd', required=True)
|
| 103 |
+
|
| 104 |
+
# โโ search ๅญๅฝไปค โโ
|
| 105 |
+
p_search = sub.add_parser('search', help='่ฏญไนๆ็ดขๆ ็ญพ')
|
| 106 |
+
p_search.add_argument('query', help='ๆ็ดข่ฏ๏ผๆฏๆไธญ่ฑๆ่ช็ถ่ฏญ่จ๏ผ')
|
| 107 |
+
p_search.add_argument('--top-k', type=int, default=5, help='ๆฏๅฑ่ฟๅๆฐ้๏ผ้ป่ฎค 5๏ผ')
|
| 108 |
+
p_search.add_argument('--limit', type=int, default=20, help='็ปๆไธ้๏ผ้ป่ฎค 20๏ผ')
|
| 109 |
+
p_search.add_argument('--weight', type=float, default=0.15, help='็ญๅบฆๆ้๏ผ้ป่ฎค 0.15๏ผ')
|
| 110 |
+
p_search.add_argument('--no-nsfw', action='store_true', help='่ฟๆปค NSFW ๅ
ๅฎน')
|
| 111 |
+
p_search.add_argument('--no-seg', action='store_true', help='็ฆ็จๆบ่ฝๅ่ฏ')
|
| 112 |
+
|
| 113 |
+
# โโ related ๅญๅฝไปค โโ
|
| 114 |
+
p_related = sub.add_parser('related', help='ๅบไบๅ
ฑ็ฐ่กจๆฅๅ
ณ่ๆจ่')
|
| 115 |
+
p_related.add_argument('tags', help='็งๅญๆ ็ญพ๏ผไปฅ่ฑๆ้ๅทๅ้๏ผๅฆ white_serafuku,sailor_collar๏ผ')
|
| 116 |
+
p_related.add_argument('--limit', type=int, default=50, help='ๆจ่็ปๆไธ้๏ผ้ป่ฎค 50๏ผ')
|
| 117 |
+
p_related.add_argument('--no-nsfw', action='store_true', help='่ฟๆปค NSFW ๅ
ๅฎน')
|
| 118 |
+
p_related.add_argument('--show-sources', action='store_true', help='ๆพ็คบๆฏๆกๆจ่็ฑๅชไธช็งๅญ่งฆๅ')
|
| 119 |
+
|
| 120 |
+
args = parser.parse_args()
|
| 121 |
+
if args.cmd == 'search':
|
| 122 |
+
await cmd_search(args)
|
| 123 |
+
elif args.cmd == 'related':
|
| 124 |
+
await cmd_related(args)
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
if __name__ == "__main__":
|
| 128 |
+
asyncio.run(main())
|