File size: 982 Bytes
5ccb6e2
 
 
54791f4
5ccb6e2
 
 
 
 
 
 
 
c564920
 
5ccb6e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---
license: afl-3.0
---

# Question generation using T5 transformer trained on SQuAD

<h2> <i>Input format: context: "..." answer: "..." </i></h2>

Import the pretrained model as well as tokenizer:
```
from transformers import T5ForConditionalGeneration, T5Tokenizer

model = T5ForConditionalGeneration.from_pretrained('AbhilashDatta/T5_qgen-squad_v1') 
tokenizer = T5Tokenizer.from_pretrained('AbhilashDatta/T5_qgen-squad_v1')
```

Then use the tokenizer to encode/decode and model to generate: 

```
input = "context: My name is Abhilash Datta. answer: Abhilash"
batch = tokenizer(input, padding='longest', max_length=512, return_tensors='pt')
inputs_batch = batch['input_ids'][0]
inputs_batch = torch.unsqueeze(inputs_batch, 0)

ques_id = model.generate(inputs_batch, max_length=100, early_stopping=True)
ques_batch = [tokenizer.decode(g, skip_special_tokens=True, clean_up_tokenization_spaces=False) for g in ques_id]

print(ques_batch)
```

Output:
```
['what is my name']
```