vijay399 commited on
Commit
cfbbe17
·
1 Parent(s): 46f9c87

Update src/paraphrase/Paraphrase.py

Browse files
Files changed (1) hide show
  1. 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
- exception = ""
20
- result_value = ""
21
-
22
 
23
- exception = ExceptionCustom.checkForException(requestValue, METHOD)
24
- if exception != "":
25
- return "", exception
26
 
27
- tokenized_sent_list = sent_tokenize(requestValue)
 
28
 
29
- for SENTENCE in tokenized_sent_list:
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
- for beam_output in beam_outputs:
50
- text_para = tokenizer.decode(beam_output, skip_special_tokens=True, clean_up_tokenization_spaces=True)
51
-
52
- if SENTENCE.lower().strip() != text_para.lower().strip():
53
- result_value += text_para + " "
54
- break
55
 
56
- return result_value, ""
 
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, ""