From a70700252e99f09f0774eb8eff13fe9c88dcc390 Mon Sep 17 00:00:00 2001 From: xunbu Date: Wed, 3 Sep 2025 18:27:48 +0800 Subject: [PATCH] =?UTF-8?q?txt=E7=BF=BB=E8=AF=91=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E6=8F=92=E5=85=A5=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docutranslate/__init__.py | 2 +- docutranslate/agents/segments_agent.py | 14 +- docutranslate/agents/txt_agent.py | 59 ----- docutranslate/app.py | 26 ++- docutranslate/static/i18nData.json | 138 +++++------ docutranslate/static/index.html | 2 +- .../ai_translator/txt_translator.py | 218 ++++++++++++++---- 7 files changed, 276 insertions(+), 183 deletions(-) delete mode 100644 docutranslate/agents/txt_agent.py diff --git a/docutranslate/__init__.py b/docutranslate/__init__.py index dcad5db..84a1860 100644 --- a/docutranslate/__init__.py +++ b/docutranslate/__init__.py @@ -1,3 +1,3 @@ # SPDX-FileCopyrightText: 2025 QinHan # SPDX-License-Identifier: MPL-2.0 -__version__="1.3.2" \ No newline at end of file +__version__="1.3.3" \ No newline at end of file diff --git a/docutranslate/agents/segments_agent.py b/docutranslate/agents/segments_agent.py index 1576476..20663a1 100644 --- a/docutranslate/agents/segments_agent.py +++ b/docutranslate/agents/segments_agent.py @@ -25,26 +25,26 @@ class SegmentsTranslateAgent(Agent): def __init__(self, config: SegmentsTranslateAgentConfig): super().__init__(config) self.system_prompt = f""" -Role +# Role You are a professional machine translation engine. -Task +# Task You will receive a sequence of segments to be translated, represented in JSON format. The keys are the segment IDs, and the values are the segments for translation. You need to translate these segments into the target language. Target language: {config.to_lang} -Requirements +# Requirements The translation must be professional and accurate. Do not output any explanations or annotations. The format of the translated segments should be as close as possible to the source format. For personal names and proper nouns, use the most commonly used words for translation. If there are multiple common translations, choose the word that comes first in dictionary order. For special tags or other non-translatable elements (like codes, brand names, specific jargon), keep them in their original form. If a segment is already in the target language, keep it as is. -Output +# Output The translated sequence of segments, represented as JSON text (note: not a code block). The keys are the segment IDs, and the values are the translated segments. The returned JSON text must be parsable by json.loads into a dictionary of the form {r'{"segment_id": "translation"}'}. -Example -Input +# Example +## Input {r'{"0":"hello","1":"apple","2":true,"3":"false"}'} -Output +## Output {r'{"0":"你好","1":"苹果","2":true,"3":"错误"}'} Warning: Never wrap the entire JSON object in quotes to make it a single string. Never wrap the JSON text in ```. """ diff --git a/docutranslate/agents/txt_agent.py b/docutranslate/agents/txt_agent.py deleted file mode 100644 index c689fd1..0000000 --- a/docutranslate/agents/txt_agent.py +++ /dev/null @@ -1,59 +0,0 @@ -# SPDX-FileCopyrightText: 2025 QinHan -# SPDX-License-Identifier: MPL-2.0 - -from dataclasses import dataclass - -from docutranslate.agents import AgentConfig, Agent -from docutranslate.glossary.glossary import Glossary - - -@dataclass -class TXTTranslateAgentConfig(AgentConfig): - to_lang: str - custom_prompt: str | None = None - glossary_dict: dict[str, str] | None = None - - -class TXTTranslateAgent(Agent): - def __init__(self, config: TXTTranslateAgentConfig): - super().__init__(config) - self.system_prompt = f""" -# Role -You are a professional machine translation engine. - -# Task -Translate the input txt text. -Target language: {config.to_lang} - -# Requirements -- The translation must be professional and accurate. -- Do not output any explanations or annotations. -- Do not change placeholders in the format of ``. -- For personal names and proper nouns, use the most commonly used words for translation. If there are multiple common translations, choose the word that comes first in dictionary order. -- For special tags or other non-translatable elements (like codes, brand names, specific jargon), keep them in their original form. - -# Output -The translated txt text as plain text. -""" - self.custom_prompt = config.custom_prompt - if config.custom_prompt: - self.system_prompt += "\n# **Important rules or background** \n" + self.custom_prompt + '\n' - self.glossary_dict = config.glossary_dict - - def _pre_send_handler(self, system_prompt, prompt): - if self.glossary_dict: - glossary = Glossary(glossary_dict=self.glossary_dict) - system_prompt += glossary.append_system_prompt(prompt) - return system_prompt, prompt - - def send_chunks(self, prompts: list[str]): - return super().send_prompts(prompts=prompts, pre_send_handler=self._pre_send_handler) - - async def send_chunks_async(self, prompts: list[str]): - return await super().send_prompts_async(prompts=prompts, pre_send_handler=self._pre_send_handler) - - def update_glossary_dict(self, update_dict: dict | None): - if self.glossary_dict is None: - self.glossary_dict = {} - if update_dict is not None: - self.glossary_dict = update_dict | self.glossary_dict diff --git a/docutranslate/app.py b/docutranslate/app.py index a497cb7..eadfbcc 100644 --- a/docutranslate/app.py +++ b/docutranslate/app.py @@ -259,12 +259,6 @@ class BaseWorkflowParams(BaseModel): glossary_agent_config: Optional[GlossaryAgentConfigPayload] = Field(None, description="用于术语表生成的Agent的配置。如果 `glossary_generate_enable` 为 `True`,此项必填。") - @field_validator('glossary_agent_config') - def check_glossary_config(cls, v, values): - if values.data.get('glossary_generate_enable') and not v: - raise ValueError("当 `glossary_generate_enable` 为 `True` 时, `glossary_agent_config` 字段是必须的。") - return v - @model_validator(mode='before') @classmethod def check_translation_fields(cls, values): @@ -279,6 +273,15 @@ class BaseWorkflowParams(BaseModel): # 如果跳过翻译,则不进行任何检查,允许 base_url 等字段为空 return values + @model_validator(mode='after') + def check_glossary_config(self) -> 'BaseWorkflowParams': + """ + 在所有字段验证后,检查术语表相关配置的逻辑一致性。 + """ + if self.glossary_generate_enable and not self.glossary_agent_config: + raise ValueError("当 `glossary_generate_enable` 为 `True` 时, `glossary_agent_config` 字段是必须的。") + return self + # 2. 为每个工作流创建独立的参数模型 class MarkdownWorkflowParams(BaseWorkflowParams): @@ -303,6 +306,14 @@ class MarkdownWorkflowParams(BaseWorkflowParams): class TextWorkflowParams(BaseWorkflowParams): workflow_type: Literal['txt'] = Field(..., description="指定使用纯文本的翻译工作流。") + insert_mode: Literal["replace", "append", "prepend"] = Field( + "replace", + description="翻译文本的插入模式。'replace':替换原文,'append':附加到原文后,'prepend':附加到原文前。" + ) + separator: str = Field( + "\n", + description="当 insert_mode 为 'append' 或 'prepend' 时,用于分隔原文和译文的分隔符。" + ) class JsonWorkflowParams(BaseWorkflowParams): @@ -616,7 +627,8 @@ async def _perform_translation( task_logger.info("构建 TXTWorkflow 配置。") translator_args = payload.model_dump(include={ 'skip_translate', 'base_url', 'api_key', 'model_id', 'to_lang', 'custom_prompt', - 'temperature', 'thinking', 'chunk_size', 'concurrent', 'glossary_dict' + 'temperature', 'thinking', 'chunk_size', 'concurrent', 'glossary_dict', + 'insert_mode', 'separator' }, exclude_none=True) translator_args['glossary_generate_enable'] = payload.glossary_generate_enable translator_args['glossary_agent_config'] = build_glossary_agent_config() diff --git a/docutranslate/static/i18nData.json b/docutranslate/static/i18nData.json index 2194529..0fa014f 100644 --- a/docutranslate/static/i18nData.json +++ b/docutranslate/static/i18nData.json @@ -17,12 +17,13 @@ "insertModeReplace": "替换原文 (Replace)", "insertModeAppend": "附加到原文后 (Append)", "insertModePrepend": "附加到原文前 (Prepend)", - "insertModeHelpDocx": "选择如何将翻译后的文本插入。", + "insertModeHelpTxt": "选择如何将翻译后的文本插入。", "separatorLabel": "分隔符", - "separatorPlaceholder": "例如: \\n---翻译---\\n", - "separatorHelp": "当插入模式为附加或前置时,用于分隔原文和译文的字符。\\n 代表换行。", - "insertModeHelpXlsx": "选择如何将翻译后的文本插入到单元格中。", "separatorPlaceholderSimple": "例如: \\n---\\n", + "separatorHelp": "当插入模式为附加或前置时,用于分隔原文和译文的字符。\\n 代表换行。", + "insertModeHelpDocx": "选择如何将翻译后的文本插入。", + "separatorPlaceholder": "例如: \\n---翻译---\\n", + "insertModeHelpXlsx": "选择如何将翻译后的文本插入到单元格中。", "xlsxTranslateRegionsLabel": "翻译区域 (可选)", "xlsxTranslateRegionsPlaceholder": "每行一个区域, 例如:Sheet1!A1:B10(不指定表名则对所有表生效)", "insertModeHelpSrt": "选择如何将翻译后的文本插入。", @@ -30,7 +31,7 @@ "insertModeHelpHtml": "选择如何将翻译后的文本插入。", "jsonPathLabel": "需要翻译的JSON路径", "jsonPathPlaceholder": "每行一个路径, 例如:\n$.name\n$.*", - "jsonPathHelp": "采用jsonpath-ng的路径选择语法,每一行表示一个json路径。将翻译路径匹配对象内的所有字符串", + "jsonPathHelp": "采用jsonpath-ng的路径选择语法,每一行表示一个json路径。 将翻译路径匹配对象内的所有字符串", "parsingEngineLabel": "解析引擎", "parsingEngineHelp": "如果上传的文件本身是.md格式,此项可不选。", "getMineruTokenTitle": "获取Mineru Token", @@ -98,7 +99,7 @@ "closeBtn": "关闭", "downloadBtn": "下载", "tutorialModalTitle": "使用教程", - "tutorialModalBody": "

