-
Notifications
You must be signed in to change notification settings - Fork 0
/
predict.py
46 lines (40 loc) · 1.58 KB
/
predict.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from classes.main import Main
import csv
import pandas as pd
from tqdm import tqdm
tqdm.pandas()
import torch
class Inference(Main):
def __init__(self):
super().__init__()
self.tokenizer.src_lang = self.cfg.params["src"]
def translate(self, text):
"""
Taking an input text and translate it into the target language
"""
inputs = self.tokenizer(text, return_tensors='pt')
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
input_ids = inputs.input_ids.to(device)
attention_mask = inputs.attention_mask.to(device)
output = self.model.generate(input_ids,
use_cache=True,
max_length=100,
attention_mask=attention_mask,
forced_bos_token_id=self.tokenizer.lang_code_to_id[self.cfg.params["tgt"]]
)
return self.tokenizer.decode(output[0], skip_special_tokens=True)
def main():
# Make an instance from the class
I = Inference()
# Read the path from config.yaml for prediction
data = pd.read_csv(I.cfg.dataset["predict_path"],
header=None, quoting=csv.QUOTE_NONE, sep='\t', names=['src', 'ref']).dropna()
print(data)
# Apply the translate function to each src's tuple and
# Add the translated text to a new column named `mt``
data['mt'] = data['src'].progress_apply(lambda x: I.translate(x))
print(data)
# Save the translation in a csv file
data.to_csv(I.cfg.dataset["predict_path"]+"-trans.csv", index=False, quoting=csv.QUOTE_NONE, sep='\t')
if __name__ == '__main__':
main()