Views
No views yet
1from typing import List
2import re
3from huggingface_hub import hf_hub_download
4import fasttext
5
6
7model_hf = fasttext.load_model(hf_hub_download("kenhktsui/code-natural-language-fasttext-classifier", "model.bin")) # "model_quantized.bin" for quantized version
8
9
10def replace_newlines(text: str) -> str:
11 return re.sub("\n+", " ", text)
12
13
14def predict(text_list: List[str]) -> List[dict]:
15 text_list = [replace_newlines(text) for text in text_list]
16 pred = model.predict(text_list)
17 return [{"label": l[0].lstrip("__label__"), "score": s[0]}
18 for l, s in zip(*pred)]
19
20
21predict([
22 """This is a lightning fast model, which can classify at throughtput of 2000 doc/s with CPU""",
23 """import torch""",
24 """Short text won't work"""
25])
26# [{'label': 'NaturalLanguage', 'score': 0.96747404},
27# {'label': 'Code', 'score': 1.00001},
28# {'label': 'Code', 'score': 1.000009}] precision recall f1-score support
Code 0.97 1.00 0.98 581282
NaturalLanguage 1.00 0.92 0.95 228993
accuracy 0.98 810275
macro avg 0.98 0.96 0.97 810275
weighted avg 0.98 0.98 0.98 810275 precision recall f1-score support
Code 0.95 1.00 0.97 581282
NaturalLanguage 1.00 0.86 0.93 228993
micro avg 0.96 0.96 0.96 810275
macro avg 0.97 0.93 0.95 810275
weighted avg 0.96 0.96 0.96 810275{'Assembly',
'Batchfile',
'C',
'C#',
'C++',
'CMake',
'CSS',
'Dockerfile',
'FORTRAN',
'GO',
'HTML',
'Haskell',
'Java',
'JavaScript',
'Julia',
'Lua',
'Makefile',
'PHP',
'Perl',
'PowerShell',
'Python',
'Ruby',
'Rust',
'SQL',
'Scala',
'Shell',
'TeX',
'TypeScript',
'Visual Basic'}@misc{ktsui2024codenaturallanguagefasttextclassifier,
title={Code Natural Language FastText Classifier},
author={Ken Tsui},
year={2024},
}