视频教程可以在B站搜索 docutranslate 获取。

欢迎使用 DocuTranslate!请按照以下步骤完成文档翻译:

  1. 选择工作流

    首先,在配置面板顶部选择您需要的翻译流程。不同的工作流适用于不同类型的文件:

    • 转Markdown再翻译: 适用于翻译PDF、markdown、图片等文件。
    • 纯文本翻译: 用于翻译 .txt 等纯文本文件。
    • JSON翻译: 用于翻译 .json 文件中的特定字段。
    • DOCX翻译: 用于翻译 .docx 文件。
    • XLSX翻译: 用于翻译 .xlsx 电子表格、 .csv 文件。
    • SRT字幕翻译: 用于翻译 .srt 字幕文件。
    • EPUB翻译: 用于翻译 .epub 电子书文件。
    • HTML翻译: 用于翻译 .html 文件。
    新增功能: \"自动选择工作流\"开关已默认开启。您只需上传文件,系统会自动为您匹配合适的工作流,简化操作。

  2. 配置参数

    根据您选择的工作流,完成相应的配置。所有配置项都会自动保存在您的浏览器中。

    • 解析配置 (仅在“转Markdown再翻译”工作流下显示):
      • 解析引擎: 选择一个引擎将您的文件(如PDF)转换为适合翻译的Markdown格式。如果您的文件已经是Markdown格式,则无需选择。
      • Mineru Token: 如果您选择 minerU 引擎,需要在此处填入您的Token。
    • DOCX/XLSX/SRT/EPUB/HTML翻译选项 (在对应工作流下显示):
      • 插入模式: 定义翻译结果如何放入文档或字幕。您可以选择直接“替换”原文,或是在原文之后“附加”,或是在原文之前“前置”。
      • 分隔符: 当选择“附加”或“前置”模式时,此项用于在原文和译文之间插入分隔符。
    • JSON路径配置 (仅在“JSON翻译”工作流下显示):
      • 需要翻译的JSON路径: 每行输入一个 JSONPath 表达式,指定需要翻译的字段。
      • 例如:$..description翻译所有键为description的值。$.items[0].name翻译第一个item的name值。 $.*翻译所有字符串。
    • 翻译模型:
      • 跳过翻译: 勾选此项后,将只执行文档解析和格式转换,不调用AI进行翻译。
      • 选择平台/API 地址/API Key/模型 ID: 配置您希望使用的AI翻译服务。
      • 模型ID参考平台文档,建议使用非推理模型或混合推理模型(关闭思考)。
    • 翻译配置:
      • 目标语言/自定义Prompt/术语表: 指定翻译的目标语言、附加指令以及用于保证特定名词翻译准确性的术语表。
      • 思考模式:设置混合推理模型是否进行思考,目前支持智谱的glm4.5系列、阿里云的qwen3系列、火山引擎的seed1.6系列,建议选择禁用思考。
      • 分块大小/并发数/Temperature: 发给AI的分块大小、并发请求数和温度,通常保持默认即可。
  3. 上传文件

    在右侧的任务列表中,点击或拖拽您的文档到文件上传区域。

  4. 开始翻译

    文件选择成功后,点击任务卡片右下角的 开始翻译 按钮。系统将开始处理任务,您可以在日志区域查看实时进度。

  5. 查看与下载

    翻译完成后,任务卡片下方会出现操作按钮:

    • 预览: 在右侧滑出的面板中进行原文和译文的对照预览(仅作参考)。
    • 下载: 下载包括 PDF, DOCX, XLSX, HTML, Markdown 等多种格式的译文。
    • 附件: 如果翻译过程中生成了附加文件(如术语表),可在此处下载。
