Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.
Python ai4sciencedocument-analysisdocxextract-datalayout-analysisocrparserpdfpdf-converterpdf-extractor-llmpdf-extractor-pretrainpdf-extractor-ragpdf-parserpptxpythonxlsx
refactor: implement i18n support for UI labels and tooltips in gradio_app
myhloli91f82a443605
diff --git a/mineru/cli/gradio_app.py b/mineru/cli/gradio_app.pyindex a39d553e..7332ce1d 100644--- a/mineru/cli/gradio_app.py+++ b/mineru/cli/gradio_app.py@@ -141,110 +141,6 @@ header_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'resourcewith open(header_path, 'r') as header_file:header = header_file.read()--latin_lang = [- "af",- "az",- "bs",- "cs",- "cy",- "da",- "de",- "es",- "et",- "fr",- "ga",- "hr",- "hu",- "id",- "is",- "it",- "ku",- "la",- "lt",- "lv",- "mi",- "ms",- "mt",- "nl",- "no",- "oc",- "pi",- "pl",- "pt",- "ro",- "rs_latin",- "sk",- "sl",- "sq",- "sv",- "sw",- "tl",- "tr",- "uz",- "vi",- "french",- "german",- "fi",- "eu",- "gl",- "lb",- "rm",- "ca",- "qu",-]-arabic_lang = ["ar", "fa", "ug", "ur", "ps", "ku", "sd", "bal"]-cyrillic_lang = [- "ru",- "rs_cyrillic",- "be",- "bg",- "uk",- "mn",- "abq",- "ady",- "kbd",- "ava",- "dar",- "inh",- "che",- "lbe",- "lez",- "tab",- "kk",- "ky",- "tg",- "mk",- "tt",- "cv",- "ba",- "mhr",- "mo",- "udm",- "kv",- "os",- "bua",- "xal",- "tyv",- "sah",- "kaa",-]-east_slavic_lang = ["ru", "be", "uk"]-devanagari_lang = [- "hi",- "mr",- "ne",- "bh",- "mai",- "ang",- "bho",- "mah",- "sck",- "new",- "gom",- "sa",- "bgc",-]other_lang = ['ch (Chinese, English, Chinese Traditional)','ch_lite (Chinese, English, Chinese Traditional, Japanese)',@@ -256,8 +152,8 @@ other_lang = ['ta (Tamil, English)','te (Telugu, English)','ka (Kannada)',- "el (Greek, English)",- "th (Thai, English)"+ 'el (Greek, English)',+ 'th (Thai, English)']add_lang = ['latin (French, German, Afrikaans, Italian, Spanish, Bosnian, Portuguese, Czech, Welsh, Danish, Estonian, Irish, Croatian, Uzbek, Hungarian, Serbian (Latin), Indonesian, Occitan, Icelandic, Lithuanian, Maori, Malay, Dutch, Norwegian, Polish, Slovak, Slovenian, Albanian, Swedish, Swahili, Tagalog, Turkish, Latin, Azerbaijani, Kurdish, Latvian, Maltese, Pali, Romanian, Vietnamese, Finnish, Basque, Galician, Luxembourgish, Romansh, Catalan, Quechua)',@@ -266,10 +162,7 @@ add_lang = ['cyrillic (Russian, Belarusian, Ukrainian, Serbian (Cyrillic), Bulgarian, Mongolian, Abkhazian, Adyghe, Kabardian, Avar, Dargin, Ingush, Chechen, Lak, Lezgin, Tabasaran, Kazakh, Kyrgyz, Tajik, Macedonian, Tatar, Chuvash, Bashkir, Malian, Moldovan, Udmurt, Komi, Ossetian, Buryat, Kalmyk, Tuvan, Sakha, Karakalpak, English)','devanagari (Hindi, Marathi, Nepali, Bihari, Maithili, Angika, Bhojpuri, Magahi, Santali, Newari, Konkani, Sanskrit, Haryanvi, English)']--all_lang = []-# all_lang.extend([*other_lang, *latin_lang, *arabic_lang, *cyrillic_lang, *devanagari_lang])-all_lang.extend([*other_lang, *add_lang])+all_lang = [*other_lang, *add_lang]def safe_stem(file_path):@@ -298,65 +191,6 @@ def to_pdf(file_path):return tmp_file_path-# 根据后端类型获取公式识别标签-def get_formula_label(backend_choice):- if backend_choice.startswith("vlm"):- return "Enable display formula recognition"- elif backend_choice == "pipeline":- return "Enable formula recognition"- elif backend_choice.startswith("hybrid"):- return "Enable inline formula recognition"- else:- return "Enable formula recognition"--def get_formula_info(backend_choice):- if backend_choice.startswith("vlm"):- return "If disabled, display formulas will be shown as images."- elif backend_choice == "pipeline":- return "If disabled, display formulas will be shown as images, and inline formulas will not be detected or parsed."- elif backend_choice.startswith("hybrid"):- return "If disabled, inline formulas will not be detected or parsed."- else:- return ""--def get_backend_info(backend_choice):- if backend_choice.startswith("vlm"):- return "High-precision parsing via VLM, supports Chinese and English documents only."- elif backend_choice == "pipeline":- return "Traditional pipeline parsing, supports multiple languages, hallucination-free."- elif backend_choice.startswith("hybrid"):- return "High-precision hybrid parsing, supports multiple languages."- else:- return "Select the backend engine for document parsing."---# 更新界面函数-def update_interface(backend_choice):- formula_label_update = gr.update(label=get_formula_label(backend_choice), info=get_formula_info(backend_choice))- backend_info_update = gr.update(info=get_backend_info(backend_choice))- if backend_choice in [- "vlm-transformers",- "vlm-vllm-async-engine",- "vlm-lmdeploy-engine",- "vlm-mlx-engine",- ]:- return gr.update(visible=False), gr.update(visible=False), formula_label_update, backend_info_update- elif backend_choice in ["vlm-http-client"]:- return gr.update(visible=True), gr.update(visible=False), formula_label_update, backend_info_update- elif backend_choice in ["hybrid-http-client"]:- return gr.update(visible=True), gr.update(visible=True), formula_label_update, backend_info_update- elif backend_choice in [- "pipeline",- "hybrid-vllm-async-engine",- "hybrid-lmdeploy-engine",- "hybrid-mlx-engine",- "hybrid-transformers",- ]:- return gr.update(visible=False), gr.update(visible=True), formula_label_update, backend_info_update- else:- return gr.update(), gr.update(), formula_label_update, backend_info_update--@click.command(context_settings=dict(ignore_unknown_options=True, allow_extra_args=True))@click.pass_context@click.option(@@ -417,6 +251,130 @@ def main(ctx,server_name, server_port, latex_delimiters_type, **kwargs):+ # 创建 i18n 实例,支持中英文+ i18n = gr.I18n(+ en={+ "upload_file": "Please upload a PDF or image",+ "max_pages": "Max convert pages",+ "backend": "Backend",+ "server_url": "Server URL",+ "server_url_info": "OpenAI-compatible server URL for http-client backend.",+ "recognition_options": "**Recognition Options:**",+ "table_enable": "Enable table recognition",+ "table_info": "If disabled, tables will be shown as images.",+ "formula_label_vlm": "Enable display formula recognition",+ "formula_label_pipeline": "Enable formula recognition",+ "formula_label_hybrid": "Enable inline formula recognition",+ "formula_info_vlm": "If disabled, display formulas will be shown as images.",+ "formula_info_pipeline": "If disabled, display formulas will be shown as images, and inline formulas will not be detected or parsed.",+ "formula_info_hybrid": "If disabled, inline formulas will not be detected or parsed.",+ "ocr_language": "OCR Language",+ "ocr_language_info": "Select the OCR language for image-based PDFs and images.",+ "force_ocr": "Force enable OCR",+ "force_ocr_info": "Enable only if the result is extremely poor. Requires correct OCR language.",+ "convert": "Convert",+ "clear": "Clear",+ "pdf_preview": "PDF preview",+ "examples": "Examples:",+ "convert_result": "Convert result",+ "md_rendering": "Markdown rendering",+ "md_text": "Markdown text",+ "backend_info_vlm": "High-precision parsing via VLM, supports Chinese and English documents only.",+ "backend_info_pipeline": "Traditional pipeline parsing, supports multiple languages, hallucination-free.",+ "backend_info_hybrid": "High-precision hybrid parsing, supports multiple languages.",+ "backend_info_default": "Select the backend engine for document parsing.",+ },+ zh={+ "upload_file": "请上传 PDF 或图片",+ "max_pages": "最大转换页数",+ "backend": "解析后端",+ "server_url": "服务器地址",+ "server_url_info": "http-client 后端的 OpenAI 兼容服务器地址。",+ "recognition_options": "**识别选项:**",+ "table_enable": "启用表格识别",+ "table_info": "禁用后,表格将显示为图片。",+ "formula_label_vlm": "启用行间公式识别",+ "formula_label_pipeline": "启用公式识别",+ "formula_label_hybrid": "启用行内公式识别",+ "formula_info_vlm": "禁用后,行间公式将显示为图片。",+ "formula_info_pipeline": "禁用后,行间公式将显示为图片,行内公式将不会被检测或解析。",+ "formula_info_hybrid": "禁用后,行内公式将不会被检测或解析。",+ "ocr_language": "OCR 语言",+ "ocr_language_info": "为扫描版 PDF 和图片选择 OCR 语言。",+ "force_ocr": "强制启用 OCR",+ "force_ocr_info": "仅在识别效果极差时启用,需选择正确的 OCR 语言。",+ "convert": "转换",+ "clear": "清除",+ "pdf_preview": "PDF 预览",+ "examples": "示例:",+ "convert_result": "转换结果",+ "md_rendering": "Markdown 渲染",+ "md_text": "Markdown 文本",+ "backend_info_vlm": "多模态大模型高精度解析,仅支持中英文文档。",+ "backend_info_pipeline": "多模型传统管道解析,支持多语言,无幻觉。",+ "backend_info_hybrid": "高精度混合解析,支持多语言。",+ "backend_info_default": "选择文档解析的后端引擎。",+ },+ )++ # 根据后端类型获取公式识别标签(闭包函数以支持 i18n)+ def get_formula_label(backend_choice):+ if backend_choice.startswith("vlm"):+ return i18n("formula_label_vlm")+ elif backend_choice == "pipeline":+ return i18n("formula_label_pipeline")+ elif backend_choice.startswith("hybrid"):+ return i18n("formula_label_hybrid")+ else:+ return i18n("formula_label_pipeline")++ def get_formula_info(backend_choice):+ if backend_choice.startswith("vlm"):+ return i18n("formula_info_vlm")+ elif backend_choice == "pipeline":+ return i18n("formula_info_pipeline")+ elif backend_choice.startswith("hybrid"):+ return i18n("formula_info_hybrid")+ else:+ return ""++ def get_backend_info(backend_choice):+ if backend_choice.startswith("vlm"):+ return i18n("backend_info_vlm")+ elif backend_choice == "pipeline":+ return i18n("backend_info_pipeline")+ elif backend_choice.startswith("hybrid"):+ return i18n("backend_info_hybrid")+ else:+ return i18n("backend_info_default")++ # 更新界面函数+ def update_interface(backend_choice):+ formula_label_update = gr.update(label=get_formula_label(backend_choice), info=get_formula_info(backend_choice))+ backend_info_update = gr.update(info=get_backend_info(backend_choice))+ if backend_choice in [+ "vlm-transformers",+ "vlm-vllm-async-engine",+ "vlm-lmdeploy-engine",+ "vlm-mlx-engine",+ ]:+ return gr.update(visible=False), gr.update(visible=False), formula_label_update, backend_info_update+ elif backend_choice in ["vlm-http-client"]:+ return gr.update(visible=True), gr.update(visible=False), formula_label_update, backend_info_update+ elif backend_choice in ["hybrid-http-client"]:+ return gr.update(visible=True), gr.update(visible=True), formula_label_update, backend_info_update+ elif backend_choice in [+ "pipeline",+ "hybrid-vllm-async-engine",+ "hybrid-lmdeploy-engine",+ "hybrid-mlx-engine",+ "hybrid-transformers",+ ]:+ return gr.update(visible=False), gr.update(visible=True), formula_label_update, backend_info_update+ else:+ return gr.update(), gr.update(), formula_label_update, backend_info_update++kwargs.update(arg_parse(ctx))if latex_delimiters_type == 'a':@@ -454,9 +412,9 @@ def main(ctx,with gr.Row():with gr.Column(variant='panel', scale=5):with gr.Row():- input_file = gr.File(label='Please upload a PDF or image', file_types=suffixes)+ input_file = gr.File(label=i18n("upload_file"), file_types=suffixes)with gr.Row():- max_pages = gr.Slider(1, max_convert_pages, max_convert_pages, step=1, label='Max convert pages')+ max_pages = gr.Slider(1, max_convert_pages, max_convert_pages, step=1, label=i18n("max_pages"))with gr.Row():if vlm_engine == "vllm-async-engine":drop_list = ["pipeline", "vlm-vllm-async-engine", "hybrid-vllm-async-engine"]@@ -472,25 +430,25 @@ def main(ctx,preferred_option = "pipeline"if http_client_enable:drop_list.extend(["vlm-http-client", "hybrid-http-client"])- backend = gr.Dropdown(drop_list, label="Backend", value=preferred_option, info=get_backend_info(preferred_option))+ backend = gr.Dropdown(drop_list, label=i18n("backend"), value=preferred_option, info=get_backend_info(preferred_option))with gr.Row(visible=False) as client_options:- url = gr.Textbox(label='Server URL', value='http://localhost:30000', placeholder='http://localhost:30000', info="OpenAI-compatible server URL for http-client backend.")+ url = gr.Textbox(label=i18n("server_url"), value='http://localhost:30000', placeholder='http://localhost:30000', info=i18n("server_url_info"))with gr.Row(equal_height=True):with gr.Column():- gr.Markdown("**Recognition Options:**")- table_enable = gr.Checkbox(label='Enable table recognition', value=True, info='If disabled, tables will be shown as images.')+ gr.Markdown(i18n("recognition_options"))+ table_enable = gr.Checkbox(label=i18n("table_enable"), value=True, info=i18n("table_info"))formula_enable = gr.Checkbox(label=get_formula_label(preferred_option), value=True, info=get_formula_info(preferred_option))with gr.Column(visible=False) as ocr_options:- language = gr.Dropdown(all_lang, label='OCR Language', value='ch (Chinese, English, Chinese Traditional)', info='Select the OCR language for image-based PDFs and images.')- is_ocr = gr.Checkbox(label='Force enable OCR', value=False, info='Enable only if the result is extremely poor. Requires correct OCR language.')+ language = gr.Dropdown(all_lang, label=i18n("ocr_language"), value='ch (Chinese, English, Chinese Traditional)', info=i18n("ocr_language_info"))+ is_ocr = gr.Checkbox(label=i18n("force_ocr"), value=False, info=i18n("force_ocr_info"))with gr.Row():- change_bu = gr.Button('Convert')- clear_bu = gr.ClearButton(value='Clear')- pdf_show = PDF(label='PDF preview', interactive=False, visible=True, height=800)+ change_bu = gr.Button(i18n("convert"))+ clear_bu = gr.ClearButton(value=i18n("clear"))+ pdf_show = PDF(label=i18n("pdf_preview"), interactive=False, visible=True, height=800)if example_enable:example_root = os.path.join(os.getcwd(), 'examples')if os.path.exists(example_root):- with gr.Accordion('Examples:'):+ with gr.Accordion(i18n("examples")):gr.Examples(examples=[os.path.join(example_root, _) for _ in os.listdir(example_root) if_.endswith(tuple(suffixes))],@@ -498,13 +456,13 @@ def main(ctx,)with gr.Column(variant='panel', scale=5):- output_file = gr.File(label='convert result', interactive=False)+ output_file = gr.File(label=i18n("convert_result"), interactive=False)with gr.Tabs():- with gr.Tab('Markdown rendering'):- md = gr.Markdown(label='Markdown rendering', height=1100, show_copy_button=True,+ with gr.Tab(i18n("md_rendering")):+ md = gr.Markdown(label=i18n("md_rendering"), height=1100, show_copy_button=True,latex_delimiters=latex_delimiters,line_breaks=True)- with gr.Tab('Markdown text'):+ with gr.Tab(i18n("md_text")):md_text = gr.TextArea(lines=45, show_copy_button=True)# 添加事件处理@@ -536,7 +494,7 @@ def main(ctx,api_name=api_name)- demo.launch(server_name=server_name, server_port=server_port, show_api=api_enable)+ demo.launch(server_name=server_name, server_port=server_port, show_api=api_enable, i18n=i18n)if __name__ == '__main__':