Translation
Transformers
PyTorch
English
Hindi
viuai
viutranslate
sarus-500m
nmt
english-to-hindi
hindi-to-english
indic
devanagari
bfloat16
zero-synthetic
Instructions to use ViuAI/ViuTranslate with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use ViuAI/ViuTranslate with Transformers:
# Use a pipeline as a high-level helper # Warning: Pipeline type "translation" is no longer supported in transformers v5. # You must load the model directly (see below) or downgrade to v4.x with: # pip install "transformers<5.0.0" from transformers import pipeline pipe = pipeline("translation", model="ViuAI/ViuTranslate")# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("ViuAI/ViuTranslate", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Delete inference.py with huggingface_hub
Browse files- inference.py +0 -128
inference.py
DELETED
|
@@ -1,128 +0,0 @@
|
|
| 1 |
-
# ==============================================================================
|
| 2 |
-
# ๐ ViuTranslate โ Interactive Translation CLI
|
| 3 |
-
# ==============================================================================
|
| 4 |
-
# Run: python inference.py
|
| 5 |
-
# Type any sentence in English or Hindi to get real-time neural translation.
|
| 6 |
-
# ==============================================================================
|
| 7 |
-
|
| 8 |
-
import os
|
| 9 |
-
import sys
|
| 10 |
-
import torch
|
| 11 |
-
from tokenizers import Tokenizer
|
| 12 |
-
from huggingface_hub import hf_hub_download
|
| 13 |
-
|
| 14 |
-
# UTF-8 encoding
|
| 15 |
-
if hasattr(sys.stdout, "reconfigure"):
|
| 16 |
-
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
| 17 |
-
|
| 18 |
-
cur_dir = os.path.dirname(os.path.abspath(__file__)) if "__file__" in locals() else os.getcwd()
|
| 19 |
-
if cur_dir not in sys.path:
|
| 20 |
-
sys.path.insert(0, cur_dir)
|
| 21 |
-
|
| 22 |
-
from model import ViuAI
|
| 23 |
-
from config import ViuAIConfig
|
| 24 |
-
|
| 25 |
-
REPO_ID = "ViuAI/ViuTranslate"
|
| 26 |
-
EOT_ID = 64002
|
| 27 |
-
|
| 28 |
-
def load_model():
|
| 29 |
-
print("=" * 75)
|
| 30 |
-
print(f"๐ Initializing ViuTranslate-500M from {REPO_ID}...")
|
| 31 |
-
print("=" * 75)
|
| 32 |
-
|
| 33 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 34 |
-
print(f"โข Hardware Device: {device.type.upper()}")
|
| 35 |
-
|
| 36 |
-
# Checkpoint candidates
|
| 37 |
-
ckpt_candidates = [
|
| 38 |
-
"viutranslate_final.pt",
|
| 39 |
-
"checkpoints/viutranslate_final.pt",
|
| 40 |
-
os.path.join(cur_dir, "viutranslate_final.pt")
|
| 41 |
-
]
|
| 42 |
-
ckpt_path = None
|
| 43 |
-
for c in ckpt_candidates:
|
| 44 |
-
if os.path.exists(c):
|
| 45 |
-
ckpt_path = c
|
| 46 |
-
break
|
| 47 |
-
|
| 48 |
-
if ckpt_path is None:
|
| 49 |
-
print(f"๐ฅ Downloading viutranslate_final.pt from {REPO_ID}...")
|
| 50 |
-
ckpt_path = hf_hub_download(repo_id=REPO_ID, filename="viutranslate_final.pt")
|
| 51 |
-
|
| 52 |
-
tok_candidates = [
|
| 53 |
-
"tokenizer.json",
|
| 54 |
-
os.path.join(cur_dir, "tokenizer.json")
|
| 55 |
-
]
|
| 56 |
-
tok_path = None
|
| 57 |
-
for tc in tok_candidates:
|
| 58 |
-
if os.path.exists(tc):
|
| 59 |
-
tok_path = tc
|
| 60 |
-
break
|
| 61 |
-
|
| 62 |
-
if tok_path is None:
|
| 63 |
-
print(f"๐ฅ Downloading tokenizer.json from {REPO_ID}...")
|
| 64 |
-
tok_path = hf_hub_download(repo_id=REPO_ID, filename="tokenizer.json")
|
| 65 |
-
|
| 66 |
-
tokenizer = Tokenizer.from_file(tok_path)
|
| 67 |
-
|
| 68 |
-
cfg = ViuAIConfig(vocab_size=64003, context_length=2048)
|
| 69 |
-
model = ViuAI(cfg).to(device)
|
| 70 |
-
|
| 71 |
-
state = torch.load(ckpt_path, map_location=device, weights_only=False)
|
| 72 |
-
weights = state.get("model_state_dict", state)
|
| 73 |
-
model.load_state_dict(weights, strict=False)
|
| 74 |
-
model.eval()
|
| 75 |
-
|
| 76 |
-
print("โ
ViuTranslate Engine loaded and ready for inference!\n")
|
| 77 |
-
return model, tokenizer, device
|
| 78 |
-
|
| 79 |
-
@torch.no_grad()
|
| 80 |
-
def translate(model, tokenizer, device, text: str, mode: str = "direct") -> str:
|
| 81 |
-
text = text.strip()
|
| 82 |
-
if mode == "direct":
|
| 83 |
-
prompt = f"<|user|>\n{text}<|endofturn|>\n<|assistant|>\n"
|
| 84 |
-
elif mode == "to_hi":
|
| 85 |
-
prompt = f"<|user|>\nTranslate to Hindi: '{text}'<|endofturn|>\n<|assistant|>\n"
|
| 86 |
-
elif mode == "to_en":
|
| 87 |
-
prompt = f"<|user|>\nTranslate to English: '{text}'<|endofturn|>\n<|assistant|>\n"
|
| 88 |
-
else:
|
| 89 |
-
prompt = f"<|user|>\n{text}<|endofturn|>\n<|assistant|>\n"
|
| 90 |
-
|
| 91 |
-
input_ids = torch.tensor([tokenizer.encode(prompt).ids], dtype=torch.long, device=device)
|
| 92 |
-
prompt_len = input_ids.shape[1]
|
| 93 |
-
|
| 94 |
-
out = model.generate(
|
| 95 |
-
input_ids,
|
| 96 |
-
max_new_tokens=150,
|
| 97 |
-
temperature=0.2,
|
| 98 |
-
top_p=0.9,
|
| 99 |
-
repetition_penalty=1.15,
|
| 100 |
-
eos_token_id=EOT_ID
|
| 101 |
-
)
|
| 102 |
-
gen_tokens = out[0][prompt_len:].tolist()
|
| 103 |
-
if EOT_ID in gen_tokens:
|
| 104 |
-
gen_tokens = gen_tokens[:gen_tokens.index(EOT_ID)]
|
| 105 |
-
|
| 106 |
-
return tokenizer.decode(gen_tokens).strip()
|
| 107 |
-
|
| 108 |
-
def interactive_loop():
|
| 109 |
-
model, tokenizer, device = load_model()
|
| 110 |
-
print("๐ก Enter text to translate (Google Translate style). Type 'exit' or 'quit' to stop.\n")
|
| 111 |
-
|
| 112 |
-
while True:
|
| 113 |
-
try:
|
| 114 |
-
inp = input("๐ [Input]: ").strip()
|
| 115 |
-
if not inp:
|
| 116 |
-
continue
|
| 117 |
-
if inp.lower() in ["exit", "quit", "q"]:
|
| 118 |
-
print("๐ Exiting ViuTranslate.")
|
| 119 |
-
break
|
| 120 |
-
|
| 121 |
-
out = translate(model, tokenizer, device, inp, mode="direct")
|
| 122 |
-
print(f"๐ [ViuTranslate]: {out}\n")
|
| 123 |
-
except (KeyboardInterrupt, EOFError):
|
| 124 |
-
print("\n๐ Exiting ViuTranslate.")
|
| 125 |
-
break
|
| 126 |
-
|
| 127 |
-
if __name__ == "__main__":
|
| 128 |
-
interactive_loop()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|