File size: 2,214 Bytes
4120fa8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from pydoc import text

import torch
from src.task.model import TinyTransformerLM, TinyClassifier, TinyAnsweringModel

class TextExtractor:

    def __init__(
        self,
        model: TinyAnsweringModel,
        tokenizer,
        context_length,
        device=None,
    ):
        self.model = model
        self.tokenizer = tokenizer
        self.context_length = context_length

        if device is None:
            device = (
                "cuda"
                if torch.cuda.is_available()
                else "cpu"
            )

        self.device = torch.device(device)
        self.model.to(self.device)
        self.model.eval()

    @torch.no_grad()
    def extract(
        self,
        context_text: str,  #context containing the answer
        question_text: str  #the actual question 
        ):

        input_text = (
                self.tokenizer.CLS_TOKEN
                + question_text
                + self.tokenizer.SEP_TOKEN
                + context_text
                + self.tokenizer.SEP_TOKEN
                )
        input_ids = self.tokenizer.encode(input_text)

        padded_ids, mask_ids = self.tokenizer.pad(
            [input_ids],
            max_length=self.context_length
            )

        token_tensor = torch.tensor(
            padded_ids[0],
            dtype=torch.long,
            device=self.device,
            )

        padding_mask = torch.tensor(
            mask_ids,
            dtype=torch.bool,
            device=self.device,
            )

        #Typically -> embedding -> transformer -> start,end logits
        #Forward pass on the model is refusing :-(
        start_logits, end_logits = self.model( 
            tokens=token_tensor.unsqueeze(0),
            padding_mask=padding_mask
            )


        # Remove batch dimension: [max_length]
        start_logits = start_logits[0]
        end_logits = end_logits[0]
        
        start_predictions = torch.argmax(
            start_logits,
            dim=-1,
            )
    
        end_predictions = torch.argmax(
            end_logits,
            dim=-1,
            )
        
        return token_tensor, start_predictions.item(), end_predictions.item()