#!/bin/bash # Style Scout CSD: bootstrap -> venv -> data/model download -> LDA -> HTML. set -euo pipefail script_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)" if [[ "${BASH_SOURCE[0]}" == *.command ]]; then cd "$script_dir"; fi if [[ "${1:-}" == --help || "${1:-}" == -h ]]; then cat <<'HELP' Style Scout CSD — 自動セットアップとLDA検索 chmod +x start.sh # 初回のみ ./start.sh # 対話式 ./start.sh --artist mochizuki_kei # 作者検索 ./start.sh --images '/path/to/folder' # 画像・フォルダ検索 ./start.sh --artist '1=2' --no-open # HTMLを開かず保存 Python 3.10以上を自動検出します(Python本体がなければ先に導入してください)。 仮想環境・必要ライブラリ・CSD作者パックは自動導入します。 画像検索時だけCSDモデルを取得。画像は外部へ送信しません。 結果は実行場所のresults/へ保存し、HTMLを開きます。 --install-dir DIR: 導入先変更(既定 ~/style-scout-csd) --offline: 導入済み環境を通信なしで使用 STYLE_SCOUT_PYTHON: 使用するPython実行ファイルを明示指定 HELP exit 0 fi if [[ $# == 0 ]]; then cat <<'USAGE' Style Scout CSD — LDA検索 オプションで直接指定することもできます: ./start.sh --artist mochizuki_kei # 作者名で検索 ./start.sh --images '/path/to/image.png' # 画像ファイルで検索 ./start.sh --images '/path/to/folder' # フォルダ内の画像で検索 ./start.sh --help # その他のオプション このまま対話式で進む場合は、次から選んでください。 1: 作者名で検索 2: 画像ファイル/フォルダで検索 USAGE read -r -p '選択 [1]: ' mode case "${mode:-1}" in 1) read -r -p 'Danbooru作者タグ: ' query; [[ -n "$query" ]] || { echo '作者タグが空です' >&2; exit 2; }; set -- --artist "$query" ;; 2) read -r -p '画像ファイル/フォルダのパス(引用符なし): ' query; [[ -n "$query" ]] || { echo 'パスが空です' >&2; exit 2; }; set -- --images "$query" ;; *) echo '1または2を指定してください' >&2; exit 2 ;; esac fi install_dir="${STYLE_SCOUT_CSD_HOME:-$HOME/style-scout-csd}" offline=0 args=("$@") for ((i=0;i<${#args[@]};i++)); do case "${args[i]}" in --install-dir) ((i+1<${#args[@]})) || { echo '--install-dirに保存先が必要です' >&2; exit 2; }; install_dir="${args[i+1]}" ;; --install-dir=*) install_dir="${args[i]#--install-dir=}" ;; --offline) offline=1 ;; esac done if [[ "$install_dir" == '~/'* ]]; then install_dir="$HOME/${install_dir:2}"; fi python_bin='' # Try an explicit interpreter or the existing venv first. Ignore broken PATH shims. candidates=("${STYLE_SCOUT_PYTHON:-}" "$install_dir/.venv/bin/python" python3.12 python3.13 python3.14 python3.11 python3.10 python3) for prefix in /opt/homebrew/bin /usr/local/bin /usr/bin; do for version in 3.12 3.13 3.14 3.11 3.10 3; do candidates+=("$prefix/python$version"); done done for candidate in "${candidates[@]}"; do [[ -n "$candidate" ]] || continue if "$candidate" -c 'import sys,venv,ssl; assert sys.version_info >= (3,10)' >/dev/null 2>&1; then python_bin="$candidate"; break; fi done if [[ -z "$python_bin" ]]; then echo 'Python 3.10以上が見つかりません。https://www.python.org/downloads/ からPythonを導入し、もう一度実行してください。' >&2 exit 2 fi mkdir -p "$install_dir" install_dir="$(cd "$install_dir" && pwd)" export PYTHONDONTWRITEBYTECODE=1 printf 'Python: %s\n導入先: %s\n' "$python_bin" "$install_dir" "$python_bin" - "$install_dir/bootstrap.py" "$offline" <<'PY' import os,sys,urllib.request from pathlib import Path path=Path(sys.argv[1]);offline=sys.argv[2]=='1' if offline: if not path.is_file():raise SystemExit('初回はネット接続が必要です(bootstrap未取得)') else: try: req=urllib.request.Request('https://huggingface.co/datasets/cloudaiman/style-scout-csd-lda/resolve/main/run_style_scout_csd.py',headers={'User-Agent':'StyleScout-CSD-Setup/1'}) with urllib.request.urlopen(req,timeout=30) as response:raw=response.read(1024*1024) compile(raw,'run_style_scout_csd.py','exec') temp=path.with_name(path.name+'.'+str(os.getpid())+'.tmp');temp.write_bytes(raw);temp.replace(path) except Exception: if not path.is_file():raise SystemExit('起動コードを取得できません。通信状況を確認してください。') print('更新確認に失敗。保存済みの起動コードを利用します。') PY exec "$python_bin" "$install_dir/bootstrap.py" --install-dir "$install_dir" "$@"