Spaces:
Sleeping
Sleeping
Abir Muhtasim
commited on
Commit
·
c52c03c
1
Parent(s):
3018397
upload files
Browse files- app.py +52 -0
- backend_model.py +118 -0
- models/java_classifier.pth +3 -0
- models/python_classifier.pth +3 -0
- requirements.txt +68 -0
app.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from backend_model import load_model_and_tokenizer, infer_single_sample
|
3 |
+
|
4 |
+
|
5 |
+
java_model_architecture = 'microsoft/graphcodebert-base'
|
6 |
+
java_model_path = 'models/java_classifier.pth'
|
7 |
+
|
8 |
+
python_model_architecture = 'microsoft/graphcodebert-base'
|
9 |
+
python_model_path = 'models/python_classifier.pth'
|
10 |
+
|
11 |
+
@st.cache_resource
|
12 |
+
def load_model(arch, path):
|
13 |
+
return load_model_and_tokenizer(arch, path)
|
14 |
+
|
15 |
+
|
16 |
+
|
17 |
+
st.title('LLM Sniffer')
|
18 |
+
|
19 |
+
# form
|
20 |
+
with st.form(key='my_form'):
|
21 |
+
|
22 |
+
|
23 |
+
# select language - java or python
|
24 |
+
language = st.selectbox(
|
25 |
+
label="Select Language",
|
26 |
+
options=["Java", "Python"],
|
27 |
+
key="language"
|
28 |
+
)
|
29 |
+
|
30 |
+
|
31 |
+
# text area
|
32 |
+
|
33 |
+
code = st.text_area(label="", value="", label_visibility="hidden", height=300, placeholder="Paste your code here", key="code")
|
34 |
+
|
35 |
+
# submit button
|
36 |
+
submit_button = st.form_submit_button(label='Submit')
|
37 |
+
|
38 |
+
if submit_button:
|
39 |
+
if code:
|
40 |
+
|
41 |
+
if language == "Java":
|
42 |
+
model, tokenizer = load_model(java_model_architecture, java_model_path)
|
43 |
+
else:
|
44 |
+
model, tokenizer = load_model(python_model_architecture, python_model_path)
|
45 |
+
|
46 |
+
result = infer_single_sample(
|
47 |
+
code_text=code,
|
48 |
+
model=model,
|
49 |
+
tokenizer=tokenizer,
|
50 |
+
language=language
|
51 |
+
)
|
52 |
+
st.write(result)
|
backend_model.py
ADDED
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torch.nn as nn
|
3 |
+
import torch.optim as optim
|
4 |
+
import re
|
5 |
+
|
6 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification, AutoModel, Trainer, TrainingArguments
|
7 |
+
from torch.utils.data import DataLoader, Dataset
|
8 |
+
|
9 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
10 |
+
|
11 |
+
# device = torch.device('cpu')
|
12 |
+
|
13 |
+
|
14 |
+
def remove_java_comments(code):
|
15 |
+
# Remove single-line comments (//)
|
16 |
+
code = re.sub(r'//.*', '', code)
|
17 |
+
|
18 |
+
# Remove multi-line comments (/* ... */)
|
19 |
+
code = re.sub(r'/\*.*?\*/', '', code, flags=re.DOTALL)
|
20 |
+
|
21 |
+
return code
|
22 |
+
|
23 |
+
|
24 |
+
def remove_python_comments(code):
|
25 |
+
|
26 |
+
# Remove single-line comments (#)
|
27 |
+
code = re.sub(r'#.*', '', code)
|
28 |
+
|
29 |
+
# Remove multi-line comments (""" ... """ or ''' ... ''')
|
30 |
+
code = re.sub(r'""".*?"""', '', code, flags=re.DOTALL)
|
31 |
+
code = re.sub(r"'''.*?'''", '', code, flags=re.DOTALL)
|
32 |
+
|
33 |
+
return code
|
34 |
+
|
35 |
+
|
36 |
+
# Model with Binary Classifier
|
37 |
+
class CodeBERTBinaryClassifier(nn.Module):
|
38 |
+
def __init__(self, encoder_model, hidden_size=256, num_layers=2):
|
39 |
+
super(CodeBERTBinaryClassifier, self).__init__()
|
40 |
+
self.encoder = encoder_model
|
41 |
+
|
42 |
+
self.classifier = nn.Sequential(
|
43 |
+
nn.Dropout(0.3), # Dropout with 30%
|
44 |
+
nn.Linear(self.encoder.config.hidden_size, 128), # Hidden layer with 128 units
|
45 |
+
nn.BatchNorm1d(128), # Batch normalization for the hidden layer
|
46 |
+
nn.ReLU(), # ReLU activation for the hidden layer
|
47 |
+
nn.Dropout(0.3), # Dropout with 30%
|
48 |
+
nn.Linear(128, 1) # Output layer with 1 unit
|
49 |
+
)
|
50 |
+
|
51 |
+
|
52 |
+
def forward(self, input_ids, attention_mask):
|
53 |
+
outputs = self.encoder(input_ids=input_ids, attention_mask=attention_mask)
|
54 |
+
cls_output = outputs.last_hidden_state[:, 0, :] # [CLS] token representation
|
55 |
+
logits = self.classifier(cls_output.detach()).squeeze(-1) # Squeeze for binary logit
|
56 |
+
return logits, cls_output
|
57 |
+
|
58 |
+
|
59 |
+
|
60 |
+
def infer_single_sample(code_text, model, tokenizer, language='java'):
|
61 |
+
|
62 |
+
# Ensure model is in evaluation mode
|
63 |
+
model.eval()
|
64 |
+
|
65 |
+
# Remove comments from the code (assuming the same preprocessing as during training)
|
66 |
+
if language == 'python':
|
67 |
+
code_text = remove_python_comments(code_text)
|
68 |
+
|
69 |
+
else:
|
70 |
+
code_text = remove_java_comments(code_text)
|
71 |
+
|
72 |
+
# print(code_text)
|
73 |
+
|
74 |
+
# Tokenize the input
|
75 |
+
inputs = tokenizer.encode_plus(
|
76 |
+
code_text,
|
77 |
+
padding='max_length',
|
78 |
+
max_length=512,
|
79 |
+
truncation=True,
|
80 |
+
return_tensors='pt'
|
81 |
+
)
|
82 |
+
|
83 |
+
# Move inputs to the specified device
|
84 |
+
input_ids = inputs['input_ids'].to(device)
|
85 |
+
attention_mask = inputs['attention_mask'].to(device)
|
86 |
+
|
87 |
+
# Disable gradient computation for inference
|
88 |
+
with torch.no_grad():
|
89 |
+
# Get model prediction
|
90 |
+
logits, _ = model(input_ids, attention_mask)
|
91 |
+
|
92 |
+
# Apply sigmoid to get probability
|
93 |
+
probability = torch.sigmoid(logits).cpu().item()
|
94 |
+
|
95 |
+
# Classify based on 0.5 threshold
|
96 |
+
predicted_label = 1 if probability > 0.5 else 0
|
97 |
+
|
98 |
+
return {
|
99 |
+
'probability': probability,
|
100 |
+
'predicted_label': predicted_label,
|
101 |
+
'interpretation': 'GPT-generated' if predicted_label == 0 else 'Human-written'
|
102 |
+
}
|
103 |
+
|
104 |
+
|
105 |
+
|
106 |
+
def load_model_and_tokenizer(model_architecture, model_path):
|
107 |
+
tokenizer = AutoTokenizer.from_pretrained(model_architecture)
|
108 |
+
base_model = AutoModel.from_pretrained(model_architecture)
|
109 |
+
|
110 |
+
model = CodeBERTBinaryClassifier(base_model)
|
111 |
+
# model = model.to(device)
|
112 |
+
|
113 |
+
# Load the model
|
114 |
+
# model = CodeBERTBinaryClassifier(base_model)
|
115 |
+
model.load_state_dict(torch.load(model_path))
|
116 |
+
model = model.to(device)
|
117 |
+
|
118 |
+
return model, tokenizer
|
models/java_classifier.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:9cb0fa45719a7bddfc6fbe3e64eb1f83b41bbfba4e17d74370b8da1b02036341
|
3 |
+
size 499068174
|
models/python_classifier.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:6925f2f2faaf6d80ad821b4f2593630ef00271363ff39ebb28f2e5b7f2657948
|
3 |
+
size 499068174
|
requirements.txt
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
altair==5.5.0
|
2 |
+
attrs==24.2.0
|
3 |
+
blinker==1.9.0
|
4 |
+
cachetools==5.5.0
|
5 |
+
certifi==2024.8.30
|
6 |
+
charset-normalizer==3.4.0
|
7 |
+
click==8.1.7
|
8 |
+
filelock==3.16.1
|
9 |
+
fsspec==2024.10.0
|
10 |
+
gitdb==4.0.11
|
11 |
+
GitPython==3.1.43
|
12 |
+
huggingface-hub==0.26.5
|
13 |
+
idna==3.10
|
14 |
+
Jinja2==3.1.4
|
15 |
+
jsonschema==4.23.0
|
16 |
+
jsonschema-specifications==2024.10.1
|
17 |
+
markdown-it-py==3.0.0
|
18 |
+
MarkupSafe==3.0.2
|
19 |
+
mdurl==0.1.2
|
20 |
+
mpmath==1.3.0
|
21 |
+
narwhals==1.16.0
|
22 |
+
networkx==3.4.2
|
23 |
+
numpy==2.2.0
|
24 |
+
nvidia-cublas-cu12==12.4.5.8
|
25 |
+
nvidia-cuda-cupti-cu12==12.4.127
|
26 |
+
nvidia-cuda-nvrtc-cu12==12.4.127
|
27 |
+
nvidia-cuda-runtime-cu12==12.4.127
|
28 |
+
nvidia-cudnn-cu12==9.1.0.70
|
29 |
+
nvidia-cufft-cu12==11.2.1.3
|
30 |
+
nvidia-curand-cu12==10.3.5.147
|
31 |
+
nvidia-cusolver-cu12==11.6.1.9
|
32 |
+
nvidia-cusparse-cu12==12.3.1.170
|
33 |
+
nvidia-nccl-cu12==2.21.5
|
34 |
+
nvidia-nvjitlink-cu12==12.4.127
|
35 |
+
nvidia-nvtx-cu12==12.4.127
|
36 |
+
packaging==24.2
|
37 |
+
pandas==2.2.3
|
38 |
+
pillow==11.0.0
|
39 |
+
protobuf==5.29.1
|
40 |
+
pyarrow==18.1.0
|
41 |
+
pydeck==0.9.1
|
42 |
+
Pygments==2.18.0
|
43 |
+
python-dateutil==2.9.0.post0
|
44 |
+
pytz==2024.2
|
45 |
+
PyYAML==6.0.2
|
46 |
+
referencing==0.35.1
|
47 |
+
regex==2024.11.6
|
48 |
+
requests==2.32.3
|
49 |
+
rich==13.9.4
|
50 |
+
rpds-py==0.22.3
|
51 |
+
safetensors==0.4.5
|
52 |
+
setuptools==75.6.0
|
53 |
+
six==1.17.0
|
54 |
+
smmap==5.0.1
|
55 |
+
streamlit==1.40.2
|
56 |
+
sympy==1.13.1
|
57 |
+
tenacity==9.0.0
|
58 |
+
tokenizers==0.21.0
|
59 |
+
toml==0.10.2
|
60 |
+
torch==2.5.1
|
61 |
+
tornado==6.4.2
|
62 |
+
tqdm==4.67.1
|
63 |
+
transformers==4.47.0
|
64 |
+
triton==3.1.0
|
65 |
+
typing_extensions==4.12.2
|
66 |
+
tzdata==2024.2
|
67 |
+
urllib3==2.2.3
|
68 |
+
watchdog==6.0.0
|