Skip to content

Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.

13.3 KB
Raw Blame
"""Gradio 界面的唯一中英文词典,不翻译文档正文或外部错误详情。"""

from __future__ import annotations

import html
import re

# 每项依次为英文和简体中文;同时供 Gradio 属性、Python HTML 和前端事件使用。
MESSAGES: dict[str, tuple[str, str]] = {
    "header_title": ("MinerU 4: Document Extraction", "MinerU 4:文档提取"),
    "header_subtitle": (
        "Open-source document extraction for PDF/JPG/PNG, DOC/DOCX, PPT/PPTX, XLS/XLSX, "
        "RTF, ODT/ODS/ODP, EPUB, HTML/MHTML, OFD, CSV and TSV.",
        "开源文档提取工具,支持 PDF/JPG/PNG、DOC/DOCX、PPT/PPTX、XLS/XLSX、RTF、ODT/ODS/ODP、"
        "EPUB、HTML/MHTML、OFD、CSV 和 TSV。",
    ),
    "header_support_text": (
        "If you find our project helpful, please consider supporting us with a ⭐️!",
        "如果我们的项目对您有帮助,请点亮 ⭐️ 支持我们!",
    ),
    "stars": ("GitHub stars", "GitHub 星标"),
    "code": ("Code", "代码"),
    "model": ("Models", "模型"),
    "paper": ("Papers", "论文"),
    "homepage": ("Homepage", "主页"),
    "download": ("Download", "下载"),
    "upload": ("Select a document to parse", "请选择要解析的文件"),
    "upload_drop": ("Choose, drop or paste a document or image", "选择、拖放或粘贴文件或图片"),
    "upload_choose": ("Choose", "选择"),
    "upload_drag": ("Drop", "拖放"),
    "upload_paste": ("Paste", "粘贴"),
    "upload_document": ("Document", "文档"),
    "upload_image": ("Image", "图片"),
    "upload_or": ("or", "或"),
    "upload_retry": ("Retry", "重试"),
    "upload_clear": ("Clear file", "清除文件"),
    "upload_cancel": ("Cancel upload and clear file", "取消上传并清除文件"),
    "upload_complete": ("Uploaded", "已上传"),
    "upload_confirming": ("Confirming upload…", "正在确认上传…"),
    "upload_failed": ("Upload failed", "上传失败"),
    "upload_size_unknown": ("Size unavailable", "大小未知"),
    "upload_progress": ("Upload progress", "上传进度"),
    "uploading": ("Uploading…", "正在上传…"),
    "upload_unsupported": ("Unsupported file type", "不支持此文件类型"),
    "preparing_pdf": ("Preparing PDF preview…", "正在准备 PDF 预览…"),
    "tier": ("Parsing tier", "解析等级"),
    "tier_value": ("Parsing tier: {tier}{notice}", "解析等级:{tier}{notice}"),
    "tier_flash": ("flash", "flash(极速)"),
    "tier_basic": ("basic (medium)", "basic(中)"),
    "tier_standard": ("standard (high)", "standard(高)"),
    "tier_advanced": ("advanced (xhigh)", "advanced(极高)"),
    "tier_compact_flash": ("flash", "flash · 极速"),
    "tier_compact_basic": ("basic · medium", "basic · 中"),
    "tier_compact_standard": ("standard · high", "standard · 高"),
    "tier_compact_advanced": ("advanced · xhigh", "advanced · 极高"),
    "tier_reset": ("Reset to default: {tier}", "恢复默认等级:{tier}"),
    "tier_at_default": ("Already at default: {tier}", "已是默认等级:{tier}"),
    "tier_locked": ("This format uses Flash", "当前格式固定使用 Flash"),
    "runtime_device_gpu": ("GPU acceleration", "GPU 加速"),
    "runtime_device_cpu": ("CPU mode", "CPU 模式"),
    "runtime_device_unknown": ("Device status unknown", "设备状态未知"),
    "tier_unavailable_suffix": (" (unavailable on this server)", "(当前服务不可用)"),
    "force_ocr": ("Force OCR", "强制 OCR"),
    "force_ocr_info": ("Ignore the PDF text layer and perform OCR", "忽略 PDF 文本层并进行 OCR"),
    "page_range": ("Page range", "页码范围"),
    "start_page": ("Start page", "起始页"),
    "end_page": ("End page", "结束页"),
    "start_page_short": ("From", "起始"),
    "end_page_short": ("To", "结束"),
    "page_count": ("{count} pages", "{count} 页"),
    "page_limit": ("Max {count}", "单次最多 {count} 页"),
    "page_first": ("First {count}", "前 {count} 页"),
    "page_all": ("All", "全部"),
    "page_interval": ("Pages {start}–{end}", "第 {start}–{end} 页"),
    "page_single": ("Page {page}", "第 {page} 页"),
    "page_read_failed_short": ("Page count unavailable", "无法读取页数"),
    "page_unlimited": ("No limit", "不限页数"),
    "reading_pages": ("Reading PDF page count…", "正在读取 PDF 页数…"),
    "page_read_failed": (
        "Cannot read the PDF page count. Check whether the file is damaged or password-protected.",
        "无法读取 PDF 页数,请检查文件是否损坏或需要密码。",
    ),
    "page_limit_exceeded": (
        "At most {limit} pages can be parsed per conversion; {count} pages are selected.",
        "单次最多解析 {limit} 页,当前选择了 {count} 页。",
    ),
    "flash_unavailable": (
        "This format requires Flash, which is unavailable on this server",
        "该格式仅支持 Flash,当前服务不可用",
    ),
    "convert": ("Convert", "转换"),
    "clear": ("Clear", "清除"),
    "preview": ("Document preview", "文档预览"),
    "pdf_loading": ("Loading PDF…", "正在加载 PDF…"),
    "pdf_previous": ("Previous page", "上一页"),
    "pdf_next": ("Next page", "下一页"),
    "pdf_page": ("Page", "页码"),
    "pdf_zoom_out": ("Zoom out", "缩小"),
    "pdf_zoom_in": ("Zoom in", "放大"),
    "trace_regions": ("Layout annotations", "版式标注"),
    "trace_source": ("Source page", "来源页"),
    "trace_merged": ("Merged", "合并"),
    "trace_merged_description": ("Merged from {count} source blocks", "由 {count} 个原始内容块合并"),
    "pdf_fit_width": ("Fit width", "适应宽度"),
    "pdf_invalid": ("This PDF is damaged or invalid.", "PDF 已损坏或格式无效。"),
    "pdf_password": ("This PDF requires a password and cannot be previewed.", "此 PDF 需要密码,暂时无法预览。"),
    "pdf_load_failed": ("Could not load the PDF preview. Please upload the file again.", "PDF 预览加载失败,请重新上传文件。"),
    "empty_preview": ("No source document preview", "暂无源文档预览"),
    "source_preview": ("Source document preview", "源文档预览"),
    "ofd_preview_failed": (
        "Could not preview this OFD file. You can still try converting it.",
        "OFD 预览生成失败,仍可尝试解析该文件。",
    ),
    "html_preview_failed": (
        "Could not preview this HTML file. You can still try converting it.",
        "HTML 预览生成失败,仍可尝试解析该文件。",
    ),
    "mhtml_preview_failed": (
        "Could not preview this MHTML file. You can still try converting it.",
        "MHTML 预览生成失败,仍可尝试解析该文件。",
    ),
    "epub_loading": ("Loading EPUB…", "正在加载 EPUB…"),
    "epub_previous": ("Previous section", "上一节"),
    "epub_next": ("Next section", "下一节"),
    "epub_spine": ("Section", "章节"),
    "epub_contents": ("Contents", "目录"),
    "epub_preview_failed": (
        "Could not preview this EPUB file. You can still try converting it.",
        "EPUB 预览生成失败,仍可尝试解析该文件。",
    ),
    "unsupported_preview": ("No source preview for this format", "该格式暂无源文档预览"),
    "result_ready": ("Results are ready", "结果已生成"),
    "markdown": ("Markdown", "Markdown"),
    "outline_title": ("Contents", "目录"),
    "outline_expand": ("Expand section", "展开章节"),
    "outline_collapse": ("Collapse section", "收起章节"),
    "json_view": ("JSON", "JSON"),
    "download_results": ("Download results", "下载结果"),
    "examples": ("Examples", "示例"),
    "preparing_download": ("Preparing…", "准备中…"),
    "download_failed": ("{format} download failed: {error}", "{format} 下载失败:{error}"),
    "missing_download": ("No download file was received. Please try again.", "未获得下载文件,请重试。"),
    "stale_download": ("The parsing result has changed. Please download again.", "解析结果已变更,请重新下载。"),
    "office_preview_title": ("Office online preview", "Office 在线预览"),
    "office_preview_source_link": ("File url", "文件链接"),
    "office_notice": (
        "This preview requires the current file to be reachable by Microsoft Office Online. "
        "Conversion does not depend on this preview.",
        "该预览需要当前文件可被 Microsoft 在线预览服务访问,转换不依赖该预览。",
    ),
    "ignore_once": ("Dismiss", "忽略"),
    "ignore_forever": ("Always dismiss", "不再提示"),
    "status_idle_title": ("Waiting", "等待任务"),
    "status_idle_hint": ("Upload a file and start conversion.", "上传文件后开始转换。"),
    "status_latest": ("Latest status", "最新状态"),
    "status_step_prepare": ("Prepare", "准备请求"),
    "status_step_check": ("Check service", "检查服务"),
    "status_step_submit": ("Submit", "提交任务"),
    "status_step_queue": ("Queue", "排队"),
    "status_step_process": ("Parse", "解析中"),
    "status_step_download": ("Download", "下载结果"),
    "status_step_outputs": ("Build outputs", "整理输出"),
    "status_step_done": ("Done", "完成"),
    "status_step_failed": ("Failed", "失败"),
    "preparing_request": ("Preparing request...", "正在准备请求…"),
    "checking_server": ("Checking server status...", "正在检查服务状态…"),
    "preparing_file": ("Preparing file...", "正在准备文件…"),
    "submitting_task": ("Submitting task...", "正在提交任务…"),
    "queued_locally": ("Queued locally", "本地排队中"),
    "queued_on_server": ("Queued on server", "服务端排队中"),
    "processing_on_server": ("Processing on server...", "服务端解析中…"),
    "processing_elapsed": ("Processing on server ({elapsed}s)", "服务端解析中({elapsed} 秒)"),
    "downloading_result": ("Task completed, downloading result...", "任务已完成,正在下载结果…"),
    "processing_output": ("Preparing outputs...", "正在整理输出…"),
    "completed": ("Completed", "已完成"),
    "completed_elapsed": ("Completed ({elapsed}s)", "已完成({elapsed} 秒)"),
    "failed": ("Failed: {error}", "失败:{error}"),
    "missing_input": ("input file does not exist", "输入文件不存在"),
    "unsupported_input": ("unsupported file type '.{suffix}'", "不支持的文件类型 '.{suffix}'"),
    "invalid_tier": ("Invalid tier slider position", "解析档位无效"),
}


