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
File size: 4,405 Bytes
9496266 6ada8c6 9496266 6ada8c6 9496266 6ada8c6 9496266 6ada8c6 9496266 6ada8c6 9496266 6ada8c6 9496266 6ada8c6 9496266 6ada8c6 9496266 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | ---
library_name: transformers
license: apache-2.0
base_model: microsoft/unixcoder-base
tags:
- generated_from_trainer
metrics:
- accuracy
- f1
- precision
- recall
model-index:
- name: CodeDetect-UniXCoder-T1
results: []
---
<!-- This model card has been generated automatically according to the information the Trainer had access to. You
should probably proofread and complete it, then remove this comment. -->
# CodeDetect-UniXCoder-T1
### 2. Load the Model
```python
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification
MODEL_NAME = "azherali/CodeDetect-UniXCoder-T1"
MAX_LENGTH = 512
# Select device
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print("Device:", device)
# Load tokenizer and model
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
# Configure class labels
model.config.id2label = {
0: "Human",
1: "AI"
}
model.to(device)
model.eval()
print("Number of classes:", model.config.num_labels)
print("Label mapping:", model.config.id2label)
```
### 3. Prediction Function
The `predict_code()` function takes source code as input and returns the predicted label, class ID, and probability scores.
```python
def predict_code(code: str) -> dict:
if not isinstance(code, str) or not code.strip():
raise ValueError("Please provide a non-empty code string.")
# Tokenize source code
inputs = tokenizer(
code,
truncation=True,
max_length=MAX_LENGTH,
padding=True,
return_tensors="pt",
)
# Move inputs to the selected device
inputs = {
key: value.to(device)
for key, value in inputs.items()
}
# Perform inference
with torch.inference_mode():
outputs = model(**inputs)
probabilities = torch.softmax(outputs.logits, dim=-1)[0]
# Get predicted class
predicted_id = int(probabilities.argmax().item())
labels = model.config.id2label or {}
predicted_label = labels.get(
predicted_id,
labels.get(str(predicted_id), f"class_{predicted_id}")
)
# Return prediction and class probabilities
return {
"predicted_class_id": predicted_id,
"predicted_label": predicted_label,
"class_probabilities": {
str(labels.get(i, labels.get(str(i), f"class_{i}"))):
round(float(probabilities[i].item()), 4)
for i in range(len(probabilities))
},
}
```
### 4. Example Inference
```python
code = """
def add(a, b):
return a + b
"""
result = predict_code(code)
print(result)
```
### 5. Example Output
The following output is illustrative. Actual predictions and scores depend on the input and model behavior.
```python
{
'predicted_class_id': 0,
'predicted_label': 'Human',
'class_probabilities': {
'Human': 0.9231,
'AI': 0.0769
}
}
```
### 6. Output Description
| Field | Description |
|---|---|
| `predicted_class_id` | Predicted class index: `0` or `1`. |
| `predicted_label` | Predicted class: `Human` or `AI`. |
| `class_probabilities` | Softmax scores for each class. |
### Training hyperparameters
The following hyperparameters were used during training:
- learning_rate: 2e-05
- train_batch_size: 64
- eval_batch_size: 64
- seed: 42
- optimizer: Use OptimizerNames.ADAMW_TORCH_FUSED with betas=(0.9,0.999) and epsilon=1e-08 and optimizer_args=No additional optimizer arguments
- lr_scheduler_type: linear
- lr_scheduler_warmup_steps: 0.1
- num_epochs: 5
- mixed_precision_training: Native AMP
### Training results
| Training Loss | Epoch | Step | Validation Loss | Accuracy | F1 | Precision | Recall |
|:-------------:|:-----:|:-----:|:---------------:|:--------:|:------:|:---------:|:------:|
| 0.0707 | 1.0 | 16897 | 0.0631 | 0.9770 | 0.9770 | 0.9768 | 0.9772 |
| 0.0526 | 2.0 | 33794 | 0.0553 | 0.9795 | 0.9794 | 0.9794 | 0.9794 |
| 0.0366 | 3.0 | 50691 | 0.0655 | 0.9791 | 0.9790 | 0.9793 | 0.9789 |
| 0.0232 | 4.0 | 67588 | 0.0852 | 0.9792 | 0.9792 | 0.9792 | 0.9791 |
| 0.0111 | 5.0 | 84485 | 0.1039 | 0.9799 | 0.9799 | 0.9800 | 0.9799 |
### Framework versions
- Transformers 5.13.1
- Pytorch 2.11.0+cu130
- Datasets 4.0.0
- Tokenizers 0.22.2
|