Image-Text-to-Text
Transformers
Safetensors
MLX
Chinese
openglyph_ocr
feature-extraction
ocr
rare-characters
glyphwiki
palaeography
conversational
custom_code
Instructions to use OpenGlyph/OpenGlyph-OCR-0 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use OpenGlyph/OpenGlyph-OCR-0 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="OpenGlyph/OpenGlyph-OCR-0", trust_remote_code=True) messages = [ { "role": "user", "content": [ {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"}, {"type": "text", "text": "What animal is on the candy?"} ] }, ] pipe(text=messages)# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("OpenGlyph/OpenGlyph-OCR-0", trust_remote_code=True, device_map="auto") - MLX
How to use OpenGlyph/OpenGlyph-OCR-0 with MLX:
# Make sure mlx-vlm is installed # pip install --upgrade mlx-vlm from mlx_vlm import load, generate from mlx_vlm.prompt_utils import apply_chat_template from mlx_vlm.utils import load_config # Load the model model, processor = load("OpenGlyph/OpenGlyph-OCR-0") config = load_config("OpenGlyph/OpenGlyph-OCR-0") # Prepare input image = ["http://images.cocodataset.org/val2017/000000039769.jpg"] prompt = "Describe this image." # Apply chat template formatted_prompt = apply_chat_template( processor, config, prompt, num_images=1 ) # Generate output output = generate(model, processor, formatted_prompt, image) print(output) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- LM Studio
- vLLM
How to use OpenGlyph/OpenGlyph-OCR-0 with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "OpenGlyph/OpenGlyph-OCR-0" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "OpenGlyph/OpenGlyph-OCR-0", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'Use Docker
docker model run hf.co/OpenGlyph/OpenGlyph-OCR-0
- SGLang
How to use OpenGlyph/OpenGlyph-OCR-0 with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "OpenGlyph/OpenGlyph-OCR-0" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "OpenGlyph/OpenGlyph-OCR-0", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "OpenGlyph/OpenGlyph-OCR-0" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "OpenGlyph/OpenGlyph-OCR-0", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }' - Pi
How to use OpenGlyph/OpenGlyph-OCR-0 with Pi:
Start the MLX server
# Install MLX LM: uv tool install mlx-lm # Start a local OpenAI-compatible server: mlx_lm.server --model "OpenGlyph/OpenGlyph-OCR-0"
Configure the model in Pi
# Install Pi: npm install -g @earendil-works/pi-coding-agent # Add to ~/.pi/agent/models.json: { "providers": { "mlx-lm": { "baseUrl": "http://localhost:8080/v1", "api": "openai-completions", "apiKey": "none", "models": [ { "id": "OpenGlyph/OpenGlyph-OCR-0" } ] } } }Run Pi
# Start Pi in your project directory: pi
- Docker Model Runner
How to use OpenGlyph/OpenGlyph-OCR-0 with Docker Model Runner:
docker model run hf.co/OpenGlyph/OpenGlyph-OCR-0
- Hermes Agent
How to use OpenGlyph/OpenGlyph-OCR-0 with Hermes Agent:
Start the MLX server
# Install MLX LM: uv tool install mlx-lm # Start a local OpenAI-compatible server: mlx_lm.server --model "OpenGlyph/OpenGlyph-OCR-0"
Configure Hermes
# Install Hermes: curl -fsSL https://hermes-agent.nousresearch.com/install.sh | bash hermes setup # Point Hermes at the local server: hermes config set model.provider custom hermes config set model.base_url http://127.0.0.1:8080/v1 hermes config set model.default OpenGlyph/OpenGlyph-OCR-0
Run Hermes
hermes
- Atomic Chat
- OpenClaw
How to use OpenGlyph/OpenGlyph-OCR-0 with OpenClaw:
Start the MLX server
# Install MLX LM: uv tool install mlx-lm # Start a local OpenAI-compatible server: mlx_lm.server --model "OpenGlyph/OpenGlyph-OCR-0"
Configure OpenClaw
# Install OpenClaw: npm install -g openclaw@latest # Register the local server and set it as the default model: openclaw onboard --non-interactive --mode local \ --auth-choice custom-api-key \ --custom-base-url http://127.0.0.1:8080/v1 \ --custom-model-id "OpenGlyph/OpenGlyph-OCR-0" \ --custom-provider-id mlx-lm \ --custom-compatibility openai \ --custom-text-input \ --accept-risk \ --skip-health
Run OpenClaw
openclaw agent --local --agent main --message "Hello from Hugging Face"
OpenGlyph-OCR-0: the model in two forms (transformers and MLX), model card, licence
853865d verified Download modeling_openglyph_ocr.py from OpenGlyph/OpenGlyph-OCR-0: direct link, hf CLI and curl.
- Browser
- Download file 4.45 kB
-
https://huggingface.co/OpenGlyph/OpenGlyph-OCR-0/resolve/main/modeling_openglyph_ocr.py
- Command line
-
hf download hf://OpenGlyph/OpenGlyph-OCR-0/modeling_openglyph_ocr.py
-
curl -L -o modeling_openglyph_ocr.py https://huggingface.co/OpenGlyph/OpenGlyph-OCR-0/resolve/main/modeling_openglyph_ocr.py
4.45 kB
| """OpenGlyph-OCR-0 for PyTorch: Qwen3.5 (Xiaomi-OCR-0) whose output layer has a bias and is not tied to the input embedding. | |
| import torch | |
| from transformers import AutoModelForImageTextToText, AutoProcessor | |
| repo = "OpenGlyph/OpenGlyph-OCR-0" | |
| model = AutoModelForImageTextToText.from_pretrained(repo, trust_remote_code=True, dtype=torch.float32).eval() | |
| processor = AutoProcessor.from_pretrained(repo, trust_remote_code=True) | |
| print(model.read_line(processor, "line.png")["text"]) | |
| Everything but the output layer is the stock model. The 159,052 added tokens (every CJK extension character and the | |
| GlyphWiki glyphs without a code point, written <gw:NAME>) each have an output row and a bias of their own; their input | |
| rows are one and the same vector. The weights that training changed are stored in float32 and the rest as the base | |
| model's 16-bit floats, so load the model in float32 (`dtype=torch.float32`, which is also what its config asks for): every | |
| weight is then exactly the trained one. Loaded in 16-bit floats the trained weights are rounded, and that was not | |
| measured. | |
| """ | |
| import warnings | |
| import torch | |
| from torch import nn | |
| from transformers.models.qwen3_5.modeling_qwen3_5 import ( | |
| Qwen3_5ForConditionalGeneration, | |
| Qwen3_5Model, | |
| Qwen3_5PreTrainedModel, | |
| ) | |
| from .configuration_openglyph_ocr import OpenGlyphOCRConfig | |
| # The prompt of training, word for word. | |
| PROMPT = ( | |
| "Transcribe the image in reading order. Output ordinary characters as Unicode text. " | |
| "For a catalog-specific historical glyph, output <gw:GLYPH_ID>, replacing GLYPH_ID with its exact ebag_s catalog name. " | |
| "Read vertical columns top-to-bottom and right-to-left. Preserve punctuation and line breaks. " | |
| "Return only the transcription, without markdown fences or explanation." | |
| ) | |
| class OpenGlyphOCRForConditionalGeneration(Qwen3_5ForConditionalGeneration): | |
| config_class = OpenGlyphOCRConfig | |
| _tied_weights_keys = {} # the head is not tied: an added token's output row is not its input row | |
| def __init__(self, config): | |
| Qwen3_5PreTrainedModel.__init__(self, config) | |
| self.model = Qwen3_5Model(config) | |
| # as the stock class, but with a bias: nought for the base vocabulary, trained for every added token | |
| self.lm_head = nn.Linear(config.text_config.hidden_size, config.text_config.vocab_size, bias=True) | |
| self.post_init() | |
| def read_line(self, processor, image, max_new_tokens=256): | |
| """Read one line picture (a path or a PIL image): greedy, nothing constrained. | |
| Returns the text, the token ids, and whether the model ended the line itself (`complete`; False: it ran into | |
| max_new_tokens). A glyph without a code point is written <gw:NAME>, NAME being its GlyphWiki name.""" | |
| from PIL import Image | |
| if not isinstance(image, Image.Image): | |
| image = Image.open(image) | |
| image = image.convert("RGB") | |
| backend = getattr(getattr(processor, "image_processor", None), "backend", None) | |
| if backend is not None and backend != "torchvision": | |
| # without torchvision, transformers prepares the picture with PIL | |
| warnings.warn( | |
| f"The picture is prepared with {backend}, not with torchvision as the pictures were when this model " | |
| "was measured: the pixel values differ by up to two grey levels. Install torchvision." | |
| ) | |
| messages =[{"role": "user", "content": [{"type": "image"}, {"type": "text", "text": PROMPT}]}] | |
| prompt = processor.apply_chat_template( | |
| messages, tokenize=False, add_generation_prompt=True, enable_thinking=False | |
| ) | |
| inputs = processor(text=[prompt], images=[image], padding=True, return_tensors="pt") | |
| inputs = inputs.to(self.device) | |
| output = self.generate(**inputs, max_new_tokens=max_new_tokens, do_sample=False) | |
| tokens = output[0, inputs["input_ids"].shape[1] :].tolist() | |
| stop = self.generation_config.eos_token_id | |
| stop = set(stop if isinstance(stop, (list, tuple)) else [stop]) | |
| complete = bool(tokens) and tokens[-1] in stop | |
| if complete: | |
| tokens = tokens[:-1] | |
| text = processor.tokenizer.decode(tokens, skip_special_tokens=False, clean_up_tokenization_spaces=False) | |
| return {"text": text, "token_ids": tokens, "complete": complete} | |
| __all__ = ["OpenGlyphOCRForConditionalGeneration"] | |