Views
No views yet
1import torch
2from transformers import T5ForConditionalGeneration,T5Tokenizer
3
4
5def set_seed(seed):
6 torch.manual_seed(seed)
7 if torch.cuda.is_available():
8 torch.cuda.manual_seed_all(seed)
9
10set_seed(42)
11
12model = T5ForConditionalGeneration.from_pretrained('ramsrigouthamg/t5_paraphraser')
13tokenizer = T5Tokenizer.from_pretrained('ramsrigouthamg/t5_paraphraser')
14
15device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
16print ("device ",device)
17model = model.to(device)
18
19sentence = "Which course should I take to get started in data science?"
20# sentence = "What are the ingredients required to bake a perfect cake?"
21# sentence = "What is the best possible approach to learn aeronautical engineering?"
22# sentence = "Do apples taste better than oranges in general?"
23
24
25text = "paraphrase: " + sentence + " </s>"
26
27
28max_len = 256
29
30encoding = tokenizer.encode_plus(text,pad_to_max_length=True, return_tensors="pt")
31input_ids, attention_masks = encoding["input_ids"].to(device), encoding["attention_mask"].to(device)
32
33
34# set top_k = 50 and set top_p = 0.95 and num_return_sequences = 3
35beam_outputs = model.generate(
36 input_ids=input_ids, attention_mask=attention_masks,
37 do_sample=True,
38 max_length=256,
39 top_k=120,
40 top_p=0.98,
41 early_stopping=True,
42 num_return_sequences=10
43)
44
45
46print ("\nOriginal Question ::")
47print (sentence)
48print ("\n")
49print ("Paraphrased Questions :: ")
50final_outputs =[]
51for beam_output in beam_outputs:
52 sent = tokenizer.decode(beam_output, skip_special_tokens=True,clean_up_tokenization_spaces=True)
53 if sent.lower() != sentence.lower() and sent not in final_outputs:
54 final_outputs.append(sent)
55
56for i, final_output in enumerate(final_outputs):
57 print("{}: {}".format(i, final_output))
58Original Question ::
Which course should I take to get started in data science?
Paraphrased Questions ::
0: What should I learn to become a data scientist?
1: How do I get started with data science?
2: How would you start a data science career?
3: How can I start learning data science?
4: How do you get started in data science?
5: What's the best course for data science?
6: Which course should I start with for data science?
7: What courses should I follow to get started in data science?
8: What degree should be taken by a data scientist?
9: Which course should I follow to become a Data Scientist?