mirror of
https://github.com/HiMeditator/auto-caption.git
synced 2026-02-04 04:14:42 +08:00
feat(vosk): 为 Vosk 模型添加非实时翻译功能 (#14)
- 添加 Ollama 大模型翻译和 Google 翻译(非实时),支持多种语言 - 为 Vosk 引擎添加非实时翻译 - 为新增的翻译功能添加和修改接口 - 修改 Electron 构建配置,之后不同平台构建无需修改构建文件
This commit is contained in:
@@ -6,4 +6,5 @@ from .audioprcs import (
|
||||
)
|
||||
from .sysout import stdout, stdout_err, stdout_cmd, stdout_obj, stderr
|
||||
from .thdata import thread_data
|
||||
from .server import start_server
|
||||
from .server import start_server
|
||||
from .translation import ollama_translate, google_translate
|
||||
57
engine/utils/translation.py
Normal file
57
engine/utils/translation.py
Normal file
@@ -0,0 +1,57 @@
|
||||
from ollama import chat
|
||||
from ollama import ChatResponse
|
||||
import asyncio
|
||||
from googletrans import Translator
|
||||
from .sysout import stdout, stdout_obj
|
||||
|
||||
lang_map = {
|
||||
'en': 'English',
|
||||
'es': 'Spanish',
|
||||
'fr': 'French',
|
||||
'de': 'German',
|
||||
'it': 'Italian',
|
||||
'ru': 'Russian',
|
||||
'ja': 'Japanese',
|
||||
'ko': 'Korean',
|
||||
'zh': 'Chinese'
|
||||
}
|
||||
|
||||
def ollama_translate(model: str, target: str, text: str, chunk_size = 3):
|
||||
stream = chat(
|
||||
model=model,
|
||||
messages=[
|
||||
{"role": "system", "content": f"/no_think Translate the following content into {lang_map[target]}, and do not output any additional information."},
|
||||
{"role": "user", "content": text}
|
||||
],
|
||||
stream=True
|
||||
)
|
||||
chunk_content = ""
|
||||
in_thinking = False
|
||||
count = 0
|
||||
for chunk in stream:
|
||||
if count == 0 and chunk['message']['content'].startswith("<think>"):
|
||||
in_thinking = True
|
||||
if in_thinking:
|
||||
if "</think>" in chunk['message']['content']:
|
||||
in_thinking = False
|
||||
continue
|
||||
chunk_content += ' '.join(chunk['message']['content'].split('\n'))
|
||||
count += 1
|
||||
if count % chunk_size == 0:
|
||||
print(chunk_content, end='')
|
||||
chunk_content = ""
|
||||
count = 0
|
||||
if chunk_content:
|
||||
print(chunk_content)
|
||||
|
||||
def google_translate(text: str, target: str, time_s: str):
|
||||
translator = Translator()
|
||||
try:
|
||||
res = asyncio.run(translator.translate(text, dest=target))
|
||||
stdout_obj({
|
||||
"command": "translation",
|
||||
"time_s": time_s,
|
||||
"translation": res.text
|
||||
})
|
||||
except Exception as e:
|
||||
stdout(f"Google Translation Request failed: {str(e)}")
|
||||
Reference in New Issue
Block a user