Commit ·
f08fc88
1
Parent(s): 90d549b
update app.py
Browse files
README.md
CHANGED
|
@@ -1,13 +1,14 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
emoji: 📐
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: streamlit
|
| 7 |
sdk_version: 1.27.2
|
| 8 |
app_file: app.py
|
| 9 |
pinned: true
|
| 10 |
-
tags:
|
|
|
|
| 11 |
---
|
| 12 |
|
| 13 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Language identification comparison
|
| 3 |
emoji: 📐
|
| 4 |
+
colorFrom: indigo
|
| 5 |
+
colorTo: blue
|
| 6 |
sdk: streamlit
|
| 7 |
sdk_version: 1.27.2
|
| 8 |
app_file: app.py
|
| 9 |
pinned: true
|
| 10 |
+
tags:
|
| 11 |
+
- multilingual
|
| 12 |
---
|
| 13 |
|
| 14 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
CHANGED
|
@@ -69,7 +69,7 @@ def preprocess_text(text):
|
|
| 69 |
replace_by = " "
|
| 70 |
replacement_map = {
|
| 71 |
ord(c): replace_by
|
| 72 |
-
for c in ':#{|}' + string.digits
|
| 73 |
}
|
| 74 |
text = text.translate(replacement_map)
|
| 75 |
|
|
@@ -141,20 +141,18 @@ def convert_df(df):
|
|
| 141 |
|
| 142 |
|
| 143 |
@st.cache_resource
|
| 144 |
-
def
|
| 145 |
model_path = hf_hub_download(repo_id=model_name, filename=file_name)
|
| 146 |
model = fasttext.load_model(model_path)
|
| 147 |
return model
|
| 148 |
|
| 149 |
-
@st.cache_resource
|
| 150 |
-
def load_GlotLID_v2(model_name, file_name):
|
| 151 |
-
model_path = hf_hub_download(repo_id=model_name, filename=file_name)
|
| 152 |
-
model = fasttext.load_model(model_path)
|
| 153 |
-
return model
|
| 154 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
|
| 156 |
-
model_1 = load_GlotLID_v1(constants.MODEL_NAME, "model_v1.bin")
|
| 157 |
-
model_2 = load_GlotLID_v2(constants.MODEL_NAME, "model_v2.bin")
|
| 158 |
|
| 159 |
# @st.cache_resource
|
| 160 |
def plot(label, prob):
|
|
@@ -179,7 +177,7 @@ def plot(label, prob):
|
|
| 179 |
ax.set_xlabel("Confidence", color=BLACK_COLOR)
|
| 180 |
st.pyplot(fig)
|
| 181 |
|
| 182 |
-
def compute(sentences, version = '
|
| 183 |
"""Computes the language probablities and labels for the given sentences.
|
| 184 |
|
| 185 |
Args:
|
|
@@ -189,7 +187,18 @@ def compute(sentences, version = 'v2'):
|
|
| 189 |
A list of language probablities and labels for the given sentences.
|
| 190 |
"""
|
| 191 |
progress_text = "Computing Language..."
|
| 192 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
my_bar = st.progress(0, text=progress_text)
|
| 194 |
|
| 195 |
probs = []
|
|
@@ -201,12 +210,12 @@ def compute(sentences, version = 'v2'):
|
|
| 201 |
|
| 202 |
output = model_choice.predict(sent)
|
| 203 |
|
| 204 |
-
output_label = output[0][0].split('__')[-1]
|
| 205 |
output_prob = max(min(output[1][0], 1), 0)
|
| 206 |
output_label_language = output_label.split('_')[0]
|
| 207 |
|
| 208 |
# script control
|
| 209 |
-
if version in ['v2'] and output_label_language!= 'zxx':
|
| 210 |
main_script, all_scripts = get_script(sent)
|
| 211 |
output_label_script = output_label.split('_')[1]
|
| 212 |
|
|
@@ -226,13 +235,14 @@ def compute(sentences, version = 'v2'):
|
|
| 226 |
my_bar.empty()
|
| 227 |
return probs, labels
|
| 228 |
|
| 229 |
-
st.markdown("[](https://huggingface.co/spaces/cis-lmu/glotlid-space?duplicate=true)")
|
| 230 |
|
| 231 |
-
render_svg(open("assets/glotlid_logo.svg").read())
|
| 232 |
|
| 233 |
render_metadata()
|
| 234 |
|
| 235 |
-
st.markdown("**GlotLID** is an open-source language identification model with support for more than **
|
|
|
|
| 236 |
|
| 237 |
|
| 238 |
tab1, tab2 = st.tabs(["Input a Sentence", "Upload a File"])
|
|
@@ -247,9 +257,9 @@ with tab1:
|
|
| 247 |
|
| 248 |
version = st.radio(
|
| 249 |
"Choose model",
|
| 250 |
-
["v1", "v2"],
|
| 251 |
-
captions=["GlotLID version 1", "GlotLID version 2
|
| 252 |
-
index =
|
| 253 |
key = 'version_tab1',
|
| 254 |
horizontal = True
|
| 255 |
)
|
|
@@ -286,9 +296,9 @@ with tab2:
|
|
| 286 |
|
| 287 |
version = st.radio(
|
| 288 |
"Choose model",
|
| 289 |
-
["v1", "v2"],
|
| 290 |
-
captions=["GlotLID version 1", "GlotLID version 2 (more data and languages)"],
|
| 291 |
-
index =
|
| 292 |
key = 'version_tab2',
|
| 293 |
horizontal = True
|
| 294 |
)
|
|
|
|
| 69 |
replace_by = " "
|
| 70 |
replacement_map = {
|
| 71 |
ord(c): replace_by
|
| 72 |
+
for c in ':•#{|}' + string.digits
|
| 73 |
}
|
| 74 |
text = text.translate(replacement_map)
|
| 75 |
|
|
|
|
| 141 |
|
| 142 |
|
| 143 |
@st.cache_resource
|
| 144 |
+
def load_model(model_name, file_name):
|
| 145 |
model_path = hf_hub_download(repo_id=model_name, filename=file_name)
|
| 146 |
model = fasttext.load_model(model_path)
|
| 147 |
return model
|
| 148 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
+
model_1 = load_model(constants.MODEL_NAME, "model_v1.bin")
|
| 151 |
+
model_2 = load_model(constants.MODEL_NAME, "model_v2.bin")
|
| 152 |
+
model_3 = load_model(constants.MODEL_NAME, "model_v3.bin")
|
| 153 |
+
openlid = load_model('laurievb/OpenLID', "model.bin")
|
| 154 |
+
nllb = load_model('facebook/fasttext-language-identification', "model.bin")
|
| 155 |
|
|
|
|
|
|
|
| 156 |
|
| 157 |
# @st.cache_resource
|
| 158 |
def plot(label, prob):
|
|
|
|
| 177 |
ax.set_xlabel("Confidence", color=BLACK_COLOR)
|
| 178 |
st.pyplot(fig)
|
| 179 |
|
| 180 |
+
def compute(sentences, version = 'v3'):
|
| 181 |
"""Computes the language probablities and labels for the given sentences.
|
| 182 |
|
| 183 |
Args:
|
|
|
|
| 187 |
A list of language probablities and labels for the given sentences.
|
| 188 |
"""
|
| 189 |
progress_text = "Computing Language..."
|
| 190 |
+
|
| 191 |
+
if version == 'nllb-218':
|
| 192 |
+
model_choice = nllb
|
| 193 |
+
elif version == 'openlid-201':
|
| 194 |
+
model_choice = openlid
|
| 195 |
+
elif version == 'v3':
|
| 196 |
+
model_choice = model_3
|
| 197 |
+
elif version == 'v2':
|
| 198 |
+
model_choice = model_2
|
| 199 |
+
else:
|
| 200 |
+
model_choice = model_1
|
| 201 |
+
|
| 202 |
my_bar = st.progress(0, text=progress_text)
|
| 203 |
|
| 204 |
probs = []
|
|
|
|
| 210 |
|
| 211 |
output = model_choice.predict(sent)
|
| 212 |
|
| 213 |
+
output_label = output[0][0].split('__')[-1].replace('_Hans', '_Hani').replace('_Hant', '_Hani')
|
| 214 |
output_prob = max(min(output[1][0], 1), 0)
|
| 215 |
output_label_language = output_label.split('_')[0]
|
| 216 |
|
| 217 |
# script control
|
| 218 |
+
if version in ['v3', 'v2', 'openlid-201', 'nllb-218'] and output_label_language!= 'zxx':
|
| 219 |
main_script, all_scripts = get_script(sent)
|
| 220 |
output_label_script = output_label.split('_')[1]
|
| 221 |
|
|
|
|
| 235 |
my_bar.empty()
|
| 236 |
return probs, labels
|
| 237 |
|
| 238 |
+
# st.markdown("[](https://huggingface.co/spaces/cis-lmu/glotlid-space?duplicate=true)")
|
| 239 |
|
| 240 |
+
# render_svg(open("assets/glotlid_logo.svg").read())
|
| 241 |
|
| 242 |
render_metadata()
|
| 243 |
|
| 244 |
+
st.markdown("**GlotLID** is an open-source language identification model with support for more than **2000 languages (V3)**.")
|
| 245 |
+
|
| 246 |
|
| 247 |
|
| 248 |
tab1, tab2 = st.tabs(["Input a Sentence", "Upload a File"])
|
|
|
|
| 257 |
|
| 258 |
version = st.radio(
|
| 259 |
"Choose model",
|
| 260 |
+
["nllb-218", "openlid-201", "v1", "v2", "v3"],
|
| 261 |
+
captions=["NLLB", "OpenLID", "GlotLID version 1", "GlotLID version 2", "GlotLID version 3 (More languages, better quality data)"],
|
| 262 |
+
index = 4,
|
| 263 |
key = 'version_tab1',
|
| 264 |
horizontal = True
|
| 265 |
)
|
|
|
|
| 296 |
|
| 297 |
version = st.radio(
|
| 298 |
"Choose model",
|
| 299 |
+
["nllb-218", "openlid-201", "v1", "v2", "v3"],
|
| 300 |
+
captions=["NLLB", "OpenLID", "GlotLID version 1", "GlotLID version 2 (more data and languages), GlotLID version 3 (More languages, better quality data)"],
|
| 301 |
+
index = 4,
|
| 302 |
key = 'version_tab2',
|
| 303 |
horizontal = True
|
| 304 |
)
|