speech_recognition.py 878 B

123456789101112131415161718192021222324252627
  1. import whisper
  2. import ffmpeg
  3. import numpy as np
  4. SAMPLE_RATE = 16000
  5. def load_audio(data: bytes):
  6. try:
  7. # This launches a subprocess to decode audio while down-mixing and resampling as necessary.
  8. # Requires the ffmpeg CLI and `ffmpeg-python` package to be installed.
  9. out, _ = (
  10. ffmpeg.input("pipe:", threads=0)
  11. .output("-", format="s16le", acodec="pcm_s16le", ac=1, ar=SAMPLE_RATE)
  12. .run(cmd="ffmpeg", capture_stdout=True, capture_stderr=True, input=data)
  13. )
  14. except ffmpeg.Error as e:
  15. raise RuntimeError(f"Failed to load audio: {e.stderr.decode()}") from e
  16. return np.frombuffer(out, np.int16).flatten().astype(np.float32) / 32768.0
  17. class ASR():
  18. def __init__(self, model = "tiny"):
  19. self.model = whisper.load_model(model)
  20. def transcribe(self, audio: bytes):
  21. audio = load_audio(audio)
  22. return self.model.transcribe(audio)