azherali's picture
Update README.md
6ada8c6 verified
|
Raw History Blame Contribute Delete
4.41 kB
metadata
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: []

CodeDetect-UniXCoder-T1

2. Load the Model

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.

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

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.

{
    '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