Download stt_handler.pyc from mgbam/rentbot: direct link, hf CLI and curl.
- Browser
- Download file 1.07 kB
-
https://huggingface.co/spaces/mgbam/rentbot/resolve/f694f6817773bd012697de3b7e13dc18daf7a809/stt_handler.pyc
- Command line
-
hf download hf://spaces/mgbam/rentbot@f694f6817773bd012697de3b7e13dc18daf7a809/stt_handler.pyc
-
curl -L -o stt_handler.pyc https://huggingface.co/spaces/mgbam/rentbot/resolve/f694f6817773bd012697de3b7e13dc18daf7a809/stt_handler.pyc
1.07 kB
| # 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() |