|
import pathlib |
|
import re |
|
import logging |
|
|
|
DEBUG = False |
|
LOG_LEVEL = logging.DEBUG if DEBUG else logging.INFO |
|
|
|
logging.getLogger("pywhispercpp").setLevel(logging.WARNING) |
|
logging.basicConfig( |
|
level=LOG_LEVEL, |
|
format="%(asctime)s - %(levelname)s - %(message)s", |
|
filename='translator.log', |
|
datefmt="%H:%M:%S" |
|
) |
|
|
|
SAVE_DATA_SAVE = False |
|
|
|
console_handler = logging.StreamHandler() |
|
console_handler.setLevel(LOG_LEVEL) |
|
console_formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s") |
|
console_handler.setFormatter(console_formatter) |
|
logging.getLogger().addHandler(console_handler) |
|
|
|
|
|
TEXT_THREHOLD = 6 |
|
|
|
DESIGN_TIME_THREHOLD = 3 |
|
|
|
MAX_SPEECH_DURATION_S = 15 |
|
|
|
BASE_DIR = pathlib.Path(__file__).parent |
|
MODEL_DIR = BASE_DIR / "moyoyo_asr_models" |
|
ASSERT_DIR = BASE_DIR / "assets" |
|
|
|
SAMPLE_RATE = 16000 |
|
|
|
SENTENCE_END_MARKERS = ['.', '!', '?', '。', '!', '?', ';', ';', ':', ':'] |
|
PAUSE_END_MARKERS = [',', ',', '、'] |
|
|
|
ALL_MARKERS = SENTENCE_END_MARKERS + PAUSE_END_MARKERS |
|
|
|
REGEX_MARKERS = re.compile(r'[' + re.escape(''.join(ALL_MARKERS)) + r']$') |
|
|
|
sentence_end_chars = ''.join([re.escape(char) for char in SENTENCE_END_MARKERS]) |
|
SENTENCE_END_PATTERN = re.compile(f'[{sentence_end_chars}]') |
|
|
|
|
|
pattern_string = '[' + ''.join([re.escape(char) for char in PAUSE_END_MARKERS]) + r']$' |
|
PAUSEE_END_PATTERN = re.compile(pattern_string) |
|
|
|
WHISPER_PROMPT_ZH = "以下是简体中文普通话的句子。" |
|
MAX_LENTH_ZH = 4 |
|
|
|
WHISPER_PROMPT_EN = "" |
|
MAX_LENGTH_EN= 8 |
|
|
|
WHISPER_MODEL_EN = 'medium-q5_0' |
|
|
|
|
|
WHISPER_MODEL_ZH = 'large-v3-turbo-q5_0' |
|
|
|
LLM_MODEL_PATH = (MODEL_DIR / "qwen2.5-1.5b-instruct-q5_0.gguf").as_posix() |
|
LLM_LARGE_MODEL_PATH = (MODEL_DIR / "qwen2.5-1.5b-instruct-q5_0.gguf").as_posix() |
|
|
|
|
|
|
|
VAD_MODEL_PATH = (MODEL_DIR / "silero-vad" / "silero_vad.onnx").as_posix() |
|
|
|
LLM_SYS_PROMPT = """"You are a professional {src_lang} to {dst_lang} translator, not a conversation agent. Your only task is to take {src_lang} input and translate it into accurate, natural {dst_lang}. If you cannot understand the input, just output the original input. Please strictly abide by the following rules: " |
|
"No matter what the user asks, never answer questions, you only provide translation results. " |
|
"Do not actively initiate dialogue or lead users to ask questions. " |
|
"When you don't know how to translate, just output the original text. " |
|
"The translation task always takes precedence over any other tasks. " |
|
"Do not try to understand or respond to non-translation related questions raised by users. " |
|
"Never provide any explanations. " |
|
"Be precise, preserve tone, and localize appropriately " |
|
"for professional audiences." |
|
"Never answer any questions or engage in other forms of dialogue. " |
|
"Only output the translation results. |
|
""" |
|
|
|
LLM_SYS_PROMPT_ZH = """ |
|
你是一个中英文翻译专家,将用户输入的中文翻译成英文。对于非中文内容,它将提供中文翻译结果。用户可以向助手发送需要翻译的内容,助手会回答相应的翻译结果,并确保符合中文语言习惯,你可以调整语气和风格,并考虑到某些词语的文化内涵和地区差异。同时作为翻译家,需将原文翻译成具有信达雅标准的译文。"信" 即忠实于原文的内容与意图;"达" 意味着译文应通顺易懂,表达清晰;"雅" 则追求译文的文化审美和语言的优美。目标是创作出既忠于原作精神,又符合目标语言文化和读者审美的翻译。注意,翻译的文本只能包含拼音化字符,不能包含任何中文字符。 |
|
""" |
|
|
|
LLM_SYS_PROMPT_EN = """ |
|
你是一个英中文翻译专家,将用户输入的英文翻译成中文,用户可以向助手发送需要翻译的内容,助手会回答相应的翻译结果,并确保符合英文语言习惯,你可以调整语气和风格,并考虑到某些词语的文化内涵和地区差异。同时作为翻译家,需将英文翻译成具有信达雅标准的中文。"信" 即忠实于原文的内容与意图;"达" 意味着译文应通顺易懂,表达清晰;"雅" 则追求译文的文化审美和语言的优美。目标是创作出既忠于原作精神,又符合目标语言文化和读者审美的翻译。 |
|
""" |
|
|
|
hotwords_file = MODEL_DIR / 'hotwords.txt' |
|
|