Views
No views yet
1from transformers import T5ForConditionalGeneration, T5Tokenizer
2
3tokenizer = T5Tokenizer.from_pretrained("hetpandya/t5-small-quora")
4model = T5ForConditionalGeneration.from_pretrained("hetpandya/t5-small-quora")
5
6def get_paraphrases(sentence, prefix="paraphrase: ", n_predictions=5, top_k=120, max_length=256,device="cpu"):
7 text = prefix + sentence + " </s>"
8 encoding = tokenizer.encode_plus(
9 text, pad_to_max_length=True, return_tensors="pt"
10 )
11 input_ids, attention_masks = encoding["input_ids"].to(device), encoding[
12 "attention_mask"
13 ].to(device)
14
15 model_output = model.generate(
16 input_ids=input_ids,
17 attention_mask=attention_masks,
18 do_sample=True,
19 max_length=max_length,
20 top_k=top_k,
21 top_p=0.98,
22 early_stopping=True,
23 num_return_sequences=n_predictions,
24 )
25
26 outputs = []
27 for output in model_output:
28 generated_sent = tokenizer.decode(
29 output, skip_special_tokens=True, clean_up_tokenization_spaces=True
30 )
31 if (
32 generated_sent.lower() != sentence.lower()
33 and generated_sent not in outputs
34 ):
35 outputs.append(generated_sent)
36 return outputs
37
38paraphrases = get_paraphrases("The house will be cleaned by me every Saturday.")
39
40for sent in paraphrases:
41 print(sent)My house is up clean on Saturday morning. Thank you for this email. I'm introducing a new name and name. I'm running my house at home. I'm a taller myself. I'm gonna go with it on Monday. (the house will be up cleaned).
Is there anything that will be cleaned every Saturday morning?
The house is clean and will be cleaned each Saturday by my wife.
I will clean the house for almost a week. I have to clean it all the weekend. I will be able to do it. My house is new.
If I clean my house every Monday, I can call it clean.