Compare commits

...
23 Commits
Author SHA1 Message Date
ketle-manandClaude Sonnet 5.5 fb0c8cc979 release: v0.8.2 - Gallery semantic search (Gemini Embedding 2 / local EmbeddingGemma 2, GPU)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-12 13:39:54 +09:00
ketle-manandClaude Sonnet 5.5 bc436a4435 Merge branch 'feature/gallery-semantic-search': Gallery semantic search (v0.8.2)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-12 13:39:17 +09:00
ketle-manandClaude Sonnet 5.5 4ed361b78a feat: tabbed semantic modal, similarity thresholds, toolbar second row, input guards
- Modal tabs: Index / Model / Search; Search tab sets min score for similar
  images and text search (saved per model) and max results
- Toolbar: "S" toggle; when ON a second row shows Similar, refresh, settings
  and the result count (Similar/refresh moved from the detail panel)
- Backend: min_score filter, numeric parameter validation (400 bad_param),
  text/image/thread input caps in the local embedding runtime
- Help/i18n updated (en/ja/zh)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-12 13:35:37 +09:00
ketle-manandClaude Sonnet 5.5 821bee4b6e feat: local EmbeddingGemma 2 GPU (CUDA) support, CPU thread limit, image precision
- Device select (auto / CPU / GPU CUDA), CPU thread count (default half the cores)
- Image precision (soft tokens 280/140/70) stored with the index; mismatch needs rebuild
- Provider shown in connection test; model released after indexing
- Settings persisted (semantic_settings.json); help (en/ja/zh) + requirements note
- DirectML unsupported (vision encoder fails)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-12 13:17:01 +09:00
ketle-manandClaude Sonnet 5.5 63c3321aa6 style: widen help pages to align with the top tab bar (AI TOOL tab)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-12 12:43:55 +09:00
ketle-manandClaude Sonnet 5.5 cbedb973aa style: use teal for Similar buttons to distinguish from Tagger
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-12 12:39:27 +09:00
ketle-manandClaude Sonnet 5.5 4ec1861790 feat: semantic search modal help button, folder-tree index dots, pricing note
- Modal "?" button jumps to the Gallery help entry (highlighted)
- Folder tree shows a green dot next to the count when all images directly
  in the folder are indexed (GET /wfm/gallery/semantic/folders)
- Cost note: Gemini free tier / paid note (as of 2026-10); help text as plain text

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-12 12:36:02 +09:00
ketle-manandClaude Sonnet 5.5 47408506ba feat: Gallery semantic search (Gemini Embedding 2 / local EmbeddingGemma 2)
- Semantic Search toggle on the search box (Enter to search by meaning)
- Detail panel: Similar toggle + refresh button to list similar images
- Semantic settings modal (toolbar right end): build/update/delete the index,
  model radio (Gemini API / EmbeddingGemma 2 local ONNX), vector size, progress
- Backend: semantic_service (index store, job, cosine search) and
  semantic_local (onnxruntime port of EmbeddingGemma 2 image/text pipeline)
