azherali commited on
Commit
6ada8c6
·
verified ·
1 Parent(s): 072ad79

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +116 -21
README.md CHANGED
@@ -19,27 +19,122 @@ should probably proofread and complete it, then remove this comment. -->
19
 
20
  # CodeDetect-UniXCoder-T1
21
 
22
- This model is a fine-tuned version of [microsoft/unixcoder-base](https://huggingface.co/microsoft/unixcoder-base) on the None dataset.
23
- It achieves the following results on the evaluation set:
24
- - Loss: 0.1039
25
- - Accuracy: 0.9799
26
- - F1: 0.9799
27
- - Precision: 0.9800
28
- - Recall: 0.9799
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-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