DocuTranslate

repository·main·Indexed 22 days ago

https://github.com/xunbu/docutranslate

A lightweight, local file translation tool powered by Large Language Models. It supports a wide range of formats including PDF, DOCX, XLSX, and PPTX, featuring automatic glossary generation and PDF table/formula recognition via MinerU. The tool includes an MCP (Model Context Protocol) server for integration with clients like Claude Desktop, Windsurf, and Cherry Studio, offering tools for task submission, status monitoring, and file retrieval.

Tokens
27.3K
Snippets
65
Records
114
Agent score
72%

What's inside docutranslate

  1. Overview of DocuTranslate features

    main

    DocuTranslate is a lightweight local file translation tool powered by Large Language Models (LLMs).

    Key Capabilities:

    • Multi-format Support: Translates pdf, docx, xlsx, md, txt, json, epub, srt, ass, and more.
    • Glossary Generation: Automatically creates glossaries to ensure terminology consistency.
    • Advanced PDF Parsing: Uses mineru (online or local) to recognize and translate tables, formulas, and code blocks in academic papers.
    • JSON Translation: Supports translating specific values in JSON using jsonpath-ng syntax.
    • Format Preservation: Maintains original formatting for docx and xlsx files.
    • High Performance: Supports asynchronous operations and parallel multitasking.
    • Deployment Options: Provides a Web UI, RESTful API, and MCP (Model Context Protocol) server support. It also offers portable packages for Windows and Mac.

    Note on PDFs: When translating pdf files, they are converted to markdown first, which may result in the loss of the original layout.

  2. Understand PDF Caching and OCR

    main

    PDF Caching

    The MarkdownBasedWorkflow caches analysis results in memory for the most recent 10 items. You can adjust this limit using the DOCUTRANSLATE_CACHE_NUM environment variable.

    Scanned PDFs

    DocuTranslate supports scanned PDFs by utilizing the OCR capabilities of the mineru engine.

  3. Use the Workflow API for advanced control

    main

    For fine-grained control, you can bypass the Client and use specific Workflow classes. All workflows follow a standard lifecycle:

    1. Create a TranslatorConfig (LLM settings).
    2. Create a WorkflowConfig (Workflow-specific settings).
    3. Instantiate the Workflow object.
    4. Call workflow.read_path(file_path).
    5. Call await workflow.translate_async() (or workflow.translate() for synchronous).
    6. Use workflow.save_as_*() or workflow.export_to_*() to retrieve results.

    Available Workflows

    WorkflowInput Formatssave_as_* / export_to_*Key Configs
    MarkdownBasedWorkflow.pdf, .docx, .md, .png, .jpghtml, markdown, markdown_zip, docxconvert_engine, md2docx_engine, translator_config
    TXTWorkflow.txttxt, htmltranslator_config
    JsonWorkflow.jsonjson, htmltranslator_config, json_paths
    DocxWorkflow.docxdocx, htmltranslator_config, insert_mode
    XlsxWorkflow.xlsx, .csvxlsx, htmltranslator_config, insert_mode
    SrtWorkflow.srtsrt, htmltranslator_config
    EpubWorkflow.epubepub, htmltranslator_config, insert_mode
    HtmlWorkflow.html, .htmhtmltranslator_config, insert_mode
    AssWorkflow.assass, htmltranslator_config
    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. LLM Configuration
        translator_config = MDTranslatorConfig(
            base_url="https://open.bigmodel.cn/api/paas/v4",
            api_key="YOUR_ZHIPU_API_KEY",
            model_id="glm-4-air",
            to_lang="English",
            chunk_size=3000,
            concurrent=10,
        )
    
        # 2. Converter Configuration (MinerU)
        converter_config = ConverterMineruConfig(
            mineru_token="YOUR_MINERU_TOKEN",
            formula_ocr=True
        )
    
        # 3. Workflow Configuration
        workflow_config = MarkdownBasedWorkflowConfig(
            convert_engine="mineru",
            converter_config=converter_config,
            translator_config=translator_config,
            html_exporter_config=MD2HTMLExporterConfig(cdn=True)
        )
    
        # 4. Execution
        workflow = MarkdownBasedWorkflow(config=workflow_config)
        workflow.read_path("path/to/your/document.pdf")
        await workflow.translate_async()
    
        # 5. Output
        workflow.save_as_html(name="translated_document.html")
        workflow.save_as_markdown(name="translated_document.md")
    
    if __name__ == "__main__":
        asyncio.run(main())
  4. Configure the PDF parsing engine

    main

    To translate PDF documents, you must choose a parsing engine.

    Option 1: Online Parsing (minerU)

    If you set convert_engine="mineru", you must use a minerU API Token.

    1. Register at the minerU Website.
    2. Generate a token in the API Token Management Interface. Note: Tokens expire every 14 days.

    Option 2: Local Parsing (minerU Deployment)

    For offline or intranet use, deploy minerU locally and set convert_engine="mineru_deploy". You must provide the mineru_deploy_base_url pointing to your local service.

  5. Run DocuTranslate MCP in SSE Mode

    main

    SSE mode is recommended for clients like Cherry Studio.

    Run the server using the following command:

    docutranslate --mcp --transport sse --mcp-host 127.0.0.1 --mcp-port 8000

    Or via the module directly:

    /path/to/your/venv/bin/python -m docutranslate.mcp --transport sse --host 127.0.0.1 --port 8000

    Client Configuration: Set the SSE endpoint in your client to http://127.0.0.1:8000/mcp/sse.

  6. Configure PDF translation engines (MinerU)

    main

    When translating PDF files, you can choose between an online MinerU service or a locally deployed MinerU instance.

    Option A: Online MinerU

    Requires a mineru_token obtained from https://mineru.net/apiManage/token.

    Option B: Local MinerU Deployment

    Recommended for internal or offline environments. Requires a running MinerU service (see https://github.com/opendatalab/MinerU). You must provide the mineru_deploy_base_url.

    # Option A: Online MinerU
    result = client.translate(
        "path/to/your/document.pdf",
        convert_engine="mineru",
        mineru_token="YOUR_MINERU_TOKEN",
        formula_ocr=True,
    )
    
    # Option B: Local MinerU
    result = client.translate(
        "path/to/your/document.pdf",
        convert_engine="mineru_deploy",
        mineru_deploy_base_url="http://127.0.0.1:8000",
        mineru_deploy_backend="hybrid-auto-engine",
    )
  7. Configure PDF Parsing Engine (MinerU)

    main

    If you need to translate PDF documents, you must configure a parsing engine.

    When using convert_engine="mineru", you must use a MinerU API Token.

    1. Register and apply for an API at the MinerU official website.
    2. Create a new token in the API Token Management dashboard. Note: MinerU tokens expire every 14 days and must be recreated.

    Option 2: Local MinerU Deployment

    For offline or intranet environments, you can deploy MinerU locally and set the mineru_deploy_base_url to your local API address.

  8. Configure DocuTranslate MCP for Claude Desktop or Windsurf (Stdio Mode)

    main

    For desktop clients like Claude Desktop or Windsurf, use the Stdio transport mode. It is highly recommended to use the absolute path to the Python interpreter within your virtual environment to ensure all dependencies are available.

    Option 1: Using uvx (No installation required)

    {
      "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"
          }
        }
      }
    }

    Use the full path to your virtual environment's Python executable.

    Linux/macOS:

    {
      "mcpServers": {
        "docutranslate": {
          "command": "/path/to/your/venv/bin/python",
          "args": ["-m", "docutranslate.mcp"],
          "env": { ... }
        }
      }
    }

    Windows:

    {
      "mcpServers": {
        "docutranslate": {
          "command": "C:\\path\\to\\your\\venv\\Scripts\\python.exe",
          "args": ["-m", "docutranslate.mcp"],
          "env": { ... }
        }
      }
    }

    Option 3: Using docutranslate in PATH

    If the command is already in your system PATH:

    {
      "mcpServers": {
        "docutranslate": {
          "command": "docutranslate",
          "args": ["--mcp"],
          "env": { ... }
        }
      }
    }
    {
      "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": "中文",
            "DOCUTRANSLATE_CONCURRENT": "10",
            "DOCUTRANSLATE_CONVERT_ENGINE": "mineru",
            "DOCUTRANSLATE_MINERU_TOKEN": "your-mineru-token"
          }
        }
      }
    }
  9. Start the Web UI and API Service

    main

    DocuTranslate provides a Web Interface and a RESTful API. Use the docutranslate -i command with various flags to configure the service.

    Common CLI Flags for Starting Service

    • docutranslate -i: Start GUI (default local access).
    • docutranslate -i --host 0.0.0.0: Allow access from other devices on your LAN.
    • docutranslate -i -p 8081: Specify a custom port number.
    • docutranslate -i --cors: Enable default CORS settings.
    • docutranslate -i --with-mcp: Start GUI with an MCP SSE endpoint (shares queue and port).

    Starting MCP Server Modes

    • docutranslate --mcp: Start MCP server in stdio mode.
    • docutranslate --mcp --transport sse: Start MCP server in SSE mode.
    • docutranslate --mcp --transport sse --mcp-host <HOST> --mcp-port <PORT>: Start MCP server in SSE mode with specific host/port.
    • docutranslate --mcp --transport streamable-http: Start MCP server in Streamable HTTP mode.

    Accessing the Service

    • Web Interface: Visit http://127.0.0.1:8010 (or your specified port).
    • API Documentation (Swagger): Visit http://127.0.0.1:8010/docs.
    • MCP SSE Endpoint:
      • If started with --with-mcp: http://127.0.0.1:8010/mcp/sse
      • If started with --mcp: http://127.0.0.1:8000/mcp/sse
    docutranslate -i --host 0.0.0.0 -p 8081 --cors
  10. Install DocuTranslate via git and uv sync

    main

    Clone the repository and use uv sync to set up the environment. You can include extras like mcp during synchronization.

    git clone https://github.com/xunbu/docutranslate.git
    cd docutranslate
    uv sync --no-dev
    git clone https://github.com/xunbu/docutranslate.git
    cd docutranslate
    uv sync --no-dev
    # For MCP: uv sync --no-dev --extra mcp
    # For all extras: uv sync --no-dev --extra all-extras
  11. Use Web UI + MCP Combined Mode

    main

    This mode runs both the Web UI and the MCP server simultaneously, allowing them to share the same task queue. This is the recommended way to manage tasks visually while using MCP tools.

    Run the following command:

    docutranslate -i --with-mcp

    Endpoints provided:

    • Web UI: http://127.0.0.1:8010
    • MCP SSE endpoint: http://127.0.0.1:8010/mcp/sse
  12. Run DocuTranslate MCP in Stdio Mode

    main

    Stdio mode is recommended for clients like Claude Desktop or Windsurf.

    Important: Always use the Python interpreter from your virtual environment in your MCP configuration to ensure all dependencies are available.

    Option 1: Using uvx (No installation required)

    Use this configuration in your MCP settings:

    {
      "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"
          }
        }
      }
    }

    Option 2: Using Virtual Environment Python

    Provide the absolute path to the Python executable in your virtual environment:

    Linux/macOS:

    {
      "mcpServers": {
        "docutranslate": {
          "command": "/path/to/your/venv/bin/python",
          "args": ["-m", "docutranslate.mcp"],
          "env": { ... }
        }
      }
    }

    Windows:

    {
      "mcpServers": {
        "docutranslate": {
          "command": "C:\\path\\to\\your\\venv\\Scripts\\python.exe",
          "args": ["-m", "docutranslate.mcp"],
          "env": { ... }
        }
      }
    }

    Option 3: Using docutranslate in PATH

    If the command is already in your system PATH:

    {
      "mcpServers": {
        "docutranslate": {
          "command": "docutranslate",
          "args": ["--mcp"],
          "env": { ... }
        }
      }
    }