Create stt_handler.pyc
Browse files- stt_handler.pyc +35 -0
stt_handler.pyc
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# rentbot/stt_handler.py
|
| 2 |
+
import whisper
|
| 3 |
+
import numpy as np
|
| 4 |
+
import asyncio
|
| 5 |
+
import os
|
| 6 |
+
from io import BytesIO
|
| 7 |
+
|
| 8 |
+
# Load the model once when the module is imported
|
| 9 |
+
print("Loading Whisper model...")
|
| 10 |
+
model = whisper.load_model("base.en")
|
| 11 |
+
print("Whisper model loaded.")
|
| 12 |
+
|
| 13 |
+
async def transcribe_audio_chunk(audio_chunk: np.ndarray) -> str:
|
| 14 |
+
"""
|
| 15 |
+
Transcribes an audio chunk using Whisper.
|
| 16 |
+
Runs the blocking whisper call in a separate thread.
|
| 17 |
+
"""
|
| 18 |
+
# The audio data is 16-bit PCM, 8000 Hz. Whisper expects float32.
|
| 19 |
+
audio_float32 = audio_chunk.astype(np.float32) / 32768.0
|
| 20 |
+
|
| 21 |
+
# Using an in-memory buffer
|
| 22 |
+
wav_buffer = BytesIO()
|
| 23 |
+
|
| 24 |
+
# We must provide the sample rate to whisper's transcribe function
|
| 25 |
+
loop = asyncio.get_event_loop()
|
| 26 |
+
result = await loop.run_in_executor(
|
| 27 |
+
None, # Use the default executor (a ThreadPoolExecutor)
|
| 28 |
+
lambda: model.transcribe(
|
| 29 |
+
audio_float32,
|
| 30 |
+
language="en",
|
| 31 |
+
fp16=False # Set to False if not using a GPU
|
| 32 |
+
)
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
return result.get("text", "").strip()
|