| """
|
| 遅延ロード対応の共有モデル管理モジュール。
|
|
|
| 各モデルは初回呼び出し時にのみロードされる(遅延初期化パターン)。
|
| スレッドセーフのために各モデルに専用ロックを用意している。
|
| モデルパスやデバイス設定は config.py で一元管理する。
|
| """
|
|
|
| import sys
|
| import threading
|
| from pathlib import Path
|
|
|
|
|
| _ROOT = Path(__file__).parent.parent.parent
|
| if str(_ROOT) not in sys.path:
|
| sys.path.insert(0, str(_ROOT))
|
|
|
| from config import (
|
| GLM_OCR_PATH,
|
| LFM_MODEL_PATH,
|
| QWEN_MODEL_PATH,
|
| WHISPER_SMALL_ID,
|
| WHISPER_LARGE_ID,
|
| DEVICE,
|
| WHISPER_COMPUTE_TYPE,
|
| )
|
|
|
|
|
|
|
|
|
| _glm_processor = None
|
| _glm_model = None
|
| _glm_lock = threading.Lock()
|
|
|
|
|
| def is_glm_loaded() -> bool:
|
| """GLM-OCR モデルがロード済みかどうかを返す(ロックなし)。"""
|
| return _glm_model is not None
|
|
|
|
|
| def get_glm_model():
|
| """
|
| GLM-OCR モデルをローカルから遅延ロードして返す。
|
|
|
| Returns:
|
| tuple[AutoProcessor, AutoModelForImageTextToText]: (processor, model)
|
|
|
| Raises:
|
| Exception: モデルのロードに失敗した場合
|
| """
|
| global _glm_processor, _glm_model
|
| with _glm_lock:
|
| if _glm_model is None:
|
| from transformers import AutoProcessor, AutoModelForImageTextToText
|
|
|
| model_path = str(GLM_OCR_PATH)
|
| print(f"[OCR] GLM モデルをロード中: {model_path}")
|
| _glm_processor = AutoProcessor.from_pretrained(model_path)
|
| _glm_model = AutoModelForImageTextToText.from_pretrained(
|
| pretrained_model_name_or_path=model_path,
|
| torch_dtype="auto",
|
| device_map=DEVICE,
|
| )
|
| print("[OCR] GLM モデルのロード完了")
|
| return _glm_processor, _glm_model
|
|
|
|
|
|
|
|
|
| _lfm = None
|
| _lfm_init_lock = threading.Lock()
|
| _lfm_infer_lock = threading.Lock()
|
|
|
|
|
| def is_lfm_loaded() -> bool:
|
| """LFM 翻訳モデルがロード済みかどうかを返す(ロックなし)。"""
|
| return _lfm is not None
|
|
|
|
|
| def get_lfm():
|
| """
|
| LFM 2.5 (ONNX) 翻訳モデルを遅延ロードして返す。
|
|
|
| Returns:
|
| LFMInference: LFM 推論インスタンス
|
|
|
| Raises:
|
| Exception: モデルのロードに失敗した場合
|
| """
|
| global _lfm
|
| with _lfm_init_lock:
|
| if _lfm is None:
|
| from lfm25_run import LFMInference
|
|
|
| model_path = str(LFM_MODEL_PATH)
|
| print(f"[Translation] LFM モデルをロード中: {model_path}")
|
| _lfm = LFMInference(model_path)
|
| print("[Translation] LFM モデルのロード完了")
|
| return _lfm
|
|
|
|
|
| def get_lfm_lock() -> threading.Lock:
|
| """
|
| LFM 推論用の共有排他ロックを返す。
|
|
|
| 全ブループリントでこのロックを取得してから推論を実行すること。
|
|
|
| Returns:
|
| threading.Lock: 推論用ロック
|
| """
|
| return _lfm_infer_lock
|
|
|
|
|
|
|
|
|
| _qwen_processor = None
|
| _qwen_model = None
|
| _qwen_lock = threading.Lock()
|
|
|
|
|
| def get_qwen_model():
|
| """
|
| Qwen3-VL-2B モデルをローカルから遅延ロードして返す。
|
|
|
| Returns:
|
| tuple[AutoProcessor, AutoModelForImageTextToText]: (processor, model)
|
|
|
| Raises:
|
| Exception: モデルのロードに失敗した場合
|
| """
|
| global _qwen_processor, _qwen_model
|
| with _qwen_lock:
|
| if _qwen_model is None:
|
| from transformers import AutoProcessor, AutoModelForImageTextToText
|
|
|
| model_path = str(QWEN_MODEL_PATH)
|
| print(f"[Ledger] Qwen モデルをロード中: {model_path}")
|
| _qwen_processor = AutoProcessor.from_pretrained(
|
| model_path,
|
| min_pixels=256 * 28 * 28,
|
| max_pixels=768 * 28 * 28,
|
| )
|
| _qwen_model = AutoModelForImageTextToText.from_pretrained(
|
| model_path,
|
| torch_dtype="auto",
|
| device_map=DEVICE,
|
| low_cpu_mem_usage=True,
|
| )
|
| _qwen_model.eval()
|
| print("[Ledger] Qwen モデルのロード完了")
|
| return _qwen_processor, _qwen_model
|
|
|
|
|
|
|
|
|
| _whisper_small = None
|
| _whisper_small_lock = threading.Lock()
|
| _whisper_large = None
|
| _whisper_large_lock = threading.Lock()
|
|
|
|
|
| def is_whisper_small_loaded() -> bool:
|
| """Whisper Small モデルがロード済みかどうかを返す(ロックなし)。"""
|
| return _whisper_small is not None
|
|
|
|
|
| def is_whisper_large_loaded() -> bool:
|
| """Whisper Large モデルがロード済みかどうかを返す(ロックなし)。"""
|
| return _whisper_large is not None
|
|
|
|
|
| def get_whisper_small():
|
| """
|
| Whisper Small モデルを遅延ロードして返す。
|
|
|
| Returns:
|
| WhisperModel: faster-whisper Small モデルインスタンス
|
| """
|
| global _whisper_small
|
| with _whisper_small_lock:
|
| if _whisper_small is None:
|
| from faster_whisper import WhisperModel
|
| print(f"[Whisper] Small モデルをロード中: {WHISPER_SMALL_ID}")
|
| _whisper_small = WhisperModel(
|
| WHISPER_SMALL_ID,
|
| device=DEVICE,
|
| compute_type=WHISPER_COMPUTE_TYPE,
|
| )
|
| print("[Whisper] Small モデルのロード完了")
|
| return _whisper_small
|
|
|
|
|
| def get_whisper_large():
|
| """
|
| Whisper Large-v3-turbo モデルを遅延ロードして返す。
|
|
|
| Returns:
|
| WhisperModel: faster-whisper Large-v3-turbo モデルインスタンス
|
| """
|
| global _whisper_large
|
| with _whisper_large_lock:
|
| if _whisper_large is None:
|
| from faster_whisper import WhisperModel
|
| print(f"[Whisper] Large モデルをロード中: {WHISPER_LARGE_ID}")
|
| _whisper_large = WhisperModel(
|
| WHISPER_LARGE_ID,
|
| device=DEVICE,
|
| compute_type=WHISPER_COMPUTE_TYPE,
|
| )
|
| print("[Whisper] Large モデルのロード完了")
|
| return _whisper_large
|
|
|