Views
No views yet
1import torch
2from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
3from IndicTransToolkit import IndicProcessor # Install IndicTransToolkit from https://github.com/VarunGumma/IndicTransToolkit
4
5device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
6src_lang, tgt_lang = "eng_Latn", "ben_Beng" # Use the BCP-47 language codes used by the FLORES-200 dataset
7tokenizer = AutoTokenizer.from_pretrained("ai4bharat/indictrans2-en-indic-1B", trust_remote_code=True) # Use IndicTrans2 tokenizer to enable their custom tokenization script to be run
8model = AutoModelForSeq2SeqLM.from_pretrained(
9 "law-ai/InLegalTrans-En2Indic-1B",
10 trust_remote_code=True,
11 attn_implementation="eager",
12 low_cpu_mem_usage=True,
13).to(device)
14ip = IndicProcessor(inference=True)
15
16input_sentences = [
17 "(7) Any such allowance for the maintenance and expenses for proceeding shall be payable from the date of the order, or, if so ordered, from the date of the application for maintenance or expenses of proceeding, as the case may be.",
18 "(2) Where it appears to the Tribunal that, in consequence of any decision of a competent Civil Court, any order made under section 9 should be cancelled or varied, it shall cancel the order or, as the case may be, vary the same accordingly.",
19]
20
21batch = ip.preprocess_batch(input_sentences, src_lang=src_lang, tgt_lang=tgt_lang)
22
23input_text_encoding = tokenizer(
24 batch,
25 max_length=256,
26 truncation=True,
27 padding="longest",
28 return_tensors="pt",
29 return_attention_mask=True,
30).to(device)
31
32generated_tokens = model.generate(
33 **input_text_encoding,
34 max_length=256,
35 do_sample=True,
36 num_beams=4,
37 num_return_sequences=1,
38 early_stopping=False,
39 use_cache=True,
40)
41
42with tokenizer.as_target_tokenizer():
43 generated_tokens = tokenizer.batch_decode(
44 generated_tokens.detach().cpu().tolist(),
45 skip_special_tokens=True,
46 clean_up_tokenization_spaces=True,
47 )
48
49translations = ip.postprocess_batch(generated_tokens, lang=tgt_lang)
50
51for input_sentence, translation in zip(input_sentences, translations):
52 print(f"Sentence in {src_lang} language: {input_sentence}")
53 print(f"Translated Sentence in {tgt_lang} language: {translation}") | EN-to-IN | Model | BLEU | GLEU | chrF++ |
|---|---|---|---|---|
| EN-to-BN | IndicTrans2 | 25.4 | 28.8 | 53.7 |
| InLegalTrans | 45.8 | 47.6 | 70.9 | |
| EN-to-HI | IndicTrans2 | 41.0 | 42.5 | 59.9 |
| InLegalTrans | 56.9 | 57.1 | 73.8 | |
| EN-to-MR | IndicTrans2 | 25.2 | 28.7 | 55.4 |
| InLegalTrans | 44.4 | 46.0 | 68.9 | |
| EN-to-TA | IndicTrans2 | 32.8 | 35.3 | 62.3 |
| InLegalTrans | 40.0 | 42.5 | 69.9 | |
| EN-to-TE | IndicTrans2 | 10.7 | 14.2 | 37.9 |
| InLegalTrans | 31.3 | 31.6 | 58.5 | |
| EN-to-ML | IndicTrans2 | 21.9 | 25.8 | 52.9 |
| InLegalTrans | 37.4 | 40.3 | 69.7 | |
| EN-to-PA | IndicTrans2 | 27.8 | 31.6 | 51.5 |
| InLegalTrans | 44.3 | 45.6 | 65.5 | |
| EN-to-GU | IndicTrans2 | 27.5 | 31.1 | 55.7 |
| InLegalTrans | 42.8 | 45.2 | 68.8 | |
| EN-to-OR | IndicTrans2 | 06.6 | 12.6 | 37.1 |
| InLegalTrans | 14.2 | 19.9 | 47.5 |
@article{mahapatra2024milpacnovelbenchmarkevaluating,
title = {MILPaC: A Novel Benchmark for Evaluating Translation of Legal Text to Indian Languages},
author = {Sayan Mahapatra and Debtanu Datta and Shubham Soni and Adrijit Goswami and Saptarshi Ghosh},
year = {2024},
journal = {ACM Trans. Asian Low-Resour. Lang. Inf. Process.},
publisher = {Association for Computing Machinery},
}