提示: 所有配置都会自动保存在您的浏览器本地,方便下次使用。
", + "tutorialModalBody": "

视频教程可以在B站搜索 docutranslate 获取。

欢迎使用 DocuTranslate!请按照以下步骤完成文档翻译:

  1. 选择工作流

    首先,在配置面板顶部选择您需要的翻译流程。不同的工作流适用于不同类型的文件:

    • 转Markdown再翻译: 适用于翻译PDF、markdown、图片等文件。
    • 纯文本翻译: 用于翻译 .txt 等纯文本文件。
    • JSON翻译: 用于翻译 .json 文件中的特定字段。
    • DOCX翻译: 用于翻译 .docx 文件。
    • XLSX翻译: 用于翻译 .xlsx 电子表格、 .csv 文件。
    • SRT字幕翻译: 用于翻译 .srt 字幕文件。
    • EPUB翻译: 用于翻译 .epub 电子书文件。
    • HTML翻译: 用于翻译 .html 文件。
    新增功能: \"自动选择工作流\"开关已默认开启。您只需上传文件,系统会自动为您匹配合适的工作流,简化操作。

  2. 配置参数

    根据您选择的工作流,完成相应的配置。所有配置项都会自动保存在您的浏览器中。

    • 解析配置 (仅在“转Markdown再翻译”工作流下显示):
      • 解析引擎: 选择一个引擎将您的文件(如PDF)转换为适合翻译的Markdown格式。如果您的文件已经是Markdown格式,则无需选择。
      • Mineru Token: 如果您选择 minerU 引擎,需要在此处填入您的Token。
    • TXT/DOCX/XLSX/SRT/EPUB/HTML翻译选项 (在对应工作流下显示):
      • 插入模式: 定义翻译结果如何放入文档或字幕。您可以选择直接“替换”原文,或是在原文之后“附加”,或是在原文之前“前置”。
      • 分隔符: 当选择“附加”或“前置”模式时,此项用于在原文和译文之间插入分隔符。
    • JSON路径配置 (仅在“JSON翻译”工作流下显示):
      • 需要翻译的JSON路径: 每行输入一个 JSONPath 表达式,指定需要翻译的字段。
      • 例如:$..description翻译所有键为description的值。$.items[0].name翻译第一个item的name值。 $.*翻译所有字符串。
    • 翻译模型:
      • 跳过翻译: 勾选此项后,将只执行文档解析和格式转换,不调用AI进行翻译。
      • 选择平台/API 地址/API Key/模型 ID: 配置您希望使用的AI翻译服务。
      • 模型ID参考平台文档,建议使用非推理模型或混合推理模型(关闭思考)。
    • 翻译配置:
      • 目标语言/自定义Prompt/术语表: 指定翻译的目标语言、附加指令以及用于保证特定名词翻译准确性的术语表。
      • 思考模式:设置混合推理模型是否进行思考,目前支持智谱的glm4.5系列、阿里云的qwen3系列、火山引擎的seed1.6系列,建议选择禁用思考。
      • 分块大小/并发数/Temperature: 发给AI的分块大小、并发请求数和温度,通常保持默认即可。
  3. 上传文件

    在右侧的任务列表中,点击或拖拽您的文档到文件上传区域。

  4. 开始翻译

    文件选择成功后,点击任务卡片右下角的 开始翻译 按钮。系统将开始处理任务,您可以在日志区域查看实时进度。

  5. 查看与下载

    翻译完成后,任务卡片下方会出现操作按钮:

    • 预览: 在右侧滑出的面板中进行原文和译文的对照预览(仅作参考)。
    • 下载: 下载包括 PDF, DOCX, XLSX, HTML, Markdown 等多种格式的译文。
    • 附件: 如果翻译过程中生成了附加文件(如术语表),可在此处下载。
