Views
No views yet
ctranslate2 + sentencepiece are needed1import ctranslate2
2import sentencepiece as spm
3from huggingface_hub import snapshot_download
4
5path = snapshot_download("Prukario/opus-mt-es-en-ct2-int8")
6translator = ctranslate2.Translator(path, device="cpu", compute_type="int8")
7sp_src = spm.SentencePieceProcessor(model_file=f"{path}/source.spm")
8sp_tgt = spm.SentencePieceProcessor(model_file=f"{path}/target.spm")
9
10def translate(text: str) -> str:
11 tokens = sp_src.encode(text, out_type=str) + ["</s>"] # Marian needs EOS
12 out = translator.translate_batch([tokens], beam_size=2)[0].hypotheses[0]
13 if "</s>" in out:
14 out = out[: out.index("</s>")]
15 return sp_tgt.decode(out)
16
17print(translate("Hola, ¿cómo estás?"))
18# -> "Hello, how are you?"1pip install torch transformers ctranslate2 sentencepiece
2ct2-transformers-converter \
3 --model Helsinki-NLP/opus-mt-es-en \
4 --output_dir opus-mt-es-en-ct2-int8 \
5 --quantization int8 \
6 --copy_files source.spm target.spm tokenizer_config.json1@inproceedings{TiedemannThottingal:EAMT2020,
2 author = {J{\"o}rg Tiedemann and Santhosh Thottingal},
3 title = {{OPUS-MT} — {B}uilding open translation services for the {W}orld},
4 booktitle = {Proceedings of the 22nd Annual Conference of the European Association for Machine Translation (EAMT)},
5 year = {2020},
6 address = {Lisbon, Portugal}
7}