From 1b72565e1de3e4528923dccb638fab5bf2cc6df3 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 13 Mar 2026 22:02:17 +0000 Subject: [PATCH 1/2] Initial plan From c70714156eae80020b7abfd5b6a2ee6f3ad1a15e Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 13 Mar 2026 22:07:23 +0000 Subject: [PATCH 2/2] Fix np.load allow_pickle=False to work with .npz NpzFile format - Replace .item().get(key) with [key] dict-style access on NpzFile - Remove variables instance attribute; use local variable instead - Add type annotation for variables local var as np.lib.npyio.NpzFile - Add allow_pickle=False to embeddings.npy load in words_spelling_correction.py Co-authored-by: wannaphong <8536487+wannaphong@users.noreply.github.com> --- pythainlp/spell/words_spelling_correction.py | 4 +- pythainlp/transliterate/w2p.py | 50 ++++++-------------- 2 files changed, 18 insertions(+), 36 deletions(-) diff --git a/pythainlp/spell/words_spelling_correction.py b/pythainlp/spell/words_spelling_correction.py index 847d63fe1..c5f20c346 100644 --- a/pythainlp/spell/words_spelling_correction.py +++ b/pythainlp/spell/words_spelling_correction.py @@ -84,7 +84,9 @@ def _load_embeddings(self) -> tuple[list[str], NDArray[np.float32]]: """Loads embeddings matrix and vocabulary list.""" import numpy as np - input_matrix = np.load(os.path.join(self.model_dir, "embeddings.npy")) + input_matrix = np.load( + os.path.join(self.model_dir, "embeddings.npy"), allow_pickle=False + ) words = [] vocab_path = os.path.join(self.model_dir, "vocabulary.txt") with open(vocab_path, encoding="utf-8") as f: diff --git a/pythainlp/transliterate/w2p.py b/pythainlp/transliterate/w2p.py index 2e5165330..7f52d331e 100644 --- a/pythainlp/transliterate/w2p.py +++ b/pythainlp/transliterate/w2p.py @@ -60,7 +60,6 @@ class Thai_W2P: p2idx: dict[str, int] idx2p: dict[int, str] checkpoint: Optional[str] - variables: "NDArray" enc_emb: "NDArray" enc_w_ih: "NDArray" enc_w_hh: "NDArray" @@ -101,52 +100,33 @@ def _load_variables(self) -> None: if self.checkpoint is None: raise RuntimeError("checkpoint path is not set") - self.variables: "NDArray" = np.load(self.checkpoint, allow_pickle=False) - # (29, 64). (len(graphemes), emb) - self.enc_emb: "NDArray" = self.variables.item().get( - "encoder.emb.weight" + variables: "np.lib.npyio.NpzFile" = np.load( + self.checkpoint, allow_pickle=False ) + # (29, 64). (len(graphemes), emb) + self.enc_emb: "NDArray" = variables["encoder.emb.weight"] # (3*128, 64) - self.enc_w_ih: "NDArray" = self.variables.item().get( - "encoder.rnn.weight_ih_l0" - ) + self.enc_w_ih: "NDArray" = variables["encoder.rnn.weight_ih_l0"] # (3*128, 128) - self.enc_w_hh: "NDArray" = self.variables.item().get( - "encoder.rnn.weight_hh_l0" - ) + self.enc_w_hh: "NDArray" = variables["encoder.rnn.weight_hh_l0"] # (3*128,) - self.enc_b_ih: "NDArray" = self.variables.item().get( - "encoder.rnn.bias_ih_l0" - ) + self.enc_b_ih: "NDArray" = variables["encoder.rnn.bias_ih_l0"] # (3*128,) - self.enc_b_hh: "NDArray" = self.variables.item().get( - "encoder.rnn.bias_hh_l0" - ) - + self.enc_b_hh: "NDArray" = variables["encoder.rnn.bias_hh_l0"] # (74, 64). (len(phonemes), emb) - self.dec_emb: "NDArray" = self.variables.item().get( - "decoder.emb.weight" - ) + self.dec_emb: "NDArray" = variables["decoder.emb.weight"] # (3*128, 64) - self.dec_w_ih: "NDArray" = self.variables.item().get( - "decoder.rnn.weight_ih_l0" - ) + self.dec_w_ih: "NDArray" = variables["decoder.rnn.weight_ih_l0"] # (3*128, 128) - self.dec_w_hh: "NDArray" = self.variables.item().get( - "decoder.rnn.weight_hh_l0" - ) + self.dec_w_hh: "NDArray" = variables["decoder.rnn.weight_hh_l0"] # (3*128,) - self.dec_b_ih: "NDArray" = self.variables.item().get( - "decoder.rnn.bias_ih_l0" - ) + self.dec_b_ih: "NDArray" = variables["decoder.rnn.bias_ih_l0"] # (3*128,) - self.dec_b_hh: "NDArray" = self.variables.item().get( - "decoder.rnn.bias_hh_l0" - ) + self.dec_b_hh: "NDArray" = variables["decoder.rnn.bias_hh_l0"] # (74, 128) - self.fc_w: "NDArray" = self.variables.item().get("decoder.fc.weight") + self.fc_w: "NDArray" = variables["decoder.fc.weight"] # (74,) - self.fc_b: "NDArray" = self.variables.item().get("decoder.fc.bias") + self.fc_b: "NDArray" = variables["decoder.fc.bias"] def _sigmoid(self, x: "np.ndarray") -> "np.ndarray": import numpy as np