Spaces:
Sleeping
feat(ui): single ranked prediction list, metadata-only family/superlanguage
Browse filesThe model emits one softmax over 128 language codes. Family and
superlanguage were never separate heads, so per-group percentages were
just re-aggregations of the same distribution and misleading. Drop those
tables and present a single ranked list instead.
- Results are now rendered as pills: language name + code, with
superlanguage and family as small italic context underneath. The top
match is highlighted (larger, accented), the rest are muted.
- No table header row, so the bilingual "Prediction / Predicción"
headers are gone; the section title comes from i18n instead.
- Renamed i18n keys to the ones actually used; removed the family,
superlanguage and code-table keys.
- Advanced Options collapsed from three groups to one (Top-K + threshold
for the ranked list).
- Subtitle now states the real scope: this model covers N of the 364
catalogue variants of the 68 indigenous languages of Mexico, instead
of implying Mexico has N variants.
- Curated cards keep only the ground-truth language label.
- requirements.txt: pandas no longer needed now that Dataframe is gone.
- README.md +5 -3
- app.py +82 -88
- requirements.txt +0 -1
|
@@ -18,7 +18,8 @@ Identify language family, superlanguage, and specific language code from audio f
|
|
| 18 |
## 🚀 How to use
|
| 19 |
1. Upload an audio file (WAV or MP3) or use the microphone to record.
|
| 20 |
2. Click **Classify Language**.
|
| 21 |
-
3. View the
|
|
|
|
| 22 |
|
| 23 |
## 🧠 What changed in 2.0
|
| 24 |
Version 1 used a Whisper encoder-only classifier. Version 2 uses a much smaller
|
|
@@ -29,8 +30,9 @@ parameters (~85 MB) which makes it viable on CPU-only hardware, where the Space
|
|
| 29 |
## 📊 Model Details
|
| 30 |
- **Architecture**: ECAPA-TDNN (SpeechBrain) + cosine head, temperature-calibrated
|
| 31 |
- **Input**: 16 kHz mono audio, 6 s sliding windows for clips longer than 10 s
|
| 32 |
-
- **Output**: 128 ISO 639-3 language codes
|
| 33 |
-
the label metadata, not predicted
|
|
|
|
| 34 |
- **Scope**: indigenous languages of Mexico (Mesoamerica)
|
| 35 |
- **Metrics** (test split, calibrated):
|
| 36 |
- Family: ~96.6%
|
|
|
|
| 18 |
## 🚀 How to use
|
| 19 |
1. Upload an audio file (WAV or MP3) or use the microphone to record.
|
| 20 |
2. Click **Classify Language**.
|
| 21 |
+
3. View the ranked predictions: the best match is highlighted, with its superlanguage and
|
| 22 |
+
family shown as metadata. Top-K and the confidence threshold live in *Advanced Options*.
|
| 23 |
|
| 24 |
## 🧠 What changed in 2.0
|
| 25 |
Version 1 used a Whisper encoder-only classifier. Version 2 uses a much smaller
|
|
|
|
| 30 |
## 📊 Model Details
|
| 31 |
- **Architecture**: ECAPA-TDNN (SpeechBrain) + cosine head, temperature-calibrated
|
| 32 |
- **Input**: 16 kHz mono audio, 6 s sliding windows for clips longer than 10 s
|
| 33 |
+
- **Output**: 128 ISO 639-3 language codes. The model predicts the *code only*;
|
| 34 |
+
superlanguage and family are attached from the label metadata as context, not predicted
|
| 35 |
+
separately, so no separate percentages are shown for them.
|
| 36 |
- **Scope**: indigenous languages of Mexico (Mesoamerica)
|
| 37 |
- **Metrics** (test split, calibrated):
|
| 38 |
- Family: ~96.6%
|
|
@@ -1,13 +1,13 @@
|
|
| 1 |
import os
|
| 2 |
import gc
|
| 3 |
import base64
|
|
|
|
| 4 |
import json
|
| 5 |
import re
|
| 6 |
|
| 7 |
import gradio as gr
|
| 8 |
import librosa
|
| 9 |
import numpy as np
|
| 10 |
-
import pandas as pd
|
| 11 |
import torch
|
| 12 |
import psutil
|
| 13 |
|
|
@@ -15,62 +15,30 @@ from lid_ecapa import TachiwinLID, LID_MODEL_REPO
|
|
| 15 |
|
| 16 |
i18n = gr.I18n(
|
| 17 |
en={
|
| 18 |
-
"subtitle": "Identify the indigenous language variants of Mexico covered by this model",
|
| 19 |
"input_audio_header": "### 🎙️ 1. Input Audio",
|
| 20 |
"upload_or_record_label": "Upload or Record",
|
| 21 |
"advanced_options": "⚙️ Advanced Options",
|
| 22 |
-
"
|
| 23 |
"top_k_label": "Top-K",
|
| 24 |
"threshold_label": "Threshold",
|
| 25 |
-
"superlanguage": "#### Superlanguage",
|
| 26 |
-
"language_code": "#### Language Code",
|
| 27 |
"clear_btn": "🗑️ Clear",
|
| 28 |
"classify_btn": "🚀 Classify",
|
| 29 |
"results_header": "### 📊 2. Classification Results",
|
| 30 |
-
"pred_fam_label": "🌍 Language Family",
|
| 31 |
-
"pred_super_label": "🗣️ Superlanguage",
|
| 32 |
-
"pred_code_label": "🔤 Language Code",
|
| 33 |
"curated_header": "### 📋 Curated Samples for Reference",
|
| 34 |
"curated_desc": "Select any of the sample results below to load the corresponding audio for inference.",
|
| 35 |
-
"prediction_header": "Prediction",
|
| 36 |
-
"confidence_header": "Confidence",
|
| 37 |
-
"col_sample": "🔊 Sample",
|
| 38 |
-
"col_pred_fam": "Pred Family",
|
| 39 |
-
"col_pred_super": "Pred Super",
|
| 40 |
-
"col_pred_code": "Pred Code",
|
| 41 |
-
"col_act_fam": "Actual Family",
|
| 42 |
-
"col_act_super": "Actual Super",
|
| 43 |
-
"col_act_code": "Actual Code",
|
| 44 |
-
"col_match": "Match"
|
| 45 |
},
|
| 46 |
es={
|
| 47 |
-
"subtitle": "Identifica las variantes de lenguas originarias de México cubiertas por este modelo",
|
| 48 |
"input_audio_header": "### 🎙️ 1. Entrada de Audio",
|
| 49 |
"upload_or_record_label": "Subir o Grabar",
|
| 50 |
"advanced_options": "⚙️ Opciones Avanzadas",
|
| 51 |
-
"
|
| 52 |
"top_k_label": "Top-K",
|
| 53 |
"threshold_label": "Umbral",
|
| 54 |
-
"superlanguage": "#### Superlengua",
|
| 55 |
-
"language_code": "#### Código de Lengua",
|
| 56 |
"clear_btn": "🗑️ Limpiar",
|
| 57 |
"classify_btn": "🚀 Clasificar",
|
| 58 |
"results_header": "### 📊 2. Resultados de Clasificación",
|
| 59 |
-
"pred_fam_label": "🌍 Familia Lingüística",
|
| 60 |
-
"pred_super_label": "🗣️ Superlengua",
|
| 61 |
-
"pred_code_label": "🔤 Código de Lengua",
|
| 62 |
"curated_header": "### 📋 Muestras Curadas de Referencia",
|
| 63 |
"curated_desc": "Seleccione cualquiera de los resultados de muestra a continuación para cargar el audio correspondiente para la clasificación.",
|
| 64 |
-
"prediction_header": "Predicción",
|
| 65 |
-
"confidence_header": "Confianza",
|
| 66 |
-
"col_sample": "🔊 Muestra",
|
| 67 |
-
"col_pred_fam": "Familia Pred",
|
| 68 |
-
"col_pred_super": "Super Pred",
|
| 69 |
-
"col_pred_code": "Cód Pred",
|
| 70 |
-
"col_act_fam": "Familia Real",
|
| 71 |
-
"col_act_super": "Super Real",
|
| 72 |
-
"col_act_code": "Cód Real",
|
| 73 |
-
"col_match": "Coincidencia"
|
| 74 |
}
|
| 75 |
)
|
| 76 |
|
|
@@ -79,6 +47,10 @@ MAX_AUDIO_SECONDS = 30
|
|
| 79 |
SAMPLE_RATE = 16000
|
| 80 |
UNKNOWN_GROUP = "Sin especificar / Unspecified"
|
| 81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
torch.set_num_threads(1)
|
| 83 |
|
| 84 |
# === LOAD MODEL ===
|
|
@@ -91,7 +63,8 @@ INFO = lid.info
|
|
| 91 |
N_CLASSES = len(CLASSES)
|
| 92 |
|
| 93 |
# === LABEL METADATA ===
|
| 94 |
-
#
|
|
|
|
| 95 |
try:
|
| 96 |
with open("languages.json", "r", encoding="utf-8") as f:
|
| 97 |
_languages_data = json.load(f)
|
|
@@ -111,7 +84,7 @@ except Exception as e:
|
|
| 111 |
CODE_TO_META = {}
|
| 112 |
|
| 113 |
# Manual completion for entries where labels.json ships an empty family/superlanguage.
|
| 114 |
-
# (Metadata gaps in the model repo --
|
| 115 |
META_FALLBACK = {
|
| 116 |
"chq": ("Otomangue", "Chinanteco"),
|
| 117 |
"hue": ("Huave", "Huave"),
|
|
@@ -127,7 +100,7 @@ META_FALLBACK = {
|
|
| 127 |
|
| 128 |
|
| 129 |
def _meta(code):
|
| 130 |
-
"""(
|
| 131 |
entry = INFO.get(code, {})
|
| 132 |
family = entry.get("family") or ""
|
| 133 |
superlanguage = entry.get("superlanguage") or ""
|
|
@@ -140,17 +113,16 @@ def _meta(code):
|
|
| 140 |
family = family or db_family or UNKNOWN_GROUP
|
| 141 |
superlanguage = superlanguage or db_super or UNKNOWN_GROUP
|
| 142 |
name = entry.get("language") or CODE_TO_NAME.get(code, code)
|
| 143 |
-
return
|
| 144 |
|
| 145 |
|
| 146 |
-
# code -> group, used to marginalise the code distribution into family / superlanguage
|
| 147 |
-
FAMILY_OF = {}
|
| 148 |
SUPERLANGUAGE_OF = {}
|
|
|
|
| 149 |
DISPLAY_NAME = {}
|
| 150 |
for _code in CLASSES:
|
| 151 |
-
|
| 152 |
-
FAMILY_OF[_code] = _family
|
| 153 |
SUPERLANGUAGE_OF[_code] = _super
|
|
|
|
| 154 |
DISPLAY_NAME[_code] = _name
|
| 155 |
|
| 156 |
print(
|
|
@@ -164,22 +136,61 @@ def get_mem_usage():
|
|
| 164 |
return process.memory_info().rss / (1024 ** 2)
|
| 165 |
|
| 166 |
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
|
| 180 |
|
| 181 |
# === INFERENCE FUNCTION ===
|
| 182 |
-
def predict_language(audio_path,
|
| 183 |
if not audio_path:
|
| 184 |
raise gr.Error("No audio provided! Please upload or record an audio file.")
|
| 185 |
|
|
@@ -211,20 +222,14 @@ def predict_language(audio_path, fam_k=1, fam_thresh=0.0, super_k=1, super_thres
|
|
| 211 |
print(f"[LOG] Memory after inference: {get_mem_usage():.2f} MB")
|
| 212 |
|
| 213 |
print("[LOG] Step 3: Post-processing results...")
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
code_rows = sorted(
|
| 218 |
-
((f"{DISPLAY_NAME[code]} ({code})", float(p)) for code, p in zip(CLASSES, probs)),
|
| 219 |
-
key=lambda kv: kv[1],
|
| 220 |
-
reverse=True,
|
| 221 |
-
)
|
| 222 |
-
df_code = _format_table(code_rows, code_k, code_thresh)
|
| 223 |
|
| 224 |
print(f"[LOG] Final Memory: {get_mem_usage():.2f} MB")
|
| 225 |
print("--- [LOG] Request Finished ---\n")
|
| 226 |
|
| 227 |
-
return
|
| 228 |
except gr.Error:
|
| 229 |
raise
|
| 230 |
except Exception as e:
|
|
@@ -273,12 +278,13 @@ except Exception as e:
|
|
| 273 |
# === UI COMPONENTS ===
|
| 274 |
with gr.Blocks(theme=gr.themes.Ocean()) as demo:
|
| 275 |
gr.HTML(
|
| 276 |
-
|
|
|
|
| 277 |
<div style="text-align: center; padding: 30px; background: linear-gradient(120deg, rgb(2, 132, 199) 0%, rgb(16, 185, 129) 60%, rgb(5, 150, 105) 100%); color: white; border-radius: 15px; margin-bottom: 25px; box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);">
|
| 278 |
{icon_html}
|
| 279 |
<h1 style="color: white; margin: 0; font-size: 2.5em;">🦡 Tachiwin AudioId 2.0 🦡</h1>
|
| 280 |
-
<p style="font-size: 1.2em; opacity: 0.9; margin-top: 10px;">Identify the {
|
| 281 |
-
<i>Identifica las {
|
| 282 |
</div>
|
| 283 |
"""
|
| 284 |
)
|
|
@@ -293,17 +299,7 @@ with gr.Blocks(theme=gr.themes.Ocean()) as demo:
|
|
| 293 |
)
|
| 294 |
with gr.Accordion(i18n("advanced_options"), open=False):
|
| 295 |
with gr.Group():
|
| 296 |
-
gr.Markdown(i18n("
|
| 297 |
-
with gr.Row():
|
| 298 |
-
fam_k = gr.Slider(minimum=1, maximum=10, step=1, value=1, label=i18n("top_k_label"))
|
| 299 |
-
fam_thresh = gr.Slider(minimum=0.0, maximum=1.0, step=0.05, value=0.0, label=i18n("threshold_label"))
|
| 300 |
-
with gr.Group():
|
| 301 |
-
gr.Markdown(i18n("superlanguage"))
|
| 302 |
-
with gr.Row():
|
| 303 |
-
super_k = gr.Slider(minimum=1, maximum=10, step=1, value=1, label=i18n("top_k_label"))
|
| 304 |
-
super_thresh = gr.Slider(minimum=0.0, maximum=1.0, step=0.05, value=0.0, label=i18n("threshold_label"))
|
| 305 |
-
with gr.Group():
|
| 306 |
-
gr.Markdown(i18n("language_code"))
|
| 307 |
with gr.Row():
|
| 308 |
code_k = gr.Slider(minimum=1, maximum=10, step=1, value=3, label=i18n("top_k_label"))
|
| 309 |
code_thresh = gr.Slider(minimum=0.0, maximum=1.0, step=0.05, value=0.0, label=i18n("threshold_label"))
|
|
@@ -314,20 +310,18 @@ with gr.Blocks(theme=gr.themes.Ocean()) as demo:
|
|
| 314 |
|
| 315 |
with gr.Column(scale=1):
|
| 316 |
gr.Markdown(i18n("results_header"))
|
| 317 |
-
|
| 318 |
-
super_table = gr.Dataframe(headers=["Prediction / Predicción", "Confidence / Confianza"], datatype=["str", "str"], label=i18n("pred_super_label"), interactive=False, wrap=True)
|
| 319 |
-
code_table = gr.Dataframe(headers=["Prediction / Predicción", "Confidence / Confianza"], datatype=["str", "str"], label=i18n("pred_code_label"), interactive=False, wrap=True)
|
| 320 |
|
| 321 |
submit_btn.click(
|
| 322 |
fn=predict_language,
|
| 323 |
-
inputs=[audio_input,
|
| 324 |
-
outputs=[
|
| 325 |
)
|
| 326 |
|
| 327 |
clear_btn.click(
|
| 328 |
-
fn=lambda: (None,
|
| 329 |
inputs=None,
|
| 330 |
-
outputs=[audio_input,
|
| 331 |
)
|
| 332 |
|
| 333 |
gr.Markdown(i18n("curated_header"))
|
|
@@ -339,7 +333,7 @@ with gr.Blocks(theme=gr.themes.Ocean()) as demo:
|
|
| 339 |
with gr.Row():
|
| 340 |
for i in range(row_idx, min(row_idx+columns, len(curated_audio))):
|
| 341 |
with gr.Column(scale=1):
|
| 342 |
-
gr.Markdown(f"
|
| 343 |
gr.Audio(value=curated_audio[i], interactive=False, show_label=False)
|
| 344 |
btn = gr.Button("Load / Cargar", size="sm")
|
| 345 |
btn.click(fn=lambda url=curated_audio[i]: url, inputs=[], outputs=[audio_input])
|
|
|
|
| 1 |
import os
|
| 2 |
import gc
|
| 3 |
import base64
|
| 4 |
+
import html
|
| 5 |
import json
|
| 6 |
import re
|
| 7 |
|
| 8 |
import gradio as gr
|
| 9 |
import librosa
|
| 10 |
import numpy as np
|
|
|
|
| 11 |
import torch
|
| 12 |
import psutil
|
| 13 |
|
|
|
|
| 15 |
|
| 16 |
i18n = gr.I18n(
|
| 17 |
en={
|
|
|
|
| 18 |
"input_audio_header": "### 🎙️ 1. Input Audio",
|
| 19 |
"upload_or_record_label": "Upload or Record",
|
| 20 |
"advanced_options": "⚙️ Advanced Options",
|
| 21 |
+
"prediction_options": "#### Ranked Predictions",
|
| 22 |
"top_k_label": "Top-K",
|
| 23 |
"threshold_label": "Threshold",
|
|
|
|
|
|
|
| 24 |
"clear_btn": "🗑️ Clear",
|
| 25 |
"classify_btn": "🚀 Classify",
|
| 26 |
"results_header": "### 📊 2. Classification Results",
|
|
|
|
|
|
|
|
|
|
| 27 |
"curated_header": "### 📋 Curated Samples for Reference",
|
| 28 |
"curated_desc": "Select any of the sample results below to load the corresponding audio for inference.",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
},
|
| 30 |
es={
|
|
|
|
| 31 |
"input_audio_header": "### 🎙️ 1. Entrada de Audio",
|
| 32 |
"upload_or_record_label": "Subir o Grabar",
|
| 33 |
"advanced_options": "⚙️ Opciones Avanzadas",
|
| 34 |
+
"prediction_options": "#### Predicciones Ordenadas",
|
| 35 |
"top_k_label": "Top-K",
|
| 36 |
"threshold_label": "Umbral",
|
|
|
|
|
|
|
| 37 |
"clear_btn": "🗑️ Limpiar",
|
| 38 |
"classify_btn": "🚀 Clasificar",
|
| 39 |
"results_header": "### 📊 2. Resultados de Clasificación",
|
|
|
|
|
|
|
|
|
|
| 40 |
"curated_header": "### 📋 Muestras Curadas de Referencia",
|
| 41 |
"curated_desc": "Seleccione cualquiera de los resultados de muestra a continuación para cargar el audio correspondiente para la clasificación.",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
}
|
| 43 |
)
|
| 44 |
|
|
|
|
| 47 |
SAMPLE_RATE = 16000
|
| 48 |
UNKNOWN_GROUP = "Sin especificar / Unspecified"
|
| 49 |
|
| 50 |
+
# INALI catalogue: 68 language groups / 364 variants. Used only for the honest subtitle.
|
| 51 |
+
INALI_VARIANTS = 364
|
| 52 |
+
INALI_LANGUAGES = 68
|
| 53 |
+
|
| 54 |
torch.set_num_threads(1)
|
| 55 |
|
| 56 |
# === LOAD MODEL ===
|
|
|
|
| 63 |
N_CLASSES = len(CLASSES)
|
| 64 |
|
| 65 |
# === LABEL METADATA ===
|
| 66 |
+
# The model predicts a language code only; superlanguage and family are *metadata*
|
| 67 |
+
# attached to that code, shown as context, never as a separate prediction.
|
| 68 |
try:
|
| 69 |
with open("languages.json", "r", encoding="utf-8") as f:
|
| 70 |
_languages_data = json.load(f)
|
|
|
|
| 84 |
CODE_TO_META = {}
|
| 85 |
|
| 86 |
# Manual completion for entries where labels.json ships an empty family/superlanguage.
|
| 87 |
+
# (Metadata gaps in the model repo -- display-side only, probabilities are unaffected.)
|
| 88 |
META_FALLBACK = {
|
| 89 |
"chq": ("Otomangue", "Chinanteco"),
|
| 90 |
"hue": ("Huave", "Huave"),
|
|
|
|
| 100 |
|
| 101 |
|
| 102 |
def _meta(code):
|
| 103 |
+
"""(superlanguage, family, language_name) for a code, with graceful fallbacks."""
|
| 104 |
entry = INFO.get(code, {})
|
| 105 |
family = entry.get("family") or ""
|
| 106 |
superlanguage = entry.get("superlanguage") or ""
|
|
|
|
| 113 |
family = family or db_family or UNKNOWN_GROUP
|
| 114 |
superlanguage = superlanguage or db_super or UNKNOWN_GROUP
|
| 115 |
name = entry.get("language") or CODE_TO_NAME.get(code, code)
|
| 116 |
+
return superlanguage, family, name
|
| 117 |
|
| 118 |
|
|
|
|
|
|
|
| 119 |
SUPERLANGUAGE_OF = {}
|
| 120 |
+
FAMILY_OF = {}
|
| 121 |
DISPLAY_NAME = {}
|
| 122 |
for _code in CLASSES:
|
| 123 |
+
_super, _family, _name = _meta(_code)
|
|
|
|
| 124 |
SUPERLANGUAGE_OF[_code] = _super
|
| 125 |
+
FAMILY_OF[_code] = _family
|
| 126 |
DISPLAY_NAME[_code] = _name
|
| 127 |
|
| 128 |
print(
|
|
|
|
| 136 |
return process.memory_info().rss / (1024 ** 2)
|
| 137 |
|
| 138 |
|
| 139 |
+
# === RESULT RENDERING ===
|
| 140 |
+
RESULT_STYLES = """
|
| 141 |
+
<style>
|
| 142 |
+
.tw-lid { display: flex; flex-direction: column; gap: 8px; margin-top: 4px; }
|
| 143 |
+
.tw-row { display: flex; align-items: center; justify-content: space-between; gap: 14px;
|
| 144 |
+
padding: 10px 14px; border-radius: 12px;
|
| 145 |
+
background: rgba(2, 132, 199, 0.06);
|
| 146 |
+
border: 1px solid rgba(2, 132, 199, 0.18); }
|
| 147 |
+
.tw-row.tw-first { padding: 14px 16px;
|
| 148 |
+
background: linear-gradient(120deg, rgba(16, 185, 129, 0.14) 0%, rgba(2, 132, 199, 0.10) 100%);
|
| 149 |
+
border: 1px solid rgba(5, 150, 105, 0.45);
|
| 150 |
+
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08); }
|
| 151 |
+
.tw-main { min-width: 0; }
|
| 152 |
+
.tw-title { display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; }
|
| 153 |
+
.tw-name { font-size: 1.02rem; font-weight: 600; line-height: 1.25; }
|
| 154 |
+
.tw-first .tw-name { font-size: 1.3rem; }
|
| 155 |
+
.tw-code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 0.72rem;
|
| 156 |
+
letter-spacing: 0.04em; padding: 2px 8px; border-radius: 999px;
|
| 157 |
+
background: rgba(2, 132, 199, 0.14); color: #0369a1; white-space: nowrap; }
|
| 158 |
+
.tw-meta { font-size: 0.74rem; font-style: italic; opacity: 0.72; margin-top: 4px; }
|
| 159 |
+
.tw-conf { font-variant-numeric: tabular-nums; font-weight: 600; font-size: 1rem;
|
| 160 |
+
white-space: nowrap; opacity: 0.85; }
|
| 161 |
+
.tw-first .tw-conf { font-size: 1.4rem; color: #047857; opacity: 1; }
|
| 162 |
+
.dark .tw-code { color: #7dd3fc; }
|
| 163 |
+
.dark .tw-first .tw-conf { color: #6ee7b7; }
|
| 164 |
+
</style>
|
| 165 |
+
"""
|
| 166 |
+
|
| 167 |
+
|
| 168 |
+
def render_results(rows):
|
| 169 |
+
"""Render ranked predictions as pills. `rows` is [(code, probability), ...]."""
|
| 170 |
+
if not rows:
|
| 171 |
+
return ""
|
| 172 |
+
|
| 173 |
+
blocks = []
|
| 174 |
+
for index, (code, prob) in enumerate(rows):
|
| 175 |
+
css = "tw-row tw-first" if index == 0 else "tw-row"
|
| 176 |
+
blocks.append(
|
| 177 |
+
f'<div class="{css}">'
|
| 178 |
+
f'<div class="tw-main">'
|
| 179 |
+
f'<div class="tw-title">'
|
| 180 |
+
f'<span class="tw-name">{html.escape(DISPLAY_NAME[code])}</span>'
|
| 181 |
+
f'<span class="tw-code">{html.escape(code)}</span>'
|
| 182 |
+
f'</div>'
|
| 183 |
+
f'<div class="tw-meta">{html.escape(SUPERLANGUAGE_OF[code])} · '
|
| 184 |
+
f'{html.escape(FAMILY_OF[code])}</div>'
|
| 185 |
+
f'</div>'
|
| 186 |
+
f'<div class="tw-conf">{prob:.2%}</div>'
|
| 187 |
+
f'</div>'
|
| 188 |
+
)
|
| 189 |
+
return '<div class="tw-lid">' + "".join(blocks) + "</div>"
|
| 190 |
|
| 191 |
|
| 192 |
# === INFERENCE FUNCTION ===
|
| 193 |
+
def predict_language(audio_path, code_k=3, code_thresh=0.0):
|
| 194 |
if not audio_path:
|
| 195 |
raise gr.Error("No audio provided! Please upload or record an audio file.")
|
| 196 |
|
|
|
|
| 222 |
print(f"[LOG] Memory after inference: {get_mem_usage():.2f} MB")
|
| 223 |
|
| 224 |
print("[LOG] Step 3: Post-processing results...")
|
| 225 |
+
order = np.argsort(-probs)[: int(code_k)]
|
| 226 |
+
rows = [(CLASSES[int(i)], float(probs[int(i)])) for i in order]
|
| 227 |
+
rows = [(code, prob) for code, prob in rows if prob >= code_thresh]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
|
| 229 |
print(f"[LOG] Final Memory: {get_mem_usage():.2f} MB")
|
| 230 |
print("--- [LOG] Request Finished ---\n")
|
| 231 |
|
| 232 |
+
return render_results(rows)
|
| 233 |
except gr.Error:
|
| 234 |
raise
|
| 235 |
except Exception as e:
|
|
|
|
| 278 |
# === UI COMPONENTS ===
|
| 279 |
with gr.Blocks(theme=gr.themes.Ocean()) as demo:
|
| 280 |
gr.HTML(
|
| 281 |
+
RESULT_STYLES
|
| 282 |
+
+ f"""
|
| 283 |
<div style="text-align: center; padding: 30px; background: linear-gradient(120deg, rgb(2, 132, 199) 0%, rgb(16, 185, 129) 60%, rgb(5, 150, 105) 100%); color: white; border-radius: 15px; margin-bottom: 25px; box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);">
|
| 284 |
{icon_html}
|
| 285 |
<h1 style="color: white; margin: 0; font-size: 2.5em;">🦡 Tachiwin AudioId 2.0 🦡</h1>
|
| 286 |
+
<p style="font-size: 1.2em; opacity: 0.9; margin-top: 10px;">Identify variants of the {INALI_LANGUAGES} indigenous languages of Mexico — this model covers {N_CLASSES} of the {INALI_VARIANTS} catalogued variants<br/>
|
| 287 |
+
<i>Identifica variantes de las {INALI_LANGUAGES} lenguas originarias de México — este modelo cubre {N_CLASSES} de las {INALI_VARIANTS} variantes catalogadas</i></p>
|
| 288 |
</div>
|
| 289 |
"""
|
| 290 |
)
|
|
|
|
| 299 |
)
|
| 300 |
with gr.Accordion(i18n("advanced_options"), open=False):
|
| 301 |
with gr.Group():
|
| 302 |
+
gr.Markdown(i18n("prediction_options"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 303 |
with gr.Row():
|
| 304 |
code_k = gr.Slider(minimum=1, maximum=10, step=1, value=3, label=i18n("top_k_label"))
|
| 305 |
code_thresh = gr.Slider(minimum=0.0, maximum=1.0, step=0.05, value=0.0, label=i18n("threshold_label"))
|
|
|
|
| 310 |
|
| 311 |
with gr.Column(scale=1):
|
| 312 |
gr.Markdown(i18n("results_header"))
|
| 313 |
+
results_html = gr.HTML(value="")
|
|
|
|
|
|
|
| 314 |
|
| 315 |
submit_btn.click(
|
| 316 |
fn=predict_language,
|
| 317 |
+
inputs=[audio_input, code_k, code_thresh],
|
| 318 |
+
outputs=[results_html]
|
| 319 |
)
|
| 320 |
|
| 321 |
clear_btn.click(
|
| 322 |
+
fn=lambda: (None, ""),
|
| 323 |
inputs=None,
|
| 324 |
+
outputs=[audio_input, results_html]
|
| 325 |
)
|
| 326 |
|
| 327 |
gr.Markdown(i18n("curated_header"))
|
|
|
|
| 333 |
with gr.Row():
|
| 334 |
for i in range(row_idx, min(row_idx+columns, len(curated_audio))):
|
| 335 |
with gr.Column(scale=1):
|
| 336 |
+
gr.Markdown(f"**{curated_labels[i]}**")
|
| 337 |
gr.Audio(value=curated_audio[i], interactive=False, show_label=False)
|
| 338 |
btn = gr.Button("Load / Cargar", size="sm")
|
| 339 |
btn.click(fn=lambda url=curated_audio[i]: url, inputs=[], outputs=[audio_input])
|
|
@@ -17,6 +17,5 @@ huggingface_hub
|
|
| 17 |
soundfile
|
| 18 |
librosa
|
| 19 |
numpy
|
| 20 |
-
pandas
|
| 21 |
psutil
|
| 22 |
gradio
|
|
|
|
| 17 |
soundfile
|
| 18 |
librosa
|
| 19 |
numpy
|
|
|
|
| 20 |
psutil
|
| 21 |
gradio
|