Spaces:
Sleeping
Sleeping
File size: 1,404 Bytes
60a2954 |
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 pandas as pd
import numpy as np
import torch
from torch import nn
import torch.nn.functional as F
from logreg_model import bert_for_logreg, tokenizer_bert
from preprocess_bert import preprocess_bert
MAX_LEN = 100
class BertTunnig(nn.Module):
def __init__(self, bert_model):
super().__init__()
self.bert = bert_model
for weights in self.bert.parameters():
weights.requires_grad = False
self.fc1 = nn.Linear(768, 256)
self.drop1 = nn.Dropout(p=0.5)
self.fc2 = nn.Linear(256, 32)
self.fc_out = nn.Linear(32, 1)
def forward(self, x, attention_mask):
output = self.bert(x, attention_mask=attention_mask)[0][:, 0, :]
output = self.fc1(output)
output_drop = self.drop1(output)
output = self.fc2(output_drop)
output = self.fc_out(output)
return torch.sigmoid(output)
model_tunning = BertTunnig(bert_model=bert_for_logreg)
model_tunning.load_state_dict(torch.load('best_weights_berttinnug(2).pt'))
def predict_2(text):
preprocessed_text, attention_mask = preprocess_bert(text, MAX_LEN=MAX_LEN)
preprocessed_text, attention_mask = torch.tensor(preprocessed_text).unsqueeze(0), torch.tensor([attention_mask])
with torch.inference_mode():
predict = model_tunning(preprocessed_text, attention_mask=attention_mask).item()
return round(predict) |