提示: 所有配置都会自动保存在您的浏览器本地,方便下次使用。
", "tutorialUnderstandBtn": "我明白了", "contributorsModalTitle": "感谢贡献", "contributorsPara1": "DocuTranslate是一个开源项目!大家的需求与使用是项目进步的动力。", @@ -113,8 +114,8 @@ "glossaryTableDestination": "译文 (dst)", "init_i18n_failed_alert": "加载界面翻译资源失败,请检查网络连接或联系管理员。", "init_failed_alert": "初始化失败,无法连接到后端服务。请检查服务是否运行或刷新页面。", - "glossaryEmpty": "术语表为空", "parsingSettingsTitleText": "解析配置", + "txtSettingsTitleText": "TXT翻译选项", "jsonSettingsTitleText": "JSON路径配置", "xlsxSettingsTitleText": "XLSX翻译选项", "docxSettingsTitleText": "DOCX翻译选项", @@ -126,27 +127,29 @@ "engineOptionIdentity": "已经是markdown格式", "engineOptionMineru": "Mineru", "engineOptionDocling": "Docling", - "status_selectFileFirst": "请先选择文件!", - "status_fillRequired": "请填写所有必填项!", - "btn_initializing": "初始化中...", + "glossaryEmpty": "术语表为空。", + "status_selectFileFirst": "请先选择文件!", + "status_fillRequired": "请填写所有必填项!", + "btn_initializing": "初始化...", "status_encodingAndSubmitting": "文件编码并提交中...", - "status_requestOk": "请求成功,任务已开始。", + "status_requestOk": "请求成功,任务已开始", "btn_cancelTranslation": "取消翻译", "status_requestFail": "请求失败", - "status_initFail": "任务初始化失败", - "status_cancelling": "取消中...", - "status_cancelSent": "取消请求已发送。", - "status_cancelFail": "取消失败", - "status_gettingStatus": "正在获取状态...", + "status_initFail": "初始化失败", + "taskCardStartBtn": "开始翻译", "btn_reTranslate": "重新翻译", - "status_updateError": "状态更新出错", - "preview_loading": "正在加载预览...", - "preview_cantReadOriginal": "无法读取原始文件内容。", - "preview_cantPreviewType": "不支持预览此文件类型", - "preview_noOriginalCache": "无原始文件缓存,无法预览。", - "preview_loadFailed": "预览加载失败", + "status_cancelling": "取消中...", + "status_cancelSent": "取消请求已发送", + "status_cancelFail": "取消失败", + "status_gettingStatus": "获取状态中...", + "status_updateError": "状态更新失败", + "preview_loading": "加载预览中...", + "preview_cantReadOriginal": "无法读取原文内容。", + "preview_cantPreviewType": "无法预览此文件类型", + "preview_noOriginalCache": "无原文缓存,请重新上传文件以预览。", + "preview_loadFailed": "预览加载失败。", "pdf_preparing": "正在准备PDF...", - "pdf_print_failed": "调用打印机失败,请尝试手动在预览页面右键打印。", + "pdf_print_failed": "调用打印功能失败。请检查浏览器设置。", "pdf_fetch_failed": "获取翻译内容失败,无法生成PDF。", "preview_bilingual": "双语预览", "preview_translatedOnly": "仅译文预览", @@ -155,9 +158,9 @@ "en": { "pageTitle": "DocuTranslate - Interactive Document Translation", "tutorialBtn": "Tutorial", - "projectContributeBtn": "Contribute", + "projectContributeBtn": "Project Contribution", "workflowTitle": "Select Workflow", - "workflowOptionMarkdown": "Markdown-based Translation (.pdf/.md/.png, etc.)", + "workflowOptionMarkdown": "Convert to Markdown then Translate (.pdf/.md/.png etc.)", "workflowOptionTxt": "Plain Text Translation (.txt)", "workflowOptionJson": "JSON Translation (.json)", "workflowOptionDocx": "DOCX Translation (.docx)", @@ -167,15 +170,16 @@ "workflowOptionHtml": "HTML Translation (.html)", "autoWorkflowLabel": "Auto-select Workflow", "insertModeLabel": "Insert Mode", - "insertModeReplace": "Replace Original", - "insertModeAppend": "Append to Original", - "insertModePrepend": "Prepend to Original", - "insertModeHelpDocx": "Choose how to insert the translated text.", + "insertModeReplace": "Replace Original (Replace)", + "insertModeAppend": "Append to Original (Append)", + "insertModePrepend": "Prepend to Original (Prepend)", + "insertModeHelpTxt": "Choose how to insert the translated text.", "separatorLabel": "Separator", - "separatorPlaceholder": "e.g., \\n---Translation---\\n", - "separatorHelp": "Used to separate the original and translated text in append/prepend mode. \\n represents a newline.", - "insertModeHelpXlsx": "Choose how to insert the translated text into cells.", "separatorPlaceholderSimple": "e.g., \\n---\\n", + "separatorHelp": "Separator used between original and translated text in append/prepend mode. \\n represents a newline.", + "insertModeHelpDocx": "Choose how to insert the translated text.", + "separatorPlaceholder": "e.g., \\n---Translation---\\n", + "insertModeHelpXlsx": "Choose how to insert the translated text into cells.", "xlsxTranslateRegionsLabel": "Translate Regions (Optional)", "xlsxTranslateRegionsPlaceholder": "One region per line, e.g., Sheet1!A1:B10 (applies to all sheets if sheet name is omitted)", "insertModeHelpSrt": "Choose how to insert the translated text.", @@ -185,18 +189,18 @@ "jsonPathPlaceholder": "One path per line, e.g.:\n$.name\n$.*", "jsonPathHelp": "Uses jsonpath-ng syntax. Each line represents a JSON path. All strings within the matched objects will be translated.", "parsingEngineLabel": "Parsing Engine", - "parsingEngineHelp": "This can be skipped if the uploaded file is already in .md format.", + "parsingEngineHelp": "If the uploaded file is already in .md format, this option can be skipped.", "getMineruTokenTitle": "Get Mineru Token", "mineruTokenPlaceholder": "Required when using Mineru engine", "modelVersionLabel": "Mineru Model Version", "modelVersionVlm": "VLM", "modelVersionPipline": "Pipeline", - "modelVersionHelp": "mineru VLM is a newer, internal test model.", + "modelVersionHelp": "mineru VLM is a newer internal beta model.", "formulaOcrLabel": "Formula Recognition", "codeOcrLabel": "Code Recognition", "skipTranslationLabel": "Skip Translation", "platformLabel": "Select Platform", - "platformCustom": "Custom Endpoint", + "platformCustom": "Custom API", "baseUrlLabel": "API Address (Base URL)", "baseUrlPlaceholder": "OpenAI-compatible address", "getApiKeyTitle": "Get API Key", @@ -205,43 +209,43 @@ "modelIdPlaceholder": "e.g., gpt-4o, glm-4", "targetLanguageLabel": "Target Language", "targetLanguageCustom": "Other (Custom)", - "customLangPlaceholder": "Enter target language, e.g., Italian", + "customLangPlaceholder": "Please enter the target language, e.g., Italian", "thinkingModeLabel": "Thinking Mode", - "thinkingModeTooltip": "Set the thinking mode for mixed-inference models. Currently supports Zhipu glm-4.5 series, Alibaba Cloud qwen3 series, Volcengine Doubao-Seed-1.6 series, etc. Disabling is recommended.", + "thinkingModeTooltip": "Set the thinking mode for mixed-inference models. Currently supports Zhipu's glm-4.5 series, Alibaba Cloud's qwen3 series, Volcengine's Doubao-Seed-1.6 series, etc. It is recommended to disable it.", "thinkingModeEnable": "Enable", "thinkingModeDisable": "Disable (Recommended)", "thinkingModeDefault": "Default", "customPromptLabel": "Custom Prompt", - "customPromptPlaceholder": "Optional, e.g., 'Keep proper names in the original language'", + "customPromptPlaceholder": "Optional, e.g., 'Do not translate personal names'", "chunkSizeLabel": "Chunk Size", "resetBtn": "Reset", "concurrentLabel": "Concurrency", "glossaryGenTitle": "Glossary", "glossaryLabel": "Glossary (Optional)", - "glossaryHelp": "Select one or more CSV files. The file must contain 'src' and 'dst' headers, representing the source and destination text respectively.", + "glossaryHelp": "Select one or more CSV files. The files must contain 'src' and 'dst' columns, representing the source and destination text respectively.", "viewGlossaryBtn": "View Glossary", "clearGlossaryBtn": "Clear", "glossaryGenEnableLabel": "Auto-generate Glossary", "glossaryGenConfigLabel": "Glossary Generation Config", "glossaryGenConfigSame": "Same as Translation Config", "glossaryGenConfigCustom": "Custom", - "githubInfo": "GitHub (star us❤):
https://github.com/xunbu/docutranslate", + "githubInfo": "GitHub Homepage (Stars are welcome❤):
https://github.com/xunbu/docutranslate", "qqGroupInfo": "QQ Group: 1047781902", "taskListTitle": "Task List", "newTaskBtn": "New Task", - "noTaskPlaceholder": "No tasks yet. Click 'New Task' to get started!", + "noTaskPlaceholder": "No tasks currently. Click 'New Task' to get started!", "taskCardIdLabel": "Task ID", - "taskCardIdPlaceholder": "Pending submission...", + "taskCardIdPlaceholder": "Waiting for submission...", "taskCardFileDrop": "Click or drag file here", "taskCardFileSelected": "File selected", "taskCardFilenameLabel": "Filename: ", - "taskCardLogLabel": "Logs", + "taskCardLogLabel": "Log", "taskCardStatusWaiting": "Waiting for file upload...", "taskCardPreviewBtn": "Preview", "taskCardDownloadBtn": "Download", "taskCardAttachmentBtn": "Attachments", "taskCardStartBtn": "Start Translation", - "downloadMdEmbedded": "Markdown (Embedded)", + "downloadMdEmbedded": "Markdown (Embedded Images)", "downloadMdZip": "Markdown (Zip)", "previewTitle": "Preview", "previewBilingualBtn": "Bilingual", @@ -251,58 +255,60 @@ "closeBtn": "Close", "downloadBtn": "Download", "tutorialModalTitle": "Tutorial", - "tutorialModalBody": "

