Update src/paraphrase/Paraphrase.py
Browse files- src/paraphrase/Paraphrase.py +17 -23
src/paraphrase/Paraphrase.py
CHANGED
@@ -3,35 +3,29 @@ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
|
3 |
import torch
|
4 |
import src.exception.Exception.Exception as ExceptionCustom
|
5 |
|
6 |
-
|
7 |
METHOD = "PARAPHRASE"
|
8 |
|
9 |
-
|
10 |
tokenizer = AutoTokenizer.from_pretrained("BlackKakapo/flan-t5-base-paraphrase-ro")
|
11 |
model = AutoModelForSeq2SeqLM.from_pretrained("BlackKakapo/flan-t5-base-paraphrase-ro")
|
12 |
|
13 |
# device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
14 |
# model.to(device)
|
15 |
|
16 |
-
|
17 |
def paraphraseParaphraseMethod(requestValue : str):
|
|
|
|
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
|
23 |
-
|
24 |
-
if exception != "":
|
25 |
-
return "", exception
|
26 |
|
27 |
-
|
|
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
text = "paraphrase: " + SENTENCE
|
32 |
-
|
33 |
-
encoding = tokenizer.encode_plus(text, pad_to_max_length=True, return_tensors="pt")
|
34 |
-
input_ids, attention_masks = encoding["input_ids"], encoding["attention_mask"]
|
35 |
|
36 |
beam_outputs = model.generate(
|
37 |
input_ids=input_ids,
|
@@ -46,11 +40,11 @@ def paraphraseParaphraseMethod(requestValue : str):
|
|
46 |
num_beams=1
|
47 |
)
|
48 |
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
|
56 |
-
|
|
|
3 |
import torch
|
4 |
import src.exception.Exception.Exception as ExceptionCustom
|
5 |
|
|
|
6 |
METHOD = "PARAPHRASE"
|
7 |
|
|
|
8 |
tokenizer = AutoTokenizer.from_pretrained("BlackKakapo/flan-t5-base-paraphrase-ro")
|
9 |
model = AutoModelForSeq2SeqLM.from_pretrained("BlackKakapo/flan-t5-base-paraphrase-ro")
|
10 |
|
11 |
# device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
12 |
# model.to(device)
|
13 |
|
|
|
14 |
def paraphraseParaphraseMethod(requestValue : str):
|
15 |
+
exception = ""
|
16 |
+
result_value = ""
|
17 |
|
18 |
+
exception = ExceptionCustom.checkForException(requestValue, METHOD)
|
19 |
+
if exception != "":
|
20 |
+
return "", exception
|
21 |
|
22 |
+
tokenized_sent_list = sent_tokenize(requestValue)
|
|
|
|
|
23 |
|
24 |
+
for SENTENCE in tokenized_sent_list:
|
25 |
+
text = "paraphrase: " + SENTENCE
|
26 |
|
27 |
+
encoding = tokenizer.encode_plus(text, pad_to_max_length=True, return_tensors="pt")
|
28 |
+
input_ids, attention_masks = encoding["input_ids"], encoding["attention_mask"]
|
|
|
|
|
|
|
|
|
29 |
|
30 |
beam_outputs = model.generate(
|
31 |
input_ids=input_ids,
|
|
|
40 |
num_beams=1
|
41 |
)
|
42 |
|
43 |
+
for beam_output in beam_outputs:
|
44 |
+
text_para = tokenizer.decode(beam_output, skip_special_tokens=True, clean_up_tokenization_spaces=True)
|
45 |
+
|
46 |
+
if SENTENCE.lower().strip() != text_para.lower().strip():
|
47 |
+
result_value += text_para + " "
|
48 |
+
break
|
49 |
|
50 |
+
return result_value, ""
|