File size: 1,605 Bytes
d8f4336
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
import torch.nn as nn
import torch.nn.functional as F
from transformers import AutoTokenizer, AutoModelForSequenceClassification

class BertClassifier(nn.Module):
    def __init__(self, bert):
        super(BertClassifier, self).__init__()
        self.bert = bert
    
    def forward(self, input_id, attention_mask):
        output = self.bert(input_ids=input_id, attention_mask=attention_mask)
        return output.logits

tokenizer = AutoTokenizer.from_pretrained('microsoft/deberta-v3-base')

bert = AutoModelForSequenceClassification.from_pretrained('microsoft/deberta-v3-base').train()

classifier = nn.Sequential(
    nn.Linear(768, 1024),
    nn.ReLU(),
    nn.Dropout(0.5),
    nn.Linear(1024, 2)
)

bert.classifier = classifier

model = BertClassifier(bert)
state_dict = torch.load(
    "./deberta/fastai_QIQC-deberta-v3.pth", map_location=torch.device('cpu'), 
    weights_only=True
    )

model.load_state_dict(state_dict, strict=False)

model.eval()

def deBERTa_predict(text):
    tokenized_input = tokenizer(text, 
                                padding="max_length", 
                                truncation=True, 
                                max_length=30, 
                                return_tensors="pt")
    
    model.eval()
    with torch.no_grad():
        logits = model(tokenized_input['input_ids'], tokenized_input['attention_mask'])
    
    probabilities = F.softmax(logits, dim=-1)
    
    prediction = torch.argmax(probabilities, dim=-1).item()

    return prediction, probabilities[0][1].item()