RealtimeSTT FastAPI 浏览器流式服务器深度实战:多用户 WebSocket 实时语音转写架构与引擎配置指南
2026/9/16 1:09:42 网站建设 项目流程

RealtimeSTT FastAPI 浏览器流式服务器深度实战:多用户 WebSocket 实时语音转写架构与引擎配置指南

【免费下载链接】RealtimeSTTA robust, efficient, low-latency speech-to-text library with advanced voice activity detection, wake word activation and instant transcription.项目地址: https://gitcode.com/GitHub_Trending/re/RealtimeSTT

example_fastapi_server是 RealtimeSTT 官方提供的浏览器流式参考服务器:它通过 FastAPI 提供一套可直接使用的 Web UI 与 WebSocket 音频端点,把浏览器麦克风流接入 RealtimeSTT,并支持多个浏览器会话并发、共享 ASR 模型车道与按会话公平调度的实时转写。读完本文,你将掌握该服务器的安装启动、多用户容量规划、运行时热配置、GPU/CPU 各引擎配方、调优 profile、WebSocket 二进制协议与性能测试方法,并能直接复用到自己的实时语音产品中。

一、服务器定位与总体架构

example_fastapi_server的核心是 server.py(约 3500 行),它承担三件事:

  1. 服务静态浏览器 UIGET /返回 static/index.html;
  2. 暴露 WebSocket 端点WS /ws/transcribe:接收浏览器麦克风音频二进制包,也接收start/stop/clear等文本命令;
  3. 在服务端维护每会话的转写状态机:每个已接受的 WebSocket 会话持有独立的轻量级AudioToTextRecorder状态机,继承 RealtimeSTT 的 WebRTC VAD、Silero VAD、唤醒词钩子、early-final 转写与音节边界实时调度能力;而重量级 ASR 引擎则被抽取为共享执行器,而不是每个浏览器各加载一份模型。

从 server.py 的create_app()可以看出完整的端点面:

端点作用
GET /浏览器 UI
GET /health就绪状态、活跃会话/说话人数、启动错误、调度器状态
GET /api/config公开配置、容量上限、受支持引擎、运行时设置契约
PATCH /api/config运行时热更新配置
GET /api/metrics每会话计数器、队列深度、合并/丢弃计数、p50/p95 延迟、worker 忙闲比
WS /ws/transcribe浏览器音频流与命令通道

注意:该参考服务器面向**源码检出(source checkout)**使用,PyPI wheel 不包含它。仅用 pip 安装的用户应使用 Python recorder/API 示例;需要本服务器时请克隆仓库或从 Git 安装(参见 docs/fastapi-server.md)。

二、安装与环境准备

官方推荐使用 Linux 环境运行 CUDA 密集型引擎(Parakeet、Qwen vLLM、大型 Transformers 模型),完整安装步骤如下:

python -m venv .venv-fastapi source .venv-fastapi/bin/activate python -m pip install -U pip setuptools wheel python -m pip install -r requirements.txt python -m pip install -r example_fastapi_server/requirements.txt

服务器自身依赖非常轻——requirements.txt 只有fastapi>=0.115uvicorn[standard]>=0.30numpyscipy四项。重依赖全部来自所选的转写引擎栈。

可选引擎栈安装

按需安装你计划运行的引擎(对应 README 与 docs/transcription-engines.md):

# Parakeet(NeMo,CUDA) python -m pip install "nemo_toolkit[asr]" soundfile librosa # CPU whisper.cpp python -m pip install pywhispercpp # CPU sherpa-onnx Moonshine python -m pip install sherpa-onnx # Meta Omnilingual ASR(Linux/WSL2) python -m pip install "RealtimeSTT[omnilingual]" # 源码检出时使用: python -m pip install -e ".[omnilingual]"

Kroko-ONNX 需要单独构建(上游当前偏向 Linux/Docker;Windows 原生构建失败时请改用 WSL2/Linux 运行服务器):

git clone https://github.com/kroko-ai/kroko-onnx.git cd kroko-onnx python -m pip install .

三、快速启动(默认 faster-whisper 配置)

python example_fastapi_server/server.py --host 0.0.0.0 --port 8010

然后浏览器打开http://localhost:8010即可开始麦克风流式转写。

命令行参数全部由 parse_args() 解析、settings_from_args() 组装成ServerSettings。核心引擎参数见下表(默认值取自 ServerSettings):

