File size: 7,036 Bytes
9a65320 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | """
遅延ロード対応の共有モデル管理モジュール。
各モデルは初回呼び出し時にのみロードされる(遅延初期化パターン)。
スレッドセーフのために各モデルに専用ロックを用意している。
モデルパスやデバイス設定は config.py で一元管理する。
"""
import sys
import threading
from pathlib import Path
# config.py のある ROOT を sys.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-OCR モデル(ocr ブループリント用) ─────────────────────────────────
_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 2.5 翻訳モデル(translation / ocr / whisper ブループリント共用) ──
_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
# ── Qwen3-VL モデル(ledger ブループリント用) ─────────────────────────────
_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 音声認識モデル(whisper ブループリント用) ────────────────────────
_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
|