detect_lang.py 465 B

12345678910111213141516
  1. from typing import Optional
  2. from langdetect import detect
  3. def detect_lang(text: str, whisper_lang: Optional[str] = None) -> str:
  4. """
  5. Détecte la langue du texte. Utilise d'abord Whisper si fourni, sinon langdetect.
  6. Retourne un code simple ('fr' pour français, sinon code ISO approximatif).
  7. """
  8. if whisper_lang:
  9. return whisper_lang
  10. try:
  11. code = detect(text)
  12. return code
  13. except Exception:
  14. return "unknown"