SAkizuki commited on
Commit
b90b1f5
ยท
verified ยท
1 Parent(s): 54316b6

Auto-sync from GitHub Actions

Browse files
Files changed (2) hide show
  1. core/engine.py +34 -5
  2. ui_nicegui.py +12 -1
core/engine.py CHANGED
@@ -25,7 +25,8 @@ import jieba
25
  import numpy as np
26
  import pandas as pd
27
  import torch
28
- from safetensors.torch import load_file as st_load, save_file as st_save
 
29
  from sentence_transformers import SentenceTransformer
30
 
31
  from .models import SearchRequest, SearchResponse, TagResult
@@ -699,11 +700,39 @@ class DanbooruTagger:
699
  print(f'[Engine] ็ผ“ๅญ˜ไฟๅญ˜ๅฎŒๆˆ๏ผˆ{len(self.df)} ๆก่ฎฐๅฝ•๏ผ‰๏ผŒ็”Ÿๆˆๆ—ถ้—ด: {current_time}')
700
 
701
  def _load_from_cache(self) -> None:
702
- tensors = st_load(str(self.paths.embeddings), device=self.device)
703
- for _, attr, _ in _LAYER_SPEC:
704
- setattr(self, attr, tensors[attr].float())
705
- self.df = pd.read_parquet(str(self.paths.metadata))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
706
  self.max_log_count = float(np.log1p(self.df['post_count'].max()))
 
 
 
707
 
708
  def _cached_schema_version(self) -> int:
709
  try:
 
25
  import numpy as np
26
  import pandas as pd
27
  import torch
28
+ from safetensors.torch import save_file as st_save
29
+ from safetensors import safe_open
30
  from sentence_transformers import SentenceTransformer
31
 
32
  from .models import SearchRequest, SearchResponse, TagResult
 
700
  print(f'[Engine] ็ผ“ๅญ˜ไฟๅญ˜ๅฎŒๆˆ๏ผˆ{len(self.df)} ๆก่ฎฐๅฝ•๏ผ‰๏ผŒ็”Ÿๆˆๆ—ถ้—ด: {current_time}')
701
 
702
  def _load_from_cache(self) -> None:
703
+ t0 = time.time()
704
+ emb_path = str(self.paths.embeddings)
705
+ meta_path = str(self.paths.metadata)
706
+ emb_size_mb = self.paths.embeddings.stat().st_size / 1024 / 1024
707
+ meta_size_mb = self.paths.metadata.stat().st_size / 1024 / 1024
708
+
709
+ # โ”€โ”€ ๆญฅ้ชค 1/3: embedding ๆ–‡ไปถ๏ผˆ้€ๅฑ‚ๅŠ ่ฝฝไปฅๆ˜พ็คบ่ฟ›ๅบฆ๏ผ‰โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
710
+ n_layers = len(_LAYER_SPEC)
711
+ print(f' [1/3] ๅŠ ่ฝฝ embedding ({emb_size_mb:.0f} MB, {n_layers} ๅฑ‚) ...')
712
+ with safe_open(emb_path, framework="pt", device=self.device) as f:
713
+ for i, (name, attr, _) in enumerate(_LAYER_SPEC, 1):
714
+ shape = f.get_shape(attr)
715
+ shape_str = 'ร—'.join(str(d) for d in shape)
716
+ print(f' [{i}/{n_layers}] {attr:12s} ({shape_str}) ...', end=' ', flush=True)
717
+ _t = time.time()
718
+ tensor = f.get_tensor(attr)
719
+ setattr(self, attr, tensor.float())
720
+ print(f'โœ“ {time.time() - _t:.1f}s')
721
+ print(f' [1/3] โœ“ {time.time() - t0:.1f}s')
722
+
723
+ # โ”€โ”€ ๆญฅ้ชค 2/3: ๅ…ƒๆ•ฐๆฎ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
724
+ print(f' [2/3] ๅŠ ่ฝฝๅ…ƒๆ•ฐๆฎ ({meta_size_mb:.0f} MB) ...', end=' ', flush=True)
725
+ _t = time.time()
726
+ self.df = pd.read_parquet(meta_path)
727
+ print(f'โœ“ {time.time() - _t:.1f}s ({len(self.df):,} ๆก)')
728
+
729
+ # โ”€โ”€ ๆญฅ้ชค 3/3: ็ปŸ่ฎกไฟกๆฏ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
730
+ print(f' [3/3] ่ฎก็ฎ—็ปŸ่ฎกไฟกๆฏ ...', end=' ', flush=True)
731
+ _t = time.time()
732
  self.max_log_count = float(np.log1p(self.df['post_count'].max()))
733
+ print(f'โœ“ {time.time() - _t:.1f}s')
734
+
735
+ print(f'[Engine] ็ผ“ๅญ˜ๅŠ ่ฝฝๅฎŒๆˆ (ๆ€ป่€—ๆ—ถ {time.time() - t0:.1f}s)')
736
 
737
  def _cached_schema_version(self) -> int:
738
  try:
ui_nicegui.py CHANGED
@@ -471,7 +471,18 @@ class DanbooruSearchUI:
471
  with self.init_banner:
472
  with ui.row().classes('items-center gap-3 p-2'):
473
  ui.spinner(size='sm')
474
- ui.label('ๅผ•ๆ“Žๅˆๅง‹ๅŒ–ไธญ๏ผŒ่ฏท็จๅ€™โ€ฆ้ฆ–ๆฌกๅŠ ่ฝฝ็บฆ้œ€ 15 ็ง’').classes('text-sm text-blue-700')
 
 
 
 
 
 
 
 
 
 
 
475
  self.init_banner.set_visibility(not DanbooruTagger.is_ready())
476
  if not DanbooruTagger.is_ready():
477
  asyncio.ensure_future(self._hide_banner_when_ready())
 
471
  with self.init_banner:
472
  with ui.row().classes('items-center gap-3 p-2'):
473
  ui.spinner(size='sm')
474
+ ui.label('ๅผ•ๆ“Žๅˆๅง‹ๅŒ–ไธญ๏ผŒ่ฏท็จๅ€™โ€ฆ็บฆ้œ€ 5~10 ๅˆ†้’Ÿ').classes('text-sm text-blue-700')
475
+ from platform_utils import PLATFORM
476
+ _alt_url = (
477
+ 'https://www.modelscope.cn/studios/SAkizuki/DanbooruSearchOnline'
478
+ if PLATFORM == 'hf' else
479
+ 'https://huggingface.co/spaces/SAkizuki/DanbooruSearch'
480
+ )
481
+ ui.html(
482
+ f'ๅˆๅง‹ๅŒ–ๆœŸ้—ด๏ผŒๆ‚จๅฏไปฅไฝฟ็”จ'
483
+ f'<a href="{_alt_url}" target="_blank" rel="noopener noreferrer" '
484
+ f'class="text-blue-600 hover:text-blue-800 underline font-bold">ๅค‡็”จๆœๅŠก</a>'
485
+ ).classes('text-xs text-blue-600 px-6 pb-3')
486
  self.init_banner.set_visibility(not DanbooruTagger.is_ready())
487
  if not DanbooruTagger.is_ready():
488
  asyncio.ensure_future(self._hide_banner_when_ready())