Skip to content

Commit a5432cc

Browse files
authored
Merge pull request #1325 from PyThaiNLP/remove-pickle
Remove pickle from pythainlp/generate/thai2fit.py
2 parents 73635bc + cb18b9a commit a5432cc

2 files changed

Lines changed: 7 additions & 10 deletions

File tree

pythainlp/generate/thai2fit.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
__all__: list[str] = ["gen_sentence"]
1313

14-
import pickle
14+
import json
1515
import random
1616
from typing import TYPE_CHECKING, Any, Union
1717

@@ -58,22 +58,18 @@
5858
thwiki: dict[str, Any] = THWIKI_LSTM
5959

6060
# Validate that corpus files are available
61-
if thwiki["itos_fname"] is None or thwiki["wgts_fname"] is None:
61+
if thwiki["json_itos_fname"] is None or thwiki["wgts_fname"] is None:
6262
raise FileNotFoundError(
6363
"corpus-not-found names=['wiki_lm_lstm', 'wiki_itos_lstm']\n"
6464
" Thai2fit model files not found.\n"
6565
" Python: pythainlp.corpus.download('wiki_lm_lstm')\n"
6666
" CLI: thainlp data get wiki_lm_lstm\n"
67-
" Python: pythainlp.corpus.download('wiki_itos_lstm')\n"
68-
" CLI: thainlp data get wiki_itos_lstm"
67+
" Python: pythainlp.corpus.download('json_itos_fname')\n"
68+
" CLI: thainlp data get json_itos_fname"
6969
)
7070

71-
# Loads a pickle file from PyThaiNLP's official repository with MD5 verification.
72-
# WARNING:
73-
# Pickle deserialization can execute arbitrary code if the file is malicious.
74-
# Users should only use files from trusted sources.
75-
with open(thwiki["itos_fname"], "rb") as f:
76-
thwiki_itos: list[str] = pickle.load(f) # noqa: S301
71+
with open(thwiki["json_itos_fname"], "r") as f:
72+
thwiki_itos: list[str] = json.load(f)
7773
thwiki_vocab: "Vocab" = fastai.text.transform.Vocab(thwiki_itos)
7874

7975
# dummy databunch

pythainlp/ulmfit/core.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
THWIKI_LSTM: dict[str, Optional[str]] = {
5252
"wgts_fname": get_corpus_path(_MODEL_NAME_LSTM),
5353
"itos_fname": get_corpus_path(_ITOS_NAME_LSTM),
54+
"json_itos_fname": get_corpus_path("wiki_itos_lstm_json"),
5455
}
5556

5657

0 commit comments

Comments
 (0)