def translations() -> dict[str, dict[str, str]]:
    """为原生组件提供命名空间词典,中文地区统一使用简体中文。"""
    english = {f"mineru.{key}": pair[0] for key, pair in MESSAGES.items()}
    chinese = {f"mineru.{key}": pair[1] for key, pair in MESSAGES.items()}
    return {"en": english, "zh": chinese, "zh-CN": chinese, "zh-TW": chinese}


def localized_text(key: str, **values: object) -> str:
    """为文本叶节点生成双语标记;参数只作为转义后的普通文本插入。"""
    english, chinese = (template.format(**values) for template in MESSAGES[key])
    return _localized_span(key, english, chinese)


def _localized_span(key: str, english: str, chinese: str) -> str:
    """统一转义标记属性与可见文字,避免错误详情成为活动 HTML。"""
    return (
        f'<span data-mineru-i18n-key="{html.escape(key, quote=True)}"'
        f' data-mineru-i18n-en="{html.escape(english, quote=True)}"'
        f' data-mineru-i18n-zh="{html.escape(chinese, quote=True)}">{html.escape(chinese)}</span>'
    )


def _message_pair(message: str) -> tuple[str, str]:
    """按固定消息及已知参数模板翻译应用提示,未知详情保留原文。"""
    for pair in MESSAGES.values():
        if message in pair:
            return pair
    if message.startswith("Failed: "):
        return tuple(template.format(error=detail) for template, detail in zip(MESSAGES["failed"], _message_pair(message[8:])))
    for prefix in ("tier_unavailable: ", "page_range_invalid: "):
        if message.startswith(prefix):
            return tuple(prefix + detail for detail in _message_pair(message[len(prefix) :]))
    patterns = (
        (r"Processing on server \((?P<elapsed>[\d.]+)s\)", "processing_elapsed"),
        (r"Completed \((?P<elapsed>[\d.]+)s\)", "completed_elapsed"),
        (r"unsupported file type '\.(?P<suffix>[^']*)'", "unsupported_input"),
        (r"单次最多解析 (?P<limit>\d+) 页,当前选择了 (?P<count>\d+) 页。", "page_limit_exceeded"),
    )
    for pattern, key in patterns:
        if match := re.fullmatch(pattern, message):
            return tuple(template.format(**match.groupdict()) for template in MESSAGES[key])
    for key in ("queued_locally", "queued_on_server"):
        english, chinese = MESSAGES[key]
        if message.startswith(english) and set(message[len(english) :]) <= {"."}:
            return message, chinese + message[len(english) :]
    return message, message


def localized_message(message: str) -> str:
    """保持后端状态协议不变,只在生成界面 HTML 时附加双语显示。"""
    return _localized_span("status_message", *_message_pair(message))


def preview_placeholder(key: str) -> str:
    """保留预览占位容器,只对内部文字应用本地化。"""
    return f'<div class="mineru-kit-empty-preview">{localized_text(key)}</div>'


__all__ = ["MESSAGES", "localized_message", "localized_text", "preview_placeholder", "translations"]