Update README.md
Browse files
README.md
CHANGED
|
@@ -13,24 +13,27 @@ tags:
|
|
| 13 |
|
| 14 |
# SourceCodeAuthorCheck-SLM-10M
|
| 15 |
|
|
|
|
|
|
|
| 16 |
A ~10 million parameter Small Language Model (SLM) Transformer designed for binary classification to detect whether Python source code was written by a human or generated by AI.
|
| 17 |
|
| 18 |
The model was trained on a dataset of human-written code extracted from GitHub (Q3 2017 and prior) and synthetic AI-generated code mimicking Q3 2026 generative AI paradigms.
|
| 19 |
|
| 20 |
-
##
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
* **Tokenizer:** GPT-2 (`gpt2`)
|
| 24 |
-
* **Hardware:** Trained on NVIDIA DGX Spark (Grace Blackwell) using mixed precision (bfloat16)
|
| 25 |
|
| 26 |
## Usage
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
| 29 |
|
| 30 |
```python
|
| 31 |
import torch
|
| 32 |
import torch.nn as nn
|
| 33 |
from transformers import AutoTokenizer
|
|
|
|
| 34 |
|
| 35 |
class SourceCodeAuthorCheck(nn.Module):
|
| 36 |
def __init__(self, vocab_size=50257, d_model=128, nhead=8, num_layers=4, dim_feedforward=512):
|
|
@@ -57,36 +60,53 @@ class SourceCodeAuthorCheck(nn.Module):
|
|
| 57 |
pooled = sum_embeddings / sum_mask
|
| 58 |
|
| 59 |
return self.fc(pooled)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
-
|
|
|
|
| 62 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 63 |
tokenizer = AutoTokenizer.from_pretrained("gpt2")
|
| 64 |
tokenizer.pad_token = tokenizer.eos_token
|
| 65 |
|
|
|
|
| 66 |
model = SourceCodeAuthorCheck().to(device)
|
| 67 |
-
|
|
|
|
| 68 |
model.eval()
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
# 3. Inference
|
| 85 |
-
with torch.no_grad():
|
| 86 |
-
with torch.autocast(device_type='cuda', dtype=torch.bfloat16):
|
| 87 |
-
logits = model(inputs['input_ids'], inputs['attention_mask'])
|
| 88 |
prob = torch.sigmoid(logits).item()
|
| 89 |
|
| 90 |
-
|
| 91 |
-
print("Verdict:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
```
|
|
|
|
| 13 |
|
| 14 |
# SourceCodeAuthorCheck-SLM-10M
|
| 15 |
|
| 16 |
+
[](https://huggingface.co/spaces/assix-research/SourceCodeAuthorCheck-UI)
|
| 17 |
+
|
| 18 |
A ~10 million parameter Small Language Model (SLM) Transformer designed for binary classification to detect whether Python source code was written by a human or generated by AI.
|
| 19 |
|
| 20 |
The model was trained on a dataset of human-written code extracted from GitHub (Q3 2017 and prior) and synthetic AI-generated code mimicking Q3 2026 generative AI paradigms.
|
| 21 |
|
| 22 |
+
## Live Demo
|
| 23 |
+
|
| 24 |
+
You can test the model interactively without writing any code by visiting our **[Gradio Web UI Space](https://huggingface.co/spaces/assix-research/SourceCodeAuthorCheck-UI)**.
|
|
|
|
|
|
|
| 25 |
|
| 26 |
## Usage
|
| 27 |
|
| 28 |
+
To use this model locally, you must include the model architecture class in your script before loading the weights.
|
| 29 |
+
|
| 30 |
+
### 1. Define the Architecture
|
| 31 |
|
| 32 |
```python
|
| 33 |
import torch
|
| 34 |
import torch.nn as nn
|
| 35 |
from transformers import AutoTokenizer
|
| 36 |
+
from huggingface_hub import hf_hub_download
|
| 37 |
|
| 38 |
class SourceCodeAuthorCheck(nn.Module):
|
| 39 |
def __init__(self, vocab_size=50257, d_model=128, nhead=8, num_layers=4, dim_feedforward=512):
|
|
|
|
| 60 |
pooled = sum_embeddings / sum_mask
|
| 61 |
|
| 62 |
return self.fc(pooled)
|
| 63 |
+
```
|
| 64 |
+
|
| 65 |
+
### 2. Load Weights and Run Inference
|
| 66 |
+
|
| 67 |
+
Once the class is defined, you can automatically pull the weights from Hugging Face and run inference cleanly using this helper function:
|
| 68 |
|
| 69 |
+
```python
|
| 70 |
+
# Initialize device and tokenizer
|
| 71 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 72 |
tokenizer = AutoTokenizer.from_pretrained("gpt2")
|
| 73 |
tokenizer.pad_token = tokenizer.eos_token
|
| 74 |
|
| 75 |
+
# Download and load model weights from the Hugging Face Hub
|
| 76 |
model = SourceCodeAuthorCheck().to(device)
|
| 77 |
+
model_path = hf_hub_download(repo_id="assix-research/SourceCodeAuthorCheck-SLM-10M", filename="source_code_classifier.pth")
|
| 78 |
+
model.load_state_dict(torch.load(model_path, map_location=device, weights_only=True))
|
| 79 |
model.eval()
|
| 80 |
|
| 81 |
+
def predict_source(code_snippet):
|
| 82 |
+
"""Predicts whether a given Python snippet is human-written or AI-generated."""
|
| 83 |
+
inputs = tokenizer(
|
| 84 |
+
code_snippet,
|
| 85 |
+
return_tensors="pt",
|
| 86 |
+
truncation=True,
|
| 87 |
+
padding="max_length",
|
| 88 |
+
max_length=1024
|
| 89 |
+
).to(device)
|
| 90 |
|
| 91 |
+
with torch.no_grad():
|
| 92 |
+
if torch.cuda.is_available():
|
| 93 |
+
with torch.autocast(device_type='cuda', dtype=torch.bfloat16):
|
| 94 |
+
logits = model(inputs['input_ids'], inputs['attention_mask'])
|
| 95 |
+
else:
|
| 96 |
+
logits = model(inputs['input_ids'], inputs['attention_mask'])
|
| 97 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
prob = torch.sigmoid(logits).item()
|
| 99 |
|
| 100 |
+
verdict = "AI Generated" if prob > 0.5 else "Human Written"
|
| 101 |
+
print(f"Verdict: {verdict} (AI Probability: {prob:.1%})")
|
| 102 |
+
|
| 103 |
+
# --- Example Usage ---
|
| 104 |
+
sample_code = """
|
| 105 |
+
def calculate_factorial(n):
|
| 106 |
+
if n == 0:
|
| 107 |
+
return 1
|
| 108 |
+
return n * calculate_factorial(n-1)
|
| 109 |
+
"""
|
| 110 |
+
|
| 111 |
+
predict_source(sample_code)
|
| 112 |
```
|