GLM-OCR结合Ollama使用另一种快速调用GLM-OCR模型的方法1. 项目概述GLM-OCR是一个基于GLM-V编码器-解码器架构构建的多模态OCR模型专为复杂文档理解而设计。它集成了在大规模图文数据上预训练的CogViT视觉编码器、轻量级跨模态连接器以及GLM-0.5B语言解码器支持文本识别、表格识别和公式识别等多种功能。传统上GLM-OCR需要通过Gradio界面或Python API调用而本文将介绍一种更便捷的方法——通过Ollama库来调用GLM-OCR模型。这种方法简化了部署流程让开发者能够更快速地集成OCR功能到自己的应用中。2. 环境准备2.1 安装Ollama首先需要安装Ollama库这是一个用于与本地或远程Ollama服务交互的Python客户端pip install ollama2.2 拉取GLM-OCR模型使用Ollama命令行工具拉取GLM-OCR模型ollama pull glm-ocr:latest这个过程会自动下载模型文件下载完成后就可以通过API调用了。2.3 项目依赖配置创建一个pyproject.toml文件来管理项目依赖[project] name glm-ocr-ollama-inference version 0.1.0 description glm-ocr ollama api inference readme README.md requires-python 3.13,3.14 dependencies [ markupsafe3.0.2, ollama0.6.1, torch, torchaudio, torchvision, ] [tool.uv.sources] torch [ { index pytorch-cu128, marker sys_platform linux or sys_platform win32 }, ] torchvision [ { index pytorch-cu128, marker sys_platform linux or sys_platform win32 }, ] [[tool.uv.index]] name pytorch-cu128 url https://download.pytorch.org/whl/cu128 explicit true3. 使用Ollama API调用GLM-OCR3.1 基本调用方法创建一个Python脚本如main.py来调用GLM-OCR模型import asyncio from ollama import Client async def main(): client Client( hosthttp://localhost:11434, ) response await asyncio.to_thread( client.chat, modelglm-ocr:latest, messages[ { role: user, content: { text: Text Recognition:, formula: Formula Recognition:, table: Table Recognition: }, images: [inputs/1.png], } ], ) print(response) response_text response[message][content] response_lines response_text.strip().split(\n) for line in response_lines: print(line) if __name__ __main__: asyncio.run(main())3.2 功能说明GLM-OCR通过Ollama API支持三种主要功能文本识别使用text: Text Recognition:作为提示词表格识别使用table: Table Recognition:作为提示词公式识别使用formula: Formula Recognition:作为提示词3.3 参数说明host: Ollama服务地址默认为http://localhost:11434model: 使用的模型名称这里是glm-ocr:latestmessages: 包含用户输入和图片路径的列表images: 要识别的图片路径数组4. 实际应用示例4.1 文本识别对于普通文档图片可以使用文本识别功能response await asyncio.to_thread( client.chat, modelglm-ocr:latest, messages[ { role: user, content: { text: Text Recognition: }, images: [document.png], } ], )4.2 表格识别对于包含表格的图片可以使用表格识别功能response await asyncio.to_thread( client.chat, modelglm-ocr:latest, messages[ { role: user, content: { table: Table Recognition: }, images: [table.png], } ], )4.3 公式识别对于包含数学公式的图片可以使用公式识别功能response await asyncio.to_thread( client.chat, modelglm-ocr:latest, messages[ { role: user, content: { formula: Formula Recognition: }, images: [formula.png], } ], )5. 性能优化建议5.1 批量处理如果需要处理多张图片可以考虑批量处理以提高效率responses [] for image_path in image_paths: response await asyncio.to_thread( client.chat, modelglm-ocr:latest, messages[ { role: user, content: { text: Text Recognition: }, images: [image_path], } ], ) responses.append(response)5.2 异步处理利用Python的异步特性可以提高处理效率async def process_image(image_path): response await asyncio.to_thread( client.chat, modelglm-ocr:latest, messages[ { role: user, content: { text: Text Recognition: }, images: [image_path], } ], ) return response tasks [process_image(path) for path in image_paths] results await asyncio.gather(*tasks)5.3 结果后处理GLM-OCR返回的结果可以直接使用也可以根据需要进行后处理def process_ocr_result(response): content response[message][content] # 自定义处理逻辑 processed content.replace(\n, br) # 示例替换换行符 return processed6. 总结通过Ollama调用GLM-OCR模型提供了一种简单高效的OCR解决方案相比传统的部署方式具有以下优势部署简单无需复杂的服务配置只需安装Ollama并拉取模型调用方便通过简洁的Python API即可完成OCR功能调用功能全面支持文本、表格、公式等多种识别任务性能良好基于GLM-V架构识别准确率高这种方法特别适合需要快速集成OCR功能的应用场景如文档处理系统、自动化办公工具等。开发者可以根据实际需求灵活选择识别功能和处理方式。获取更多AI镜像想探索更多AI镜像和应用场景访问 CSDN星图镜像广场提供丰富的预置镜像覆盖大模型推理、图像生成、视频生成、模型微调等多个领域支持一键部署。
GLM-OCR结合Ollama使用:另一种快速调用GLM-OCR模型的方法
发布时间:2026/5/28 16:30:55
GLM-OCR结合Ollama使用另一种快速调用GLM-OCR模型的方法1. 项目概述GLM-OCR是一个基于GLM-V编码器-解码器架构构建的多模态OCR模型专为复杂文档理解而设计。它集成了在大规模图文数据上预训练的CogViT视觉编码器、轻量级跨模态连接器以及GLM-0.5B语言解码器支持文本识别、表格识别和公式识别等多种功能。传统上GLM-OCR需要通过Gradio界面或Python API调用而本文将介绍一种更便捷的方法——通过Ollama库来调用GLM-OCR模型。这种方法简化了部署流程让开发者能够更快速地集成OCR功能到自己的应用中。2. 环境准备2.1 安装Ollama首先需要安装Ollama库这是一个用于与本地或远程Ollama服务交互的Python客户端pip install ollama2.2 拉取GLM-OCR模型使用Ollama命令行工具拉取GLM-OCR模型ollama pull glm-ocr:latest这个过程会自动下载模型文件下载完成后就可以通过API调用了。2.3 项目依赖配置创建一个pyproject.toml文件来管理项目依赖[project] name glm-ocr-ollama-inference version 0.1.0 description glm-ocr ollama api inference readme README.md requires-python 3.13,3.14 dependencies [ markupsafe3.0.2, ollama0.6.1, torch, torchaudio, torchvision, ] [tool.uv.sources] torch [ { index pytorch-cu128, marker sys_platform linux or sys_platform win32 }, ] torchvision [ { index pytorch-cu128, marker sys_platform linux or sys_platform win32 }, ] [[tool.uv.index]] name pytorch-cu128 url https://download.pytorch.org/whl/cu128 explicit true3. 使用Ollama API调用GLM-OCR3.1 基本调用方法创建一个Python脚本如main.py来调用GLM-OCR模型import asyncio from ollama import Client async def main(): client Client( hosthttp://localhost:11434, ) response await asyncio.to_thread( client.chat, modelglm-ocr:latest, messages[ { role: user, content: { text: Text Recognition:, formula: Formula Recognition:, table: Table Recognition: }, images: [inputs/1.png], } ], ) print(response) response_text response[message][content] response_lines response_text.strip().split(\n) for line in response_lines: print(line) if __name__ __main__: asyncio.run(main())3.2 功能说明GLM-OCR通过Ollama API支持三种主要功能文本识别使用text: Text Recognition:作为提示词表格识别使用table: Table Recognition:作为提示词公式识别使用formula: Formula Recognition:作为提示词3.3 参数说明host: Ollama服务地址默认为http://localhost:11434model: 使用的模型名称这里是glm-ocr:latestmessages: 包含用户输入和图片路径的列表images: 要识别的图片路径数组4. 实际应用示例4.1 文本识别对于普通文档图片可以使用文本识别功能response await asyncio.to_thread( client.chat, modelglm-ocr:latest, messages[ { role: user, content: { text: Text Recognition: }, images: [document.png], } ], )4.2 表格识别对于包含表格的图片可以使用表格识别功能response await asyncio.to_thread( client.chat, modelglm-ocr:latest, messages[ { role: user, content: { table: Table Recognition: }, images: [table.png], } ], )4.3 公式识别对于包含数学公式的图片可以使用公式识别功能response await asyncio.to_thread( client.chat, modelglm-ocr:latest, messages[ { role: user, content: { formula: Formula Recognition: }, images: [formula.png], } ], )5. 性能优化建议5.1 批量处理如果需要处理多张图片可以考虑批量处理以提高效率responses [] for image_path in image_paths: response await asyncio.to_thread( client.chat, modelglm-ocr:latest, messages[ { role: user, content: { text: Text Recognition: }, images: [image_path], } ], ) responses.append(response)5.2 异步处理利用Python的异步特性可以提高处理效率async def process_image(image_path): response await asyncio.to_thread( client.chat, modelglm-ocr:latest, messages[ { role: user, content: { text: Text Recognition: }, images: [image_path], } ], ) return response tasks [process_image(path) for path in image_paths] results await asyncio.gather(*tasks)5.3 结果后处理GLM-OCR返回的结果可以直接使用也可以根据需要进行后处理def process_ocr_result(response): content response[message][content] # 自定义处理逻辑 processed content.replace(\n, br) # 示例替换换行符 return processed6. 总结通过Ollama调用GLM-OCR模型提供了一种简单高效的OCR解决方案相比传统的部署方式具有以下优势部署简单无需复杂的服务配置只需安装Ollama并拉取模型调用方便通过简洁的Python API即可完成OCR功能调用功能全面支持文本、表格、公式等多种识别任务性能良好基于GLM-V架构识别准确率高这种方法特别适合需要快速集成OCR功能的应用场景如文档处理系统、自动化办公工具等。开发者可以根据实际需求灵活选择识别功能和处理方式。获取更多AI镜像想探索更多AI镜像和应用场景访问 CSDN星图镜像广场提供丰富的预置镜像覆盖大模型推理、图像生成、视频生成、模型微调等多个领域支持一键部署。