MonkeyOCR在Win习题部署指南和报错提醒
1、部署平台
本项目需要cuda请另行配置。
win11+4090(cuda=12.9)
2、部署流程
2.1 从github下载项目源代码到本地:
执行命令(前提有git命令):
git lone https://github.com/heise199/MonkeyOCR.git
然后用vscode打开该项目目录:
注:其中多的文件夹是我对该项目的更改操作,不影响最开始的操作。
2.2 配置环境
需要python的环境,目前在电脑安装python(推荐使用anaconda和uv)
以conda为例,在该目录下执行外部包的安装命令:
pip install -r requirements.txt #可选清华源加速下载
下载安装等待完成即可。
2.3下载模型文件
官方给出了两种方式从HuggingFace和ModelScope下载,前者需要VNP,因此选择后者。
命令如下:
pip install modelscopepython tools/download_model.py -t modelscope
2.4启动命令
在vscode中运行demo下的demo_gradio.py文件即可进入gradio:
命令如下:
python demo/demo_gradio.py
控制台出现如下字样代表启动成功。
界面如图所示:
3、问题解决
1、在运行demo_gradio.py的时候,控制台输出报错:
PS E:\MonkeyOCR> & D:/anaconda/python.exe e:/MonkeyOCR/demo/demo_gradio.py
Traceback (most recent call last):File "e:\MonkeyOCR\demo\demo_gradio.py", line 13, in <module>from magic_pdf.data.data_reader_writer import FileBasedDataWriter, FileBasedDataReader
ModuleNotFoundError: No module named 'magic_pdf'
说明没有正确找到magic_pdf这个包,因此需要手动导入:
在这个文件导包前面加上:
import syssys.path.append("E:/MonkeyOCR")
代表我们导入的包会从该目录下进行查找
2、另外需要安装两个额外的包:
pip install dill triton-windows
这两个包可以保证在windows上运行
3、运行起来后demo后,在进行解析的时候也会报错,但是无法看到报错内容
不出现解析结果,然后后端也没有报错,大概率原因是有报错,但是被try_except捕获,无法查到精确的报错位置,经过查找后,大概率是这个位置报错:
注释掉try和except后就能看到报错,大概意思是没有找到poppler这个路径,我们需要额外去下载这个东西,好在github上有poppler的源码:Release Release 24.08.0-0 · oschwartz10612/poppler-windows
选择zip的压缩包下载即可。
然后解压后需要加到环境变量,这里用简单的方法:
首先定位到poppler的bin
我的是这样:
C:\POPPLER\poppler-24.08.0\Library\bin
然后再demo_gradio.py中加入一行:
poppler_path = r'C:\POPPLER\poppler-24.08.0\Library\bin'
然后修改刚刚注释掉try-except的代码,如下:
def parse_and_update_view(pdf_file):"""Parse PDF and update view"""if pdf_file is None:return (gr.update(),"Please upload a PDF file","Please upload a PDF file","<div id='page_info_box'>0 / 0</div>",gr.update(value=None, visible=True),gr.update(value=None, visible=True),)try:# Call the original parsing functionmd_content_ori, md_content, layout_pdf_update, zip_update = parse_pdf_and_return_results(pdf_file)# Update global variableslayout_pdf_path = layout_pdf_update['value']markdown_zip_path = zip_update['value']# Load parsed layout PDF for previewif layout_pdf_path and os.path.exists(layout_pdf_path):pages = convert_from_path(layout_pdf_path, dpi=150,poppler_path=poppler_path)pdf_cache["images"] = pagespdf_cache["current_page"] = 0pdf_cache["total_pages"] = len(pages)preview_image = pages[0]page_info = f"<div id='page_info_box'>1 / {len(pages)}</div>"else:preview_image = Nonepage_info = "<div id='page_info_box'>0 / 0</div>"return (preview_image,md_content,md_content_ori,page_info,layout_pdf_update,zip_update,)except:logger.warning("Parsing failed, switching to chat mode for direct recognition...")# If parsing fails, directly use chat mode for recognitionmd_content_ori, md_content, layout_pdf_update, zip_update = chat_with_image(instruction, pdf_file)return (gr.update(),md_content,md_content_ori,"<div id='page_info_box'>1 / 1</div>",layout_pdf_update,zip_update,)
然后重新启动即可。
完美解析,不得不说,比多模态大模型快多了。
感谢开源社区官方和共同开发者的贡献。