Introduction
Language identification (LID) serves as the foundational "gatekeeper" for multilingual AI systems, such as virtual assistants. A failure at this stage propagates through downstream tasks like speech recognition and response generation. Existing LID solutions typically face a trade-off: fast, rule-based models often struggle with short utterances and named entities, while high-accuracy Large Language Models (LLMs) are computationally expensive and introduce prohibitive latency for real-time applications. Furthermore, most models fail to handle Out-of-Domain (OOD) detection effectively, often misclassifying unknown languages into supported categories with high confidence.
PolyLingua addresses these challenges by providing a lightweight, multi-task Transformer model designed for robust LID. It achieves state-of-the-art accuracy while remaining orders of magnitude faster than massive generative models.
Methodology
PolyLingua utilizes a shared encoder architecture based on a multilingual MiniLM, ensuring computational efficiency. The core innovation is a two-level contrastive learning framework designed to enhance linguistic separation.
The framework consists of three primary components:
- Instance-level Separation: A supervised contrastive loss pulls embeddings of the same language together while pushing different languages apart.
- Class-level Alignment with Adaptive Margins: The model encourages utterances to cluster around a language "centroid." To handle linguistically similar pairs (e.g., Spanish, Portuguese, and French), the authors introduce adaptive margins ($\delta$) that enforce stricter separation during training.
- Multi-task Heads: The architecture includes an in-domain detection head (binary), a language ID head (multi-class), and a projection head for contrastive loss calculation.
To prevent the model from simply memorizing specific entities, the training process incorporates Dynamic Entity Replacement, swapping named entities like artist names or song titles to force the model to learn underlying linguistic structures.
Experimental Setup
The model was evaluated on two benchmarks:
- Amazon Massive: A 52-language dataset for natural language understanding. 10 languages were designated as "in-domain," with the remainder serving as OOD.
- Song Dataset: A synthetic dataset mimicking music requests, characterized by heavy "code-switching" (e.g., mixing a command in one language with a title in another).
Results and Discussion
PolyLingua demonstrates superior performance across both accuracy and efficiency metrics. On the Amazon Massive dataset, it achieved a 99.25% F1 score, outperforming Claude 3.5 Sonnet (97.79%). On the more challenging Song Dataset, PolyLingua maintained a 98.15% F1, significantly higher than Sonnet 3.5 (95.52%) and XLM-LID (81.73%).
The effectiveness of the adaptive margin strategy is evident in the embedding space. As shown in the UMAP visualization, PolyLingua creates distinct, tight clusters even for linguistically similar languages that typically cause confusion in baseline models.
In terms of production readiness, PolyLingua offers an average latency of 7.19 ms per inference, making it approximately 200x faster than Claude 3.5 Sonnet (1373 ms). This efficiency, combined with its robust OOD detection, makes it ideal for low-latency production environments.
Conclusion
PolyLingua proves that task-specific, well-tuned encoder models can outperform general-purpose LLMs in specialized domains like LID. Future work will focus on automating the assignment of adaptive margins for larger language sets and testing robustness against Automatic Speech Recognition (ASR) transcription errors.
Summary updated