DaniilOr commited on
Commit
4328ba9
·
verified ·
1 Parent(s): 0e3a544

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +83 -4
README.md CHANGED
@@ -1,5 +1,84 @@
 
 
 
 
 
 
 
 
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** and **AI-generated** code.
13
+
14
+ The model was trained on the `DroidCollection` dataset. It's designed as a **binary 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 two classes. The mapping from ID to label is as follows:
29
+
30
+ ```json
31
+ {
32
+ "0": "HUMAN_GENERATED",
33
+ "1": "MACHINE_GENERATED"
34
+ }
35
+ ```
36
+
37
+ ## Model Code
38
+
39
+ The following code can be used for reproducibility:
40
+
41
+ ```python
42
+ TEXT_EMBEDDING_DIM = 768
43
+
44
+
45
+ class TLModel(nn.Module):
46
+ def __init__(self, text_encoder, projection_dim=128, num_classes=NUM_CLASSES, class_weights=None):
47
+ super().__init__()
48
+ self.text_encoder = text_encoder
49
+ self.num_classes = num_classes
50
+ text_output_dim = TEXT_EMBEDDING_DIM
51
+ self.additional_loss = losses.BatchHardSoftMarginTripletLoss(self.text_encoder)
52
+
53
+ self.text_projection = nn.Linear(text_output_dim, projection_dim)
54
+ self.classifier = nn.Linear(projection_dim, num_classes)
55
+ self.class_weights = class_weights
56
+
57
+ def forward(self, labels=None, input_ids=None, attention_mask=None):
58
+ actual_labels = labels
59
+ sentence_embeddings = self.text_encoder(input_ids=input_ids, attention_mask=attention_mask).last_hidden_state
60
+ sentence_embeddings = sentence_embeddings.mean(dim=1)
61
+ projected_text = F.relu(self.text_projection(sentence_embeddings))
62
+ logits = self.classifier(projected_text)
63
+ loss = None
64
+ cross_entropy_loss = None
65
+ contrastive_loss = None
66
+
67
+ if actual_labels is not None:
68
+ loss_fct_ce = nn.CrossEntropyLoss(weight=self.class_weights.to(logits.device) if self.class_weights is not None else None)
69
+ cross_entropy_loss = loss_fct_ce(logits.view(-1, self.num_classes), actual_labels.view(-1))
70
+ contrastive_loss = self.additional_loss.batch_hard_triplet_loss(embeddings=projected_text, labels=actual_labels)
71
+ lambda_contrast = 0.1
72
+ loss = cross_entropy_loss + lambda_contrast * contrastive_loss
73
+
74
+
75
+ output = {"logits": logits, "fused_embedding": projected_text}
76
+ if loss is not None:
77
+ output["loss"] = loss
78
+ if cross_entropy_loss is not None:
79
+ output["cross_entropy_loss"] = cross_entropy_loss
80
+ if contrastive_loss is not None:
81
+ output["contrastive_loss"] = contrastive_loss
82
+
83
+ return output
84
+ ```