Text Classification
fastText
English
scikit-learn
code-classification
programming-language-detection
source-code
machine-learning
modernbert
classification
nlp
code-analysis
software-engineering
Eval Results (legacy)
Instructions to use kaushik-harsh-99/Code-Lang-Classifier with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- fastText
How to use kaushik-harsh-99/Code-Lang-Classifier with fastText:
from huggingface_hub import hf_hub_download import fasttext model = fasttext.load_model(hf_hub_download("kaushik-harsh-99/Code-Lang-Classifier", "model.bin")) - Notebooks
- Google Colab
- Kaggle
File size: 4,049 Bytes
95f644c | 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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | import json
import fasttext
import pandas as pd
from sklearn.metrics import (
accuracy_score,
classification_report,
confusion_matrix,
)
# ============================================================
# CONFIG
# ============================================================
MODEL_FILE = "fasttext_language_classifier.bin"
VALIDATION_FILE = "dataset/validation.jsonl"
TEST_FILE = "dataset/test.jsonl"
# ============================================================
# LOAD MODEL
# ============================================================
print("Loading model...")
model = fasttext.load_model(MODEL_FILE)
print("Model loaded.")
# ============================================================
# EVALUATION
# ============================================================
def evaluate_jsonl(
model,
jsonl_file,
split_name,
):
print(f"\nEvaluating {split_name}")
y_true = []
y_pred = []
processed = 0
with open(
jsonl_file,
"r",
encoding="utf-8",
) as f:
for line in f:
row = json.loads(line)
true_label = row["label"]
# Match FastText training format
text = " ".join(
row["content"].split()
)
labels, probs = model.predict(
text,
k=1,
)
pred_label = (
labels[0]
.replace("__label__", "")
)
y_true.append(true_label)
y_pred.append(pred_label)
processed += 1
if processed % 5000 == 0:
print(
f"Processed {processed:,}"
)
# ========================================================
# ACCURACY
# ========================================================
acc = accuracy_score(
y_true,
y_pred,
)
print(
f"\n{split_name} Accuracy: "
f"{acc:.6f}"
)
# ========================================================
# CLASSIFICATION REPORT
# ========================================================
report = classification_report(
y_true,
y_pred,
output_dict=True,
digits=4,
)
report_df = (
pd.DataFrame(report)
.transpose()
)
report_csv = (
f"{split_name}_classification_report.csv"
)
report_df.to_csv(report_csv)
print(f"Saved {report_csv}")
# ========================================================
# CONFUSION MATRIX
# ========================================================
labels_sorted = sorted(
list(set(y_true))
)
cm = confusion_matrix(
y_true,
y_pred,
labels=labels_sorted,
)
cm_df = pd.DataFrame(
cm,
index=labels_sorted,
columns=labels_sorted,
)
cm_csv = (
f"{split_name}_confusion_matrix.csv"
)
cm_df.to_csv(cm_csv)
print(f"Saved {cm_csv}")
return acc
# ============================================================
# VALIDATION
# ============================================================
validation_accuracy = evaluate_jsonl(
model,
VALIDATION_FILE,
"validation",
)
# ============================================================
# TEST
# ============================================================
test_accuracy = evaluate_jsonl(
model,
TEST_FILE,
"test",
)
# ============================================================
# SUMMARY
# ============================================================
summary = pd.DataFrame([
{
"validation_accuracy": validation_accuracy,
"test_accuracy": test_accuracy,
}
])
summary.to_csv(
"fasttext_summary.csv",
index=False,
)
print("\nSaved fasttext_summary.csv")
print("\n==============================")
print(f"Validation Accuracy: {validation_accuracy:.6f}")
print(f"Test Accuracy: {test_accuracy:.6f}")
print("==============================")
print("\nDone.") |