- GEMINI_API_KEY read from .env; help + i18n (en/ja/zh)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-12 12:14:46 +09:00
ketle-manandClaude Sonnet 5.5 333c5e5021 release: v0.8.1 - code review fixes (Nodes/Metadata/Gallery bugs, perf, dedupe)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-11 13:40:29 +09:00
ketle-manandClaude Sonnet 5.5 1b85fb0cd8 release: v0.8.0 - audio support, file conversion, Nodes tab speedup, metadata reading fixes
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-11 12:17:30 +09:00
ketle-manandClaude Sonnet 5.5 eacdae5831 docs: help for move filter, waveform colors, audio assets (en/ja/zh)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-11 12:15:19 +09:00
ketle-manandClaude Sonnet 5.5 15e19bc9e5 fix: neutral 'moved N' wording for files; bust browser cache for audio thumbnails
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-11 12:11:27 +09:00
ketle-manandClaude Sonnet 5.5 6751555b0b feat: folder filter in Gallery Move To dialog
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-11 11:33:46 +09:00
ketle-manandClaude Sonnet 5.5 00342a7f68 fix: audio player clipped in Gallery detail panel; per-format waveform colors
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-11 11:27:39 +09:00
ketle-manandClaude Sonnet 5.5 0973dc15a4 feat: Gallery file conversion (audio formats, audio extraction from video)
Convert... bulk button; PyAV-based; tag carry-over for tag-capable formats,
optional gallery-meta carry-over and original deletion.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-11 11:00:59 +09:00
ketle-manandClaude Sonnet 5.5 f1dba89f7b feat: audio assets in Video Asset tab (Set as BGM / Add as sound)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-11 10:50:53 +09:00
ketle-manandClaude Sonnet 5.5 0abebba4b1 feat: audio file support in Gallery (wav/mp3/ogg/m4a/flac/opus)
Waveform thumbnails, built-in player, duration and embedded prompt/workflow tags.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-11 10:40:19 +09:00
ketle-manandClaude Sonnet 5.5 1185aabbcf perf: show Nodes tab list from a light API, load /object_info in background
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-11 10:20:21 +09:00
ketle-manandClaude Sonnet 5.5 662b4dd209 perf: chunked rendering and debounced search in Nodes tab
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-11 10:08:32 +09:00
ketle-manandClaude Sonnet 5.5 3bbbfddd0d perf: do not block the Library N tab on /object_info
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-11 10:05:07 +09:00
ketle-manandClaude Sonnet 5.5 2355f503cd feat: Alt+click to toggle node favorite in Library N tab
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-11 09:58:42 +09:00
ketle-manandClaude Sonnet 5.5 be8cdd28ad docs: update help (en/ja/zh) and README for embedded metadata reading fixes
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-11 09:45:25 +09:00
ketle-manandClaude Sonnet 5.5 12a46c683c fix: improve embedded metadata reading in Gallery/Metadata/Info tabs
- read PNG chunks past IEND (ComfyUI-Custom-Scripts appends workflow after it)
- read workflow/prompt from WebP EXIF in the Gallery
- show prompts for A1111/Forge/Fooocus images in the Gallery Prompt tab
- pick up API-format ImageMetadataLoRALoader LoRAs (Metadata and Info tabs)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-11 09:43:57 +09:00
28 changed files with 3167 additions and 131 deletions
+5
View File
@@ -14,3 +14,8 @@
# 3. Restart ComfyUI
UNSLOTH_API_KEY=
# ComfyUI-Workflow-Studio - Gallery Semantic Search (Gemini Embedding 2)
# Get a key at https://aistudio.google.com (free tier available).
# Same steps as above: copy to ".env", paste the key, restart ComfyUI.
GEMINI_API_KEY=
+3
View File
@@ -2,6 +2,9 @@
Condensed version-by-version index of DEVLOG.md (each line: version + one-line summary of what changed and why). Full rationale, code details, and "How to apply" lessons live in the excluded DEVLOG.md itself; this index exists so graphify can extract a queryable semantic node per release without feeding the full log to the local Ollama model. Regenerate with `python tools/generate_doc_index.py devlog` whenever DEVLOG.md changes.
- **v0.8.2(2026-10-12)** — Gallery: セマンティック検索(Gemini Embedding 2 / EmbeddingGemma 2)
- **v0.8.1(2026-10-11)** — バグ修正
- **v0.8.0(2026-10-11)** — Gallery: 音声ファイル対応(wav / mp3 / ogg / m4a / flac / opus)
- **v0.7.14(2026-10-09)** — Comic Creator連携: nanobananaタブの生成履歴をGallery/Metadataタブに表示
- **v0.7.13(2026-10-06)** — VRAM調整(Ollama)— Comic Creatorから移植
- **v0.7.12(2026-10-04)** — セキュリティ: バックエンド接続先をブラウザから任意指定できた問題(SSRF)を修正
+95
View File
@@ -2,6 +2,101 @@
---
## v0.8.2(2026-10-12)
### Gallery: セマンティック検索(Gemini Embedding 2 / EmbeddingGemma 2)
画像を文章の意味で探す検索と、選択画像に類似した画像の一覧表示を追加した。
**UI**
- 検索ボックス横に「S」ボタン。ONで検索ボックスが意味検索になる(Enterで実行、スコア順、サムネイルにスコアバッジ)。OFFは従来のファイル名/タグ/プロンプト検索。
- Sを入れるとツールバーに2行目が現れ、`Similar`(トグル)・`↻`(更新)・`⚙`(設定)・検索結果件数を左詰めで表示する。Similarは、類似一覧の中で別の画像を選んでも一覧はそのままで、↻でその画像の類似を再表示、Similar再押下で通常表示に戻る。フォルダ切替・フィルタクリア・S OFFでも解除。Similar/↻は当初詳細パネルに置いていたが2行目へ移設(Taggerの紫と区別するためティール)。
- ⚙のモーダルを3タブ化: インデックス(対象フォルダ・サブフォルダ・新規のみ/全件再作成・進捗・停止・全削除)/ モデル設定(モデルのラジオボタン・次元・ローカルのデバイス/CPUスレッド/画像精度・接続テスト)/ 検索設定(類似画像と文章検索の最小スコア、最大表示件数)。「?」でヘルプの該当項目へジャンプ。
- 最小スコア(例: 0.6以上)はモデルごとに保存(スコアの水準がモデルで違うため。少数画像の実測では、類似画像はEmbeddingGemma 2で約0.6〜0.8、Geminiで約0.6、文章検索はそれぞれ約0.6〜0.7・約0.3)。
- フォルダツリーの枚数バッジの右に緑のドット = そのフォルダ直下の画像が全件インデックス済み(`GET /wfm/gallery/semantic/folders`)。インデックスは全フォルダ共通の1つで、フォルダ単位で少しずつ作れる。
- ヘルプページの幅を760→1076px(上部タブバーのAI TOOLタブの右端に揃える)。
**バックエンド**
- `py/services/semantic_service.py`: ベクトルを `semantic_index.npz` に保存(パス→正規化ベクトル、サイズ/mtimeで鮮度判定、モデル/次元/画像精度をヘッダに保持)。インデックス作成はバックグラウンドスレッド(進捗ポーリング・停止可)、検索はコサイン類似度(`mat @ q`)、未インデックスの基準画像はその場で1枚だけ埋め込む。動画/PSDはサムネイルから、音声は対象外。
- Gemini: `gemini-embedding-2`(`x-goog-api-key`ヘッダ、接続先固定、429/5xxは指数バックオフで再試行)。APIキーは `.env` の `GEMINI_API_KEY`(UNSLOTHと同じく settings.json・フロントには出さない)。
- EmbeddingGemma 2: `py/services/semantic_local.py`。onnx-community/embeddinggemma-2-ONNX を `onnxruntime` + `tokenizers` + `huggingface_hub` だけで実行(`transformers` の対応を待たない)。Transformers.jsの `Gemma4ImageProcessor` / `EmbeddingGemma2Processor` を移植(アスペクト比維持リサイズ → 16pxパッチ化 → vision_encoder → `<|image|>` 位置へ `image_features` を差し込み → `sentence_embedding`)。クエリは `task: search result | query: ` プレフィックス。MRL(128/256/512/768)で切り詰め+再正規化。
- 実行デバイス: 自動(CUDAがあればGPU)/ CPU / GPU(CUDA)。CPUスレッド数の既定はコアの半分。設定は `semantic_settings.json` に保存。画像精度はソフトトークン数 280/140/70(高精度/標準/軽量、CPUで約2.5倍/約5倍速)でインデックスに記録し、変えたら全件再作成が必要。インデックス作成後はセッションを解放しVRAM/RAMを画像生成に譲る。
**実測(RTX 4070 / 32コア)**
- 768×576の画像1枚あたり: CUDA 0.13秒(標準 0.07秒)、CPU 4スレッド 5.8秒(標準 2.3秒、軽量 1.1秒)。CPUとCUDAのベクトルは一致(cos 1.0)。実機で122枚を約20秒で作成。
- DirectMLはvision_encoderのReshapeノードで実行時エラーとなり非対応(テキストモデルのみ動作)。WindowsMLは未検証。
- 環境メモ: ComfyUI_5のvenvには `onnxruntime` と `onnxruntime-gpu`(ともに1.30.0)が同居し、CPU版が勝っていたため、両方を削除して `onnxruntime-gpu==1.30.0` のみを入れ直した(`pip check` は `onnxruntime` 名での依存を警告するが動作に影響なし)。
**安全性(セキュリティレビュー指摘への対応)**
- 数値パラメータ(dim / soft / threads / limit / min_score)を共通関数で範囲内に丸め、数値でない値は500ではなく400(`bad_param`)で返す。スレッド数はコア数の2倍まで。
- ローカルモデルへの入力を制限: テキスト1024トークン、検索文2000文字、画像は長辺2048pxを超えたら先に縮小。
**その他**
- `.env.example` に `GEMINI_API_KEY`、`requirements.txt` にGPU用の手順コメントを追加。ヘルプ(3言語)・READMEを更新。
- Gemini Embedding 2には無料枠があるが、無料枠で送った内容はGoogleの製品改善に使われる場合がある(2026年10月時点の公式料金: 画像1枚あたり約$0.00012)。
## v0.8.1(2026-10-11)
v0.8.0 のコードレビュー指摘(#1〜#10)への対応。
### バグ修正
- Nodes: 検索結果が0件のとき `renderToken` を進めておらず、描画途中のカードが「No nodes found」の下に追加され続けていた。
- Metadata: `convertUiToApi()` に `{ readOnly: true }` を追加。表示用の変換ではCOMBO値の無警告差し替え(サンプラー名の誤表示)を行わず、Workflowタブの互換性チェックが参照する `_last*` 記録も更新しない。
- Metadata: 画像を連続で開いたとき、先に始めた処理の結果が後から描画されて混ざる問題を、リクエストID(`_loadSeq`)で防止。
- Metadata: サンプラー設定の抽出を改善。Flux/SD3系(RandomNoise / BasicScheduler / KSamplerSelect / CFGGuider)の設定を統合し、複数KSamplerでは設定が最も多いノード(同数なら後ろ)を採用。
- Gallery: `history.jsonl` に dict でない行・`filenames` が list でない行があっても、フォルダ内の全画像でメタ取得が失敗しないようにした。
- Gallery: 同じファイルを同時に変換すると出力名が衝突して上書きされる問題を、出力名の決定を `_convert_lock` 内に移して解消。
- Gallery: PNGチャンクの読み取りを先勝ちに統一(`png_extractor` と同じ)。ワークフロー読み込みとの食い違いを防止。
### パフォーマンス・保守性
- Gallery: 一括選択の変換対象判定を O(選択数×画像数) から O(選択数) に(パス→画像Mapのキャッシュ + `some` で打ち切り)。12,780枚の全選択で約33ms。
- Gallery: `history.jsonl` を `(mtime_ns, size)` キーの索引としてキャッシュ(`OrderedDict`、上限16件)。
- 音声・動画の拡張子判定を `isAudioFile()` / `isVideoFile()` に統一し、波形サムネイルの版数を `WAVE_THUMB_VERSION` 定数に集約(JS/Python)。
## v0.8.0(2026-10-11)
### Gallery: 音声ファイル対応(wav / mp3 / ogg / m4a / flac / opus)
ComfyUIの音楽生成の増加に合わせ、Galleryで音声ファイルを画像・動画と同じ一覧で扱えるようにした。
- `gallery_service.py`: `AUDIO_EXTENSIONS` を追加し `IMAGE_EXTENSIONS` に合流。再生時間は`_read_media_dimensions()`(PyAV)、埋め込みタグは`_read_audio_metadata()`で取得(ストリーム側タグも統合し、`prompt`/`workflow`は小文字キーでも引ける)。`_extract_embedded_workflow()`も音声に対応し、Prompt/JSONタブが動く。
- サムネイルは`_render_audio_waveform()`でPyAVデコード→8kHzモノラル→列ごとの最大振幅を描画してJPEGキャッシュ。拡張子ごとに波形色を変える(`AUDIO_WAVE_COLORS`)。キャッシュキーに版数(`wave2`)、サムネイルURLにも`&v=wave2`を付けブラウザの24hキャッシュを回避。
- `gallery_routes.py`: Windowsで音声MIMEが返らない/不統一なため`mimetypes.add_type`で明示登録。`FileResponse`のRange対応でシークも可能。
- `gallery-tab.js`: `isAudioFile()`、♪バッジ、詳細パネル/ライトボックス/Compareで「波形+`<audio controls>`」を表示(音量は動画と共通設定)。詳細パネルは音声のとき固定高さをやめ、再生メニューが見切れないようにした。PSD書き出し・Edit as Layersでは音声を除外。
### Video: Assetサブタブで音声を素材として利用
- 先に音声を一覧から除外していたのを改め、種別フィルタに「Audio Only」を追加。音声はプレイヤー付きで、「BGMに設定」「サウンドとして追加(Audioトラックの選択クリップ先頭に配置)」ボタンが使える。`video-edit-tab.js`に`addSoundFromFile()`を追加。Editに送る/オーバーレイ/First・Last Imageと、Frame/GIFツールへのSourceプレビュー投入は音声では出さない。
### Gallery: ファイル変換(音声変換 / 動画からの音声抽出)
- 新規`py/services/convert_service.py`(PyAVのみ、システムffmpeg不要)。出力はFLAC / MP3 / Opus / M4A / WAV(Vorbisエンコーダが無いため`.ogg`は入力のみ)。ビットレート・サンプルレート・チャンネル指定可。
- `GalleryService.convert_file()` + `POST /wfm/gallery/convert`(1ファイルずつ呼ぶ。`run_in_executor`+同時1本のロック)。同じフォルダに`name.ext`(既存なら`name_converted.ext`、以降連番)で保存し上書きしない。
- メタデータ: `prompt`/`workflow`と曲情報タグを、タグを持てる形式(FLAC/MP3/Opus/M4A)へ引き継ぐ。WAVは対象外(通知)。お気に入り/タグ/メモ/グループの引き継ぎ、変換成功後の元ファイル削除(確認ダイアログ付き)を選択可。
- UI: 一括バーに「変換...」ボタン(音声/動画を含む選択時のみ表示)とダイアログ。i18n(en/ja/zh)対応。
### Gallery: メタデータ読み取りの修正(メタデータ抽出 比較レポートA1〜A4)
- A1: PNGの読み取りがIENDで止まっていたため、ComfyUI-Custom-ScriptsがIENDの後ろに追記する`workflow`チャンクを読めなかった(`_read_png_metadata()` / `extract_png_workflow()`)。
- A2: WebPのEXIFから`workflow:`/`prompt:`を取り出す`_read_webp_metadata()`を追加。
- A3: ワークフローが無い画像(A1111/Forge/Fooocus)は`parameters`からPromptタブを表示(`extractPromptsFromParameters()`)。
- A4: API形式の`ImageMetadataLoRALoader`(`lora_N`/`strength_model_N`/`strength_clip_N`)をMetadataタブとサイドパネルIタブで検出。
### Library/Nodes: 表示速度の改善とAlt+クリックお気に入り
- LibraryのNタブ: Alt+クリックでお気に入りをON/OFF。`/object_info`(数MB)の取得を待たずに一覧表示し、Category/Packageは`LiteGraph.registered_node_types[..].nodeData`から取得(無い環境のみ裏で取得)。
- SPAのNodesタブ: 数千件を一括でDOM生成していたのを分割描画に変更し、検索をデバウンス+検索文字列の事前計算で軽量化。さらに`/api/wfm/nodes/light`(`INPUT_TYPES`を呼ばず約6ms)で一覧を先に表示し、入力定義を含む`/object_info`(実測で約14秒)は裏で取得して補完する方式にした。
### Gallery: Move Toダイアログにフォルダ絞り込み
- フォルダ名/パスの部分一致(空白区切りAND)で絞り込み、Enterで先頭候補へ移動。音声/動画も移動できるため、文言を「N件移動しました」に変更(`movedNImages`、`moveToFolderTitle`)。
### その他
- ヘルプ(en/ja/zh)とREADMEを更新。READMEのバージョンバッジが0.7.13のままだったのを修正。
---
## v0.7.14(2026-10-09)
### Comic Creator連携: nanobananaタブの生成履歴をGallery/Metadataタブに表示
+4 -2
View File
@@ -11,6 +11,7 @@ A comprehensive workflow, asset management, and generation UI plugin for [ComfyU
- Workflow, model, image, and prompt management
- AI-powered prompt writing assistance, translation, and tag generation
- File drop & Gallery-linked metadata viewing — inspect and reuse generation settings instantly
- Semantic image search in the Gallery — find images by describing them, or list images similar to a selected one
**⚡ GenerateUI** — a productivity-focused generation tab connected to every other tab
@@ -26,7 +27,7 @@ A comprehensive workflow, asset management, and generation UI plugin for [ComfyU
- Built-in AI tools (translation and more)
![Workflow Studio](https://img.shields.io/badge/ComfyUI-Custom_Node-blue)
![Version](https://img.shields.io/badge/version-0.7.13-green)
![Version](https://img.shields.io/badge/version-0.8.2-green)
## Features (tab -> feature names)
@@ -52,7 +53,7 @@ Setting / Results / Plan JSON sub-panels, Per-column keyframes, Model column —
2-column layout, Collapsible sections, Theme selection, Theme customization, Workflows directory, Gallery output directory, Eagle connection, CivitAI Host, CivitAI API Key, Default workflow, Default Checkpoint, VRAM Management (Ollama), Video Playback Volume, Data Management, Text Size, RAW JSON Colors, GenerateUI Model Tab Highlight, Wildcard Integration, G'MIC-Qt Integration, Language
### Gallery Tab (v0.3.44)
Output / ImagePrompt / Style_Catalog / Metadata sub-tabs, Image browser, Folder tree root label, Thumbnail / Table views, Folder management, File operations, Download, MP4 video support, MP4 embedded metadata, WebM support, Info tab: dimensions & duration, Multi-select, Image Compare, Prompt search, Server-side filtering, Group management, Thumbnail F button, Favorites, Detail panel, Prompt tab, Nanobanana tab, Workflow viewer, GenUI button, Image Edit button, Send GenUI Image button, Send to LI node button, Send to LV node button, Send CC button, Search clear (✕), Clear all filters (✕ Clear), Workflow auto-save, Output folder configurable, SVG file support, PSD file support, Performance
Output / ImagePrompt / Style_Catalog / Metadata sub-tabs, Image browser, Folder tree root label, Thumbnail / Table views, Folder management, File operations, Download, MP4 video support, MP4 embedded metadata, WebM support, Embedded metadata reading fixes, Audio support, File conversion, Move To folder filter, Semantic Search, Audio waveform colors, Info tab: dimensions & duration, Multi-select, Image Compare, Prompt search, Server-side filtering, Group management, Thumbnail F button, Favorites, Detail panel, Prompt tab, Nanobanana tab, Workflow viewer, GenUI button, Image Edit button, Send GenUI Image button, Send to LI node button, Send to LV node button, Send CC button, Search clear (✕), Clear all filters (✕ Clear), Workflow auto-save, Output folder configurable, SVG file support, PSD file support, Performance
### ImagePrompt Gallery subtab (v0.3.94, renamed from Style/Prompt in v0.3.95)
Visual prompt library, 3-column layout, Prompt builder (right pane), Plain-text prompt storage, ponyxlWildcardsVault format support, Seed data importer
@@ -158,6 +159,7 @@ Click the **camera icon** (next to the W button) in ComfyUI's top bar to capture
- **[LM Studio](https://lmstudio.ai/)** — alternative backend for translation and VLM (OpenAI-compatible API)
- **[Lemonade Server](https://lemonade-server.ai/)** — alternative backend for translation, chat, and VLM (OpenAI-compatible API)
- **[Unsloth](https://unsloth.ai/)** — alternative backend for translation, chat, and VLM (OpenAI-compatible API); needs an API key set via `UNSLOTH_API_KEY` in a `.env` file, or Unsloth's Keyless API access → "Chat and inference" turned on — `pip install -r requirements.txt` for `python-dotenv`
- **Gemini API key / EmbeddingGemma 2** (v0.8.2) — only for the Gallery's Semantic Search: either set `GEMINI_API_KEY` in the plugin's `.env` (key from [Google AI Studio](https://aistudio.google.com); free tier available, paid beyond it), or use the local **EmbeddingGemma 2** (no key; `onnxruntime`, `tokenizers` and `huggingface_hub` — normally already present with ComfyUI; the model downloads on first use). `onnxruntime-gpu` makes the local model ≈45× faster on NVIDIA GPUs
- **[Eagle](https://eagle.cool/)** — for auto-saving generated images with metadata
- **[comfyui-mask-editor-one](https://github.com/ketle-man/comfyui-mask-editor-one) (v0.1.9+)** — enables BiRefNet background removal, SAM3 text-prompt segmentation, and ABR stamp-brush library in the Image Edit Mask tool; `birefnet.safetensors` must be placed in `ComfyUI/models/background_removal/` for BiRefNet
- **[psd-tools](https://pypi.org/project/psd-tools/)** (v0.6.0) — enables all PSD (Photoshop) features: Gallery bulk "Create PSD" export, Gallery browsing/thumbnails for `.psd` files, and Image Edit's Save PSD / Open PSD buttons; `pip install -r requirements.txt` (ships prebuilt wheels, no C compiler required); without it, PSD-related actions show an error toast with the install command instead of failing silently
+15 -3
View File
@@ -7,6 +7,7 @@ A comprehensive workflow, asset management, and generation UI plugin for [ComfyU
- Workflow, model, image, and prompt management
- AI-powered prompt writing assistance, translation, and tag generation
- File drop & Gallery-linked metadata viewing — inspect and reuse generation settings instantly
- Semantic image search in the Gallery — find images by describing them, or list images similar to a selected one
**⚡ GenerateUI** — a productivity-focused generation tab connected to every other tab
@@ -22,7 +23,7 @@ A comprehensive workflow, asset management, and generation UI plugin for [ComfyU
- Built-in AI tools (translation and more)
![Workflow Studio](https://img.shields.io/badge/ComfyUI-Custom_Node-blue)
![Version](https://img.shields.io/badge/version-0.7.13-green)
![Version](https://img.shields.io/badge/version-0.8.2-green)
## Screenshots
@@ -263,6 +264,16 @@ An experimental batch generator: runs the workflow currently loaded in GenerateU
- **MP4 video support** (v0.5.0) — `.mp4` files are browsable, previewable (detail panel with native `<video controls>`, lightbox, and Compare), and included in favorites/tags/groups/move/delete/export alongside images; server-side thumbnails are generated from the first video frame via PyAV (falls back to a placeholder if PyAV/ffmpeg decoding isn't available); a small ▶ badge overlays video thumbnails in the grid
- **MP4 embedded metadata** (v0.5.1) — the Prompt and JSON tabs now also work for `.mp4` files: reads the `workflow`/`prompt` container tags ComfyUI's `SaveVideo` writes into the mp4 (via PyAV server-side), including all-in-one video-generation nodes that carry their prompt directly on a `prompt` input rather than a separate `CLIPTextEncode`-style node (e.g. `MiniMaxH3ImageToVideo`)
- **WebM support** (v0.7.14) — `.webm` files get the same treatment as `.mp4` everywhere above (browsing, thumbnails, dimensions/duration, embedded workflow/prompt metadata, favorites/tags/groups/move/delete/export, Compare, Comic Creator video send): all the video-specific code paths go through PyAV, which handles both containers identically, so no format-specific logic was needed beyond recognizing the extension
- **Embedded metadata reading fixes** (v0.8.0) — PNG chunks appended after `IEND` (ComfyUI-Custom-Scripts writes `workflow` there) are now read; `.webp` files expose their EXIF `workflow`/`prompt` to the Prompt and JSON tabs; images without a ComfyUI workflow (A1111 / Forge / Fooocus) show their `parameters` prompts in the Prompt tab; API-format `ImageMetadataLoRALoader` LoRAs are listed in the Metadata tab and the side-panel I tab
- **Audio support** (v0.8.0) — `.wav` / `.mp3` / `.ogg` / `.m4a` / `.flac` / `.opus` files are browsable alongside images and videos (for ComfyUI music generation output): thumbnails are waveform images generated server-side (PyAV, cached), the detail panel / lightbox / Compare show the waveform with a built-in audio player (shared volume setting with videos), the Info tab shows the duration, and the Prompt / JSON tabs read the `prompt` / `workflow` tags that SaveAudio-type nodes embed (FLAC / MP3 / Opus); favorites, tags, groups, move, delete, and export work as for other files, while image-only actions (PSD export, Edit Layers) skip audio; in the Video tab's Asset sub-tab, audio files added to the Video Assets group appear with an "Audio Only" filter, an inline player, and **Set as BGM** / **Add as sound** buttons that feed the Edit sub-tab
- **File conversion** (v0.8.0) — a **Convert...** button in the Gallery bulk bar (shown when the selection contains audio or video) converts audio files to FLAC / MP3 / Opus / M4A / WAV, or extracts the audio track of videos, using PyAV (no system ffmpeg needed); bitrate, sample rate (44.1 / 48 kHz) and channels (mono / stereo) are selectable, output goes next to the source as a new file (`name.ext`, or `name_converted.ext` if taken). ComfyUI `prompt` / `workflow` tags (and title / artist / album tags) are carried over to formats that can hold them — WAV cannot, so they are skipped there; favorite / tags / memo / groups can be carried over, and the original can optionally be deleted after a successful conversion (with a confirmation). `.ogg` is readable but not offered as an output (the bundled PyAV has no Vorbis encoder)
- **Move To folder filter** (v0.8.0) — the Move To dialog (detail panel and bulk bar) has a filter box that narrows the folder list by name or path (space = AND), Enter moves to the first match; the move toast now reads "Moved N file(s)" since audio/video files are moved too
- **Semantic Search** (v0.8.2) — find images by meaning instead of keywords. The **S** button next to the search box turns it on: the search box then takes a free-text description (e.g. "sunset city street"), Enter searches, and results are ordered by similarity with a score badge on each thumbnail. Turning **S** on also shows a second toolbar row (left-aligned): **Similar** (lists images similar to the selected one; selecting another image keeps the list, **↻** re-runs it for the newly selected image, pressing Similar again returns to the normal list), the **⚙** settings button, and the result count. Searches cover the selected folder and its subfolders; sort and tag/group filters do not apply to semantic results
- **Index window (⚙)** — three tabs. **Index**: target folder (optionally with subfolders), "new/changed only" or "rebuild all", progress bar, stop, delete all. **Model**: choose with radio buttons between **Gemini Embedding 2** (API; set `GEMINI_API_KEY` in the plugin's `.env` and restart ComfyUI — the free tier exists but content sent on it may be used by Google to improve its products, beyond it the API is paid) and **EmbeddingGemma 2** (runs locally, free, no key; the ~300 MB q4 ONNX model is downloaded on first use), plus vector size (Gemini 768 / 1536 / 3072, EmbeddingGemma 2 128 / 256 / 512 / 768), and for the local model the device (auto / CPU / GPU CUDA), CPU thread count (default: half the cores) and image precision (high / standard ≈2.5× faster / light ≈5× faster). **Search**: minimum score for similar-image results (e.g. `0.6` shows only images scoring 0.6 or higher), minimum score for text search, and maximum results — stored per model because score levels differ between models
- **Index state in the folder tree** — a green dot next to a folder's image count means every image directly in that folder is indexed; the index is one shared store, so folders can be indexed one at a time. Images, plus videos / PSD via their thumbnails (audio is skipped), are embedded; changing the model, vector size or image precision requires "Rebuild all"
- **Local GPU** — EmbeddingGemma 2 is a port of Transformers.js' pipeline onto `onnxruntime` (no `transformers` support needed). On an RTX 4070 it indexes ≈0.13 s per image with CUDA versus ≈6 s on the CPU (≈45× faster, identical vectors); to use the GPU, install `onnxruntime-gpu` in place of `onnxruntime` (`pip uninstall onnxruntime onnxruntime-gpu` then `pip install onnxruntime-gpu`). DirectML is not supported (its vision model fails). The model is released from memory when indexing finishes
- **Audio waveform colors** (v0.8.0) — waveform thumbnails are colored per format (MP3 blue, WAV green, FLAC amber, OGG/Opus purple, M4A rose)
- **Info tab: dimensions & duration** (v0.5.1) — the detail panel's Info tab now also shows image dimensions (width×height) or video duration, in addition to filename, size, date, tags, and memo
- **Multi-select** — Ctrl+click to select multiple images; Bulk Bar appears for batch operations: select group → "Add to Group" / "Remove from Group"; Favorite All / Unfavorite All; Compare (2–4 images); Move To...; **Export** (downloads all selected images as a ZIP file); **Create PSD** (v0.6.0, combines the selected images into one layered PSD file, positioned top-left on a canvas sized to the largest image); **Edit as Layers** (v0.6.0, opens all selected images in the Image Edit tab at once, each as its own layer); Delete File
- **Image Compare** — select 2–4 images with Ctrl+click, then click "Compare" in the Bulk Bar to open a side-by-side lightbox; grid adapts to the number of selected images
@@ -483,12 +494,12 @@ An experimental batch generator: runs the workflow currently loaded in GenerateU
- **Tab layout (W / N / P / M / I / A)** — compact single-letter tabs with full name shown on hover
- **W — Workflows tab** — browse favorite workflows (All / ★ Favorites / Groups / By Badge sub-tabs), ★ star shown for favorites in All view
- **N — Nodes tab** — browse favorite nodes (All / ★ Favorites / Groups / Sets / 📂 Category / 🧩 Package sub-tabs), ★ star shown for favorites in All view
- **N — Nodes tab** — browse favorite nodes (All / ★ Favorites / Groups / Sets / 📂 Category / 🧩 Package sub-tabs), ★ star shown for favorites in All view; **Alt+click** a node to add it to (or remove it from) Favorites; the tab now opens without waiting for `/object_info` (v0.8.0)
- **Category sub-tab** — dropdown to filter nodes by top-level category
- **Package sub-tab** — dropdown to filter nodes by custom node package name
- **M — Models tab** — browse installed models (All / ★ Favorites / Groups / By Type sub-tabs); LoRA groups show an **All N LoRAs** item — drag to canvas to place a `Lora Loader (LoraManager)` node with all LoRAs pre-loaded
- **P — Prompts tab** — browse prompt presets with All / ★ Favorites / Categories sub-tabs; **Groups sub-tab** (row 2) — view presets by group (shared with the Batch tab's `wfm_prompt_preset_groups`)
- **I — Information tab** — drop a ComfyUI-generated PNG/WebP/MP4 or workflow JSON in the side panel to view its metadata; detects LoRAs from `LoraLoader`, `LoraLoaderModelOnly`, and `Lora Loader (LoraManager)` nodes (API format supported); supports `UnetLoaderGGUF` and `QuadrupleCLIPLoader` node types; preview area fixed at 110px
- **I — Information tab** — drop a ComfyUI-generated PNG/WebP/MP4 or workflow JSON in the side panel to view its metadata; detects LoRAs from `LoraLoader`, `LoraLoaderModelOnly`, `Lora Loader (LoraManager)`, and `ImageMetadataLoRALoader` nodes (API format supported); supports `UnetLoaderGGUF` and `QuadrupleCLIPLoader` node types; preview area fixed at 110px
- **MP4 embedded metadata** (v0.5.1) — reads the `workflow`/`prompt` container tags ComfyUI's `SaveVideo` writes into the mp4's `moov/udta/meta` box, parsed entirely client-side (same box-walking logic as the Gallery tab's Metadata sub-tab); also recognizes all-in-one video-generation nodes that carry their prompt directly on a `prompt` input rather than a separate `CLIPTextEncode`-style node (e.g. `MiniMaxH3ImageToVideo`)
- **A — AI TOOL tab** — Translation, Chat, TOOLS, and Settings sub-tabs powered by Ollama, LM Studio, Lemonade, or Unsloth; Chat supports multi-turn conversations (full history sent each turn); TOOLS includes VLM image analysis and wildcard generation; settings (backend, URL, model) shared with the SPA AI TOOL tab via `localStorage`
- **VRAM (Ollama) → Unload all now** (v0.7.13) — in the A tab's Settings sub-tab; unloads every model loaded in Ollama on localhost, decision models included (same as Workflow Studio's Settings → VRAM Management)
@@ -589,6 +600,7 @@ Click the **camera icon** (next to the W button) in ComfyUI's top bar to capture
- **[LM Studio](https://lmstudio.ai/)** — alternative backend for translation and VLM (OpenAI-compatible API)
- **[Lemonade Server](https://lemonade-server.ai/)** — alternative backend for translation, chat, and VLM (OpenAI-compatible API)
- **[Unsloth](https://unsloth.ai/)** — alternative backend for translation, chat, and VLM (OpenAI-compatible API); needs an API key set via `UNSLOTH_API_KEY` in a `.env` file, or Unsloth's Keyless API access → "Chat and inference" turned on — `pip install -r requirements.txt` for `python-dotenv`
- **Gemini API key / EmbeddingGemma 2** (v0.8.2) — only for the Gallery's Semantic Search: either set `GEMINI_API_KEY` in the plugin's `.env` (key from [Google AI Studio](https://aistudio.google.com); free tier available, paid beyond it), or use the local **EmbeddingGemma 2** (no key; `onnxruntime`, `tokenizers` and `huggingface_hub` — normally already present with ComfyUI; the model downloads on first use). `onnxruntime-gpu` makes the local model ≈45× faster on NVIDIA GPUs
- **[Eagle](https://eagle.cool/)** — for auto-saving generated images with metadata
- **[comfyui-mask-editor-one](https://github.com/ketle-man/comfyui-mask-editor-one) (v0.1.9+)** — enables BiRefNet background removal, SAM3 text-prompt segmentation, and ABR stamp-brush library in the Image Edit Mask tool; `birefnet.safetensors` must be placed in `ComfyUI/models/background_removal/` for BiRefNet
- **[psd-tools](https://pypi.org/project/psd-tools/)** (v0.6.0) — enables all PSD (Photoshop) features: Gallery bulk "Create PSD" export, Gallery browsing/thumbnails for `.psd` files, and Image Edit's Save PSD / Open PSD buttons; `pip install -r requirements.txt` (ships prebuilt wheels, no C compiler required); without it, PSD-related actions show an error toast with the install command instead of failing silently
+1 -1
View File
@@ -63,7 +63,7 @@ if ENV_FILE.exists():
except ImportError:
print(
"[WARNING] Workflow Studio: .env file found but python-dotenv is not installed "
"(pip install -r requirements.txt). UNSLOTH_API_KEY will not be loaded from .env."
"(pip install -r requirements.txt). UNSLOTH_API_KEY / GEMINI_API_KEY will not be loaded from .env."
)
except Exception as e:
print(f"[WARNING] Workflow Studio: failed to load .env file: {e}")
+170
View File
@@ -10,10 +10,12 @@ from aiohttp import web
from ..config import DATA_DIR
from ..services.gallery_service import GalleryService
from ..services.semantic_service import SemanticService, SemanticError, DEFAULT_DIM, MODEL as SEMANTIC_DEFAULT_MODEL
logger = logging.getLogger(__name__)
_service = GalleryService(DATA_DIR)
_semantic = SemanticService(DATA_DIR, _service)
def _init_allowed_root() -> None:
@@ -55,8 +57,22 @@ def _init_allowed_root() -> None:
_init_allowed_root()
# Windowsのレジストリ依存で音声のMIMEが返らない/不統一なため明示登録する(<audio>の再生に必要)
for _ext, _mime in ((".wav", "audio/wav"), (".mp3", "audio/mpeg"), (".ogg", "audio/ogg"),
(".opus", "audio/ogg"), (".m4a", "audio/mp4"), (".flac", "audio/flac")):
mimetypes.add_type(_mime, _ext)
def setup_routes(app: web.Application):
app.router.add_get("/wfm/gallery/folders", list_folders)
app.router.add_get("/wfm/gallery/semantic/status", semantic_status)
app.router.add_get("/wfm/gallery/semantic/folders", semantic_folders)
app.router.add_post("/wfm/gallery/semantic/index", semantic_index)
app.router.add_post("/wfm/gallery/semantic/cancel", semantic_cancel)
app.router.add_post("/wfm/gallery/semantic/search", semantic_search)
app.router.add_post("/wfm/gallery/semantic/similar", semantic_similar)
app.router.add_post("/wfm/gallery/semantic/clear", semantic_clear)
app.router.add_post("/wfm/gallery/semantic/test", semantic_test)
app.router.add_get("/wfm/gallery/images", list_images)
app.router.add_get("/wfm/gallery/image/meta", get_image_meta)
app.router.add_get("/wfm/gallery/image/workflow", get_image_workflow)
@@ -81,6 +97,7 @@ def setup_routes(app: web.Application):
app.router.add_get("/wfm/gallery/groups/{name}/images", list_group_images)
# バルク操作
app.router.add_post("/wfm/gallery/bulk/favorite", bulk_favorite)
app.router.add_post("/wfm/gallery/convert", convert_file_route)
app.router.add_post("/wfm/gallery/bulk/group", bulk_group)
# フォルダ・ファイル操作
app.router.add_post("/wfm/gallery/folder", create_folder_route)
@@ -94,6 +111,144 @@ def setup_routes(app: web.Application):
app.router.add_get("/wfm/gallery/image/psd-layers", get_psd_layers)
# ──────────────────────────────────────────────────────────────
# セマンティック検索 (Gemini Embedding 2)
# ──────────────────────────────────────────────────────────────
def _semantic_error(e: SemanticError) -> web.Response:
# code はフロントのi18n用。message は詳細(APIキー等の秘密は含まない)
return web.json_response({"error": str(e), "code": e.code}, status=400)
def _num(body: dict, key: str, default, lo, hi, cast=int):
"""リクエストの数値パラメータを検証して範囲内に収める。数値でなければ400相当のSemanticErrorにする"""
raw = body.get(key, default)
if raw is None or raw == "":
raw = default
try:
v = cast(raw)
except (TypeError, ValueError, OverflowError):
raise SemanticError("bad_param", key)
if v != v: # NaN
raise SemanticError("bad_param", key)
return max(lo, min(v, hi))
async def _json_body(request: web.Request) -> dict:
try:
body = await request.json()
return body if isinstance(body, dict) else {}
except Exception:
return {}
async def semantic_status(request: web.Request) -> web.Response:
folder = request.rel_url.query.get("folder", "")
recursive = request.rel_url.query.get("recursive", "true") == "true"
try:
return web.json_response(await asyncio.to_thread(_semantic.status, folder, recursive))
except Exception as e:
logger.error("semantic_status error: %s", e)
return web.json_response({"error": str(e)}, status=500)
async def semantic_folders(request: web.Request) -> web.Response:
root = request.rel_url.query.get("root", "")
try:
return web.json_response({"folders": await asyncio.to_thread(_semantic.folder_status, root)})
except SemanticError as e:
return _semantic_error(e)
except Exception as e:
logger.error("semantic_folders error: %s", e)
return web.json_response({"error": str(e)}, status=500)
def _apply_local_options(body: dict) -> None:
"""リクエストに実行デバイス/スレッド数が含まれていれば反映・保存する"""
if "device" in body:
_semantic.set_local_options(str(body.get("device", "auto")), _num(body, "threads", 0, 0, 256))
async def semantic_index(request: web.Request) -> web.Response:
body = await _json_body(request)
try:
_apply_local_options(body)
await asyncio.to_thread(
_semantic.start_index,
str(body.get("folder", "")),
bool(body.get("recursive", True)),
"all" if body.get("mode") == "all" else "missing",
str(body.get("model", SEMANTIC_DEFAULT_MODEL)),
_num(body, "dim", DEFAULT_DIM, 0, 100000),
_num(body, "soft", 280, 0, 100000),
)
return web.json_response({"ok": True})
except SemanticError as e:
return _semantic_error(e)
except Exception as e:
logger.error("semantic_index error: %s", e)
return web.json_response({"error": str(e)}, status=500)
async def semantic_cancel(request: web.Request) -> web.Response:
_semantic.cancel()
return web.json_response({"ok": True})
async def semantic_search(request: web.Request) -> web.Response:
body = await _json_body(request)
try:
limit = _num(body, "limit", 200, 1, 1000)
min_score = _num(body, "min_score", 0.0, -1.0, 1.0, float)
images = await asyncio.to_thread(
_semantic.search_text, str(body.get("query", "")), str(body.get("folder", "")),
bool(body.get("recursive", True)), limit, min_score,
)
return web.json_response({"images": images, "total": len(images)})
except SemanticError as e:
return _semantic_error(e)
except Exception as e:
logger.error("semantic_search error: %s", e)
return web.json_response({"error": str(e)}, status=500)
async def semantic_similar(request: web.Request) -> web.Response:
body = await _json_body(request)
try:
limit = _num(body, "limit", 200, 1, 1000)
min_score = _num(body, "min_score", 0.0, -1.0, 1.0, float)
images = await asyncio.to_thread(
_semantic.search_similar, str(body.get("path", "")), str(body.get("folder", "")),
bool(body.get("recursive", True)), limit, min_score,
)
return web.json_response({"images": images, "total": len(images)})
except SemanticError as e:
return _semantic_error(e)
except Exception as e:
logger.error("semantic_similar error: %s", e)
return web.json_response({"error": str(e)}, status=500)
async def semantic_clear(request: web.Request) -> web.Response:
try:
await asyncio.to_thread(_semantic.clear)
return web.json_response({"ok": True})
except SemanticError as e:
return _semantic_error(e)
async def semantic_test(request: web.Request) -> web.Response:
body = await _json_body(request)
try:
_apply_local_options(body)
res = await asyncio.to_thread(_semantic.test_connection, str(body.get("model", SEMANTIC_DEFAULT_MODEL)), _num(body, "dim", DEFAULT_DIM, 0, 100000))
return web.json_response({"ok": True, **res})
except SemanticError as e:
return _semantic_error(e)
except Exception as e:
return web.json_response({"error": str(e)}, status=500)
# ──────────────────────────────────────────────────────────────
# フォルダツリー
# ──────────────────────────────────────────────────────────────
@@ -235,6 +390,21 @@ async def serve_thumb(request: web.Request) -> web.Response:
# バルク操作
# ──────────────────────────────────────────────────────────────
async def convert_file_route(request: web.Request) -> web.Response:
"""POST /wfm/gallery/convert — 音声/動画の音声を別形式へ変換(1ファイルずつ呼ぶ)"""
try:
body = await request.json()
path = body.get("path", "")
if not path:
return web.json_response({"ok": False, "error": "path required"}, status=400)
# 変換はCPU負荷が高く同期実行するとイベントループ全体が止まるためexecutorで実行する
loop = asyncio.get_event_loop()
result = await loop.run_in_executor(None, _service.convert_file, path, body)
return web.json_response(result, status=200 if result.get("ok") else 400)
except Exception as e:
return web.json_response({"ok": False, "error": str(e)}, status=500)
async def bulk_favorite(request: web.Request) -> web.Response:
"""POST /wfm/gallery/bulk/favorite — 複数画像のお気に入りを一括設定"""
try:
+45
View File
@@ -15,6 +15,7 @@ _service = NodesService()
def setup_routes(app: web.Application):
"""Register all node management API routes."""
# Node metadata
app.router.add_get("/api/wfm/nodes/light", handle_get_light_nodes)
app.router.add_get("/api/wfm/nodes/metadata", handle_get_metadata)
app.router.add_post("/api/wfm/nodes/metadata", handle_save_metadata)
# Node groups
@@ -28,6 +29,50 @@ def setup_routes(app: web.Application):
app.router.add_get("/api/wfm/node-sets/export", handle_export_set)
# ── Light node list ────────────────────────────────────────
def _build_light_node_list() -> list[dict]:
"""全ノードの軽量情報(INPUT_TYPES を呼ばない)。
/object_info は各ノードの INPUT_TYPES() を呼ぶため、モデル一覧を走査するノードが
多い環境では十数秒かかる。Nodesタブの一覧表示に必要なのはクラス属性だけなので、
それらのみを読んで高速に返す(入力定義は呼び出し側が /object_info で後から補う)。
"""
import nodes as comfy_nodes
result = []
for name, cls in list(comfy_nodes.NODE_CLASS_MAPPINGS.items()):
try:
return_types = getattr(cls, "RETURN_TYPES", ()) or ()
result.append({
"name": name,
"display_name": comfy_nodes.NODE_DISPLAY_NAME_MAPPINGS.get(name, name),
"description": getattr(cls, "DESCRIPTION", "") or "",
"category": getattr(cls, "CATEGORY", "sd") or "sd",
"python_module": getattr(cls, "RELATIVE_PYTHON_MODULE", "nodes") or "nodes",
"output": list(return_types),
"output_name": list(getattr(cls, "RETURN_NAMES", None) or return_types),
"output_node": getattr(cls, "OUTPUT_NODE", False) is True,
"search_aliases": list(getattr(cls, "SEARCH_ALIASES", []) or []),
"deprecated": bool(getattr(cls, "DEPRECATED", False)),
"experimental": bool(getattr(cls, "EXPERIMENTAL", False)),
})
except Exception as e:
logger.debug("light node info failed for %s: %s", name, e)
return result
async def handle_get_light_nodes(request: web.Request) -> web.Response:
"""GET /api/wfm/nodes/light"""
try:
result = await asyncio.to_thread(_build_light_node_list)
return web.json_response(result)
except Exception as e:
logger.error("Error building light node list: %s", e)
return web.json_response({"error": str(e)}, status=500)
# ── Node Metadata ──────────────────────────────────────────
+93
View File
@@ -0,0 +1,93 @@
"""Audio conversion (and audio extraction from video) using PyAV.
Gallery の「Convert」機能のコア。PyAV(av)だけで完結し、システムの ffmpeg には依存しない。
"""
import logging
from pathlib import Path
logger = logging.getLogger(__name__)
# 出力フォーマット -> (拡張子, エンコーダ, 標準サンプル形式, ビットレート指定可否, タグ保持可否)
# .ogg は PyAV 同梱ビルドに Vorbis エンコーダが無いため出力対象外(入力は読める)。
# wav は任意キー(prompt/workflow)のタグを持てないため、メタデータ引き継ぎの対象外。
OUTPUT_FORMATS = {
"flac": {"ext": ".flac", "codec": "flac", "sample_fmt": "s16", "bitrate": False, "tags": True},
"mp3": {"ext": ".mp3", "codec": "libmp3lame", "sample_fmt": "fltp", "bitrate": True, "tags": True},
"opus": {"ext": ".opus", "codec": "libopus", "sample_fmt": "flt", "bitrate": True, "tags": True},
"m4a": {"ext": ".m4a", "codec": "aac", "sample_fmt": "fltp", "bitrate": True, "tags": True},
"wav": {"ext": ".wav", "codec": "pcm_s16le", "sample_fmt": "s16", "bitrate": False, "tags": False},
}
# 引き継ぐタグ(小文字で比較)。ComfyUIの prompt/workflow と一般的な曲情報のみ。
_CARRY_TAGS = {"prompt", "workflow", "title", "artist", "album", "date", "genre", "comment"}
_OPUS_RATES = (8000, 12000, 16000, 24000, 48000)
def supports_tags(fmt: str) -> bool:
return bool(OUTPUT_FORMATS.get(fmt, {}).get("tags"))
def _channel_layout(opt: str, src_channels: int) -> str:
if opt == "mono":
return "mono"
if opt == "stereo":
return "stereo"
return "mono" if src_channels == 1 else "stereo"
def convert_audio(src: Path, dst: Path, fmt: str, *, bitrate_kbps: int = 0,
sample_rate: int = 0, channels: str = "keep",
inherit_metadata: bool = True) -> dict:
"""src(音声または音声トラックを持つ動画)から dst へ変換する。
sample_rate=0 / channels="keep" は元の設定を維持。opusは48kHz固定扱い。
戻り値: {"tags_written": bool}(タグを持てない形式や元にタグが無い場合は False)
"""
import av
spec = OUTPUT_FORMATS[fmt]
tags_written = False
with av.open(str(src)) as in_c:
if not in_c.streams.audio:
raise ValueError("No audio stream in the source file")
in_stream = in_c.streams.audio[0]
rate = sample_rate or in_stream.rate or 44100
if fmt == "opus":
rate = min(_OPUS_RATES, key=lambda r: abs(r - rate)) if sample_rate else 48000
elif fmt == "mp3" and rate not in (8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000):
rate = 44100
layout = _channel_layout(channels, in_stream.channels)
options = {"movflags": "use_metadata_tags"} if fmt == "m4a" else {}
with av.open(str(dst), "w", options=options) as out_c:
# タグ: 引き継ぎ対象かつ形式がタグを持てる場合のみ
if inherit_metadata and spec["tags"]:
src_tags = dict(in_c.metadata or {})
for k, v in (in_stream.metadata or {}).items(): # Opus等はストリーム側にタグを持つ
src_tags.setdefault(k, v)
for k, v in src_tags.items():
if k.lower() in _CARRY_TAGS and isinstance(v, str) and v:
out_c.metadata[k.lower()] = v
tags_written = True
out_stream = out_c.add_stream(spec["codec"], rate=rate)
out_stream.layout = layout
if spec["bitrate"] and bitrate_kbps:
out_stream.bit_rate = int(bitrate_kbps) * 1000
resampler = av.AudioResampler(format=out_stream.format.name, layout=layout, rate=rate)
def _encode(frames):
for f in frames:
for pkt in out_stream.encode(f):
out_c.mux(pkt)
for frame in in_c.decode(in_stream):
_encode(resampler.resample(frame))
_encode(resampler.resample(None))
for pkt in out_stream.encode(None):
out_c.mux(pkt)
return {"tags_written": tags_written}
+283 -17
View File
@@ -11,6 +11,7 @@ import re
import struct
import threading
import time
from collections import OrderedDict
from pathlib import Path
from typing import Any
@@ -20,9 +21,26 @@ logger = logging.getLogger(__name__)
# メタデータ読み取り・サムネイル抽出は両形式で同じコードパスを通る)
VIDEO_EXTENSIONS = {".mp4", ".webm"}
# サポートする画像拡張子(動画はGalleryでは静止画と同じ一覧・配信経路を共有する。
# 音声拡張子(ComfyUIの音楽生成出力)。動画と同様にPyAVで再生時間・埋め込みメタデータを読む。
# サムネイルは波形画像を生成する。
AUDIO_EXTENSIONS = {".wav", ".mp3", ".ogg", ".m4a", ".flac", ".opus"}
# 波形サムネイルの版数(描画仕様を変えたら上げる)。static/js/gallery-tab.js の WAVE_THUMB_VERSION と同じ値にすること。
WAVE_THUMB_VERSION = "wave2"
_NB_INDEX_CACHE_MAX = 16
# 音声サムネイル(波形)の色。拡張子ごとに変えて、一覧で形式を見分けやすくする。
AUDIO_WAVE_COLORS = {
".mp3": (56, 189, 248), # blue
".wav": (74, 222, 128), # green
".flac": (251, 191, 36), # amber
".ogg": (192, 132, 252), # purple
".opus": (192, 132, 252), # purple(Ogg系は同色)
".m4a": (251, 113, 133), # rose
}
# サポートする画像拡張子(動画・音声はGalleryでは静止画と同じ一覧・配信経路を共有する。
# .psdはブラウザが直接レンダリングできないため、配信時に合成済みPNGへ変換する)
IMAGE_EXTENSIONS = {".png", ".jpg", ".jpeg", ".webp", ".gif", ".svg", ".psd"} | VIDEO_EXTENSIONS
IMAGE_EXTENSIONS = {".png", ".jpg", ".jpeg", ".webp", ".gif", ".svg", ".psd"} | VIDEO_EXTENSIONS | AUDIO_EXTENSIONS
# Canvas2D の globalCompositeOperation 文字列 -> psd_tools.constants.BlendMode 名
# (Image Edit タブの Layer.blendMode はCanvas2Dの合成モード文字列をそのまま保持している)
@@ -183,6 +201,10 @@ class _FolderCache:
class GalleryService:
_nb_index_cache: "OrderedDict[str, tuple]" = OrderedDict() # history.jsonlの索引キャッシュ(上限付き)
_nb_index_lock = threading.Lock()
_convert_lock = threading.Lock() # 変換は同時に1本だけ実行する
def __init__(self, data_dir: Path):
self.data_dir = data_dir
self.metadata_store = GalleryMetadataStore(data_dir / "gallery_metadata.json")
@@ -422,6 +444,21 @@ class GalleryService:
_walk(folder)
return entries
def make_list_item(self, name: str, abs_path: str, size: int, mtime: float) -> dict:
"""一覧アイテムdict(list_imagesと同形式)を作る。セマンティック検索の結果表示用。"""
meta = self.metadata_store.get(abs_path)
return {
"filename": name,
"path": abs_path,
"size": size,
"mtime": mtime,
"ext": Path(name).suffix.lower(),
"favorite": meta.get("favorite", False),
"tags": meta.get("tags", []),
"memo": meta.get("memo", ""),
"groups": meta.get("groups", []),
}
def list_images(
self,
folder_path: str,
@@ -586,14 +623,29 @@ class GalleryService:
if image_path.parent.name != NANOBANANA_FOLDER_NAME:
return None
history_path = image_path.parent / "history.jsonl"
if not history_path.is_file():
index = self._load_nanobanana_index(history_path)
return index.get(image_path.name) if index else None
def _load_nanobanana_index(self, history_path: Path) -> dict | None:
"""history.jsonl を「ファイル名→エントリ」の辞書にして返す。(mtime, size)をキーに
キャッシュし、メタデータ要求のたびの全読み込みを避ける。同名再生成は新しい行を優先する。"""
try:
st = history_path.stat()
except OSError:
return None
key = str(history_path)
sig = (st.st_mtime_ns, st.st_size)
with self._nb_index_lock:
cached = self._nb_index_cache.get(key)
if cached and cached[0] == sig:
self._nb_index_cache.move_to_end(key)
return cached[1]
try:
lines = history_path.read_text(encoding="utf-8", errors="replace").splitlines()
except OSError:
return None
filename = image_path.name
for line in reversed(lines):
index: dict = {}
for line in reversed(lines): # 新しい行から。最初に見つかったものを優先
line = line.strip()
if not line:
continue
@@ -601,9 +653,20 @@ class GalleryService:
entry = json.loads(line)
except json.JSONDecodeError:
continue
if filename in (entry.get("filenames") or []):
return entry
return None
if not isinstance(entry, dict): # 破損行([]・数値など)は無視
continue
filenames = entry.get("filenames")
if not isinstance(filenames, list):
continue
for name in filenames:
if isinstance(name, str) and name not in index:
index[name] = entry
with self._nb_index_lock:
self._nb_index_cache[key] = (sig, index)
self._nb_index_cache.move_to_end(key)
while len(self._nb_index_cache) > _NB_INDEX_CACHE_MAX:
self._nb_index_cache.popitem(last=False)
return index
def _find_vault_category_root(self, image_path: Path) -> Path | None:
"""image_path の祖先を辿り、直下に thumbnails(_option2) を持つフォルダ
@@ -693,8 +756,12 @@ class GalleryService:
embedded = self._read_png_metadata(path)
elif ext in {".jpg", ".jpeg"}:
embedded = self._read_jpeg_metadata(path)
elif ext == ".webp":
embedded = self._read_webp_metadata(path)
elif ext in VIDEO_EXTENSIONS:
embedded = self._read_mp4_metadata(path)
elif ext in AUDIO_EXTENSIONS:
embedded = self._read_audio_metadata(path)
dims = self._read_media_dimensions(path, ext)
saved = self.metadata_store.get(str(path))
@@ -742,8 +809,6 @@ class GalleryService:
chunk_type = f.read(4).decode("ascii", errors="ignore")
if chunk_len > _PNG_CHUNK_MAX:
f.seek(chunk_len + 4, 1)
if chunk_type == "IEND":
break
continue
chunk_data = f.read(chunk_len)
f.read(4) # CRC
@@ -763,16 +828,55 @@ class GalleryService:
rest = rest[null_idx2 + 1:]
null_idx3 = rest.index(b"\x00")
value = rest[null_idx3 + 1:].decode("utf-8", errors="replace")
result[key] = value
# 先勝ち: png_extractor.extract_png_workflow(ワークフロー読み込み)と
# 同じチャンクを採用し、Gallery表示との食い違いを防ぐ。
if key not in result:
result[key] = value
except (ValueError, UnicodeDecodeError):
pass
if chunk_type == "IEND":
break
# IENDで止めない: ComfyUI-Custom-Scripts等はIENDの後ろに
# workflowチャンクを追記するため、ファイル末尾まで読む。
except Exception as e:
logger.debug("PNG metadata read error: %s", e)
return result
def _read_webp_metadata(self, path: Path) -> dict:
"""WebPのRIFF EXIFチャンクから埋め込みworkflow/prompt(JSON文字列)を抽出する。
ComfyUIはWebPのEXIFに "workflow:{...}" / "prompt:{...}" 形式で書き込む
(static/js/metadata-tab.js の extractWorkflowFromEXIF と同じ処理)。"""
result = {}
try:
with open(path, "rb") as f:
head = f.read(12)
if len(head) < 12 or head[:4] != b"RIFF" or head[8:12] != b"WEBP":
return result
while True:
hdr = f.read(8)
if len(hdr) < 8:
break
fourcc = hdr[:4]
size = struct.unpack("<I", hdr[4:])[0]
if fourcc == b"EXIF":
if size > _PNG_CHUNK_MAX:
break
text = f.read(size).decode("utf-8", errors="replace")
decoder = json.JSONDecoder()
for key in ("workflow", "prompt"):
idx = text.find(key + ":{")
if idx < 0:
continue
start = idx + len(key) + 1
try:
_, end = decoder.raw_decode(text, start)
except json.JSONDecodeError:
continue
result[key] = text[start:end]
break
f.seek(size + (size & 1), 1)
except Exception as e:
logger.debug("WebP metadata read error: %s", e)
return result
def _read_jpeg_metadata(self, path: Path) -> dict:
"""JPEGのEXIF/commentからメタデータを抽出(簡易)"""
result = {}
@@ -812,6 +916,25 @@ class GalleryService:
logger.debug("MP4 metadata read error: %s", e)
return result
def _read_audio_metadata(self, path: Path) -> dict:
"""音声ファイルのタグ(ComfyUIのSaveAudio系は prompt/workflow をJSON文字列で書き込む)をPyAVで抽出する。
FLACのVorbisコメント等はキーの大文字小文字が実装依存のため、prompt/workflowは小文字キーでも引けるようにする。"""
result = {}
try:
import av
with av.open(str(path)) as container:
tags = dict(container.metadata or {})
for stream in container.streams.audio:
for k, v in (stream.metadata or {}).items():
tags.setdefault(k, v)
for k, v in tags.items():
result[k] = v
if k.lower() in ("workflow", "prompt"):
result.setdefault(k.lower(), v)
except Exception as e:
logger.debug("Audio metadata read error: %s", e)
return result
def _read_media_dimensions(self, path: Path, ext: str) -> dict:
"""画像は幅高さ、動画は幅高さ+再生時間(秒)を返す。取得できない項目はNone。"""
info: dict = {"width": None, "height": None, "duration": None}
@@ -825,6 +948,11 @@ class GalleryService:
info["height"] = stream.height
if container.duration:
info["duration"] = container.duration / 1_000_000
elif ext in AUDIO_EXTENSIONS:
import av
with av.open(str(path)) as container:
if container.duration:
info["duration"] = container.duration / 1_000_000
elif ext == ".psd":
from psd_tools import PSDImage
psd = PSDImage.open(path)
@@ -855,8 +983,12 @@ class GalleryService:
embedded = None
if ext == ".png":
embedded = self._read_png_metadata(path)
elif ext == ".webp":
embedded = self._read_webp_metadata(path)
elif ext in VIDEO_EXTENSIONS:
embedded = self._read_mp4_metadata(path)
elif ext in AUDIO_EXTENSIONS:
embedded = self._read_audio_metadata(path)
if embedded:
key_groups = (("workflow", "Workflow"), ("prompt", "Prompt"))
@@ -889,6 +1021,87 @@ class GalleryService:
# メタデータ保存
# ──────────────────────────────────────────────────────────────
# ──────────────────────────────────────────────────────────────
# ファイル変換(音声変換 / 動画からの音声抽出)
# ──────────────────────────────────────────────────────────────
def convert_file(self, image_path: str, opts: dict) -> dict:
"""音声ファイル(または動画の音声トラック)を指定形式へ変換し、同じフォルダに別ファイルとして保存する。
opts: format(flac/mp3/opus/m4a/wav), bitrate(kbps), sample_rate(0=元のまま), channels(keep/mono/stereo),
inherit_metadata(prompt/workflow等のタグ引き継ぎ。タグを持てない形式は対象外),
copy_gallery_meta(お気に入り/タグ/メモ/グループの引き継ぎ), delete_original(変換成功後に元を削除)
"""
from .convert_service import OUTPUT_FORMATS, convert_audio, supports_tags
p = Path(image_path).resolve()
if not p.is_file():
return {"ok": False, "error": "File not found"}
if p.suffix.lower() not in AUDIO_EXTENSIONS | VIDEO_EXTENSIONS:
return {"ok": False, "error": "Not an audio/video file"}
if not self._check_path_allowed(p):
return {"ok": False, "error": "Access denied"}
fmt = str(opts.get("format", ""))
if fmt not in OUTPUT_FORMATS:
return {"ok": False, "error": f"Unsupported format: {fmt}"}
def _int(key: str, default: int = 0) -> int:
try:
return max(0, int(opts.get(key) or default))
except (TypeError, ValueError):
return default
ext = OUTPUT_FORMATS[fmt]["ext"]
inherit = bool(opts.get("inherit_metadata", True))
# 出力名の決定〜変換完了までを同一ロック内で行う(同時リクエストで同じ名前を選んで
# 上書きし合うのを防ぐ)。エラー時に消すのは自分が決めたdstだけ。
with self._convert_lock:
dst = p.with_suffix(ext)
if dst.exists(): # 同名(同形式への再変換を含む)は上書きせず連番
counter = 1
while True:
dst = p.with_name(f"{p.stem}_converted{'' if counter == 1 else counter}{ext}")
if not dst.exists():
break
counter += 1
try:
result = convert_audio(
p, dst, fmt,
bitrate_kbps=_int("bitrate"),
sample_rate=_int("sample_rate"),
channels=str(opts.get("channels", "keep")),
inherit_metadata=inherit,
)
except Exception as e:
logger.warning("convert_file: failed for %s: %s", p, e)
try:
dst.unlink(missing_ok=True)
except OSError:
pass
return {"ok": False, "error": str(e)}
dst_str = str(dst.resolve()).replace("\\", "/")
if opts.get("copy_gallery_meta", True):
saved = self.metadata_store.get(str(p))
carry = {k: saved[k] for k in ("favorite", "tags", "memo", "groups") if saved.get(k)}
if carry:
self.metadata_store.save(str(dst.resolve()), carry)
self._folder_cache.invalidate(p.parent)
deleted = False
if opts.get("delete_original"):
deleted = bool(self.delete_images([str(p)])["deleted"])
return {
"ok": True,
"path": dst_str,
"filename": dst.name,
"tags_written": result["tags_written"],
"tags_skipped": inherit and not supports_tags(fmt),
"deleted": deleted,
}
def save_image_meta(self, image_path: str, data: dict) -> bool:
resolved = Path(image_path).resolve()
if not self._check_path_allowed(resolved):
@@ -1116,8 +1329,8 @@ class GalleryService:
p = Path(img_path).resolve()
if not p.is_file() or p.suffix.lower() not in IMAGE_EXTENSIONS:
continue
if p.suffix.lower() in VIDEO_EXTENSIONS:
continue # 動画はPSDレイヤーにできない
if p.suffix.lower() in VIDEO_EXTENSIONS | AUDIO_EXTENSIONS:
continue # 動画・音声はPSDレイヤーにできない
if not self._check_path_allowed(p):
continue
try:
@@ -1341,6 +1554,48 @@ class GalleryService:
logger.warning("serve_image: psd composite failed for %s: %s", p, e)
return None
def _render_audio_waveform(self, path: Path, out_path: Path, size: int) -> None:
"""音声ファイルの波形を size×size のJPEGとして out_path に書き出す。
8kHzモノラルへリサンプルしてから列ごとの最大振幅を描く(長尺でも軽く保つ)。"""
import av
import numpy as np
from PIL import Image, ImageDraw
max_samples = 8000 * 600 # 先頭10分まで
chunks = []
total = 0
with av.open(str(path)) as container:
stream = container.streams.audio[0]
resampler = av.AudioResampler(format="flt", layout="mono", rate=8000)
for frame in container.decode(stream):
for rf in resampler.resample(frame):
arr = rf.to_ndarray().reshape(-1)
chunks.append(arr)
total += arr.size
if total >= max_samples:
break
data = np.concatenate(chunks) if chunks else np.zeros(1, dtype=np.float32)
data = np.abs(data[:max_samples])
cols = size
edges = np.linspace(0, data.size, cols + 1).astype(int)
peaks = np.array([
data[edges[i]:max(edges[i + 1], edges[i] + 1)].max() if edges[i] < data.size else 0.0
for i in range(cols)
])
top = float(peaks.max())
if top > 0:
peaks = peaks / top
color = AUDIO_WAVE_COLORS.get(path.suffix.lower(), (56, 189, 248))
img = Image.new("RGB", (size, size), (24, 28, 38))
draw = ImageDraw.Draw(img)
mid = size / 2
for x, v in enumerate(peaks):
h = max(1.0, float(v) * (size * 0.42))
draw.line([(x, mid - h), (x, mid + h)], fill=color)
img.save(out_path, "JPEG", quality=85, optimize=True)
def serve_thumbnail(self, image_path: str, width: int = 256) -> Path | None:
"""縮小サムネイルのPathを返す。
ディスクキャッシュがあればそれを返し、なければPillowで生成して保存する。
@@ -1373,7 +1628,9 @@ class GalleryService:
except OSError:
return None
cache_key = hashlib.md5(f"{p}:{mtime}:{width}".encode()).hexdigest()
# 音声は波形の描画仕様(色)を変えたらキャッシュを作り直せるよう版数をキーに含める
wave_ver = f":{WAVE_THUMB_VERSION}" if p.suffix.lower() in AUDIO_EXTENSIONS else ""
cache_key = hashlib.md5(f"{p}:{mtime}:{width}{wave_ver}".encode()).hexdigest()
cache_dir = self.data_dir / "thumb_cache"
cache_dir.mkdir(exist_ok=True)
thumb_path = cache_dir / f"{cache_key}.jpg"
@@ -1381,6 +1638,15 @@ class GalleryService:
if thumb_path.exists():
return thumb_path
# 音声はPillowで開けない。PyAVでデコードして波形をJPEGに描画しキャッシュする。
if p.suffix.lower() in AUDIO_EXTENSIONS:
try:
self._render_audio_waveform(p, thumb_path, width)
return thumb_path
except Exception as e:
logger.warning("serve_thumbnail: audio waveform failed for %s: %s", p, e)
return None
# 動画(mp4/webm)はPillowで開けない。PyAV(av)で先頭フレームを抽出しJPEGとしてキャッシュする。
# avが未導入/デコード失敗の場合、元ファイルは<img>的な経路に渡せないため
# (GIF/SVGと違い元ファイルへフォールバックできない) Noneを返しプレースホルダー表示に委ねる。
+1 -2
View File
@@ -44,8 +44,7 @@ def extract_png_workflow(buf):
except Exception:
pass
# IENDで止めない: IENDの後ろに追記されたworkflowチャンクも読む
pos += length + 12
if chunk_type == "IEND":
break
return None
+239
View File
@@ -0,0 +1,239 @@
"""
EmbeddingGemma 2 ローカル実行 (onnx-community/embeddinggemma-2-ONNX)
onnxruntime + tokenizers + huggingface_hub だけで動かす(transformers の対応を待たない)。
前処理は Transformers.js の Gemma4ImageProcessor / EmbeddingGemma2Processor の移植:
画像 -> アスペクト比維持でリサイズ(48px倍数, 最大2520パッチ) -> 16pxパッチ化 -> vision_encoder
-> image_features を text model の <|image|> トークン位置に差し込み -> sentence_embedding(768)
モデルは初回のみ固定リポジトリからダウンロードする(約300MB, q4)。
"""
import logging
import math
import os
import threading
from pathlib import Path
import numpy as np
logger = logging.getLogger(__name__)
REPO_ID = "onnx-community/embeddinggemma-2-ONNX"
LOCAL_MODEL = "embeddinggemma-2"
LOCAL_DIMS = (128, 256, 512, 768) # MRL対応次元
DEFAULT_LOCAL_DIM = 768
# 量子化精度 -> ファイル名サフィックス
DTYPES = {"q4": "_q4", "q8": "_quantized", "fp16": "_fp16", "fp32": ""}
DEFAULT_DTYPE = "q4"
_PATCH = 16
_POOL = 3
SOFT_TOKENS = (70, 140, 280) # 画像1枚あたりのソフトトークン数(多いほど高精度・低速)
DEFAULT_SOFT = 280
_MAX_TOKENS = 1024 # テキスト入力のトークン数上限(画像用プロンプトは約290トークン)
_MAX_IMAGE_SIDE = 2048 # 画像の長辺上限(超える場合は縮小してから処理)
_IMAGE_TOKEN = "<|image|>"
_BOI = "<|image>"
_EOI = "<image|>"
DEVICES = ("auto", "cpu", "cuda", "dml")
_PROVIDER_BY_DEVICE = {"cuda": "CUDAExecutionProvider", "dml": "DmlExecutionProvider"}
_lock = threading.Lock()
_runtime = None # ((dtype, device, threads), _Runtime)
_opts = {"device": "auto", "threads": 0} # 実行デバイス/CPUスレッド数(configureで更新)
def configure(device: str = "auto", threads: int = 0) -> None:
"""ローカル推論の実行設定。次回のensure_runtimeから反映(device/threads変更時はセッション再作成)。"""
_opts["device"] = device if device in DEVICES else "auto"
_opts["threads"] = max(0, min(int(threads), (os.cpu_count() or 1) * 2))
def current_config() -> dict:
return dict(_opts)
def release() -> None:
"""セッションを破棄してVRAM/RAMを解放する(インデックス作成後など)。次回利用時に再ロードされる。"""
global _runtime
with _lock:
_runtime = None
def active_provider() -> str:
"""直近にロードしたセッションの実行プロバイダ(未ロードなら空)"""
rt = _runtime[1] if _runtime else None
return rt.providers[0].replace("ExecutionProvider", "") if rt else ""
def available_devices() -> dict:
"""onnxruntime が使える実行デバイス。GPU用ビルド(onnxruntime-gpu / -directml)が無ければcpuのみ。"""
try:
import onnxruntime as ort
prov = ort.get_available_providers()
version = ort.__version__
except Exception:
return {"cpu": True, "cuda": False, "dml": False, "ort_version": ""}
return {"cpu": True, "cuda": "CUDAExecutionProvider" in prov, "dml": "DmlExecutionProvider" in prov,
"ort_version": version, "cpu_count": os.cpu_count() or 1}
def default_threads() -> int:
"""既定のCPUスレッド数: 画像生成と同居しても邪魔になりにくいよう、コアの半分(最低1)"""
return max(1, (os.cpu_count() or 2) // 2)
def _target_size(h: int, w: int, max_patches: int) -> tuple[int, int]:
"""Gemma4ImageProcessor の get_aspect_ratio_preserving_size"""
factor = math.sqrt(max_patches * _PATCH ** 2 / (h * w))
side = _POOL * _PATCH
th = int(math.floor(factor * h / side)) * side
tw = int(math.floor(factor * w / side)) * side
max_side = (max_patches // _POOL ** 2) * side
if th == 0 and tw == 0:
raise ValueError("image too small")
if th == 0:
th, tw = side, min((w // h) * side, max_side)
elif tw == 0:
tw, th = side, min((h // w) * side, max_side)
return th, tw
class _Runtime:
def __init__(self, model_dir: Path, dtype: str, device: str = "auto", threads: int = 0):
import onnxruntime as ort
from tokenizers import Tokenizer
sfx = DTYPES[dtype]
so = ort.SessionOptions()
so.log_severity_level = 3 # CUDA使用時のMemcpy警告などを抑止
if threads > 0:
so.intra_op_num_threads = threads
so.inter_op_num_threads = 1
avail = ort.get_available_providers()
if device in _PROVIDER_BY_DEVICE:
providers = [_PROVIDER_BY_DEVICE[device]] if _PROVIDER_BY_DEVICE[device] in avail else []
elif device == "cpu":
providers = []
else: # auto: 使えるGPUを優先
providers = [p for p in ("CUDAExecutionProvider", "DmlExecutionProvider") if p in avail]
if "DmlExecutionProvider" in providers:
# DirectMLは並列実行/メモリパターン最適化に非対応
so.enable_mem_pattern = False
so.execution_mode = ort.ExecutionMode.ORT_SEQUENTIAL
providers.append("CPUExecutionProvider")
self.providers = providers
def make(name):
path = str(model_dir / "onnx" / f"{name}{sfx}.onnx")
try:
return ort.InferenceSession(path, so, providers=providers)
except Exception as e:
if providers == ["CPUExecutionProvider"]:
raise
logger.warning("EmbeddingGemma2: %s failed on %s (%s); falling back to CPU", name, providers[0], e)
self.providers = ["CPUExecutionProvider"]
return ort.InferenceSession(path, so, providers=["CPUExecutionProvider"])
self.text = make("model")
self.vision = make("vision_encoder")
self.tok = Tokenizer.from_file(str(model_dir / "tokenizer.json"))
self.tok.no_truncation()
self.tok.no_padding()
def _run_text(self, text: str, image_features: np.ndarray | None) -> np.ndarray:
ids = np.asarray([self.tok.encode(text).ids[:_MAX_TOKENS]], dtype=np.int64)
feeds = {
"input_ids": ids,
"attention_mask": np.ones_like(ids),
"image_features": image_features if image_features is not None else np.zeros((0, 512), np.float32),
"video_features": np.zeros((0, 512), np.float32),
"audio_features": np.zeros((0, 512), np.float32),
}
return self.text.run(["sentence_embedding"], feeds)[0][0]
def embed_text(self, text: str, prefix: str) -> np.ndarray:
return self._run_text(f"{prefix}{text.strip()}", None)
def embed_image(self, image, soft_tokens: int = DEFAULT_SOFT) -> np.ndarray:
from PIL import Image
im = image.convert("RGB")
if im.width < 1 or im.height < 1:
raise ValueError("empty image")
if max(im.size) > _MAX_IMAGE_SIDE: # 巨大画像でメモリを使い切らないよう先に縮小
im.thumbnail((_MAX_IMAGE_SIDE, _MAX_IMAGE_SIDE), Image.LANCZOS)
max_patches = soft_tokens * _POOL ** 2
th, tw = _target_size(im.height, im.width, max_patches)
if (th, tw) != (im.height, im.width):
im = im.resize((tw, th), Image.BICUBIC)
arr = np.asarray(im, dtype=np.float32) * (1.0 / 255.0) # H,W,3
ph, pw = th // _PATCH, tw // _PATCH
n = ph * pw
# (pH, dy, pW, dx, C) -> (pH, pW, dy, dx, C) -> (n, 768)
patches = arr.reshape(ph, _PATCH, pw, _PATCH, 3).transpose(0, 2, 1, 3, 4).reshape(n, _PATCH * _PATCH * 3)
# パディング無し(n個ちょうど)で渡す: 計算量が減り、パディングは出力に影響しない
pixel = patches[None].astype(np.float32)
pos = np.zeros((1, n, 2), dtype=np.int64)
rows, cols = np.divmod(np.arange(n), pw)
pos[0, :, 0], pos[0, :, 1] = cols, rows
feats = self.vision.run(["image_features"], {"pixel_values": pixel, "pixel_position_ids": pos})[0]
text = f"{_BOI}{_IMAGE_TOKEN * (n // _POOL ** 2)}{_EOI}"
return self._run_text(text, feats.astype(np.float32))
def model_dir(data_dir: Path) -> Path:
return data_dir / "models" / LOCAL_MODEL
def is_downloaded(data_dir: Path, dtype: str = DEFAULT_DTYPE) -> bool:
d, sfx = model_dir(data_dir), DTYPES.get(dtype, "_q4")
need = [d / "tokenizer.json"] + [d / "onnx" / f"{n}{sfx}.onnx{e}" for n in ("model", "vision_encoder") for e in ("", "_data")]
return all(p.exists() for p in need)
def ensure_runtime(data_dir: Path, dtype: str = DEFAULT_DTYPE, progress=None,
device: str | None = None, threads: int | None = None) -> _Runtime:
"""モデルをダウンロード(未取得時)してセッションを返す。dtype/device/threads変更時は作り直す。"""
global _runtime
if dtype not in DTYPES:
dtype = DEFAULT_DTYPE
device = _opts["device"] if device is None else device
threads = _opts["threads"] if threads is None else threads
if device not in DEVICES:
device = "auto"
key = (dtype, device, int(threads))
with _lock:
if _runtime and _runtime[0] == key:
return _runtime[1]
d, sfx = model_dir(data_dir), DTYPES[dtype]
if not is_downloaded(data_dir, dtype):
if progress:
progress("downloading")
from huggingface_hub import hf_hub_download
files = ["tokenizer.json"] + [f"onnx/{n}{sfx}.onnx{e}" for n in ("model", "vision_encoder") for e in ("", "_data")]
for f in files:
hf_hub_download(REPO_ID, f, local_dir=str(d))
if progress:
progress("loading")
_runtime = (key, _Runtime(d, dtype, device, int(threads)))
return _runtime[1]
def _finish(vec: np.ndarray, dim: int) -> np.ndarray:
v = np.asarray(vec, dtype=np.float32)
if dim < v.shape[0]:
v = v[:dim]
n = float(np.linalg.norm(v))
return v / n if n > 0 else v
def embed_text(data_dir: Path, text: str, dim: int, dtype: str = DEFAULT_DTYPE) -> np.ndarray:
rt = ensure_runtime(data_dir, dtype)
return _finish(rt.embed_text(text[:8000], "task: search result | query: "), dim)
def embed_image(data_dir: Path, image, dim: int, soft: int = DEFAULT_SOFT, dtype: str = DEFAULT_DTYPE) -> np.ndarray:
rt = ensure_runtime(data_dir, dtype)
return _finish(rt.embed_image(image, soft if soft in SOFT_TOKENS else DEFAULT_SOFT), dim)
+513
View File
@@ -0,0 +1,513 @@
"""
Semantic Service - Gallery用セマンティック検索(Gemini Embedding 2)
画像(動画・PSDはサムネイル)をGemini Embedding APIでベクトル化してローカルに保存し、
テキストクエリ/類似画像でコサイン類似度検索する。インデックス構築後の検索は
(テキストクエリの埋め込み1回を除き)ローカル処理でAPI費用はかからない。
APIキーは環境変数 GEMINI_API_KEY (または GOOGLE_API_KEY) から読む(.env経由)。
settings.json やフロントエンドには出さない。接続先は固定ホストのみ(SSRF対策)。
"""
import io
import json
import logging
import os
import threading
import time
import urllib.error
import urllib.request
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
import numpy as np
from . import semantic_local as local
logger = logging.getLogger(__name__)
API_BASE = "https://generativelanguage.googleapis.com/v1beta"
MODEL = "gemini-embedding-2" # Gemini API (クラウド)
LOCAL_MODEL = local.LOCAL_MODEL # EmbeddingGemma 2 (ローカルONNX)
DEFAULT_DIM = 1536
VALID_DIMS = (768, 1536, 3072)
# バックエンド(モデル名) -> 選択可能な出力次元
DIMS_BY_MODEL = {MODEL: VALID_DIMS, LOCAL_MODEL: local.LOCAL_DIMS}
DEFAULT_DIM_BY_MODEL = {MODEL: DEFAULT_DIM, LOCAL_MODEL: local.DEFAULT_LOCAL_DIM}
IMAGE_MAX_SIZE = 768 # 送信前の長辺リサイズ(px)
_MAX_QUERY_CHARS = 2000 # 検索文の最大長
_WORKERS = 4 # 並列API呼び出し数
_CHECKPOINT_EVERY = 100 # この件数ごとにディスクへ保存
_RETRIES = 4
# PILで直接開ける(静止画/GIFは先頭フレーム)
_PIL_EXTS = {".png", ".jpg", ".jpeg", ".webp", ".gif", ".bmp"}
# GalleryService.serve_thumbnail 経由でJPEG化する(動画先頭フレーム/PSD合成)
_THUMB_EXTS = {".mp4", ".webm", ".psd"}
INDEXABLE_EXTS = _PIL_EXTS | _THUMB_EXTS
class SemanticError(Exception):
"""ユーザーに見せられるエラー(code はフロントのi18n用)"""
def __init__(self, code: str, message: str = ""):
super().__init__(message or code)
self.code = code
def get_api_key() -> str:
return (os.environ.get("GEMINI_API_KEY", "") or os.environ.get("GOOGLE_API_KEY", "")).strip()
def _normalize(v: np.ndarray) -> np.ndarray:
n = float(np.linalg.norm(v))
return v / n if n > 0 else v
def _call_embed(content: dict, task_type: str, dim: int) -> np.ndarray:
key = get_api_key()
if not key:
raise SemanticError("no_api_key", "GEMINI_API_KEY is not set")
body = {"model": f"models/{MODEL}", "content": content, "taskType": task_type}
if dim != 768:
body["outputDimensionality"] = dim
data = json.dumps(body).encode("utf-8")
url = f"{API_BASE}/models/{MODEL}:embedContent"
last_err = ""
for attempt in range(_RETRIES + 1):
req = urllib.request.Request(
url, data=data, method="POST",
headers={"Content-Type": "application/json", "x-goog-api-key": key},
)
try:
with urllib.request.urlopen(req, timeout=60) as res:
payload = json.loads(res.read().decode("utf-8"))
values = (payload.get("embedding") or {}).get("values")
if not values:
raise SemanticError("bad_response", "no embedding.values in response")
return _normalize(np.asarray(values, dtype=np.float32))
except urllib.error.HTTPError as e:
try:
msg = json.loads(e.read().decode("utf-8")).get("error", {}).get("message", "")
except Exception:
msg = ""
last_err = f"HTTP {e.code}: {msg or e.reason}"
if e.code in (400, 403) and ("API key" in msg or "API_KEY" in msg or e.code == 403):
raise SemanticError("api_key_invalid", last_err)
if e.code in (429, 500, 502, 503, 504) and attempt < _RETRIES:
time.sleep(1.5 * (2 ** attempt))
continue
raise SemanticError("api_error", last_err)
except (urllib.error.URLError, TimeoutError, ConnectionError) as e:
last_err = str(getattr(e, "reason", e))
if attempt < _RETRIES:
time.sleep(1.5 * (attempt + 1))
continue
raise SemanticError("network_error", last_err)
raise SemanticError("api_error", last_err)
def embed_text(text: str, dim: int) -> np.ndarray:
return _call_embed({"parts": [{"text": text[:8000]}]}, "RETRIEVAL_QUERY", dim)
def embed_image_bytes(data: bytes, mime: str, dim: int) -> np.ndarray:
import base64
b64 = base64.b64encode(data).decode("ascii")
return _call_embed({"parts": [{"inlineData": {"mimeType": mime, "data": b64}}]}, "RETRIEVAL_DOCUMENT", dim)
class SemanticService:
def __init__(self, data_dir: Path, gallery_service):
self.data_dir = data_dir
self.gallery = gallery_service
self._file = data_dir / "semantic_index.npz"
self._lock = threading.RLock()
self._vecs: dict[str, np.ndarray] = {} # path -> 正規化済みベクトル
self._stamp: dict[str, tuple[int, float]] = {} # path -> (size, mtime)
self._dim = 0 # インデックスの次元(0=空)
self._model = MODEL # インデックスを作ったモデル
self._soft = local.DEFAULT_SOFT # ローカルモデルの画像ソフトトークン数(インデックスと一致させる)
self._settings_file = data_dir / "semantic_settings.json"
self._load_settings()
self._mat_cache = None # (paths, matrix)
self._loaded = False
self._dirty = 0
# ジョブ
self._job = self._idle_job()
self._cancel = threading.Event()
self._thread: threading.Thread | None = None
# ── ローカル推論の実行設定(デバイス/スレッド数) ─────────────
def _load_settings(self) -> None:
try:
if self._settings_file.exists():
d = json.loads(self._settings_file.read_text(encoding="utf-8"))
local.configure(str(d.get("device", "auto")), int(d.get("threads", 0)))
return
except Exception as e:
logger.warning("SemanticService: failed to load settings: %s", e)
local.configure("auto", local.default_threads())
def set_local_options(self, device: str, threads: int) -> None:
"""実行デバイス/CPUスレッド数を反映して保存する(検索時のクエリ埋め込みにも使われる)"""
local.configure(device, threads)
try:
self._settings_file.write_text(json.dumps(local.current_config()), encoding="utf-8")
except Exception as e:
logger.warning("SemanticService: failed to save settings: %s", e)
# ── 永続化 ─────────────────────────────────────────────
@staticmethod
def _idle_job() -> dict:
return {"running": False, "total": 0, "done": 0, "failed": 0, "current": "",
"error": "", "error_code": "", "cancelled": False, "finished": False}
def _ensure_loaded(self) -> None:
with self._lock:
if self._loaded:
return
self._loaded = True
if not self._file.exists():
return
try:
with np.load(self._file, allow_pickle=False) as z:
model = str(z["model"])
if model not in DIMS_BY_MODEL:
return
self._model = model
self._soft = int(z["soft"]) if "soft" in z.files else local.DEFAULT_SOFT
paths = [str(p) for p in z["paths"]]
sizes, mtimes, vecs = z["sizes"], z["mtimes"], z["vecs"]
self._dim = int(z["dim"])
for i, p in enumerate(paths):
self._vecs[p] = vecs[i]
self._stamp[p] = (int(sizes[i]), float(mtimes[i]))
except Exception as e:
logger.warning("SemanticService: failed to load index: %s", e)
self._vecs.clear()
self._stamp.clear()
self._dim = 0
def _save(self) -> None:
with self._lock:
if not self._vecs:
if self._file.exists():
self._file.unlink()
self._dirty = 0
return
paths = list(self._vecs.keys())
tmp = self._file.with_suffix(".tmp.npz")
np.savez(
tmp,
model=np.array(self._model), dim=np.array(self._dim), soft=np.array(self._soft),
paths=np.array(paths),
sizes=np.array([self._stamp[p][0] for p in paths], dtype=np.int64),
mtimes=np.array([self._stamp[p][1] for p in paths], dtype=np.float64),
vecs=np.stack([self._vecs[p] for p in paths]).astype(np.float32),
)
os.replace(tmp, self._file)
self._dirty = 0
def _put(self, path: str, vec: np.ndarray, size: int, mtime: float) -> None:
with self._lock:
self._vecs[path] = vec
self._stamp[path] = (size, mtime)
self._mat_cache = None
self._dirty += 1
# ── 状態 ───────────────────────────────────────────────
def _scope_entries(self, folder: str, recursive: bool) -> list[tuple[str, str, int, float]]:
p = Path(folder).resolve()
if not p.is_dir() or not self.gallery._check_path_allowed(p):
raise SemanticError("folder_not_allowed", str(folder))
raw = self.gallery._scan_folder_recursive(p) if recursive else self.gallery._scan_folder(p)
return [e for e in raw if Path(e[0]).suffix.lower() in INDEXABLE_EXTS]
def status(self, folder: str = "", recursive: bool = True) -> dict:
self._ensure_loaded()
out = {
"has_key": bool(get_api_key()),
"model": MODEL,
"backends": {
MODEL: {"dims": list(VALID_DIMS), "default_dim": DEFAULT_DIM, "ready": bool(get_api_key())},
LOCAL_MODEL: {"dims": list(local.LOCAL_DIMS), "default_dim": local.DEFAULT_LOCAL_DIM,
"ready": True, "downloaded": local.is_downloaded(self.data_dir)},
},
"index_model": self._model,
"index_dim": self._dim,
"index_soft": self._soft,
"local": {
"devices": local.available_devices(),
"options": local.current_config(),
"default_threads": local.default_threads(),
"soft_tokens": list(local.SOFT_TOKENS),
"default_soft": local.DEFAULT_SOFT,
},
"indexed_total": len(self._vecs),
"job": dict(self._job),
}
if folder:
try:
entries = self._scope_entries(folder, recursive)
with self._lock:
done = sum(1 for _, ap, sz, mt in entries if self._is_fresh(ap, sz, mt))
out["scope_total"] = len(entries)
out["scope_indexed"] = done
except SemanticError:
pass
return out
def folder_status(self, root: str) -> dict:
"""フォルダごとの索引状況 {絶対パス: {"indexable": n, "indexed": m}} (直下の画像のみ。Galleryツリーの枚数表示と同じ単位)"""
self._ensure_loaded()
entries = self._scope_entries(root, True)
out: dict[str, dict] = {}
with self._lock:
for _, ap, sz, mt in entries:
d = out.setdefault(ap.rsplit("/", 1)[0], {"indexable": 0, "indexed": 0})
d["indexable"] += 1
if self._is_fresh(ap, sz, mt):
d["indexed"] += 1
return out
def _is_fresh(self, path: str, size: int, mtime: float) -> bool:
st = self._stamp.get(path)
return st is not None and st[0] == size and abs(st[1] - mtime) < 1e-3
# ── インデックス作成ジョブ ────────────────────────────
def start_index(self, folder: str, recursive: bool, mode: str, model: str, dim: int,
soft: int = local.DEFAULT_SOFT) -> None:
self._ensure_loaded()
if model not in DIMS_BY_MODEL:
raise SemanticError("bad_model", model)
if model == MODEL and not get_api_key():
raise SemanticError("no_api_key")
if dim not in DIMS_BY_MODEL[model]:
raise SemanticError("bad_dim", str(dim))
if model != LOCAL_MODEL or soft not in local.SOFT_TOKENS:
soft = local.DEFAULT_SOFT # ソフトトークン数はローカルモデルのみの設定
with self._lock:
if self._job["running"]:
raise SemanticError("already_running")
entries = self._scope_entries(folder, recursive)
mismatch = bool(self._vecs) and (self._dim != dim or self._model != model or self._soft != soft)
if mismatch and mode != "all":
raise SemanticError("dim_mismatch", f"{self._model}/{self._dim}/{self._soft}")
if mode == "all":
# 範囲内の既存ベクトルを破棄して作り直す(モデル/次元違いなら全消去)
if mismatch:
self._vecs.clear()
self._stamp.clear()
else:
for _, ap, _, _ in entries:
self._vecs.pop(ap, None)
self._stamp.pop(ap, None)
self._mat_cache = None
self._dim = dim
self._model = model
self._soft = soft
todo = [e for e in entries if not self._is_fresh(e[1], e[2], e[3])]
# スコープ内で消えたファイルの残骸を掃除(スコープ配下のキーのみ)
self._prune_missing(folder, recursive, {e[1] for e in entries})
self._cancel.clear()
self._job = self._idle_job()
self._job.update(running=True, total=len(todo))
self._thread = threading.Thread(target=self._run_index, args=(todo, model, dim, soft), daemon=True)
self._thread.start()
def _prune_missing(self, folder: str, recursive: bool, existing: set[str]) -> None:
base = Path(folder).resolve().as_posix().rstrip("/") + "/"
for p in list(self._vecs.keys()):
if not p.startswith(base):
continue
if not recursive and "/" in p[len(base):]:
continue
if p not in existing:
self._vecs.pop(p, None)
self._stamp.pop(p, None)
self._mat_cache = None
self._dirty += 1
def cancel(self) -> None:
self._cancel.set()
def _run_index(self, todo: list, model: str, dim: int, soft: int = local.DEFAULT_SOFT) -> None:
def work(entry):
if self._cancel.is_set():
return
name, ap, size, mtime = entry
self._job["current"] = name
try:
vec = self._embed_image(model, dim, Path(ap), soft)
self._put(ap, vec, size, mtime)
self._job["done"] += 1
except SemanticError as e:
if e.code in ("no_api_key", "api_key_invalid"):
self._job["error_code"], self._job["error"] = e.code, str(e)
self._cancel.set()
else:
self._job["failed"] += 1
if not self._job["error"]:
self._job["error_code"], self._job["error"] = e.code, str(e)
except Exception as e:
logger.warning("SemanticService: embed failed for %s: %s", ap, e)
self._job["failed"] += 1
if self._dirty >= _CHECKPOINT_EVERY:
try:
self._save()
except Exception as e:
logger.warning("SemanticService: checkpoint failed: %s", e)
try:
if model == LOCAL_MODEL:
# 初回はモデルのダウンロード/ロードを先に行う(失敗したらジョブ全体を中止)
try:
local.ensure_runtime(self.data_dir, progress=lambda ph: self._job.update(current=f"({ph} model...)"))
except Exception as e:
logger.warning("SemanticService: local model load failed: %s", e)
self._job["error_code"], self._job["error"] = "local_error", str(e)
self._cancel.set()
workers = 1 if model == LOCAL_MODEL else _WORKERS # ローカルはORT側がマルチスレッド
with ThreadPoolExecutor(max_workers=workers) as ex:
list(ex.map(work, todo))
finally:
try:
self._save()
except Exception as e:
logger.warning("SemanticService: save failed: %s", e)
if model == LOCAL_MODEL:
local.release() # 画像生成とVRAM/RAMを奪い合わないよう、作成後はモデルを解放(検索時に再ロード)
self._job["running"] = False
self._job["finished"] = True
self._job["cancelled"] = self._cancel.is_set() and not self._job["error_code"]
self._job["current"] = ""
def _load_pil(self, path: Path):
"""埋め込み用に縮小したPIL画像を返す(動画/PSDはサムネイルから)"""
from PIL import Image
if path.suffix.lower() in _THUMB_EXTS:
thumb = self.gallery.serve_thumbnail(str(path), IMAGE_MAX_SIZE)
if not thumb:
raise SemanticError("thumb_failed", str(path))
with Image.open(thumb) as im:
return im.convert("RGB")
with Image.open(path) as im:
im.seek(0)
im = im.convert("RGBA") if im.mode in ("RGBA", "LA", "P") else im.convert("RGB")
im.thumbnail((IMAGE_MAX_SIZE, IMAGE_MAX_SIZE), Image.LANCZOS)
return im
def _embed_image(self, model: str, dim: int, path: Path, soft: int | None = None) -> np.ndarray:
from PIL import Image
im = self._load_pil(path)
if model == LOCAL_MODEL:
if im.mode == "RGBA": # 透過部は白背景に合成
bg = Image.new("RGB", im.size, (255, 255, 255))
bg.paste(im, mask=im.getchannel("A"))
im = bg
try:
return local.embed_image(self.data_dir, im, dim, self._soft if soft is None else soft)
except Exception as e:
raise SemanticError("local_error", str(e))
buf = io.BytesIO()
if im.mode == "RGBA":
im.save(buf, "PNG")
return embed_image_bytes(buf.getvalue(), "image/png", dim)
im.save(buf, "JPEG", quality=88)
return embed_image_bytes(buf.getvalue(), "image/jpeg", dim)
def _embed_text(self, model: str, dim: int, text: str) -> np.ndarray:
if model == LOCAL_MODEL:
try:
return local.embed_text(self.data_dir, text, dim)
except Exception as e:
raise SemanticError("local_error", str(e))
return embed_text(text, dim)
# ── 検索 ───────────────────────────────────────────────
def _matrix(self):
with self._lock:
if self._mat_cache is None:
paths = list(self._vecs.keys())
mat = np.stack([self._vecs[p] for p in paths]) if paths else np.zeros((0, max(self._dim, 1)), np.float32)
self._mat_cache = (paths, mat)
return self._mat_cache
def _rank(self, qvec: np.ndarray, folder: str, recursive: bool, limit: int, exclude: str = "",
min_score: float = 0.0) -> list[dict]:
entries = self._scope_entries(folder, recursive)
info = {ap: (name, sz, mt) for name, ap, sz, mt in entries}
paths, mat = self._matrix()
if not paths:
raise SemanticError("index_empty")
if mat.shape[1] != qvec.shape[0]:
raise SemanticError("dim_mismatch", str(self._dim))
scores = mat @ qvec
order = np.argsort(-scores)
out = []
for i in order:
if float(scores[i]) < min_score:
break # スコア降順なので、以降はすべてしきい値未満
p = paths[i]
if p == exclude or p not in info:
continue
name, sz, mt = info[p]
item = self.gallery.make_list_item(name, p, sz, mt)
item["score"] = round(float(scores[i]), 4)
out.append(item)
if len(out) >= limit:
break
return out
def search_text(self, query: str, folder: str, recursive: bool, limit: int, min_score: float = 0.0) -> list[dict]:
self._ensure_loaded()
query = (query or "").strip()[:_MAX_QUERY_CHARS]
if not query:
return []
if not self._vecs:
raise SemanticError("index_empty")
qvec = self._embed_text(self._model, self._dim, query)
return self._rank(qvec, folder, recursive, limit, min_score=min_score)
def search_similar(self, path: str, folder: str, recursive: bool, limit: int, min_score: float = 0.0) -> list[dict]:
self._ensure_loaded()
p = Path(path).resolve()
if not p.is_file() or not self.gallery._check_path_allowed(p):
raise SemanticError("folder_not_allowed", path)
ap = p.as_posix()
vec = self._vecs.get(ap)
if vec is None or not self._is_fresh(ap, p.stat().st_size, p.stat().st_mtime):
# 未インデックスの基準画像は、その場で1枚だけ埋め込む
if not self._vecs:
raise SemanticError("index_empty")
if p.suffix.lower() not in INDEXABLE_EXTS:
raise SemanticError("unsupported_type", p.suffix)
vec = self._embed_image(self._model, self._dim, p)
st = p.stat()
self._put(ap, vec, st.st_size, st.st_mtime)
self._save()
return self._rank(vec, folder, recursive, limit, exclude=ap, min_score=min_score)
def clear(self) -> None:
self._ensure_loaded()
with self._lock:
if self._job["running"]:
raise SemanticError("already_running")
self._vecs.clear()
self._stamp.clear()
self._dim = 0
self._mat_cache = None
self._save()
def test_connection(self, model: str, dim: int) -> dict:
if model not in DIMS_BY_MODEL:
raise SemanticError("bad_model", model)
if dim not in DIMS_BY_MODEL[model]:
dim = DEFAULT_DIM_BY_MODEL[model]
n = int(self._embed_text(model, dim, "test").shape[0])
return {"dimensions": n, "provider": local.active_provider() if model == LOCAL_MODEL else "API"}
+1 -1
View File
@@ -1,7 +1,7 @@
[project]
name = "comfyui-workflow-studio"
description = "A comprehensive workflow, asset management, and generation UI plugin for ComfyUI. Manage workflows, models, images, and prompts; batch generation and Image Feeder in the GenerateUI tab; library side panel with canvas drag & drop and AI tools."
version = "0.7.14"
version = "0.8.2"
license = { file = "LICENSE" }
requires-python = ">= 3.10"
dependencies = ["jinja2"]
+10
View File
@@ -33,3 +33,13 @@ psd-tools>=1.10
# If missing, mp4 thumbnail generation simply fails and falls back to a placeholder
# (Gallery itself keeps working). Not pinned here on purpose — see torch note above.
# av>=17.0.0
# -- Gallery Semantic Search: local EmbeddingGemma 2 (optional) --
# Uses onnxruntime (above) + tokenizers + huggingface_hub (both normally pre-installed with ComfyUI's
# transformers). The model (~300 MB, q4) is downloaded to the data folder on first use.
# GPU (NVIDIA/CUDA): replace onnxruntime with onnxruntime-gpu
# pip uninstall onnxruntime onnxruntime-gpu
# pip install onnxruntime-gpu
# Local EmbeddingGemma 2 then indexes ~45x faster than on the CPU. DirectML is not supported.
# tokenizers
# huggingface_hub
+171
View File
@@ -867,6 +867,13 @@
font-size: 13px;
}
.wfm-gallery-move-filter {
width: 100%;
padding: 6px 8px;
font-size: 13px;
box-sizing: border-box;
}
.wfm-gallery-move-modal-footer {
display: flex;
gap: 8px;
@@ -983,3 +990,167 @@
min-height: 0;
resize: none;
}
/* 音声ファイル: 波形画像+<audio> コントロール */
.wfm-gallery-audio-box {
display: flex;
flex-direction: column;
align-items: stretch;
gap: 8px;
width: 100%;
}
.wfm-gallery-audio-wave {
object-fit: contain;
max-height: 220px;
background: #181c26;
border-radius: 6px;
}
.wfm-gallery-audio-player {
width: 100%;
}
/* 変換ダイアログ */
.wfm-gallery-convert-modal {
min-width: 360px;
max-width: 480px;
}
.wfm-gallery-convert-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin: 8px 0;
}
.wfm-gallery-convert-row .wfm-select {
min-width: 150px;
}
.wfm-gallery-convert-check {
display: flex;
align-items: flex-start;
gap: 6px;
margin: 6px 0;
font-size: 12px;
}
.wfm-gallery-convert-note,
.wfm-gallery-convert-status {
font-size: 12px;
opacity: 0.8;
margin: 4px 0;
}
/* 音声: 詳細パネルのプレビュー枠は固定高さ(正方形)をやめ、波形+プレイヤーを収める */
.wfm-gallery-detail-preview--audio {
aspect-ratio: auto;
max-height: none;
overflow: visible;
padding: 8px;
}
.wfm-gallery-detail-preview--audio .wfm-gallery-audio-wave {
max-height: 150px;
}
/* ── セマンティック検索 ─────────────────────────────────────── */
.wfm-gallery-sem-toggle.active {
background: var(--wfm-primary);
border-color: var(--wfm-primary);
color: #fff;
}
/* 2行目(Similar / 更新)のボタン: Taggerの紫と区別できるティール */
.wfm-sem-sim {
background: #0d9488;
border-color: #0d9488;
color: #fff;
}
.wfm-sem-sim:hover {
background: #0f766e;
border-color: #0f766e;
}
/* 類似画像表示中(トグルON)は縁取りで状態を示す */
#wfm-gallery-similar-btn.active {
box-shadow: 0 0 0 2px #fff inset, 0 0 0 2px #0d9488;
}
.wfm-gallery-sem-row {
gap: 6px;
align-items: center;
justify-content: flex-start;
}
.wfm-gallery-sem-count {
margin-left: 6px;
font-size: 12px;
opacity: 0.85;
}
.wfm-gallery-thumb-score {
position: absolute;
left: 4px;
bottom: 4px;
padding: 1px 5px;
border-radius: 8px;
background: rgba(13, 148, 136, 0.85);
color: #fff;
font-size: 10px;
line-height: 14px;
pointer-events: none;
}
.wfm-sem-modal { display: flex; flex-direction: column; gap: 10px; min-width: 420px; }
.wfm-sem-row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.wfm-sem-folder { font-family: monospace; font-size: 12px; word-break: break-all; opacity: 0.85; }
.wfm-sem-hint { font-size: 11px; opacity: 0.7; margin: 0; }
.wfm-sem-ok { color: #22c55e; }
.wfm-sem-ng { color: #ef4444; }
.wfm-sem-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.wfm-sem-bar { height: 8px; border-radius: 4px; background: var(--wfm-border, #444); overflow: hidden; }
.wfm-sem-bar-fill { height: 100%; width: 0; background: #0d9488; transition: width 0.3s; }
/* フォルダツリー: 直下の画像が全件セマンティック索引済みのフォルダに付くドット */
.wfm-gallery-tree-sem-dot {
display: inline-block;
width: 7px;
height: 7px;
margin-left: 4px;
border-radius: 50%;
background: #22c55e;
vertical-align: middle;
}
/* モーダルのヘルプボタンから飛んだ先の説明を一時的に強調 */
.wfm-help-flash {
animation: wfm-help-flash 2.5s ease-out;
}
@keyframes wfm-help-flash {
0%, 60% { background: rgba(13, 148, 136, 0.35); }
100% { background: transparent; }
}
.wfm-sem-local-opts { display: flex; flex-direction: column; gap: 10px; padding: 8px 10px; border: 1px solid var(--wfm-border, #444); border-radius: 6px; }
/* セマンティック検索モーダル: タブ */
.wfm-sem-tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--wfm-border, #444); }
.wfm-sem-tab-btn {
padding: 6px 14px;
background: transparent;
border: 1px solid transparent;
border-bottom: none;
border-radius: 6px 6px 0 0;
color: var(--wfm-text-secondary, #aaa);
cursor: pointer;
font-size: 13px;
}
.wfm-sem-tab-btn:hover { color: var(--wfm-text, #fff); }
.wfm-sem-tab-btn.active {
color: var(--wfm-text, #fff);
background: var(--wfm-bg-secondary, #1a1a2e);
border-color: var(--wfm-border, #444);
margin-bottom: -1px;
border-bottom: 1px solid var(--wfm-bg-secondary, #1a1a2e);
}
.wfm-sem-pane { display: flex; flex-direction: column; gap: 10px; min-height: 230px; }
.wfm-sem-footer { justify-content: flex-end; border-top: 1px solid var(--wfm-border, #444); padding-top: 10px; }
.wfm-sem-num { width: 80px; }
.wfm-sem-danger { margin-left: auto; color: #ef4444; }
.wfm-sem-row .wfm-select { width: auto; min-width: 90px; }
+2 -1
View File
@@ -3491,7 +3491,8 @@ body {
.wfm-help-page {
display: none;
max-width: 760px;
/* 上部タブバー(AI TOOLタブの右端)あたりまで広げ、折り返しを減らす。狭い画面では領域幅に収まる */
max-width: 1076px;
}
.wfm-help-page.active {
+3 -1
View File
@@ -249,6 +249,8 @@ function applyI18nToHtml() {
if (galleryBulkPsd) galleryBulkPsd.textContent = t("galleryBulkPsd");
const galleryBulkEditLayers = document.getElementById("wfm-gallery-bulk-edit-layers");
if (galleryBulkEditLayers) galleryBulkEditLayers.textContent = t("galleryBulkEditLayers");
const galleryBulkConvert = document.getElementById("wfm-gallery-bulk-convert");
if (galleryBulkConvert) galleryBulkConvert.textContent = t("galleryBulkConvert");
const galleryBulkDelete = document.getElementById("wfm-gallery-bulk-delete");
if (galleryBulkDelete) galleryBulkDelete.textContent = t("galleryBulkDelete");
const galleryOpenTaggerBtn = document.getElementById("wfm-gallery-open-tagger-btn");
@@ -585,7 +587,7 @@ function applyI18nToHtml() {
"wfm-help-gallery-26": "helpGallery26",
"wfm-help-gallery-28": "helpGallery28", "wfm-help-gallery-29": "helpGallery29",
"wfm-help-gallery-30": "helpGallery30", "wfm-help-gallery-31": "helpGallery31",
"wfm-help-gallery-32": "helpGallery32",
"wfm-help-gallery-32": "helpGallery32", "wfm-help-gallery-33": "helpGallery33", "wfm-help-gallery-34": "helpGallery34", "wfm-help-gallery-35": "helpGallery35", "wfm-help-gallery-36": "helpGallery36",
"wfm-help-gallery-card-styleprompt-title": "helpGalleryCardStyleprompt",
"wfm-help-gallery-card-stylecatalog-title": "helpGalleryCardStylecatalog",
"wfm-help-tagger-title": "helpTaggerTitle",
+14 -2
View File
@@ -649,7 +649,19 @@ export const comfyWorkflow = {
_objectInfoCache = null;
},
async convertUiToApi(workflow) {
// opts.readOnly: 表示用の読み取り変換。COMBO値の差し替えを行わず、Workflowタブの互換性
// チェックが参照する _last* 記録も変更しない(Metadataタブなど表示目的の呼び出し用)。
async convertUiToApi(workflow, opts = {}) {
if (!opts.readOnly) return comfyWorkflow._convertUiToApiImpl(workflow, opts);
const saved = [_lastCheckpointSubstitutions, _lastComboSubstitutions, _lastBypassedNodes, _lastMutedNodes];
try {
return await comfyWorkflow._convertUiToApiImpl(workflow, opts);
} finally {
[_lastCheckpointSubstitutions, _lastComboSubstitutions, _lastBypassedNodes, _lastMutedNodes] = saved;
}
},
async _convertUiToApiImpl(workflow, opts = {}) {
_lastCheckpointSubstitutions = [];
_lastComboSubstitutions = [];
_lastBypassedNodes = [];
@@ -807,7 +819,7 @@ export const comfyWorkflow = {
const spec = allInputDefs[name];
if (spec) {
const choices = Array.isArray(spec[0]) ? spec[0] : null;
if (choices && choices.length > 0 && !choices.includes(val)) {
if (!opts.readOnly && choices && choices.length > 0 && !choices.includes(val)) {
if (name === "ckpt_name") {
_lastCheckpointSubstitutions.push({
nodeId: String(node.id),
+580
View File
@@ -0,0 +1,580 @@
/**
* Gallery Tab - セマンティック検索 (Gemini Embedding 2)
* - 検索ボックスのSemantic Searchトグル(ONでEnter実行の意味検索)
* - 詳細パネルの「類似画像」トグル/更新ボタン
* - インデックス作成モーダル(Semantic⚙)
* 一覧の差し替えは gallery-tab.js の loadImages() が getSemanticMode() / fetchSemanticImages() を呼んで行う。
*/
import { openModal, closeModal, showToast } from "./app.js";
import { t } from "./i18n.js";
import { escapeHtml } from "./util.js";
const API = {
status: (folder, recursive) => `/wfm/gallery/semantic/status?folder=${encodeURIComponent(folder)}&recursive=${recursive}`,
index: "/wfm/gallery/semantic/index",
cancel: "/wfm/gallery/semantic/cancel",
search: "/wfm/gallery/semantic/search",
similar: "/wfm/gallery/semantic/similar",
clear: "/wfm/gallery/semantic/clear",
folders: (root) => `/wfm/gallery/semantic/folders?root=${encodeURIComponent(root)}`,
test: "/wfm/gallery/semantic/test",
};
const LS_ON = "wfm_gallery_semantic_on";
const LS_DIM = "wfm_gallery_semantic_dim";
const LS_MODEL = "wfm_gallery_semantic_model";
const LS_SOFT = "wfm_gallery_semantic_soft";
const GEMINI = "gemini-embedding-2";
const LOCAL = "embeddinggemma-2";
const LS_SUB = "wfm_gallery_semantic_recursive";
const LS_TAB = "wfm_gallery_semantic_tab";
const LS_TH = "wfm_gallery_semantic_th";
const LIMITS = [50, 100, 200, 500, 1000];
const sem = {
on: false, // Semantic Searchトグル
anchor: "", // 類似画像表示中の基準画像パス(空=類似表示OFF)
anchorName: "",
indexModel: GEMINI, // 現在のインデックスを作ったモデル(しきい値はモデルごとに保存する)
};
let ctx = null; // { getSearch, getFolder, getSelected, reload, focusSearch }
function lsGet(key, fallback) {
try { return localStorage.getItem(key) ?? fallback; } catch { return fallback; }
}
function lsSet(key, value) {
try { localStorage.setItem(key, value); } catch { /* 保存できなくても動作に影響しない */ }
}
async function postJson(url, body) {
const res = await fetch(url, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body || {}),
});
let data = {};
try { data = await res.json(); } catch { /* 本文なし */ }
if (!res.ok) {
throw Object.assign(new Error(data.error || `HTTP ${res.status}`), { code: data.code || "" });
}
return data;
}
/** APIエラーコードをi18nメッセージにする */
export function semanticErrorMessage(e) {
const known = ["no_api_key", "api_key_invalid", "index_empty", "dim_mismatch", "already_running",
"folder_not_allowed", "unsupported_type", "network_error", "api_error", "local_error"];
if (e?.code && known.includes(e.code)) {
return t(`sem_err_${e.code}`, e.message);
}
return t("errorWithMsg", e?.message || String(e));
}
// ── gallery-tab.js から使う公開API ────────────────────────────
/** 現在の一覧モード: {kind:"similar", path} | {kind:"text", query} | null(通常) */
export function getSemanticMode() {
if (sem.anchor) return { kind: "similar", path: sem.anchor, name: sem.anchorName };
const q = (ctx?.getSearch() || "").trim();
if (sem.on && q) return { kind: "text", query: q };
return null;
}
export function isSemanticOn() {
return sem.on;
}
/**
* 表示条件(類似度のしきい値・最大件数)。スコアの水準はモデルで違うため、モデルごとに保存する。
* similar: 類似画像検索の最小スコア / text: 文章検索の最小スコア(0=絞り込みなし)
*/
function loadThresholds(model) {
let v = {};
try { v = JSON.parse(lsGet(LS_TH, "{}"))[model] || {}; } catch { /* 壊れた値は既定に戻す */ }
const num = x => { const n = parseFloat(x); return Number.isFinite(n) ? Math.min(1, Math.max(0, n)) : 0; };
return { similar: num(v.similar), text: num(v.text), limit: LIMITS.includes(v.limit) ? v.limit : 200 };
}
function saveThresholds(model, th) {
let all = {};
try { all = JSON.parse(lsGet(LS_TH, "{}")) || {}; } catch { all = {}; }
all[model] = th;
lsSet(LS_TH, JSON.stringify(all));
}
function minScoreFor(mode) {
const th = loadThresholds(sem.indexModel);
return mode.kind === "similar" ? th.similar : th.text;
}
/** モードに応じてサーバーから結果(スコア降順・score付き)を取得する */
export async function fetchSemanticImages(mode, folder) {
const th = loadThresholds(sem.indexModel);
const common = { folder, recursive: true, limit: th.limit, min_score: minScoreFor(mode) };
const data = mode.kind === "similar"
? await postJson(API.similar, { path: mode.path, ...common })
: await postJson(API.search, { query: mode.query, ...common });
return data.images || [];
}
/** 2行目の結果件数表示(意味検索/類似表示のときだけ文言を入れる。通常は空) */
export function setSemanticCountText(text) {
const el = document.getElementById("wfm-gallery-sem-count");
if (el) el.textContent = text || "";
}
/** 一覧件数ラベル */
export function semanticCountLabel(mode, count) {
const min = minScoreFor(mode);
return mode.kind === "similar"
? t("semCountSimilar", count, mode.name, min)
: t("semCountText", count, min);
}
let _dotRoot = "";
let _dotSeq = 0;
/**
* フォルダツリーの枚数バッジ横に、直下の画像が全件インデックス済みのフォルダへドットを付ける。
* ツリー再構築後・インデックス作成後に呼ぶ(DOMはその場で更新し、ツリーは作り直さない)。
*/
export async function refreshSemanticDots(root) {
if (root) _dotRoot = root;
if (!_dotRoot) return;
const seq = ++_dotSeq;
let folders;
try {
const res = await fetch(API.folders(_dotRoot));
if (!res.ok) return;
folders = (await res.json()).folders || {};
} catch {
return;
}
if (seq !== _dotSeq) return; // より新しい呼び出しがある
document.querySelectorAll(".wfm-gallery-tree-item").forEach(item => {
const label = item.querySelector(".wfm-gallery-tree-label");
const badge = label?.querySelector(".wfm-gallery-tree-count");
label?.querySelector(".wfm-gallery-tree-sem-dot")?.remove();
const st = folders[(item.dataset.path || "").replace(/\\/g, "/")];
if (badge && st && st.indexable > 0 && st.indexed >= st.indexable) {
const dot = document.createElement("span");
dot.className = "wfm-gallery-tree-sem-dot";
dot.title = t("semDotTitle");
badge.after(dot);
}
});
}
/** フォルダ切替・フィルタクリア時に類似表示を解除する(再読込は呼び出し側) */
export function exitSimilarMode() {
sem.anchor = "";
sem.anchorName = "";
updateSimilarButtons();
}
// ── トグル/類似ボタン ───────────────────────────────────────
function updateToggleUi() {
const btn = document.getElementById("wfm-gallery-semantic-toggle");
btn?.classList.toggle("active", sem.on);
// ON のときだけ2行目(Similar / 更新 / 設定 / 件数)を表示する
const row = document.getElementById("wfm-gallery-sem-row");
if (row) row.style.display = sem.on ? "flex" : "none";
const input = document.getElementById("wfm-gallery-search");
if (input) input.placeholder = sem.on ? t("semSearchPlaceholder") : "Search images...";
}
function updateSimilarButtons() {
document.getElementById("wfm-gallery-similar-btn")?.classList.toggle("active", !!sem.anchor);
}
async function toggleSimilar() {
if (sem.anchor) {
exitSimilarMode();
ctx.reload();
return;
}
await applySimilar();
}
async function applySimilar() {
const img = ctx.getSelected();
if (!img) {
showToast(t("gallerySelectImageFirst"), "error");
return;
}
sem.anchor = img.path;
sem.anchorName = img.filename;
updateSimilarButtons();
ctx.reload();
}
// ── インデックス作成モーダル ─────────────────────────────────
function modalHtml() {
const tab = ["index", "model", "search"].includes(lsGet(LS_TAB, "index")) ? lsGet(LS_TAB, "index") : "index";
const pane = (id, body) => `<div class="wfm-sem-pane" data-sem-pane="${id}" style="${tab === id ? "" : "display:none;"}">${body}</div>`;
const tabBtn = (id, label) =>
`<button type="button" class="wfm-sem-tab-btn${tab === id ? " active" : ""}" data-sem-tab="${id}">${escapeHtml(label)}</button>`;
const indexPane = `
<div class="wfm-sem-status" id="wfm-sem-status">${escapeHtml(t("loading"))}</div>
<div class="wfm-sem-row"><b>${escapeHtml(t("semScope"))}</b>
<span id="wfm-sem-folder" class="wfm-sem-folder"></span></div>
<label class="wfm-sem-row"><input type="checkbox" id="wfm-sem-recursive" ${lsGet(LS_SUB, "1") === "1" ? "checked" : ""}>
${escapeHtml(t("semIncludeSub"))}</label>
<div class="wfm-sem-row"><b>${escapeHtml(t("semMode"))}</b>
<label><input type="radio" name="wfm-sem-mode" value="missing" checked> ${escapeHtml(t("semModeMissing"))}</label>
<label><input type="radio" name="wfm-sem-mode" value="all"> ${escapeHtml(t("semModeAll"))}</label></div>
<div class="wfm-sem-progress" id="wfm-sem-progress" style="display:none;">
<div class="wfm-sem-bar"><div class="wfm-sem-bar-fill" id="wfm-sem-bar-fill"></div></div>
<div id="wfm-sem-progress-text" class="wfm-sem-hint"></div>
</div>
<div class="wfm-sem-actions">
<button class="wfm-btn wfm-btn-primary" id="wfm-sem-start">${escapeHtml(t("semStart"))}</button>
<button class="wfm-btn" id="wfm-sem-cancel" disabled>${escapeHtml(t("semCancelJob"))}</button>
<button class="wfm-btn wfm-sem-danger" id="wfm-sem-clear">${escapeHtml(t("semClear"))}</button>
</div>`;
const modelPane = `
<div class="wfm-sem-row"><b>${escapeHtml(t("semModel"))}</b>
<label><input type="radio" name="wfm-sem-model" value="${GEMINI}"> ${escapeHtml(t("semModelGemini"))}</label>
<label><input type="radio" name="wfm-sem-model" value="${LOCAL}"> ${escapeHtml(t("semModelLocal"))}</label></div>
<div class="wfm-sem-row"><b>${escapeHtml(t("semDimension"))}</b>
<select id="wfm-sem-dim" class="wfm-select"></select>
<span class="wfm-sem-hint">${escapeHtml(t("semDimHint"))}</span></div>
<div id="wfm-sem-local-opts" class="wfm-sem-local-opts" style="display:none;">
<div class="wfm-sem-row"><b>${escapeHtml(t("semDevice"))}</b>
<select id="wfm-sem-device" class="wfm-select">
<option value="auto">${escapeHtml(t("semDeviceAuto"))}</option>
<option value="cpu">CPU</option>
<option value="cuda">GPU (CUDA)</option>
</select>
<span id="wfm-sem-device-info" class="wfm-sem-hint"></span></div>
<div class="wfm-sem-row" id="wfm-sem-threads-row"><b>${escapeHtml(t("semThreads"))}</b>
<select id="wfm-sem-threads" class="wfm-select"></select>
<span class="wfm-sem-hint">${escapeHtml(t("semThreadsHint"))}</span></div>
<div class="wfm-sem-row"><b>${escapeHtml(t("semSoft"))}</b>
<select id="wfm-sem-soft" class="wfm-select">
<option value="280">${escapeHtml(t("semSoftHigh"))}</option>
<option value="140">${escapeHtml(t("semSoftStd"))}</option>
<option value="70">${escapeHtml(t("semSoftLight"))}</option>
</select>
<span class="wfm-sem-hint">${escapeHtml(t("semSoftHint"))}</span></div>
</div>
<div class="wfm-sem-actions">
<button class="wfm-btn" id="wfm-sem-test">${escapeHtml(t("semTest"))}</button>
</div>
<p class="wfm-sem-hint">${escapeHtml(t("semCostNote"))}</p>`;
const searchPane = `
<div class="wfm-sem-row"><b>${escapeHtml(t("semThTarget"))}</b>
<span id="wfm-sem-th-model" class="wfm-sem-folder"></span></div>
<div class="wfm-sem-row"><b>${escapeHtml(t("semThSimilar"))}</b>
<input type="number" id="wfm-sem-th-similar" class="wfm-input wfm-sem-num" min="0" max="1" step="0.01" placeholder="0">
<span class="wfm-sem-hint">${escapeHtml(t("semThSimilarHint"))}</span></div>
<div class="wfm-sem-row"><b>${escapeHtml(t("semThText"))}</b>
<input type="number" id="wfm-sem-th-text" class="wfm-input wfm-sem-num" min="0" max="1" step="0.01" placeholder="0">
<span class="wfm-sem-hint">${escapeHtml(t("semThTextHint"))}</span></div>
<div class="wfm-sem-row"><b>${escapeHtml(t("semThLimit"))}</b>
<select id="wfm-sem-th-limit" class="wfm-select">${LIMITS.map(n => `<option value="${n}">${n}</option>`).join("")}</select></div>
<p class="wfm-sem-hint">${escapeHtml(t("semThNote"))}</p>`;
return `
<div class="wfm-sem-modal">
<div class="wfm-sem-tabs">${tabBtn("index", t("semTabIndex"))}${tabBtn("model", t("semTabModel"))}${tabBtn("search", t("semTabSearch"))}</div>
${pane("index", indexPane)}${pane("model", modelPane)}${pane("search", searchPane)}
<div class="wfm-sem-actions wfm-sem-footer">
<button class="wfm-btn" id="wfm-sem-help" title="${escapeHtml(t("semHelpTitle"))}">?</button>
<button class="wfm-btn" id="wfm-sem-close">${escapeHtml(t("close"))}</button>
</div>
</div>`;
}
function modalAlive() {
const overlay = document.getElementById("wfm-modal-overlay");
return !!document.getElementById("wfm-sem-status") && overlay && overlay.style.display !== "none";
}
let _pollTimer = null;
let _backends = {};
let _sawRunning = false;
function selectedModel() {
return document.querySelector('input[name="wfm-sem-model"]:checked')?.value || GEMINI;
}
/** ローカルモデルの実行設定(デバイス/スレッド/画像精度)の初期値をサーバー保存値から設定する */
function initLocalOpts(st) {
const lo = st.local || {};
const devSel = document.getElementById("wfm-sem-device");
devSel.value = (lo.options && lo.options.device) || "auto";
const cores = lo.devices?.cpu_count || 4;
const cur = (lo.options && lo.options.threads) || lo.default_threads || Math.max(1, Math.floor(cores / 2));
const set = new Set([1, 2, 4, 6, 8, 12, 16, 24, 32].filter(n => n <= cores).concat([cores, cur, lo.default_threads || 1]));
document.getElementById("wfm-sem-threads").innerHTML = [...set].sort((a, b) => a - b)
.map(n => `<option value="${n}">${n}${n === cores ? ` (${t("semThreadsAll")})` : ""}</option>`).join("");
document.getElementById("wfm-sem-threads").value = String(cur);
const soft = st.indexed_total && st.index_model === LOCAL ? st.index_soft : parseInt(lsGet(LS_SOFT, String(lo.default_soft || 280)), 10);
document.getElementById("wfm-sem-soft").value = String([70, 140, 280].includes(soft) ? soft : 280);
}
/** 検索タブ: しきい値の入力欄にインデックスのモデル用の保存値を入れる(入力中の値は上書きしない) */
function fillThresholds(st) {
const model = st.indexed_total ? st.index_model : selectedModel();
const box = document.getElementById("wfm-sem-th-model");
if (box) box.textContent = model + (st.indexed_total ? "" : ` ${t("semThNoIndex")}`);
const pane = document.getElementById("wfm-sem-th-similar");
if (!pane || pane.dataset.model === model) return;
const th = loadThresholds(model);
pane.value = th.similar || "";
document.getElementById("wfm-sem-th-text").value = th.text || "";
document.getElementById("wfm-sem-th-limit").value = String(th.limit);
pane.dataset.model = model;
}
/** 検索タブの入力を保存する。一覧が意味検索/類似表示中なら閉じるときに再読込する */
let _thDirty = false;
function saveThresholdInputs() {
const model = document.getElementById("wfm-sem-th-similar")?.dataset.model;
if (!model) return;
const clamp = id => {
const n = parseFloat(document.getElementById(id).value);
return Number.isFinite(n) ? Math.min(1, Math.max(0, n)) : 0;
};
saveThresholds(model, {
similar: clamp("wfm-sem-th-similar"),
text: clamp("wfm-sem-th-text"),
limit: parseInt(document.getElementById("wfm-sem-th-limit").value, 10) || 200,
});
_thDirty = true;
}
/** モーダルを閉じる共通処理 */
function closeSemanticModal() {
clearTimeout(_pollTimer);
closeModal();
refreshSemanticDots();
if (_thDirty) {
_thDirty = false;
if (getSemanticMode()) ctx.reload(); // 新しいしきい値で一覧を取り直す
}
}
/** 検出した実行デバイスの表示(GPU版onnxruntimeが無いときの案内を含む) */
function updateDeviceInfo(lo) {
const dev = lo?.devices;
if (!dev) return;
const info = document.getElementById("wfm-sem-device-info");
const cudaOpt = document.querySelector('#wfm-sem-device option[value="cuda"]');
if (cudaOpt) cudaOpt.disabled = !dev.cuda;
if (!dev.cuda && document.getElementById("wfm-sem-device").value === "cuda") {
document.getElementById("wfm-sem-device").value = "auto";
}
if (info) info.textContent = dev.cuda ? t("semDeviceCudaOk", dev.ort_version) : t("semDeviceNoGpu", dev.ort_version);
const dv = document.getElementById("wfm-sem-device").value;
const useCpu = dv === "cpu" || (dv === "auto" && !dev.cuda);
document.getElementById("wfm-sem-threads-row").style.display = useCpu ? "" : "none";
}
/** 選択中モデルの次元選択肢を作り直す(preferredが選択肢にあればそれを選ぶ) */
function fillDims(model, preferred) {
const sel = document.getElementById("wfm-sem-dim");
const info = _backends[model];
if (!sel || !info) return;
sel.innerHTML = info.dims.map(d => `<option value="${d}">${d}</option>`).join("");
sel.value = String(info.dims.includes(preferred) ? preferred : info.default_dim);
}
async function refreshModal() {
if (!modalAlive()) return;
const folder = ctx.getFolder();
const recursive = document.getElementById("wfm-sem-recursive")?.checked ?? true;
let st;
try {
const res = await fetch(API.status(folder, recursive));
st = await res.json();
if (!res.ok) throw new Error(st.error || `HTTP ${res.status}`);
} catch (e) {
document.getElementById("wfm-sem-status").textContent = t("errorWithMsg", e.message);
return;
}
if (!modalAlive()) return;
const job = st.job || {};
_backends = st.backends || {};
if (st.indexed_total) sem.indexModel = st.index_model;
if (!document.getElementById("wfm-sem-dim").dataset.init) {
// 初回のみ: 既存インデックスのモデル/次元、なければ前回の選択を初期値にする
const initModel = st.indexed_total ? st.index_model : lsGet(LS_MODEL, GEMINI);
const radio = document.querySelector(`input[name="wfm-sem-model"][value="${initModel}"]`)
|| document.querySelector(`input[name="wfm-sem-model"][value="${GEMINI}"]`);
radio.checked = true;
fillDims(radio.value, st.indexed_total ? st.index_dim : parseInt(lsGet(LS_DIM, "0"), 10));
initLocalOpts(st);
document.getElementById("wfm-sem-dim").dataset.init = "1";
}
fillThresholds(st);
const model = selectedModel();
document.getElementById("wfm-sem-local-opts").style.display = model === LOCAL ? "" : "none";
updateDeviceInfo(st.local);
const ready = model === LOCAL ? true : !!st.has_key;
const keyLine = model === LOCAL
? (_backends[LOCAL]?.downloaded ? t("semLocalReady") : t("semLocalNeedDownload"))
: (st.has_key ? t("semKeyOk") : t("semKeyMissing"));
const idxLine = t("semIndexInfo", st.indexed_total, st.index_dim || "-");
const scopeLine = st.scope_total != null ? t("semScopeInfo", st.scope_indexed, st.scope_total) : "";
document.getElementById("wfm-sem-status").innerHTML =
`<div class="${ready ? "wfm-sem-ok" : "wfm-sem-ng"}">${escapeHtml(keyLine)}</div>` +
`<div>${escapeHtml(st.indexed_total ? st.index_model : "-")} / ${escapeHtml(idxLine)}</div>` +
(scopeLine ? `<div>${escapeHtml(scopeLine)}</div>` : "");
document.getElementById("wfm-sem-folder").textContent = folder || t("semNoFolder");
const prog = document.getElementById("wfm-sem-progress");
const hasJob = job.running || job.finished;
prog.style.display = hasJob ? "" : "none";
if (hasJob) {
const total = job.total || 0;
const doneAll = (job.done || 0) + (job.failed || 0);
document.getElementById("wfm-sem-bar-fill").style.width = total ? `${Math.min(100, (doneAll / total) * 100)}%` : "100%";
let text = t("semProgress", job.done || 0, total, job.failed || 0);
if (job.running && job.current) text += ` ${job.current}`;
if (!job.running) {
if (["no_api_key", "api_key_invalid", "local_error"].includes(job.error_code)) text += ` ${t(`sem_err_${job.error_code}`, job.error)}`;
else text += ` ${job.cancelled ? t("semCancelled") : t("semFinished")}`;
if (job.failed && job.error) text += ` (${job.error})`;
}
document.getElementById("wfm-sem-progress-text").textContent = text;
}
document.getElementById("wfm-sem-start").disabled = !!job.running || !ready;
document.getElementById("wfm-sem-cancel").disabled = !job.running;
document.getElementById("wfm-sem-clear").disabled = !!job.running;
document.getElementById("wfm-sem-test").disabled = !ready;
clearTimeout(_pollTimer);
if (job.running) {
_pollTimer = setTimeout(refreshModal, 1000);
_sawRunning = true;
} else if (_sawRunning) {
_sawRunning = false;
refreshSemanticDots();
}
}
/** ヘルプタブのGallery > セマンティック検索の説明へ飛ぶ */
function openSemanticHelp() {
closeSemanticModal();
document.querySelector('.wfm-tab[data-tab="help"]')?.click();
document.querySelector('.wfm-help-nav-item[data-help-page="gallery"]')?.click();
const target = document.getElementById("wfm-help-gallery-33");
if (!target) return;
target.scrollIntoView({ behavior: "smooth", block: "center" });
target.classList.add("wfm-help-flash");
setTimeout(() => target.classList.remove("wfm-help-flash"), 2500);
}
function openSemanticModal() {
if (!ctx.getFolder()) {
showToast(t("semNoFolder"), "error");
return;
}
openModal(t("semModalTitle"), modalHtml());
const dimOf = () => parseInt(document.getElementById("wfm-sem-dim").value, 10) || 0;
// ローカルモデル選択時のみ実行設定を送る(サーバー側で保存され、検索時のクエリ埋め込みにも使われる)
const localOpts = () => selectedModel() === LOCAL ? {
device: document.getElementById("wfm-sem-device").value,
threads: parseInt(document.getElementById("wfm-sem-threads").value, 10) || 0,
soft: parseInt(document.getElementById("wfm-sem-soft").value, 10) || 280,
} : {};
document.getElementById("wfm-sem-device").addEventListener("change", () => refreshModal());
document.querySelectorAll('input[name="wfm-sem-model"]').forEach(r => r.addEventListener("change", () => {
fillDims(selectedModel(), 0);
refreshModal();
}));
document.getElementById("wfm-sem-recursive").addEventListener("change", (e) => {
lsSet(LS_SUB, e.target.checked ? "1" : "0");
refreshModal();
});
document.getElementById("wfm-sem-close").addEventListener("click", closeSemanticModal);
document.querySelectorAll(".wfm-sem-tab-btn").forEach(btn => btn.addEventListener("click", () => {
document.querySelectorAll(".wfm-sem-tab-btn").forEach(b => b.classList.toggle("active", b === btn));
document.querySelectorAll(".wfm-sem-pane").forEach(p => { p.style.display = p.dataset.semPane === btn.dataset.semTab ? "" : "none"; });
lsSet(LS_TAB, btn.dataset.semTab);
}));
["wfm-sem-th-similar", "wfm-sem-th-text"].forEach(id => document.getElementById(id).addEventListener("change", saveThresholdInputs));
document.getElementById("wfm-sem-th-limit").addEventListener("change", saveThresholdInputs);
document.getElementById("wfm-sem-help").addEventListener("click", openSemanticHelp);
document.getElementById("wfm-sem-start").addEventListener("click", async () => {
const mode = document.querySelector('input[name="wfm-sem-mode"]:checked')?.value || "missing";
if (mode === "all" && !confirm(t("semConfirmAll"))) return;
lsSet(LS_DIM, String(dimOf()));
lsSet(LS_MODEL, selectedModel());
lsSet(LS_SOFT, document.getElementById("wfm-sem-soft").value);
try {
await postJson(API.index, {
folder: ctx.getFolder(),
recursive: document.getElementById("wfm-sem-recursive").checked,
mode,
model: selectedModel(),
dim: dimOf(),
...localOpts(),
});
} catch (e) {
showToast(semanticErrorMessage(e), "error");
}
refreshModal();
});
document.getElementById("wfm-sem-cancel").addEventListener("click", async () => {
await postJson(API.cancel, {}).catch(() => {});
refreshModal();
});
document.getElementById("wfm-sem-test").addEventListener("click", async () => {
try {
const r = await postJson(API.test, { model: selectedModel(), dim: dimOf(), ...localOpts() });
showToast(r.provider && r.provider !== "API" ? t("semTestOkProvider", r.dimensions, r.provider) : t("semTestOk", r.dimensions), "success");
} catch (e) {
showToast(semanticErrorMessage(e), "error");
}
});
document.getElementById("wfm-sem-clear").addEventListener("click", async () => {
if (!confirm(t("semConfirmClear"))) return;
try {
await postJson(API.clear, {});
showToast(t("semCleared"), "success");
refreshSemanticDots();
} catch (e) {
showToast(semanticErrorMessage(e), "error");
}
refreshModal();
});
refreshModal();
}
// ── 初期化 ───────────────────────────────────────────────────
/**
* @param {object} context
* getSearch(): 検索ボックスの文字列 / getFolder(): 選択中フォルダ / getSelected(): 選択中画像
* reload(): 一覧再読込(loadImages)
*/
export function initSemantic(context) {
ctx = context;
sem.on = lsGet(LS_ON, "0") === "1";
updateToggleUi();
updateSimilarButtons();
document.getElementById("wfm-gallery-semantic-toggle")?.addEventListener("click", () => {
sem.on = !sem.on;
lsSet(LS_ON, sem.on ? "1" : "0");
updateToggleUi();
// OFFにしたら類似表示も解除する(2行目ごと消えるため)
const wasSimilar = !!sem.anchor;
if (!sem.on) exitSimilarMode();
// 検索文字列があれば、モード切替後の方式で結果を取り直す
if (wasSimilar && !sem.on || (ctx.getSearch() || "").trim()) ctx.reload();
});
fetch(API.status("", true)).then(r => r.json()).then(st => {
if (st.indexed_total) sem.indexModel = st.index_model;
}).catch(() => { /* 取得できなくても既定のモデル扱いで動作する */ });
document.getElementById("wfm-gallery-semantic-settings-btn")?.addEventListener("click", openSemanticModal);
document.getElementById("wfm-gallery-similar-btn")?.addEventListener("click", toggleSimilar);
document.getElementById("wfm-gallery-similar-refresh-btn")?.addEventListener("click", applySimilar);
}
+282 -25
View File
@@ -5,7 +5,7 @@
import { showToast } from "./app.js";
import { t } from "./i18n.js";
import { loadFileIntoMetadataTab, extractPrompts, buildPromptItem } from "./metadata-tab.js";
import { loadFileIntoMetadataTab, extractPrompts, extractPromptsFromParameters, buildPromptItem } from "./metadata-tab.js";
import { loadWorkflowIntoEditor } from "./generate-tab.js";
import { escapeHtml, setupSearchClearBtn, getAiBackendDefaultUrl } from "./util.js";
import { comfyWorkflow } from "./comfyui-workflow.js";
@@ -15,6 +15,7 @@ import { callLLM, loadAiSettings, isValidBackendUrl } from "./ai-tab.js";
import { initImagePromptTab, activateImagePromptTab } from "./image-prompt-tab.js";
import { initStyleCatalogTab, activateStyleCatalogTab } from "./style-catalog-tab.js";
import { applyStoredVideoVolume } from "./settings-tab.js";
import { initSemantic, refreshSemanticDots, getSemanticMode, isSemanticOn, fetchSemanticImages, semanticCountLabel, setSemanticCountText, semanticErrorMessage, exitSimilarMode } from "./gallery-semantic.js";
// ── 定数 ─────────────────────────────────────────────────────
@@ -24,9 +25,11 @@ const API = {
imageMeta: (path) => `/wfm/gallery/image/meta?path=${encodeURIComponent(path)}`,
imageWorkflow: (path) => `/wfm/gallery/image/workflow?path=${encodeURIComponent(path)}`,
serveImage: (path) => `/wfm/gallery/image/serve?path=${encodeURIComponent(path)}`,
thumb: (path, w = 256) => `/wfm/gallery/image/thumb?path=${encodeURIComponent(path)}&w=${w}`,
// 音声の波形サムネイルは描画仕様を変えた際にブラウザのHTTPキャッシュ(24h)を回避するため版数を付ける
thumb: (path, w = 256) => `/wfm/gallery/image/thumb?path=${encodeURIComponent(path)}&w=${w}${isAudioFile({ path }) ? `&v=${WAVE_THUMB_VERSION}` : ""}`,
bulkFavorite: "/wfm/gallery/bulk/favorite",
bulkGroup: "/wfm/gallery/bulk/group",
convert: "/wfm/gallery/convert",
saveImageMeta: `/wfm/gallery/image/meta`,
toggleFavorite: `/wfm/gallery/image/favorite`,
groups: `/wfm/gallery/groups`,
@@ -66,6 +69,30 @@ export function isVideoFile(img) {
return VIDEO_EXTENSIONS.some(ext => source.endsWith(ext));
}
// 音声の波形サムネイルの版数。py/services/gallery_service.py の WAVE_THUMB_VERSION と同じ値にすること。
const WAVE_THUMB_VERSION = "wave2";
// 音声ファイル(ComfyUIの音楽生成出力)。サムネイルはサーバー生成の波形画像、再生は<audio>。
const AUDIO_EXTENSIONS = [".wav", ".mp3", ".ogg", ".m4a", ".flac", ".opus"];
export function isAudioFile(img) {
const source = (img?.ext || img?.filename || img?.path || "").toLowerCase();
return AUDIO_EXTENSIONS.some(ext => source.endsWith(ext));
}
// プレビュー/ライトボックス/比較用のメディア要素HTML。音声は波形画像+<audio controls>。
function mediaElementHtml(img, { imgClass, videoAttrs = "controls", audioAttrs = "controls", title = "" }) {
const src = API.serveImage(img.path);
const titleAttr = title ? ` title="${title}"` : "";
if (isVideoFile(img)) return `<video src="${src}" class="${imgClass}" ${videoAttrs}${titleAttr}></video>`;
if (isAudioFile(img)) {
return `<div class="wfm-gallery-audio-box">
<img src="${API.thumb(img.path, 512)}" class="${imgClass} wfm-gallery-audio-wave" alt="${escapeHtml(img.filename)}"${titleAttr}>
<audio src="${src}" class="wfm-gallery-audio-player" ${audioAttrs}></audio>
</div>`;
}
return `<img src="${src}" class="${imgClass}" alt="${escapeHtml(img.filename)}"${titleAttr}>`;
}
// ComfyUI Comic Creater からiframe越しに画像を受け取り、Generate UIのImage入力スロットへ直接セットする(I2I連携)。
// Comic Creater側の「I2Iへ送る」ボタンから
// iframe.contentWindow._wfmReceiveImageForI2I(blob, name, workflowData?, workflowFilename?) として呼ばれる。
@@ -427,6 +454,7 @@ async function loadFolderTree() {
state.folderTree = data;
tree.innerHTML = "";
renderTreeNode(data, tree, 0, true);
refreshSemanticDots(state.outputRoot);
if (isFirstLoad) {
// 初回のみ root を自動選択
@@ -482,6 +510,7 @@ function renderTreeNode(node, container, depth, isRoot) {
state.selectedImages.clear();
state.lastSelectionIndex = -1;
updateBulkBar();
exitSimilarMode();
loadImages();
// Delete Folder ボタン: rootは削除不可
const delBtn = document.getElementById("wfm-gallery-folder-delete-btn");
@@ -565,15 +594,37 @@ async function loadImages() {
params.recursive = "true";
}
// セマンティック検索/類似画像モード: サーバーがスコア順で返すためソート・各フィルタは適用しない
const semMode = getSemanticMode();
try {
if (semMode) {
const images = await fetchSemanticImages(semMode, state.currentFolder);
if (controller.signal.aborted) return;
state.images = images;
state.lastSelectionIndex = -1;
document.getElementById("wfm-gallery-count").textContent = `${images.length} images`;
setSemanticCountText(semanticCountLabel(semMode, images.length));
renderImages();
return;
}
const images = (await apiFetch(API.images(params), { signal: controller.signal })).images || [];
state.images = images;
state.lastSelectionIndex = -1;
document.getElementById("wfm-gallery-count").textContent = `${state.images.length} images`;
setSemanticCountText("");
renderImages();
updateTagFilter(state.images);
} catch (e) {
if (e.name === "AbortError") return;
if (semMode) {
state.images = [];
document.getElementById("wfm-gallery-count").textContent = "";
setSemanticCountText("");
grid.innerHTML = `<p class="wfm-placeholder">${escapeHtml(semanticErrorMessage(e))}</p>`;
showToast(semanticErrorMessage(e), "error");
return;
}
grid.innerHTML = `<p class="wfm-placeholder">Error: ${escapeHtml(e.message)}</p>`;
} finally {
if (_loadImagesAbortController === controller) {
@@ -672,13 +723,21 @@ function createThumbCard(img) {
card.appendChild(imgEl);
// 動画ファイルは再生アイコンバッジを重ねて表示(サムネイル自体は先頭フレームのJPEG)
if (isVideoFile(img)) {
if (isVideoFile(img) || isAudioFile(img)) {
const playBadge = document.createElement("div");
playBadge.className = "wfm-gallery-thumb-video-badge";
playBadge.textContent = "▶";
playBadge.textContent = isAudioFile(img) ? "♪" : "▶";
card.appendChild(playBadge);
}
// セマンティック検索/類似画像の結果にはコサイン類似度を表示
if (img.score != null) {
const scoreBadge = document.createElement("div");
scoreBadge.className = "wfm-gallery-thumb-score";
scoreBadge.textContent = img.score.toFixed(3);
card.appendChild(scoreBadge);
}
// お気に入りトグルボタン(カード右上)
const favBtn = document.createElement("button");
favBtn.className = `wfm-gallery-thumb-fav-btn${img.favorite ? " active" : ""}`;
@@ -800,7 +859,7 @@ function createTable(images) {
tdSize.textContent = formatBytes(img.size);
const tdDate = document.createElement("td");
tdDate.textContent = formatDate(img.mtime);
tdDate.textContent = formatDate(img.mtime) + (img.score != null ? ` (${img.score.toFixed(3)})` : "");
const tdTags = document.createElement("td");
tdTags.innerHTML = (img.tags || []).map(tag => `<span class="wfm-gallery-tag-badge">${escapeHtml(tag)}</span>`).join("");
@@ -879,6 +938,12 @@ function updateBulkBar() {
} else {
bar.style.display = "none";
}
// Convert ボタンは音声/動画を含む選択時のみ表示
const convertBtn = document.getElementById("wfm-gallery-bulk-convert");
if (convertBtn) {
convertBtn.style.display = count > 0 && [...state.selectedImages].some(p => isConvertible(imageByPath(p))) ? "" : "none";
convertBtn.textContent = t("galleryBulkConvert");
}
// Compare ボタンは 2〜4 枚選択時のみ表示
const compareBtn = document.getElementById("wfm-gallery-bulk-compare");
if (compareBtn) {
@@ -981,17 +1046,18 @@ async function loadImageDetail(img) {
// プレビュー(mp4は<video controls>、それ以外は<img>)
const preview = document.getElementById("wfm-gallery-detail-preview");
const isVideo = isVideoFile(img);
const mediaHtml = isVideo
? `<video src="${API.serveImage(img.path)}" class="wfm-gallery-detail-img" controls title="Double-click to enlarge"></video>`
: `<img src="${API.serveImage(img.path)}" class="wfm-gallery-detail-img" alt="${escapeHtml(img.filename)}" title="Double-click to enlarge">`;
const isAudio = isAudioFile(img);
// 音声は波形の下にプレイヤーが要るため、固定高さの枠をやめて内容なりの高さにする
preview.classList.toggle("wfm-gallery-detail-preview--audio", isAudio);
preview.innerHTML = `
<div class="wfm-gallery-preview-wrapper">
${mediaHtml}
${mediaElementHtml(img, { imgClass: "wfm-gallery-detail-img", title: "Double-click to enlarge" })}
</div>
`;
const previewMediaEl = preview.querySelector(isVideo ? "video" : "img");
previewMediaEl.addEventListener("dblclick", () => openLightbox(img));
if (isVideo) applyStoredVideoVolume(previewMediaEl);
if (isAudio) applyStoredVideoVolume(preview.querySelector("audio"));
// ファイル名
document.getElementById("wfm-gallery-detail-filename").textContent = img.filename;
@@ -1024,7 +1090,7 @@ async function loadImageDetail(img) {
// Promptタブは prompt_workflow (API形式優先) を使う。トップレベルとサブグラフに
// 独立した複数系統を持つワークフローでは workflow (UI形式) からの抽出だと
// トップレベル系統しか拾えないことがあるため(Metadataタブと同じ優先順位に揃える)。
renderPromptTab(wfRes.prompt_workflow ?? state.embeddedWorkflow);
renderPromptTab(wfRes.prompt_workflow ?? state.embeddedWorkflow, metaRes.embedded);
} catch (e) {
renderWorkflowJson(null);
_updateCopyCanvasBtn();
@@ -1138,7 +1204,7 @@ function renderWorkflowJson(workflow) {
// 複数候補があるケース(PromptStyler等で正負が複数生成される場合)に対応するため、
// Metadataタブと同じ POS/NEGリスト → クリックで全文表示のUI(buildPromptItem)をそのまま
// 再利用する。選択画像を切り替えるたびにマウス操作無しで読めるよう、先頭項目を自動選択する。
function renderPromptTab(workflow) {
function renderPromptTab(workflow, embedded = null) {
const listEl = document.getElementById("wfm-gallery-prompt-list");
const fullArea = document.getElementById("wfm-gallery-prompt-full");
const fullLabel = document.getElementById("wfm-gallery-prompt-full-label");
@@ -1148,12 +1214,14 @@ function renderPromptTab(workflow) {
fullArea.value = "";
fullLabel.textContent = "";
if (!workflow) {
// ワークフローが無い画像はA1111/Forge/Fooocusの parameters テキストから抽出する
const extracted = workflow ? extractPrompts(workflow) : extractPromptsFromParameters(embedded);
if (!extracted) {
listEl.innerHTML = `<div class="wfm-meta-item" style="opacity:0.6;">No workflow embedded in this image.</div>`;
return;
}
const { positives, negatives, texts } = extractPrompts(workflow);
const { positives, negatives, texts } = extracted;
const allPrompts = [
...positives.map(p => ({ type: "positive", text: p })),
...negatives.map(p => ({ type: "negative", text: p })),
@@ -1701,10 +1769,9 @@ function openMoveModal(paths) {
overlay.className = "wfm-gallery-lightbox";
overlay.innerHTML = `
<div class="wfm-gallery-move-modal">
<div class="wfm-gallery-move-modal-title">Move ${paths.length} image(s) to folder:</div>
<select id="wfm-gallery-move-dest-sel" class="wfm-select wfm-gallery-move-dest-sel">
${destinations.map(f => `<option value="${escapeHtml(f.abs_path)}">${escapeHtml(f.name)}</option>`).join("")}
</select>
<div class="wfm-gallery-move-modal-title">${escapeHtml(t("moveToFolderTitle", paths.length))}</div>
<input type="text" id="wfm-gallery-move-filter" class="wfm-input wfm-gallery-move-filter" placeholder="${escapeHtml(t("galleryMoveFilterPlaceholder"))}" autocomplete="off">
<select id="wfm-gallery-move-dest-sel" class="wfm-select wfm-gallery-move-dest-sel" size="8"></select>
<div class="wfm-gallery-move-modal-footer">
<button id="wfm-gallery-move-confirm" class="wfm-btn wfm-btn-primary">Move</button>
<button id="wfm-gallery-move-cancel" class="wfm-btn">Cancel</button>
@@ -1712,6 +1779,31 @@ function openMoveModal(paths) {
</div>
`;
const destSel = overlay.querySelector("#wfm-gallery-move-dest-sel");
const filterInput = overlay.querySelector("#wfm-gallery-move-filter");
const confirmBtn = overlay.querySelector("#wfm-gallery-move-confirm");
// フォルダ名・パスの部分一致(大文字小文字無視、空白区切りはAND)で絞り込む
const renderDestinations = () => {
const words = filterInput.value.trim().toLowerCase().split(/\s+/).filter(Boolean);
const matched = destinations.filter(f => {
const hay = `${f.name} ${f.abs_path}`.toLowerCase();
return words.every(w => hay.includes(w));
});
destSel.innerHTML = matched
.map(f => `<option value="${escapeHtml(f.abs_path)}" title="${escapeHtml(f.abs_path)}">${escapeHtml(f.name)}</option>`)
.join("");
if (matched.length > 0) destSel.selectedIndex = 0;
confirmBtn.disabled = matched.length === 0;
};
filterInput.addEventListener("input", renderDestinations);
filterInput.addEventListener("keydown", (e) => {
if (e.key === "Enter") { e.preventDefault(); if (!confirmBtn.disabled) confirmBtn.click(); }
else if (e.key === "ArrowDown") { e.preventDefault(); destSel.focus(); }
});
destSel.addEventListener("dblclick", () => { if (!confirmBtn.disabled) confirmBtn.click(); });
renderDestinations();
setTimeout(() => filterInput.focus(), 0); // 開いたらすぐ入力できるように
overlay.querySelector("#wfm-gallery-move-cancel").addEventListener("click", () => overlay.remove());
overlay.addEventListener("click", (e) => { if (e.target === overlay) overlay.remove(); });
overlay.querySelector("#wfm-gallery-move-confirm").addEventListener("click", async () => {
@@ -1724,6 +1816,146 @@ function openMoveModal(paths) {
document.body.appendChild(overlay);
}
// ── 変換(音声変換 / 動画からの音声抽出) ───────────────────────
const CONVERT_FORMATS = {
flac: { bitrate: false },
mp3: { bitrate: true, bitrates: [320, 256, 192, 128, 96], defaultBitrate: 192 },
opus: { bitrate: true, bitrates: [192, 128, 96, 64, 32], defaultBitrate: 128 },
m4a: { bitrate: true, bitrates: [256, 192, 160, 128, 96], defaultBitrate: 192 },
wav: { bitrate: false },
};
// パス→画像のMap。state.imagesは再代入/filterで新しい配列になるため、配列ごとにキャッシュする
// (選択のたびに O(選択数 × 画像数) の線形探索をしないため)。
const _imageByPathCache = new WeakMap();
function imageByPath(path) {
let map = _imageByPathCache.get(state.images);
if (!map) {
map = new Map(state.images.map(i => [i.path, i]));
_imageByPathCache.set(state.images, map);
}
return map.get(path);
}
function isConvertible(img) {
return !!img && (isAudioFile(img) || isVideoFile(img));
}
function convertTargets(paths) {
return paths.map(imageByPath).filter(isConvertible);
}
function openConvertModal(paths) {
const targets = convertTargets(paths);
if (targets.length === 0) {
showToast(t("convertNoTarget"), "info");
return;
}
const hasVideo = targets.some(isVideoFile);
const overlay = document.createElement("div");
overlay.className = "wfm-gallery-lightbox";
overlay.innerHTML = `
<div class="wfm-gallery-move-modal wfm-gallery-convert-modal">
<div class="wfm-gallery-move-modal-title">${escapeHtml(t("convertTitle", targets.length))}</div>
${hasVideo ? `<div class="wfm-gallery-convert-note">${escapeHtml(t("convertVideoNote"))}</div>` : ""}
<label class="wfm-gallery-convert-row">${escapeHtml(t("convertFormat"))}
<select id="wfm-convert-format" class="wfm-select">
${Object.keys(CONVERT_FORMATS).map(f => `<option value="${f}">${f.toUpperCase()}</option>`).join("")}
</select>
</label>
<label class="wfm-gallery-convert-row" id="wfm-convert-bitrate-row">${escapeHtml(t("convertBitrate"))}
<select id="wfm-convert-bitrate" class="wfm-select"></select>
</label>
<label class="wfm-gallery-convert-row">${escapeHtml(t("convertSampleRate"))}
<select id="wfm-convert-rate" class="wfm-select">
<option value="0">${escapeHtml(t("convertKeep"))}</option>
<option value="44100">44100 Hz</option>
<option value="48000">48000 Hz</option>
</select>
</label>
<label class="wfm-gallery-convert-row">${escapeHtml(t("convertChannels"))}
<select id="wfm-convert-channels" class="wfm-select">
<option value="keep">${escapeHtml(t("convertKeep"))}</option>
<option value="mono">${escapeHtml(t("convertMono"))}</option>
<option value="stereo">${escapeHtml(t("convertStereo"))}</option>
</select>
</label>
<label class="wfm-gallery-convert-check"><input type="checkbox" id="wfm-convert-inherit" checked> ${escapeHtml(t("convertInherit"))}</label>
<label class="wfm-gallery-convert-check"><input type="checkbox" id="wfm-convert-copymeta" checked> ${escapeHtml(t("convertCopyMeta"))}</label>
<label class="wfm-gallery-convert-check"><input type="checkbox" id="wfm-convert-delete"> ${escapeHtml(t("convertDeleteOriginal"))}</label>
<div class="wfm-gallery-convert-status" id="wfm-convert-status"></div>
<div class="wfm-gallery-move-modal-footer">
<button id="wfm-convert-run" class="wfm-btn wfm-btn-primary">${escapeHtml(t("convertRun"))}</button>
<button id="wfm-convert-cancel" class="wfm-btn">${escapeHtml(t("convertCancel"))}</button>
</div>
</div>
`;
const q = (id) => overlay.querySelector(id);
const fmtSel = q("#wfm-convert-format");
const brSel = q("#wfm-convert-bitrate");
const syncFormat = () => {
const cfg = CONVERT_FORMATS[fmtSel.value];
q("#wfm-convert-bitrate-row").style.display = cfg.bitrate ? "" : "none";
if (cfg.bitrate) {
brSel.innerHTML = cfg.bitrates
.map(b => `<option value="${b}"${b === cfg.defaultBitrate ? " selected" : ""}>${b} kbps</option>`).join("");
}
// タグを持てない形式ではタグ引き継ぎ指定が無効であることを見せる
q("#wfm-convert-inherit").disabled = fmtSel.value === "wav";
};
fmtSel.addEventListener("change", syncFormat);
syncFormat();
let running = false;
q("#wfm-convert-cancel").addEventListener("click", () => { if (!running) overlay.remove(); });
overlay.addEventListener("click", (e) => { if (e.target === overlay && !running) overlay.remove(); });
q("#wfm-convert-run").addEventListener("click", async () => {
const deleteOriginal = q("#wfm-convert-delete").checked;
if (deleteOriginal && !window.confirm(t("convertConfirmDelete", targets.length))) return;
running = true;
q("#wfm-convert-run").disabled = true;
q("#wfm-convert-cancel").disabled = true;
const opts = {
format: fmtSel.value,
bitrate: CONVERT_FORMATS[fmtSel.value].bitrate ? Number(brSel.value) : 0,
sample_rate: Number(q("#wfm-convert-rate").value),
channels: q("#wfm-convert-channels").value,
inherit_metadata: q("#wfm-convert-inherit").checked,
copy_gallery_meta: q("#wfm-convert-copymeta").checked,
delete_original: deleteOriginal,
};
let ok = 0, fail = 0, tagsSkipped = false;
for (let i = 0; i < targets.length; i++) {
q("#wfm-convert-status").textContent = t("convertProgress", i + 1, targets.length);
try {
const res = await fetch(API.convert, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ path: targets[i].path, ...opts }),
});
const data = await res.json();
if (!data.ok) throw new Error(data.error || `HTTP ${res.status}`);
ok++;
if (data.tags_skipped) tagsSkipped = true;
if (data.deleted) state.selectedImages.delete(targets[i].path);
} catch (e) {
fail++;
console.warn("[Gallery] convert failed:", targets[i].filename, e);
}
}
overlay.remove();
showToast(t("convertDone", ok, fail), fail ? "error" : "success");
if (tagsSkipped && opts.inherit_metadata) showToast(t("convertTagsSkipped"), "info");
await loadImages();
updateBulkBar();
});
document.body.appendChild(overlay);
}
// ── 一括操作 ─────────────────────────────────────────────────
async function bulkAddToGroup(groupName) {
@@ -1807,9 +2039,9 @@ async function bulkSetFavorite(favoriteValue) {
function openLightbox(img) {
const overlay = document.createElement("div");
overlay.className = "wfm-gallery-lightbox";
const mediaHtml = isVideoFile(img)
? `<video src="${API.serveImage(img.path)}" class="wfm-gallery-lightbox-img" controls autoplay></video>`
: `<img src="${API.serveImage(img.path)}" class="wfm-gallery-lightbox-img" alt="${escapeHtml(img.filename)}">`;
const mediaHtml = mediaElementHtml(img, {
imgClass: "wfm-gallery-lightbox-img", videoAttrs: "controls autoplay", audioAttrs: "controls autoplay",
});
overlay.innerHTML = `
<div class="wfm-gallery-lightbox-inner">
${mediaHtml}
@@ -1824,6 +2056,7 @@ function openLightbox(img) {
});
document.body.appendChild(overlay);
if (isVideoFile(img)) applyStoredVideoVolume(overlay.querySelector("video"));
if (isAudioFile(img)) applyStoredVideoVolume(overlay.querySelector("audio"));
}
// ── 画像比較ライトボックス ─────────────────────────────────────
@@ -1836,9 +2069,11 @@ function openCompare(paths) {
overlay.className = "wfm-gallery-lightbox wfm-lightbox-compare";
const itemsHtml = imgs.map(img => {
const mediaHtml = isVideoFile(img)
? `<video src="${API.serveImage(img.path)}" class="wfm-lightbox-compare-img" controls></video>`
: `<img src="${API.serveImage(img.path)}" class="wfm-lightbox-compare-img" alt="${escapeHtml(img.filename)}" loading="lazy">`;
const mediaHtml = isAudioFile(img)
? mediaElementHtml(img, { imgClass: "wfm-lightbox-compare-img" })
: isVideoFile(img)
? `<video src="${API.serveImage(img.path)}" class="wfm-lightbox-compare-img" controls></video>`
: `<img src="${API.serveImage(img.path)}" class="wfm-lightbox-compare-img" alt="${escapeHtml(img.filename)}" loading="lazy">`;
return `
<div class="wfm-lightbox-compare-item">
${mediaHtml}
@@ -1859,7 +2094,7 @@ function openCompare(paths) {
}
});
document.body.appendChild(overlay);
overlay.querySelectorAll("video").forEach(applyStoredVideoVolume);
overlay.querySelectorAll("video, audio").forEach(applyStoredVideoVolume);
}
// ── outputパス取得 ────────────────────────────────────────────
@@ -2152,8 +2387,17 @@ function bindEvents() {
document.getElementById("wfm-gallery-search")?.addEventListener("input", (e) => {
state.search = e.target.value;
clearTimeout(_searchDebounceTimer);
// Semantic Search ON中は入力のたびにAPIを呼ばず、Enterで実行する(クエリ埋め込みにAPI呼び出しが要るため)。
// 全文消去した時だけ通常一覧へ即復帰する。
if (isSemanticOn() && state.search.trim()) return;
_searchDebounceTimer = setTimeout(loadImages, SEARCH_DEBOUNCE_MS);
});
document.getElementById("wfm-gallery-search")?.addEventListener("keydown", (e) => {
if (e.key === "Enter" && isSemanticOn() && !e.isComposing) {
clearTimeout(_searchDebounceTimer);
loadImages();
}
});
setupSearchClearBtn("wfm-gallery-search", "wfm-gallery-search-clear-btn", () => {
clearTimeout(_searchDebounceTimer);
state.search = "";
@@ -2309,6 +2553,11 @@ function bindEvents() {
exportSelectedImagesToZip([...state.selectedImages]);
});
document.getElementById("wfm-gallery-bulk-convert")?.addEventListener("click", () => {
if (state.selectedImages.size === 0) return;
openConvertModal([...state.selectedImages]);
});
document.getElementById("wfm-gallery-bulk-psd")?.addEventListener("click", () => {
if (state.selectedImages.size === 0) return;
exportSelectedImagesToPsd([...state.selectedImages]);
@@ -2319,7 +2568,7 @@ function bindEvents() {
if (!window._wfmImageEditTab) return;
const allPaths = [...state.selectedImages];
const paths = allPaths.filter(p => !/\.(mp4|webm)$/i.test(p));
const paths = allPaths.filter(p => !isVideoFile({ path: p }) && !isAudioFile({ path: p }));
if (paths.length === 0) {
showToast(t("editLayersNoImages"), "error");
return;
@@ -2446,7 +2695,15 @@ function bindEvents() {
if (groupFilter) groupFilter.value = "";
const favBtn = document.getElementById("wfm-gallery-fav-btn");
if (favBtn) favBtn.classList.remove("active");
exitSimilarMode();
loadImages();
});
}
initSemantic({
getSearch: () => state.search,
getFolder: () => state.currentFolder,
getSelected: () => state.selectedImage,
reload: loadImages,
});
}
+315 -24
View File
File diff suppressed because one or more lines are too long
+60 -11
View File
@@ -249,6 +249,7 @@ function extractLoRAs(wf) {
const ct = n.class_type ?? "";
if (ct === "LoraLoader") add(n.inputs?.lora_name, n.inputs?.strength_model, n.inputs?.strength_clip);
else if (ct === "LoraLoaderModelOnly") add(n.inputs?.lora_name, n.inputs?.strength, 1.0);
else if (ct === "ImageMetadataLoRALoader") { for (let i = 1; i <= 3; i++) add(n.inputs?.[`lora_${i}`], n.inputs?.[`strength_model_${i}`], n.inputs?.[`strength_clip_${i}`]); }
else if (ct === "Lora Loader (LoraManager)") {
const lorasData = n.inputs?.loras;
const list = lorasData?.__value__ ?? (Array.isArray(lorasData) ? lorasData : null);
@@ -274,17 +275,44 @@ function isPromptStylerNode(ct) { return ct.includes("PromptStyler"); }
const SAMPLER_SETTING_KEYS = ["seed", "noise_seed", "steps", "cfg", "sampler_name", "scheduler", "denoise"];
function extractSamplerSettings(apiWf) {
if (!apiWf || typeof apiWf !== "object" || Array.isArray(apiWf.nodes)) return null;
for (const n of Object.values(apiWf)) {
if (!n || typeof n !== "object" || !isSamplerNode(n.class_type ?? "")) continue;
const pick = (inputs, keys) => {
const out = {};
for (const key of keys) {
const v = inputs?.[key];
if (v !== undefined && !Array.isArray(v)) out[key] = v;
}
return out;
};
// Flux/SD3系はRandomNoise+BasicScheduler+KSamplerSelect+CFGGuider等に設定が分散するため、
// 補助ノードから不足分を補う。
const AUX = {
RandomNoise: ["noise_seed"],
BasicScheduler: ["steps", "scheduler", "denoise"],
KSamplerSelect: ["sampler_name"],
CFGGuider: ["cfg"],
DualCFGGuider: ["cfg"],
};
// 設定キーを最も多く持つサンプラーを主とし、同数なら後ろのノード(hires-fix等の最終パス)を優先する
let main = null, mainCount = 0;
const aux = {};
for (const [id, n] of Object.entries(apiWf)) {
if (!n || typeof n !== "object") continue;
const ct = n.class_type ?? "";
const inputs = n.inputs ?? {};
const settings = {};
for (const key of SAMPLER_SETTING_KEYS) {
const v = inputs[key];
if (v !== undefined && !Array.isArray(v)) settings[key] = v;
if (AUX[ct]) {
const got = pick(inputs, AUX[ct]);
for (const [k, v] of Object.entries(got)) if (!(k in aux)) aux[k] = v;
continue;
}
if (!isSamplerNode(ct)) continue;
const settings = pick(inputs, SAMPLER_SETTING_KEYS);
const count = Object.keys(settings).length;
if (count > 0 && count >= mainCount) {
main = settings; mainCount = count;
}
if (Object.keys(settings).length > 0) return settings;
}
return null;
const merged = { ...aux, ...(main ?? {}) };
return Object.keys(merged).length > 0 ? merged : null;
}
// CLIPTextEncodeEditPlus (model-and-prompt-from-metadata) の encode() と同じ結合ルール。
@@ -698,7 +726,7 @@ function extractPromptsLiteGraph(wf) {
}
// ── SD/Fooocus prompt extraction ──────────────────────────────
function parseSDAParameters(raw) {
export function parseSDAParameters(raw) {
const text = raw.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
const stepsMatch = text.match(/\nSteps:\s+\d/);
if (!stepsMatch) return null;
@@ -716,13 +744,27 @@ function parseSDAParameters(raw) {
while ((m = re.exec(paramsLine)) !== null) params[m[1].trim()] = m[2].trim().replace(/^"|"$/g, "");
return { positive, negative, params };
}
function parseFooocusMetadata(raw) {
export function parseFooocusMetadata(raw) {
let obj; try { obj = JSON.parse(raw); } catch { return null; }
if (!obj?.base_model) return null;
const toArray = v => !v ? [] : Array.isArray(v) ? v.filter(Boolean) : [String(v)];
return { checkpoint: obj.base_model, vae: (obj.vae && obj.vae !== "Default") ? obj.vae : null, positives: toArray(obj.full_prompt ?? obj.prompt), negatives: toArray(obj.full_negative_prompt ?? obj.negative_prompt) };
}
// ComfyUIワークフローを持たない画像(A1111/Forge/Fooocus)の埋め込みテキストからプロンプトを取り出す。
// embedded: PNG tEXt/iTXtのキー→文字列マップ。取り出せなければnull。
export function extractPromptsFromParameters(embedded) {
const raw = embedded?.parameters;
if (!raw || typeof raw !== "string") return null;
if (embedded.fooocus_scheme === "fooocus") {
const f = parseFooocusMetadata(raw);
return f ? { positives: f.positives, negatives: f.negatives, texts: [] } : null;
}
const p = parseSDAParameters(raw);
if (!p) return null;
return { positives: p.positive ? [p.positive] : [], negatives: p.negative ? [p.negative] : [], texts: [] };
}
// ── Master extractAllMetadata ─────────────────────────────────
export async function extractAllMetadata(file) {
const name = file.name.toLowerCase();
@@ -739,7 +781,7 @@ export async function extractAllMetadata(file) {
// 正しく解決するため、そちらを経由してから抽出する。通常のワークフローには影響しない。
if (Array.isArray(wf?.nodes)) {
try {
const converted = await comfyWorkflow.convertUiToApi(wf);
const converted = await comfyWorkflow.convertUiToApi(wf, { readOnly: true });
if (converted && Object.keys(converted).length > 0) {
apiWf = converted;
if (wf.definitions?.subgraphs?.length > 0) wf = converted;
@@ -1024,8 +1066,11 @@ export function initMetadataTab() {
}
}
// 連続で画像を開いたとき、先に始めた処理が後から描画して結果が混ざるのを防ぐ
let _loadSeq = 0;
async function handleFile(file, path) {
if (!file) return;
const seq = ++_loadSeq;
if (file.size > MAX_FILE_SIZE) {
fileInfo.textContent = t("metaFileTooLarge");
fileInfo.style.color = "var(--wfm-warning)";
@@ -1037,6 +1082,7 @@ export function initMetadataTab() {
clearAll();
const serverMeta = await fetchServerMeta(path);
if (seq !== _loadSeq) return;
// Show preview for images
const isImage = file.type.startsWith("image/") || file.name.toLowerCase().match(/\.(png|webp|jpg|jpeg)$/);
@@ -1059,6 +1105,7 @@ export function initMetadataTab() {
dropLabel.style.display = "flex";
}
if (seq !== _loadSeq) return;
const dims = (serverMeta?.width && serverMeta?.height) ? serverMeta : naturalDims;
renderNanobanana(serverMeta?.nanobanana ?? null);
@@ -1066,6 +1113,7 @@ export function initMetadataTab() {
try {
meta = await extractAllMetadata(file);
} catch (err) {
if (seq !== _loadSeq) return;
console.error("[MetadataTab]", err);
fileInfo.textContent = t("metaParseError");
fileInfo.style.color = "var(--wfm-danger)";
@@ -1073,6 +1121,7 @@ export function initMetadataTab() {
return;
}
if (seq !== _loadSeq) return;
if (!meta) {
// nanobanana(Gemini API経由)の画像はComfyUIワークフローを埋め込んでいないため
// extractAllMetadataはnullを返す。その場合でもnanobanana履歴があれば
+131 -19
View File
@@ -28,8 +28,35 @@ const state = {
currentPage: 0,
selectedNodes: new Set(),
lastSelectionIndex: -1,
renderToken: 0,
};
// 数千件のカード/行を一度にDOMへ作ると描画が固まるため、先頭だけ同期で作り
// 残りはフレームごとに分割して追加する。新しい描画が始まったら古い分は打ち切る。
const RENDER_FIRST_CHUNK = 120;
const RENDER_CHUNK = 250;
function appendChunked(items, parent, build) {
const token = ++state.renderToken;
let i = 0;
const step = (size) => {
if (token !== state.renderToken) return;
const frag = document.createDocumentFragment();
const end = Math.min(i + size, items.length);
for (; i < end; i++) frag.appendChild(build(items[i]));
parent.appendChild(frag);
if (i < items.length) requestAnimationFrame(() => step(RENDER_CHUNK));
};
step(RENDER_FIRST_CHUNK);
}
function debounce(fn, ms) {
let timer = null;
return (...args) => {
clearTimeout(timer);
timer = setTimeout(() => fn(...args), ms);
};
}
// ── Helpers ───────────────────────────────────────────────
function extractPackageName(pythonModule) {
@@ -56,6 +83,40 @@ function categoryBadgeHtml(cat) {
// ── API ───────────────────────────────────────────────────
// 軽量一覧(入力定義なし)。/object_info より桁違いに速い。失敗時は null。
async function fetchLightNodes() {
try {
const res = await fetch("/api/wfm/nodes/light");
if (!res.ok) return null;
const list = await res.json();
if (!Array.isArray(list)) return null;
return list.map(info => ({
name: info.name,
display_name: info.display_name || info.name,
description: info.description || "",
category: info.category || "uncategorized",
python_module: info.python_module || "",
package: extractPackageName(info.python_module),
input: null, // /object_info で後から補完
input_order: {},
output: info.output || [],
output_name: info.output_name || [],
output_node: info.output_node || false,
search_aliases: info.search_aliases || [],
deprecated: info.deprecated || false,
experimental: info.experimental || false,
}));
} catch { return null; }
}
// 入力定義などを full node (object_info 由来) から既存ノードへ反映する
function mergeFullNode(target, full) {
target.input = full.input;
target.input_order = full.input_order;
target.output = full.output;
target.output_name = full.output_name;
}
async function fetchAllNodes() {
const data = await comfyUI.fetchAllObjectInfo();
return Object.entries(data).map(([name, info]) => ({
@@ -76,6 +137,15 @@ async function fetchAllNodes() {
}));
}
// 検索用の小文字化済み文字列(タグ以外)を1回だけ作る。キー入力ごとの再計算を避ける。
function searchBase(node) {
if (node._searchBase === undefined) {
node._searchBase = [node.name, node.display_name, node.description, ...(node.search_aliases || [])]
.join(" ").toLowerCase();
}
return node._searchBase;
}
async function fetchNodeMetadata() {
try {
const res = await fetch("/api/wfm/nodes/metadata");
@@ -189,13 +259,10 @@ function filterNodes() {
if (state.searchText) {
const q = state.searchText.toLowerCase();
const meta = state.nodeMetadata[node.name];
const searchable = [
node.name, node.display_name, node.description,
...(node.search_aliases || []),
...(meta?.tags || []),
].join(" ").toLowerCase();
if (!searchable.includes(q)) return false;
if (!searchBase(node).includes(q)) {
const tags = state.nodeMetadata[node.name]?.tags;
if (!tags || !tags.join(" ").toLowerCase().includes(q)) return false;
}
}
return true;
@@ -255,6 +322,7 @@ function renderNodeGrid() {
if (countEl) countEl.textContent = `${filtered.length} / ${state.allNodes.length}`;
if (filtered.length === 0) {
state.renderToken++; // 実行中の分割描画を止める(止めないと古いカードが追加され続ける)
grid.innerHTML = `<p class="wfm-placeholder">${t("noNodesFound")}</p>`;
return;
}
@@ -265,7 +333,7 @@ function renderNodeGrid() {
}
grid.innerHTML = "";
filtered.forEach(node => grid.appendChild(createNodeCard(node)));
appendChunked(filtered, grid, createNodeCard);
}
function createNodeCard(node) {
@@ -341,7 +409,7 @@ function renderNodeTableView(grid, filtered) {
<th>In</th><th>Out</th>
</tr></thead>`;
const tbody = document.createElement("tbody");
filtered.forEach(node => {
const buildRow = (node) => {
const meta = state.nodeMetadata[node.name] || {};
const tr = document.createElement("tr");
tr.className = "wfm-nodes-table-row";
@@ -352,15 +420,16 @@ function renderNodeTableView(grid, filtered) {
if (state.selectedNodes.has(node.name)) {
tr.classList.add("multi-selected");
}
const inputCount = Object.keys(node.input.required || {}).length +
Object.keys(node.input.optional || {}).length;
const inputCount = node.input
? Object.keys(node.input.required || {}).length + Object.keys(node.input.optional || {}).length
: "\u2026";
const outputCount = (node.output || []).length;
tr.innerHTML = `
<td><button class="${meta.favorite ? "wfm-fav-btn active" : "wfm-fav-btn"}">${meta.favorite ? "\u2605" : "\u2606"}</button></td>
<td title="${escapeHtml(node.name)}">${escapeHtml(node.display_name)}</td>
<td>${packageBadgeHtml(node.package)}</td>
<td>${escapeHtml(node.category)}</td>
<td>${inputCount}</td><td>${outputCount}</td>`;
<td class="wfm-nodes-in-count">${inputCount}</td><td>${outputCount}</td>`;
tr.querySelector(".wfm-fav-btn").addEventListener("click", e => {
e.stopPropagation();
toggleFavorite(node.name, e.currentTarget);
@@ -390,10 +459,11 @@ function renderNodeTableView(grid, filtered) {
showNodeSidePanel(node);
}
});
tbody.appendChild(tr);
});
return tr;
};
table.appendChild(tbody);
grid.appendChild(table);
appendChunked(filtered, tbody, buildRow);
}
// ── Favorite ──────────────────────────────────────────────
@@ -526,6 +596,19 @@ function showNodeSidePanel(node) {
renderSideDetails(node);
renderSideIO(node);
renderSideGroups(node);
// 入力定義が未取得なら、そのノード分だけ先に取得して差し替える
if (!node.input) {
comfyUI.fetchObjectInfo(node.name).then(res => {
const full = res?.[node.name];
if (!full) return;
node.input = full.input || {};
node.input_order = full.input_order || {};
node.output = full.output || node.output;
node.output_name = full.output_name || node.output_name;
if (state.selectedNode === node) renderSideIO(node);
}).catch(() => {});
}
}
function renderSideDetails(node) {
@@ -591,8 +674,8 @@ function renderSideIO(node) {
const el = document.getElementById("wfm-nodes-side-io");
if (!el) return;
const required = node.input.required || {};
const optional = node.input.optional || {};
const required = node.input?.required || {};
const optional = node.input?.optional || {};
let html = `<h4 style="margin:0 0 8px;">${t("nodesInputs")}</h4>`;
html += `<table class="wfm-node-io-table"><thead><tr><th>Name</th><th>Type</th><th>Details</th></tr></thead><tbody>`;
@@ -1128,6 +1211,30 @@ function showEditSetModal(set) {
// ── Data Loading ──────────────────────────────────────────
async function loadFullNodeDefinitions(nodes) {
try {
const full = await fetchAllNodes();
// 取得中に再読み込みされていたら破棄
if (state.allNodes !== nodes) return;
const byName = new Map(full.map(n => [n.name, n]));
for (const node of nodes) {
const f = byName.get(node.name);
if (f) mergeFullNode(node, f);
}
// テーブル表示のIn列を埋める
document.querySelectorAll("#wfm-nodes-grid tr.wfm-nodes-table-row").forEach(tr => {
const node = byName.get(tr.dataset.nodeName);
const cell = tr.querySelector(".wfm-nodes-in-count");
if (node && cell) {
cell.textContent = Object.keys(node.input.required || {}).length + Object.keys(node.input.optional || {}).length;
}
});
if (state.selectedNode) renderSideIO(state.selectedNode);
} catch (err) {
console.warn("Failed to load full node definitions:", err);
}
}
async function loadNodesData() {
const placeholder = document.getElementById("wfm-nodes-placeholder");
if (placeholder) placeholder.textContent = t("loading");
@@ -1138,11 +1245,13 @@ async function loadNodesData() {
return;
}
const [nodes, metadata, sets] = await Promise.all([
fetchAllNodes(),
// 一覧は軽量APIで先に表示し、入力定義を含む /object_info は裏で取得する
const [lightNodes, metadata, sets] = await Promise.all([
fetchLightNodes(),
fetchNodeMetadata(),
fetchNodeSets(),
]);
const nodes = lightNodes ?? await fetchAllNodes();
state.allNodes = nodes;
state.nodeMetadata = metadata;
@@ -1152,6 +1261,8 @@ async function loadNodesData() {
renderFilters();
renderNodeGrid();
if (lightNodes) loadFullNodeDefinitions(nodes);
} catch (err) {
console.error("Failed to load nodes:", err);
if (placeholder) placeholder.textContent = t("nodesConnectToComfyUI");
@@ -1189,10 +1300,11 @@ export function initNodesTab() {
// Search
const searchInput = document.getElementById("wfm-nodes-search");
if (searchInput) {
const rerender = debounce(() => renderNodeGrid(), 150);
searchInput.addEventListener("input", () => {
state.searchText = searchInput.value;
state.currentPage = 0;
renderNodeGrid();
rerender();
});
}
setupSearchClearBtn("wfm-nodes-search", "wfm-nodes-search-clear-btn", () => {
+32 -11
View File
@@ -8,11 +8,11 @@
* to the real Gallery tab via the "Open in Gallery" button.
*/
import { VIDEO_GROUP, VTEMP_GROUP, ensureVideoGroup, isVideoFile } from "./gallery-tab.js";
import { VIDEO_GROUP, VTEMP_GROUP, ensureVideoGroup, isVideoFile, isAudioFile } from "./gallery-tab.js";
import { setSourcePreview } from "./video-preview.js";
import { showToast } from "./app.js";
import { t } from "./i18n.js";
import { addClipFromFile, setBgmFromFile, addPipFromFile } from "./video-edit-tab.js";
import { addClipFromFile, setBgmFromFile, addPipFromFile, addSoundFromFile } from "./video-edit-tab.js";
import { setBlockImageFromFile } from "./video-plan-tab.js";
// Sentinel for the "All Video Assets" option — not a real backend group, since
@@ -33,7 +33,7 @@ const _s = {
loaded: false, // becomes true once the Asset subtab has been shown at least once
searchQuery: "",
viewMode: "grid", // "grid" | "table"
kindFilter: "all", // "all" | "video" | "image"
kindFilter: "all", // "all" | "video" | "image" | "audio"
};
function _formatDate(mtime) {
@@ -44,7 +44,8 @@ function _filteredImages() {
const q = _s.searchQuery.trim().toLowerCase();
let list = _s.images;
if (_s.kindFilter === "video") list = list.filter((img) => isVideoFile(img));
else if (_s.kindFilter === "image") list = list.filter((img) => !isVideoFile(img));
else if (_s.kindFilter === "audio") list = list.filter((img) => isAudioFile(img));
else if (_s.kindFilter === "image") list = list.filter((img) => !isVideoFile(img) && !isAudioFile(img));
if (q) list = list.filter((img) => img.filename.toLowerCase().includes(q));
return list;
}
@@ -212,6 +213,7 @@ function _selectImage(img) {
// videos — anything tagged into it shows up here) preview via the pane's <img>
// companion element instead of trying to play them as a <video>.
async function _loadIntoSourcePreview(img) {
if (isAudioFile(img)) return; // 音声は映像プレビュー対象外(詳細パネルの<audio>で試聴)
try {
const res = await fetch(`/wfm/gallery/image/serve?path=${encodeURIComponent(img.path)}`);
if (!res.ok) throw new Error(String(res.status));
@@ -232,6 +234,7 @@ function _renderDetail(img) {
// via textContent/.value, never interpolated into the HTML string itself.
panel.innerHTML = `
<div class="wfm-video-asset-name" id="wfm-video-asset-name"></div>
${isAudioFile(img) ? `<audio id="wfm-video-asset-audio" class="wfm-gallery-audio-player" controls src="/wfm/gallery/image/serve?path=${encodeURIComponent(img.path)}" style="width:100%;margin-bottom:8px;"></audio>` : ""}
<label>Tags</label>
<div class="wfm-video-asset-tags" id="wfm-video-asset-tags"></div>
<div style="display:flex;gap:6px;">
@@ -241,10 +244,11 @@ function _renderDetail(img) {
<label style="margin-top:10px;">Memo</label>
<textarea id="wfm-video-asset-memo" class="wfm-textarea" rows="3"></textarea>
<button type="button" class="wfm-btn wfm-btn-sm" id="wfm-video-asset-memo-save" style="margin-top:6px;">Save Memo</button>
<button type="button" class="wfm-btn wfm-btn-sm" id="wfm-video-asset-send-to-edit" style="width:100%;margin-top:12px;">${t("videoEditSendToEdit")}</button>
<button type="button" class="wfm-btn wfm-btn-sm" id="wfm-video-asset-add-overlay" style="width:100%;margin-top:6px;">${t("videoAssetAddAsOverlay")}</button>
<button type="button" class="wfm-btn wfm-btn-sm" id="wfm-video-asset-set-bgm" style="width:100%;margin-top:6px;display:${isVideoFile(img) ? "block" : "none"};">${t("videoAssetSetAsBgm")}</button>
<div id="wfm-video-asset-set-plan-image-row" style="display:${isVideoFile(img) ? "none" : "flex"};gap:6px;margin-top:6px;">
<button type="button" class="wfm-btn wfm-btn-sm" id="wfm-video-asset-send-to-edit" style="width:100%;margin-top:12px;display:${isAudioFile(img) ? "none" : "block"};">${t("videoEditSendToEdit")}</button>
<button type="button" class="wfm-btn wfm-btn-sm" id="wfm-video-asset-add-overlay" style="width:100%;margin-top:6px;display:${isAudioFile(img) ? "none" : "block"};">${t("videoAssetAddAsOverlay")}</button>
<button type="button" class="wfm-btn wfm-btn-sm" id="wfm-video-asset-set-bgm" style="width:100%;margin-top:${isAudioFile(img) ? "12" : "6"}px;display:${isVideoFile(img) || isAudioFile(img) ? "block" : "none"};">${t("videoAssetSetAsBgm")}</button>
<button type="button" class="wfm-btn wfm-btn-sm" id="wfm-video-asset-add-sound" style="width:100%;margin-top:6px;display:${isAudioFile(img) ? "block" : "none"};">${t("videoAssetAddAsSound")}</button>
<div id="wfm-video-asset-set-plan-image-row" style="display:${isVideoFile(img) || isAudioFile(img) ? "none" : "flex"};gap:6px;margin-top:6px;">
<button type="button" class="wfm-btn wfm-btn-sm" id="wfm-video-asset-set-first" style="flex:1;">${t("videoAssetSetAsFirst")}</button>
<button type="button" class="wfm-btn wfm-btn-sm" id="wfm-video-asset-set-last" style="flex:1;">${t("videoAssetSetAsLast")}</button>
</div>
@@ -307,14 +311,14 @@ function _renderDetail(img) {
}
});
// Gallery lists no audio files, so a video asset's own audio track is the
// Asset-side way to pick a BGM (LoadAudio decodes audio from video files).
// BGM source: an audio asset directly, or a video asset's own audio track
// (LoadAudio decodes audio from video files).
panel.querySelector("#wfm-video-asset-set-bgm")?.addEventListener("click", async () => {
try {
const res = await fetch(`/wfm/gallery/image/serve?path=${encodeURIComponent(img.path)}`);
if (!res.ok) throw new Error(String(res.status));
const blob = await res.blob();
const file = new File([blob], img.filename, { type: blob.type || "video/mp4" });
const file = new File([blob], img.filename, { type: blob.type || (isAudioFile(img) ? "audio/mpeg" : "video/mp4") });
if (await setBgmFromFile(file, img.filename)) {
document.querySelector('.wfm-video-center-panel .wfm-video-subtab-btn[data-video-subtab="edit"]')?.click();
showToast(t("videoAssetSetAsBgmDone", img.filename), "success");
@@ -324,6 +328,23 @@ function _renderDetail(img) {
}
});
// Timeline sound (sound effect / voice): placed at the selected clip's start on the Audio track.
panel.querySelector("#wfm-video-asset-add-sound")?.addEventListener("click", async () => {
try {
const res = await fetch(`/wfm/gallery/image/serve?path=${encodeURIComponent(img.path)}`);
if (!res.ok) throw new Error(String(res.status));
const blob = await res.blob();
const file = new File([blob], img.filename, { type: blob.type || "audio/mpeg" });
if (await addSoundFromFile(file, img.filename)) {
document.querySelector('.wfm-video-center-panel .wfm-video-subtab-btn[data-video-subtab="edit"]')?.click();
document.getElementById("wfm-video-edit-track-audio")?.click();
showToast(t("videoAssetAddAsSoundDone", img.filename), "success");
}
} catch (err) {
showToast(t("errorWithMsg", err.message), "error");
}
});
const setAsPlanImage = async (which) => {
try {
const res = await fetch(`/wfm/gallery/image/serve?path=${encodeURIComponent(img.path)}`);
+8
View File
@@ -2601,6 +2601,14 @@ function _renderSoundList() {
}
}
// Entry point for video-asset-tab.js's "Add as Sound" button (an audio asset becomes
// a timeline sound at the selected clip's start). Resolves once the sound is added.
export async function addSoundFromFile(file, displayName) {
const before = _s.sounds.length;
await _addSound(file, displayName);
return _s.sounds.length > before;
}
// Entry point for video-asset-tab.js's "Set as BGM" button (a video asset's
// audio track becomes the BGM). Returns whether the BGM was actually set.
export async function setBgmFromFile(file, displayName) {
+20 -6
View File
@@ -1791,7 +1791,7 @@
<div class="wfm-help-card">
<h3 id="wfm-help-gallery-card-detail-title">Detail Panel</h3>
<ul>
<li id="wfm-help-gallery-7">Detail panel — Info tab: filename, size, dimensions (image width×height) or video duration, date, tags (add/remove), memo; <b>download icon</b> (on image hover) to download the single image; "Move To..." moves the image to another folder, "Delete File" removes it permanently</li>
<li id="wfm-help-gallery-7">Detail panel — Info tab: filename, size, dimensions (image width×height) or video duration, date, tags (add/remove), memo; <b>download icon</b> (on image hover) to download the single image; "Move To..." moves the image to another folder, "Delete File" removes it permanently. In the Move To dialog (also from the bulk bar), type in the filter box to narrow the folder list by name or path (space = AND); Enter moves to the first match.</li>
<li id="wfm-help-gallery-8">Detail panel — JSON tab: displays workflow JSON from PNG embedded data (prompt / workflow keys) or from metadata saved by the Generate UI tab; 'Copy &amp; Send Canvas' button copies to clipboard and stores the workflow for title-drag to canvas</li>
<li id="wfm-help-gallery-9">Detail panel — Groups tab: create, assign, remove, rename, and delete groups per image; the <b>__Feeder__</b> group is reserved (shown with 🔒) and cannot be renamed or deleted — rename and delete buttons are disabled when it is selected</li>
<li id="wfm-help-gallery-32">Detail panel — Prompt tab (selected by default): lists positive/negative prompts found in the embedded workflow; hidden for images in a <b>cc_nanobanana</b> folder, since those carry no embedded ComfyUI workflow — a <b>Nanobanana</b> tab (v0.7.14) appears there instead, shown only when that folder's <b>history.jsonl</b> generation log (written by ComfyUI Comic Creator's Nanobanana tab) has a matching entry for the selected image: engine, model, image size, seed, timestamp, and the prompt/negative prompt actually used</li>
@@ -1807,15 +1807,19 @@
<li id="wfm-help-gallery-17"><b>Send GenUI Image</b> button (toolbar, next to Image Edit): uploads the selected image to ComfyUI and sets it as the input for the first LoadImage node in the GenerateUI tab; automatically switches to GenerateUI → Input → Image; requires a workflow with at least one LoadImage node to be loaded first</li>
<li id="wfm-help-gallery-27"><b>Send to LI node</b> button (toolbar, next to Send GenUI Image): uploads the selected image and writes its filename into the image widget of the currently selected node on the ComfyUI canvas (falling back to the first LoadImage-type node found) — same destination as Image Edit tab's Send to LI node; requires Workflow Studio to have been opened from ComfyUI's top menu button</li>
<li id="wfm-help-gallery-31"><b>Send to LV node</b> button (toolbar, next to Send to LI node): the video equivalent — uploads the selected video and writes its filename into the file widget of the currently selected node, which must be ComfyUI's native <b>Load Video</b> node; only enabled when the selected item is a video (mp4/webm); requires Workflow Studio to have been opened from ComfyUI's top menu button</li>
<li id="wfm-help-gallery-28"><b>Create PSD</b> bulk button: combines all selected images into a single layered PSD file and downloads it — each image becomes its own layer (named after the file), positioned top-left on a canvas sized to the largest selected image; the last selected image ends up on top</li>
<li id="wfm-help-gallery-28"><b>Create PSD</b> bulk button: combines all selected images into a single layered PSD file and downloads it — each image becomes its own layer (named after the file), positioned top-left on a canvas sized to the largest selected image; the last selected image ends up on top Convert... bulk button (shown when audio/video files are selected): converts audio files — or extracts the audio track of videos — to FLAC / MP3 / Opus / M4A / WAV (bitrate, sample rate, channels selectable) into the same folder as new files; prompt/workflow tags are carried over for formats that can hold them (WAV cannot), favorite/tags/memo/groups can be carried over, and the originals can optionally be deleted afterwards.</li>
<li id="wfm-help-gallery-29"><b>Edit as Layers</b> bulk button: switches to the Image Edit tab and opens all selected images at once, each as an independent layer (video files in the selection are skipped); the last selected image ends up on top</li>
<li id="wfm-help-gallery-33">Semantic Search (v0.8.2): the S button next to the search box turns semantic search on. When ON, the search box finds images by meaning using Gemini Embedding 2 or the local EmbeddingGemma 2 (e.g. 'sunset city street') - press Enter to search; results are ordered by similarity with a score badge. When OFF it is the normal filename/tag/prompt search. Turning S on also shows a second toolbar row with Similar, the update button, the settings button and the result count. The settings button (gear) opens a window to build/update/delete the index for the selected folder (optionally with subfolders); choose the model with the radio buttons (Gemini needs GEMINI_API_KEY in the plugin's .env + a ComfyUI restart; EmbeddingGemma 2 runs locally and downloads its model on first use). Switching model or vector size needs 'Rebuild all'. Searches cover the selected folder tree; sort and tag/group filters do not apply to semantic results</li>
<li id="wfm-help-gallery-34">Similar toggle and update button (second toolbar row, shown while S is ON): Similar shows images similar to the selected one (an image not yet indexed is embedded on the fly). Selecting another image keeps the list as is; press the update button to show images similar to the newly selected one, or press Similar again to return to the normal list. Changing folder, Clear filters or turning S off also leaves similar mode</li>
<li id="wfm-help-gallery-35">EmbeddingGemma 2 performance: by default it runs on the CPU with half of the cores (about 6 s per image at full precision). Image precision trades accuracy for speed (Standard is about 2.5x and Light about 5x faster; vectors differ, so changing it needs 'Rebuild all'); fewer CPU threads keep the PC lighter while generating images. To use an NVIDIA GPU, install onnxruntime-gpu into ComfyUI's Python (pip uninstall onnxruntime, then pip install onnxruntime-gpu; the CUDA runtime/cuDNN that ComfyUI's PyTorch uses is picked up) and restart ComfyUI: indexing then takes about 0.1 s per image (measured on an RTX 4070, about 45x faster than the CPU) and the results are identical to the CPU's. DirectML is not supported (its vision model fails to run). The model is released from memory when indexing finishes and reloaded on the next search</li>
<li id="wfm-help-gallery-36">Modal tabs and the similarity threshold: the Semantic settings window has three tabs - Index (target folder, rebuild mode, progress, delete all), Model (Gemini / EmbeddingGemma 2, vector size, device, CPU threads, image precision, connection test) and Search. On the Search tab you can set a minimum score for similar-image results (e.g. 0.6 shows only images scoring 0.6 or higher), a minimum score for text search, and the maximum number of results. Scores differ by model, so each model keeps its own values; the list header shows the active threshold</li>
</ul>
</div>
<div class="wfm-help-card">
<h3 id="wfm-help-gallery-card-formats-title">Formats &amp; Auto-save</h3>
<ul>
<li id="wfm-help-gallery-10">Workflow auto-save: images generated in the Generate UI tab automatically have their workflow saved to gallery metadata and shown in the JSON tab</li>
<li id="wfm-help-gallery-11">Supports PNG (tEXt/iTXt), JPEG, WebP, GIF, MP4, WebM, PSD; workflow extraction reads ComfyUI prompt/workflow keys embedded in PNG metadata or in MP4/WebM container tags (as written by SaveVideo, e.g. MiniMax H3 output)</li>
<li id="wfm-help-gallery-11">Supports PNG (tEXt/iTXt), JPEG, WebP, GIF, MP4, WebM, PSD, and audio (WAV, MP3, OGG, M4A, FLAC, Opus — waveform thumbnail, built-in player, prompt/workflow tags written by ComfyUI's SaveAudio nodes); workflow extraction reads ComfyUI prompt/workflow keys embedded in PNG metadata (including chunks appended after IEND by ComfyUI-Custom-Scripts), WebP EXIF, or MP4/WebM container tags (as written by SaveVideo, e.g. MiniMax H3 output); the Prompt tab also reads A1111 / Forge / Fooocus <code>parameters</code> text when no workflow is embedded. Audio waveform thumbnails are colored by format (MP3 blue, WAV green, FLAC amber, OGG/Opus purple, M4A rose) so formats can be told apart at a glance; the detail panel shows the waveform with the player below it.</li>
<li id="wfm-help-gallery-30">PSD files: thumbnails and full-size previews show all layers merged together (hidden layers excluded); the underlying PSD file itself is left untouched — only the preview shown in Gallery is a flattened rendering</li>
</ul>
</div>
@@ -1874,7 +1878,7 @@
<ul>
<li id="wfm-help-sidepanel-1">Open from the Workflow Studio Library button in the ComfyUI toolbar</li>
<li id="wfm-help-sidepanel-2">Workflows tab: All / ★ Favorites / Groups sub-tabs; ★ shown for favorites in All view</li>
<li id="wfm-help-sidepanel-3">Nodes tab: All / ★ Favorites / Groups / Sets / 📂 Category / 🧩 Package sub-tabs; Category and Package show a dropdown to filter nodes</li>
<li id="wfm-help-sidepanel-3">Nodes tab: All / ★ Favorites / Groups / Sets / 📂 Category / 🧩 Package sub-tabs; Category and Package show a dropdown to filter nodes Alt+click a node to add/remove it from Favorites.</li>
<li id="wfm-help-sidepanel-11">Models tab: All / ★ Favorites / Groups / By Type sub-tabs</li>
<li id="wfm-help-sidepanel-4">Prompts tab: browse prompt presets with All/Favorites/Categories sub-tabs</li>
</ul>
@@ -1893,7 +1897,7 @@
<div class="wfm-help-card">
<h3 id="wfm-help-sidepanel-card-info-title">Information (I) tab</h3>
<ul>
<li id="wfm-help-sidepanel-13">I tab (Information): drop a ComfyUI PNG/WebP/MP4/JSON to inspect model, LoRA, and prompt metadata in the side panel — sub-tabs: model / lora / Prompts; supports UnetLoaderGGUF and QuadrupleCLIPLoader node types; MP4 reads the workflow/prompt tags embedded by SaveVideo, including all-in-one video nodes like MiniMax H3 ImageToVideo</li>
<li id="wfm-help-sidepanel-13">I tab (Information): drop a ComfyUI PNG/WebP/MP4/JSON to inspect model, LoRA, and prompt metadata in the side panel — sub-tabs: model / lora / Prompts; supports UnetLoaderGGUF, QuadrupleCLIPLoader, Lora Loader (LoraManager), and ImageMetadataLoRALoader node types (API format included); MP4 reads the workflow/prompt tags embedded by SaveVideo, including all-in-one video nodes like MiniMax H3 ImageToVideo</li>
<li id="wfm-help-sidepanel-14">Information tab — drag items to canvas: model sub-tab places the loader node (Checkpoint → CheckpointLoaderSimple, VAE → VAELoader, Diffusion Model → UNETLoader, Text Encoder → CLIPLoader); double-click also places at canvas center</li>
<li id="wfm-help-sidepanel-15">Information tab — LoRA sub-tab: drag individual LoRA to place LoraLoader; "Multiple LORA" section at bottom places all LoRAs in a single Lora Loader (LoraManager) node; Prompts sub-tab: drag a prompt to place CLIPTextEncode node with text pre-filled; click prompt to view full text and copy</li>
</ul>
@@ -2139,7 +2143,7 @@
<li id="wfm-help-video-9">Project tab (sidebar): lists saved Video Plans while the center Plan subtab is active, or saved Edit projects while the center Edit subtab is active (thumbnail/block or clip count, last updated) — click one to load it, or delete it with the × button; clicking the same tab again collapses the panel.</li>
<li id="wfm-help-video-10">Highlight color: the Prompt / Duration / First Image / Last Image / Aspect Ratio / Megapixels / Multiple / Noise Seed / Width / Height / Turbo Mode / Strength / Steps / Prompt Enhance labels turn the same highlight color as the GenerateUI Model tab (customizable in the Settings tab) when the loaded workflow actually supports that field — e.g. for a Wan2.2 workflow (no ResolutionSelector node), the Aspect Ratio / Megapixels / Multiple labels stay unhighlighted while Width / Height light up instead.</li>
<li id="wfm-help-video-11">Edit subtab: a lightweight clip timeline for trimming and concatenating video/image clips outside of generation. Add clips via the Video Source panel's "Add to Edit" button or the Asset subtab's "Send to Edit" button, then drag blocks to reorder (or use the ◀/▶ toolbar buttons), Duplicate/Delete the selected clip, and Clear to empty the timeline (this also resets the Audio section). The trim panel for the selected video clip shows a time ruler, a draggable playhead synced to the Source preview, and draggable in/out handles with time badges — image clips instead get a simple hold-duration field. ▶ Preview plays every ready clip back-to-back (trimmed to its in/out points) in the right-hand result preview; Export builds and runs the actual ComfyUI graph (trim → concatenate if more than one clip → save), tagging the result into the Video Temp group automatically. The Audio section of the export panel controls the soundtrack: "Keep original audio" (with its own volume in dB) keeps each clip's audio aligned to its segment even across multiple clips (image clips contribute silence), and "BGM..." lays one audio file over the whole video with its own volume and start offset (a longer BGM is cut to the video length). Each clip can also carry text overlays ("+ Add text" under the trim panel: text, start/end seconds within the clip, size as % of frame height, color, 9-position anchor, outline, background box); they are previewed live over the Source/Result panes and burned in on Export as a final pass. The BGM can be chosen with "BGM...", by dropping an audio/video file onto the Audio section, or with "Set as BGM" on a video in the Asset subtab (its audio track is used). Video clips can also be cropped: "Edit crop" shows a draggable/resizable rectangle over the Source preview (optionally locked to 16:9 / 9:16 / 1:1 / 4:3 / 3:4) — "Done" keeps it, "Cancel" restores the crop from before editing, and switching to another subtab/tab ends editing like Done; the export size becomes the first video clip's cropped size, and other clips of a different size are center-fit to it. ↶/↷ in the timeline toolbar (or Ctrl+Z / Ctrl+Y) undo and redo edits to clips, trims, crops, text and audio settings. The export panel's Save/Save As/Load buttons persist the timeline itself (clip order, trim points, crop, text overlays, audio/BGM settings, and which uploaded file each clip and the BGM refer to) as a JSON project file on the server. The project only stores references to those files in ComfyUI's input folder, so Load re-fetches each clip from its saved server reference and re-probes it — the original files must still exist (Settings → Full Backup can bundle them with "Include media used by Video Edit projects"). These saved projects also show up in the sidebar's Project tab whenever the Edit subtab is active (see the Project tab item).</li>
<li id="wfm-help-video-12">Asset subtab (center panel, next to Plan/Edit): browses every video/image tagged into Video Assets or Video Temp (via the group dropdown, or "All Video Assets" for both). Search filters by filename; the ▦/☰ buttons switch between a thumbnail grid and a table (filename/updated/tags). Selecting an item shows its tags/memo, loads it into the center preview, and offers "Send to Edit" (queues it as a clip in the Edit subtab) plus, for images only, "Set as First Image"/"Set as Last Image" (uploads it straight into the currently-selected Plan block, switching to the Plan subtab).</li>
<li id="wfm-help-video-12">Asset subtab (center panel, next to Plan/Edit): browses every video/image tagged into Video Assets or Video Temp (via the group dropdown, or "All Video Assets" for both). Search filters by filename; the ▦/☰ buttons switch between a thumbnail grid and a table (filename/updated/tags). Selecting an item shows its tags/memo, loads it into the center preview, and offers "Send to Edit" (queues it as a clip in the Edit subtab) plus, for images only, "Set as First Image"/"Set as Last Image" (uploads it straight into the currently-selected Plan block, switching to the Plan subtab). Audio files tagged into Video Assets also appear (&quot;Audio Only&quot; filter): the detail panel gets a player plus &quot;Set as BGM&quot; and &quot;Add as sound&quot; (places it on the Edit subtab's Audio track at the selected clip's start).</li>
</ul>
</div>
<button class="wfm-help-jump-btn wfm-btn wfm-btn-sm" data-jump-tab="video">Go to Video Tab</button>
@@ -2265,6 +2269,7 @@
<input type="text" id="wfm-gallery-search" class="wfm-search-input" placeholder="Search images...">
<button class="wfm-search-clear-btn" id="wfm-gallery-search-clear-btn" title="Clear search">✕</button>
</div>
<button id="wfm-gallery-semantic-toggle" class="wfm-btn wfm-btn-sm wfm-gallery-sem-toggle" title="Semantic Search: when ON, the search box finds images by meaning (press Enter to search) and a second row with Similar / settings appears.">S</button>
<select id="wfm-gallery-tag-filter" class="wfm-select wfm-toolbar-select">
<option value="">All Tags</option>
</select>
@@ -2291,6 +2296,13 @@
<button id="wfm-gallery-send-lv-node-btn" class="wfm-btn wfm-btn-sm" title="Upload and write selected video's filename into the selected node's file widget (native Load Video node)">Send to LV node</button>
<button id="wfm-gallery-send-cc-btn" class="wfm-btn wfm-btn-sm" title="Send selected image to ComfyUI Comic Creator's selected panel" style="display:none;">Send CC</button>
</div>
<!-- Semantic Search ON時の2行目: 類似画像 / 更新 / 設定 / 結果件数 -->
<div id="wfm-gallery-sem-row" class="wfm-toolbar-row wfm-gallery-sem-row" style="display:none;">
<button id="wfm-gallery-similar-btn" class="wfm-btn wfm-btn-sm wfm-sem-sim" title="Show images similar to the selected one (toggle)">Similar</button>
<button id="wfm-gallery-similar-refresh-btn" class="wfm-btn wfm-btn-sm wfm-sem-sim" title="Update: show images similar to the currently selected image">&#8635;</button>
<button id="wfm-gallery-semantic-settings-btn" class="wfm-btn wfm-btn-sm" title="Semantic Search settings (index / model / search)">&#9881;</button>
<span id="wfm-gallery-sem-count" class="wfm-gallery-sem-count"></span>
</div>
<!-- 複数選択時の一括操作バー -->
<div id="wfm-gallery-bulk-bar" class="wfm-gallery-bulk-bar" style="display:none;">
<span id="wfm-gallery-bulk-count" class="wfm-gallery-bulk-count"></span>
@@ -2308,6 +2320,7 @@
<button id="wfm-gallery-bulk-export" class="wfm-btn wfm-btn-sm"></button>
<button id="wfm-gallery-bulk-psd" class="wfm-btn wfm-btn-sm"></button>
<button id="wfm-gallery-bulk-edit-layers" class="wfm-btn wfm-btn-sm"></button>
<button id="wfm-gallery-bulk-convert" class="wfm-btn wfm-btn-sm" style="display:none;"></button>
<button id="wfm-gallery-bulk-delete" class="wfm-btn wfm-btn-sm wfm-btn-danger"></button>
</div>
</div>
@@ -2860,6 +2873,7 @@
<option value="all">All Types</option>
<option value="video">Video Only</option>
<option value="image">Image Only</option>
<option value="audio">Audio Only</option>
</select>
<input type="text" id="wfm-video-asset-search" class="wfm-input" placeholder="Search filename..." style="flex:1;">
<button type="button" id="wfm-video-asset-view-grid" class="wfm-btn wfm-btn-sm wfm-video-asset-view-btn active" title="Thumbnail view">▦</button>
+71 -5
View File
@@ -96,6 +96,26 @@ const fetchObjectInfo = async () => {
} catch { return {}; }
};
const getNodeDef = (nodeType) => {
if (state.objectInfo[nodeType]) return state.objectInfo[nodeType];
return typeof LiteGraph !== "undefined" ? LiteGraph.registered_node_types[nodeType]?.nodeData : undefined;
};
let objectInfoPromise = null;
const ensureObjectInfo = () => {
if (objectInfoPromise) return objectInfoPromise;
const registered = typeof LiteGraph !== "undefined" ? LiteGraph.registered_node_types : {};
if (Object.values(registered).some(d => d?.nodeData?.python_module)) {
return (objectInfoPromise = Promise.resolve());
}
objectInfoPromise = fetchObjectInfo().then((info) => {
state.objectInfo = info;
// Category/Packageを表示中なら取得完了後に描き直す
if (state.topTab === "nodes" && (state.activeTab2 === "category" || state.activeTab2 === "package")) renderContent();
});
return objectInfoPromise;
};
const extractPackageName = (pythonModule) => {
if (!pythonModule || pythonModule === "nodes") return "ComfyUI (Built-in)";
const parts = pythonModule.split(".");
@@ -104,13 +124,15 @@ const extractPackageName = (pythonModule) => {
};
const loadData = async () => {
const [metadata, nodeSets, groups, objectInfo] = await Promise.all([
fetchMetadata(), fetchNodeSets(), fetchGroups(), fetchObjectInfo(),
// /object_info は数MBになりNodesタブ表示を待たせるため、ここでは待たない。
// Category/Package は LiteGraph の登録済みノード定義(nodeData)から取れるので、
// それが無い環境でのみバックグラウンドで取得する(ensureObjectInfo)。
const [metadata, nodeSets, groups] = await Promise.all([
fetchMetadata(), fetchNodeSets(), fetchGroups(),
]);
state.metadata = metadata;
state.nodeSets = nodeSets;
state.groups = groups;
state.objectInfo = objectInfo;
// Extract favorites from metadata
state.favorites = [];
@@ -121,6 +143,7 @@ const loadData = async () => {
}
}
state.loaded = true;
ensureObjectInfo();
};
// ============================================
@@ -2262,7 +2285,7 @@ const renderGroups = (container) => {
// ============================================
const getNodeCategory = (nodeType) => {
const info = state.objectInfo[nodeType];
const info = getNodeDef(nodeType);
if (info?.category) return info.category.split("/")[0] || "uncategorized";
// Fallback: LiteGraph type string often contains category as prefix
const def = typeof LiteGraph !== "undefined"
@@ -2273,7 +2296,7 @@ const getNodeCategory = (nodeType) => {
};
const getNodePackage = (nodeType) => {
const info = state.objectInfo[nodeType];
const info = getNodeDef(nodeType);
if (!info) return "ComfyUI (Built-in)";
return extractPackageName(info.python_module || "");
};
@@ -2407,9 +2430,51 @@ const createDraggableItem = (label, type, data, subtitle) => {
el.classList.remove("dragging");
});
// Alt+クリックでお気に入りのON/OFF(Nodesサブタブの単体ノード項目のみ)
if (type === "single" && data?.classType) {
el.addEventListener("click", (e) => {
if (!e.altKey) return;
e.preventDefault();
toggleNodeFavorite(data.classType, el);
});
}
return el;
};
const toggleNodeFavorite = async (name, el) => {
const prev = !!state.metadata[name]?.favorite;
const next = !prev;
try {
const res = await fetch("/api/wfm/nodes/metadata", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ nodeName: name, favorite: next }),
});
if (!res.ok) throw new Error(`HTTP ${res.status}`);
} catch (err) {
console.warn("[WFM] failed to toggle node favorite:", err);
return;
}
state.metadata[name] = { ...(state.metadata[name] || {}), favorite: next };
state.favorites = state.favorites.filter(n => n.name !== name);
if (next) state.favorites.push({ name, display_name: name });
if (state.topTab === "nodes" && !state.activeTab2 && state.activeTab === "favorites") {
renderContent(); // 解除した項目をFavorites一覧から外す
return;
}
// その他のタブはDOMだけ更新(パッケージ選択などの表示状態を保つ)
const label = el.querySelector(".wfm-nlp-item-label");
label?.querySelector(".wfm-nlp-fav-star")?.remove();
if (next && label) {
const star = document.createElement("span");
star.className = "wfm-nlp-fav-star";
star.textContent = "★";
label.prepend(star);
}
};
// ============================================
// Toggle & exports
// ============================================
@@ -3197,6 +3262,7 @@ function _extractLoRAs(wf) {
const ct = n.class_type ?? "";
if (ct === "LoraLoader") add(n.inputs?.lora_name, n.inputs?.strength_model, n.inputs?.strength_clip);
else if (ct === "LoraLoaderModelOnly") add(n.inputs?.lora_name, n.inputs?.strength, 1.0);
else if (ct === "ImageMetadataLoRALoader") { for (let i = 1; i <= 3; i++) add(n.inputs?.[`lora_${i}`], n.inputs?.[`strength_model_${i}`], n.inputs?.[`strength_clip_${i}`]); }
else if (ct === "Lora Loader (LoraManager)") {
const lorasData = n.inputs?.loras;
const list = lorasData?.__value__ ?? (Array.isArray(lorasData) ? lorasData : null);