Text Classification
Transformers
Safetensors
roberta
Generated from Trainer
text-embeddings-inference
Instructions to use azherali/CodeDetect-UniXCoder-T2 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use azherali/CodeDetect-UniXCoder-T2 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-classification", model="azherali/CodeDetect-UniXCoder-T2")# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForSequenceClassification tokenizer = AutoTokenizer.from_pretrained("azherali/CodeDetect-UniXCoder-T2") model = AutoModelForSequenceClassification.from_pretrained("azherali/CodeDetect-UniXCoder-T2", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Update README.md
Browse files
README.md
CHANGED
|
@@ -19,27 +19,156 @@ should probably proofread and complete it, then remove this comment. -->
|
|
| 19 |
|
| 20 |
# CodeDetect-UniXCoder-T2
|
| 21 |
|
| 22 |
-
This model is a fine-tuned version of [microsoft/unixcoder-base](https://huggingface.co/microsoft/unixcoder-base)
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
## Model description
|
| 31 |
-
|
| 32 |
-
More information needed
|
| 33 |
-
|
| 34 |
-
## Intended uses & limitations
|
| 35 |
-
|
| 36 |
-
More information needed
|
| 37 |
-
|
| 38 |
-
## Training and evaluation data
|
| 39 |
-
|
| 40 |
-
More information needed
|
| 41 |
-
|
| 42 |
-
## Training procedure
|
| 43 |
|
| 44 |
### Training hyperparameters
|
| 45 |
|
|
|
|
| 19 |
|
| 20 |
# CodeDetect-UniXCoder-T2
|
| 21 |
|
| 22 |
+
This model is a fine-tuned version of [microsoft/unixcoder-base](https://huggingface.co/microsoft/unixcoder-base)
|
| 23 |
+
|
| 24 |
+
### 2. Load the Model
|
| 25 |
+
|
| 26 |
+
```python
|
| 27 |
+
import torch
|
| 28 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 29 |
+
|
| 30 |
+
MODEL_NAME = "azherali/CodeDetect-UniXCoder-T2"
|
| 31 |
+
MAX_LENGTH = 512
|
| 32 |
+
|
| 33 |
+
# Select device
|
| 34 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 35 |
+
print("Device:", device)
|
| 36 |
+
|
| 37 |
+
# Load tokenizer and model
|
| 38 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 39 |
+
|
| 40 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
|
| 41 |
+
|
| 42 |
+
# Configure class labels
|
| 43 |
+
model.config.id2label = {
|
| 44 |
+
"0": "human",
|
| 45 |
+
"1": "qwen",
|
| 46 |
+
"2": "phi",
|
| 47 |
+
"3": "meta-llama",
|
| 48 |
+
"4": "01-ai",
|
| 49 |
+
"5": "gemma",
|
| 50 |
+
"6": "ibm-granite",
|
| 51 |
+
"7": "bigcode",
|
| 52 |
+
"8": "deepseek",
|
| 53 |
+
"9": "gpt",
|
| 54 |
+
"10": "mistral"
|
| 55 |
+
}
|
| 56 |
+
model.config.label2id = {
|
| 57 |
+
"human": 0,
|
| 58 |
+
"qwen": 1,
|
| 59 |
+
"phi": 2,
|
| 60 |
+
"meta-llama": 3,
|
| 61 |
+
"01-ai": 4,
|
| 62 |
+
"gemma": 5,
|
| 63 |
+
"ibm-granite": 6,
|
| 64 |
+
"bigcode": 7,
|
| 65 |
+
"deepseek": 8,
|
| 66 |
+
"gpt": 9,
|
| 67 |
+
"mistral": 10
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
model.to(device)
|
| 71 |
+
model.eval()
|
| 72 |
+
|
| 73 |
+
print("Number of classes:", model.config.num_labels)
|
| 74 |
+
print("Label mapping:", model.config.id2label)
|
| 75 |
+
```
|
| 76 |
+
|
| 77 |
+
### 3. Prediction Function
|
| 78 |
+
|
| 79 |
+
The `predict_code()` function takes source code as input and returns the predicted label, class ID, and probability scores.
|
| 80 |
+
|
| 81 |
+
```python
|
| 82 |
+
def predict_code(code: str) -> dict:
|
| 83 |
+
if not isinstance(code, str) or not code.strip():
|
| 84 |
+
raise ValueError("Please provide a non-empty code string.")
|
| 85 |
+
|
| 86 |
+
# Tokenize source code
|
| 87 |
+
inputs = tokenizer(
|
| 88 |
+
code,
|
| 89 |
+
truncation=True,
|
| 90 |
+
max_length=MAX_LENGTH,
|
| 91 |
+
padding=True,
|
| 92 |
+
return_tensors="pt",
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
# Move inputs to the selected device
|
| 96 |
+
inputs = {
|
| 97 |
+
key: value.to(device)
|
| 98 |
+
for key, value in inputs.items()
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
# Perform inference
|
| 102 |
+
with torch.inference_mode():
|
| 103 |
+
outputs = model(**inputs)
|
| 104 |
+
probabilities = torch.softmax(outputs.logits, dim=-1)[0]
|
| 105 |
+
|
| 106 |
+
# Get predicted class
|
| 107 |
+
predicted_id = int(probabilities.argmax().item())
|
| 108 |
+
|
| 109 |
+
labels = model.config.id2label or {}
|
| 110 |
+
|
| 111 |
+
predicted_label = labels.get(
|
| 112 |
+
predicted_id,
|
| 113 |
+
labels.get(str(predicted_id), f"class_{predicted_id}")
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
# Return prediction and class probabilities
|
| 117 |
+
return {
|
| 118 |
+
"predicted_class_id": predicted_id,
|
| 119 |
+
"predicted_label": predicted_label,
|
| 120 |
+
"class_probabilities": {
|
| 121 |
+
str(labels.get(i, labels.get(str(i), f"class_{i}"))):
|
| 122 |
+
round(float(probabilities[i].item()), 4)
|
| 123 |
+
for i in range(len(probabilities))
|
| 124 |
+
},
|
| 125 |
+
}
|
| 126 |
+
```
|
| 127 |
+
|
| 128 |
+
### 4. Example Inference
|
| 129 |
+
|
| 130 |
+
```python
|
| 131 |
+
code = """
|
| 132 |
+
def add(a, b):
|
| 133 |
+
return a + b
|
| 134 |
+
"""
|
| 135 |
+
|
| 136 |
+
result = predict_code(code)
|
| 137 |
+
print(result)
|
| 138 |
+
```
|
| 139 |
+
|
| 140 |
+
### 5. Example Output
|
| 141 |
+
|
| 142 |
+
The following output is illustrative. Actual predictions and scores depend on the input and model behavior.
|
| 143 |
+
|
| 144 |
+
```python
|
| 145 |
+
{
|
| 146 |
+
"predicted_class_id": 9,
|
| 147 |
+
"predicted_label": "gpt",
|
| 148 |
+
"class_probabilities": {
|
| 149 |
+
"human": 0.0001,
|
| 150 |
+
"qwen": 0.4001,
|
| 151 |
+
"phi": 0.0014,
|
| 152 |
+
"meta-llama": 0.0405,
|
| 153 |
+
"01-ai": 0.0024,
|
| 154 |
+
"gemma": 0.0007,
|
| 155 |
+
"ibm-granite": 0.0034,
|
| 156 |
+
"bigcode": 0.0,
|
| 157 |
+
"deepseek": 0.0162,
|
| 158 |
+
"gpt": 0.5345,
|
| 159 |
+
"mistral": 0.0007,
|
| 160 |
+
},
|
| 161 |
+
}
|
| 162 |
+
```
|
| 163 |
+
|
| 164 |
+
### 6. Output Description
|
| 165 |
+
|
| 166 |
+
| Field | Description |
|
| 167 |
+
|---|---|
|
| 168 |
+
| `predicted_class_id` | Predicted class index: `0` or `1`. |
|
| 169 |
+
| `predicted_label` | Predicted class: `Human` or `AI`. |
|
| 170 |
+
| `class_probabilities` | Softmax scores for each class. |
|
| 171 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
|
| 173 |
### Training hyperparameters
|
| 174 |
|