Video tutorials are available on Bilibili (search for docutranslate).

Welcome to DocuTranslate! Please follow these steps to translate your documents:

  1. Select Workflow

    First, select the desired translation process at the top of the settings panel. Different workflows are suitable for different file types:

    • Markdown-based Translation: Suitable for translating PDF, Markdown, images, etc.
    • Plain Text Translation: For translating plain text files like .txt.
    • JSON Translation: For translating specific fields in .json files.
    • DOCX Translation: For translating .docx files.
    • XLSX Translation: For translating .xlsx spreadsheets and .csv files.
    • SRT Subtitle Translation: For translating .srt subtitle files.
    • EPUB Translation: For translating .epub ebook files.
    • HTML Translation: For translating .html files.
    New Feature: The 'Auto-select Workflow' switch is now on by default. Just upload your file, and the system will automatically match it with the appropriate workflow, simplifying the process.

  2. Configure Settings

    Configure the settings according to your chosen workflow. All settings are automatically saved in your browser.

    • Parsing Config (only for 'Markdown-based Translation' workflow):
      • Parsing Engine: Choose an engine to convert your file (e.g., PDF) into a translation-friendly Markdown format. Not required if your file is already in Markdown.
      • Mineru Token: If you select the minerU engine, you need to enter your token here.
    • DOCX/XLSX/SRT/EPUB/HTML Options (shown for the respective workflows):
      • Insert Mode: Defines how the translation result is placed in the document or subtitle. You can choose to 'Replace' the original, 'Append' after, or 'Prepend' before it.
      • Separator: When 'Append' or 'Prepend' mode is selected, this is used to insert a separator between the original and translated text.
    • JSON Path Config (only for 'JSON Translation' workflow):
      • JSON Paths to Translate: Enter one JSONPath expression per line to specify the fields to be translated.
      • For example: $..description translates all values for the key 'description'. $.items[0].name translates the 'name' of the first item. $.* translates all strings.
    • Translation Model:
      • Skip Translation: If checked, only document parsing and format conversion will be performed, without calling the AI for translation.
      • Select Platform/API Address/API Key/Model ID: Configure the AI translation service you want to use.
      • Refer to the platform's documentation for Model IDs. It is recommended to use non-reasoning models or mixed-inference models (with thinking turned off).
    • Translation Config:
      • Target Language/Custom Prompt/Glossary: Specify the target language, additional instructions, and a glossary to ensure the accuracy of specific terms.
      • Thinking Mode: Sets whether a mixed-inference model should 'think'. Supported by Zhipu glm4.5 series, Alibaba Cloud qwen3 series, Volcengine seed1.6 series. Disabling is recommended.
      • Chunk Size/Concurrency/Temperature: The chunk size sent to the AI, number of concurrent requests, and temperature. Default values are usually fine.
  3. Upload File

    In the task list on the right, click or drag your document to the file upload area.

  4. Start Translation

    After successfully selecting a file, click the Start Translation button on the bottom right of the task card. The system will begin processing, and you can view real-time progress in the log area.

  5. View & Download

    Once the translation is complete, action buttons will appear on the task card:

    • Preview: Compare the original and translated text in the side panel that slides out from the right (for reference only).
    • Download: Download the translation in various formats, including PDF, DOCX, XLSX, HTML, Markdown, etc.
    • Attachments: If additional files (like a glossary) were generated during the translation, they can be downloaded here.
