forked from lyccol/CodeBERT-based-webshell-detection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo2.py
69 lines (51 loc) · 1.84 KB
/
demo2.py
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
import torch
from transformers import RobertaTokenizer, RobertaConfig, RobertaModel, AdamW
import numpy as np
from dataSet import PhpDataset
from sklearn.model_selection import train_test_split
from torch.utils.data import DataLoader
from pathlib import Path
from pretreatment.code_pre import code_pre
import NNModel
if __name__ == '__main__':
print('initing model...')
tokenizer = RobertaTokenizer.from_pretrained("microsoft/codebert-base")
# model = RobertaModel.from_pretrained("microsoft/codebert-base")
print('open file.....')
try:
rf = open('/home/xxxxx/Document/data/odata/a.php', 'r', encoding='utf-8', errors='ignore')
data = rf.read()
finally:
rf.close()
data = code_pre(data)[:10000]
print(data)
inputs = tokenizer.encode_plus(
data,
None,
add_special_tokens=True,
max_length=512,
padding='max_length',
return_token_type_ids=True,
truncation=True,
)
ids = inputs['input_ids']
mask = inputs['attention_mask']
token_type_ids = inputs["token_type_ids"]
data = {
'ids': torch.tensor(ids, dtype=torch.long),
'mask': torch.tensor(mask, dtype=torch.long),
'token_type_ids': torch.tensor(token_type_ids, dtype=torch.long),
}
# targets = data['targets']
# targets = targets.view(-1, 1)
# targets = torch.LongTensor(targets)
# targets = torch.zeros(batch_size, 2).scatter_(1, targets, 1)
ids = data['ids'].view(1, 512).cpu()
mask = data['mask'].view(1, 512).cpu()
token_type_ids = data['token_type_ids'].view(1, 512).cpu()
# print(type(torch.load('model/cls_model_9.pth')))
model = NNModel.CodeBERTClassifer().cpu()
model.load_state_dict(torch.load('model/cls_model_0.pth'))
outputs = model(ids, mask, token_type_ids)
print(outputs)
# print(model)