Image-Text-to-Text
MLX
Safetensors
Chinese
ocr
rare-characters
ids
ideographic-description-sequence
glyphwiki
palaeography
Instructions to use OpenGlyph/OpenGlyph-OCR-IDS with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- MLX
How to use OpenGlyph/OpenGlyph-OCR-IDS 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-IDS") config = load_config("OpenGlyph/OpenGlyph-OCR-IDS") # 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
- Atomic Chat
Download openglyph_ocr_ids/idsparse.py from OpenGlyph/OpenGlyph-OCR-IDS: direct link, hf CLI and curl.
- Browser
- Download file 2.4 kB
-
https://huggingface.co/OpenGlyph/OpenGlyph-OCR-IDS/resolve/main/openglyph_ocr_ids/idsparse.py
- Command line
-
hf download hf://OpenGlyph/OpenGlyph-OCR-IDS/openglyph_ocr_ids/idsparse.py
-
curl -L -o idsparse.py https://huggingface.co/OpenGlyph/OpenGlyph-OCR-IDS/resolve/main/openglyph_ocr_ids/idsparse.py
2.4 kB
| """Strict parsing of an Ideographic Description Sequence (IDS), and the literal GlyphWiki name of an IDS glyph | |
| (u2ff0-u961d-u4ee4 for ⿰阝令); such a name does not mean GlyphWiki has the glyph.""" | |
| from __future__ import annotations | |
| import re | |
| IDC = {chr(cp): f'u{cp:x}' for cp in range(0x2FF0, 0x3000)} | |
| ARITY = {op: (3 if op in '⿲⿳' else 1 if op in '' else 2) for op in IDC} | |
| def parse_tree(expression): | |
| if not isinstance(expression, str) or not expression: | |
| raise ValueError('IDS 不能为空。') | |
| stack, root, i = [], None, 0 | |
| while i < len(expression): | |
| ch = expression[i] | |
| i += 1 | |
| if root is not None and not stack: | |
| raise ValueError('IDS 有多余部件;请检查结构符和部件数。') | |
| if ch in IDC: | |
| node = {'op': ch, 'children': []} | |
| elif ch == '{': | |
| end = expression.find('}', i) | |
| if end < 0 or end == i: | |
| raise ValueError('IDS 部件引用需要完整的 {glyph_ref}。') | |
| ref = expression[i:end] | |
| if any(c.isspace() or c in '{}' for c in ref): | |
| raise ValueError('无效 IDS 部件引用。') | |
| node = {'glyph': ref} | |
| i = end + 1 | |
| else: | |
| cp = ord(ch) | |
| if ch.isspace() or ch in '{}()[]#' or cp < 0x2000 or 0xD800 <= cp <= 0xDFFF or 0xE0100 <= cp <= 0xE01EF: | |
| raise ValueError('IDS 含不支持的选择符;请用 glyph_lookup 返回的具体字形引用。') | |
| node = {'glyph': ch} | |
| if stack: | |
| stack[-1]['children'].append(node) | |
| else: | |
| root = node | |
| if 'op' in node: | |
| stack.append(node) | |
| while stack and len(stack[-1]['children']) == ARITY[stack[-1]['op']]: | |
| stack.pop() | |
| if stack or root is None: | |
| raise ValueError('IDS 缺少部件;不会用空白或相近字补齐。') | |
| return root | |
| def to_glyph_name(ids): | |
| try: | |
| node = parse_tree(ids) | |
| except ValueError: | |
| return None | |
| parts, pending = [], [node] | |
| while pending: | |
| item = pending.pop() | |
| if 'glyph' in item: | |
| if len(item['glyph']) != 1: | |
| return None | |
| parts.append(f'u{ord(item["glyph"]):x}') | |
| else: | |
| parts.append(IDC[item['op']]) | |
| pending.extend(reversed(item['children'])) | |
| return '-'.join(parts) | |