Added openlid
Browse files
app.py
CHANGED
|
@@ -11,7 +11,7 @@ all_langs = {iso[0]: (iso[1], iso[2], iso[3]) for iso in non_empty_isos} # {'Rom
|
|
| 11 |
iso1toall = {iso[1]: (iso[0], iso[2], iso[3]) for iso in non_empty_isos} # {'ro': ('Romanian', 'rum', 'ron')}
|
| 12 |
DEFAULTS = None
|
| 13 |
|
| 14 |
-
libraries = ["langdetect", "langid", "lingua-py", "pycld2", "fastlangdetect", "fasttext"]
|
| 15 |
|
| 16 |
class Detect():
|
| 17 |
def __init__(self, text: str) -> None:
|
|
@@ -56,6 +56,16 @@ class Detect():
|
|
| 56 |
long_langname = reversed_nllb_langs[language[0].replace('__label__', '')]
|
| 57 |
lang_code = all_langs[long_langname][0]
|
| 58 |
return [lang_code, round(number=probabilities[0] * 100, ndigits=2)]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
def detect_language(input_text: str, used_libraries: list[str]) -> tuple[str, str]:
|
| 61 |
"""
|
|
@@ -89,6 +99,8 @@ def detect_language(input_text: str, used_libraries: list[str]) -> tuple[str, st
|
|
| 89 |
detections.append(['fastlangdetect'] + detectinstance.fastlangdetect())
|
| 90 |
if 'fasttext' in used_libraries:
|
| 91 |
detections.append(['fasttext'] + detectinstance.fasttext())
|
|
|
|
|
|
|
| 92 |
unique_languages = list(set([x[1] for x in detections]))
|
| 93 |
print(unique_languages, detections)
|
| 94 |
return detections
|
|
|
|
| 11 |
iso1toall = {iso[1]: (iso[0], iso[2], iso[3]) for iso in non_empty_isos} # {'ro': ('Romanian', 'rum', 'ron')}
|
| 12 |
DEFAULTS = None
|
| 13 |
|
| 14 |
+
libraries = ["langdetect", "langid", "lingua-py", "pycld2", "fastlangdetect", "fasttext", "openlid"]
|
| 15 |
|
| 16 |
class Detect():
|
| 17 |
def __init__(self, text: str) -> None:
|
|
|
|
| 56 |
long_langname = reversed_nllb_langs[language[0].replace('__label__', '')]
|
| 57 |
lang_code = all_langs[long_langname][0]
|
| 58 |
return [lang_code, round(number=probabilities[0] * 100, ndigits=2)]
|
| 59 |
+
def openlid(self) -> list[str, float]:
|
| 60 |
+
import fasttext
|
| 61 |
+
from huggingface_hub import hf_hub_download
|
| 62 |
+
model_path = hf_hub_download(repo_id="laurievb/OpenLID-v2", filename="model.bin")
|
| 63 |
+
model = fasttext.load_model(model_path)
|
| 64 |
+
language, probabilities = model.predict(self.text, , k=3)
|
| 65 |
+
reversed_nllb_langs = {v: k for k, v in languagecodes.nllb_language_codes.items()}
|
| 66 |
+
long_langname = reversed_nllb_langs[language[0].replace('__label__', '')]
|
| 67 |
+
lang_code = all_langs[long_langname][0]
|
| 68 |
+
return [lang_code, round(number=probabilities[0] * 100, ndigits=2)]
|
| 69 |
|
| 70 |
def detect_language(input_text: str, used_libraries: list[str]) -> tuple[str, str]:
|
| 71 |
"""
|
|
|
|
| 99 |
detections.append(['fastlangdetect'] + detectinstance.fastlangdetect())
|
| 100 |
if 'fasttext' in used_libraries:
|
| 101 |
detections.append(['fasttext'] + detectinstance.fasttext())
|
| 102 |
+
if 'openlid' in used_libraries:
|
| 103 |
+
detections.append(['openlid'] + detectinstance.openlid())
|
| 104 |
unique_languages = list(set([x[1] for x in detections]))
|
| 105 |
print(unique_languages, detections)
|
| 106 |
return detections
|