| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- from pathlib import Path
- from typing import Tuple
- import tempfile
- import subprocess
- import whisper
- _WHISPER_MODEL = None
- def _get_model(name: str = "base"):
- global _WHISPER_MODEL
- if _WHISPER_MODEL is None:
- _WHISPER_MODEL = whisper.load_model(name)
- return _WHISPER_MODEL
- def ensure_wav(input_path: Path) -> Path:
- """Convertit l'audio en WAV si nécessaire via ffmpeg."""
- if input_path.suffix.lower() == ".wav":
- return input_path
- out = Path(tempfile.mkstemp(suffix=".wav")[1])
- cmd = [
- "ffmpeg",
- "-i",
- str(input_path),
- "-ar",
- "16000",
- "-ac",
- "1",
- str(out),
- "-y",
- ]
- subprocess.run(cmd, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
- return out
- def transcribe(audio_path: Path) -> Tuple[str, str]:
- """
- Transcrit le fichier audio avec Whisper.
- Retourne (texte, langue_detectee_par_whisper).
- """
- model = _get_model()
- wav_path = ensure_wav(audio_path)
- result = model.transcribe(str(wav_path))
- text = result.get("text", "").strip()
- lang = result.get("language", "") # e.g., 'fr'
- return text, lang
|