Views
No views yet
1import ctranslate2, transformers
2
3tok = transformers.AutoTokenizer.from_pretrained("JaydeepGupta/nllb-ct2-int8")
4translator = ctranslate2.Translator("JaydeepGupta/nllb-ct2-int8", compute_type="int8")
5src_lang, tgt_lang = "eng_Latn", "deu_Latn" # English to German
6tok.src_lang = src_lang
7
8src = tok.convert_ids_to_tokens(tok.encode(" The algebraic sum of all voltage drops around a closed loop is zero, crucial for analyzing voltage regulation and the impact of switching actions."))
9tgt_prefix = [tgt_lang]
10
11out = translator.translate_batch([src], target_prefix=[tgt_prefix])
12print(tok.decode(tok.convert_tokens_to_ids(out[0].hypotheses[0][1:])))1import re
2
3def split_sentences(text):
4 parts = re.split(r'(?<=[.!?])\s+', text.strip())
5 return [p for p in parts if p]
6
7src_text_hf = "It is important that the library is getting installed into a directory that is on the CMAKE_PREFIX_PATH, [Musik] otherwise you can install to a custom directory, e.g.:"
8sentences = split_sentences(src_text_hf)
9
10sources = [tok_hf.convert_ids_to_tokens(tok_hf.encode(s)) for s in sentences]
11target_prefixes = [[tgt_lang_hf] for _ in sentences]
12
13results = translator_hf.translate_batch(sources, target_prefix=target_prefixes)
14
15translated = " ".join(
16 tok_hf.decode(tok_hf.convert_tokens_to_ids(r.hypotheses[0][1:]))
17 for r in results
18)
19print("Translated:", translated)