Tip: All settings are automatically saved locally in your browser for your convenience.
", + "tutorialModalBody": "

Video tutorials can be found by searching for docutranslate on Bilibili.

Welcome to DocuTranslate! Please follow the steps below to complete your document translation:

  1. Select Workflow

    First, choose the translation process you need at the top of the settings panel. Different workflows are suitable for different file types:

    • Convert to Markdown then Translate: Suitable for translating PDF, Markdown, images, etc.
    • Plain Text Translation: For translating .txt and other plain text files.
    • JSON Translation: For translating specific fields in .json files.
    • DOCX Translation: For translating .docx files.
    • XLSX Translation: For translating .xlsx spreadsheets and .csv files.
    • SRT Subtitle Translation: For translating .srt subtitle files.
    • EPUB Translation: For translating .epub e-book files.
    • HTML Translation: For translating .html files.
    New Feature: The \"Auto-select Workflow\" switch is now on by default. Simply upload your file, and the system will automatically match it with the appropriate workflow to simplify the process.

  2. Configure Parameters

    Configure the relevant settings based on your chosen workflow. All settings are automatically saved in your browser.

    • Parsing Configuration (Only shown for 'Convert to Markdown' workflow):
      • Parsing Engine: Choose an engine to convert your file (like a PDF) into a translation-friendly Markdown format. If your file is already Markdown, no selection is needed.
      • Mineru Token: If you select the minerU engine, you need to enter your token here.
    • TXT/DOCX/XLSX/SRT/EPUB/HTML Translation Options (Shown for corresponding workflows):
      • Insert Mode: Define how the translation result is placed in the document or subtitle. You can choose to 'Replace' the original, 'Append' after it, or 'Prepend' before it.
      • Separator: When 'Append' or 'Prepend' mode is selected, this is used to insert a separator between the original and translated text.
    • JSON Path Configuration (Only shown for 'JSON Translation' workflow):
      • JSON Paths to Translate: Enter one JSONPath expression per line to specify the fields to be translated.
      • For example: $..description translates all values for the key 'description'. $.items[0].name translates the 'name' of the first item. $.* translates all strings.
    • Translation Model:
      • Skip Translation: If checked, only document parsing and format conversion will be performed, without calling an AI for translation.
      • Select Platform/API Address/API Key/Model ID: Configure the AI translation service you wish to use.
      • Refer to the platform's documentation for Model IDs. It is recommended to use non-inference models or mixed-inference models (with thinking turned off).
    • Translation Configuration:
      • Target Language/Custom Prompt/Glossary: Specify the target language for the translation, add any extra instructions, and provide a glossary to ensure the accuracy of specific terms.
      • Thinking Mode: Sets whether a mixed-inference model should perform thinking. Currently supports Zhipu's glm4.5 series, Alibaba Cloud's qwen3 series, and Volcengine's seed1.6 series. Disabling thinking is recommended.
      • Chunk Size/Concurrency/Temperature: The size of text chunks sent to the AI, the number of concurrent requests, and the temperature. The defaults are usually fine.
  3. Upload File

    In the task list on the right, click or drag your document into the file upload area.

  4. Start Translation

    Once the file is successfully selected, click the Start Translation button at the bottom right of the task card. The system will begin processing the task, and you can view real-time progress in the log area.

  5. View & Download

    After the translation is complete, action buttons will appear at the bottom of the task card:

    • Preview: View a side-by-side comparison of the original and translated text in a panel that slides out from the right (for reference only).
    • Download: Download the translated document in various formats, including PDF, DOCX, XLSX, HTML, and Markdown.
    • Attachments: If any additional files were generated during the translation process (like a glossary), you can download them here.
Tip: All your settings are automatically saved locally in your browser for your convenience.
", "tutorialUnderstandBtn": "I Understand", "contributorsModalTitle": "Thanks for Contributing", "contributorsPara1": "DocuTranslate is an open-source project! The community's needs and usage are the driving force behind its progress.", - "contributorsPara2": "Thank you to everyone who has funded the project, submitted code, provided valuable suggestions, and starred the project!", + "contributorsPara2": "Thank you to everyone who has sponsored the project, submitted code, provided valuable suggestions, and starred the project!", "contributorsWelcome": "We welcome contributions in the following ways:", - "contributorsGithub": "GitHub Home", - "contributorsPR": "Submit a Pull Request", + "contributorsGithub": "GitHub Homepage", + "contributorsPR": "Submit Pull Request", "contributorsIssue": "Report an Issue", - "contributorsQQ": "Or contact the author via the QQ group: 1047781902", + "contributorsQQ": "Or contact the author via QQ group: 1047781902", "glossaryModalTitle": "Current Glossary", "glossaryTableSource": "Source (src)", "glossaryTableDestination": "Destination (dst)", "init_i18n_failed_alert": "Failed to load interface translations. Please check your network connection or contact an administrator.", "init_failed_alert": "Initialization failed, could not connect to the backend service. Please ensure the service is running and refresh the page.", - "glossaryEmpty": "Glossary is empty", - "parsingSettingsTitleText": "Parsing Config", - "jsonSettingsTitleText": "JSON Path Config", + "parsingSettingsTitleText": "Parsing Configuration", + "txtSettingsTitleText": "TXT Translation Options", + "jsonSettingsTitleText": "JSON Path Configuration", "xlsxSettingsTitleText": "XLSX Translation Options", "docxSettingsTitleText": "DOCX Translation Options", "srtSettingsTitleText": "SRT Translation Options", "epubSettingsTitleText": "EPUB Translation Options", "htmlSettingsTitleText": "HTML Translation Options", "aiSettingsTitleText": "Translation Model", - "translationSettingsTitleText": "Translation Config", - "engineOptionIdentity": "Its Markdown format.", + "translationSettingsTitleText": "Translation Configuration", + "engineOptionIdentity": "Already in Markdown format", "engineOptionMineru": "Mineru", "engineOptionDocling": "Docling", + "glossaryEmpty": "Glossary is empty.", "status_selectFileFirst": "Please select a file first!", "status_fillRequired": "Please fill in all required fields!", "btn_initializing": "Initializing...", "status_encodingAndSubmitting": "Encoding and submitting file...", - "status_requestOk": "Request successful, task has started.", + "status_requestOk": "Request successful, task has started", "btn_cancelTranslation": "Cancel Translation", "status_requestFail": "Request failed", - "status_initFail": "Task initialization failed", + "status_initFail": "Initialization failed", + "taskCardStartBtn": "Start Translation", + "btn_reTranslate": "Re-translate", "status_cancelling": "Cancelling...", - "status_cancelSent": "Cancel request sent.", - "status_cancelFail": "Cancellation failed", + "status_cancelSent": "Cancel request sent", + "status_cancelFail": "Cancel failed", "status_gettingStatus": "Getting status...", - "btn_reTranslate": "Translate Again", - "status_updateError": "Error updating status", + "status_updateError": "Status update failed", "preview_loading": "Loading preview...", - "preview_cantReadOriginal": "Could not read original file content.", - "preview_cantPreviewType": "Preview is not supported for this file type", - "preview_noOriginalCache": "No original file cached, cannot generate preview.", - "preview_loadFailed": "Failed to load preview", + "preview_cantReadOriginal": "Cannot read original content.", + "preview_cantPreviewType": "Cannot preview this file type", + "preview_noOriginalCache": "No original file cached. Please re-upload to preview.", + "preview_loadFailed": "Failed to load preview.", "pdf_preparing": "Preparing PDF...", - "pdf_print_failed": "Failed to open print dialog. Please try right-clicking on the preview page and selecting 'Print'.", + "pdf_print_failed": "Failed to call print function. Please check browser settings.", "pdf_fetch_failed": "Failed to fetch translated content, cannot generate PDF.", "preview_bilingual": "Bilingual Preview", "preview_translatedOnly": "Translated-Only Preview", - "admin_tasklist_failed": "Admin mode: Failed to load task list." + "admin_tasklist_failed": "Admin Mode: Failed to load task list." } } \ No newline at end of file diff --git a/docutranslate/static/index.html b/docutranslate/static/index.html index 96ef6e2..ba73ace 100644 --- a/docutranslate/static/index.html +++ b/docutranslate/static/index.html @@ -1 +1 @@ - DocuTranslate - 交互式文档翻译

