# rentbot/stt_handler.py import whisper import numpy as np import asyncio import os from io import BytesIO # Load the model once when the module is imported print("Loading Whisper model...") model = whisper.load_model("base.en") print("Whisper model loaded.") async def transcribe_audio_chunk(audio_chunk: np.ndarray) -> str: """ Transcribes an audio chunk using Whisper. Runs the blocking whisper call in a separate thread. """ # The audio data is 16-bit PCM, 8000 Hz. Whisper expects float32. audio_float32 = audio_chunk.astype(np.float32) / 32768.0 # Using an in-memory buffer wav_buffer = BytesIO() # We must provide the sample rate to whisper's transcribe function loop = asyncio.get_event_loop() result = await loop.run_in_executor( None, # Use the default executor (a ThreadPoolExecutor) lambda: model.transcribe( audio_float32, language="en", fp16=False # Set to False if not using a GPU ) ) return result.get("text", "").strip()