

Batch unprocessed audio to reduce Whisper streaming calls
@0e0a12e470c70bc2b475fe0c10f11846f1dc74ab
--- whisper_fastapi_online_server.py
+++ whisper_fastapi_online_server.py
... | ... | @@ -3,6 +3,7 @@ |
3 | 3 |
import asyncio |
4 | 4 |
import numpy as np |
5 | 5 |
import ffmpeg |
6 |
+from time import time |
|
6 | 7 |
|
7 | 8 |
from fastapi import FastAPI, WebSocket, WebSocketDisconnect |
8 | 9 |
from fastapi.responses import HTMLResponse |
... | ... | @@ -69,22 +70,24 @@ |
69 | 70 |
nonlocal pcm_buffer |
70 | 71 |
loop = asyncio.get_event_loop() |
71 | 72 |
full_transcription = "" |
73 |
+ beg = time() |
|
72 | 74 |
while True: |
73 | 75 |
try: |
74 |
- chunk = await loop.run_in_executor(None, ffmpeg_process.stdout.read, 4096) |
|
75 |
- if not chunk: # FFmpeg might have closed |
|
76 |
- print("FFmpeg stdout closed.") |
|
77 |
- break |
|
76 |
+ elapsed_time = int(time() - beg) |
|
77 |
+ beg = time() |
|
78 |
+ chunk = await loop.run_in_executor(None, ffmpeg_process.stdout.read, 32000*elapsed_time) |
|
79 |
+ if not chunk: # The first chunk will be almost empty, FFmpeg is still starting up |
|
80 |
+ chunk = await loop.run_in_executor(None, ffmpeg_process.stdout.read, 4096) |
|
81 |
+ if not chunk: # FFmpeg might have closed |
|
82 |
+ print("FFmpeg stdout closed.") |
|
83 |
+ break |
|
78 | 84 |
|
79 | 85 |
pcm_buffer.extend(chunk) |
80 | 86 |
|
81 |
- while len(pcm_buffer) >= BYTES_PER_SEC: |
|
82 |
- three_sec_chunk = pcm_buffer[:BYTES_PER_SEC] |
|
83 |
- del pcm_buffer[:BYTES_PER_SEC] |
|
84 |
- |
|
87 |
+ if len(pcm_buffer) >= BYTES_PER_SEC: |
|
85 | 88 |
# Convert int16 -> float32 |
86 |
- pcm_array = np.frombuffer(three_sec_chunk, dtype=np.int16).astype(np.float32) / 32768.0 |
|
87 |
- |
|
89 |
+ pcm_array = np.frombuffer(pcm_buffer, dtype=np.int16).astype(np.float32) / 32768.0 |
|
90 |
+ pcm_buffer = bytearray() |
|
88 | 91 |
online.insert_audio_chunk(pcm_array) |
89 | 92 |
transcription = online.process_iter()[2] |
90 | 93 |
if args.vac: |
Add a comment
Delete comment
Once you delete this comment, you won't be able to recover it. Are you sure you want to delete this comment?