MartyNattakit
commited on
Commit
·
fbfb0eb
1
Parent(s):
252a2a0
Add CWE classification app, dependencies, and model file
Browse files- app.py +100 -0
- requirements.txt +0 -0
app.py
ADDED
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from transformers import RobertaTokenizer, RobertaModel
|
3 |
+
import numpy as np
|
4 |
+
from scipy.special import softmax
|
5 |
+
import gradio as gr
|
6 |
+
import re
|
7 |
+
|
8 |
+
# Define the model class with dimension reduction
|
9 |
+
class CodeClassifier(torch.nn.Module):
|
10 |
+
def __init__(self, base_model, num_labels=6):
|
11 |
+
super(CodeClassifier, self).__init__()
|
12 |
+
self.base = base_model
|
13 |
+
self.reduction = torch.nn.Linear(768, 512) # Randomly initialized
|
14 |
+
self.classifier = torch.nn.Linear(512, num_labels) # Match checkpoint
|
15 |
+
|
16 |
+
def forward(self, input_ids, attention_mask):
|
17 |
+
outputs = self.base(input_ids=input_ids, attention_mask=attention_mask)
|
18 |
+
reduced = self.reduction(outputs.pooler_output)
|
19 |
+
return self.classifier(reduced)
|
20 |
+
|
21 |
+
# Load model and tokenizer
|
22 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
23 |
+
tokenizer = RobertaTokenizer.from_pretrained('microsoft/codebert-base')
|
24 |
+
base_model = RobertaModel.from_pretrained('microsoft/codebert-base')
|
25 |
+
model = CodeClassifier(base_model)
|
26 |
+
checkpoint = torch.load("C:\\Users\\MartyNattakit\\Downloads\\best_model.pt", map_location=device)
|
27 |
+
|
28 |
+
# Load the state dict, focusing on classifier weights
|
29 |
+
model_state = checkpoint.get('model_state_dict', checkpoint)
|
30 |
+
model.load_state_dict(model_state, strict=False)
|
31 |
+
print("Loaded state dict keys:", model.state_dict().keys())
|
32 |
+
print("Classifier weight shape:", model.classifier.weight.shape)
|
33 |
+
model.eval()
|
34 |
+
model.to(device)
|
35 |
+
|
36 |
+
# Label mapping with descriptions
|
37 |
+
label_map = {
|
38 |
+
0: ('none', 'No Vulnerability Detected'),
|
39 |
+
1: ('cwe-121', 'Stack-based Buffer Overflow'),
|
40 |
+
2: ('cwe-78', 'OS Command Injection'),
|
41 |
+
3: ('cwe-190', 'Integer Overflow or Wraparound'),
|
42 |
+
4: ('cwe-191', 'Integer Underflow'),
|
43 |
+
5: ('cwe-122', 'Heap-based Buffer Overflow')
|
44 |
+
}
|
45 |
+
|
46 |
+
def load_c_file(file):
|
47 |
+
try:
|
48 |
+
if file is None:
|
49 |
+
return ""
|
50 |
+
with open(file.name, 'r', encoding='utf-8') as f:
|
51 |
+
content = f.read()
|
52 |
+
return content
|
53 |
+
except Exception as e:
|
54 |
+
return f"Error reading file: {str(e)}"
|
55 |
+
|
56 |
+
def clean_code(code):
|
57 |
+
code = re.sub(r'/\*.*?\*/', '', code, flags=re.DOTALL)
|
58 |
+
code = re.sub(r'//.*$', '', code, flags=re.MULTILINE)
|
59 |
+
code = ' '.join(code.split())
|
60 |
+
return code
|
61 |
+
|
62 |
+
def evaluate_code(code):
|
63 |
+
try:
|
64 |
+
if len(code) >= 1500000:
|
65 |
+
return "Code too large"
|
66 |
+
|
67 |
+
cleaned_code = clean_code(code)
|
68 |
+
inputs = tokenizer(cleaned_code, return_tensors="pt", truncation=True, padding=True, max_length=256).to(device)
|
69 |
+
print("Input shape:", inputs['input_ids'].shape)
|
70 |
+
|
71 |
+
with torch.no_grad():
|
72 |
+
outputs = model(**inputs)
|
73 |
+
print("Raw logits:", outputs.cpu().numpy())
|
74 |
+
probs = softmax(outputs.cpu().numpy(), axis=1)
|
75 |
+
pred = np.argmax(probs, axis=1)[0]
|
76 |
+
cwe, description = label_map[pred]
|
77 |
+
return f"{cwe} {description}"
|
78 |
+
|
79 |
+
except Exception as e:
|
80 |
+
return f"Error during prediction: {str(e)}"
|
81 |
+
|
82 |
+
with gr.Blocks() as web:
|
83 |
+
with gr.Row():
|
84 |
+
with gr.Column(scale=1):
|
85 |
+
code_box = gr.Textbox(lines=20, label="** C/C++ Code", placeholder="Paste your C or C++ code here...")
|
86 |
+
with gr.Column(scale=1):
|
87 |
+
cc_file = gr.File(label="Upload C/C++ File (.c or .cpp)", file_types=[".c", ".cpp"])
|
88 |
+
check_btn = gr.Button("Check")
|
89 |
+
|
90 |
+
with gr.Row():
|
91 |
+
gr.Markdown("### Result:")
|
92 |
+
|
93 |
+
with gr.Row():
|
94 |
+
with gr.Column(scale=1):
|
95 |
+
label_box = gr.Textbox(label="Vulnerability", interactive=False)
|
96 |
+
|
97 |
+
cc_file.change(fn=load_c_file, inputs=cc_file, outputs=code_box)
|
98 |
+
check_btn.click(fn=evaluate_code, inputs=code_box, outputs=[label_box])
|
99 |
+
|
100 |
+
web.launch()
|
requirements.txt
ADDED
File without changes
|