ashishkat commited on
Commit
8245ec7
·
1 Parent(s): 826367f

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +51 -0
README.md ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import (
2
+ T5ForConditionalGeneration,
3
+ T5Tokenizer
4
+ )
5
+ import pandas as pd
6
+ import numpy as np
7
+
8
+
9
+ ## loading tokenizer model
10
+ tokenizer = T5Tokenizer.from_pretrained(model_name)
11
+
12
+ ## loading trained model
13
+ model = T5ForConditionalGeneration.from_pretrained(model_name, return_dict=True)
14
+
15
+ def generate_answer(question, context):
16
+
17
+ """Function gives the answer to the question asked, given context
18
+
19
+ question(str) : question asked by user
20
+ context(str): Paragraph given by used
21
+
22
+ Returns:
23
+ string: Answer to respective question asked
24
+ """
25
+
26
+ ## tokenizeing question + context at a same time
27
+ ## max length is 512, greater are removed, less are padded
28
+ source_encoding = tokenizer(
29
+ question,
30
+ context,
31
+ max_length = 512,
32
+ padding="max_length",
33
+ truncation="only_second",
34
+ return_attention_mask = True,
35
+ return_tensors="pt",
36
+ add_special_tokens=True
37
+ )
38
+ ## generating answer from model
39
+ generate_ids = model.generate(
40
+ input_ids = source_encoding["input_ids"],
41
+ attention_mask = source_encoding["attention_mask"],
42
+ max_length = 30,
43
+ use_cache=True,
44
+ )
45
+
46
+ ## decoding the tokenized prediction
47
+ pred = [
48
+ tokenizer.decode(ids, skip_special_tokens=True) for ids in generate_ids
49
+ ]
50
+
51
+ return " ".join(pred) ## returns the predicted string as answer