Compared to large cloud APIs and heavy desktop models, this model is built for fast local CPU execution:
ठूला क्लाउड API वा भारी डेस्कटप मोडेलहरूको तुलनामा, यो मोडेल मोबाइलको सामान्य CPU मा पनि अत्यन्तै द्रुत गतिमा चल्छ:
1import numpy as np
2import sentencepiece as spm
3import onnxruntime as ort
4
5# 1. Load Tokenizer & ONNX Model
6sp = spm.SentencePieceProcessor(model_file="tokenizer.model")
7session = ort.InferenceSession("model.onnx")
8
9def pad_sequence(seq, max_len=128, pad_val=0):
10 if len(seq) > max_len:
11 return seq[:max_len]
12 return seq + [pad_val] * (max_len - len(seq))
13
14def translate(text, direction="ja2ne"):
15 # Assign tags and branch IDs
16 if direction == "ja2ne":
17 tag, tid = "<JA2NE>", 0
18 elif direction == "ne2ja":
19 tag, tid = "<NE2JA>", 1
20 else:
21 raise ValueError("Invalid direction. Choose 'ja2ne' or 'ne2ja'.")
22
23 # Encode prompt
24 prompt = f"{tag} {text}"
25 tokens = sp.encode(prompt)
26 x_padded = pad_sequence(tokens, max_len=128)
27
28 x_in = np.array([x_padded], dtype=np.int32)
29 tid_in = np.array([tid], dtype=np.int32)
30
31 # Autoregressive generation
32 tgt_tokens = []
33 for _ in range(128):
34 dec_input = [sp.bos_id()] + tgt_tokens
35 dec_padded = pad_sequence(dec_input, max_len=128)
36 dec_in = np.array([dec_padded], dtype=np.int32)
37
38 # Run inference using ONNX session
39 outputs = session.run(None, {
40 "x": x_in,
41 "tgt": dec_in,
42 "tid": tid_in
43 })
44 logits = outputs[0]
45 current_pos = len(dec_input) - 1
46
47 next_token = int(np.argmax(logits[0, current_pos, :]))
48 if next_token == sp.eos_id():
49 break
50 tgt_tokens.append(next_token)
51
52 return sp.decode(tgt_tokens)
53
54# Try Bi-directional Translation
55print("JA to NE:", translate("株式取引は、リスクの高い投資です。", direction="ja2ne"))
56print("NE to JA:", translate("मलाई भोक लाग्यो।", direction="ne2ja"))