menimeni123
commited on
Commit
•
180d0f0
1
Parent(s):
8f655f1
latest
Browse files- .DS_Store +0 -0
- README.md +14 -0
- handler.py +30 -0
.DS_Store
CHANGED
Binary files a/.DS_Store and b/.DS_Store differ
|
|
README.md
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# DistilBERT for Text Classification
|
2 |
+
|
3 |
+
This repository contains a fine-tuned DistilBERT model for text classification. The model is designed to classify text into four categories: SAFE, JAILBREAK, INJECTION, and PHISHING.
|
4 |
+
|
5 |
+
## Model Details
|
6 |
+
|
7 |
+
- Base model: DistilBERT (distilbert-base-uncased)
|
8 |
+
- Task: Sequence Classification
|
9 |
+
- Number of labels: 4
|
10 |
+
- Labels: SAFE, JAILBREAK, INJECTION, PHISHING
|
11 |
+
|
12 |
+
## Usage
|
13 |
+
|
14 |
+
To use this model, you can leverage the Hugging Face Transformers library:
|
handler.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import Pipeline
|
2 |
+
import torch
|
3 |
+
import joblib
|
4 |
+
|
5 |
+
class CustomPipeline(Pipeline):
|
6 |
+
def __init__(self, model, tokenizer, device=-1, **kwargs):
|
7 |
+
super().__init__(model=model, tokenizer=tokenizer, device=device, **kwargs)
|
8 |
+
self.label_mapping = joblib.load("label_mapping.joblib")
|
9 |
+
|
10 |
+
def _sanitize_parameters(self, **kwargs):
|
11 |
+
return {}, {}, {}
|
12 |
+
|
13 |
+
def preprocess(self, inputs):
|
14 |
+
return self.tokenizer(inputs, return_tensors="pt", truncation=True, padding=True, max_length=512)
|
15 |
+
|
16 |
+
def _forward(self, model_inputs):
|
17 |
+
with torch.no_grad():
|
18 |
+
outputs = self.model(**model_inputs)
|
19 |
+
return outputs
|
20 |
+
|
21 |
+
def postprocess(self, model_outputs):
|
22 |
+
logits = model_outputs.logits
|
23 |
+
predicted_class = torch.argmax(logits, dim=1).item()
|
24 |
+
predicted_label = self.label_mapping[predicted_class]
|
25 |
+
confidence = torch.softmax(logits, dim=1)[0][predicted_class].item()
|
26 |
+
|
27 |
+
return {
|
28 |
+
"label": predicted_label,
|
29 |
+
"score": confidence
|
30 |
+
}
|