| """ |
| 設定ファイル - パス設定専用(Tauriアプリ用) |
| """ |
|
|
| import os |
| import sys |
| from typing import Dict, Any |
| from path_manager import get_path_manager |
|
|
|
|
| class Config: |
| """設定管理クラス(Tauriアプリ用)""" |
| |
| |
| @classmethod |
| def _get_base_path(cls) -> str: |
| """ベースパスを取得(pyinstaller対応)""" |
| if getattr(sys, 'frozen', False): |
| |
| return os.path.dirname(sys.executable) |
| else: |
| |
| return os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| |
| |
| MECAB_CONFIG_PATH = "/opt/homebrew/etc/mecabrc" |
| MECAB_DICT_PATH = "/opt/homebrew/lib/mecab/dic/ipadic" |
| |
| |
| |
| DEFAULT_MODEL_PATH = os.path.expanduser("~/Documents/models/llama-3.2-3b-instruct-q4_k_m.gguf") |
| |
| |
| |
| try: |
| import importlib.util |
| spec = importlib.util.find_spec("sudachidict_core") |
| if spec and spec.origin: |
| |
| import os as _os |
| _pkg_dir = _os.path.dirname(spec.origin) |
| SUDACHI_DICT_PATH = _pkg_dir |
| else: |
| SUDACHI_DICT_PATH = "" |
| except Exception: |
| SUDACHI_DICT_PATH = "" |
|
|
| |
| FUGASHI_ARGS = f"-r {MECAB_CONFIG_PATH}" |
| |
| @classmethod |
| def get_mecab_config_path(cls) -> str: |
| """MeCab設定ファイルのパスを取得""" |
| return cls.MECAB_CONFIG_PATH |
| |
| @classmethod |
| def get_mecab_dict_path(cls) -> str: |
| """MeCab辞書のパスを取得""" |
| return cls.MECAB_DICT_PATH |
| |
| @classmethod |
| def get_fugashi_args(cls) -> str: |
| """fugashi用の引数を取得""" |
| return cls.FUGASHI_ARGS |
| |
| @classmethod |
| def get_default_model_path(cls) -> str: |
| """デフォルトのモデルパスを取得""" |
| return get_path_manager().get_model_path() |
| |
| @classmethod |
| def get_package_path(cls) -> str: |
| """パッケージパスを取得""" |
| return get_path_manager().get_package_path() |
| |
| @classmethod |
| def validate_paths(cls) -> Dict[str, bool]: |
| """パスの存在確認""" |
| return { |
| "mecab_config": os.path.exists(cls.MECAB_CONFIG_PATH), |
| "mecab_dict": os.path.exists(cls.MECAB_DICT_PATH), |
| "default_model": os.path.exists(cls.DEFAULT_MODEL_PATH) |
| } |
| |
| @classmethod |
| def print_status(cls): |
| """設定状況を表示""" |
| print("=== 設定状況 ===") |
| print(f"MeCab設定ファイル: {cls.MECAB_CONFIG_PATH}") |
| print(f"MeCab辞書: {cls.MECAB_DICT_PATH}") |
| print(f"デフォルトモデル: {cls.DEFAULT_MODEL_PATH}") |
| print(f"fugashi引数: {cls.FUGASHI_ARGS}") |
| print(f"Sudachi辞書パス検出: {getattr(cls, 'SUDACHI_DICT_PATH', '')}") |
| |
| print("\n=== パス存在確認 ===") |
| status = cls.validate_paths() |
| for name, exists in status.items(): |
| status_text = "✓" if exists else "✗" |
| print(f"{name}: {status_text}") |
|
|
|
|
| |
| def load_config_from_env(): |
| """環境変数から設定を読み込み""" |
| Config.MECAB_CONFIG_PATH = os.getenv("MECAB_CONFIG_PATH", Config.MECAB_CONFIG_PATH) |
| Config.MECAB_DICT_PATH = os.getenv("MECAB_DICT_PATH", Config.MECAB_DICT_PATH) |
| Config.DEFAULT_MODEL_PATH = os.getenv("DEFAULT_MODEL_PATH", Config.DEFAULT_MODEL_PATH) |
| |
| sudachi_env = os.getenv("SUDACHI_DICT_PATH", getattr(Config, "SUDACHI_DICT_PATH", "")) |
| if sudachi_env: |
| Config.SUDACHI_DICT_PATH = sudachi_env |
| |
| Config.FUGASHI_ARGS = f"-r {Config.MECAB_CONFIG_PATH}" |
|
|
|
|
| |
| load_config_from_env() |
|
|
|
|
| |
| def test_config(): |
| """設定のテスト""" |
| print("=== Configテスト ===") |
| |
| |
| Config.print_status() |
| |
| |
| try: |
| import fugashi |
| print(f"\n=== fugashiテスト ===") |
| tagger = fugashi.GenericTagger(Config.get_fugashi_args()) |
| test_text = "こんにちは世界" |
| tokens = tagger(test_text) |
| print(f"テストテキスト: '{test_text}'") |
| print(f"形態素: {[token.surface for token in tokens]}") |
| print("✓ fugashi動作確認完了") |
| except Exception as e: |
| print(f"✗ fugashiテスト失敗: {e}") |
| |
| |
| try: |
| from ai import AI |
| print(f"\n=== AIクラステスト ===") |
| if os.path.exists(Config.get_default_model_path()): |
| ai = AI(Config.get_default_model_path()) |
| tokens = ai.get_token_probabilities("こんにちは", k=3) |
| print(f"テスト結果: {tokens}") |
| print("✓ AIクラス動作確認完了") |
| else: |
| print("✗ デフォルトモデルが見つかりません") |
| except Exception as e: |
| print(f"✗ AIクラステスト失敗: {e}") |
|
|
|
|
| if __name__ == "__main__": |
| test_config() |
|
|