LLM MLX VLM GLM OCR - eiichiromomma/CVMLAB GitHub Wiki
(LLM) MLX-VLM GLM-OCR
話題のOCR
やることは
- パーサー込みの分解: glmocr
- 脳味噌: mlx-vlmで
mlx-community/GLM-OCR-bf16をサーバー駆動で呼び出し
なので,GPU1つしかない状況の場合はmax_workers=32を1にしておかないと並列で変換待ちが大量発生してタイムアウトが起きる.
WordやExcelからAcrobat経由で変換されて,神Excelな表が埋め込まれてたり,エンコードとか透明テキストがグチャってるPDFも綺麗にデジタル化できるので,RAGに放り込める.
セットアップ
git clone https://github.com/zai-org/GLM-OCR.git
cd GLM-OCR
uv python pin 3.10
でpyproject.tomlを弄ってpythonを3.10以上にしておく
uv sync
uv pip install -e .
uv add git+https://github.com/huggingface/transformers.git
サーバー起動
mlx-vlmを入れてる方のプロジェクトで
uv run mlx_vlm.server --trust-remote-code
別のターミナルで
curl http://localhost:8080/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "mlx-community/GLM-OCR-bf16",
"messages": [{"role": "user", "content": [{"type": "text", "text": "hello"}]}],
"max_tokens": 10
}'
で応答してればOK.mlx_vlm.serverの方はそのまま放置.
glmocrに画像を投げる
glmocr/config.yamlを編集する
- modelをmlx-community/GLM-OCR-bf16
- api_pathのv1/を削る
- max_workers = 1
- request_timeout = 600
- max_tokens = 8192
uv run glmocr parse my_image.png
でパース込みのOCRが始まり,outputフォルダ内にファイル名のフォルダが作られ,Markdown, JSONの出力と,パースした結果のjpg(領域とConfidence)が収められる.
ちなみにpdfも対応できるので,本当に丸投げできるが,max_workers=1にしておかないと,密なページが来たときにタイムアウト連発して詰む.1にしておけば260ページ近いうちの学部要覧も完走した.ただし,Denseな表のページで力尽きるのでmax_tokens=8192は必須.
エラー対策
Error: Layout detector failed to import (dependencies may already be installed). See the original error for the real cause. Original error: SyntaxError: invalid syntax. Perhaps you forgot a comma? (_tensor.py, line 1307)
というエラーが出たらバージョンの問題らしいので
uv add -U torch torchvision
uv add -U --force-reinstall layoutparser