Upload 5 files
Browse files- AnViMt-V3.zip +3 -0
- decoder_model.onnx +3 -0
- encoder_model.onnx +3 -0
- main.py +224 -0
- spm.model +3 -0
AnViMt-V3.zip
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ad3b87cca4b2bd9ac3b5251c17cf9d97d4883955a15014ce4151957766c565fc
|
| 3 |
+
size 346930635
|
decoder_model.onnx
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:33850c6cb7264f3f8a112154c65eb962b48a0d28884cfee83a8db07e5d17ffdb
|
| 3 |
+
size 232873752
|
encoder_model.onnx
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:591fdcb6a8cc64a04a56fd3657f6ae06761c950135af4bff9d4e0efb3f064212
|
| 3 |
+
size 141826006
|
main.py
ADDED
|
@@ -0,0 +1,224 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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| 7 |
+
# =========================
|
| 8 |
+
# PATH
|
| 9 |
+
# =========================
|
| 10 |
+
|
| 11 |
+
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 12 |
+
|
| 13 |
+
ENCODER_PATH = os.path.join(BASE_DIR, "encoder_model.onnx")
|
| 14 |
+
DECODER_PATH = os.path.join(BASE_DIR, "decoder_model.onnx")
|
| 15 |
+
SPM_PATH = os.path.join(BASE_DIR, "spm.model")
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
# =========================
|
| 19 |
+
# SETTINGS
|
| 20 |
+
# =========================
|
| 21 |
+
|
| 22 |
+
MAX_INPUT_LEN = 256
|
| 23 |
+
MAX_OUTPUT_LEN = 256
|
| 24 |
+
|
| 25 |
+
PAD_ID = 0
|
| 26 |
+
UNK_ID = 1
|
| 27 |
+
BOS_ID = 2
|
| 28 |
+
EOS_ID = 3
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
# =========================
|
| 32 |
+
# CHECK FILES
|
| 33 |
+
# =========================
|
| 34 |
+
|
| 35 |
+
for path in [ENCODER_PATH, DECODER_PATH, SPM_PATH]:
|
| 36 |
+
if not os.path.exists(path):
|
| 37 |
+
raise FileNotFoundError(f"Không tìm thấy file: {path}")
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
# =========================
|
| 41 |
+
# LOAD TOKENIZER
|
| 42 |
+
# =========================
|
| 43 |
+
|
| 44 |
+
print("Đang load tokenizer...")
|
| 45 |
+
|
| 46 |
+
sp = spm.SentencePieceProcessor()
|
| 47 |
+
sp.load(SPM_PATH)
|
| 48 |
+
|
| 49 |
+
print(
|
| 50 |
+
f"Tokenizer: vocab={sp.get_piece_size()} | "
|
| 51 |
+
f"PAD={sp.pad_id()} BOS={sp.bos_id()} EOS={sp.eos_id()}"
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
# =========================
|
| 56 |
+
# ONNX RUNTIME
|
| 57 |
+
# =========================
|
| 58 |
+
|
| 59 |
+
print("Đang load ONNX model...")
|
| 60 |
+
|
| 61 |
+
providers = ["CPUExecutionProvider"]
|
| 62 |
+
|
| 63 |
+
encoder_session = ort.InferenceSession(
|
| 64 |
+
ENCODER_PATH,
|
| 65 |
+
providers=providers
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
decoder_session = ort.InferenceSession(
|
| 69 |
+
DECODER_PATH,
|
| 70 |
+
providers=providers
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
# =========================
|
| 75 |
+
# SHOW MODEL INFO
|
| 76 |
+
# =========================
|
| 77 |
+
|
| 78 |
+
print("\nEncoder inputs:")
|
| 79 |
+
for x in encoder_session.get_inputs():
|
| 80 |
+
print(" ", x.name, x.shape, x.type)
|
| 81 |
+
|
| 82 |
+
print("\nDecoder inputs:")
|
| 83 |
+
for x in decoder_session.get_inputs():
|
| 84 |
+
print(" ", x.name, x.shape, x.type)
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
# =========================
|
| 88 |
+
# TRANSLATE
|
| 89 |
+
# =========================
|
| 90 |
+
|
| 91 |
+
def translate(text):
|
| 92 |
+
text = text.strip()
|
| 93 |
+
|
| 94 |
+
if not text:
|
| 95 |
+
return ""
|
| 96 |
+
|
| 97 |
+
# ---------------------------------
|
| 98 |
+
# 1. ENCODE INPUT
|
| 99 |
+
# ---------------------------------
|
| 100 |
+
|
| 101 |
+
src_ids = sp.encode(text, out_type=int)
|
| 102 |
+
|
| 103 |
+
# Giới hạn context
|
| 104 |
+
src_ids = src_ids[:MAX_INPUT_LEN - 2]
|
| 105 |
+
|
| 106 |
+
# BART format
|
| 107 |
+
src_ids = [BOS_ID] + src_ids + [EOS_ID]
|
| 108 |
+
|
| 109 |
+
input_ids = np.array(
|
| 110 |
+
[src_ids],
|
| 111 |
+
dtype=np.int64
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
attention_mask = np.ones_like(
|
| 115 |
+
input_ids,
|
| 116 |
+
dtype=np.int64
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
# ---------------------------------
|
| 120 |
+
# 2. ENCODER
|
| 121 |
+
# ---------------------------------
|
| 122 |
+
|
| 123 |
+
encoder_outputs = encoder_session.run(
|
| 124 |
+
None,
|
| 125 |
+
{
|
| 126 |
+
"input_ids": input_ids,
|
| 127 |
+
"attention_mask": attention_mask
|
| 128 |
+
}
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
encoder_hidden_states = encoder_outputs[0]
|
| 132 |
+
|
| 133 |
+
# ---------------------------------
|
| 134 |
+
# 3. DECODER GREEDY
|
| 135 |
+
# ---------------------------------
|
| 136 |
+
|
| 137 |
+
generated = [BOS_ID]
|
| 138 |
+
|
| 139 |
+
for _ in range(MAX_OUTPUT_LEN):
|
| 140 |
+
|
| 141 |
+
decoder_input_ids = np.array(
|
| 142 |
+
[generated],
|
| 143 |
+
dtype=np.int64
|
| 144 |
+
)
|
| 145 |
+
|
| 146 |
+
decoder_inputs = {}
|
| 147 |
+
|
| 148 |
+
# Tự map input để tránh lệch tên
|
| 149 |
+
for inp in decoder_session.get_inputs():
|
| 150 |
+
|
| 151 |
+
name = inp.name
|
| 152 |
+
|
| 153 |
+
if name == "input_ids":
|
| 154 |
+
decoder_inputs[name] = decoder_input_ids
|
| 155 |
+
|
| 156 |
+
elif "encoder_hidden_states" in name:
|
| 157 |
+
decoder_inputs[name] = encoder_hidden_states
|
| 158 |
+
|
| 159 |
+
elif "encoder_attention_mask" in name:
|
| 160 |
+
decoder_inputs[name] = attention_mask
|
| 161 |
+
|
| 162 |
+
decoder_outputs = decoder_session.run(
|
| 163 |
+
None,
|
| 164 |
+
decoder_inputs
|
| 165 |
+
)
|
| 166 |
+
|
| 167 |
+
# Output đầu tiên là logits
|
| 168 |
+
logits = decoder_outputs[0]
|
| 169 |
+
|
| 170 |
+
# Lấy token cuối
|
| 171 |
+
next_token_logits = logits[0, -1, :]
|
| 172 |
+
|
| 173 |
+
next_token = int(
|
| 174 |
+
np.argmax(next_token_logits)
|
| 175 |
+
)
|
| 176 |
+
|
| 177 |
+
generated.append(next_token)
|
| 178 |
+
|
| 179 |
+
if next_token == EOS_ID:
|
| 180 |
+
break
|
| 181 |
+
|
| 182 |
+
# ---------------------------------
|
| 183 |
+
# 4. DECODE
|
| 184 |
+
# ---------------------------------
|
| 185 |
+
|
| 186 |
+
output_ids = generated[1:]
|
| 187 |
+
|
| 188 |
+
if EOS_ID in output_ids:
|
| 189 |
+
output_ids = output_ids[
|
| 190 |
+
:output_ids.index(EOS_ID)
|
| 191 |
+
]
|
| 192 |
+
|
| 193 |
+
result = sp.decode(output_ids)
|
| 194 |
+
|
| 195 |
+
return result
|
| 196 |
+
|
| 197 |
+
|
| 198 |
+
# =========================
|
| 199 |
+
# INTERACTIVE CLI
|
| 200 |
+
# =========================
|
| 201 |
+
|
| 202 |
+
print("\nEN → VI translator v2 ready")
|
| 203 |
+
print("Type 'exit' để thoát\n")
|
| 204 |
+
|
| 205 |
+
while True:
|
| 206 |
+
|
| 207 |
+
try:
|
| 208 |
+
text = input("EN > ").strip()
|
| 209 |
+
|
| 210 |
+
except (KeyboardInterrupt, EOFError):
|
| 211 |
+
print("\nBye")
|
| 212 |
+
break
|
| 213 |
+
|
| 214 |
+
if text.lower() in ["exit", "quit", "q"]:
|
| 215 |
+
print("Bye")
|
| 216 |
+
break
|
| 217 |
+
|
| 218 |
+
if not text:
|
| 219 |
+
continue
|
| 220 |
+
|
| 221 |
+
result = translate(text)
|
| 222 |
+
|
| 223 |
+
print("VI >", result)
|
| 224 |
+
print()
|
spm.model
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a57bcef944e5819febc36b85d5a3f5d7350dc13a3fd6a04c07fc58dcfaa4d0e6
|
| 3 |
+
size 769430
|