DaniilOr commited on
Commit
b3c363c
·
verified ·
1 Parent(s): ba5e286

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +84 -4
README.md CHANGED
@@ -1,5 +1,85 @@
 
 
 
 
 
 
 
 
1
 
2
- ---
3
- license: apache-2.0
4
- ---
5
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ datasets:
4
+ - project-droid/DroidCollection
5
+ base_model:
6
+ - answerdotai/ModernBERT-base
7
+ pipeline_tag: text-classification
8
+ ---
9
 
10
+ # DroidDetect-Base
11
+
12
+ This is a text classification model based on `answerdotai/ModernBERT-base`, fine-tuned to distinguish between **human-written**, **AI-refined** and **AI-generated** code.
13
+
14
+ The model was trained on the `DroidCollection` dataset. It's designed as a **ternary classifier** to address the core task of AI code detection.
15
+
16
+ A key feature of this model is its training objective, which combines standard **Cross-Entropy Loss** with a **Batch-Hard Triplet Loss**. This contrastive loss component encourages the model to learn more discriminative embeddings by pushing representations of human vs. machine code further apart in the vector space.
17
+
18
+ ***
19
+
20
+ ## Model Details
21
+
22
+ * **Base Model:** `answerdotai/ModernBERT-base`
23
+ * **Loss Function:** `Total Loss = CrossEntropyLoss + 0.1 * TripletLoss`
24
+ * **Dataset:** Filtered training set of the [DroidCollection](https://huggingface.co/datasets/project-droid/DroidCollection).
25
+
26
+ #### Label Mapping
27
+
28
+ The model predicts one of 3 classes. The mapping from ID to label is as follows:
29
+
30
+ ```json
31
+ {
32
+ "0": "HUMAN_GENERATED",
33
+ "1": "MACHINE_GENERATED",
34
+ "2": "MACHINE_REFINED",
35
+ }
36
+ ```
37
+
38
+ ## Model Code
39
+
40
+ The following code can be used for reproducibility:
41
+
42
+ ```python
43
+ TEXT_EMBEDDING_DIM = 768
44
+
45
+
46
+ class TLModel(nn.Module):
47
+ def __init__(self, text_encoder, projection_dim=128, num_classes=NUM_CLASSES, class_weights=None):
48
+ super().__init__()
49
+ self.text_encoder = text_encoder
50
+ self.num_classes = num_classes
51
+ text_output_dim = TEXT_EMBEDDING_DIM
52
+ self.additional_loss = losses.BatchHardSoftMarginTripletLoss(self.text_encoder)
53
+
54
+ self.text_projection = nn.Linear(text_output_dim, projection_dim)
55
+ self.classifier = nn.Linear(projection_dim, num_classes)
56
+ self.class_weights = class_weights
57
+
58
+ def forward(self, labels=None, input_ids=None, attention_mask=None):
59
+ actual_labels = labels
60
+ sentence_embeddings = self.text_encoder(input_ids=input_ids, attention_mask=attention_mask).last_hidden_state
61
+ sentence_embeddings = sentence_embeddings.mean(dim=1)
62
+ projected_text = F.relu(self.text_projection(sentence_embeddings))
63
+ logits = self.classifier(projected_text)
64
+ loss = None
65
+ cross_entropy_loss = None
66
+ contrastive_loss = None
67
+
68
+ if actual_labels is not None:
69
+ loss_fct_ce = nn.CrossEntropyLoss(weight=self.class_weights.to(logits.device) if self.class_weights is not None else None)
70
+ cross_entropy_loss = loss_fct_ce(logits.view(-1, self.num_classes), actual_labels.view(-1))
71
+ contrastive_loss = self.additional_loss.batch_hard_triplet_loss(embeddings=projected_text, labels=actual_labels)
72
+ lambda_contrast = 0.1
73
+ loss = cross_entropy_loss + lambda_contrast * contrastive_loss
74
+
75
+
76
+ output = {"logits": logits, "fused_embedding": projected_text}
77
+ if loss is not None:
78
+ output["loss"] = loss
79
+ if cross_entropy_loss is not None:
80
+ output["cross_entropy_loss"] = cross_entropy_loss
81
+ if contrastive_loss is not None:
82
+ output["contrastive_loss"] = contrastive_loss
83
+
84
+ return output
85
+ ```