OpenSoftware-World commited on
Commit
b65dc0a
·
verified ·
1 Parent(s): a2e78fe

Training code for the SentencePiece tokenizer for the OpenSoftware-World-OSW1 AI model. (This code was written by ChatGPT and edited by OpenSoftware-World.)

Browse files

Thanks to the SentencePiece tokenizer, the OpenSoftware-World-OSW1 AI model will be able to generate more natural and high-quality responses. (The quality of the responses depends on the quality of the dataset.)

Files changed (1) hide show
  1. sentencepiece_tokenizer_training.py +117 -0
sentencepiece_tokenizer_training.py ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ import glob
4
+ import sentencepiece as spm
5
+
6
+ DATA_DIR = "data"
7
+ OUTPUT_TEXT = "dataset.txt"
8
+
9
+ VOCAB_SIZE = 8000
10
+ MODEL_PREFIX = "opensoftware_world_osw1_tokenizer"
11
+
12
+ def load_json_pairs(json_dir):
13
+ texts = []
14
+ if not os.path.isdir(json_dir):
15
+ return texts
16
+
17
+ for path in glob.glob(os.path.join(json_dir, "*.json")):
18
+ try:
19
+ with open(path, "r", encoding="utf-8") as f:
20
+ data = json.load(f)
21
+ except Exception as e:
22
+ print(f"⚠️ {path} Unreadable: {e}")
23
+ continue
24
+
25
+ intents = data.get("intents", data if isinstance(data, list) else [])
26
+
27
+ for intent in intents:
28
+ for p in intent.get("patterns", []):
29
+ texts.append(p)
30
+
31
+ for r in intent.get("responses", []):
32
+ texts.append(r)
33
+
34
+ return texts
35
+
36
+ def load_txt_qa_pairs(qa_dir):
37
+ texts = []
38
+ if not os.path.isdir(qa_dir):
39
+ return texts
40
+
41
+ for path in glob.glob(os.path.join(qa_dir, "*.txt")):
42
+ with open(path, "r", encoding="utf-8") as f:
43
+ lines = f.readlines()
44
+
45
+ for line in lines:
46
+ line = line.strip()
47
+
48
+ if line.startswith("Q:"):
49
+ texts.append(line[2:].strip())
50
+
51
+ elif line.startswith("A:"):
52
+ texts.append(line[2:].strip())
53
+
54
+ return texts
55
+
56
+ def load_plain_texts(txt_dir):
57
+ texts = []
58
+ if not os.path.isdir(txt_dir):
59
+ return texts
60
+
61
+ for path in glob.glob(os.path.join(txt_dir, "*.txt")):
62
+ with open(path, "r", encoding="utf-8") as f:
63
+ content = f.read().strip()
64
+
65
+ if content:
66
+ texts.append(content)
67
+
68
+ return texts
69
+
70
+ print("📚 Reading training data...")
71
+
72
+ all_texts = []
73
+
74
+ all_texts.extend(load_json_pairs(os.path.join(DATA_DIR, "json")))
75
+ all_texts.extend(load_txt_qa_pairs(os.path.join(DATA_DIR, "txt_qa")))
76
+ all_texts.extend(load_plain_texts(os.path.join(DATA_DIR, "txt")))
77
+
78
+ if len(all_texts) == 0:
79
+ raise RuntimeError("No training data was found.")
80
+
81
+ print(f"✅ Total number of texts: {len(all_texts)}")
82
+
83
+ with open(OUTPUT_TEXT, "w", encoding="utf-8") as f:
84
+ for text in all_texts:
85
+ f.write(text.replace("\n", " ") + "\n")
86
+
87
+ print(f"📝 {OUTPUT_TEXT} was created.")
88
+
89
+ print("🧠 The SentencePiece tokenizer is being trained...")
90
+
91
+ spm.SentencePieceTrainer.train(
92
+ input=OUTPUT_TEXT,
93
+ model_prefix=MODEL_PREFIX,
94
+ vocab_size=VOCAB_SIZE,
95
+ hard_vocab_limit=False,
96
+ model_type="unigram",
97
+
98
+ character_coverage=1.0,
99
+
100
+ pad_id=0,
101
+ unk_id=1,
102
+ bos_id=2,
103
+ eos_id=3,
104
+
105
+ shuffle_input_sentence=True,
106
+
107
+ pad_piece="<pad>",
108
+ unk_piece="<unk>",
109
+ bos_piece="<bos>",
110
+ eos_piece="<eos>",
111
+
112
+ train_extremely_large_corpus=True
113
+ )
114
+
115
+ print("\n🎉 Completed!")
116
+ print(f"Model : {MODEL_PREFIX}.model")
117
+ print(f"Vocab : {MODEL_PREFIX}.vocab")