ar5entum commited on
Commit
72d1bb7
·
verified ·
1 Parent(s): b9cf533

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +108 -6
README.md CHANGED
@@ -23,17 +23,119 @@ It achieves the following results on the evaluation set:
23
 
24
  ## Model description
25
 
26
- More information needed
27
 
28
- ## Intended uses & limitations
 
 
 
 
29
 
30
- More information needed
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
- ## Training and evaluation data
 
 
 
 
 
 
 
33
 
34
- More information needed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
- ## Training procedure
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
  ### Training hyperparameters
39
 
 
23
 
24
  ## Model description
25
 
26
+ This model is trained on transliteration dataset of roman and devnagiri sentences. The objective of this experiment was to correctly transliterate sentences based on their context.
27
 
28
+ ## Inference and Evaluation
29
+ ```python
30
+ import torch
31
+ import evaluate
32
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
33
 
34
+ def batch_long_string(text):
35
+ batch = []
36
+ temp = []
37
+ count = 0
38
+ for word in text.split():
39
+ count+=len(word)
40
+ temp.append(word.strip())
41
+ if count > 40:
42
+ count = 0
43
+ batch.append(" ".join(temp).strip())
44
+ temp = []
45
+ if len(temp) > 0:
46
+ batch.append(" ".join(temp).strip())
47
+ return batch
48
 
49
+ class BartSmall():
50
+ def __init__(self, model_path = 'ar5entum/bart_rom_dev_tl', device = None):
51
+ self.tokenizer = AutoTokenizer.from_pretrained(model_path)
52
+ self.model = AutoModelForSeq2SeqLM.from_pretrained(model_path)
53
+ if not device:
54
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
55
+ self.device = device
56
+ self.model.to(device)
57
 
58
+ def predict(self, input_text):
59
+ inputs = self.tokenizer(input_text, return_tensors="pt", max_length=512, truncation=True).to(self.device)
60
+ pred_ids = self.model.generate(inputs.input_ids, max_length=512, num_beams=4, early_stopping=True)
61
+ prediction = self.tokenizer.decode(pred_ids[0], skip_special_tokens=True)
62
+ return prediction
63
+
64
+ def predict_batch(self, input_texts, batch_size=32):
65
+ all_predictions = []
66
+ for i in range(0, len(input_texts), batch_size):
67
+ batch_texts = input_texts[i:i+batch_size]
68
+ inputs = self.tokenizer(batch_texts, return_tensors="pt", max_length=512,
69
+ truncation=True, padding=True).to(self.device)
70
+
71
+ with torch.no_grad():
72
+ pred_ids = self.model.generate(inputs.input_ids,
73
+ max_length=512,
74
+ num_beams=4,
75
+ early_stopping=True)
76
+
77
+ predictions = self.tokenizer.batch_decode(pred_ids, skip_special_tokens=True)
78
+ all_predictions.extend(predictions)
79
 
80
+ return all_predictions
81
+
82
+ model = BartSmall(device='cuda')
83
+
84
+ input_texts = [
85
+ "the education researcher evaluated the effectiveness of online learning.",
86
+ "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.",
87
+ "kuch ne kaha ye chand hai kuch ne kaha chehra ter"
88
+ ]
89
+ ground_truths = [
90
+ "द एजुकेशन रिसर्चर इवैल्युएटेड द इफेक्टिवनेस ऑफ ऑनलाइन लर्निंग",
91
+ "यह अभिषेक जल, इक्षुरस, दुध, चावल का आटा, लाल चंदन, हल्दी, अष्टगंध, चंदन चुरा, चार कलश, केसर वृष्टि, आरती, सुगंधित कलश, महाशांतिधारा एवं महाअर्घ्य के साथ भगवान नेमिनाथ को समर्पित किया जाता है।",
92
+ "कुछ ने कहा ये चांद है कुछ ने कहा चेहरा तेरा"
93
+ ]
94
+ import time
95
+ start = time.time()
96
+
97
+ def batch_long_string(text):
98
+ batch = []
99
+ temp = []
100
+ count = 0
101
+ for word in text.split():
102
+ count+=len(word)
103
+ temp.append(word.strip())
104
+ if count > 40:
105
+ count = 0
106
+ batch.append(" ".join(temp).strip())
107
+ temp = []
108
+ if len(temp) > 0:
109
+ batch.append(" ".join(temp).strip())
110
+ return batch
111
+
112
+ predictions = [" ".join([" ".join(model.predict_batch(batch, batch_size=len(batch))) for batch in batch_long_string(text)]) for text in input_texts]
113
+ end = time.time()
114
+ print("TIME: ", end-start)
115
+ for i in range(len(input_texts)):
116
+ print("‾‾‾‾‾‾‾‾‾‾‾‾")
117
+ print("Input text:\t", input_texts[i])
118
+ print("Prediction:\t", predictions[i])
119
+ print("Ground Truth:\t", ground_truths[i])
120
+ bleu = evaluate.load("bleu")
121
+ results = bleu.compute(predictions=predictions, references=ground_truths)
122
+ print(results)
123
+
124
+ # TIME: 9.683340787887573
125
+ # ‾‾‾‾‾‾‾‾‾‾‾‾
126
+ # Input text: the education researcher evaluated the effectiveness of online learning.
127
+ # Prediction: द एजुकेशन रिसर्चर इवैल्युएट्स द इफेक्टिं��� ओफ ऑनाइनल लर्निंग
128
+ # Ground Truth: द एजुकेशन रिसर्चर इवैल्युएटेड द इफेक्टिवनेस ऑफ ऑनलाइन लर्निंग
129
+ # ‾‾‾‾‾‾‾‾‾‾‾‾
130
+ # 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.
131
+ # Prediction: यह अभिषेक जल, इक्षुरस, दुध, चावल का आता, लाल चन्दन, हालडी, अष्टगंध, चन्दन चुरा, चार कलाश, केसर वृष्टि, आर्ती, सुगंधित कलाश, महासंतिधारा एवं महार्घ्य के साथ भगवान नेमीनाथ को समर्पित किया जाता है।
132
+ # Ground Truth: यह अभिषेक जल, इक्षुरस, दुध, चावल का आटा, लाल चंदन, हल्दी, अष्टगंध, चंदन चुरा, चार कलश, केसर वृष्टि, आरती, सुगंधित कलश, महाशांतिधारा एवं महाअर्घ्य के साथ भगवान नेमिनाथ को समर्पित किया जाता है।
133
+ # ‾‾‾‾‾‾‾‾‾‾‾‾
134
+ # Input text: kuch ne kaha ye chand hai kuch ne kaha chehra ter
135
+ # Prediction: कुछ ने कहा ये चाँद है कुछ ने कहा चेहरा तेर
136
+ # Ground Truth: कुछ ने कहा ये चांद है कुछ ने कहा चेहरा तेरा
137
+ # {'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}
138
+ ```
139
 
140
  ### Training hyperparameters
141