Text Classification
Transformers
Safetensors
roberta
Generated from Trainer
text-embeddings-inference
Instructions to use azherali/CodeDetect-UniXCoder-T1 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use azherali/CodeDetect-UniXCoder-T1 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-classification", model="azherali/CodeDetect-UniXCoder-T1")# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForSequenceClassification tokenizer = AutoTokenizer.from_pretrained("azherali/CodeDetect-UniXCoder-T1") model = AutoModelForSequenceClassification.from_pretrained("azherali/CodeDetect-UniXCoder-T1", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Update README.md
Browse files
README.md
CHANGED
|
@@ -19,27 +19,122 @@ should probably proofread and complete it, then remove this comment. -->
|
|
| 19 |
|
| 20 |
# CodeDetect-UniXCoder-T1
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
### Training hyperparameters
|
| 45 |
|
|
|
|
| 19 |
|
| 20 |
# CodeDetect-UniXCoder-T1
|
| 21 |
|
| 22 |
+
### 2. Load the Model
|
| 23 |
+
|
| 24 |
+
```python
|
| 25 |
+
import torch
|
| 26 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 27 |
+
|
| 28 |
+
MODEL_NAME = "azherali/CodeDetect-UniXCoder-T1"
|
| 29 |
+
MAX_LENGTH = 512
|
| 30 |
+
|
| 31 |
+
# Select device
|
| 32 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 33 |
+
print("Device:", device)
|
| 34 |
+
|
| 35 |
+
# Load tokenizer and model
|
| 36 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 37 |
+
|
| 38 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
|
| 39 |
+
|
| 40 |
+
# Configure class labels
|
| 41 |
+
model.config.id2label = {
|
| 42 |
+
0: "Human",
|
| 43 |
+
1: "AI"
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
model.to(device)
|
| 47 |
+
model.eval()
|
| 48 |
+
|
| 49 |
+
print("Number of classes:", model.config.num_labels)
|
| 50 |
+
print("Label mapping:", model.config.id2label)
|
| 51 |
+
```
|
| 52 |
+
|
| 53 |
+
### 3. Prediction Function
|
| 54 |
+
|
| 55 |
+
The `predict_code()` function takes source code as input and returns the predicted label, class ID, and probability scores.
|
| 56 |
+
|
| 57 |
+
```python
|
| 58 |
+
def predict_code(code: str) -> dict:
|
| 59 |
+
if not isinstance(code, str) or not code.strip():
|
| 60 |
+
raise ValueError("Please provide a non-empty code string.")
|
| 61 |
+
|
| 62 |
+
# Tokenize source code
|
| 63 |
+
inputs = tokenizer(
|
| 64 |
+
code,
|
| 65 |
+
truncation=True,
|
| 66 |
+
max_length=MAX_LENGTH,
|
| 67 |
+
padding=True,
|
| 68 |
+
return_tensors="pt",
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
# Move inputs to the selected device
|
| 72 |
+
inputs = {
|
| 73 |
+
key: value.to(device)
|
| 74 |
+
for key, value in inputs.items()
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
# Perform inference
|
| 78 |
+
with torch.inference_mode():
|
| 79 |
+
outputs = model(**inputs)
|
| 80 |
+
probabilities = torch.softmax(outputs.logits, dim=-1)[0]
|
| 81 |
+
|
| 82 |
+
# Get predicted class
|
| 83 |
+
predicted_id = int(probabilities.argmax().item())
|
| 84 |
+
|
| 85 |
+
labels = model.config.id2label or {}
|
| 86 |
+
|
| 87 |
+
predicted_label = labels.get(
|
| 88 |
+
predicted_id,
|
| 89 |
+
labels.get(str(predicted_id), f"class_{predicted_id}")
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
# Return prediction and class probabilities
|
| 93 |
+
return {
|
| 94 |
+
"predicted_class_id": predicted_id,
|
| 95 |
+
"predicted_label": predicted_label,
|
| 96 |
+
"class_probabilities": {
|
| 97 |
+
str(labels.get(i, labels.get(str(i), f"class_{i}"))):
|
| 98 |
+
round(float(probabilities[i].item()), 4)
|
| 99 |
+
for i in range(len(probabilities))
|
| 100 |
+
},
|
| 101 |
+
}
|
| 102 |
+
```
|
| 103 |
+
|
| 104 |
+
### 4. Example Inference
|
| 105 |
+
|
| 106 |
+
```python
|
| 107 |
+
code = """
|
| 108 |
+
def add(a, b):
|
| 109 |
+
return a + b
|
| 110 |
+
"""
|
| 111 |
+
|
| 112 |
+
result = predict_code(code)
|
| 113 |
+
print(result)
|
| 114 |
+
```
|
| 115 |
+
|
| 116 |
+
### 5. Example Output
|
| 117 |
+
|
| 118 |
+
The following output is illustrative. Actual predictions and scores depend on the input and model behavior.
|
| 119 |
+
|
| 120 |
+
```python
|
| 121 |
+
{
|
| 122 |
+
'predicted_class_id': 0,
|
| 123 |
+
'predicted_label': 'Human',
|
| 124 |
+
'class_probabilities': {
|
| 125 |
+
'Human': 0.9231,
|
| 126 |
+
'AI': 0.0769
|
| 127 |
+
}
|
| 128 |
+
}
|
| 129 |
+
```
|
| 130 |
+
|
| 131 |
+
### 6. Output Description
|
| 132 |
+
|
| 133 |
+
| Field | Description |
|
| 134 |
+
|---|---|
|
| 135 |
+
| `predicted_class_id` | Predicted class index: `0` or `1`. |
|
| 136 |
+
| `predicted_label` | Predicted class: `Human` or `AI`. |
|
| 137 |
+
| `class_probabilities` | Softmax scores for each class. |
|
| 138 |
|
| 139 |
### Training hyperparameters
|
| 140 |
|