DocuTranslate

如果上传的文件本身是.md格式,此项可不选。
mineru VLM是更新的内测模型。

选择一个或多个CSV文件。文件需包含'src'和'dst'两列标题,分别代表原文和译文。

GitHub主页(欢迎star❤):
https://github.com/xunbu/docutranslate

交流QQ群: 1047781902

version:

任务列表

LOGO

当前没有任务,点击“新建任务”开始吧!

预览
原文
译文
\ No newline at end of file + DocuTranslate - 交互式文档翻译

DocuTranslate

如果上传的文件本身是.md格式,此项可不选。
mineru VLM是更新的内测模型。

选择一个或多个CSV文件。文件需包含'src'和'dst'两列标题,分别代表原文和译文。

GitHub主页(欢迎star❤):
https://github.com/xunbu/docutranslate

交流QQ群: 1047781902

version:

任务列表

LOGO

当前没有任务,点击“新建任务”开始吧!

预览
原文
译文
\ No newline at end of file diff --git a/docutranslate/translator/ai_translator/txt_translator.py b/docutranslate/translator/ai_translator/txt_translator.py index 2313d48..c0c2bac 100644 --- a/docutranslate/translator/ai_translator/txt_translator.py +++ b/docutranslate/translator/ai_translator/txt_translator.py @@ -1,70 +1,204 @@ # SPDX-FileCopyrightText: 2025 QinHan # SPDX-License-Identifier: MPL-2.0 +import asyncio from dataclasses import dataclass -from typing import Self +from typing import Self, Literal, List -from docutranslate.agents.txt_agent import TXTTranslateAgent, TXTTranslateAgentConfig +from docutranslate.agents.segments_agent import SegmentsTranslateAgentConfig, SegmentsTranslateAgent from docutranslate.ir.document import Document from docutranslate.translator.ai_translator.base import AiTranslatorConfig, AiTranslator -from docutranslate.utils.markdown_splitter import split_markdown_text @dataclass class TXTTranslatorConfig(AiTranslatorConfig): - ... + """ + TXTTranslator的配置类。 + + Attributes: + insert_mode (Literal["replace", "append", "prepend"]): + 指定如何插入翻译文本的模式。 + - "replace": 用译文替换原文。 + - "append": 将译文追加到原文后面。 + - "prepend": 将译文前置到原文前面。 + 默认为 "replace"。 + separator (str): + 在 "append" 或 "prepend" 模式下,用于分隔原文和译文的字符串。 + 默认为换行符 "\n"。 + """ + insert_mode: Literal["replace", "append", "prepend"] = "replace" + separator: str = "\n" class TXTTranslator(AiTranslator): + """ + 一个用于翻译纯文本 (.txt) 文件的翻译器。 + 它会按行读取文件内容,对每一行进行翻译,然后根据配置将译文写回。 + """ + def __init__(self, config: TXTTranslatorConfig): + """ + 初始化 TXTTranslator。 + + Args: + config (TxtTranslatorConfig): 翻译器的配置。 + """ super().__init__(config=config) self.chunk_size = config.chunk_size - self.translate_agent =None + self.translate_agent = None if not self.skip_translate: - agent_config = TXTTranslateAgentConfig(custom_prompt=config.custom_prompt, - to_lang=config.to_lang, - baseurl=config.base_url, - key=config.api_key, - model_id=config.model_id, - temperature=config.temperature, - thinking=config.thinking, - max_concurrent=config.concurrent, - timeout=config.timeout, - logger=self.logger, - glossary_dict=config.glossary_dict) - self.translate_agent = TXTTranslateAgent(agent_config) + agent_config = SegmentsTranslateAgentConfig( + custom_prompt=config.custom_prompt, + to_lang=config.to_lang, + baseurl=config.base_url, + key=config.api_key, + model_id=config.model_id, + temperature=config.temperature, + thinking=config.thinking, + max_concurrent=config.concurrent, + timeout=config.timeout, + logger=self.logger, + glossary_dict=config.glossary_dict + ) + self.translate_agent = SegmentsTranslateAgent(agent_config) + self.insert_mode = config.insert_mode + self.separator = config.separator + + def _pre_translate(self, document: Document) -> List[str]: + """ + 预处理步骤:解析TXT文件,按行分割文本。 + + Args: + document (Document): 待处理的文档对象。 + + Returns: + List[str]: 待翻译的原文文本行列表。 + """ + try: + # 使用 utf-8-sig 解码以处理可能存在的BOM (Byte Order Mark) + txt_content = document.content.decode('utf-8-sig') + except (UnicodeDecodeError, AttributeError) as e: + self.logger.error(f"无法解码TXT文件内容,请确保文件编码为UTF-8: {e}") + return [] + + # 按行分割文本,并保留空行,因为它们可能是格式的一部分 + original_texts = txt_content.splitlines() + + return original_texts + + def _after_translate(self, translated_texts: List[str], original_texts: List[str]) -> bytes: + """ + 翻译后处理步骤:将译文根据配置模式与原文合并,并生成新的TXT文件内容。 + + Args: + translated_texts (List[str]): 翻译后的文本行列表。 + original_texts (List[str]): 原始文本行列表。 + + Returns: + bytes: 新的TXT文件内容的字节流。 + """ + processed_lines = [] + for i, original_text in enumerate(original_texts): + # 如果原文是空行或仅包含空白字符,则直接保留,不进行翻译处理 + if not original_text.strip(): + processed_lines.append(original_text) + continue + + translated_text = translated_texts[i] + + # 根据插入模式更新内容 + if self.insert_mode == "replace": + processed_lines.append(translated_text) + elif self.insert_mode == "append": + # strip() 避免在原文和译文间产生多余的空白 + processed_lines.append(original_text.strip() + self.separator + translated_text.strip()) + elif self.insert_mode == "prepend": + processed_lines.append(translated_text.strip() + self.separator + original_text.strip()) + else: + self.logger.error(f"不正确的TxtTranslatorConfig参数: insert_mode='{self.insert_mode}'") + # 默认回退到替换模式,避免程序中断 + processed_lines.append(translated_text) + + # 将所有处理后的行重新合成为一个字符串,以换行符分隔 + new_txt_content_str = "\n".join(processed_lines) + + # 返回UTF-8编码的字节流 + return new_txt_content_str.encode('utf-8') def translate(self, document: Document) -> Self: - self.logger.info("正在翻译txt") - chunks: list[str] = split_markdown_text(document.content.decode(), max_block_size=self.chunk_size) - if self.glossary_agent: - self.glossary_dict_gen = self.glossary_agent.send_segments(chunks, self.chunk_size) + """ + 同步翻译TXT文档。 + + Args: + document (Document): 待翻译的文档对象。 + + Returns: + Self: 返回翻译器实例,以支持链式调用。 + """ + original_texts = self._pre_translate(document) + + if not original_texts: + self.logger.info("\n文件中没有找到需要翻译的文本内容。") + return self + + # 过滤掉仅包含空白字符的行,避免不必要的翻译API调用 + texts_to_translate = [text for text in original_texts if text.strip()] + + # --- 步骤 1: (可选) 术语提取 --- + if self.glossary_agent and texts_to_translate: + self.glossary_dict_gen = self.glossary_agent.send_segments(texts_to_translate, self.chunk_size) if self.translate_agent: self.translate_agent.update_glossary_dict(self.glossary_dict_gen) - self.logger.info(f"txt分为{len(chunks)}块") - if self.translate_agent: - result: list[str] = self.translate_agent.send_chunks(chunks) - else: - result=chunks - content = "\n".join(result) - document.content = content.encode() - self.logger.info("翻译完成") + + # --- 步骤 2: 调用翻译Agent --- + translated_texts_map = {} + if self.translate_agent and texts_to_translate: + translated_segments = self.translate_agent.send_segments(texts_to_translate, self.chunk_size) + translated_texts_map = dict(zip(texts_to_translate, translated_segments)) + + # 将翻译结果映射回原始行列表,非翻译行保持不变 + final_translated_texts = [translated_texts_map.get(text, text) for text in original_texts] + + # --- 步骤 3: 后处理并更新文档内容 --- + document.content = self._after_translate(final_translated_texts, original_texts) return self async def translate_async(self, document: Document) -> Self: - self.logger.info("正在翻译txt") - chunks: list[str] = split_markdown_text(document.content.decode(), max_block_size=self.chunk_size) + """ + 异步翻译TXT文档。 - if self.glossary_agent: - self.glossary_dict_gen = await self.glossary_agent.send_segments_async(chunks, self.chunk_size) + Args: + document (Document): 待翻译的文档对象。 + + Returns: + Self: 返回翻译器实例,以支持链式调用。 + """ + # I/O密集型操作在线程中运行 + original_texts = await asyncio.to_thread(self._pre_translate, document) + + if not original_texts: + self.logger.info("\n文件中没有找到需要翻译的文本内容。") + return self + + # 过滤掉仅包含空白字符的行 + texts_to_translate = [text for text in original_texts if text.strip()] + + # --- 步骤 1: (可选) 术语提取 (异步) --- + if self.glossary_agent and texts_to_translate: + self.glossary_dict_gen = await self.glossary_agent.send_segments_async(texts_to_translate, self.chunk_size) if self.translate_agent: self.translate_agent.update_glossary_dict(self.glossary_dict_gen) - self.logger.info(f"txt分为{len(chunks)}块") - if self.translate_agent: - result: list[str] = await self.translate_agent.send_chunks_async(chunks) - else: - result=chunks - content = "\n".join(result) - document.content = content.encode() - self.logger.info("翻译完成") - return self + # --- 步骤 2: 调用翻译Agent (异步) --- + translated_texts_map = {} + if self.translate_agent and texts_to_translate: + translated_segments = await self.translate_agent.send_segments_async(texts_to_translate, self.chunk_size) + translated_texts_map = dict(zip(texts_to_translate, translated_segments)) + + # 将翻译结果映射回原始行列表 + final_translated_texts = [translated_texts_map.get(text, text) for text in original_texts] + + # --- 步骤 3: 后处理并更新文档内容 (I/O密集型) --- + document.content = await asyncio.to_thread( + self._after_translate, final_translated_texts, original_texts + ) + return self \ No newline at end of file