feat(translator): 添加llama-cpp-python库可用性检查
在翻译功能中添加对llama-cpp-python库的可用性检查,当库未安装时显示警告信息并禁用翻译功能
This commit is contained in:
@@ -1,16 +1,29 @@
|
||||
from llama_cpp import Llama
|
||||
import os
|
||||
from utils.logger import logger
|
||||
|
||||
# 尝试导入llama_cpp,如果失败则设置Llama为None
|
||||
try:
|
||||
from llama_cpp import Llama
|
||||
llama_cpp_available = True
|
||||
except ImportError:
|
||||
logger.warning("llama-cpp-python库未找到,将禁用翻译功能")
|
||||
Llama = None
|
||||
llama_cpp_available = False
|
||||
|
||||
class Translator:
|
||||
def __init__(self, model_path=None):
|
||||
self.model = None
|
||||
self.model_path = model_path
|
||||
self.is_ready = False
|
||||
self.model_name = ""
|
||||
self.llama_cpp_available = llama_cpp_available
|
||||
|
||||
def load_model(self, model_path=None):
|
||||
"""加载模型"""
|
||||
if not self.llama_cpp_available:
|
||||
logger.error("llama-cpp-python库未找到,无法加载模型")
|
||||
return False
|
||||
|
||||
if model_path:
|
||||
self.model_path = model_path
|
||||
|
||||
@@ -41,6 +54,10 @@ class Translator:
|
||||
|
||||
def translate(self, text, context="", terms=None):
|
||||
"""执行翻译"""
|
||||
if not self.llama_cpp_available:
|
||||
logger.error("llama-cpp-python库未找到,无法执行翻译")
|
||||
return ""
|
||||
|
||||
if not self.is_ready or not self.model:
|
||||
logger.error("模型未就绪,无法执行翻译")
|
||||
return ""
|
||||
|
||||
Reference in New Issue
Block a user