bilinguascript-htr / language_detect.py
BertinAm
BiLinguaScript HTR API
15c77b8
Raw
History Blame Contribute Delete
496 Bytes
"""Standalone language detection used by app.py routing logic."""
from langdetect import detect, DetectorFactory, LangDetectException
DetectorFactory.seed = 42
SUPPORTED = {"en", "fr"}
def detect_language(text: str, hint: str = "auto") -> str:
if hint in SUPPORTED:
return hint
if not text or not text.strip():
return "en"
try:
lang = detect(text[:500])
return lang if lang in SUPPORTED else "en"
except LangDetectException:
return "en"