-
Notifications
You must be signed in to change notification settings - Fork 90
/
utils.py
45 lines (35 loc) · 1.31 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import os
import logging
import sentencepiece as spm
def chinese_tokenizer_load():
sp_chn = spm.SentencePieceProcessor()
sp_chn.Load('{}.model'.format("./tokenizer/chn"))
return sp_chn
def english_tokenizer_load():
sp_eng = spm.SentencePieceProcessor()
sp_eng.Load('{}.model'.format("./tokenizer/eng"))
return sp_eng
def set_logger(log_path):
"""Set the logger to log info in terminal and file `log_path`.
In general, it is useful to have a logger so that every output to the terminal is saved
in a permanent file. Here we save it to `model_dir/train.log`.
Example:
```
logging.info("Starting training...")
```
Args:
log_path: (string) where to log
"""
if os.path.exists(log_path) is True:
os.remove(log_path)
logger = logging.getLogger()
logger.setLevel(logging.INFO)
if not logger.handlers:
# Logging to a file
file_handler = logging.FileHandler(log_path)
file_handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s: %(message)s'))
logger.addHandler(file_handler)
# Logging to console
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(logging.Formatter('%(message)s'))
logger.addHandler(stream_handler)