Luis J Camargo commited on
Commit
cc3e2aa
·
1 Parent(s): 32bc284

fix(ui): official catalogue names, drop redundant meta fallbacks

Browse files

- Display names now come from the catalogue: inali_name for the Spanish
UI, english_name for the English UI, falling back to the model's label
and then the raw code. NAMES[code] carries both.
- META_FALLBACK trimmed to hue and nah, the only two model classes the
catalogue does not contain. The other eight entries were duplicates of
catalogue values, and the stp one ('Tepehuano') contradicted the
catalogue ('Tepehuan').
- Locale is read from the Accept-Language header, since Gradio resolves
i18n client-side only.

Files changed (1) hide show
  1. app.py +33 -16
app.py CHANGED
@@ -68,6 +68,7 @@ N_CLASSES = len(CLASSES)
68
  try:
69
  with open("languages.json", "r", encoding="utf-8") as f:
70
  _languages_data = json.load(f)
 
71
  CODE_TO_NAME = {
72
  item.get("code"): item.get("inali_name")
73
  for item in _languages_data
@@ -80,22 +81,17 @@ try:
80
  }
81
  except Exception as e:
82
  print(f"Warning: Could not load languages.json: {e}")
 
83
  CODE_TO_NAME = {}
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"),
91
- "mir": ("Mixe-Zoqueano", "Mixe"),
92
  "nah": ("Yuto-Nahua", "Náhuatl"),
93
- "ots": ("Otomangue", "Otomí"),
94
- "pua": ("Purépecha", "Purépecha"),
95
- "stp": ("Yuto-Nahua", "Tepehuano"),
96
- "zad": ("Otomangue", "Zapoteco"),
97
- "zar": ("Otomangue", "Zapoteco"),
98
- "zpd": ("Otomangue", "Zapoteco"),
99
  }
100
 
101
 
@@ -118,12 +114,18 @@ def _meta(code):
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(
129
  f"Language-ID model loaded: {len(CLASSES)} codes, "
@@ -165,11 +167,26 @@ RESULT_STYLES = """
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"
@@ -177,7 +194,7 @@ def render_results(rows):
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])} · '
@@ -190,7 +207,7 @@ def render_results(rows):
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
 
@@ -229,7 +246,7 @@ def predict_language(audio_path, code_k=3, code_thresh=0.0):
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:
 
68
  try:
69
  with open("languages.json", "r", encoding="utf-8") as f:
70
  _languages_data = json.load(f)
71
+ CATALOGUE = {item.get("code"): item for item in _languages_data if item.get("code")}
72
  CODE_TO_NAME = {
73
  item.get("code"): item.get("inali_name")
74
  for item in _languages_data
 
81
  }
82
  except Exception as e:
83
  print(f"Warning: Could not load languages.json: {e}")
84
+ CATALOGUE = {}
85
  CODE_TO_NAME = {}
86
  CODE_TO_META = {}
87
 
88
+ # Only two codes are absent from the catalogue: `nah` (Náhuatl) and `hue` (Huave).
89
+ # They are macrolanguage placeholders the dataset kept as a fallback for variants that could
90
+ # not be resolved. The model is deterministic and never emits them as an "unknown" bucket,
91
+ # so they exist purely so those two classes still have a family/superlanguage to display.
92
  META_FALLBACK = {
 
93
  "hue": ("Huave", "Huave"),
 
94
  "nah": ("Yuto-Nahua", "Náhuatl"),
 
 
 
 
 
 
95
  }
96
 
97
 
 
114
 
115
  SUPERLANGUAGE_OF = {}
116
  FAMILY_OF = {}
117
+ NAMES = {}
118
  for _code in CLASSES:
119
  _super, _family, _name = _meta(_code)
120
  SUPERLANGUAGE_OF[_code] = _super
121
  FAMILY_OF[_code] = _family
122
+ # Official presentation name: INALI name for the Spanish UI, English name for the English
123
+ # UI, falling back to the model's own label and finally to the raw code.
124
+ _entry = CATALOGUE.get(_code, {})
125
+ _fallback = _name or _code
126
+ _es = (_entry.get("inali_name") or "").strip() or _fallback
127
+ _en = (_entry.get("english_name") or "").strip() or _es
128
+ NAMES[_code] = {"es": _es, "en": _en}
129
 
130
  print(
131
  f"Language-ID model loaded: {len(CLASSES)} codes, "
 
167
  """
168
 
169
 
170
+ def _ui_locale(request):
171
+ """Best-effort UI locale.
172
+
173
+ Gradio resolves i18n on the frontend from the browser's locale, so the browser header is
174
+ the faithful mirror of the default UI language; the in-app locale switch is client-side
175
+ and is not visible to this process.
176
+ """
177
+ raw = ""
178
+ if request is not None:
179
+ raw = (request.headers.get("accept-language") or "")
180
+ first = raw.split(",")[0].strip().lower()
181
+ return "en" if first.startswith("en") else "es"
182
+
183
+
184
+ def render_results(rows, locale="es"):
185
  """Render ranked predictions as pills. `rows` is [(code, probability), ...]."""
186
  if not rows:
187
  return ""
188
 
189
+ lang = "en" if locale == "en" else "es"
190
  blocks = []
191
  for index, (code, prob) in enumerate(rows):
192
  css = "tw-row tw-first" if index == 0 else "tw-row"
 
194
  f'<div class="{css}">'
195
  f'<div class="tw-main">'
196
  f'<div class="tw-title">'
197
+ f'<span class="tw-name">{html.escape(NAMES[code][lang])}</span>'
198
  f'<span class="tw-code">{html.escape(code)}</span>'
199
  f'</div>'
200
  f'<div class="tw-meta">{html.escape(SUPERLANGUAGE_OF[code])} · '
 
207
 
208
 
209
  # === INFERENCE FUNCTION ===
210
+ def predict_language(audio_path, code_k=3, code_thresh=0.0, request: gr.Request = None):
211
  if not audio_path:
212
  raise gr.Error("No audio provided! Please upload or record an audio file.")
213
 
 
246
  print(f"[LOG] Final Memory: {get_mem_usage():.2f} MB")
247
  print("--- [LOG] Request Finished ---\n")
248
 
249
+ return render_results(rows, _ui_locale(request))
250
  except gr.Error:
251
  raise
252
  except Exception as e: