### Install with uv Source: https://github.com/xunbu/docutranslate/blob/main/README.md Installation using uv, including initializing the environment and adding packages. ```bash uv init uv add docutranslate uv add docutranslate[mcp] uv run --no-dev docutranslate -i #uv run --no-dev docutranslate -i --with-mcp ``` -------------------------------- ### Install with pip Source: https://github.com/xunbu/docutranslate/blob/main/README.md Basic installation and installation with the mcp extension using pip. ```bash pip install docutranslate pip install docutranslate[mcp] docutranslate -i #docutranslate -i --with-mcp ``` -------------------------------- ### Configure Client Example Source: https://github.com/xunbu/docutranslate/blob/main/docutranslate/mcp/README.md An example of how to configure the client with specific LLM settings. ```bash 请配置 DocuTranslate,使用以下设置: - API Key: sk-xxxxxx - Base URL: https://api.openai.com/v1 - Model: gpt-4o - 目标语言: 中文 ``` -------------------------------- ### configure_client Example Source: https://github.com/xunbu/docutranslate/blob/main/docutranslate/mcp/README.md Example of how to configure the DocuTranslate client with specific LLM settings. ```bash Please configure DocuTranslate with these settings: - API Key: sk-xxxxxx - Base URL: https://api.openai.com/v1 - Model: gpt-4o - Target language: Chinese ``` -------------------------------- ### Installation Source: https://github.com/xunbu/docutranslate/blob/main/docutranslate/mcp/README.md Install the docutranslate package with MCP support. ```bash pip install docutranslate[mcp] ``` -------------------------------- ### Workflow Example Source: https://github.com/xunbu/docutranslate/blob/main/README.md Example of how a workflow result might be structured. ```python workflow = result.workflow ``` -------------------------------- ### Install with git Source: https://github.com/xunbu/docutranslate/blob/main/README.md Cloning the repository and synchronizing dependencies using uv. ```bash git clone https://github.com/xunbu/docutranslate.git cd docutranslate uv sync --no-dev # uv sync --no-dev --extra mcp # uv sync --no-dev --all-extras ``` -------------------------------- ### Client SDK Example Source: https://github.com/xunbu/docutranslate/blob/main/README.md Example of using the Client SDK to translate a PDF document with a locally deployed minerU service. ```python from docutranslate.sdk import Client client = Client( api_key="YOUR_LLM_API_KEY", model_id="llama3", to_lang="Chinese", convert_engine="mineru_deploy", mineru_deploy_base_url="http://127.0.0.1:8000", # Your minerU API address ) result = client.translate("document.pdf") result.save(fmt="markdown") ``` -------------------------------- ### Install with docker Source: https://github.com/xunbu/docutranslate/blob/main/README.md Running the application using a Docker container. ```bash docker run -d -p 8010:8010 xunbu/docutranslate:latest # docker run -it -p 8010:8010 xunbu/docutranslate:latest # docker run -it -p 8010:8010 xunbu/docutranslate:v1.5.4 ``` -------------------------------- ### Submit Task Example Workflow Source: https://github.com/xunbu/docutranslate/blob/main/docutranslate/mcp/README.md An example workflow demonstrating the use of submit_task, get_task_status, download_file, and release_task. ```bash 1. submit_task(file_path="doc.pdf", api_key="sk-...", model_id="gpt-4o") → task_id="abc-123" 2. get_task_status("abc-123") → { "status": "running", "progress_percent": 45 } 3. get_task_status("abc-123") # 完成时 → 翻译完成! → 可用格式:docx, html, markdown → 附件:glossary → 使用 download_file 保存文件 4. download_file(task_id="abc-123", file_name="docx", output_dir="./output") → 文件已保存到 ./output/doc_translated.docx 5. download_file(task_id="abc-123", file_name="glossary", output_dir="./output") → 文件已保存到 ./output/glossary.csv 6. release_task("abc-123") # 清理临时文件 ``` -------------------------------- ### Client SDK Initialization and Usage Source: https://github.com/xunbu/docutranslate/blob/main/README_ZH.md Example of initializing the Client SDK and performing various translation tasks. ```python from docutranslate.sdk import Client # 使用您的 AI 平台设置初始化客户端 client = Client( api_key="YOUR_OPENAI_API_KEY", # 或其他 AI 平台 API key base_url="https://api.openai.com/v1/", model_id="gpt-4o", to_lang="中文", concurrent=10, # 并发请求数 ) # 示例 1: 翻译纯文本文件 (无需 PDF 解析引擎) result = client.translate("path/to/your/document.txt") print(f"翻译完成!保存位置: {result.save()}") # 示例 2: 翻译 PDF 文件 (需要指定 mineru_token 或使用本地部署) # 方式 A: 使用在线 MinerU (需要 token: https://mineru.net/apiManage/token) result = client.translate( "path/to/your/document.pdf", convert_engine="mineru", mineru_token="YOUR_MINERU_TOKEN", # 替换为您的 MinerU Token formula_ocr=True, # 启用公式识别 ) result.save(fmt="html") # 方式 B: 使用本地部署的 MinerU (推荐内网/离线环境) # 需要先启动本地 MinerU 服务,参考: https://github.com/opendatalab/MinerU result = client.translate( "path/to/your/document.pdf", convert_engine="mineru_deploy", mineru_deploy_base_url="http://127.0.0.1:8000", # 您的本地 MinerU 地址 mineru_deploy_backend="hybrid-auto-engine", # 后端类型 ) result.save(fmt="markdown") # 示例 3: 翻译 Docx 文件 (保持格式) result = client.translate( "path/to/your/document.docx", insert_mode="replace", # replace/append/prepend ) result.save(fmt="docx") # 保存为 docx 格式 # 示例 4: 导出为 Base64 编码字符串 (用于 API 传输) base64_content = result.export(fmt="html") print(f"导出内容长度: {len(base64_content)}") # 您还可以访问底层工作流以进行高级操作 # workflow = result.workflow ``` -------------------------------- ### Start Web UI and API Service Source: https://github.com/xunbu/docutranslate/blob/main/README.md Commands to start the DocuTranslate service with various options for GUI, API access, port specification, CORS, and MCP integration. ```bash docutranslate -i (Start GUI, default local access) docutranslate -i --host 0.0.0.0 (Allow access from other devices on LAN) docutranslate -i -p 8081 (Specify port number) docutranslate -i --cors (Enable default CORS settings) docutranslate -i --with-mcp (Start GUI with MCP SSE endpoint, shared queue, shared port) docutranslate --mcp (Start MCP server, stdio mode) docutranslate --mcp --transport sse (Start MCP server, SSE mode) docutranslate --mcp --transport sse --mcp-host MCP_HOST --mcp-port MCP_PORT (Start MCP server, SSE mode) docutranslate --mcp --transport streamable-http (Start MCP server, Streamable HTTP mode) ``` -------------------------------- ### Simple Client SDK - Translate Docx Files Source: https://github.com/xunbu/docutranslate/blob/main/README.md Python code example for translating Docx files using the DocuTranslate Client, preserving formatting. ```python # Example 3: Translate Docx files (preserve formatting) result = client.translate( "path/to/your/document.docx", insert_mode="replace", # replace/append/prepend ) result.save(fmt="docx") # Save as docx format ``` -------------------------------- ### Usage: Stdio Mode with uvx (No Installation Required) Source: https://github.com/xunbu/docutranslate/blob/main/docutranslate/mcp/README.md MCP server configuration using uvx. ```json { "mcpServers": { "docutranslate": { "command": "uvx", "args": ["--from", "docutranslate[mcp]", "docutranslate", "--mcp"], "env": { "DOCUTRANSLATE_API_KEY": "sk-xxxxxx", "DOCUTRANSLATE_BASE_URL": "https://api.openai.com/v1", "DOCUTRANSLATE_MODEL_ID": "gpt-4o", "DOCUTRANSLATE_TO_LANG": "Chinese", "DOCUTRANSLATE_CONCURRENT": "10", "DOCUTRANSLATE_CONVERT_ENGINE": "mineru", "DOCUTRANSLATE_MINERU_TOKEN": "your-mineru-token" } } } } ``` -------------------------------- ### submit_task Example Workflow Source: https://github.com/xunbu/docutranslate/blob/main/docutranslate/mcp/README.md Demonstrates a typical workflow for submitting a translation task, checking its status, downloading the translated file, and releasing the task resources. ```bash 1. submit_task(file_path="doc.pdf", api_key="sk-...", model_id="gpt-4o") → task_id="abc-123" 2. get_task_status("abc-123") → { "status": "running", "progress_percent": 45 } 3. get_task_status("abc-123") # when complete → Translation completed! → Available formats: docx, html, markdown → Attachments: glossary → Use download_file to save files 4. download_file(task_id="abc-123", file_name="docx", output_dir="./output") → File saved to ./output/doc_translated.docx 5. download_file(task_id="abc-123", file_name="glossary", output_dir="./output") → File saved to ./output/glossary.csv 6. release_task("abc-123") # cleanup temp files ``` -------------------------------- ### Simple Client SDK - Export as Base64 Source: https://github.com/xunbu/docutranslate/blob/main/README.md Python code example for exporting translated content as a base64 encoded string using the DocuTranslate Client. ```python # Example 4: Export as base64 encoded string (for API transmission) base64_content = result.export(fmt="html") print(f"Exported content length: {len(base64_content)}") ``` -------------------------------- ### Simple Client SDK - Translate Plain Text Files Source: https://github.com/xunbu/docutranslate/blob/main/README.md Python code example for translating plain text files using the DocuTranslate Client. ```python # Example 1: Translate plain text files (no PDF parsing engine needed) result = client.translate("path/to/your/document.txt") print(f"Translation complete! Saved to: {result.save()}") ``` -------------------------------- ### Other Workflows Import Examples Source: https://github.com/xunbu/docutranslate/blob/main/README.md This snippet shows how to import configurations and workflows for various file types supported by DocuTranslate, including TXT, JSON, DOCX, XLSX, EPUB, HTML, SRT, and ASS. ```python # TXT: from docutranslate.workflow.txt_workflow import TXTWorkflow, TXTWorkflowConfig # JSON: from docutranslate.workflow.json_workflow import JsonWorkflow, JsonWorkflowConfig # DOCX: from docutranslate.workflow.docx_workflow import DocxWorkflow, DocxWorkflowConfig # XLSX: from docutranslate.workflow.xlsx_workflow import XlsxWorkflow, XlsxWorkflowConfig # EPUB: from docutranslate.workflow.epub_workflow import EpubWorkflow, EpubWorkflowConfig # HTML: from docutranslate.workflow.html_workflow import HtmlWorkflow, HtmlWorkflowConfig # SRT: from docutranslate.workflow.srt_workflow import SrtWorkflow, SrtWorkflowConfig # ASS: from docutranslate.workflow.ass_workflow import AssWorkflow, AssWorkflowConfig ``` -------------------------------- ### Simple Client SDK - Translate PDF Files (Local MinerU) Source: https://github.com/xunbu/docutranslate/blob/main/README.md Python code example for translating PDF files using the DocuTranslate Client with a locally deployed MinerU service. ```python # Option B: Use locally deployed MinerU (recommended for intranet/offline) # First start local MinerU service, reference: https://github.com/opendatalab/MinerU result = client.translate( "path/to/your/document.pdf", convert_engine="mineru_deploy", mineru_deploy_base_url="http://127.0.0.1:8000", # Your local MinerU address mineru_deploy_backend="hybrid-auto-engine", # Backend type ) result.save(fmt="markdown") ``` -------------------------------- ### Simple Client SDK - Translate PDF Files (Online MinerU) Source: https://github.com/xunbu/docutranslate/blob/main/README.md Python code example for translating PDF files using the DocuTranslate Client with the online MinerU service. ```python # Example 2: Translate PDF files (requires mineru_token or local deployment) # Option A: Use online MinerU (token required: https://mineru.net/apiManage/token) result = client.translate( "path/to/your/document.pdf", convert_engine="mineru", mineru_token="YOUR_MINERU_TOKEN", # Replace with your MinerU Token formula_ocr=True, # Enable formula recognition ) result.save(fmt="html") ``` -------------------------------- ### Translate Multiple Files Concurrently Source: https://github.com/xunbu/docutranslate/blob/main/README.md Example of using the DocuTranslate SDK to translate multiple files concurrently using asyncio. ```python import asyncio from docutranslate.sdk import Client async def translate_multiple(): client = Client( api_key="YOUR_API_KEY", base_url="https://api.openai.com/v1/", model_id="gpt-4o", to_lang="Chinese", ) # Translate multiple files concurrently files = ["doc1.pdf", "doc2.docx", "notes.txt"] results = await asyncio.gather( *[client.translate_async(f) for f in files] ) for r in results: print(f"Saved: {r.save()}") asyncio.run(translate_multiple()) ``` -------------------------------- ### Usage: Stdio Mode with Virtual Environment Python (Windows Example) Source: https://github.com/xunbu/docutranslate/blob/main/docutranslate/mcp/README.md MCP server configuration using the Python interpreter from a virtual environment on Windows. ```json { "mcpServers": { "docutranslate": { "command": "C:\\path\\to\\your\\venv\\Scripts\\python.exe", "args": ["-m", "docutranslate.mcp"], "env": { "DOCUTRANSLATE_API_KEY": "sk-xxxxxx", "DOCUTRANSLATE_BASE_URL": "https://api.openai.com/v1", "DOCUTRANSLATE_MODEL_ID": "gpt-4o", "DOCUTRANSLATE_TO_LANG": "Chinese", "DOCUTRANSLATE_CONCURRENT": "10", "DOCUTRANSLATE_CONVERT_ENGINE": "mineru", "DOCUTRANSLATE_MINERU_TOKEN": "your-mineru-token" } } } } ``` -------------------------------- ### Translate a PDF File (Using `MarkdownBasedWorkflow`) Source: https://github.com/xunbu/docutranslate/blob/main/README.md This example demonstrates the most common use case of translating a PDF file to Markdown and then to another language using an LLM. It utilizes asynchronous execution and configures various components like the translator, converter, and exporter. ```python import asyncio from docutranslate.workflow.md_based_workflow import MarkdownBasedWorkflow, MarkdownBasedWorkflowConfig from docutranslate.converter.x2md.converter_mineru import ConverterMineruConfig from docutranslate.translator.ai_translator.md_translator import MDTranslatorConfig from docutranslate.exporter.md.md2html_exporter import MD2HTMLExporterConfig async def main(): # 1. Build Translator Configuration translator_config = MDTranslatorConfig( base_url="https://open.bigmodel.cn/api/paas/v4", # AI Platform Base URL api_key="YOUR_ZHIPU_API_KEY", # AI Platform API Key model_id="glm-4-air", # Model ID to_lang="English", # Target Language chunk_size=3000, # Text chunk size concurrent=10, # Concurrency level # glossary_generate_enable=True, # Enable auto-glossary generation # glossary_dict={"Jobs":"Steve Jobs"}, # Pass in a glossary dictionary # system_proxy_enable=True, # Enable system proxy ) # 2. Build Converter Configuration (Using minerU) converter_config = ConverterMineruConfig( mineru_token="YOUR_MINERU_TOKEN", # Your minerU Token formula_ocr=True # Enable formula recognition ) # 3. Build Main Workflow Configuration workflow_config = MarkdownBasedWorkflowConfig( convert_engine="mineru", # Specify parsing engine converter_config=converter_config, # Pass converter config translator_config=translator_config, # Pass translator config html_exporter_config=MD2HTMLExporterConfig(cdn=True) # HTML export config ) # 4. Instantiate Workflow workflow = MarkdownBasedWorkflow(config=workflow_config) # 5. Read file and execute translation print("Starting to read and translate file...") workflow.read_path("path/to/your/document.pdf") await workflow.translate_async() # Or use synchronous method # workflow.translate() print("Translation complete!") # 6. Save results workflow.save_as_html(name="translated_document.html") workflow.save_as_markdown_zip(name="translated_document.zip") workflow.save_as_markdown(name="translated_document.md") # Markdown with embedded images print("Files saved to ./output folder.") # Or get content strings directly html_content = workflow.export_to_html() html_content = workflow.export_to_markdown() # print(html_content) if __name__ == "__main__": asyncio.run(main()) ``` -------------------------------- ### Simple Client SDK - Initialize Client Source: https://github.com/xunbu/docutranslate/blob/main/README.md Python code to initialize the DocuTranslate Client with AI platform settings. ```python from docutranslate.sdk import Client # Initialize the client with your AI platform settings client = Client( api_key="YOUR_OPENAI_API_KEY", # or any other AI platform API key base_url="https://api.openai.com/v1/", model_id="gpt-4o", to_lang="Chinese", concurrent=10, # Number of concurrent requests ) ``` -------------------------------- ### Usage: Combined Mode with Web UI (Recommended!) Source: https://github.com/xunbu/docutranslate/blob/main/docutranslate/mcp/README.md Run both Web UI and MCP server together, sharing the same task queue. ```bash docutranslate -i --with-mcp ``` -------------------------------- ### Usage: SSE Mode (Recommended for Cherry Studio) Source: https://github.com/xunbu/docutranslate/blob/main/docutranslate/mcp/README.md Run the docutranslate MCP server in SSE mode. ```bash docutranslate --mcp --transport sse --mcp-host 127.0.0.1 --mcp-port 8000 ``` -------------------------------- ### Usage: Stdio Mode (Recommended for Claude Desktop, Windsurf) Source: https://github.com/xunbu/docutranslate/blob/main/docutranslate/mcp/README.md Run the docutranslate MCP server in stdio mode. ```bash docutranslate --mcp ``` -------------------------------- ### Vite Legacy Polyfill and Entry Point Source: https://github.com/xunbu/docutranslate/blob/main/docutranslate/static/index.html This snippet contains JavaScript code related to Vite's legacy polyfill and entry point loading, likely for browser compatibility. ```javascript import.meta.url;import("\_").catch(()=>1);(async function*(){})().next();if(location.protocol!="file:"){window.__vite_is_modern_browser=true} !function(){if(window.__vite_is_modern_browser)return;console.warn("vite: loading legacy chunks, syntax error above and the same error below should be ignored");var e=document.getElementById("vite-legacy-polyfill"),n=document.createElement("script");n.src=e.src,n.onload=function(){System.import(document.getElementById('vite-legacy-entry').getAttribute('data-src'))},document.body.appendChild(n)}(); !function(){var e=document,t=e.createElement("script");if(!("noModule"in t)&&"onbeforeload"in t){var n=!1;e.addEventListener("beforeload",(function(e){if(e.target===t)n=!0;else if(!e.target.hasAttribute("nomodule")||!n)return;e.preventDefault()}),!0),t.type="module",t.src=".",e.head.appendChild(t),t.remove()}}(); System.import(document.getElementById('vite-legacy-entry').getAttribute('data-src')) ``` -------------------------------- ### MCP Configuration using uvx Source: https://github.com/xunbu/docutranslate/blob/main/docutranslate/mcp/README.md JSON configuration for MCP servers using uvx command. ```json { "mcpServers": { "docutranslate": { "command": "uvx", "args": ["--from", "docutranslate[mcp]", "docutranslate", "--mcp"], "env": { "DOCUTRANSLATE_API_KEY": "sk-xxxxxx", "DOCUTRANSLATE_BASE_URL": "https://api.openai.com/v1", "DOCUTRANSLATE_MODEL_ID": "gpt-4o", "DOCUTRANSLATE_TO_LANG": "中文", "DOCUTRANSLATE_CONCURRENT": "10", "DOCUTRANSLATE_CONVERT_ENGINE": "mineru", "DOCUTRANSLATE_MINERU_TOKEN": "your-mineru-token" } } } } ``` -------------------------------- ### renderjson Library Initialization and Usage Source: https://github.com/xunbu/docutranslate/blob/main/docutranslate/template/json.html This snippet shows the core functionality of the renderjson library, including its initialization, configuration options, and how to render JSON data into an HTML element. ```javascript var module,window,define,renderjson=function(){var n=function(){for(var n=[\];arguments.length;)n.push(t(r(Array.prototype.shift.call(arguments)),e(Array.prototype.shift.call(arguments))));return n},t=function(){for(var n=Array.prototype.shift.call(arguments),e=0;e0&&"string"!=a&&d(),g};return null===l?n(null,p,"keyword","null"):void 0===l?n(null,p,"keyword","undefined"):"string"==typeof l&&l.length>u.max_string_length?f('"',l.substr(0,u.max_string_length)+" ...",'"',"string",(function(){return t(r("string"),n(null,p,"string",JSON.stringify(l)))})):"object"!=typeof l||[Number,String,Boolean,Date].indexOf(l.constructor)>=0?n(null,p,typeof l,JSON.stringify(l)):l.constructor==Array?0==l.length?n(null,p,"array syntax","[]"): f("[",u.collapse_msg(l.length),"]","array",(function(){for(var o=t(r("array"),n("array syntax","[",null,"\n")),a=0;a { // 修复 Katex 错误显示 const errors = document.getElementsByClassName('katex-error'); for (let i = 0; i < errors.length; i++) { errors[i].innerHTML = errors[i].title; } }, 200); // 确保 Mermaid 存在再初始化 if (typeof mermaid !== 'undefined') { mermaid.initialize({ securityLevel: 'loose', startOnLoad: true }); } ``` -------------------------------- ### Usage: Streamable HTTP Mode Source: https://github.com/xunbu/docutranslate/blob/main/docutranslate/mcp/README.md Run the docutranslate MCP server in streamable HTTP mode. ```bash docutranslate --mcp --transport streamable-http --mcp-host 127.0.0.1 --mcp-port 8000 ``` -------------------------------- ### Usage: Stdio Mode with Virtual Environment Python Source: https://github.com/xunbu/docutranslate/blob/main/docutranslate/mcp/README.md MCP server configuration using the Python interpreter from a virtual environment. ```json { "mcpServers": { "docutranslate": { "command": "/path/to/your/venv/bin/python", "args": ["-m", "docutranslate.mcp"], "env": { "DOCUTRANSLATE_API_KEY": "sk-xxxxxx", "DOCUTRANSLATE_BASE_URL": "https://api.openai.com/v1", "DOCUTRANSLATE_MODEL_ID": "gpt-4o", "DOCUTRANSLATE_TO_LANG": "Chinese", "DOCUTRANSLATE_CONCURRENT": "10", "DOCUTRANSLATE_CONVERT_ENGINE": "mineru", "DOCUTRANSLATE_MINERU_TOKEN": "your-mineru-token" } } } } ``` -------------------------------- ### ASS Subtitle Preview Structure and Styling Source: https://github.com/xunbu/docutranslate/blob/main/docutranslate/template/ass.html HTML and CSS code for a subtitle preview container, canvas, controls, and a time slider, along with basic styling for preformatted text. ```html ASS Subtitle Preview
0s
{{ ass_data }}
``` -------------------------------- ### Usage: SSE Mode with Virtual Environment Python Source: https://github.com/xunbu/docutranslate/blob/main/docutranslate/mcp/README.md Run the docutranslate MCP server in SSE mode using the Python interpreter from a virtual environment. ```bash /path/to/your/venv/bin/python -m docutranslate.mcp --transport sse --host 127.0.0.1 --port 8000 ``` -------------------------------- ### MCP Configuration using docutranslate command Source: https://github.com/xunbu/docutranslate/blob/main/docutranslate/mcp/README.md JSON configuration for MCP servers when 'docutranslate' is in PATH. ```json { "mcpServers": { "docutranslate": { "command": "docutranslate", "args": ["--mcp"], "env": { "DOCUTRANSLATE_API_KEY": "sk-xxxxxx", "DOCUTRANSLATE_BASE_URL": "https://api.openai.com/v1", "DOCUTRANSLATE_MODEL_ID": "gpt-4o", "DOCUTRANSLATE_TO_LANG": "中文", "DOCUTRANSLATE_CONCURRENT": "10", "DOCUTRANSLATE_CONVERT_ENGINE": "mineru", "DOCUTRANSLATE_MINERU_TOKEN": "your-mineru-token" } } } } ``` -------------------------------- ### Workflow API Usage Pattern Source: https://github.com/xunbu/docutranslate/blob/main/README.md General pattern for using the Workflow API for advanced control in DocuTranslate. ```python # Pattern: # 1. Create TranslatorConfig (LLM settings) # 2. Create WorkflowConfig (workflow settings) # 3. Create Workflow instance # 4. workflow.read_path(file) # 5. await workflow.translate_async() # 6. workflow.save_as_*(name=...) or export_to*(...) ```