Flag含义默认值
--engine/--transcription-engine最终转写引擎faster_whisper
--model最终模型名或路径small.en
--realtime-engine/--realtime-transcription-engine实时引擎,省略时复用最终引擎
--realtime-model实时模型名或路径tiny.en
--engine-options传给最终引擎的 JSON 对象
--realtime-engine-options传给实时引擎的 JSON 对象
--download-root模型缓存/查找根目录
--devicecudacpucuda
--compute-type引擎精度/量化提示default
--language语言代码en
--use-main-model-for-realtime用单条共享模型车道同时承担实时与最终转写(低内存模式)关闭

四、多用户会话架构深入

4.1 会话级隔离(session-scoped)

服务器接受多个独立浏览器会话。每个 WebSocket 被分配一个sessionId,以下内容全部按会话隔离:音频缓冲区、VAD 状态、转写片段segmentId、clear/reset 命令、实时文本、最终文本、状态、警告与错误(参见 README)。

SegmentState(server.py)维护会话内的片段号生成逻辑:实时事件与最终事件共享同一个segmentId,最终文本到达后 UI 用最终块替换实时块。SegmentTimelineTracker(server.py)为每个片段记录录音起止时间戳、时长、pre-roll 范围与唤醒词时间线。

4.2 VAD 为什么按会话隔离

服务器刻意让 VAD会话级隔离。WebRTC/Silero 检测在流级别是有状态的(模型权重、循环状态、线程访问),若多个会话共享单个 VAD 对象,除非实现能证明按会话分离模型权重、循环状态与线程访问,否则就是正确性风险。README 明确指出:未来优化可以共享不可变的 VAD 权重,但必须保持每会话的 VAD 状态与重置语义(README)。

在每个会话内部,VoiceActivityDetector 优先使用 WebRTC VAD(webrtcvad.Vad,aggressiveness 由--webrtc-sensitivity指定),无法加载时回退到基于 RMS 的能量 VAD(阈值由--vad-energy-threshold控制,默认 250.0)。

4.3 共享模型车道(shared model lanes)

模型资源按车道共享,而不是每个浏览器一份模型副本:

  • 默认均衡模式:一条共享最终模型车道 + 一条共享实时模型车道;
  • 低内存模式--use-main-model-for-realtime让一条共享模型车道同时负责实时与最终转写。

InferenceScheduler(server.py)负责创建车道:main_worker--engine/--modelrealtime_worker--realtime-engine/--realtime-model(未指定时回退到最终引擎)。每条车道由SharedEngineWorker(server.py)在独立后台线程中驱动,并在启动时用 RealtimeSTT/assets/warmup_audio.wav 做模型预热;每个会话在 recorder 构造时也会预热 VAD 路径,避免第一段语音付出 Silero/WebRTC 懒加载的运行期成本(warmup_vad--no-model-warmup关闭,见 server.py)。

4.4 公平调度队列

推理任务通过每会话公平队列调度(FairInferenceQueue,server.py):

  • 最终任务(final):按会话保留到--max-final-queue-depth-per-session上限,超出即拒绝;
  • 实时任务(realtime):同一会话的新实时任务会**合并(coalesce)**旧的未执行实时任务,只保留最新一帧;超过--max-realtime-queue-age-ms的陈旧任务直接丢弃。

这样设计保证:一个嘈杂客户端无法用过期的 interim 工作灌满全局队列;同时队列在会话之间轮转(round-robin),没有会话会饿死其他会话。这些行为都有单元测试直接验证,例如 test_fastapi_server_protocol.py 中的test_fair_queue_coalesces_realtime_but_preserves_finalstest_fair_queue_round_robins_between_sessionstest_fair_queue_drops_stale_realtime_jobs

4.5 容量控制参数

容量是显式可配置的。官方推荐的容量参数组合(README):

python example_fastapi_server/server.py \ --max-sessions 4 \ --max-active-speakers 4 \ --max-global-inference-queue-depth 64 \ --max-final-queue-depth-per-session 8 \ --max-realtime-queue-age-ms 1500 \ --max-audio-queue-seconds-per-session 30

各容量参数语义(实现于SessionStore/RealtimeSession,见 server.py):

