1import torch
2import evaluate
3from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
4
5def batch_long_string(text):
6 batch = []
7 temp = []
8 count = 0
9 for word in text.split():
10 count+=len(word)
11 temp.append(word.strip())
12 if count > 40:
13 count = 0
14 batch.append(" ".join(temp).strip())
15 temp = []
16 if len(temp) > 0:
17 batch.append(" ".join(temp).strip())
18 return batch
19
20class BartSmall():
21 def __init__(self, model_path = 'ar5entum/bart_rom_dev_tl', device = None):
22 self.tokenizer = AutoTokenizer.from_pretrained(model_path)
23 self.model = AutoModelForSeq2SeqLM.from_pretrained(model_path)
24 if not device:
25 device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
26 self.device = device
27 self.model.to(device)
28
29 def predict(self, input_text):
30 inputs = self.tokenizer(input_text, return_tensors="pt", max_length=512, truncation=True).to(self.device)
31 pred_ids = self.model.generate(inputs.input_ids, max_length=512, num_beams=4, early_stopping=True)
32 prediction = self.tokenizer.decode(pred_ids[0], skip_special_tokens=True)
33 return prediction
34
35 def predict_batch(self, input_texts, batch_size=32):
36 all_predictions = []
37 for i in range(0, len(input_texts), batch_size):
38 batch_texts = input_texts[i:i+batch_size]
39 inputs = self.tokenizer(batch_texts, return_tensors="pt", max_length=512,
40 truncation=True, padding=True).to(self.device)
41
42 with torch.no_grad():
43 pred_ids = self.model.generate(inputs.input_ids,
44 max_length=512,
45 num_beams=4,
46 early_stopping=True)
47
48 predictions = self.tokenizer.batch_decode(pred_ids, skip_special_tokens=True)
49 all_predictions.extend(predictions)
50
51 return all_predictions
52
53model = BartSmall(device='cuda')
54
55input_texts = [
56 "the education researcher evaluated the effectiveness of online learning.",
57 "yah abhishek jal, ikshuras, dudh, chaval ka ataa, laal chandan, haldi, ashtagandh, chandan chura, char kalash, kesar vrishti, aarti, sugandhit kalash, mahashantidhara evam mahaarghya ke saath bhagvan Neminath ko samarpit kiya jata hai.",
58 "kuch ne kaha ye chand hai kuch ne kaha chehra ter"
59 ]
60ground_truths = [
61 "द एजुकेशन रिसर्चर इवैल्युएटेड द इफेक्टिवनेस ऑफ ऑनलाइन लर्निंग",
62 "यह अभिषेक जल, इक्षुरस, दुध, चावल का आटा, लाल चंदन, हल्दी, अष्टगंध, चंदन चुरा, चार कलश, केसर वृष्टि, आरती, सुगंधित कलश, महाशांतिधारा एवं महाअर्घ्य के साथ भगवान नेमिनाथ को समर्पित किया जाता है।",
63 "कुछ ने कहा ये चांद है कुछ ने कहा चेहरा तेरा"
64 ]
65import time
66start = time.time()
67
68def batch_long_string(text):
69 batch = []
70 temp = []
71 count = 0
72 for word in text.split():
73 count+=len(word)
74 temp.append(word.strip())
75 if count > 40:
76 count = 0
77 batch.append(" ".join(temp).strip())
78 temp = []
79 if len(temp) > 0:
80 batch.append(" ".join(temp).strip())
81 return batch
82
83predictions = [" ".join([" ".join(model.predict_batch(batch, batch_size=len(batch))) for batch in batch_long_string(text)]) for text in input_texts]
84end = time.time()
85print("TIME: ", end-start)
86for i in range(len(input_texts)):
87 print("‾‾‾‾‾‾‾‾‾‾‾‾")
88 print("Input text:\t", input_texts[i])
89 print("Prediction:\t", predictions[i])
90 print("Ground Truth:\t", ground_truths[i])
91bleu = evaluate.load("bleu")
92results = bleu.compute(predictions=predictions, references=ground_truths)
93print(results)
94
95# TIME: 9.683340787887573
96# ‾‾‾‾‾‾‾‾‾‾‾‾
97# Input text: the education researcher evaluated the effectiveness of online learning.
98# Prediction: द एजुकेशन रिसर्चर इवैल्युएट्स द इफेक्टिंग ओफ ऑनाइनल लर्निंग
99# Ground Truth: द एजुकेशन रिसर्चर इवैल्युएटेड द इफेक्टिवनेस ऑफ ऑनलाइन लर्निंग
100# ‾‾‾‾‾‾‾‾‾‾‾‾
101# Input text: yah abhishek jal, ikshuras, dudh, chaval ka ataa, laal chandan, haldi, ashtagandh, chandan chura, char kalash, kesar vrishti, aarti, sugandhit kalash, mahashantidhara evam mahaarghya ke saath bhagvan Neminath ko samarpit kiya jata hai.
102# Prediction: यह अभिषेक जल, इक्षुरस, दुध, चावल का आता, लाल चन्दन, हालडी, अष्टगंध, चन्दन चुरा, चार कलाश, केसर वृष्टि, आर्ती, सुगंधित कलाश, महासंतिधारा एवं महार्घ्य के साथ भगवान नेमीनाथ को समर्पित किया जाता है।
103# Ground Truth: यह अभिषेक जल, इक्षुरस, दुध, चावल का आटा, लाल चंदन, हल्दी, अष्टगंध, चंदन चुरा, चार कलश, केसर वृष्टि, आरती, सुगंधित कलश, महाशांतिधारा एवं महाअर्घ्य के साथ भगवान नेमिनाथ को समर्पित किया जाता है।
104# ‾‾‾‾‾‾‾‾‾‾‾‾
105# Input text: kuch ne kaha ye chand hai kuch ne kaha chehra ter
106# Prediction: कुछ ने कहा ये चाँद है कुछ ने कहा चेहरा तेर
107# Ground Truth: कुछ ने कहा ये चांद है कुछ ने कहा चेहरा तेरा
108# {'bleu': 0.43170068926336663, 'precisions': [0.7538461538461538, 0.532258064516129, 0.3728813559322034, 0.23214285714285715], 'brevity_penalty': 1.0, 'length_ratio': 1.0, 'translation_length': 65, 'reference_length': 65}