-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_missing_tokens.py
58 lines (47 loc) · 2.06 KB
/
add_missing_tokens.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
46
47
48
49
50
51
52
53
54
55
56
57
58
# add_missing_tokens.py
import json
def extract_tokens_from_merges(merges_path):
tokens = set()
try:
with open(merges_path, 'r', encoding='utf-8') as merges_file:
for line in merges_file:
if not line.startswith("#") and line.strip():
tokens.update(line.strip().split())
except Exception as e:
print(f"Error reading merges.txt: {e}")
return tokens
def extract_tokens_from_vocab(vocab_path):
tokens = set()
vocab = {}
try:
with open(vocab_path, 'r', encoding='utf-8') as vocab_file:
vocab = json.load(vocab_file)
tokens = set(vocab.keys())
except Exception as e:
print(f"Error reading vocab.json: {e}")
return tokens, vocab
def add_missing_tokens(vocab_path, merges_tokens, vocab_tokens, vocab):
missing_tokens = merges_tokens - vocab_tokens
current_index = max(vocab.values()) + 1
if missing_tokens:
try:
for token in missing_tokens:
print(f"Adding missing token: {token} -> {current_index}") # Debugging
vocab[token] = current_index
current_index += 1
with open(vocab_path, 'w', encoding='utf-8') as vocab_file:
json.dump(vocab, vocab_file, ensure_ascii=False, indent=2)
print(f"Added {len(missing_tokens)} missing tokens to vocab.json.")
print(f"Missing tokens added: {missing_tokens}")
except Exception as e:
print(f"Error updating vocab.json: {e}")
else:
print("No missing tokens found. vocab.json is up to date.")
if __name__ == "__main__":
merges_path = "./converted_model/merges.txt"
vocab_path = "./converted_model/vocab.json"
merges_tokens = extract_tokens_from_merges(merges_path)
vocab_tokens, vocab = extract_tokens_from_vocab(vocab_path)
print(f"Extracted tokens from merges.txt: {list(merges_tokens)[:10]} ...")
print(f"Extracted tokens from vocab.json: {list(vocab_tokens)[:10]} ...")
add_missing_tokens(vocab_path, merges_tokens, vocab_tokens, vocab)