LMStudio と Ollama の翻訳バックエンドを追加・統合。
- config: SELECTABLE_/SELECTED_ の LMStudio/Ollama 項目と LMSTUDIO_URL を追加。 - Controller: LMStudio/Ollama の認証チェック、URL取得/設定、モデル一覧取得/設定のエンドポイントを実装。 - Model/Translator: LMStudio/Ollama 用の認証・モデル一覧・モデル設定・クライアント更新メソッドを追加し、翻訳処理の選択肢に対応。 - translation_* クライアント: 各クライアントでのプロンプト読み込み処理を共通化し、translation_utils.loadPromptConfig を利用するようにリファクタ。 - translation_languages: LMStudio/Ollama 用の言語マッピングを追加。
This commit is contained in:
@@ -1,8 +1,15 @@
|
||||
import logging
|
||||
from google import genai
|
||||
from langchain_google_genai import ChatGoogleGenerativeAI
|
||||
import yaml
|
||||
from os import path as os_path
|
||||
|
||||
try:
|
||||
from .translation_utils import loadPromptConfig
|
||||
except Exception:
|
||||
import sys
|
||||
from os import path as os_path
|
||||
print(os_path.dirname(os_path.dirname(os_path.dirname(os_path.abspath(__file__)))))
|
||||
sys.path.append(os_path.dirname(os_path.dirname(os_path.dirname(os_path.abspath(__file__)))))
|
||||
from translation_utils import loadPromptConfig
|
||||
|
||||
logger = logging.getLogger("langchain_google_genai")
|
||||
logger.setLevel(logging.ERROR)
|
||||
@@ -42,32 +49,13 @@ def _get_available_text_models(api_key: str) -> list[str]:
|
||||
allowed_models.sort()
|
||||
return allowed_models
|
||||
|
||||
def _load_prompt_config(root_path: str = None) -> dict:
|
||||
"""プロンプト設定をYAMLファイルから読み込む"""
|
||||
prompt_filename = "translation_gemini.yml"
|
||||
|
||||
# PyInstallerでビルドされた場合のパス
|
||||
if root_path and os_path.exists(os_path.join(root_path, "_internal", "prompt", prompt_filename)):
|
||||
prompt_path = os_path.join(root_path, "_internal", "prompt", prompt_filename)
|
||||
# src-pythonフォルダから直接実行している場合のパス
|
||||
elif os_path.exists(os_path.join(os_path.dirname(__file__), "models", "translation", "prompt", prompt_filename)):
|
||||
prompt_path = os_path.join(os_path.dirname(__file__), "models", "translation", "prompt", prompt_filename)
|
||||
# translationフォルダから直接実行している場合のパス
|
||||
elif os_path.exists(os_path.join(os_path.dirname(__file__), "prompt", prompt_filename)):
|
||||
prompt_path = os_path.join(os_path.dirname(__file__), "prompt", prompt_filename)
|
||||
else:
|
||||
raise FileNotFoundError(f"Prompt file not found: {prompt_filename}")
|
||||
|
||||
with open(prompt_path, "r", encoding="utf-8") as f:
|
||||
return yaml.safe_load(f)
|
||||
|
||||
class GeminiClient:
|
||||
def __init__(self, root_path: str = None):
|
||||
self.api_key = None
|
||||
self.model = None
|
||||
|
||||
# プロンプト設定をYAMLファイルから読み込む
|
||||
prompt_config = _load_prompt_config(root_path)
|
||||
prompt_config = loadPromptConfig(root_path, "translation_gemini.yml")
|
||||
self.supported_languages = prompt_config["supported_languages"]
|
||||
self.prompt_template = prompt_config["system_prompt"]
|
||||
|
||||
|
||||
@@ -665,7 +665,6 @@ dict_gemini_languages = {
|
||||
|
||||
translation_lang["Gemini_API"] = {"source":dict_gemini_languages, "target":dict_gemini_languages}
|
||||
|
||||
# OpenAI API (Chat Completions) - Gemini とほぼ同等の自然言語名を使用
|
||||
dict_openai_languages = {
|
||||
"Arabic": "Arabic",
|
||||
"Bengali": "Bengali",
|
||||
@@ -709,4 +708,6 @@ dict_openai_languages = {
|
||||
"Vietnamese": "Vietnamese",
|
||||
}
|
||||
|
||||
translation_lang["OpenAI_API"] = {"source": dict_openai_languages, "target": dict_openai_languages}
|
||||
translation_lang["OpenAI_API"] = {"source": dict_openai_languages, "target": dict_openai_languages}
|
||||
translation_lang["LMStudio"] = {"source": dict_openai_languages, "target": dict_openai_languages}
|
||||
translation_lang["Ollama"] = {"source": dict_openai_languages, "target": dict_openai_languages}
|
||||
@@ -1,8 +1,14 @@
|
||||
from openai import OpenAI
|
||||
from langchain_openai import ChatOpenAI
|
||||
from pydantic import SecretStr
|
||||
import yaml
|
||||
from os import path as os_path
|
||||
|
||||
try:
|
||||
from .translation_utils import loadPromptConfig
|
||||
except Exception:
|
||||
import sys
|
||||
from os import path as os_path
|
||||
sys.path.append(os_path.dirname(os_path.dirname(os_path.dirname(os_path.abspath(__file__)))))
|
||||
from translation_utils import loadPromptConfig
|
||||
|
||||
def _authentication_check(api_key: str, base_url: str | None = None) -> bool:
|
||||
"""Check if the provided API key is valid by attempting to list models.
|
||||
@@ -27,22 +33,6 @@ def _get_available_text_models(api_key: str, base_url: str | None = None) -> lis
|
||||
allowed_models.sort()
|
||||
return allowed_models
|
||||
|
||||
def _load_prompt_config(root_path: str = None) -> dict:
|
||||
prompt_filename = "translation_lmstudio.yml"
|
||||
# PyInstaller 展開後
|
||||
if root_path and os_path.exists(os_path.join(root_path, "_internal", "prompt", prompt_filename)):
|
||||
prompt_path = os_path.join(root_path, "_internal", "prompt", prompt_filename)
|
||||
# src-python 直下実行
|
||||
elif os_path.exists(os_path.join(os_path.dirname(__file__), "models", "translation", "prompt", prompt_filename)):
|
||||
prompt_path = os_path.join(os_path.dirname(__file__), "models", "translation", "prompt", prompt_filename)
|
||||
# translation フォルダ直下実行
|
||||
elif os_path.exists(os_path.join(os_path.dirname(__file__), "prompt", prompt_filename)):
|
||||
prompt_path = os_path.join(os_path.dirname(__file__), "prompt", prompt_filename)
|
||||
else:
|
||||
raise FileNotFoundError(f"Prompt file not found: {prompt_filename}")
|
||||
with open(prompt_path, "r", encoding="utf-8") as f:
|
||||
return yaml.safe_load(f)
|
||||
|
||||
class LMStudioClient:
|
||||
"""LM Studio Translation simple wrapper.
|
||||
prompt/translation_lmstudio.yml から system_prompt / supported_languages を読み込む。
|
||||
@@ -52,7 +42,7 @@ class LMStudioClient:
|
||||
self.model = None
|
||||
self.base_url = base_url # None の場合は公式エンドポイント
|
||||
|
||||
prompt_config = _load_prompt_config(root_path)
|
||||
prompt_config = loadPromptConfig(root_path, "translation_lmstudio.yml")
|
||||
self.supported_languages = prompt_config["supported_languages"]
|
||||
self.prompt_template = prompt_config["system_prompt"]
|
||||
|
||||
@@ -112,13 +102,11 @@ class LMStudioClient:
|
||||
return content.strip()
|
||||
|
||||
if __name__ == "__main__":
|
||||
AUTH_KEY = "lm-studio"
|
||||
client = LMStudioClient(base_url="http://192.168.68.110:1234/v1")
|
||||
models = client.getModelList()
|
||||
print(models)
|
||||
# if models:
|
||||
# print("Available models:", models)
|
||||
# model = input("Select a model: ")
|
||||
client.setModel("google/gemma-3n-e4b")
|
||||
client.updateClient()
|
||||
print(client.translate("こんにちは世界", "Japanese", "English"))
|
||||
if models:
|
||||
print("Available models:", models)
|
||||
model = input("Select a model: ")
|
||||
client.setModel(model)
|
||||
client.updateClient()
|
||||
print(client.translate("こんにちは世界", "Japanese", "English"))
|
||||
@@ -1,8 +1,13 @@
|
||||
import requests
|
||||
from langchain_ollama import ChatOllama
|
||||
import yaml
|
||||
from os import path as os_path
|
||||
|
||||
try:
|
||||
from .translation_utils import loadPromptConfig
|
||||
except Exception:
|
||||
import sys
|
||||
from os import path as os_path
|
||||
sys.path.append(os_path.dirname(os_path.dirname(os_path.dirname(os_path.abspath(__file__)))))
|
||||
from translation_utils import loadPromptConfig
|
||||
|
||||
def _authentication_check(base_url: str | None = None) -> bool:
|
||||
"""Check authentication for Ollama API.
|
||||
@@ -29,22 +34,6 @@ def _get_available_text_models(base_url: str | None = None) -> list[str]:
|
||||
allowed_models.sort()
|
||||
return allowed_models
|
||||
|
||||
def _load_prompt_config(root_path: str = None) -> dict:
|
||||
prompt_filename = "translation_ollama.yml"
|
||||
# PyInstaller 展開後
|
||||
if root_path and os_path.exists(os_path.join(root_path, "_internal", "prompt", prompt_filename)):
|
||||
prompt_path = os_path.join(root_path, "_internal", "prompt", prompt_filename)
|
||||
# src-python 直下実行
|
||||
elif os_path.exists(os_path.join(os_path.dirname(__file__), "models", "translation", "prompt", prompt_filename)):
|
||||
prompt_path = os_path.join(os_path.dirname(__file__), "models", "translation", "prompt", prompt_filename)
|
||||
# translation フォルダ直下実行
|
||||
elif os_path.exists(os_path.join(os_path.dirname(__file__), "prompt", prompt_filename)):
|
||||
prompt_path = os_path.join(os_path.dirname(__file__), "prompt", prompt_filename)
|
||||
else:
|
||||
raise FileNotFoundError(f"Prompt file not found: {prompt_filename}")
|
||||
with open(prompt_path, "r", encoding="utf-8") as f:
|
||||
return yaml.safe_load(f)
|
||||
|
||||
class OllamaClient:
|
||||
"""Ollama Translation simple wrapper.
|
||||
prompt/translation_ollama.yml から system_prompt / supported_languages を読み込む。
|
||||
@@ -53,14 +42,17 @@ class OllamaClient:
|
||||
self.model = None
|
||||
self.base_url = "http://localhost:11434"
|
||||
|
||||
prompt_config = _load_prompt_config(root_path)
|
||||
prompt_config = loadPromptConfig(root_path)
|
||||
self.supported_languages = prompt_config["supported_languages"]
|
||||
self.prompt_template = prompt_config["system_prompt"]
|
||||
|
||||
self.openai_llm = None
|
||||
|
||||
def authenticationCheck(self) -> bool:
|
||||
return _authentication_check(self.base_url)
|
||||
|
||||
def getModelList(self) -> list[str]:
|
||||
if _authentication_check(self.base_url):
|
||||
if self.authenticationCheck():
|
||||
return _get_available_text_models(self.base_url)
|
||||
return []
|
||||
|
||||
@@ -111,5 +103,5 @@ if __name__ == "__main__":
|
||||
print("Available models:", models)
|
||||
model = input("Select a model: ")
|
||||
client.setModel(model)
|
||||
client.updateClient()
|
||||
print(client.translate("こんにちは世界", "Japanese", "English"))
|
||||
client.updateClient()
|
||||
print(client.translate("こんにちは世界", "Japanese", "English"))
|
||||
@@ -1,8 +1,14 @@
|
||||
from openai import OpenAI
|
||||
from langchain_openai import ChatOpenAI
|
||||
from pydantic import SecretStr
|
||||
import yaml
|
||||
from os import path as os_path
|
||||
|
||||
try:
|
||||
from .translation_utils import loadPromptConfig
|
||||
except Exception:
|
||||
import sys
|
||||
from os import path as os_path
|
||||
sys.path.append(os_path.dirname(os_path.dirname(os_path.dirname(os_path.abspath(__file__)))))
|
||||
from translation_utils import loadPromptConfig
|
||||
|
||||
def _authentication_check(api_key: str, base_url: str | None = None) -> bool:
|
||||
"""Check if the provided API key is valid by attempting to list models.
|
||||
@@ -51,22 +57,6 @@ def _get_available_text_models(api_key: str, base_url: str | None = None) -> lis
|
||||
allowed_models.sort()
|
||||
return allowed_models
|
||||
|
||||
def _load_prompt_config(root_path: str = None) -> dict:
|
||||
prompt_filename = "translation_openai.yml"
|
||||
# PyInstaller 展開後
|
||||
if root_path and os_path.exists(os_path.join(root_path, "_internal", "prompt", prompt_filename)):
|
||||
prompt_path = os_path.join(root_path, "_internal", "prompt", prompt_filename)
|
||||
# src-python 直下実行
|
||||
elif os_path.exists(os_path.join(os_path.dirname(__file__), "models", "translation", "prompt", prompt_filename)):
|
||||
prompt_path = os_path.join(os_path.dirname(__file__), "models", "translation", "prompt", prompt_filename)
|
||||
# translation フォルダ直下実行
|
||||
elif os_path.exists(os_path.join(os_path.dirname(__file__), "prompt", prompt_filename)):
|
||||
prompt_path = os_path.join(os_path.dirname(__file__), "prompt", prompt_filename)
|
||||
else:
|
||||
raise FileNotFoundError(f"Prompt file not found: {prompt_filename}")
|
||||
with open(prompt_path, "r", encoding="utf-8") as f:
|
||||
return yaml.safe_load(f)
|
||||
|
||||
class OpenAIClient:
|
||||
"""OpenAI Translation simple wrapper.
|
||||
prompt/translation_openai.yml から system_prompt / supported_languages を読み込む。
|
||||
@@ -76,7 +66,7 @@ class OpenAIClient:
|
||||
self.model = None
|
||||
self.base_url = base_url # None の場合は公式エンドポイント
|
||||
|
||||
prompt_config = _load_prompt_config(root_path)
|
||||
prompt_config = loadPromptConfig(root_path, "translation_openai.yml")
|
||||
self.supported_languages = prompt_config["supported_languages"]
|
||||
self.prompt_template = prompt_config["system_prompt"]
|
||||
|
||||
|
||||
@@ -1,8 +1,14 @@
|
||||
from openai import OpenAI
|
||||
from langchain_openai import ChatOpenAI
|
||||
from pydantic import SecretStr
|
||||
import yaml
|
||||
from os import path as os_path
|
||||
|
||||
try:
|
||||
from .translation_utils import loadPromptConfig
|
||||
except Exception:
|
||||
import sys
|
||||
from os import path as os_path
|
||||
sys.path.append(os_path.dirname(os_path.dirname(os_path.dirname(os_path.abspath(__file__)))))
|
||||
from translation_utils import loadPromptConfig
|
||||
|
||||
BASE_URL = "https://api.platform.preferredai.jp/v1"
|
||||
|
||||
@@ -29,32 +35,13 @@ def _get_available_text_models(api_key: str) -> list[str]:
|
||||
allowed_models.sort()
|
||||
return allowed_models
|
||||
|
||||
def _load_prompt_config(root_path: str = None) -> dict:
|
||||
"""プロンプト設定をYAMLファイルから読み込む"""
|
||||
prompt_filename = "translation_plamo.yml"
|
||||
|
||||
# PyInstallerでビルドされた場合のパス
|
||||
if root_path and os_path.exists(os_path.join(root_path, "_internal", "prompt", prompt_filename)):
|
||||
prompt_path = os_path.join(root_path, "_internal", "prompt", prompt_filename)
|
||||
# src-pythonフォルダから直接実行している場合のパス
|
||||
elif os_path.exists(os_path.join(os_path.dirname(__file__), "models", "translation", "prompt", prompt_filename)):
|
||||
prompt_path = os_path.join(os_path.dirname(__file__), "models", "translation", "prompt", prompt_filename)
|
||||
# translationフォルダから直接実行している場合のパス
|
||||
elif os_path.exists(os_path.join(os_path.dirname(__file__), "prompt", prompt_filename)):
|
||||
prompt_path = os_path.join(os_path.dirname(__file__), "prompt", prompt_filename)
|
||||
else:
|
||||
raise FileNotFoundError(f"Prompt file not found: {prompt_filename}")
|
||||
|
||||
with open(prompt_path, "r", encoding="utf-8") as f:
|
||||
return yaml.safe_load(f)
|
||||
|
||||
class PlamoClient:
|
||||
def __init__(self, root_path: str = None):
|
||||
self.api_key = None
|
||||
self.base_url = BASE_URL
|
||||
self.model = None
|
||||
|
||||
prompt_config = _load_prompt_config(root_path)
|
||||
prompt_config = loadPromptConfig(root_path, "translation_plamo.yml")
|
||||
self.supported_languages = prompt_config["supported_languages"]
|
||||
self.prompt_template = prompt_config["system_prompt"]
|
||||
|
||||
|
||||
@@ -13,15 +13,18 @@ try:
|
||||
from .translation_plamo import PlamoClient
|
||||
from .translation_gemini import GeminiClient
|
||||
from .translation_openai import OpenAIClient
|
||||
from .translation_lmstudio import LMStudioClient
|
||||
from .translation_ollama import OllamaClient
|
||||
except Exception:
|
||||
import sys
|
||||
print(os_path.dirname(os_path.dirname(os_path.dirname(os_path.abspath(__file__)))))
|
||||
sys.path.append(os_path.dirname(os_path.dirname(os_path.dirname(os_path.abspath(__file__)))))
|
||||
from translation_languages import translation_lang
|
||||
from translation_utils import ctranslate2_weights
|
||||
from translation_plamo import PlamoClient
|
||||
from translation_gemini import GeminiClient
|
||||
from translation_openai import OpenAIClient
|
||||
from translation_lmstudio import LMStudioClient
|
||||
from translation_ollama import OllamaClient
|
||||
|
||||
import ctranslate2
|
||||
import transformers
|
||||
@@ -47,6 +50,8 @@ class Translator:
|
||||
self.plamo_client: Optional[PlamoClient] = None
|
||||
self.gemini_client: Optional[GeminiClient] = None
|
||||
self.openai_client: Optional[OpenAIClient] = None
|
||||
self.lmstudio_client: LMStudioClient[LMStudioClient] = None
|
||||
self.ollama_client: OllamaClient[OllamaClient] = None
|
||||
self.ctranslate2_translator: Any = None
|
||||
self.ctranslate2_tokenizer: Any = None
|
||||
self.is_loaded_ctranslate2_model: bool = False
|
||||
@@ -171,6 +176,67 @@ class Translator:
|
||||
"""Update the OpenAI client (fetch available models)."""
|
||||
self.openai_client.updateClient()
|
||||
|
||||
def setLMStudioClientURL(self, base_url: str | None = None, root_path: str = None) -> bool:
|
||||
"""Authenticate LM Studio with the provided base URL.
|
||||
|
||||
Returns True on success, False on failure.
|
||||
"""
|
||||
self.lmstudio_client = LMStudioClient(base_url=base_url, root_path=root_path)
|
||||
result = self.lmstudio_client.setBaseURL(base_url)
|
||||
if result is False:
|
||||
self.lmstudio_client = None
|
||||
return result
|
||||
|
||||
def getLMStudioModelList(self) -> list[str]:
|
||||
"""Get available LM Studio models.
|
||||
|
||||
Returns a list of model names, or an empty list on failure.
|
||||
"""
|
||||
if self.lmstudio_client is None:
|
||||
return []
|
||||
return self.lmstudio_client.getModelList()
|
||||
|
||||
def setLMStudioModel(self, model: str) -> bool:
|
||||
"""Change the LM Studio model used for translation.
|
||||
"""
|
||||
if self.lmstudio_client is None:
|
||||
return False
|
||||
return self.lmstudio_client.setModel(model)
|
||||
|
||||
def updateLMStudioClient(self) -> None:
|
||||
"""Update the LM Studio client (fetch available models)."""
|
||||
self.lmstudio_client.updateClient()
|
||||
|
||||
def checkOllamaClient(self, root_path: str = None) -> bool:
|
||||
"""Check if Ollama client is available.
|
||||
|
||||
Returns True if Ollama is reachable, False otherwise.
|
||||
"""
|
||||
self.ollama_client = OllamaClient(root_path=root_path)
|
||||
return self.ollama_client.authenticationCheck()
|
||||
|
||||
def getOllamaModelList(self, root_path: str = None) -> bool:
|
||||
"""Initialize Ollama client and fetch available models.
|
||||
|
||||
Returns True on success, False on failure.
|
||||
"""
|
||||
if self.ollama_client is None:
|
||||
return []
|
||||
return self.ollama_client.getModelList()
|
||||
|
||||
def setOllamaModel(self, model: str) -> bool:
|
||||
"""Change the Ollama model used for translation.
|
||||
|
||||
Returns True on success, False on failure.
|
||||
"""
|
||||
if self.ollama_client is None:
|
||||
return False
|
||||
return self.ollama_client.setModel(model)
|
||||
|
||||
def updateOllamaClient(self) -> None:
|
||||
"""Update the Ollama client (fetch available models)."""
|
||||
self.ollama_client.updateClient()
|
||||
|
||||
def changeCTranslate2Model(self, path: str, model_type: str, device: str = "cpu", device_index: int = 0, compute_type: str = "auto") -> None:
|
||||
"""Load a CTranslate2 model from weights.
|
||||
|
||||
@@ -320,6 +386,24 @@ class Translator:
|
||||
input_lang=source_language,
|
||||
output_lang=target_language,
|
||||
)
|
||||
case "LMStudio":
|
||||
if self.lmstudio_client is None:
|
||||
result = False
|
||||
else:
|
||||
result = self.lmstudio_client.translate(
|
||||
message,
|
||||
input_lang=source_language,
|
||||
output_lang=target_language,
|
||||
)
|
||||
case "Ollama":
|
||||
if self.ollama_client is None:
|
||||
result = False
|
||||
else:
|
||||
result = self.ollama_client.translate(
|
||||
message,
|
||||
input_lang=source_language,
|
||||
output_lang=target_language,
|
||||
)
|
||||
case "Google":
|
||||
if self.is_enable_translators is True and other_web_Translator is not None:
|
||||
result = other_web_Translator(
|
||||
|
||||
@@ -2,11 +2,10 @@ from os import path as os_path
|
||||
from os import makedirs as os_makedirs
|
||||
from requests import get as requests_get
|
||||
from typing import Callable
|
||||
import hashlib
|
||||
import transformers
|
||||
import ctranslate2
|
||||
from huggingface_hub import hf_hub_url, list_repo_files
|
||||
from requests import get as requests_get
|
||||
import yaml
|
||||
|
||||
try:
|
||||
from utils import errorLogging, getBestComputeType
|
||||
@@ -102,6 +101,21 @@ def downloadCTranslate2Tokenizer(path: str, weight_type: str = "m2m100_418M-ct2-
|
||||
tokenizer_path = os_path.join("./weights", "ctranslate2", directory_name, "tokenizer")
|
||||
transformers.AutoTokenizer.from_pretrained(tokenizer, cache_dir=tokenizer_path)
|
||||
|
||||
def loadPromptConfig(root_path: str | None = None, prompt_filename: str | None = None) -> dict:
|
||||
# PyInstaller 展開後
|
||||
if root_path and prompt_filename and os_path.exists(os_path.join(root_path, "_internal", "prompt", prompt_filename)):
|
||||
prompt_path = os_path.join(root_path, "_internal", "prompt", prompt_filename)
|
||||
# src-python 直下実行
|
||||
elif prompt_filename and os_path.exists(os_path.join(os_path.dirname(__file__), "models", "translation", "prompt", prompt_filename)):
|
||||
prompt_path = os_path.join(os_path.dirname(__file__), "models", "translation", "prompt", prompt_filename)
|
||||
# translation フォルダ直下実行
|
||||
elif prompt_filename and os_path.exists(os_path.join(os_path.dirname(__file__), "prompt", prompt_filename)):
|
||||
prompt_path = os_path.join(os_path.dirname(__file__), "prompt", prompt_filename)
|
||||
else:
|
||||
raise FileNotFoundError(f"Prompt file not found: {prompt_filename}")
|
||||
with open(prompt_path, "r", encoding="utf-8") as f:
|
||||
return yaml.safe_load(f)
|
||||
|
||||
# テスト用コード(直接実行時のみ)
|
||||
if __name__ == "__main__":
|
||||
def progress_callback(percent):
|
||||
|
||||
Reference in New Issue
Block a user