mirror of
https://github.com/vladmandic/sdnext.git
synced 2026-01-27 15:02:48 +03:00
25 lines
1.1 KiB
Python
25 lines
1.1 KiB
Python
repo_id = "facebook/fasttext-language-identification"
|
|
model = None
|
|
|
|
|
|
def lang_detect(text:str, top:int=1, threshold:float=0.25) -> str:
|
|
try:
|
|
global model # pylint: disable=global-statement
|
|
from modules import shared
|
|
if model is None:
|
|
from installer import install
|
|
install("fasttext")
|
|
import fasttext
|
|
from huggingface_hub import hf_hub_download
|
|
model_path = hf_hub_download(repo_id, filename="model.bin", cache_dir=shared.opts.hfcache_dir)
|
|
shared.log.info(f'NudeNet load: model="{repo_id}"')
|
|
model = fasttext.load_model(model_path)
|
|
text = text.replace('\n', '. ')
|
|
lang, score = model.predict(text, k=top, threshold=threshold, on_unicode_error="ignore")
|
|
result = [f'{l.replace("__label__", "").lower()}:{s:.2f}' for l, s in zip(lang, score) if s > threshold][:top]
|
|
shared.log.debug(f'NudeNet LangDetect: {result}')
|
|
return result
|
|
except Exception as e:
|
|
shared.log.error(f'NudeNet LangDetect: {e}')
|
|
return str(e)
|