"""GPT-2 language model for "AI 選字" (numpy). Port of LanguageModel.swift. Reads the "AZLM" file made by training/lm/export_lm.py (CKIP gpt2-tiny-chinese, GPL-3.0, float16 weights, expanded to float32 in memory). numpy is optional: when it cannot be imported, `available()` is False and the input method works without AI. """ import json import math import struct try: import numpy as np except Exception: # ImportError, or a numpy build for another Python / architecture np = None CLS, UNK = 101, 100 def available(): return np is not None class GPT2LanguageModel: max_tokens = 64 def __init__(self, model_path, vocab_path): if np is None: raise RuntimeError("numpy is not available") with open(model_path, "rb") as f: raw = f.read() if raw[:4] != b"AZLM": raise ValueError("not an AZLM file") n = struct.unpack("