ar5entum commited on
Commit
298af31
·
verified ·
1 Parent(s): a7473b6

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +110 -5
README.md CHANGED
@@ -23,15 +23,120 @@ 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
 
 
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_dev_rom_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
+ "द एजुकेशन रिसर्चर इवैल्युएटेड द इफेक्टिवनेस ऑफ ऑनलाइन लर्निंग",
86
+ "यह अभिषेक जल, इक्षुरस, दुध, चावल का आटा, लाल चंदन, हल्दी, अष्टगंध, चंदन चुरा, चार कलश, केसर वृष्टि, आरती, सुगंधित कलश, महाशांतिधारा एवं महाअर्घ्य के साथ भगवान नेमिनाथ को समर्पित किया जाता है।",
87
+ "कुछ ने कहा ये चांद है कुछ ने कहा चेहरा तेरा"
88
+ ]
89
+ ground_truths = [
90
+ "the education researcher evaluated the effectiveness of online learning.",
91
+ "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.",
92
+ "kuch ne kaha ye chand hai kuch ne kaha chehra ter"
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: 1.6740131378173828
125
+ # ‾‾‾‾‾‾‾‾‾‾‾‾
126
+ # Input text: द एजुकेशन रिसर्चर इवैल्युएटेड द इफेक्टिवनेस ऑफ ऑनलाइन लर्निंग
127
+ # Prediction: the education researcher evaluated the inflation of online. Larning
128
+ # Ground Truth: the education researcher evaluated the effectiveness of online learning.
129
+ # ‾‾‾‾‾‾‾‾‾‾‾‾
130
+ # Input text: यह अभिषेक जल, इक्षुरस, दुध, चावल का आटा, लाल चंदन, हल्दी, अष्टगंध, चंदन चुरा, चार कलश, केसर वृष्टि, आरती, सुगंधित कलश, महाशांतिधारा एवं महाअर्घ्य के साथ भगवान नेमिनाथ को समर्पित किया जाता है।
131
+ # Prediction: yah abhishek jal, ikshuras, dudh, chaval ka aata, laal chandan, Haldi, asthagandh, chandan chura, char kalash, kesar vritti, Aarti, Sugandhit kalash, Mahashantidhara evam Maharghya ke saath bhagwan Nemith ko samarpit kiya jata hai.
132
+ # Ground Truth: 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.
133
+ # ‾‾‾‾‾‾‾‾‾‾‾‾
134
+ # Input text: कुछ ने कहा ये चांद है कुछ ने कहा चेहरा तेरा
135
+ # Prediction: kuchh ne kaha ye chand hai kuch ne kaha chehra tera
136
+ # Ground Truth: kuch ne kaha ye chand hai kuch ne kaha chehra ter
137
+ # {'bleu': 0.5596481750975065, 'precisions': [0.7910447761194029, 0.609375, 0.4918032786885246, 0.41379310344827586], 'brevity_penalty': 1.0, 'length_ratio': 1.0, 'translation_length': 67, 'reference_length': 67}
138
+
139
+ ```
140
 
141
  ## Training procedure
142