Self-host GLM-OCR with SGLang
mainDeploy the model locally using SGLang.
Note: You may need to adjust --context-len and --mem-fraction-static based on your hardware to handle large images or PDFs.
repository·main·Indexed 27 days ago
https://github.com/zai-org/glm-ocrGLM-OCR is a multimodal OCR model for complex document understanding, including formula, table, and information extraction, utilizing a two-stage pipeline of layout analysis and parallel recognition. The project includes a FastAPI-based asynchronous backend, a Z-OCR frontend, and a Python SDK. It supports multiple deployment options including Zhipu MaaS API, vLLM, SGLang, and Docker.
Deploy the model locally using SGLang.
Note: You may need to adjust --context-len and --mem-fraction-static based on your hardware to handle large images or PDFs.
To prepare the application for production, use the following commands to build the project and preview the resulting build locally:
pnpm buildpnpm previewpnpm build
pnpm previewTo self-host the model using vLLM, ensure you have installed the selfhosted dependencies and a compatible version of vLLM. You can use the --speculative-config flag to enable Multi-Token Prediction (MTP) for better performance.
# Install dependencies
pip install -U "vllm>=0.19.0"
pip install "transformers>=5.3.0"
# Start vLLM service
vllm serve zai-org/GLM-OCR --port 8080 --speculative-config '{"method": "mtp", "num_speculative_tokens": 3}' --served-model-name glm-ocrYou can deploy the backend using Docker.
docker build -t glm-ocr:latest .docker run -d \
--name glm-ocr \
-p 8000:8000 \
-v $(pwd)/data:/backend/data \
-e WORKER_COUNT=5 \
glm-ocr:latestDeploy the model locally using vLLM for full control over the pipeline (layout detection, OCR, and formatting).
Note: You may need to adjust --max-model-len and --gpu-memory-utilization based on your hardware to handle large images or PDFs.
# Install vLLM
pip install -U "vllm>=0.19.0"
# Launch the service
pip install "transformers>=5.3.0"
vllm serve zai-org/GLM-OCR --port 8080 --speculative-config '{"method": "mtp", "num_speculative_tokens": 3}' --served-model-name glm-ocrTo use GLM-OCR, you must first obtain a ZHIPU_API_KEY from the Zhipu AI platform. Once obtained, configure the environment using the provided setup script.
python scripts/config_setup.py setup --api-key YOUR_KEYThe easiest way to use GLM-OCR without a GPU is via the Zhipu cloud API. The SDK acts as a thin wrapper that forwards documents to the cloud and returns Markdown and JSON layout details.
config.yaml to enable MaaS mode.pipeline:
maas:
enabled: true # Enable MaaS mode
api_key: your-api-key # RequiredThe glmocr-handwriting skill requires a ZHIPU_API_KEY environment variable to access the ZhiPu GLM-OCR layout parsing API. You can configure this in three ways:
openclaw.json under env.vars to share it across all Zhipu skills.openclaw.json under skills.entries.glmocr-handwriting.env.export ZHIPU_API_KEY="your_key" to your shell profile (e.g., ~/.zshrc).Obtain your key from the Zhipu Open Platform.
{
"env": {
"vars": {
"ZHIPU_API_KEY": "你的密钥"
}
}
}To add a new processing flow, create a new class in backend/core/flows/ that inherits from TaskProcessingFlow and implement the process() method. Finally, register it with the FlowFactory.
from backend.core.flows.base import TaskProcessingFlow, FlowFactory
class MyCustomFlow(TaskProcessingFlow):
async def process(self) -> Dict[str, Any]:
# Implement processing logic
pass
# Register the flow
FlowFactory.register_flow("custom", MyCustomFlow)from backend.core.flows.base import TaskProcessingFlow, FlowFactory
class MyCustomFlow(TaskProcessingFlow):
async def process(self) -> Dict[str, Any]:
# Implement processing logic
pass
# Register the flow
FlowFactory.register_flow("custom", MyCustomFlow)To add new UI components from the Shadcn library to the project, use the pnpm dlx shadcn@latest add command followed by the component name.
pnpm dlx shadcn@latest add [component-name]
# Example:
pnpm dlx shadcn@latest add buttonThe GLM OCR backend is an asynchronous document processing system built with FastAPI. It requires Python 3.12+ and SQLite (or another supported database).
uv package managermacOS / Linux:
curl -LsSf https://astral.sh/uv/install.sh | shWindows:
powershell -c "irm https://astral.sh/uv/install.ps1 | iex"uv syncCreate a .env file to configure the system:
APP_NAME=OCR Task System
DEBUG=False
HOST=0.0.0.0
PORT=8000
DATABASE_URL=sqlite+aiosqlite:///./tasks.db
OUTPUT_DIR=./data
WORKER_COUNT=5
TASK_TIMEOUT=3600uv run uvicorn backend.main:app --reload --host 0.0.0.0 --port 8000Access the interactive Swagger/OpenAPI documentation at http://localhost:8000/docs.
uv run uvicorn backend.main:app --reload --host 0.0.0.0 --port 8000The glmocr-formula skill requires a ZHIPU_API_KEY to access the ZhiPu GLM-OCR layout parsing API. You can obtain a key from the Zhipu Open Platform.
Choose one of the following configuration methods:
Add the key to your openclaw.json file under env.vars. This allows all Zhipu-related skills to share the same key.
{
"env": {
"vars": {
"ZHIPU_API_KEY": "YOUR_API_KEY"
}
}
}Set the key specifically for the glmocr-formula skill in openclaw.json:
{
"skills": {
"entries": {
"glmocr-formula": {
"env": {
"ZHIPU_API_KEY": "YOUR_API_KEY"
}
}
}
}
}Add the following to your shell profile (e.g., ~/.zshrc or ~/.bashrc):
export ZHIPU_API_KEY="YOUR_API_KEY"{
"env": {
"vars": {
"ZHIPU_API_KEY": "你的密钥"
}
}
}