translate_pipeline.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. from pathlib import Path
  2. from typing import Dict
  3. import os
  4. import tempfile
  5. from .stt import transcribe
  6. from .detect_lang import detect_lang
  7. from .translate import translate_text
  8. from .tts import synthesize
  9. AUDIO_DIR = Path("/work/models/audio")
  10. def map_to_nllb(code: str) -> str:
  11. """
  12. Mappe un code comme 'fr' vers le code NLLB 'fra_Latn'.
  13. Pour la langue cible, on lit TARGET_LANG_CODE dans l’environnement.
  14. """
  15. if code.startswith("fr"):
  16. return "fra_Latn"
  17. target = os.getenv("TARGET_LANG_CODE", "lin_Latn")
  18. return target
  19. def process_audio_file(file_bytes: bytes, filename: str) -> Dict:
  20. # Sauvegarde temporaire
  21. tmp_path = Path(tempfile.mkstemp(suffix=os.path.splitext(filename)[1] or ".wav")[1])
  22. with open(tmp_path, "wb") as f:
  23. f.write(file_bytes)
  24. # STT
  25. text, whisper_lang = transcribe(tmp_path)
  26. # Détection de langue
  27. code = detect_lang(text, whisper_lang)
  28. # Déterminer direction
  29. src_nllb = map_to_nllb(code)
  30. tgt_nllb = "fra_Latn" if src_nllb != "fra_Latn" else os.getenv("TARGET_LANG_CODE", "lin_Latn")
  31. # Traduction
  32. translated = translate_text(text, src_nllb, tgt_nllb)
  33. # TTS
  34. tts_lang = "fr" if tgt_nllb == "fra_Latn" else "xx" # 'xx' = code générique
  35. out_path = synthesize(translated, tts_lang, AUDIO_DIR)
  36. return {
  37. "source_text": text,
  38. "detected_lang": src_nllb,
  39. "translated_text": translated,
  40. "audio_url": f"/audio/{out_path.name}",
  41. }