File size: 1,071 Bytes
4af42ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Log-only wrapper: records every string the model's own HuggingfaceTokenizer receives (raw + cleaned + untruncated
# token count), then runs the unmodified-entry script. Does not change any tensor or argument.
import importlib, json, os, runpy, sys
mod_name, log_path, script = sys.argv[1], sys.argv[2], sys.argv[3]
sys.argv = [script] + sys.argv[4:]
sys.path.insert(0, os.path.dirname(os.path.abspath(script)))
mod = importlib.import_module(mod_name)
cls = mod.HuggingfaceTokenizer; orig = cls.__call__
def logged(self, sequence, **kw):
    seqs = [sequence] if isinstance(sequence, str) else list(sequence)
    with open(log_path, "a", encoding="utf-8") as f:
        for s in seqs:
            c = self._clean(s) if self.clean else s
            n = len(self.tokenizer(c, add_special_tokens=kw.get("add_special_tokens", True)).input_ids)
            f.write(json.dumps({"raw": s, "cleaned": c, "n_tokens": n, "seq_len": self.seq_len}, ensure_ascii=False) + "\n")
    return orig(self, sequence, **kw)
cls.__call__ = logged
runpy.run_path(script, run_name="__main__")