参数默认值作用
--max-sessions4最大浏览器会话数,超出直接拒绝(WebSocket 关闭码 1013)
--max-active-speakers4最大并发说话人数,超出仅告警,已接收会话继续尽可能保留最终转写
--max-global-inference-queue-depth64全局推理队列总上限
--max-final-queue-depth-per-session8每会话最终任务积压上限,超出的 recorded segment 被裁剪丢弃
--max-realtime-queue-age-ms1500实时任务最大排队年龄,过期丢弃
--max-audio-queue-seconds-per-session30.0单会话连续录音超过该时长被强制 finalize
--audio-queue-size128每会话输入队列大小(映射到 recorder 的allowed_latency_limit
--max-audio-packet-bytes512KB单个二进制音频包上限
--pre-recording-buffer-duration0.75每会话 pre-roll(预录音缓冲)时长

会话插槽在构造 recorder/VAD之前被预留(SessionStore.reserve()),因此并发连接洪峰也不会实例化出超过会话上限的 recorder;音频包只有在收到start命令之后才被接受(见 server.py 中“Audio stream is stopped”的拒绝逻辑)。长时间连续录音会在达到--max-audio-queue-seconds-per-session时被强制 finalize(_force_finalize_after_limit,server.py)。

五、运行时配置:GET/PATCH /api/config

服务器的运行时可安全配置项通过GET /api/config暴露,响应中的runtimeSettings契约(实现于 runtime_settings_contract())把设置分成三类:

  • activeSessionSafe:立即作用于正在运行的会话(如max_sessionsmax_active_speakers、各队列深度);
  • newSessionOnly:仅复制到未来的浏览器会话,已存在的会话保留其 recorder 配置(如wake_wordssilero_sensitivitypost_speech_silence_duration等 VAD/唤醒词/实时转写参数);
  • startupOnly:因为共享推理 worker 已初始化而需要重启(如引擎、模型路径、batch_sizebeam_sizedevice),热更新会被明确拒绝。

三类全集分别定义在 ACTIVE_RUNTIME_SETTINGS、NEW_SESSION_RUNTIME_SETTINGS 与 STARTUP_ONLY_SETTINGS。值的类型校验由 coerce_setting_value() 依据INT_SETTINGS/FLOAT_SETTINGS/BOOL_SETTINGS/DICT_SETTINGS/TUPLE_FLOAT_SETTINGS分类表完成。

示例:把max_sessions提到 8 并给后续会话启用唤醒词jarvis

curl -X PATCH http://localhost:8010/api/config \ -H 'Content-Type: application/json' \ -d '{"settings":{"max_sessions":8,"wake_words":"jarvis"}}'

响应会逐项给出applied(含appliesTo: active_sessions/new_sessions)与rejected(含reason: startup_only/unknown/invalid_value),其行为被单元测试test_runtime_settings_update_distinguishes_safe_and_startup_only_fields覆盖(test_fastapi_server_protocol.py)。

唤醒词模式

唤醒词配置直接透传给每个浏览器会话的 recorder:

python example_fastapi_server/server.py \ --wakeword-backend pvporcupine \ --wake-words jarvis \ --wake-words-sensitivity 0.7 \ --wake-word-timeout 5 \ --wake-word-followup-window 5

相关 flag 还包括--wake-word-activation-delay(唤醒模式延迟生效)、--wake-word-buffer-duration(从录音开头剥离的唤醒词音频)、--openwakeword-model-paths--openwakeword-inference-framework(默认onnx)。唤醒等待、检测、后续语音窗口、超时、录音开始/结束、实时与最终转写等状态迁移,都会以timelineWebSocket 事件呈现并在浏览器 UI 中展示;转写块携带片段时间、时长、pre-roll 与唤醒检测元数据(实现于SegmentTimelineTracker_publish_timeline_event,server.py)。

六、GPU 引擎配方

6.1 Parakeet 最终转写 + 小实时 Whisper 模型

export HF_HOME="$HOME/.cache/huggingface" python example_fastapi_server/server.py \ --host 0.0.0.0 \ --port 8010 \ --engine parakeet \ --model nvidia/parakeet-tdt-0.6b-v3 \ --realtime-engine faster_whisper \ --realtime-model tiny.en \ --device cuda \ --language en

6.2 Parakeet 同时承担实时与最终转写

python example_fastapi_server/server.py \ --engine parakeet \ --model nvidia/parakeet-tdt-0.6b-v3 \ --use-main-model-for-realtime \ --profile parakeet-low-latency \ --device cuda \ --language en

6.3 Meta Omnilingual ASR(Linux 或 WSL2)

单条 CTC 模型车道同时负责实时与最终转写,用--use-main-model-for-realtime合并车道以省显存:

PYTHONPATH=. python example_fastapi_server/server.py \ --host 0.0.0.0 \ --port 8010 \ --engine omnilingual_asr \ --model omniASR_CTC_300M_v2 \ --realtime-engine omnilingual_asr \ --realtime-model omniASR_CTC_300M_v2 \ --use-main-model-for-realtime \ --device cuda \ --compute-type float16 \ --language eng_Latn \ --engine-options '{"batch_size":1,"sample_rate":16000}'

WSL2 开启 localhost 转发后,可在 Windows 浏览器直接打开http://localhost:8010。300M 模型跑通后,若 GPU 显存足够可尝试omniASR_CTC_1B_v2

七、CPU 引擎配方(Windows cmd.exe)

以下命令面向 Windowscmd.exe,需在仓库根目录执行;先用set PYTHON_EXE=python指向正确的解释器(或虚拟环境中的 python)。

7.1 whisper.cpp CPU

tiny.en同时承担实时与最终转写;实时侧使用 greedy 解码和 single-segment/no-context 设置换取更快的 interim 文本:

"%PYTHON_EXE%" example_fastapi_server\server.py --host 0.0.0.0 --port 8010 --engine whisper_cpp --model tiny.en --realtime-engine whisper_cpp --realtime-model tiny.en --device cpu --beam-size 5 --beam-size-realtime 1 --download-root test-model-cache\pywhispercpp --engine-options "{\"model\":{\"n_threads\":8,\"redirect_whispercpp_logs_to\":null}}" --realtime-engine-options "{\"model\":{\"n_threads\":8,\"redirect_whispercpp_logs_to\":null},\"transcribe\":{\"single_segment\":true,\"no_context\":true,\"print_timestamps\":false}}"

7.2 sherpa-onnx Moonshine CPU

一次性下载并解压 Tiny 与 Base 两个 int8 模型到test-model-cache\sherpa-onnx

mkdir test-model-cache\sherpa-onnx curl.exe -L -o test-model-cache\sherpa-onnx\sherpa-onnx-moonshine-tiny-en-int8.tar.bz2 https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-moonshine-tiny-en-int8.tar.bz2 "%PYTHON_EXE%" -c "import tarfile; tarfile.open(r'test-model-cache\sherpa-onnx\sherpa-onnx-moonshine-tiny-en-int8.tar.bz2', 'r:bz2').extractall(r'test-model-cache\sherpa-onnx')" curl.exe -L -o test-model-cache\sherpa-onnx\sherpa-onnx-moonshine-base-en-int8.tar.bz2 https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-moonshine-base-en-int8.tar.bz2 "%PYTHON_EXE%" -c "import tarfile; tarfile.open(r'test-model-cache\sherpa-onnx\sherpa-onnx-moonshine-base-en-int8.tar.bz2', 'r:bz2').extractall(r'test-model-cache\sherpa-onnx')"

Base 负责最终转写、Tiny 负责实时转写(启用音节边界实时调度):

"%PYTHON_EXE%" example_fastapi_server\server.py --host 0.0.0.0 --port 8010 --engine sherpa_onnx_moonshine --model sherpa-onnx-moonshine-base-en-int8 --realtime-engine sherpa_onnx_moonshine --realtime-model sherpa-onnx-moonshine-tiny-en-int8 --device cpu --language en --download-root test-model-cache\sherpa-onnx --engine-options "{\"num_threads\":4,\"provider\":\"cpu\"}" --realtime-engine-options "{\"num_threads\":2,\"provider\":\"cpu\"}" --realtime-processing-pause 0.8 --realtime-use-syllable-boundaries --realtime-boundary-detector-sensitivity 0.6 --realtime-boundary-followup-delays 0.1,0.2,0.4

需要更低内存时,用 Tiny 同时承担最终与实时转写:

"%PYTHON_EXE%" example_fastapi_server\server.py --host 0.0.0.0 --port 8010 --engine sherpa_onnx_moonshine --model sherpa-onnx-moonshine-tiny-en-int8 --realtime-engine sherpa_onnx_moonshine --realtime-model sherpa-onnx-moonshine-tiny-en-int8 --device cpu --language en --download-root test-model-cache\sherpa-onnx --engine-options "{\"num_threads\":2,\"provider\":\"cpu\"}" --realtime-engine-options "{\"num_threads\":2,\"provider\":\"cpu\"}" --realtime-processing-pause 0.8 --realtime-use-syllable-boundaries --realtime-boundary-detector-sensitivity 0.6 --realtime-boundary-followup-delays 0.1,0.2,0.4

7.3 Kroko-ONNX CPU

一次性下载社区英文流式模型:

mkdir test-model-cache\kroko-onnx python -c "from huggingface_hub import hf_hub_download; hf_hub_download(repo_id='Banafo/Kroko-ASR', filename='Kroko-EN-Community-64-L-Streaming-001.data', local_dir='test-model-cache/kroko-onnx')"

Kroko 同时承担最终与实时转写:

python example_fastapi_server\server.py --host 0.0.0.0 --port 8010 --engine kroko_onnx --model test-model-cache\kroko-onnx\Kroko-EN-Community-64-L-Streaming-001.data --realtime-engine kroko_onnx --realtime-model test-model-cache\kroko-onnx\Kroko-EN-Community-64-L-Streaming-001.data --device cpu --language en --engine-options "{\"provider\":\"cpu\",\"num_threads\":2}" --realtime-engine-options "{\"provider\":\"cpu\",\"num_threads\":1}"

或 Kroko 负责最终文本、轻量 whisper.cpp 负责实时:

python example_fastapi_server\server.py --host 0.0.0.0 --port 8010 --engine kroko_onnx --model test-model-cache\kroko-onnx\Kroko-EN-Community-64-L-Streaming-001.data --realtime-engine whisper_cpp --realtime-model tiny.en --device cpu --language en --engine-options "{\"provider\":\"cpu\",\"num_threads\":2}"

八、Parakeet 调优配置文件

Whisper 系引擎用--beam-size/--beam-size-realtime直接做速度/质量权衡;Parakeet TDT 使用不同的 NeMo 解码栈,因此服务器改用--profile批大小、实时节奏、VAD/分段时机三个维度调延迟。三个内置 profile 定义在 TUNING_PROFILES:

Profile设计意图关键取值
parakeet-low-latency频繁 interim 更新、更小批次、更短静音窗batch_size=1realtime_processing_pause=0.04post_speech_silence_duration=0.45early_transcription_on_silence=0.15
parakeet-balanced浏览器听写场景的均衡默认batch_size=8realtime_processing_pause=0.06post_speech_silence_duration=0.55early_transcription_on_silence=0.2
parakeet-accurate-final更慢的最终化、更稳定的最终块batch_size=16realtime_processing_pause=0.1post_speech_silence_duration=0.7early_transcription_on_silence=0.35

profile 的默认值合并逻辑见 _tuning_defaults()(基于BASE_TUNING_DEFAULTS叠加 profile 覆盖),单元测试test_parakeet_tuning_profile_sets_latency_oriented_defaults验证了 low-latency 的取值(test_fastapi_server_protocol.py)。

显式 flag 始终覆盖 profile 值

python example_fastapi_server/server.py \ --engine parakeet \ --model nvidia/parakeet-tdt-0.6b-v3 \ --use-main-model-for-realtime \ --profile parakeet-balanced \ --realtime-processing-pause 0.08 \ --post-speech-silence-duration 0.6

九、WebSocket 协议详解

9.1 上行:二进制音频包

浏览器向WS /ws/transcribe发送二进制音频包,格式为(编解码实现见 protocol.py):

  • 4 字节小端无符号元数据长度;
  • UTF-8 JSON 元数据;
  • 16-bit 小端单声道 PCM 音频字节。

元数据示例(来自 README):

{ "sampleRate": 48000, "channels": 1, "format": "pcm_s16le", "frames": 1920 }

服务端 packet_to_server_samples() 会校验包大小上限(--max-audio-packet-bytes)、通道数(≤8)、格式必须为pcm_s16leframes与实际负载长度一致,随后做多声道混音下采样,并把任意采样率用scipy.signal.resample_poly重采样到服务器内部统一的 16000 Hz。非法包会以error事件(where: audio_packet)返回。

9.2 下行:服务器事件

  • hello:分配clientIdsessionId,附公开设置、容量上限、受支持引擎;
  • ready:模型车道初始化完成,附公开设置与上限(广播给所有会话);
  • timeline:片段时序与唤醒词状态迁移;
  • realtime:会话内segmentId的 interim 文本;
  • final:同一segmentId的最终文本,UI 用其替换 interim 块;
  • status/warning/error/clear/pong/metrics

所有携带文本的事件都包含sessionId并只路由到该会话;clear只重置发起会话,并丢弃早前会话世代的陈旧结果(通过generation计数实现,见RealtimeSession.clear()handle_inference_result(),server.py)。实时文本若带realtime_callback=stabilized配置,事件还会附带stableText/unstableText/consensusText等结构化稳定化字段(见 server.py)。

9.3 文本命令

命令是 JSON 对象,例如{"type": "start"}。支持startstopclearping(回复pong)、metrics(回复会话级指标快照),未知命令返回error(实现见 server.py)。

十、引擎命名与规范化

服务器把引擎名直接透传给 RealtimeSTT,接受连字符命名并自动规范化(normalize_engine_name:去空白、转小写、-替换为_,见 protocol.py)。以下名字都可以直接使用(README):

  • faster_whisper
  • whisper_cpp
  • openai_whisper
  • parakeet/nvidia-parakeet
  • sherpa-onnx-parakeet
  • kroko-onnx/kroko/banafo-kroko
  • omnilingual-asr/omnilingual/meta-omnilingual-asr/omni-asr
  • cohere-transcribe
  • granite-speech
  • qwen3-asr
  • moonshine-streaming
  • sherpa-onnx-moonshine

引擎相关测试断言了kroko_onnx/kroko/banafo_kroko都在get_supported_transcription_engines()列表中(test_fastapi_server_protocol.py),并验证cohere-transcribemoonshine-streaming会被规范化(test_fastapi_server_protocol.py)。后端专属参数字典以 JSON 传入,例如:

python example_fastapi_server/server.py \ --engine cohere-transcribe \ --model CohereLabs/cohere-transcribe-03-2026 \ --engine-options '{"language":"en"}'

十一、健康检查与指标

curl http://localhost:8010/health curl http://localhost:8010/api/metrics
  • /health返回ok/ready、活跃会话/说话人数、拒绝会话数、调度器快照与启动错误(server.py);
  • /api/metrics返回每会话计数器(提交/完成/拒绝/合并/陈旧丢弃/强制 finalize)、队列深度、p50/p95 排队延迟与推理延迟、worker 忙闲比(RunningStats计算,server.py)。

部署时应把/health用于就绪探针、/api/metrics用于负载与延迟监控;模型缓存放在持久化存储上避免重启重下;对外暴露时置于反向代理之后;按所选引擎与硬件规模设定--max-sessions--max-active-speakers、队列深度与模型车道数(详见 docs/fastapi-server.md 的部署注意事项)。

十二、测试与性能基准

12.1 快速单元测试(不加载 ASR 模型)

使用假调度器,覆盖协议编解码、队列合并/轮转/陈旧丢弃、运行时设置契约、引擎名规范化、profile 默认值等:

python -m unittest -v \ tests.unit.test_fastapi_server_protocol \ tests.unit.test_fastapi_server_multi_user

测试入口在 tests/unit/test_fastapi_server_protocol.py,其中test_audio_packet_round_trip等用例直接验证了二进制包的编码/解码往返与各类非法包拒绝(test_fastapi_server_protocol.py)。

12.2 真实引擎多用户负载/质量/性能测试(opt-in)

该测试会并行地把 tests/unit/audio/asr-reference.wav 流式灌入多个会话,并将最终转写与 tests/unit/audio/asr-reference.expected_sentences.json 对比,最后打印每轮运行的时序报告:

REALTIMESTT_RUN_FASTAPI_MULTI_USER_PERF=1 \ python -m unittest -v tests.unit.test_fastapi_server_multi_user_asr_integration

REALTIMESTT_RUN_FASTAPI_MULTI_USER_ASR=1运行同一测试;当主要目标是测延迟时使用PERF名字。报告包含:首次实时延迟、首次最终延迟、音频上传结束后的最终延迟、首次录音/VAD 启动时机、实时/最终事件节奏、WER、调度器 p50/p95 延迟、合并计数与拒绝/丢弃计数。设置REALTIMESTT_FASTAPI_ASR_METRICS_JSON=/path/to/report.json可把报告写为 JSON。

常用覆盖变量(完整列表见 README):

REALTIMESTT_FASTAPI_ASR_CLIENTS=4 REALTIMESTT_FASTAPI_ASR_ENGINE=sherpa_onnx_moonshine REALTIMESTT_FASTAPI_ASR_MODEL=sherpa-onnx-moonshine-base-en-int8 REALTIMESTT_FASTAPI_ASR_REALTIME_ENGINE=sherpa_onnx_moonshine REALTIMESTT_FASTAPI_ASR_REALTIME_MODEL=sherpa-onnx-moonshine-tiny-en-int8 REALTIMESTT_FASTAPI_ASR_DOWNLOAD_ROOT=test-model-cache/sherpa-onnx REALTIMESTT_FASTAPI_ASR_DEVICE=cpu REALTIMESTT_FASTAPI_ASR_MAX_WER=0.30 REALTIMESTT_FASTAPI_ASR_ENGINE_OPTIONS='{"num_threads":4,"provider":"cpu"}' REALTIMESTT_FASTAPI_ASR_REALTIME_ENGINE_OPTIONS='{"num_threads":2,"provider":"cpu"}' REALTIMESTT_FASTAPI_ASR_REALTIME_PROCESSING_PAUSE=0.8 REALTIMESTT_FASTAPI_ASR_REALTIME_USE_SYLLABLE_BOUNDARIES=1 REALTIMESTT_FASTAPI_ASR_REALTIME_BOUNDARY_FOLLOWUP_DELAYS=0.1,0.2,0.4

引擎选项变量也接受key=value列表形式(如REALTIMESTT_FASTAPI_ASR_ENGINE_OPTIONS=num_threads=4,provider=cpu),更方便在 Windows cmd.exe 下使用。

Kroko-ONNX 性能测试走同一套通用变量:

REALTIMESTT_FASTAPI_ASR_ENGINE=kroko_onnx REALTIMESTT_FASTAPI_ASR_MODEL=test-model-cache/kroko-onnx/Kroko-EN-Community-64-L-Streaming-001.data REALTIMESTT_FASTAPI_ASR_REALTIME_ENGINE=kroko_onnx REALTIMESTT_FASTAPI_ASR_REALTIME_MODEL=test-model-cache/kroko-onnx/Kroko-EN-Community-64-L-Streaming-001.data REALTIMESTT_FASTAPI_ASR_DEVICE=cpu REALTIMESTT_FASTAPI_ASR_ENGINE_OPTIONS=provider=cpu,num_threads=2 REALTIMESTT_FASTAPI_ASR_REALTIME_ENGINE_OPTIONS=provider=cpu,num_threads=1

Windows cmd.exe 下,常见 4 客户端 sherpa-onnx Moonshine 配置可一键运行(脚本默认值见 run_multi_user_perf.cmd:4 客户端、Moonshine Base 最终 + Tiny 实时、结果写入test-results\fastapi-4-user-perf.json):

example_fastapi_server\run_multi_user_perf.cmd

只覆盖你关心的变量再调用它:

set REALTIMESTT_FASTAPI_ASR_CLIENTS=8 set REALTIMESTT_FASTAPI_ASR_METRICS_JSON=test-results\fastapi-8-user-perf.json example_fastapi_server\run_multi_user_perf.cmd

十三、部署注意事项小结

  • CUDA 密集型引擎(Parakeet、Qwen vLLM、大型 Transformers)使用 Linux;Omnilingual ASR 目前需要 Linux/WSL2 且 Python 3.11.x;
  • Kroko-ONNX建议按RealtimeSTT[kroko-builder,silero-onnx-cpu]+stt-install-kroko --build方式安装;Windows 上用 Python 3.12 x64 并先启动 Docker Desktop;
  • 模型缓存放持久化存储,避免重启重复下载;
  • 对外暴露时置于反向代理之后;
  • 依据所选引擎与硬件规模规划--max-sessions--max-active-speakers、队列深度与模型车道;
  • /health做就绪探针、/api/metrics做负载与延迟监控。

如需更多背景,可继续阅读仓库中的 MULTI_USER_IMPLEMENTATION_GUIDE.md、docs/fastapi-server.md、docs/testing.md 与 docs/transcription-engines.md,并对照核心库实现 RealtimeSTT/audio_recorder.py 理解会话内 recorder 状态机与 RealtimeSTT/transcription_engines/init.py 中的引擎工厂。

【免费下载链接】RealtimeSTTA robust, efficient, low-latency speech-to-text library with advanced voice activity detection, wake word activation and instant transcription.项目地址: https://gitcode.com/GitHub_Trending/re/RealtimeSTT

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询