mirror of
https://github.com/QuentinFuxa/WhisperLiveKit.git
synced 2026-03-07 22:33:36 +00:00
Compare commits
367 Commits
regularfry
...
0.2.6
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
12973711f6 | ||
|
|
909ac9dd41 | ||
|
|
d94a07d417 | ||
|
|
b32dd8bfc4 | ||
|
|
9feb0e597b | ||
|
|
9dab84a573 | ||
|
|
d089c7fce0 | ||
|
|
253a080df5 | ||
|
|
0c6e4b2aee | ||
|
|
e14bbde77d | ||
|
|
7496163467 | ||
|
|
696a94d1ce | ||
|
|
2699b0974c | ||
|
|
90c0250ba4 | ||
|
|
eb96153ffd | ||
|
|
47e3eb9b5b | ||
|
|
b8b07adeef | ||
|
|
d0e9e37ef6 | ||
|
|
820f92d8cb | ||
|
|
e42523af84 | ||
|
|
e2184d5e06 | ||
|
|
7fe0353260 | ||
|
|
0f2eba507e | ||
|
|
55e08474f3 | ||
|
|
28bdc52e1d | ||
|
|
e4221fa6c3 | ||
|
|
1652db9a2d | ||
|
|
601f17653a | ||
|
|
7718190fcd | ||
|
|
349c7dcb9e | ||
|
|
1c42b867cf | ||
|
|
d4771e563e | ||
|
|
b0a5fc0693 | ||
|
|
3b96fb8776 | ||
|
|
7f93c4b978 | ||
|
|
15c3df1cba | ||
|
|
7fb8e66c01 | ||
|
|
728e1f1290 | ||
|
|
87b9ed6ecd | ||
|
|
38b4ebe8ba | ||
|
|
d098af3185 | ||
|
|
4e56130a40 | ||
|
|
2bbdc70187 | ||
|
|
b678a55f63 | ||
|
|
5491964e81 | ||
|
|
b05297a96d | ||
|
|
197293e25e | ||
|
|
ba41c4ab56 | ||
|
|
bda72b8bc0 | ||
|
|
bb6b9f4cb1 | ||
|
|
e40b5a3ea0 | ||
|
|
4cfed6e98e | ||
|
|
687e3dd5e2 | ||
|
|
e4140cd299 | ||
|
|
8e056cbdf2 | ||
|
|
9dcfb38967 | ||
|
|
47b9235d70 | ||
|
|
f3cd53a4db | ||
|
|
dbdb4ea66c | ||
|
|
00424d7ca3 | ||
|
|
4b738d6f63 | ||
|
|
8a5e2adb1e | ||
|
|
f85329e112 | ||
|
|
46efbdf1d9 | ||
|
|
8885ade003 | ||
|
|
2564928d83 | ||
|
|
56114d3071 | ||
|
|
5b9977c9af | ||
|
|
12a544164f | ||
|
|
2ca1156b7e | ||
|
|
3ad3683ca7 | ||
|
|
1599bd87a0 | ||
|
|
90623400a4 | ||
|
|
64e44fb24f | ||
|
|
156b9a133f | ||
|
|
df8cb23848 | ||
|
|
9ff513093b | ||
|
|
17184e552c | ||
|
|
aad2c55d8c | ||
|
|
2f177c4a3b | ||
|
|
b362eccb23 | ||
|
|
5daaf77258 | ||
|
|
36cc4412c3 | ||
|
|
e1d4bf7e94 | ||
|
|
62bf28949e | ||
|
|
25526b3aa2 | ||
|
|
1e3fab9550 | ||
|
|
f25de6d8a4 | ||
|
|
8a175e79d8 | ||
|
|
dc37b44486 | ||
|
|
2d1df92aa7 | ||
|
|
2c1a603e38 | ||
|
|
774cee036b | ||
|
|
d22916988e | ||
|
|
5b8ad94dde | ||
|
|
f668570292 | ||
|
|
7c0768e8f3 | ||
|
|
b42d8b2692 | ||
|
|
0cd885247c | ||
|
|
8e30e8010a | ||
|
|
bfec335a5f | ||
|
|
6867041254 | ||
|
|
e165916952 | ||
|
|
8532a91c7a | ||
|
|
b01b81bad0 | ||
|
|
0f79d442ee | ||
|
|
c9f60504e3 | ||
|
|
993a83546a | ||
|
|
eabd1b199a | ||
|
|
f7644268c1 | ||
|
|
34e8fe260e | ||
|
|
debfefaf3e | ||
|
|
101ca9ef90 | ||
|
|
94bb05d53e | ||
|
|
6797b88176 | ||
|
|
46770efd6c | ||
|
|
b23ef3ec3e | ||
|
|
fa29a24abe | ||
|
|
fea3c3553c | ||
|
|
d6d65a663b | ||
|
|
083d5b2f44 | ||
|
|
8e4674b093 | ||
|
|
bc7c32100f | ||
|
|
c4150894af | ||
|
|
25bf242ce1 | ||
|
|
14cc601a5c | ||
|
|
34d5d513fa | ||
|
|
2ab3dac948 | ||
|
|
b56fcffde1 | ||
|
|
2def194893 | ||
|
|
29978da301 | ||
|
|
b708890788 | ||
|
|
3ac4c514cf | ||
|
|
3c58bfcfa2 | ||
|
|
d53b7a323a | ||
|
|
02de5993e6 | ||
|
|
d94560ef37 | ||
|
|
f62baa80b7 | ||
|
|
0b43035701 | ||
|
|
704170ccf3 | ||
|
|
09279c572a | ||
|
|
23e41f993f | ||
|
|
c791b1e125 | ||
|
|
3de2990ec4 | ||
|
|
51e6a6f6f9 | ||
|
|
f6e53b2fab | ||
|
|
5d6f08ff7a | ||
|
|
583a26da88 | ||
|
|
5b3d8969e8 | ||
|
|
40cca184c1 | ||
|
|
47ed345f9e | ||
|
|
9c9c179684 | ||
|
|
b870c12f62 | ||
|
|
cfd5905fd4 | ||
|
|
2399487e45 | ||
|
|
afd88310fd | ||
|
|
080f446b0d | ||
|
|
8bd2b36488 | ||
|
|
25fd924bf9 | ||
|
|
ff8fd0ec72 | ||
|
|
e99f53e649 | ||
|
|
e9022894b2 | ||
|
|
ccf99cecdf | ||
|
|
40e2814cd7 | ||
|
|
cd29eace3d | ||
|
|
38cb54640f | ||
|
|
81268a7ca3 | ||
|
|
33cbd24964 | ||
|
|
e966e78584 | ||
|
|
e61d1d111f | ||
|
|
c13d36b5e7 | ||
|
|
5624c1f6b7 | ||
|
|
7679370cf6 | ||
|
|
5ca65e21b7 | ||
|
|
dc02bcdbdd | ||
|
|
4f87ac3ea4 | ||
|
|
eead544977 | ||
|
|
f4a57cd810 | ||
|
|
b768b219fe | ||
|
|
2fb386f94c | ||
|
|
cb5cf39336 | ||
|
|
3024a9bdb2 | ||
|
|
7b582f3f9f | ||
|
|
8ae38a48ef | ||
|
|
fc3ffada59 | ||
|
|
e3550ef07d | ||
|
|
b502c8c81d | ||
|
|
b37d3cafb3 | ||
|
|
d304011aac | ||
|
|
597772c6c5 | ||
|
|
a656ccae72 | ||
|
|
e910873312 | ||
|
|
2a869cd509 | ||
|
|
d053bac871 | ||
|
|
e486ef8d98 | ||
|
|
0a1fb08371 | ||
|
|
ddb8860528 | ||
|
|
2e19516b3e | ||
|
|
3c7bc6f472 | ||
|
|
2d2a4967e6 | ||
|
|
7e880e039e | ||
|
|
627386a8a4 | ||
|
|
14af47e84b | ||
|
|
00eb4a0a4f | ||
|
|
2f87e592e0 | ||
|
|
56717b094f | ||
|
|
7b1c88589e | ||
|
|
72ce8d0e3f | ||
|
|
09090aa3f5 | ||
|
|
d3960ffef9 | ||
|
|
247582fb33 | ||
|
|
091d5d7bf5 | ||
|
|
9d5d6d8031 | ||
|
|
8aa3c760c7 | ||
|
|
f925ef3786 | ||
|
|
2ced4fef20 | ||
|
|
5b9b9328e0 | ||
|
|
d89622b9c2 | ||
|
|
d4096e7e11 | ||
|
|
296327071d | ||
|
|
34b707d84e | ||
|
|
f200f2cad4 | ||
|
|
8c6d39162f | ||
|
|
e3adc379ed | ||
|
|
90f24ef537 | ||
|
|
e4c84346c9 | ||
|
|
cf7944f13d | ||
|
|
d7c945dcce | ||
|
|
fa39eda923 | ||
|
|
01f02b066a | ||
|
|
a93bae69a5 | ||
|
|
f21dad559d | ||
|
|
97c0ae6154 | ||
|
|
09d40a7de8 | ||
|
|
2608abf0f3 | ||
|
|
58eba2a1f6 | ||
|
|
450c93fef8 | ||
|
|
1ffa2fa224 | ||
|
|
dc24366580 | ||
|
|
6121083549 | ||
|
|
0ecac75455 | ||
|
|
525abcbca7 | ||
|
|
365e7c882f | ||
|
|
84b09bb2cc | ||
|
|
4601e97221 | ||
|
|
15089c80fd | ||
|
|
788fe1c676 | ||
|
|
d623578d95 | ||
|
|
149d2ee44c | ||
|
|
adaca751ce | ||
|
|
eb989038bd | ||
|
|
1f6119e405 | ||
|
|
f7f1f259c1 | ||
|
|
b82cc3b613 | ||
|
|
46f7f9cbd1 | ||
|
|
48c111f494 | ||
|
|
54628274d6 | ||
|
|
0d874fb515 | ||
|
|
4d1aa4421a | ||
|
|
f4d98e2c8c | ||
|
|
15205f31d1 | ||
|
|
b1f7034577 | ||
|
|
23dee02d56 | ||
|
|
efd80095a7 | ||
|
|
f4d3df3d87 | ||
|
|
9c7d429e15 | ||
|
|
611d33cba5 | ||
|
|
ab7c22d3e3 | ||
|
|
870a779666 | ||
|
|
c3d72cae7c | ||
|
|
4622fe7aff | ||
|
|
8ee1488c08 | ||
|
|
77d43885a3 | ||
|
|
04170153e0 | ||
|
|
baddf0284b | ||
|
|
6e0f1dda25 | ||
|
|
c66794e1f5 | ||
|
|
f0eaffacd3 | ||
|
|
69a2ed6bfb | ||
|
|
25eb276794 | ||
|
|
9f262813ec | ||
|
|
4293580581 | ||
|
|
42d2784c20 | ||
|
|
7fad0a3ee2 | ||
|
|
27d2db77f7 | ||
|
|
fba37eba0a | ||
|
|
5523b51fd7 | ||
|
|
9bdb92e923 | ||
|
|
b51c8427f4 | ||
|
|
977436622a | ||
|
|
ce56264241 | ||
|
|
9cbac96c44 | ||
|
|
3f30d3de6e | ||
|
|
f884d1162d | ||
|
|
6ee91c3c93 | ||
|
|
f52a5ae3c2 | ||
|
|
0ff6067f37 | ||
|
|
da6c8d25e4 | ||
|
|
aa0ba598f0 | ||
|
|
b7a2d23a18 | ||
|
|
58e48bb717 | ||
|
|
6a04ddbed2 | ||
|
|
aa4d2599cc | ||
|
|
5fdb08edae | ||
|
|
4cb3660666 | ||
|
|
122368bff3 | ||
|
|
0d833eaea2 | ||
|
|
c960d1571d | ||
|
|
1aa1b9ea99 | ||
|
|
99019f1dd7 | ||
|
|
1cea20a42d | ||
|
|
50bbd26517 | ||
|
|
cf5d1cf013 | ||
|
|
0553b75415 | ||
|
|
baa01728be | ||
|
|
8dcebd9329 | ||
|
|
bfe973a0d2 | ||
|
|
87cab7c280 | ||
|
|
bee27c68e6 | ||
|
|
aa4480b138 | ||
|
|
cc92e97e17 | ||
|
|
8c6c0104a3 | ||
|
|
494b6e3ca9 | ||
|
|
d045137ba8 | ||
|
|
54a37fbcb6 | ||
|
|
104f7bde03 | ||
|
|
e6648e4f46 | ||
|
|
863242f107 | ||
|
|
d48895c343 | ||
|
|
8cfd8d85a3 | ||
|
|
e1b0e146a5 | ||
|
|
e3dc524783 | ||
|
|
2de090023c | ||
|
|
e25ad4fcd7 | ||
|
|
63870987c0 | ||
|
|
7eeb73f4d4 | ||
|
|
d665f9a96e | ||
|
|
827425bb91 | ||
|
|
4a89935ee5 | ||
|
|
4c17b56041 | ||
|
|
52da12120c | ||
|
|
7edc534f8a | ||
|
|
14c2bbef87 | ||
|
|
36bf3a32d4 | ||
|
|
2ec2266929 | ||
|
|
f3907703ed | ||
|
|
13fd21a201 | ||
|
|
84a999570a | ||
|
|
884958127f | ||
|
|
726fa574a2 | ||
|
|
333eea4b76 | ||
|
|
8d60fd3bf6 | ||
|
|
9c15262015 | ||
|
|
7bca7a2b8e | ||
|
|
264b8a32c2 | ||
|
|
706b7f847e | ||
|
|
c8123344c6 | ||
|
|
6b968c6e29 | ||
|
|
6fa008080a | ||
|
|
d543411bbd | ||
|
|
b2e4e9f727 | ||
|
|
324dee03e7 | ||
|
|
fe4207edca | ||
|
|
ea2a9ca2e6 | ||
|
|
c8c786af4f | ||
|
|
3fad8133b4 | ||
|
|
9556d07484 |
13
.gitignore
vendored
13
.gitignore
vendored
@@ -54,7 +54,6 @@ coverage.xml
|
|||||||
# Translations
|
# Translations
|
||||||
*.mo
|
*.mo
|
||||||
*.pot
|
*.pot
|
||||||
|
|
||||||
# Django stuff:
|
# Django stuff:
|
||||||
*.log
|
*.log
|
||||||
local_settings.py
|
local_settings.py
|
||||||
@@ -127,3 +126,15 @@ dmypy.json
|
|||||||
|
|
||||||
# Pyre type checker
|
# Pyre type checker
|
||||||
.pyre/
|
.pyre/
|
||||||
|
|
||||||
|
*.wav
|
||||||
|
run_*.sh
|
||||||
|
|
||||||
|
# Downloaded models
|
||||||
|
*.pt
|
||||||
|
|
||||||
|
# Debug & testing
|
||||||
|
test_*.py
|
||||||
|
launch.json
|
||||||
|
.DS_Store
|
||||||
|
test/*
|
||||||
46
CONTRIBUTING.md
Normal file
46
CONTRIBUTING.md
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
# Contributing
|
||||||
|
|
||||||
|
Thank you for considering contributing ! We appreciate your time and effort to help make this project better.
|
||||||
|
|
||||||
|
## Before You Start
|
||||||
|
|
||||||
|
1. **Search for Existing Issues or Discussions:**
|
||||||
|
- Before opening a new issue or discussion, please check if there's already an existing one related to your topic. This helps avoid duplicates and keeps discussions centralized.
|
||||||
|
|
||||||
|
2. **Discuss Your Contribution:**
|
||||||
|
- If you plan to make a significant change, it's advisable to discuss it in an issue first. This ensures that your contribution aligns with the project's goals and avoids duplicated efforts.
|
||||||
|
|
||||||
|
3. **General questions about whisper streaming web:**
|
||||||
|
- For general questions about whisper streaming web, use the discussion space on GitHub. This helps in fostering a collaborative environment and encourages knowledge-sharing.
|
||||||
|
|
||||||
|
## Opening Issues
|
||||||
|
|
||||||
|
If you encounter a problem with WhisperLiveKit or want to suggest an improvement, please follow these guidelines when opening an issue:
|
||||||
|
|
||||||
|
- **Bug Reports:**
|
||||||
|
- Clearly describe the error. **Please indicate the parameters you use, especially the model(s)**
|
||||||
|
- Provide a minimal, reproducible example that demonstrates the issue.
|
||||||
|
|
||||||
|
- **Feature Requests:**
|
||||||
|
- Clearly outline the new feature you are proposing.
|
||||||
|
- Explain how it would benefit the project.
|
||||||
|
|
||||||
|
## Opening Pull Requests
|
||||||
|
|
||||||
|
We welcome and appreciate contributions! To ensure a smooth review process, please follow these guidelines when opening a pull request:
|
||||||
|
|
||||||
|
- **Commit Messages:**
|
||||||
|
- Write clear and concise commit messages, explaining the purpose of each change.
|
||||||
|
|
||||||
|
- **Documentation:**
|
||||||
|
- Update documentation when introducing new features or making changes that impact existing functionality.
|
||||||
|
|
||||||
|
- **Tests:**
|
||||||
|
- If applicable, add or update tests to cover your changes.
|
||||||
|
|
||||||
|
- **Discuss Before Major Changes:**
|
||||||
|
- If your PR includes significant changes, discuss it in an issue first.
|
||||||
|
|
||||||
|
## Thank You
|
||||||
|
|
||||||
|
Your contributions make WhisperLiveKit better for everyone. Thank you for your time and dedication!
|
||||||
84
Dockerfile
Normal file
84
Dockerfile
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
FROM nvidia/cuda:12.8.1-cudnn-runtime-ubuntu22.04
|
||||||
|
|
||||||
|
ENV DEBIAN_FRONTEND=noninteractive
|
||||||
|
ENV PYTHONUNBUFFERED=1
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
ARG EXTRAS
|
||||||
|
ARG HF_PRECACHE_DIR
|
||||||
|
ARG HF_TKN_FILE
|
||||||
|
|
||||||
|
# Install system dependencies
|
||||||
|
#RUN apt-get update && \
|
||||||
|
# apt-get install -y ffmpeg git && \
|
||||||
|
# apt-get clean && \
|
||||||
|
# rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
|
# 2) Install system dependencies + Python + pip
|
||||||
|
RUN apt-get update && \
|
||||||
|
apt-get install -y --no-install-recommends \
|
||||||
|
python3 \
|
||||||
|
python3-pip \
|
||||||
|
ffmpeg \
|
||||||
|
git \
|
||||||
|
build-essential \
|
||||||
|
python3-dev && \
|
||||||
|
rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
|
RUN pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu128
|
||||||
|
|
||||||
|
COPY . .
|
||||||
|
|
||||||
|
# Install WhisperLiveKit directly, allowing for optional dependencies
|
||||||
|
# Note: For gates models, need to add your HF toke. See README.md
|
||||||
|
# for more details.
|
||||||
|
RUN if [ -n "$EXTRAS" ]; then \
|
||||||
|
echo "Installing with extras: [$EXTRAS]"; \
|
||||||
|
pip install --no-cache-dir .[$EXTRAS]; \
|
||||||
|
else \
|
||||||
|
echo "Installing base package only"; \
|
||||||
|
pip install --no-cache-dir .; \
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Enable in-container caching for Hugging Face models by:
|
||||||
|
# Note: If running multiple containers, better to map a shared
|
||||||
|
# bucket.
|
||||||
|
#
|
||||||
|
# A) Make the cache directory persistent via an anonymous volume.
|
||||||
|
# Note: This only persists for a single, named container. This is
|
||||||
|
# only for convenience at de/test stage.
|
||||||
|
# For prod, it is better to use a named volume via host mount/k8s.
|
||||||
|
VOLUME ["/root/.cache/huggingface/hub"]
|
||||||
|
|
||||||
|
# or
|
||||||
|
# B) Conditionally copy a local pre-cache from the build context to the
|
||||||
|
# container's cache via the HF_PRECACHE_DIR build-arg.
|
||||||
|
# WARNING: This will copy ALL files in the pre-cache location.
|
||||||
|
|
||||||
|
# Conditionally copy a cache directory if provided
|
||||||
|
RUN if [ -n "$HF_PRECACHE_DIR" ]; then \
|
||||||
|
echo "Copying Hugging Face cache from $HF_PRECACHE_DIR"; \
|
||||||
|
mkdir -p /root/.cache/huggingface/hub && \
|
||||||
|
cp -r $HF_PRECACHE_DIR/* /root/.cache/huggingface/hub; \
|
||||||
|
else \
|
||||||
|
echo "No local Hugging Face cache specified, skipping copy"; \
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Conditionally copy a Hugging Face token if provided
|
||||||
|
|
||||||
|
RUN if [ -n "$HF_TKN_FILE" ]; then \
|
||||||
|
echo "Copying Hugging Face token from $HF_TKN_FILE"; \
|
||||||
|
mkdir -p /root/.cache/huggingface && \
|
||||||
|
cp $HF_TKN_FILE /root/.cache/huggingface/token; \
|
||||||
|
else \
|
||||||
|
echo "No Hugging Face token file specified, skipping token setup"; \
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Expose port for the transcription server
|
||||||
|
EXPOSE 8000
|
||||||
|
|
||||||
|
ENTRYPOINT ["whisperlivekit-server", "--host", "0.0.0.0"]
|
||||||
|
|
||||||
|
# Default args
|
||||||
|
CMD ["--model", "base"]
|
||||||
57
LICENSE
57
LICENSE
@@ -1,21 +1,52 @@
|
|||||||
|
# License
|
||||||
|
|
||||||
|
## Main Software License
|
||||||
|
|
||||||
MIT License
|
MIT License
|
||||||
|
|
||||||
Copyright (c) 2023 ÚFAL
|
Copyright (c) 2025 Quentin Fuxa.
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
in the Software without restriction, including without limitation the rights
|
in the Software without restriction, including without limitation the rights
|
||||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
copies of the Software, and to permit persons to whom the Software is
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
furnished to do so, subject to the following conditions:
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be included in all
|
The above copyright notice and this permission notice shall be included in all
|
||||||
copies or substantial portions of the Software.
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
SOFTWARE.
|
SOFTWARE.
|
||||||
|
|
||||||
|
## SimulStreaming Backend License
|
||||||
|
|
||||||
|
**When using the SimulStreaming backend (SimulWhisper), additional licensing terms apply:**
|
||||||
|
|
||||||
|
SimulStreaming (https://github.com/ufal/SimulStreaming) is dual-licensed:
|
||||||
|
|
||||||
|
### 🔹 Non-Commercial Use
|
||||||
|
You may use SimulStreaming under the **PolyForm Noncommercial License 1.0.0** if you obtain the code through the GitHub repository. This license is **free of charge** and comes with **no obligations** for non-commercial users.
|
||||||
|
|
||||||
|
### 🔸 Commercial Use
|
||||||
|
Understanding who uses SimulStreaming commercially helps improve and prioritize development. Therefore, **registration is required** for those who acquire a commercial license.
|
||||||
|
|
||||||
|
Commercial licenses are planned to be **affordable** to SMEs and individuals. They are considering providing commercial licenses either for free or for a symbolic one-time fee, and may also provide additional support. You can share your preference via the [questionnaire](https://forms.cloud.microsoft.com/e/7tCxb4gJfB).
|
||||||
|
|
||||||
|
You can also leave your contact [there](https://forms.cloud.microsoft.com/e/7tCxb4gJfB) to be notified when commercial licenses become available.
|
||||||
|
|
||||||
|
**Contact for SimulStreaming licensing:**
|
||||||
|
[Dominik Macháček](https://ufal.mff.cuni.cz/dominik-machacek/), machacek@ufal.mff.cuni.cz
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Based on:
|
||||||
|
- **whisper_streaming** by ÚFAL – MIT License – https://github.com/ufal/whisper_streaming. The original work by ÚFAL. License: https://github.com/ufal/whisper_streaming/blob/main/LICENSE
|
||||||
|
- **silero-vad** by Snakers4 – MIT License – https://github.com/snakers4/silero-vad. The work by Snakers4 (silero-vad). License: https://github.com/snakers4/silero-vad/blob/f6b1294cb27590fb2452899df98fb234dfef1134/LICENSE
|
||||||
|
- **Diart** by juanmc2005 – MIT License – https://github.com/juanmc2005/diart. The work in Diart by juanmc2005. License: https://github.com/juanmc2005/diart/blob/main/LICENSE
|
||||||
|
- **SimulStreaming** by ÚFAL – Dual License (PolyForm Noncommercial License 1.0.0 / Commercial License) – https://github.com/ufal/SimulStreaming
|
||||||
396
README.md
396
README.md
@@ -1,247 +1,241 @@
|
|||||||
# whisper_streaming
|
<h1 align="center">WhisperLiveKit</h1>
|
||||||
Whisper realtime streaming for long speech-to-text transcription and translation
|
|
||||||
|
|
||||||
**Turning Whisper into Real-Time Transcription System**
|
<p align="center">
|
||||||
|
<img src="https://raw.githubusercontent.com/QuentinFuxa/WhisperLiveKit/refs/heads/main/demo.png" alt="WhisperLiveKit Demo" width="730">
|
||||||
|
</p>
|
||||||
|
|
||||||
Demonstration paper, by [Dominik Macháček](https://ufal.mff.cuni.cz/dominik-machacek), [Raj Dabre](https://prajdabre.github.io/), [Ondřej Bojar](https://ufal.mff.cuni.cz/ondrej-bojar), 2023
|
<p align="center"><b>Real-time, Fully Local Speech-to-Text with Speaker Identification</b></p>
|
||||||
|
|
||||||
Abstract: Whisper is one of the recent state-of-the-art multilingual speech recognition and translation models, however, it is not designed for real-time transcription. In this paper, we build on top of Whisper and create Whisper-Streaming, an implementation of real-time speech transcription and translation of Whisper-like models. Whisper-Streaming uses local agreement policy with self-adaptive latency to enable streaming transcription. We show that Whisper-Streaming achieves high quality and 3.3 seconds latency on unsegmented long-form speech transcription test set, and we demonstrate its robustness and practical usability as a component in live transcription service at a multilingual conference.
|
<p align="center">
|
||||||
|
<a href="https://pypi.org/project/whisperlivekit/"><img alt="PyPI Version" src="https://img.shields.io/pypi/v/whisperlivekit?color=g"></a>
|
||||||
|
<a href="https://pepy.tech/project/whisperlivekit"><img alt="PyPI Downloads" src="https://static.pepy.tech/personalized-badge/whisperlivekit?period=total&units=international_system&left_color=grey&right_color=brightgreen&left_text=downloads"></a>
|
||||||
|
<a href="https://pypi.org/project/whisperlivekit/"><img alt="Python Versions" src="https://img.shields.io/badge/python-3.9--3.13-dark_green"></a>
|
||||||
|
<a href="https://github.com/QuentinFuxa/WhisperLiveKit/blob/main/LICENSE"><img alt="License" src="https://img.shields.io/badge/License-MIT/Dual Licensed-dark_green"></a>
|
||||||
|
</p>
|
||||||
|
|
||||||
|
|
||||||
[Paper PDF](https://aclanthology.org/2023.ijcnlp-demo.3.pdf), [Demo video](https://player.vimeo.com/video/840442741)
|
Real-time speech transcription directly to your browser, with a ready-to-use backend+server and a simple frontend. ✨
|
||||||
|
|
||||||
[Slides](http://ufallab.ms.mff.cuni.cz/~machacek/pre-prints/AACL23-2.11.2023-Turning-Whisper-oral.pdf) -- 15 minutes oral presentation at IJCNLP-AACL 2023
|
#### Powered by Leading Research:
|
||||||
|
|
||||||
Please, cite us. [ACL Anthology](https://aclanthology.org/2023.ijcnlp-demo.3/), [Bibtex citation](https://aclanthology.org/2023.ijcnlp-demo.3.bib):
|
- [SimulStreaming](https://github.com/ufal/SimulStreaming) (SOTA 2025) - Ultra-low latency transcription with AlignAtt policy
|
||||||
|
- [WhisperStreaming](https://github.com/ufal/whisper_streaming) (SOTA 2023) - Low latency transcription with LocalAgreement policy
|
||||||
|
- [Streaming Sortformer](https://arxiv.org/abs/2507.18446) (SOTA 2025) - Advanced real-time speaker diarization
|
||||||
|
- [Diart](https://github.com/juanmc2005/diart) (SOTA 2021) - Real-time speaker diarization
|
||||||
|
- [Silero VAD](https://github.com/snakers4/silero-vad) (2024) - Enterprise-grade Voice Activity Detection
|
||||||
|
|
||||||
```
|
|
||||||
@inproceedings{machacek-etal-2023-turning,
|
> **Why not just run a simple Whisper model on every audio batch?** Whisper is designed for complete utterances, not real-time chunks. Processing small segments loses context, cuts off words mid-syllable, and produces poor transcription. WhisperLiveKit uses state-of-the-art simultaneous speech research for intelligent buffering and incremental processing.
|
||||||
title = "Turning Whisper into Real-Time Transcription System",
|
|
||||||
author = "Mach{\'a}{\v{c}}ek, Dominik and
|
|
||||||
Dabre, Raj and
|
### Architecture
|
||||||
Bojar, Ond{\v{r}}ej",
|
|
||||||
editor = "Saha, Sriparna and
|
<img alt="Architecture" src="https://raw.githubusercontent.com/QuentinFuxa/WhisperLiveKit/refs/heads/main/architecture.png" />
|
||||||
Sujaini, Herry",
|
|
||||||
booktitle = "Proceedings of the 13th International Joint Conference on Natural Language Processing and the 3rd Conference of the Asia-Pacific Chapter of the Association for Computational Linguistics: System Demonstrations",
|
*The backend supports multiple concurrent users. Voice Activity Detection reduces overhead when no voice is detected.*
|
||||||
month = nov,
|
|
||||||
year = "2023",
|
### Installation & Quick Start
|
||||||
address = "Bali, Indonesia",
|
|
||||||
publisher = "Association for Computational Linguistics",
|
```bash
|
||||||
url = "https://aclanthology.org/2023.ijcnlp-demo.3",
|
pip install whisperlivekit
|
||||||
pages = "17--24",
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Installation
|
> **FFmpeg is required** and must be installed before using WhisperLiveKit
|
||||||
|
>
|
||||||
|
> | OS | How to install |
|
||||||
|
> |-----------|-------------|
|
||||||
|
> | Ubuntu/Debian | `sudo apt install ffmpeg` |
|
||||||
|
> | MacOS | `brew install ffmpeg` |
|
||||||
|
> | Windows | Download .exe from https://ffmpeg.org/download.html and add to PATH |
|
||||||
|
|
||||||
1) ``pip install librosa soundfile`` -- audio processing library
|
#### Quick Start
|
||||||
|
1. **Start the transcription server:**
|
||||||
|
```bash
|
||||||
|
whisperlivekit-server --model base --language en
|
||||||
|
```
|
||||||
|
|
||||||
2) Whisper backend.
|
2. **Open your browser** and navigate to `http://localhost:8000`. Start speaking and watch your words appear in real-time!
|
||||||
|
|
||||||
Several alternative backends are integrated. The most recommended one is [faster-whisper](https://github.com/guillaumekln/faster-whisper) with GPU support. Follow their instructions for NVIDIA libraries -- we succeeded with CUDNN 8.5.0 and CUDA 11.7. Install with `pip install faster-whisper`.
|
|
||||||
|
|
||||||
Alternative, less restrictive, but slower backend is [whisper-timestamped](https://github.com/linto-ai/whisper-timestamped): `pip install git+https://github.com/linto-ai/whisper-timestamped`
|
> - See [tokenizer.py](https://github.com/QuentinFuxa/WhisperLiveKit/blob/main/whisperlivekit/simul_whisper/whisper/tokenizer.py) for the list of all available languages.
|
||||||
|
> - For HTTPS requirements, see the **Parameters** section for SSL configuration options.
|
||||||
|
|
||||||
Thirdly, it's also possible to run this software from the [OpenAI Whisper API](https://platform.openai.com/docs/api-reference/audio/createTranscription). This solution is fast and requires no GPU, just a small VM will suffice, but you will need to pay OpenAI for api access. Also note that, since each audio fragment is processed multiple times, the [price](https://openai.com/pricing) will be higher than obvious from the pricing page, so keep an eye on costs while using. Setting a higher chunk-size will reduce costs significantly.
|
|
||||||
Install with: `pip install openai`
|
|
||||||
|
|
||||||
For running with the openai-api backend, make sure that your [OpenAI api key](https://platform.openai.com/api-keys) is set in the `OPENAI_API_KEY` environment variable. For example, before running, do: `export OPENAI_API_KEY=sk-xxx` with *sk-xxx* replaced with your api key.
|
#### Optional Dependencies
|
||||||
|
|
||||||
The backend is loaded only when chosen. The unused one does not have to be installed.
|
| Optional | `pip install` |
|
||||||
|
|-----------|-------------|
|
||||||
|
| Speaker diarization | `whisperlivekit[diarization]` |
|
||||||
|
| Original Whisper backend | `whisperlivekit[whisper]` |
|
||||||
|
| Improved timestamps backend | `whisperlivekit[whisper-timestamped]` |
|
||||||
|
| Apple Silicon optimization backend | `whisperlivekit[mlx-whisper]` |
|
||||||
|
| OpenAI API backend | `whisperlivekit[openai]` |
|
||||||
|
|
||||||
3) Optional, not recommended: sentence segmenter (aka sentence tokenizer)
|
See **Parameters & Configuration** below on how to use them.
|
||||||
|
|
||||||
Two buffer trimming options are integrated and evaluated. They have impact on
|
|
||||||
the quality and latency. The default "segment" option performs better according
|
> **Pyannote Models Setup** For diarization, you need access to pyannote.audio models:
|
||||||
to our tests and does not require any sentence segmentation installed.
|
> 1. [Accept user conditions](https://huggingface.co/pyannote/segmentation) for the `pyannote/segmentation` model
|
||||||
|
> 2. [Accept user conditions](https://huggingface.co/pyannote/segmentation-3.0) for the `pyannote/segmentation-3.0` model
|
||||||
|
> 3. [Accept user conditions](https://huggingface.co/pyannote/embedding) for the `pyannote/embedding` model
|
||||||
|
>4. Login with HuggingFace:
|
||||||
|
> ```bash
|
||||||
|
> huggingface-cli login
|
||||||
|
> ```
|
||||||
|
|
||||||
The other option, "sentence" -- trimming at the end of confirmed sentences,
|
## 💻 Usage Examples
|
||||||
requires sentence segmenter installed. It splits punctuated text to sentences by full
|
|
||||||
stops, avoiding the dots that are not full stops. The segmenters are language
|
|
||||||
specific. The unused one does not have to be installed. We integrate the
|
|
||||||
following segmenters, but suggestions for better alternatives are welcome.
|
|
||||||
|
|
||||||
- `pip install opus-fast-mosestokenizer` for the languages with codes `as bn ca cs de el en es et fi fr ga gu hi hu is it kn lt lv ml mni mr nl or pa pl pt ro ru sk sl sv ta te yue zh`
|
#### Command-line Interface
|
||||||
|
|
||||||
- `pip install tokenize_uk` for Ukrainian -- `uk`
|
Start the transcription server with various options:
|
||||||
|
|
||||||
- for other languages, we integrate a good performing multi-lingual model of `wtpslit`. It requires `pip install torch wtpsplit`, and its neural model `wtp-canine-s-12l-no-adapters`. It is downloaded to the default huggingface cache during the first use.
|
```bash
|
||||||
|
# SimulStreaming backend for ultra-low latency
|
||||||
|
whisperlivekit-server --backend simulstreaming --model large-v3
|
||||||
|
|
||||||
- we did not find a segmenter for languages `as ba bo br bs fo haw hr ht jw lb ln lo mi nn oc sa sd sn so su sw tk tl tt` that are supported by Whisper and not by wtpsplit. The default fallback option for them is wtpsplit with unspecified language. Alternative suggestions welcome.
|
# Advanced configuration with diarization
|
||||||
|
whisperlivekit-server --host 0.0.0.0 --port 8000 --model medium --diarization --language fr
|
||||||
In case of installation issues of opus-fast-mosestokenizer, especially on Windows and Mac, we recommend using only the "segment" option that does not require it.
|
|
||||||
|
|
||||||
## Usage
|
|
||||||
|
|
||||||
### Real-time simulation from audio file
|
|
||||||
|
|
||||||
```
|
|
||||||
usage: whisper_online.py [-h] [--min-chunk-size MIN_CHUNK_SIZE] [--model {tiny.en,tiny,base.en,base,small.en,small,medium.en,medium,large-v1,large-v2,large-v3,large}] [--model_cache_dir MODEL_CACHE_DIR] [--model_dir MODEL_DIR] [--lan LAN] [--task {transcribe,translate}]
|
|
||||||
[--backend {faster-whisper,whisper_timestamped,openai-api}] [--vad] [--buffer_trimming {sentence,segment}] [--buffer_trimming_sec BUFFER_TRIMMING_SEC] [--start_at START_AT] [--offline] [--comp_unaware]
|
|
||||||
audio_path
|
|
||||||
|
|
||||||
positional arguments:
|
|
||||||
audio_path Filename of 16kHz mono channel wav, on which live streaming is simulated.
|
|
||||||
|
|
||||||
options:
|
|
||||||
-h, --help show this help message and exit
|
|
||||||
--min-chunk-size MIN_CHUNK_SIZE
|
|
||||||
Minimum audio chunk size in seconds. It waits up to this time to do processing. If the processing takes shorter time, it waits, otherwise it processes the whole segment that was received by this time.
|
|
||||||
--model {tiny.en,tiny,base.en,base,small.en,small,medium.en,medium,large-v1,large-v2,large-v3,large}
|
|
||||||
Name size of the Whisper model to use (default: large-v2). The model is automatically downloaded from the model hub if not present in model cache dir.
|
|
||||||
--model_cache_dir MODEL_CACHE_DIR
|
|
||||||
Overriding the default model cache dir where models downloaded from the hub are saved
|
|
||||||
--model_dir MODEL_DIR
|
|
||||||
Dir where Whisper model.bin and other files are saved. This option overrides --model and --model_cache_dir parameter.
|
|
||||||
--lan LAN, --language LAN
|
|
||||||
Source language code, e.g. en,de,cs, or 'auto' for language detection.
|
|
||||||
--task {transcribe,translate}
|
|
||||||
Transcribe or translate.
|
|
||||||
--backend {faster-whisper,whisper_timestamped,openai-api}
|
|
||||||
Load only this backend for Whisper processing.
|
|
||||||
--vad Use VAD = voice activity detection, with the default parameters.
|
|
||||||
--buffer_trimming {sentence,segment}
|
|
||||||
Buffer trimming strategy -- trim completed sentences marked with punctuation mark and detected by sentence segmenter, or the completed segments returned by Whisper. Sentence segmenter must be installed for "sentence" option.
|
|
||||||
--buffer_trimming_sec BUFFER_TRIMMING_SEC
|
|
||||||
Buffer trimming length threshold in seconds. If buffer length is longer, trimming sentence/segment is triggered.
|
|
||||||
--start_at START_AT Start processing audio at this time.
|
|
||||||
--offline Offline mode.
|
|
||||||
--comp_unaware Computationally unaware simulation.
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Example:
|
|
||||||
|
|
||||||
It simulates realtime processing from a pre-recorded mono 16k wav file.
|
#### Python API Integration (Backend)
|
||||||
|
Check [basic_server](https://github.com/QuentinFuxa/WhisperLiveKit/blob/main/whisperlivekit/basic_server.py) for a more complete example of how to use the functions and classes.
|
||||||
```
|
|
||||||
python3 whisper_online.py en-demo16.wav --language en --min-chunk-size 1 > out.txt
|
|
||||||
```
|
|
||||||
|
|
||||||
Simulation modes:
|
|
||||||
|
|
||||||
- default mode, no special option: real-time simulation from file, computationally aware. The chunk size is `MIN_CHUNK_SIZE` or larger, if more audio arrived during last update computation.
|
|
||||||
|
|
||||||
- `--comp_unaware` option: computationally unaware simulation. It means that the timer that counts the emission times "stops" when the model is computing. The chunk size is always `MIN_CHUNK_SIZE`. The latency is caused only by the model being unable to confirm the output, e.g. because of language ambiguity etc., and not because of slow hardware or suboptimal implementation. We implement this feature for finding the lower bound for latency.
|
|
||||||
|
|
||||||
- `--start_at START_AT`: Start processing audio at this time. The first update receives the whole audio by `START_AT`. It is useful for debugging, e.g. when we observe a bug in a specific time in audio file, and want to reproduce it quickly, without long waiting.
|
|
||||||
|
|
||||||
- `--offline` option: It processes the whole audio file at once, in offline mode. We implement it to find out the lowest possible WER on given audio file.
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### Output format
|
|
||||||
|
|
||||||
```
|
|
||||||
2691.4399 300 1380 Chairman, thank you.
|
|
||||||
6914.5501 1940 4940 If the debate today had a
|
|
||||||
9019.0277 5160 7160 the subject the situation in
|
|
||||||
10065.1274 7180 7480 Gaza
|
|
||||||
11058.3558 7480 9460 Strip, I might
|
|
||||||
12224.3731 9460 9760 have
|
|
||||||
13555.1929 9760 11060 joined Mrs.
|
|
||||||
14928.5479 11140 12240 De Kaiser and all the
|
|
||||||
16588.0787 12240 12560 other
|
|
||||||
18324.9285 12560 14420 colleagues across the
|
|
||||||
```
|
|
||||||
|
|
||||||
[See description here](https://github.com/ufal/whisper_streaming/blob/d915d790a62d7be4e7392dde1480e7981eb142ae/whisper_online.py#L361)
|
|
||||||
|
|
||||||
### As a module
|
|
||||||
|
|
||||||
TL;DR: use OnlineASRProcessor object and its methods insert_audio_chunk and process_iter.
|
|
||||||
|
|
||||||
The code whisper_online.py is nicely commented, read it as the full documentation.
|
|
||||||
|
|
||||||
|
|
||||||
This pseudocode describes the interface that we suggest for your implementation. You can implement any features that you need for your application.
|
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from whisper_online import *
|
from whisperlivekit import TranscriptionEngine, AudioProcessor, parse_args
|
||||||
|
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
|
||||||
|
from fastapi.responses import HTMLResponse
|
||||||
|
from contextlib import asynccontextmanager
|
||||||
|
import asyncio
|
||||||
|
|
||||||
src_lan = "en" # source language
|
transcription_engine = None
|
||||||
tgt_lan = "en" # target language -- same as source for ASR, "en" if translate task is used
|
|
||||||
|
|
||||||
asr = FasterWhisperASR(lan, "large-v2") # loads and wraps Whisper model
|
@asynccontextmanager
|
||||||
# set options:
|
async def lifespan(app: FastAPI):
|
||||||
# asr.set_translate_task() # it will translate from lan into English
|
global transcription_engine
|
||||||
# asr.use_vad() # set using VAD
|
transcription_engine = TranscriptionEngine(model="medium", diarization=True, lan="en")
|
||||||
|
yield
|
||||||
|
|
||||||
online = OnlineASRProcessor(asr) # create processing object with default buffer trimming option
|
app = FastAPI(lifespan=lifespan)
|
||||||
|
|
||||||
while audio_has_not_ended: # processing loop:
|
async def handle_websocket_results(websocket: WebSocket, results_generator):
|
||||||
a = # receive new audio chunk (and e.g. wait for min_chunk_size seconds first, ...)
|
async for response in results_generator:
|
||||||
online.insert_audio_chunk(a)
|
await websocket.send_json(response)
|
||||||
o = online.process_iter()
|
await websocket.send_json({"type": "ready_to_stop"})
|
||||||
print(o) # do something with current partial output
|
|
||||||
# at the end of this audio processing
|
|
||||||
o = online.finish()
|
|
||||||
print(o) # do something with the last output
|
|
||||||
|
|
||||||
|
@app.websocket("/asr")
|
||||||
|
async def websocket_endpoint(websocket: WebSocket):
|
||||||
|
global transcription_engine
|
||||||
|
|
||||||
online.init() # refresh if you're going to re-use the object for the next audio
|
# Create a new AudioProcessor for each connection, passing the shared engine
|
||||||
|
audio_processor = AudioProcessor(transcription_engine=transcription_engine)
|
||||||
|
results_generator = await audio_processor.create_tasks()
|
||||||
|
results_task = asyncio.create_task(handle_websocket_results(websocket, results_generator))
|
||||||
|
await websocket.accept()
|
||||||
|
while True:
|
||||||
|
message = await websocket.receive_bytes()
|
||||||
|
await audio_processor.process_audio(message)
|
||||||
```
|
```
|
||||||
|
|
||||||
### Server -- real-time from mic
|
#### Frontend Implementation
|
||||||
|
|
||||||
`whisper_online_server.py` has the same model options as `whisper_online.py`, plus `--host` and `--port` of the TCP connection and the `--warmup-file`. See the help message (`-h` option).
|
The package includes an HTML/JavaScript implementation [here](https://github.com/QuentinFuxa/WhisperLiveKit/blob/main/whisperlivekit/web/live_transcription.html). You can also import it using `from whisperlivekit import get_web_interface_html` & `page = get_web_interface_html()`
|
||||||
|
|
||||||
Client example:
|
|
||||||
|
|
||||||
```
|
### ⚙️ Parameters & Configuration
|
||||||
arecord -f S16_LE -c1 -r 16000 -t raw -D default | nc localhost 43001
|
|
||||||
|
| Parameter | Description | Default |
|
||||||
|
|-----------|-------------|---------|
|
||||||
|
| `--model` | Whisper model size. | `small` |
|
||||||
|
| `--language` | Source language code or `auto` | `en` |
|
||||||
|
| `--task` | `transcribe` or `translate` | `transcribe` |
|
||||||
|
| `--backend` | Processing backend | `simulstreaming` |
|
||||||
|
| `--min-chunk-size` | Minimum audio chunk size (seconds) | `1.0` |
|
||||||
|
| `--no-vac` | Disable Voice Activity Controller | `False` |
|
||||||
|
| `--no-vad` | Disable Voice Activity Detection | `False` |
|
||||||
|
| `--warmup-file` | Audio file path for model warmup | `jfk.wav` |
|
||||||
|
| `--host` | Server host address | `localhost` |
|
||||||
|
| `--port` | Server port | `8000` |
|
||||||
|
| `--ssl-certfile` | Path to the SSL certificate file (for HTTPS support) | `None` |
|
||||||
|
| `--ssl-keyfile` | Path to the SSL private key file (for HTTPS support) | `None` |
|
||||||
|
|
||||||
|
|
||||||
|
| WhisperStreaming backend options | Description | Default |
|
||||||
|
|-----------|-------------|---------|
|
||||||
|
| `--confidence-validation` | Use confidence scores for faster validation | `False` |
|
||||||
|
| `--buffer_trimming` | Buffer trimming strategy (`sentence` or `segment`) | `segment` |
|
||||||
|
|
||||||
|
|
||||||
|
| SimulStreaming backend options | Description | Default |
|
||||||
|
|-----------|-------------|---------|
|
||||||
|
| `--frame-threshold` | AlignAtt frame threshold (lower = faster, higher = more accurate) | `25` |
|
||||||
|
| `--beams` | Number of beams for beam search (1 = greedy decoding) | `1` |
|
||||||
|
| `--decoder` | Force decoder type (`beam` or `greedy`) | `auto` |
|
||||||
|
| `--audio-max-len` | Maximum audio buffer length (seconds) | `30.0` |
|
||||||
|
| `--audio-min-len` | Minimum audio length to process (seconds) | `0.0` |
|
||||||
|
| `--cif-ckpt-path` | Path to CIF model for word boundary detection | `None` |
|
||||||
|
| `--never-fire` | Never truncate incomplete words | `False` |
|
||||||
|
| `--init-prompt` | Initial prompt for the model | `None` |
|
||||||
|
| `--static-init-prompt` | Static prompt that doesn't scroll | `None` |
|
||||||
|
| `--max-context-tokens` | Maximum context tokens | `None` |
|
||||||
|
| `--model-path` | Direct path to .pt model file. Download it if not found | `./base.pt` |
|
||||||
|
| `--preloaded-model-count` | Optional. Number of models to preload in memory to speed up loading (set up to the expected number of concurrent users) | `1` |
|
||||||
|
|
||||||
|
| Diarization options | Description | Default |
|
||||||
|
|-----------|-------------|---------|
|
||||||
|
| `--diarization` | Enable speaker identification | `False` |
|
||||||
|
| `--punctuation-split` | Use punctuation to improve speaker boundaries | `True` |
|
||||||
|
| `--segmentation-model` | Hugging Face model ID for pyannote.audio segmentation model. [Available models](https://github.com/juanmc2005/diart/tree/main?tab=readme-ov-file#pre-trained-models) | `pyannote/segmentation-3.0` |
|
||||||
|
| `--embedding-model` | Hugging Face model ID for pyannote.audio embedding model. [Available models](https://github.com/juanmc2005/diart/tree/main?tab=readme-ov-file#pre-trained-models) | `speechbrain/spkrec-ecapa-voxceleb` |
|
||||||
|
|
||||||
|
### 🚀 Deployment Guide
|
||||||
|
|
||||||
|
To deploy WhisperLiveKit in production:
|
||||||
|
|
||||||
|
1. **Server Setup**: Install production ASGI server & launch with multiple workers
|
||||||
|
```bash
|
||||||
|
pip install uvicorn gunicorn
|
||||||
|
gunicorn -k uvicorn.workers.UvicornWorker -w 4 your_app:app
|
||||||
|
```
|
||||||
|
|
||||||
|
2. **Frontend**: Host your customized version of the `html` example & ensure WebSocket connection points correctly
|
||||||
|
|
||||||
|
3. **Nginx Configuration** (recommended for production):
|
||||||
|
```nginx
|
||||||
|
server {
|
||||||
|
listen 80;
|
||||||
|
server_name your-domain.com;
|
||||||
|
location / {
|
||||||
|
proxy_pass http://localhost:8000;
|
||||||
|
proxy_set_header Upgrade $http_upgrade;
|
||||||
|
proxy_set_header Connection "upgrade";
|
||||||
|
proxy_set_header Host $host;
|
||||||
|
}}
|
||||||
|
```
|
||||||
|
|
||||||
|
4. **HTTPS Support**: For secure deployments, use "wss://" instead of "ws://" in WebSocket URL
|
||||||
|
|
||||||
|
### 🐋 Docker
|
||||||
|
|
||||||
|
A Dockerfile is provided which allows re-use of Python package installation options. Create a reusable image with only the basics and then run as a named container:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker build -t whisperlivekit-defaults .
|
||||||
|
docker create --gpus all --name whisperlivekit -p 8000:8000 whisperlivekit-defaults --model base
|
||||||
|
docker start -i whisperlivekit
|
||||||
```
|
```
|
||||||
|
|
||||||
- arecord sends realtime audio from a sound device (e.g. mic), in raw audio format -- 16000 sampling rate, mono channel, S16\_LE -- signed 16-bit integer low endian. (use the alternative to arecord that works for you)
|
> **Note**: For **large** models, ensure that your **docker runtime** has enough **memory** available
|
||||||
|
|
||||||
- nc is netcat with server's host and port
|
> **Note**: If you're running on a system without NVIDIA GPU support (such as Mac with Apple Silicon or any system without CUDA capabilities), you need to **remove the `--gpus all` flag** from the `docker create` command. Without GPU acceleration, transcription will use CPU only, which may be significantly slower. Consider using small models for better performance on CPU-only systems.
|
||||||
|
|
||||||
|
|
||||||
## Background
|
|
||||||
|
|
||||||
Default Whisper is intended for audio chunks of at most 30 seconds that contain
|
|
||||||
one full sentence. Longer audio files must be split to shorter chunks and
|
|
||||||
merged with "init prompt". In low latency simultaneous streaming mode, the
|
|
||||||
simple and naive chunking fixed-sized windows does not work well, it can split
|
|
||||||
a word in the middle. It is also necessary to know when the transcribt is
|
|
||||||
stable, should be confirmed ("commited") and followed up, and when the future
|
|
||||||
content makes the transcript clearer.
|
|
||||||
|
|
||||||
For that, there is LocalAgreement-n policy: if n consecutive updates, each with
|
|
||||||
a newly available audio stream chunk, agree on a prefix transcript, it is
|
|
||||||
confirmed. (Reference: CUNI-KIT at IWSLT 2022 etc.)
|
|
||||||
|
|
||||||
In this project, we re-use the idea of Peter Polák from this demo:
|
|
||||||
https://github.com/pe-trik/transformers/blob/online_decode/examples/pytorch/online-decoding/whisper-online-demo.py
|
|
||||||
However, it doesn't do any sentence segmentation, but Whisper produces
|
|
||||||
punctuation and the libraries `faster-whisper` and `whisper_transcribed` make
|
|
||||||
word-level timestamps. In short: we
|
|
||||||
consecutively process new audio chunks, emit the transcripts that are confirmed
|
|
||||||
by 2 iterations, and scroll the audio processing buffer on a timestamp of a
|
|
||||||
confirmed complete sentence. The processing audio buffer is not too long and
|
|
||||||
the processing is fast.
|
|
||||||
|
|
||||||
In more detail: we use the init prompt, we handle the inaccurate timestamps, we
|
|
||||||
re-process confirmed sentence prefixes and skip them, making sure they don't
|
|
||||||
overlap, and we limit the processing buffer window.
|
|
||||||
|
|
||||||
### Performance evaluation
|
|
||||||
|
|
||||||
[See the paper.](http://www.afnlp.org/conferences/ijcnlp2023/proceedings/main-demo/cdrom/pdf/2023.ijcnlp-demo.3.pdf)
|
|
||||||
|
|
||||||
### Contributions
|
|
||||||
|
|
||||||
Contributions are welcome. We acknowledge especially:
|
|
||||||
|
|
||||||
- [The GitHub contributors](https://github.com/ufal/whisper_streaming/graphs/contributors) for their pull requests with new features and bugfixes.
|
|
||||||
- [The translation of this repo into Chinese.](https://github.com/Gloridust/whisper_streaming_CN)
|
|
||||||
- [Ondřej Plátek](https://opla.cz/) for the paper pre-review.
|
|
||||||
- [Peter Polák](https://ufal.mff.cuni.cz/peter-polak) for the original idea.
|
|
||||||
- The UEDIN team of the [ELITR project](https://elitr.eu) for the original line_packet.py.
|
|
||||||
|
|
||||||
|
|
||||||
## Contact
|
|
||||||
|
|
||||||
Dominik Macháček, machacek@ufal.mff.cuni.cz
|
|
||||||
|
|
||||||
|
#### Customization
|
||||||
|
|
||||||
|
- `--build-arg` Options:
|
||||||
|
- `EXTRAS="whisper-timestamped"` - Add extras to the image's installation (no spaces). Remember to set necessary container options!
|
||||||
|
- `HF_PRECACHE_DIR="./.cache/"` - Pre-load a model cache for faster first-time start
|
||||||
|
- `HF_TKN_FILE="./token"` - Add your Hugging Face Hub access token to download gated models
|
||||||
|
|
||||||
|
## 🔮 Use Cases
|
||||||
|
Capture discussions in real-time for meeting transcription, help hearing-impaired users follow conversations through accessibility tools, transcribe podcasts or videos automatically for content creation, transcribe support calls with speaker identification for customer service...
|
||||||
|
|||||||
BIN
architecture.png
Normal file
BIN
architecture.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 388 KiB |
@@ -1,93 +0,0 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
|
|
||||||
"""Functions for sending and receiving individual lines of text over a socket.
|
|
||||||
|
|
||||||
A line is transmitted using one or more fixed-size packets of UTF-8 bytes
|
|
||||||
containing:
|
|
||||||
|
|
||||||
- Zero or more bytes of UTF-8, excluding \n and \0, followed by
|
|
||||||
|
|
||||||
- Zero or more \0 bytes as required to pad the packet to PACKET_SIZE
|
|
||||||
|
|
||||||
Originally from the UEDIN team of the ELITR project.
|
|
||||||
"""
|
|
||||||
|
|
||||||
PACKET_SIZE = 65536
|
|
||||||
|
|
||||||
|
|
||||||
def send_one_line(socket, text):
|
|
||||||
"""Sends a line of text over the given socket.
|
|
||||||
|
|
||||||
The 'text' argument should contain a single line of text (line break
|
|
||||||
characters are optional). Line boundaries are determined by Python's
|
|
||||||
str.splitlines() function [1]. We also count '\0' as a line terminator.
|
|
||||||
If 'text' contains multiple lines then only the first will be sent.
|
|
||||||
|
|
||||||
If the send fails then an exception will be raised.
|
|
||||||
|
|
||||||
[1] https://docs.python.org/3.5/library/stdtypes.html#str.splitlines
|
|
||||||
|
|
||||||
Args:
|
|
||||||
socket: a socket object.
|
|
||||||
text: string containing a line of text for transmission.
|
|
||||||
"""
|
|
||||||
text.replace('\0', '\n')
|
|
||||||
lines = text.splitlines()
|
|
||||||
first_line = '' if len(lines) == 0 else lines[0]
|
|
||||||
# TODO Is there a better way of handling bad input than 'replace'?
|
|
||||||
data = first_line.encode('utf-8', errors='replace') + b'\n\0'
|
|
||||||
for offset in range(0, len(data), PACKET_SIZE):
|
|
||||||
bytes_remaining = len(data) - offset
|
|
||||||
if bytes_remaining < PACKET_SIZE:
|
|
||||||
padding_length = PACKET_SIZE - bytes_remaining
|
|
||||||
packet = data[offset:] + b'\0' * padding_length
|
|
||||||
else:
|
|
||||||
packet = data[offset:offset+PACKET_SIZE]
|
|
||||||
socket.sendall(packet)
|
|
||||||
|
|
||||||
|
|
||||||
def receive_one_line(socket):
|
|
||||||
"""Receives a line of text from the given socket.
|
|
||||||
|
|
||||||
This function will (attempt to) receive a single line of text. If data is
|
|
||||||
currently unavailable then it will block until data becomes available or
|
|
||||||
the sender has closed the connection (in which case it will return an
|
|
||||||
empty string).
|
|
||||||
|
|
||||||
The string should not contain any newline characters, but if it does then
|
|
||||||
only the first line will be returned.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
socket: a socket object.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
A string representing a single line with a terminating newline or
|
|
||||||
None if the connection has been closed.
|
|
||||||
"""
|
|
||||||
data = b''
|
|
||||||
while True:
|
|
||||||
packet = socket.recv(PACKET_SIZE)
|
|
||||||
if not packet: # Connection has been closed.
|
|
||||||
return None
|
|
||||||
data += packet
|
|
||||||
if b'\0' in packet:
|
|
||||||
break
|
|
||||||
# TODO Is there a better way of handling bad input than 'replace'?
|
|
||||||
text = data.decode('utf-8', errors='replace').strip('\0')
|
|
||||||
lines = text.split('\n')
|
|
||||||
return lines[0] + '\n'
|
|
||||||
|
|
||||||
|
|
||||||
def receive_lines(socket):
|
|
||||||
try:
|
|
||||||
data = socket.recv(PACKET_SIZE)
|
|
||||||
except BlockingIOError:
|
|
||||||
return []
|
|
||||||
if data is None: # Connection has been closed.
|
|
||||||
return None
|
|
||||||
# TODO Is there a better way of handling bad input than 'replace'?
|
|
||||||
text = data.decode('utf-8', errors='replace').strip('\0')
|
|
||||||
lines = text.split('\n')
|
|
||||||
if len(lines)==1 and not lines[0]:
|
|
||||||
return None
|
|
||||||
return lines
|
|
||||||
56
pyproject.toml
Normal file
56
pyproject.toml
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
[build-system]
|
||||||
|
requires = ["setuptools>=61.0"]
|
||||||
|
build-backend = "setuptools.build_meta"
|
||||||
|
|
||||||
|
[project]
|
||||||
|
name = "whisperlivekit"
|
||||||
|
version = "0.2.6"
|
||||||
|
description = "Real-time, Fully Local Whisper's Speech-to-Text and Speaker Diarization"
|
||||||
|
readme = "README.md"
|
||||||
|
authors = [
|
||||||
|
{ name = "Quentin Fuxa" }
|
||||||
|
]
|
||||||
|
license = { file = "LICENSE" }
|
||||||
|
requires-python = ">=3.9"
|
||||||
|
classifiers = [
|
||||||
|
"Development Status :: 4 - Beta",
|
||||||
|
"Intended Audience :: Developers",
|
||||||
|
"License :: OSI Approved :: MIT License",
|
||||||
|
"Programming Language :: Python :: 3.9",
|
||||||
|
"Programming Language :: Python :: 3.10",
|
||||||
|
"Topic :: Scientific/Engineering :: Artificial Intelligence",
|
||||||
|
"Topic :: Multimedia :: Sound/Audio :: Speech"
|
||||||
|
]
|
||||||
|
dependencies = [
|
||||||
|
"fastapi",
|
||||||
|
"librosa",
|
||||||
|
"soundfile",
|
||||||
|
"faster-whisper",
|
||||||
|
"uvicorn",
|
||||||
|
"websockets",
|
||||||
|
"torch",
|
||||||
|
"tqdm",
|
||||||
|
"tiktoken",
|
||||||
|
'triton>=2.0.0,<3; platform_machine == "x86_64" and (sys_platform == "linux" or sys_platform == "linux2")'
|
||||||
|
]
|
||||||
|
|
||||||
|
[project.optional-dependencies]
|
||||||
|
diarization = ["diart"]
|
||||||
|
sentence = ["mosestokenizer", "wtpsplit"]
|
||||||
|
whisper = ["whisper"]
|
||||||
|
whisper-timestamped = ["whisper-timestamped"]
|
||||||
|
mlx-whisper = ["mlx-whisper"]
|
||||||
|
openai = ["openai"]
|
||||||
|
|
||||||
|
[project.urls]
|
||||||
|
Homepage = "https://github.com/QuentinFuxa/WhisperLiveKit"
|
||||||
|
|
||||||
|
[project.scripts]
|
||||||
|
whisperlivekit-server = "whisperlivekit.basic_server:main"
|
||||||
|
|
||||||
|
[tool.setuptools]
|
||||||
|
packages = ["whisperlivekit", "whisperlivekit.diarization", "whisperlivekit.simul_whisper", "whisperlivekit.simul_whisper.whisper", "whisperlivekit.simul_whisper.whisper.assets", "whisperlivekit.simul_whisper.whisper.normalizers", "whisperlivekit.web", "whisperlivekit.whisper_streaming_custom"]
|
||||||
|
|
||||||
|
[tool.setuptools.package-data]
|
||||||
|
whisperlivekit = ["web/*.html", "web/*.css", "web/*.js", "web/src/*.svg"]
|
||||||
|
"whisperlivekit.simul_whisper.whisper.assets" = ["*.tiktoken", "*.npz"]
|
||||||
@@ -1,738 +0,0 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
import sys
|
|
||||||
import numpy as np
|
|
||||||
import librosa
|
|
||||||
from functools import lru_cache
|
|
||||||
import time
|
|
||||||
import logging
|
|
||||||
|
|
||||||
|
|
||||||
import io
|
|
||||||
import soundfile as sf
|
|
||||||
import math
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
@lru_cache
|
|
||||||
def load_audio(fname):
|
|
||||||
a, _ = librosa.load(fname, sr=16000, dtype=np.float32)
|
|
||||||
return a
|
|
||||||
|
|
||||||
def load_audio_chunk(fname, beg, end):
|
|
||||||
audio = load_audio(fname)
|
|
||||||
beg_s = int(beg*16000)
|
|
||||||
end_s = int(end*16000)
|
|
||||||
return audio[beg_s:end_s]
|
|
||||||
|
|
||||||
|
|
||||||
# Whisper backend
|
|
||||||
|
|
||||||
class ASRBase:
|
|
||||||
|
|
||||||
sep = " " # join transcribe words with this character (" " for whisper_timestamped,
|
|
||||||
# "" for faster-whisper because it emits the spaces when neeeded)
|
|
||||||
|
|
||||||
def __init__(self, lan, modelsize=None, cache_dir=None, model_dir=None, logfile=sys.stderr):
|
|
||||||
self.logfile = logfile
|
|
||||||
|
|
||||||
self.transcribe_kargs = {}
|
|
||||||
if lan == "auto":
|
|
||||||
self.original_language = None
|
|
||||||
else:
|
|
||||||
self.original_language = lan
|
|
||||||
|
|
||||||
self.model = self.load_model(modelsize, cache_dir, model_dir)
|
|
||||||
|
|
||||||
|
|
||||||
def load_model(self, modelsize, cache_dir):
|
|
||||||
raise NotImplemented("must be implemented in the child class")
|
|
||||||
|
|
||||||
def transcribe(self, audio, init_prompt=""):
|
|
||||||
raise NotImplemented("must be implemented in the child class")
|
|
||||||
|
|
||||||
def use_vad(self):
|
|
||||||
raise NotImplemented("must be implemented in the child class")
|
|
||||||
|
|
||||||
|
|
||||||
class WhisperTimestampedASR(ASRBase):
|
|
||||||
"""Uses whisper_timestamped library as the backend. Initially, we tested the code on this backend. It worked, but slower than faster-whisper.
|
|
||||||
On the other hand, the installation for GPU could be easier.
|
|
||||||
"""
|
|
||||||
|
|
||||||
sep = " "
|
|
||||||
|
|
||||||
def load_model(self, modelsize=None, cache_dir=None, model_dir=None):
|
|
||||||
import whisper
|
|
||||||
import whisper_timestamped
|
|
||||||
from whisper_timestamped import transcribe_timestamped
|
|
||||||
self.transcribe_timestamped = transcribe_timestamped
|
|
||||||
if model_dir is not None:
|
|
||||||
logger.debug("ignoring model_dir, not implemented")
|
|
||||||
return whisper.load_model(modelsize, download_root=cache_dir)
|
|
||||||
|
|
||||||
def transcribe(self, audio, init_prompt=""):
|
|
||||||
result = self.transcribe_timestamped(self.model,
|
|
||||||
audio, language=self.original_language,
|
|
||||||
initial_prompt=init_prompt, verbose=None,
|
|
||||||
condition_on_previous_text=True, **self.transcribe_kargs)
|
|
||||||
return result
|
|
||||||
|
|
||||||
def ts_words(self,r):
|
|
||||||
# return: transcribe result object to [(beg,end,"word1"), ...]
|
|
||||||
o = []
|
|
||||||
for s in r["segments"]:
|
|
||||||
for w in s["words"]:
|
|
||||||
t = (w["start"],w["end"],w["text"])
|
|
||||||
o.append(t)
|
|
||||||
return o
|
|
||||||
|
|
||||||
def segments_end_ts(self, res):
|
|
||||||
return [s["end"] for s in res["segments"]]
|
|
||||||
|
|
||||||
def use_vad(self):
|
|
||||||
self.transcribe_kargs["vad"] = True
|
|
||||||
|
|
||||||
def set_translate_task(self):
|
|
||||||
self.transcribe_kargs["task"] = "translate"
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class FasterWhisperASR(ASRBase):
|
|
||||||
"""Uses faster-whisper library as the backend. Works much faster, appx 4-times (in offline mode). For GPU, it requires installation with a specific CUDNN version.
|
|
||||||
"""
|
|
||||||
|
|
||||||
sep = ""
|
|
||||||
|
|
||||||
def load_model(self, modelsize=None, cache_dir=None, model_dir=None):
|
|
||||||
from faster_whisper import WhisperModel
|
|
||||||
# logging.getLogger("faster_whisper").setLevel(logger.level)
|
|
||||||
if model_dir is not None:
|
|
||||||
logger.debug(f"Loading whisper model from model_dir {model_dir}. modelsize and cache_dir parameters are not used.")
|
|
||||||
model_size_or_path = model_dir
|
|
||||||
elif modelsize is not None:
|
|
||||||
model_size_or_path = modelsize
|
|
||||||
else:
|
|
||||||
raise ValueError("modelsize or model_dir parameter must be set")
|
|
||||||
|
|
||||||
|
|
||||||
# this worked fast and reliably on NVIDIA L40
|
|
||||||
model = WhisperModel(model_size_or_path, device="cuda", compute_type="float16", download_root=cache_dir)
|
|
||||||
|
|
||||||
# or run on GPU with INT8
|
|
||||||
# tested: the transcripts were different, probably worse than with FP16, and it was slightly (appx 20%) slower
|
|
||||||
#model = WhisperModel(model_size, device="cuda", compute_type="int8_float16")
|
|
||||||
|
|
||||||
# or run on CPU with INT8
|
|
||||||
# tested: works, but slow, appx 10-times than cuda FP16
|
|
||||||
# model = WhisperModel(modelsize, device="cpu", compute_type="int8") #, download_root="faster-disk-cache-dir/")
|
|
||||||
return model
|
|
||||||
|
|
||||||
def transcribe(self, audio, init_prompt=""):
|
|
||||||
|
|
||||||
# tested: beam_size=5 is faster and better than 1 (on one 200 second document from En ESIC, min chunk 0.01)
|
|
||||||
segments, info = self.model.transcribe(audio, language=self.original_language, initial_prompt=init_prompt, beam_size=5, word_timestamps=True, condition_on_previous_text=True, **self.transcribe_kargs)
|
|
||||||
#print(info) # info contains language detection result
|
|
||||||
|
|
||||||
return list(segments)
|
|
||||||
|
|
||||||
def ts_words(self, segments):
|
|
||||||
o = []
|
|
||||||
for segment in segments:
|
|
||||||
for word in segment.words:
|
|
||||||
# not stripping the spaces -- should not be merged with them!
|
|
||||||
w = word.word
|
|
||||||
t = (word.start, word.end, w)
|
|
||||||
o.append(t)
|
|
||||||
return o
|
|
||||||
|
|
||||||
def segments_end_ts(self, res):
|
|
||||||
return [s.end for s in res]
|
|
||||||
|
|
||||||
def use_vad(self):
|
|
||||||
self.transcribe_kargs["vad_filter"] = True
|
|
||||||
|
|
||||||
def set_translate_task(self):
|
|
||||||
self.transcribe_kargs["task"] = "translate"
|
|
||||||
|
|
||||||
|
|
||||||
class OpenaiApiASR(ASRBase):
|
|
||||||
"""Uses OpenAI's Whisper API for audio transcription."""
|
|
||||||
|
|
||||||
def __init__(self, lan=None, temperature=0, logfile=sys.stderr):
|
|
||||||
self.logfile = logfile
|
|
||||||
|
|
||||||
self.modelname = "whisper-1"
|
|
||||||
self.original_language = None if lan == "auto" else lan # ISO-639-1 language code
|
|
||||||
self.response_format = "verbose_json"
|
|
||||||
self.temperature = temperature
|
|
||||||
|
|
||||||
self.load_model()
|
|
||||||
|
|
||||||
self.use_vad_opt = False
|
|
||||||
|
|
||||||
# reset the task in set_translate_task
|
|
||||||
self.task = "transcribe"
|
|
||||||
|
|
||||||
def load_model(self, *args, **kwargs):
|
|
||||||
from openai import OpenAI
|
|
||||||
self.client = OpenAI()
|
|
||||||
|
|
||||||
self.transcribed_seconds = 0 # for logging how many seconds were processed by API, to know the cost
|
|
||||||
|
|
||||||
|
|
||||||
def ts_words(self, segments):
|
|
||||||
no_speech_segments = []
|
|
||||||
if self.use_vad_opt:
|
|
||||||
for segment in segments.segments:
|
|
||||||
# TODO: threshold can be set from outside
|
|
||||||
if segment["no_speech_prob"] > 0.8:
|
|
||||||
no_speech_segments.append((segment.get("start"), segment.get("end")))
|
|
||||||
|
|
||||||
o = []
|
|
||||||
for word in segments.words:
|
|
||||||
start = word.get("start")
|
|
||||||
end = word.get("end")
|
|
||||||
if any(s[0] <= start <= s[1] for s in no_speech_segments):
|
|
||||||
# print("Skipping word", word.get("word"), "because it's in a no-speech segment")
|
|
||||||
continue
|
|
||||||
o.append((start, end, word.get("word")))
|
|
||||||
return o
|
|
||||||
|
|
||||||
|
|
||||||
def segments_end_ts(self, res):
|
|
||||||
return [s["end"] for s in res.words]
|
|
||||||
|
|
||||||
def transcribe(self, audio_data, prompt=None, *args, **kwargs):
|
|
||||||
# Write the audio data to a buffer
|
|
||||||
buffer = io.BytesIO()
|
|
||||||
buffer.name = "temp.wav"
|
|
||||||
sf.write(buffer, audio_data, samplerate=16000, format='WAV', subtype='PCM_16')
|
|
||||||
buffer.seek(0) # Reset buffer's position to the beginning
|
|
||||||
|
|
||||||
self.transcribed_seconds += math.ceil(len(audio_data)/16000) # it rounds up to the whole seconds
|
|
||||||
|
|
||||||
params = {
|
|
||||||
"model": self.modelname,
|
|
||||||
"file": buffer,
|
|
||||||
"response_format": self.response_format,
|
|
||||||
"temperature": self.temperature,
|
|
||||||
"timestamp_granularities": ["word", "segment"]
|
|
||||||
}
|
|
||||||
if self.task != "translate" and self.original_language:
|
|
||||||
params["language"] = self.original_language
|
|
||||||
if prompt:
|
|
||||||
params["prompt"] = prompt
|
|
||||||
|
|
||||||
if self.task == "translate":
|
|
||||||
proc = self.client.audio.translations
|
|
||||||
else:
|
|
||||||
proc = self.client.audio.transcriptions
|
|
||||||
|
|
||||||
# Process transcription/translation
|
|
||||||
transcript = proc.create(**params)
|
|
||||||
logger.debug(f"OpenAI API processed accumulated {self.transcribed_seconds} seconds")
|
|
||||||
|
|
||||||
return transcript
|
|
||||||
|
|
||||||
def use_vad(self):
|
|
||||||
self.use_vad_opt = True
|
|
||||||
|
|
||||||
def set_translate_task(self):
|
|
||||||
self.task = "translate"
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class HypothesisBuffer:
|
|
||||||
|
|
||||||
def __init__(self, logfile=sys.stderr):
|
|
||||||
self.commited_in_buffer = []
|
|
||||||
self.buffer = []
|
|
||||||
self.new = []
|
|
||||||
|
|
||||||
self.last_commited_time = 0
|
|
||||||
self.last_commited_word = None
|
|
||||||
|
|
||||||
self.logfile = logfile
|
|
||||||
|
|
||||||
def insert(self, new, offset):
|
|
||||||
# compare self.commited_in_buffer and new. It inserts only the words in new that extend the commited_in_buffer, it means they are roughly behind last_commited_time and new in content
|
|
||||||
# the new tail is added to self.new
|
|
||||||
|
|
||||||
new = [(a+offset,b+offset,t) for a,b,t in new]
|
|
||||||
self.new = [(a,b,t) for a,b,t in new if a > self.last_commited_time-0.1]
|
|
||||||
|
|
||||||
if len(self.new) >= 1:
|
|
||||||
a,b,t = self.new[0]
|
|
||||||
if abs(a - self.last_commited_time) < 1:
|
|
||||||
if self.commited_in_buffer:
|
|
||||||
# it's going to search for 1, 2, ..., 5 consecutive words (n-grams) that are identical in commited and new. If they are, they're dropped.
|
|
||||||
cn = len(self.commited_in_buffer)
|
|
||||||
nn = len(self.new)
|
|
||||||
for i in range(1,min(min(cn,nn),5)+1): # 5 is the maximum
|
|
||||||
c = " ".join([self.commited_in_buffer[-j][2] for j in range(1,i+1)][::-1])
|
|
||||||
tail = " ".join(self.new[j-1][2] for j in range(1,i+1))
|
|
||||||
if c == tail:
|
|
||||||
words = []
|
|
||||||
for j in range(i):
|
|
||||||
words.append(repr(self.new.pop(0)))
|
|
||||||
words_msg = " ".join(words)
|
|
||||||
logger.debug(f"removing last {i} words: {words_msg}")
|
|
||||||
break
|
|
||||||
|
|
||||||
def flush(self):
|
|
||||||
# returns commited chunk = the longest common prefix of 2 last inserts.
|
|
||||||
|
|
||||||
commit = []
|
|
||||||
while self.new:
|
|
||||||
na, nb, nt = self.new[0]
|
|
||||||
|
|
||||||
if len(self.buffer) == 0:
|
|
||||||
break
|
|
||||||
|
|
||||||
if nt == self.buffer[0][2]:
|
|
||||||
commit.append((na,nb,nt))
|
|
||||||
self.last_commited_word = nt
|
|
||||||
self.last_commited_time = nb
|
|
||||||
self.buffer.pop(0)
|
|
||||||
self.new.pop(0)
|
|
||||||
else:
|
|
||||||
break
|
|
||||||
self.buffer = self.new
|
|
||||||
self.new = []
|
|
||||||
self.commited_in_buffer.extend(commit)
|
|
||||||
return commit
|
|
||||||
|
|
||||||
def pop_commited(self, time):
|
|
||||||
while self.commited_in_buffer and self.commited_in_buffer[0][1] <= time:
|
|
||||||
self.commited_in_buffer.pop(0)
|
|
||||||
|
|
||||||
def complete(self):
|
|
||||||
return self.buffer
|
|
||||||
|
|
||||||
class OnlineASRProcessor:
|
|
||||||
|
|
||||||
SAMPLING_RATE = 16000
|
|
||||||
|
|
||||||
def __init__(self, asr, tokenizer=None, buffer_trimming=("segment", 15), logfile=sys.stderr):
|
|
||||||
"""asr: WhisperASR object
|
|
||||||
tokenizer: sentence tokenizer object for the target language. Must have a method *split* that behaves like the one of MosesTokenizer. It can be None, if "segment" buffer trimming option is used, then tokenizer is not used at all.
|
|
||||||
("segment", 15)
|
|
||||||
buffer_trimming: a pair of (option, seconds), where option is either "sentence" or "segment", and seconds is a number. Buffer is trimmed if it is longer than "seconds" threshold. Default is the most recommended option.
|
|
||||||
logfile: where to store the log.
|
|
||||||
"""
|
|
||||||
self.asr = asr
|
|
||||||
self.tokenizer = tokenizer
|
|
||||||
self.logfile = logfile
|
|
||||||
|
|
||||||
self.init()
|
|
||||||
|
|
||||||
self.buffer_trimming_way, self.buffer_trimming_sec = buffer_trimming
|
|
||||||
|
|
||||||
def init(self):
|
|
||||||
"""run this when starting or restarting processing"""
|
|
||||||
self.audio_buffer = np.array([],dtype=np.float32)
|
|
||||||
self.buffer_time_offset = 0
|
|
||||||
|
|
||||||
self.transcript_buffer = HypothesisBuffer(logfile=self.logfile)
|
|
||||||
self.commited = []
|
|
||||||
|
|
||||||
def insert_audio_chunk(self, audio):
|
|
||||||
self.audio_buffer = np.append(self.audio_buffer, audio)
|
|
||||||
|
|
||||||
def prompt(self):
|
|
||||||
"""Returns a tuple: (prompt, context), where "prompt" is a 200-character suffix of commited text that is inside of the scrolled away part of audio buffer.
|
|
||||||
"context" is the commited text that is inside the audio buffer. It is transcribed again and skipped. It is returned only for debugging and logging reasons.
|
|
||||||
"""
|
|
||||||
k = max(0,len(self.commited)-1)
|
|
||||||
while k > 0 and self.commited[k-1][1] > self.buffer_time_offset:
|
|
||||||
k -= 1
|
|
||||||
|
|
||||||
p = self.commited[:k]
|
|
||||||
p = [t for _,_,t in p]
|
|
||||||
prompt = []
|
|
||||||
l = 0
|
|
||||||
while p and l < 200: # 200 characters prompt size
|
|
||||||
x = p.pop(-1)
|
|
||||||
l += len(x)+1
|
|
||||||
prompt.append(x)
|
|
||||||
non_prompt = self.commited[k:]
|
|
||||||
return self.asr.sep.join(prompt[::-1]), self.asr.sep.join(t for _,_,t in non_prompt)
|
|
||||||
|
|
||||||
def process_iter(self):
|
|
||||||
"""Runs on the current audio buffer.
|
|
||||||
Returns: a tuple (beg_timestamp, end_timestamp, "text"), or (None, None, "").
|
|
||||||
The non-emty text is confirmed (committed) partial transcript.
|
|
||||||
"""
|
|
||||||
|
|
||||||
prompt, non_prompt = self.prompt()
|
|
||||||
logger.debug(f"PROMPT: {prompt}")
|
|
||||||
logger.debug(f"CONTEXT: {non_prompt}")
|
|
||||||
logger.debug(f"transcribing {len(self.audio_buffer)/self.SAMPLING_RATE:2.2f} seconds from {self.buffer_time_offset:2.2f}")
|
|
||||||
res = self.asr.transcribe(self.audio_buffer, init_prompt=prompt)
|
|
||||||
|
|
||||||
# transform to [(beg,end,"word1"), ...]
|
|
||||||
tsw = self.asr.ts_words(res)
|
|
||||||
|
|
||||||
self.transcript_buffer.insert(tsw, self.buffer_time_offset)
|
|
||||||
o = self.transcript_buffer.flush()
|
|
||||||
self.commited.extend(o)
|
|
||||||
completed = self.to_flush(o)
|
|
||||||
logger.debug(f">>>>COMPLETE NOW: {completed}")
|
|
||||||
the_rest = self.to_flush(self.transcript_buffer.complete())
|
|
||||||
logger.debug(f"INCOMPLETE: {the_rest}")
|
|
||||||
|
|
||||||
# there is a newly confirmed text
|
|
||||||
|
|
||||||
if o and self.buffer_trimming_way == "sentence": # trim the completed sentences
|
|
||||||
if len(self.audio_buffer)/self.SAMPLING_RATE > self.buffer_trimming_sec: # longer than this
|
|
||||||
self.chunk_completed_sentence()
|
|
||||||
|
|
||||||
|
|
||||||
if self.buffer_trimming_way == "segment":
|
|
||||||
s = self.buffer_trimming_sec # trim the completed segments longer than s,
|
|
||||||
else:
|
|
||||||
s = 30 # if the audio buffer is longer than 30s, trim it
|
|
||||||
|
|
||||||
if len(self.audio_buffer)/self.SAMPLING_RATE > s:
|
|
||||||
self.chunk_completed_segment(res)
|
|
||||||
|
|
||||||
# alternative: on any word
|
|
||||||
#l = self.buffer_time_offset + len(self.audio_buffer)/self.SAMPLING_RATE - 10
|
|
||||||
# let's find commited word that is less
|
|
||||||
#k = len(self.commited)-1
|
|
||||||
#while k>0 and self.commited[k][1] > l:
|
|
||||||
# k -= 1
|
|
||||||
#t = self.commited[k][1]
|
|
||||||
logger.debug("chunking segment")
|
|
||||||
#self.chunk_at(t)
|
|
||||||
|
|
||||||
logger.debug(f"len of buffer now: {len(self.audio_buffer)/self.SAMPLING_RATE:2.2f}")
|
|
||||||
return self.to_flush(o)
|
|
||||||
|
|
||||||
def chunk_completed_sentence(self):
|
|
||||||
if self.commited == []: return
|
|
||||||
logger.debug(self.commited)
|
|
||||||
sents = self.words_to_sentences(self.commited)
|
|
||||||
for s in sents:
|
|
||||||
logger.debug(f"\t\tSENT: {s}")
|
|
||||||
if len(sents) < 2:
|
|
||||||
return
|
|
||||||
while len(sents) > 2:
|
|
||||||
sents.pop(0)
|
|
||||||
# we will continue with audio processing at this timestamp
|
|
||||||
chunk_at = sents[-2][1]
|
|
||||||
|
|
||||||
logger.debug(f"--- sentence chunked at {chunk_at:2.2f}")
|
|
||||||
self.chunk_at(chunk_at)
|
|
||||||
|
|
||||||
def chunk_completed_segment(self, res):
|
|
||||||
if self.commited == []: return
|
|
||||||
|
|
||||||
ends = self.asr.segments_end_ts(res)
|
|
||||||
|
|
||||||
t = self.commited[-1][1]
|
|
||||||
|
|
||||||
if len(ends) > 1:
|
|
||||||
|
|
||||||
e = ends[-2]+self.buffer_time_offset
|
|
||||||
while len(ends) > 2 and e > t:
|
|
||||||
ends.pop(-1)
|
|
||||||
e = ends[-2]+self.buffer_time_offset
|
|
||||||
if e <= t:
|
|
||||||
logger.debug(f"--- segment chunked at {e:2.2f}")
|
|
||||||
self.chunk_at(e)
|
|
||||||
else:
|
|
||||||
logger.debug(f"--- last segment not within commited area")
|
|
||||||
else:
|
|
||||||
logger.debug(f"--- not enough segments to chunk")
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def chunk_at(self, time):
|
|
||||||
"""trims the hypothesis and audio buffer at "time"
|
|
||||||
"""
|
|
||||||
self.transcript_buffer.pop_commited(time)
|
|
||||||
cut_seconds = time - self.buffer_time_offset
|
|
||||||
self.audio_buffer = self.audio_buffer[int(cut_seconds*self.SAMPLING_RATE):]
|
|
||||||
self.buffer_time_offset = time
|
|
||||||
|
|
||||||
def words_to_sentences(self, words):
|
|
||||||
"""Uses self.tokenizer for sentence segmentation of words.
|
|
||||||
Returns: [(beg,end,"sentence 1"),...]
|
|
||||||
"""
|
|
||||||
|
|
||||||
cwords = [w for w in words]
|
|
||||||
t = " ".join(o[2] for o in cwords)
|
|
||||||
s = self.tokenizer.split(t)
|
|
||||||
out = []
|
|
||||||
while s:
|
|
||||||
beg = None
|
|
||||||
end = None
|
|
||||||
sent = s.pop(0).strip()
|
|
||||||
fsent = sent
|
|
||||||
while cwords:
|
|
||||||
b,e,w = cwords.pop(0)
|
|
||||||
w = w.strip()
|
|
||||||
if beg is None and sent.startswith(w):
|
|
||||||
beg = b
|
|
||||||
elif end is None and sent == w:
|
|
||||||
end = e
|
|
||||||
out.append((beg,end,fsent))
|
|
||||||
break
|
|
||||||
sent = sent[len(w):].strip()
|
|
||||||
return out
|
|
||||||
|
|
||||||
def finish(self):
|
|
||||||
"""Flush the incomplete text when the whole processing ends.
|
|
||||||
Returns: the same format as self.process_iter()
|
|
||||||
"""
|
|
||||||
o = self.transcript_buffer.complete()
|
|
||||||
f = self.to_flush(o)
|
|
||||||
logger.debug("last, noncommited: {f}")
|
|
||||||
return f
|
|
||||||
|
|
||||||
|
|
||||||
def to_flush(self, sents, sep=None, offset=0, ):
|
|
||||||
# concatenates the timestamped words or sentences into one sequence that is flushed in one line
|
|
||||||
# sents: [(beg1, end1, "sentence1"), ...] or [] if empty
|
|
||||||
# return: (beg1,end-of-last-sentence,"concatenation of sentences") or (None, None, "") if empty
|
|
||||||
if sep is None:
|
|
||||||
sep = self.asr.sep
|
|
||||||
t = sep.join(s[2] for s in sents)
|
|
||||||
if len(sents) == 0:
|
|
||||||
b = None
|
|
||||||
e = None
|
|
||||||
else:
|
|
||||||
b = offset + sents[0][0]
|
|
||||||
e = offset + sents[-1][1]
|
|
||||||
return (b,e,t)
|
|
||||||
|
|
||||||
WHISPER_LANG_CODES = "af,am,ar,as,az,ba,be,bg,bn,bo,br,bs,ca,cs,cy,da,de,el,en,es,et,eu,fa,fi,fo,fr,gl,gu,ha,haw,he,hi,hr,ht,hu,hy,id,is,it,ja,jw,ka,kk,km,kn,ko,la,lb,ln,lo,lt,lv,mg,mi,mk,ml,mn,mr,ms,mt,my,ne,nl,nn,no,oc,pa,pl,ps,pt,ro,ru,sa,sd,si,sk,sl,sn,so,sq,sr,su,sv,sw,ta,te,tg,th,tk,tl,tr,tt,uk,ur,uz,vi,yi,yo,zh".split(",")
|
|
||||||
|
|
||||||
def create_tokenizer(lan):
|
|
||||||
"""returns an object that has split function that works like the one of MosesTokenizer"""
|
|
||||||
|
|
||||||
assert lan in WHISPER_LANG_CODES, "language must be Whisper's supported lang code: " + " ".join(WHISPER_LANG_CODES)
|
|
||||||
|
|
||||||
if lan == "uk":
|
|
||||||
import tokenize_uk
|
|
||||||
class UkrainianTokenizer:
|
|
||||||
def split(self, text):
|
|
||||||
return tokenize_uk.tokenize_sents(text)
|
|
||||||
return UkrainianTokenizer()
|
|
||||||
|
|
||||||
# supported by fast-mosestokenizer
|
|
||||||
if lan in "as bn ca cs de el en es et fi fr ga gu hi hu is it kn lt lv ml mni mr nl or pa pl pt ro ru sk sl sv ta te yue zh".split():
|
|
||||||
from mosestokenizer import MosesTokenizer
|
|
||||||
return MosesTokenizer(lan)
|
|
||||||
|
|
||||||
# the following languages are in Whisper, but not in wtpsplit:
|
|
||||||
if lan in "as ba bo br bs fo haw hr ht jw lb ln lo mi nn oc sa sd sn so su sw tk tl tt".split():
|
|
||||||
logger.debug(f"{lan} code is not supported by wtpsplit. Going to use None lang_code option.")
|
|
||||||
lan = None
|
|
||||||
|
|
||||||
from wtpsplit import WtP
|
|
||||||
# downloads the model from huggingface on the first use
|
|
||||||
wtp = WtP("wtp-canine-s-12l-no-adapters")
|
|
||||||
class WtPtok:
|
|
||||||
def split(self, sent):
|
|
||||||
return wtp.split(sent, lang_code=lan)
|
|
||||||
return WtPtok()
|
|
||||||
|
|
||||||
|
|
||||||
def add_shared_args(parser):
|
|
||||||
"""shared args for simulation (this entry point) and server
|
|
||||||
parser: argparse.ArgumentParser object
|
|
||||||
"""
|
|
||||||
parser.add_argument('--min-chunk-size', type=float, default=1.0, help='Minimum audio chunk size in seconds. It waits up to this time to do processing. If the processing takes shorter time, it waits, otherwise it processes the whole segment that was received by this time.')
|
|
||||||
parser.add_argument('--model', type=str, default='large-v2', choices="tiny.en,tiny,base.en,base,small.en,small,medium.en,medium,large-v1,large-v2,large-v3,large".split(","),help="Name size of the Whisper model to use (default: large-v2). The model is automatically downloaded from the model hub if not present in model cache dir.")
|
|
||||||
parser.add_argument('--model_cache_dir', type=str, default=None, help="Overriding the default model cache dir where models downloaded from the hub are saved")
|
|
||||||
parser.add_argument('--model_dir', type=str, default=None, help="Dir where Whisper model.bin and other files are saved. This option overrides --model and --model_cache_dir parameter.")
|
|
||||||
parser.add_argument('--lan', '--language', type=str, default='auto', help="Source language code, e.g. en,de,cs, or 'auto' for language detection.")
|
|
||||||
parser.add_argument('--task', type=str, default='transcribe', choices=["transcribe","translate"],help="Transcribe or translate.")
|
|
||||||
parser.add_argument('--backend', type=str, default="faster-whisper", choices=["faster-whisper", "whisper_timestamped", "openai-api"],help='Load only this backend for Whisper processing.')
|
|
||||||
parser.add_argument('--vad', action="store_true", default=False, help='Use VAD = voice activity detection, with the default parameters.')
|
|
||||||
parser.add_argument('--buffer_trimming', type=str, default="segment", choices=["sentence", "segment"],help='Buffer trimming strategy -- trim completed sentences marked with punctuation mark and detected by sentence segmenter, or the completed segments returned by Whisper. Sentence segmenter must be installed for "sentence" option.')
|
|
||||||
parser.add_argument('--buffer_trimming_sec', type=float, default=15, help='Buffer trimming length threshold in seconds. If buffer length is longer, trimming sentence/segment is triggered.')
|
|
||||||
parser.add_argument("-l", "--log-level", dest="log_level", choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'], help="Set the log level", default='DEBUG')
|
|
||||||
|
|
||||||
def asr_factory(args, logfile=sys.stderr):
|
|
||||||
"""
|
|
||||||
Creates and configures an ASR and ASR Online instance based on the specified backend and arguments.
|
|
||||||
"""
|
|
||||||
backend = args.backend
|
|
||||||
if backend == "openai-api":
|
|
||||||
logger.debug("Using OpenAI API.")
|
|
||||||
asr = OpenaiApiASR(lan=args.lan)
|
|
||||||
else:
|
|
||||||
if backend == "faster-whisper":
|
|
||||||
asr_cls = FasterWhisperASR
|
|
||||||
else:
|
|
||||||
asr_cls = WhisperTimestampedASR
|
|
||||||
|
|
||||||
# Only for FasterWhisperASR and WhisperTimestampedASR
|
|
||||||
size = args.model
|
|
||||||
t = time.time()
|
|
||||||
logger.info(f"Loading Whisper {size} model for {args.lan}...")
|
|
||||||
asr = asr_cls(modelsize=size, lan=args.lan, cache_dir=args.model_cache_dir, model_dir=args.model_dir)
|
|
||||||
e = time.time()
|
|
||||||
logger.info(f"done. It took {round(e-t,2)} seconds.")
|
|
||||||
|
|
||||||
# Apply common configurations
|
|
||||||
if getattr(args, 'vad', False): # Checks if VAD argument is present and True
|
|
||||||
logger.info("Setting VAD filter")
|
|
||||||
asr.use_vad()
|
|
||||||
|
|
||||||
language = args.lan
|
|
||||||
if args.task == "translate":
|
|
||||||
asr.set_translate_task()
|
|
||||||
tgt_language = "en" # Whisper translates into English
|
|
||||||
else:
|
|
||||||
tgt_language = language # Whisper transcribes in this language
|
|
||||||
|
|
||||||
# Create the tokenizer
|
|
||||||
if args.buffer_trimming == "sentence":
|
|
||||||
tokenizer = create_tokenizer(tgt_language)
|
|
||||||
else:
|
|
||||||
tokenizer = None
|
|
||||||
|
|
||||||
# Create the OnlineASRProcessor
|
|
||||||
online = OnlineASRProcessor(asr,tokenizer,logfile=logfile,buffer_trimming=(args.buffer_trimming, args.buffer_trimming_sec))
|
|
||||||
|
|
||||||
return asr, online
|
|
||||||
|
|
||||||
def set_logging(args,logger,other="_server"):
|
|
||||||
logging.basicConfig(#format='%(name)s
|
|
||||||
format='%(levelname)s\t%(message)s')
|
|
||||||
logger.setLevel(args.log_level)
|
|
||||||
logging.getLogger("whisper_online"+other).setLevel(args.log_level)
|
|
||||||
# logging.getLogger("whisper_online_server").setLevel(args.log_level)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
|
|
||||||
import argparse
|
|
||||||
parser = argparse.ArgumentParser()
|
|
||||||
parser.add_argument('audio_path', type=str, help="Filename of 16kHz mono channel wav, on which live streaming is simulated.")
|
|
||||||
add_shared_args(parser)
|
|
||||||
parser.add_argument('--start_at', type=float, default=0.0, help='Start processing audio at this time.')
|
|
||||||
parser.add_argument('--offline', action="store_true", default=False, help='Offline mode.')
|
|
||||||
parser.add_argument('--comp_unaware', action="store_true", default=False, help='Computationally unaware simulation.')
|
|
||||||
|
|
||||||
args = parser.parse_args()
|
|
||||||
|
|
||||||
# reset to store stderr to different file stream, e.g. open(os.devnull,"w")
|
|
||||||
logfile = sys.stderr
|
|
||||||
|
|
||||||
if args.offline and args.comp_unaware:
|
|
||||||
logger.error("No or one option from --offline and --comp_unaware are available, not both. Exiting.")
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
# if args.log_level:
|
|
||||||
# logging.basicConfig(format='whisper-%(levelname)s:%(name)s: %(message)s',
|
|
||||||
# level=getattr(logging, args.log_level))
|
|
||||||
|
|
||||||
set_logging(args,logger)
|
|
||||||
|
|
||||||
audio_path = args.audio_path
|
|
||||||
|
|
||||||
SAMPLING_RATE = 16000
|
|
||||||
duration = len(load_audio(audio_path))/SAMPLING_RATE
|
|
||||||
logger.info("Audio duration is: %2.2f seconds" % duration)
|
|
||||||
|
|
||||||
asr, online = asr_factory(args, logfile=logfile)
|
|
||||||
min_chunk = args.min_chunk_size
|
|
||||||
|
|
||||||
# load the audio into the LRU cache before we start the timer
|
|
||||||
a = load_audio_chunk(audio_path,0,1)
|
|
||||||
|
|
||||||
# warm up the ASR because the very first transcribe takes much more time than the other
|
|
||||||
asr.transcribe(a)
|
|
||||||
|
|
||||||
beg = args.start_at
|
|
||||||
start = time.time()-beg
|
|
||||||
|
|
||||||
def output_transcript(o, now=None):
|
|
||||||
# output format in stdout is like:
|
|
||||||
# 4186.3606 0 1720 Takhle to je
|
|
||||||
# - the first three words are:
|
|
||||||
# - emission time from beginning of processing, in milliseconds
|
|
||||||
# - beg and end timestamp of the text segment, as estimated by Whisper model. The timestamps are not accurate, but they're useful anyway
|
|
||||||
# - the next words: segment transcript
|
|
||||||
if now is None:
|
|
||||||
now = time.time()-start
|
|
||||||
if o[0] is not None:
|
|
||||||
print("%1.4f %1.0f %1.0f %s" % (now*1000, o[0]*1000,o[1]*1000,o[2]),file=logfile,flush=True)
|
|
||||||
print("%1.4f %1.0f %1.0f %s" % (now*1000, o[0]*1000,o[1]*1000,o[2]),flush=True)
|
|
||||||
else:
|
|
||||||
# No text, so no output
|
|
||||||
pass
|
|
||||||
|
|
||||||
if args.offline: ## offline mode processing (for testing/debugging)
|
|
||||||
a = load_audio(audio_path)
|
|
||||||
online.insert_audio_chunk(a)
|
|
||||||
try:
|
|
||||||
o = online.process_iter()
|
|
||||||
except AssertionError as e:
|
|
||||||
log.error(f"assertion error: {repr(e)}")
|
|
||||||
else:
|
|
||||||
output_transcript(o)
|
|
||||||
now = None
|
|
||||||
elif args.comp_unaware: # computational unaware mode
|
|
||||||
end = beg + min_chunk
|
|
||||||
while True:
|
|
||||||
a = load_audio_chunk(audio_path,beg,end)
|
|
||||||
online.insert_audio_chunk(a)
|
|
||||||
try:
|
|
||||||
o = online.process_iter()
|
|
||||||
except AssertionError as e:
|
|
||||||
logger.error(f"assertion error: {repr(e)}")
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
output_transcript(o, now=end)
|
|
||||||
|
|
||||||
logger.debug(f"## last processed {end:.2f}s")
|
|
||||||
|
|
||||||
if end >= duration:
|
|
||||||
break
|
|
||||||
|
|
||||||
beg = end
|
|
||||||
|
|
||||||
if end + min_chunk > duration:
|
|
||||||
end = duration
|
|
||||||
else:
|
|
||||||
end += min_chunk
|
|
||||||
now = duration
|
|
||||||
|
|
||||||
else: # online = simultaneous mode
|
|
||||||
end = 0
|
|
||||||
while True:
|
|
||||||
now = time.time() - start
|
|
||||||
if now < end+min_chunk:
|
|
||||||
time.sleep(min_chunk+end-now)
|
|
||||||
end = time.time() - start
|
|
||||||
a = load_audio_chunk(audio_path,beg,end)
|
|
||||||
beg = end
|
|
||||||
online.insert_audio_chunk(a)
|
|
||||||
|
|
||||||
try:
|
|
||||||
o = online.process_iter()
|
|
||||||
except AssertionError as e:
|
|
||||||
logger.error(f"assertion error: {e}")
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
output_transcript(o)
|
|
||||||
now = time.time() - start
|
|
||||||
logger.debug(f"## last processed {end:.2f} s, now is {now:.2f}, the latency is {now-end:.2f}")
|
|
||||||
|
|
||||||
if end >= duration:
|
|
||||||
break
|
|
||||||
now = None
|
|
||||||
|
|
||||||
o = online.finish()
|
|
||||||
output_transcript(o, now=now)
|
|
||||||
@@ -1,175 +0,0 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
from whisper_online import *
|
|
||||||
|
|
||||||
import sys
|
|
||||||
import argparse
|
|
||||||
import os
|
|
||||||
import logging
|
|
||||||
import numpy as np
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
|
||||||
parser = argparse.ArgumentParser()
|
|
||||||
|
|
||||||
# server options
|
|
||||||
parser.add_argument("--host", type=str, default='localhost')
|
|
||||||
parser.add_argument("--port", type=int, default=43007)
|
|
||||||
parser.add_argument("--warmup-file", type=str, dest="warmup_file",
|
|
||||||
help="The path to a speech audio wav file to warm up Whisper so that the very first chunk processing is fast. It can be e.g. https://github.com/ggerganov/whisper.cpp/raw/master/samples/jfk.wav .")
|
|
||||||
|
|
||||||
|
|
||||||
# options from whisper_online
|
|
||||||
add_shared_args(parser)
|
|
||||||
args = parser.parse_args()
|
|
||||||
|
|
||||||
set_logging(args,logger,other="")
|
|
||||||
|
|
||||||
# setting whisper object by args
|
|
||||||
|
|
||||||
SAMPLING_RATE = 16000
|
|
||||||
|
|
||||||
size = args.model
|
|
||||||
language = args.lan
|
|
||||||
asr, online = asr_factory(args)
|
|
||||||
min_chunk = args.min_chunk_size
|
|
||||||
|
|
||||||
# warm up the ASR because the very first transcribe takes more time than the others.
|
|
||||||
# Test results in https://github.com/ufal/whisper_streaming/pull/81
|
|
||||||
msg = "Whisper is not warmed up. The first chunk processing may take longer."
|
|
||||||
if args.warmup_file:
|
|
||||||
if os.path.isfile(args.warmup_file):
|
|
||||||
a = load_audio_chunk(args.warmup_file,0,1)
|
|
||||||
asr.transcribe(a)
|
|
||||||
logger.info("Whisper is warmed up.")
|
|
||||||
else:
|
|
||||||
logger.critical("The warm up file is not available. "+msg)
|
|
||||||
sys.exit(1)
|
|
||||||
else:
|
|
||||||
logger.warning(msg)
|
|
||||||
|
|
||||||
|
|
||||||
######### Server objects
|
|
||||||
|
|
||||||
import line_packet
|
|
||||||
import socket
|
|
||||||
|
|
||||||
class Connection:
|
|
||||||
'''it wraps conn object'''
|
|
||||||
PACKET_SIZE = 65536
|
|
||||||
|
|
||||||
def __init__(self, conn):
|
|
||||||
self.conn = conn
|
|
||||||
self.last_line = ""
|
|
||||||
|
|
||||||
self.conn.setblocking(True)
|
|
||||||
|
|
||||||
def send(self, line):
|
|
||||||
'''it doesn't send the same line twice, because it was problematic in online-text-flow-events'''
|
|
||||||
if line == self.last_line:
|
|
||||||
return
|
|
||||||
line_packet.send_one_line(self.conn, line)
|
|
||||||
self.last_line = line
|
|
||||||
|
|
||||||
def receive_lines(self):
|
|
||||||
in_line = line_packet.receive_lines(self.conn)
|
|
||||||
return in_line
|
|
||||||
|
|
||||||
def non_blocking_receive_audio(self):
|
|
||||||
r = self.conn.recv(self.PACKET_SIZE)
|
|
||||||
return r
|
|
||||||
|
|
||||||
|
|
||||||
import io
|
|
||||||
import soundfile
|
|
||||||
|
|
||||||
# wraps socket and ASR object, and serves one client connection.
|
|
||||||
# next client should be served by a new instance of this object
|
|
||||||
class ServerProcessor:
|
|
||||||
|
|
||||||
def __init__(self, c, online_asr_proc, min_chunk):
|
|
||||||
self.connection = c
|
|
||||||
self.online_asr_proc = online_asr_proc
|
|
||||||
self.min_chunk = min_chunk
|
|
||||||
|
|
||||||
self.last_end = None
|
|
||||||
|
|
||||||
def receive_audio_chunk(self):
|
|
||||||
# receive all audio that is available by this time
|
|
||||||
# blocks operation if less than self.min_chunk seconds is available
|
|
||||||
# unblocks if connection is closed or a chunk is available
|
|
||||||
out = []
|
|
||||||
while sum(len(x) for x in out) < self.min_chunk*SAMPLING_RATE:
|
|
||||||
raw_bytes = self.connection.non_blocking_receive_audio()
|
|
||||||
if not raw_bytes:
|
|
||||||
break
|
|
||||||
sf = soundfile.SoundFile(io.BytesIO(raw_bytes), channels=1,endian="LITTLE",samplerate=SAMPLING_RATE, subtype="PCM_16",format="RAW")
|
|
||||||
audio, _ = librosa.load(sf,sr=SAMPLING_RATE,dtype=np.float32)
|
|
||||||
out.append(audio)
|
|
||||||
if not out:
|
|
||||||
return None
|
|
||||||
return np.concatenate(out)
|
|
||||||
|
|
||||||
def format_output_transcript(self,o):
|
|
||||||
# output format in stdout is like:
|
|
||||||
# 0 1720 Takhle to je
|
|
||||||
# - the first two words are:
|
|
||||||
# - beg and end timestamp of the text segment, as estimated by Whisper model. The timestamps are not accurate, but they're useful anyway
|
|
||||||
# - the next words: segment transcript
|
|
||||||
|
|
||||||
# This function differs from whisper_online.output_transcript in the following:
|
|
||||||
# succeeding [beg,end] intervals are not overlapping because ELITR protocol (implemented in online-text-flow events) requires it.
|
|
||||||
# Therefore, beg, is max of previous end and current beg outputed by Whisper.
|
|
||||||
# Usually it differs negligibly, by appx 20 ms.
|
|
||||||
|
|
||||||
if o[0] is not None:
|
|
||||||
beg, end = o[0]*1000,o[1]*1000
|
|
||||||
if self.last_end is not None:
|
|
||||||
beg = max(beg, self.last_end)
|
|
||||||
|
|
||||||
self.last_end = end
|
|
||||||
print("%1.0f %1.0f %s" % (beg,end,o[2]),flush=True,file=sys.stderr)
|
|
||||||
return "%1.0f %1.0f %s" % (beg,end,o[2])
|
|
||||||
else:
|
|
||||||
logger.debug("No text in this segment")
|
|
||||||
return None
|
|
||||||
|
|
||||||
def send_result(self, o):
|
|
||||||
msg = self.format_output_transcript(o)
|
|
||||||
if msg is not None:
|
|
||||||
self.connection.send(msg)
|
|
||||||
|
|
||||||
def process(self):
|
|
||||||
# handle one client connection
|
|
||||||
self.online_asr_proc.init()
|
|
||||||
while True:
|
|
||||||
a = self.receive_audio_chunk()
|
|
||||||
if a is None:
|
|
||||||
break
|
|
||||||
self.online_asr_proc.insert_audio_chunk(a)
|
|
||||||
o = online.process_iter()
|
|
||||||
try:
|
|
||||||
self.send_result(o)
|
|
||||||
except BrokenPipeError:
|
|
||||||
logger.info("broken pipe -- connection closed?")
|
|
||||||
break
|
|
||||||
|
|
||||||
# o = online.finish() # this should be working
|
|
||||||
# self.send_result(o)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# server loop
|
|
||||||
|
|
||||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
|
||||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
||||||
s.bind((args.host, args.port))
|
|
||||||
s.listen(1)
|
|
||||||
logger.info('Listening on'+str((args.host, args.port)))
|
|
||||||
while True:
|
|
||||||
conn, addr = s.accept()
|
|
||||||
logger.info('Connected to client on {}'.format(addr))
|
|
||||||
connection = Connection(conn)
|
|
||||||
proc = ServerProcessor(connection, online, min_chunk)
|
|
||||||
proc.process()
|
|
||||||
conn.close()
|
|
||||||
logger.info('Connection to client closed')
|
|
||||||
logger.info('Connection closed, terminating.')
|
|
||||||
12
whisperlivekit/__init__.py
Normal file
12
whisperlivekit/__init__.py
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
from .audio_processor import AudioProcessor
|
||||||
|
from .core import TranscriptionEngine
|
||||||
|
from .parse_args import parse_args
|
||||||
|
from .web.web_interface import get_web_interface_html
|
||||||
|
|
||||||
|
__all__ = [
|
||||||
|
"TranscriptionEngine",
|
||||||
|
"AudioProcessor",
|
||||||
|
"parse_args",
|
||||||
|
"get_web_interface_html",
|
||||||
|
"download_simulstreaming_backend",
|
||||||
|
]
|
||||||
665
whisperlivekit/audio_processor.py
Normal file
665
whisperlivekit/audio_processor.py
Normal file
@@ -0,0 +1,665 @@
|
|||||||
|
import asyncio
|
||||||
|
import numpy as np
|
||||||
|
from time import time, sleep
|
||||||
|
import math
|
||||||
|
import logging
|
||||||
|
import traceback
|
||||||
|
from datetime import timedelta
|
||||||
|
from whisperlivekit.timed_objects import ASRToken, Silence
|
||||||
|
from whisperlivekit.core import TranscriptionEngine, online_factory
|
||||||
|
from whisperlivekit.ffmpeg_manager import FFmpegManager, FFmpegState
|
||||||
|
from whisperlivekit.remove_silences import handle_silences
|
||||||
|
from whisperlivekit.trail_repetition import trim_tail_repetition
|
||||||
|
from whisperlivekit.silero_vad_iterator import FixedVADIterator
|
||||||
|
# Set up logging once
|
||||||
|
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
logger.setLevel(logging.DEBUG)
|
||||||
|
|
||||||
|
SENTINEL = object() # unique sentinel object for end of stream marker
|
||||||
|
|
||||||
|
def format_time(seconds: float) -> str:
|
||||||
|
"""Format seconds as HH:MM:SS."""
|
||||||
|
return str(timedelta(seconds=int(seconds)))
|
||||||
|
|
||||||
|
class AudioProcessor:
|
||||||
|
"""
|
||||||
|
Processes audio streams for transcription and diarization.
|
||||||
|
Handles audio processing, state management, and result formatting.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, **kwargs):
|
||||||
|
"""Initialize the audio processor with configuration, models, and state."""
|
||||||
|
|
||||||
|
if 'transcription_engine' in kwargs and isinstance(kwargs['transcription_engine'], TranscriptionEngine):
|
||||||
|
models = kwargs['transcription_engine']
|
||||||
|
else:
|
||||||
|
models = TranscriptionEngine(**kwargs)
|
||||||
|
|
||||||
|
# Audio processing settings
|
||||||
|
self.args = models.args
|
||||||
|
self.sample_rate = 16000
|
||||||
|
self.channels = 1
|
||||||
|
self.samples_per_sec = int(self.sample_rate * self.args.min_chunk_size)
|
||||||
|
self.bytes_per_sample = 2
|
||||||
|
self.bytes_per_sec = self.samples_per_sec * self.bytes_per_sample
|
||||||
|
self.max_bytes_per_sec = 32000 * 5 # 5 seconds of audio at 32 kHz
|
||||||
|
self.last_ffmpeg_activity = time()
|
||||||
|
self.ffmpeg_health_check_interval = 5
|
||||||
|
self.ffmpeg_max_idle_time = 10
|
||||||
|
self.debug = False
|
||||||
|
|
||||||
|
# State management
|
||||||
|
self.is_stopping = False
|
||||||
|
self.silence = False
|
||||||
|
self.silence_duration = 0.0
|
||||||
|
self.tokens = []
|
||||||
|
self.buffer_transcription = ""
|
||||||
|
self.buffer_diarization = ""
|
||||||
|
self.end_buffer = 0
|
||||||
|
self.end_attributed_speaker = 0
|
||||||
|
self.lock = asyncio.Lock()
|
||||||
|
self.beg_loop = None #to deal with a potential little lag at the websocket initialization, this is now set in process_audio
|
||||||
|
self.sep = " " # Default separator
|
||||||
|
self.last_response_content = ""
|
||||||
|
|
||||||
|
# Models and processing
|
||||||
|
self.asr = models.asr
|
||||||
|
self.tokenizer = models.tokenizer
|
||||||
|
self.diarization = models.diarization
|
||||||
|
self.vac_model = models.vac_model
|
||||||
|
if self.args.vac:
|
||||||
|
self.vac = FixedVADIterator(models.vac_model)
|
||||||
|
else:
|
||||||
|
self.vac = None
|
||||||
|
|
||||||
|
self.ffmpeg_manager = FFmpegManager(
|
||||||
|
sample_rate=self.sample_rate,
|
||||||
|
channels=self.channels
|
||||||
|
)
|
||||||
|
|
||||||
|
async def handle_ffmpeg_error(error_type: str):
|
||||||
|
logger.error(f"FFmpeg error: {error_type}")
|
||||||
|
self._ffmpeg_error = error_type
|
||||||
|
|
||||||
|
self.ffmpeg_manager.on_error_callback = handle_ffmpeg_error
|
||||||
|
self._ffmpeg_error = None
|
||||||
|
|
||||||
|
self.transcription_queue = asyncio.Queue() if self.args.transcription else None
|
||||||
|
self.diarization_queue = asyncio.Queue() if self.args.diarization else None
|
||||||
|
self.pcm_buffer = bytearray()
|
||||||
|
|
||||||
|
# Task references
|
||||||
|
self.transcription_task = None
|
||||||
|
self.diarization_task = None
|
||||||
|
self.ffmpeg_reader_task = None
|
||||||
|
self.watchdog_task = None
|
||||||
|
self.all_tasks_for_cleanup = []
|
||||||
|
|
||||||
|
# Initialize transcription engine if enabled
|
||||||
|
if self.args.transcription:
|
||||||
|
self.online = online_factory(self.args, models.asr, models.tokenizer)
|
||||||
|
|
||||||
|
def convert_pcm_to_float(self, pcm_buffer):
|
||||||
|
"""Convert PCM buffer in s16le format to normalized NumPy array."""
|
||||||
|
return np.frombuffer(pcm_buffer, dtype=np.int16).astype(np.float32) / 32768.0
|
||||||
|
|
||||||
|
async def update_transcription(self, new_tokens, buffer, end_buffer, sep):
|
||||||
|
"""Thread-safe update of transcription with new data."""
|
||||||
|
async with self.lock:
|
||||||
|
self.tokens.extend(new_tokens)
|
||||||
|
|
||||||
|
# self.tokens, has_been_trimmed = trim_tail_repetition(
|
||||||
|
# self.tokens,
|
||||||
|
# key=lambda t: t.text.strip().lower(),
|
||||||
|
# min_block=2, # avoid trimming single '.' loops; set to 1 if you want to remove those too
|
||||||
|
# max_tail=200,
|
||||||
|
# prefer="longest", # prefer removing the longest repeated phrase
|
||||||
|
# keep=1
|
||||||
|
# )
|
||||||
|
# if has_been_trimmed:
|
||||||
|
# print('HAS BEEN TRIMMED !')
|
||||||
|
self.buffer_transcription = buffer
|
||||||
|
self.end_buffer = end_buffer
|
||||||
|
self.sep = sep
|
||||||
|
|
||||||
|
async def update_diarization(self, end_attributed_speaker, buffer_diarization=""):
|
||||||
|
"""Thread-safe update of diarization with new data."""
|
||||||
|
async with self.lock:
|
||||||
|
self.end_attributed_speaker = end_attributed_speaker
|
||||||
|
if buffer_diarization:
|
||||||
|
self.buffer_diarization = buffer_diarization
|
||||||
|
|
||||||
|
async def add_dummy_token(self):
|
||||||
|
"""Placeholder token when no transcription is available."""
|
||||||
|
async with self.lock:
|
||||||
|
current_time = time() - self.beg_loop
|
||||||
|
self.tokens.append(ASRToken(
|
||||||
|
start=current_time, end=current_time + 1,
|
||||||
|
text=".", speaker=-1, is_dummy=True
|
||||||
|
))
|
||||||
|
|
||||||
|
async def get_current_state(self):
|
||||||
|
"""Get current state."""
|
||||||
|
async with self.lock:
|
||||||
|
current_time = time()
|
||||||
|
|
||||||
|
# Calculate remaining times
|
||||||
|
remaining_transcription = 0
|
||||||
|
if self.end_buffer > 0:
|
||||||
|
remaining_transcription = max(0, round(current_time - self.beg_loop - self.end_buffer, 1))
|
||||||
|
|
||||||
|
remaining_diarization = 0
|
||||||
|
if self.tokens:
|
||||||
|
latest_end = max(self.end_buffer, self.tokens[-1].end if self.tokens else 0)
|
||||||
|
remaining_diarization = max(0, round(latest_end - self.end_attributed_speaker, 1))
|
||||||
|
|
||||||
|
return {
|
||||||
|
"tokens": self.tokens.copy(),
|
||||||
|
"buffer_transcription": self.buffer_transcription,
|
||||||
|
"buffer_diarization": self.buffer_diarization,
|
||||||
|
"end_buffer": self.end_buffer,
|
||||||
|
"end_attributed_speaker": self.end_attributed_speaker,
|
||||||
|
"sep": self.sep,
|
||||||
|
"remaining_time_transcription": remaining_transcription,
|
||||||
|
"remaining_time_diarization": remaining_diarization
|
||||||
|
}
|
||||||
|
|
||||||
|
async def reset(self):
|
||||||
|
"""Reset all state variables to initial values."""
|
||||||
|
async with self.lock:
|
||||||
|
self.tokens = []
|
||||||
|
self.buffer_transcription = self.buffer_diarization = ""
|
||||||
|
self.end_buffer = self.end_attributed_speaker = 0
|
||||||
|
self.beg_loop = time()
|
||||||
|
|
||||||
|
async def ffmpeg_stdout_reader(self):
|
||||||
|
"""Read audio data from FFmpeg stdout and process it."""
|
||||||
|
beg = time()
|
||||||
|
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
# Check if FFmpeg is running
|
||||||
|
state = await self.ffmpeg_manager.get_state()
|
||||||
|
if state == FFmpegState.FAILED:
|
||||||
|
logger.error("FFmpeg is in FAILED state, cannot read data")
|
||||||
|
break
|
||||||
|
elif state == FFmpegState.STOPPED:
|
||||||
|
logger.info("FFmpeg is stopped")
|
||||||
|
break
|
||||||
|
elif state != FFmpegState.RUNNING:
|
||||||
|
logger.warning(f"FFmpeg is in {state} state, waiting...")
|
||||||
|
await asyncio.sleep(0.5)
|
||||||
|
continue
|
||||||
|
|
||||||
|
current_time = time()
|
||||||
|
elapsed_time = math.floor((current_time - beg) * 10) / 10
|
||||||
|
buffer_size = max(int(32000 * elapsed_time), 4096)
|
||||||
|
beg = current_time
|
||||||
|
|
||||||
|
chunk = await self.ffmpeg_manager.read_data(buffer_size)
|
||||||
|
|
||||||
|
if not chunk:
|
||||||
|
if self.is_stopping:
|
||||||
|
logger.info("FFmpeg stdout closed, stopping.")
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
# No data available, but not stopping - FFmpeg might be restarting
|
||||||
|
await asyncio.sleep(0.1)
|
||||||
|
continue
|
||||||
|
|
||||||
|
self.pcm_buffer.extend(chunk)
|
||||||
|
|
||||||
|
# Process when enough data
|
||||||
|
if len(self.pcm_buffer) >= self.bytes_per_sec:
|
||||||
|
if len(self.pcm_buffer) > self.max_bytes_per_sec:
|
||||||
|
logger.warning(
|
||||||
|
f"Audio buffer too large: {len(self.pcm_buffer) / self.bytes_per_sec:.2f}s. "
|
||||||
|
f"Consider using a smaller model."
|
||||||
|
)
|
||||||
|
|
||||||
|
# Process audio chunk
|
||||||
|
pcm_array = self.convert_pcm_to_float(self.pcm_buffer[:self.max_bytes_per_sec])
|
||||||
|
self.pcm_buffer = self.pcm_buffer[self.max_bytes_per_sec:]
|
||||||
|
|
||||||
|
res = None
|
||||||
|
end_of_audio = False
|
||||||
|
silence_buffer = None
|
||||||
|
|
||||||
|
if self.args.vac:
|
||||||
|
res = self.vac(pcm_array)
|
||||||
|
|
||||||
|
if res is not None:
|
||||||
|
if res.get('end', 0) > res.get('start', 0):
|
||||||
|
end_of_audio = True
|
||||||
|
elif self.silence: #end of silence
|
||||||
|
self.silence = False
|
||||||
|
silence_buffer = Silence(duration=time() - self.start_silence)
|
||||||
|
|
||||||
|
if silence_buffer:
|
||||||
|
if self.args.transcription and self.transcription_queue:
|
||||||
|
await self.transcription_queue.put(silence_buffer)
|
||||||
|
if self.args.diarization and self.diarization_queue:
|
||||||
|
await self.diarization_queue.put(silence_buffer)
|
||||||
|
|
||||||
|
if not self.silence:
|
||||||
|
if self.args.transcription and self.transcription_queue:
|
||||||
|
await self.transcription_queue.put(pcm_array.copy())
|
||||||
|
|
||||||
|
if self.args.diarization and self.diarization_queue:
|
||||||
|
await self.diarization_queue.put(pcm_array.copy())
|
||||||
|
|
||||||
|
self.silence_duration = 0.0
|
||||||
|
if end_of_audio:
|
||||||
|
self.silence = True
|
||||||
|
self.start_silence = time()
|
||||||
|
|
||||||
|
# Sleep if no processing is happening
|
||||||
|
if not self.args.transcription and not self.args.diarization:
|
||||||
|
await asyncio.sleep(0.1)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"Exception in ffmpeg_stdout_reader: {e}")
|
||||||
|
logger.warning(f"Traceback: {traceback.format_exc()}")
|
||||||
|
# Try to recover by waiting a bit
|
||||||
|
await asyncio.sleep(1)
|
||||||
|
|
||||||
|
# Check if we should exit
|
||||||
|
if self.is_stopping:
|
||||||
|
break
|
||||||
|
|
||||||
|
logger.info("FFmpeg stdout processing finished. Signaling downstream processors.")
|
||||||
|
if self.args.transcription and self.transcription_queue:
|
||||||
|
await self.transcription_queue.put(SENTINEL)
|
||||||
|
logger.debug("Sentinel put into transcription_queue.")
|
||||||
|
if self.args.diarization and self.diarization_queue:
|
||||||
|
await self.diarization_queue.put(SENTINEL)
|
||||||
|
logger.debug("Sentinel put into diarization_queue.")
|
||||||
|
|
||||||
|
|
||||||
|
async def transcription_processor(self):
|
||||||
|
"""Process audio chunks for transcription."""
|
||||||
|
self.sep = self.online.asr.sep
|
||||||
|
cumulative_pcm_duration_stream_time = 0.0
|
||||||
|
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
item = await self.transcription_queue.get()
|
||||||
|
if item is SENTINEL:
|
||||||
|
logger.debug("Transcription processor received sentinel. Finishing.")
|
||||||
|
self.transcription_queue.task_done()
|
||||||
|
break
|
||||||
|
|
||||||
|
if not self.online:
|
||||||
|
logger.warning("Transcription processor: self.online not initialized.")
|
||||||
|
self.transcription_queue.task_done()
|
||||||
|
continue
|
||||||
|
|
||||||
|
asr_internal_buffer_duration_s = len(getattr(self.online, 'audio_buffer', [])) / self.online.SAMPLING_RATE
|
||||||
|
transcription_lag_s = max(0.0, time() - self.beg_loop - self.end_buffer)
|
||||||
|
asr_processing_logs = f"internal_buffer={asr_internal_buffer_duration_s:.2f}s | lag={transcription_lag_s:.2f}s |"
|
||||||
|
if type(item) is Silence:
|
||||||
|
asr_processing_logs += f" + Silence of = {item.duration:.2f}s"
|
||||||
|
if self.tokens:
|
||||||
|
asr_processing_logs += " | last_end = {self.tokens[-1].end} |"
|
||||||
|
logger.info(asr_processing_logs)
|
||||||
|
|
||||||
|
if type(item) is Silence:
|
||||||
|
cumulative_pcm_duration_stream_time += item.duration
|
||||||
|
self.online.insert_silence(item.duration, self.tokens[-1].end)
|
||||||
|
continue
|
||||||
|
|
||||||
|
if isinstance(item, np.ndarray):
|
||||||
|
pcm_array = item
|
||||||
|
else:
|
||||||
|
raise Exception('item should be pcm_array')
|
||||||
|
|
||||||
|
duration_this_chunk = len(pcm_array) / self.sample_rate
|
||||||
|
cumulative_pcm_duration_stream_time += duration_this_chunk
|
||||||
|
stream_time_end_of_current_pcm = cumulative_pcm_duration_stream_time
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
self.online.insert_audio_chunk(pcm_array, stream_time_end_of_current_pcm)
|
||||||
|
new_tokens, current_audio_processed_upto = self.online.process_iter()
|
||||||
|
|
||||||
|
# Get buffer information
|
||||||
|
_buffer_transcript_obj = self.online.get_buffer()
|
||||||
|
buffer_text = _buffer_transcript_obj.text
|
||||||
|
|
||||||
|
if new_tokens:
|
||||||
|
validated_text = self.sep.join([t.text for t in new_tokens])
|
||||||
|
if buffer_text.startswith(validated_text):
|
||||||
|
buffer_text = buffer_text[len(validated_text):].lstrip()
|
||||||
|
|
||||||
|
candidate_end_times = [self.end_buffer]
|
||||||
|
|
||||||
|
if new_tokens:
|
||||||
|
candidate_end_times.append(new_tokens[-1].end)
|
||||||
|
|
||||||
|
if _buffer_transcript_obj.end is not None:
|
||||||
|
candidate_end_times.append(_buffer_transcript_obj.end)
|
||||||
|
|
||||||
|
candidate_end_times.append(current_audio_processed_upto)
|
||||||
|
|
||||||
|
new_end_buffer = max(candidate_end_times)
|
||||||
|
|
||||||
|
await self.update_transcription(
|
||||||
|
new_tokens, buffer_text, new_end_buffer, self.sep
|
||||||
|
)
|
||||||
|
self.transcription_queue.task_done()
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"Exception in transcription_processor: {e}")
|
||||||
|
logger.warning(f"Traceback: {traceback.format_exc()}")
|
||||||
|
if 'pcm_array' in locals() and pcm_array is not SENTINEL : # Check if pcm_array was assigned from queue
|
||||||
|
self.transcription_queue.task_done()
|
||||||
|
logger.info("Transcription processor task finished.")
|
||||||
|
|
||||||
|
|
||||||
|
async def diarization_processor(self, diarization_obj):
|
||||||
|
"""Process audio chunks for speaker diarization."""
|
||||||
|
buffer_diarization = ""
|
||||||
|
cumulative_pcm_duration_stream_time = 0.0
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
item = await self.diarization_queue.get()
|
||||||
|
if item is SENTINEL:
|
||||||
|
logger.debug("Diarization processor received sentinel. Finishing.")
|
||||||
|
self.diarization_queue.task_done()
|
||||||
|
break
|
||||||
|
|
||||||
|
if type(item) is Silence:
|
||||||
|
cumulative_pcm_duration_stream_time += item.duration
|
||||||
|
diarization_obj.insert_silence(item.duration)
|
||||||
|
continue
|
||||||
|
|
||||||
|
if isinstance(item, np.ndarray):
|
||||||
|
pcm_array = item
|
||||||
|
else:
|
||||||
|
raise Exception('item should be pcm_array')
|
||||||
|
|
||||||
|
# Process diarization
|
||||||
|
await diarization_obj.diarize(pcm_array)
|
||||||
|
|
||||||
|
async with self.lock:
|
||||||
|
self.tokens = diarization_obj.assign_speakers_to_tokens(
|
||||||
|
self.tokens,
|
||||||
|
use_punctuation_split=self.args.punctuation_split
|
||||||
|
)
|
||||||
|
if len(self.tokens) > 0:
|
||||||
|
self.end_attributed_speaker = max(self.tokens[-1].end, self.end_attributed_speaker)
|
||||||
|
if buffer_diarization:
|
||||||
|
self.buffer_diarization = buffer_diarization
|
||||||
|
|
||||||
|
self.diarization_queue.task_done()
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"Exception in diarization_processor: {e}")
|
||||||
|
logger.warning(f"Traceback: {traceback.format_exc()}")
|
||||||
|
if 'pcm_array' in locals() and pcm_array is not SENTINEL:
|
||||||
|
self.diarization_queue.task_done()
|
||||||
|
logger.info("Diarization processor task finished.")
|
||||||
|
|
||||||
|
|
||||||
|
async def results_formatter(self):
|
||||||
|
"""Format processing results for output."""
|
||||||
|
last_sent_trans = None
|
||||||
|
last_sent_diar = None
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
ffmpeg_state = await self.ffmpeg_manager.get_state()
|
||||||
|
if ffmpeg_state == FFmpegState.FAILED and self._ffmpeg_error:
|
||||||
|
yield {
|
||||||
|
"status": "error",
|
||||||
|
"error": f"FFmpeg error: {self._ffmpeg_error}",
|
||||||
|
"lines": [],
|
||||||
|
"buffer_transcription": "",
|
||||||
|
"buffer_diarization": "",
|
||||||
|
"remaining_time_transcription": 0,
|
||||||
|
"remaining_time_diarization": 0
|
||||||
|
}
|
||||||
|
self._ffmpeg_error = None
|
||||||
|
await asyncio.sleep(1)
|
||||||
|
continue
|
||||||
|
|
||||||
|
# Get current state
|
||||||
|
state = await self.get_current_state()
|
||||||
|
tokens = state["tokens"]
|
||||||
|
buffer_transcription = state["buffer_transcription"]
|
||||||
|
buffer_diarization = state["buffer_diarization"]
|
||||||
|
end_attributed_speaker = state["end_attributed_speaker"]
|
||||||
|
sep = state["sep"]
|
||||||
|
|
||||||
|
# Add dummy tokens if needed
|
||||||
|
if (not tokens or tokens[-1].is_dummy) and not self.args.transcription and self.args.diarization:
|
||||||
|
await self.add_dummy_token()
|
||||||
|
sleep(0.5)
|
||||||
|
state = await self.get_current_state()
|
||||||
|
tokens = state["tokens"]
|
||||||
|
|
||||||
|
# Format output
|
||||||
|
previous_speaker = -1
|
||||||
|
lines = []
|
||||||
|
last_end_diarized = 0
|
||||||
|
undiarized_text = []
|
||||||
|
current_time = time() - self.beg_loop if self.beg_loop else None
|
||||||
|
tokens, buffer_transcription, buffer_diarization = handle_silences(tokens, buffer_transcription, buffer_diarization, current_time, self.silence)
|
||||||
|
for token in tokens:
|
||||||
|
speaker = token.speaker
|
||||||
|
|
||||||
|
if speaker == -1: #Speaker -1 means no attributed by diarization. In the frontend, it should appear under 'Speaker 1'
|
||||||
|
speaker = 1
|
||||||
|
|
||||||
|
# Handle diarization
|
||||||
|
if self.args.diarization and not tokens[-1].speaker == -2:
|
||||||
|
if (speaker in [-1, 0]) and token.end >= end_attributed_speaker:
|
||||||
|
undiarized_text.append(token.text)
|
||||||
|
continue
|
||||||
|
elif (speaker in [-1, 0]) and token.end < end_attributed_speaker:
|
||||||
|
speaker = previous_speaker
|
||||||
|
if speaker not in [-1, 0]:
|
||||||
|
last_end_diarized = max(token.end, last_end_diarized)
|
||||||
|
|
||||||
|
debug_info = ""
|
||||||
|
if self.debug:
|
||||||
|
debug_info = f"[{format_time(token.start)} : {format_time(token.end)}]"
|
||||||
|
if speaker != previous_speaker or not lines:
|
||||||
|
lines.append({
|
||||||
|
"speaker": speaker,
|
||||||
|
"text": token.text + debug_info,
|
||||||
|
"beg": format_time(token.start),
|
||||||
|
"end": format_time(token.end),
|
||||||
|
"diff": round(token.end - last_end_diarized, 2)
|
||||||
|
})
|
||||||
|
previous_speaker = speaker
|
||||||
|
elif token.text: # Only append if text isn't empty
|
||||||
|
lines[-1]["text"] += sep + token.text + debug_info
|
||||||
|
lines[-1]["end"] = format_time(token.end)
|
||||||
|
lines[-1]["diff"] = round(token.end - last_end_diarized, 2)
|
||||||
|
|
||||||
|
# Handle undiarized text
|
||||||
|
if undiarized_text:
|
||||||
|
combined = sep.join(undiarized_text)
|
||||||
|
if buffer_transcription:
|
||||||
|
combined += sep
|
||||||
|
await self.update_diarization(end_attributed_speaker, combined)
|
||||||
|
buffer_diarization = combined
|
||||||
|
|
||||||
|
response_status = "active_transcription"
|
||||||
|
final_lines_for_response = lines.copy()
|
||||||
|
|
||||||
|
if not tokens and not buffer_transcription and not buffer_diarization:
|
||||||
|
response_status = "no_audio_detected"
|
||||||
|
final_lines_for_response = []
|
||||||
|
elif response_status == "active_transcription" and not final_lines_for_response:
|
||||||
|
final_lines_for_response = [{
|
||||||
|
"speaker": 1,
|
||||||
|
"text": "",
|
||||||
|
"beg": format_time(state.get("end_buffer", 0)),
|
||||||
|
"end": format_time(state.get("end_buffer", 0)),
|
||||||
|
"diff": 0
|
||||||
|
}]
|
||||||
|
|
||||||
|
response = {
|
||||||
|
"status": response_status,
|
||||||
|
"lines": final_lines_for_response,
|
||||||
|
"buffer_transcription": buffer_transcription,
|
||||||
|
"buffer_diarization": buffer_diarization,
|
||||||
|
"remaining_time_transcription": state["remaining_time_transcription"],
|
||||||
|
"remaining_time_diarization": state["remaining_time_diarization"]
|
||||||
|
}
|
||||||
|
|
||||||
|
current_response_signature = f"{response_status} | " + \
|
||||||
|
' '.join([f"{line['speaker']} {line['text']}" for line in final_lines_for_response]) + \
|
||||||
|
f" | {buffer_transcription} | {buffer_diarization}"
|
||||||
|
|
||||||
|
trans = state["remaining_time_transcription"]
|
||||||
|
diar = state["remaining_time_diarization"]
|
||||||
|
should_push = (
|
||||||
|
current_response_signature != self.last_response_content
|
||||||
|
or last_sent_trans is None
|
||||||
|
or round(trans, 1) != round(last_sent_trans, 1)
|
||||||
|
or round(diar, 1) != round(last_sent_diar, 1)
|
||||||
|
)
|
||||||
|
if should_push and (final_lines_for_response or buffer_transcription or buffer_diarization or response_status == "no_audio_detected" or trans > 0 or diar > 0):
|
||||||
|
yield response
|
||||||
|
self.last_response_content = current_response_signature
|
||||||
|
last_sent_trans = trans
|
||||||
|
last_sent_diar = diar
|
||||||
|
|
||||||
|
# Check for termination condition
|
||||||
|
if self.is_stopping:
|
||||||
|
all_processors_done = True
|
||||||
|
if self.args.transcription and self.transcription_task and not self.transcription_task.done():
|
||||||
|
all_processors_done = False
|
||||||
|
if self.args.diarization and self.diarization_task and not self.diarization_task.done():
|
||||||
|
all_processors_done = False
|
||||||
|
|
||||||
|
if all_processors_done:
|
||||||
|
logger.info("Results formatter: All upstream processors are done and in stopping state. Terminating.")
|
||||||
|
final_state = await self.get_current_state()
|
||||||
|
return
|
||||||
|
|
||||||
|
await asyncio.sleep(0.1) # Avoid overwhelming the client
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"Exception in results_formatter: {e}")
|
||||||
|
logger.warning(f"Traceback: {traceback.format_exc()}")
|
||||||
|
await asyncio.sleep(0.5) # Back off on error
|
||||||
|
|
||||||
|
async def create_tasks(self):
|
||||||
|
"""Create and start processing tasks."""
|
||||||
|
self.all_tasks_for_cleanup = []
|
||||||
|
processing_tasks_for_watchdog = []
|
||||||
|
|
||||||
|
success = await self.ffmpeg_manager.start()
|
||||||
|
if not success:
|
||||||
|
logger.error("Failed to start FFmpeg manager")
|
||||||
|
async def error_generator():
|
||||||
|
yield {
|
||||||
|
"status": "error",
|
||||||
|
"error": "FFmpeg failed to start. Please check that FFmpeg is installed.",
|
||||||
|
"lines": [],
|
||||||
|
"buffer_transcription": "",
|
||||||
|
"buffer_diarization": "",
|
||||||
|
"remaining_time_transcription": 0,
|
||||||
|
"remaining_time_diarization": 0
|
||||||
|
}
|
||||||
|
return error_generator()
|
||||||
|
|
||||||
|
if self.args.transcription and self.online:
|
||||||
|
self.transcription_task = asyncio.create_task(self.transcription_processor())
|
||||||
|
self.all_tasks_for_cleanup.append(self.transcription_task)
|
||||||
|
processing_tasks_for_watchdog.append(self.transcription_task)
|
||||||
|
|
||||||
|
if self.args.diarization and self.diarization:
|
||||||
|
self.diarization_task = asyncio.create_task(self.diarization_processor(self.diarization))
|
||||||
|
self.all_tasks_for_cleanup.append(self.diarization_task)
|
||||||
|
processing_tasks_for_watchdog.append(self.diarization_task)
|
||||||
|
|
||||||
|
self.ffmpeg_reader_task = asyncio.create_task(self.ffmpeg_stdout_reader())
|
||||||
|
self.all_tasks_for_cleanup.append(self.ffmpeg_reader_task)
|
||||||
|
processing_tasks_for_watchdog.append(self.ffmpeg_reader_task)
|
||||||
|
|
||||||
|
# Monitor overall system health
|
||||||
|
self.watchdog_task = asyncio.create_task(self.watchdog(processing_tasks_for_watchdog))
|
||||||
|
self.all_tasks_for_cleanup.append(self.watchdog_task)
|
||||||
|
|
||||||
|
return self.results_formatter()
|
||||||
|
|
||||||
|
async def watchdog(self, tasks_to_monitor):
|
||||||
|
"""Monitors the health of critical processing tasks."""
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
await asyncio.sleep(10)
|
||||||
|
|
||||||
|
for i, task in enumerate(tasks_to_monitor):
|
||||||
|
if task.done():
|
||||||
|
exc = task.exception()
|
||||||
|
task_name = task.get_name() if hasattr(task, 'get_name') else f"Monitored Task {i}"
|
||||||
|
if exc:
|
||||||
|
logger.error(f"{task_name} unexpectedly completed with exception: {exc}")
|
||||||
|
else:
|
||||||
|
logger.info(f"{task_name} completed normally.")
|
||||||
|
|
||||||
|
# Check FFmpeg status through the manager
|
||||||
|
ffmpeg_state = await self.ffmpeg_manager.get_state()
|
||||||
|
if ffmpeg_state == FFmpegState.FAILED:
|
||||||
|
logger.error("FFmpeg is in FAILED state, notifying results formatter")
|
||||||
|
# FFmpeg manager will handle its own recovery
|
||||||
|
elif ffmpeg_state == FFmpegState.STOPPED and not self.is_stopping:
|
||||||
|
logger.warning("FFmpeg unexpectedly stopped, attempting restart")
|
||||||
|
await self.ffmpeg_manager.restart()
|
||||||
|
|
||||||
|
except asyncio.CancelledError:
|
||||||
|
logger.info("Watchdog task cancelled.")
|
||||||
|
break
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error in watchdog task: {e}", exc_info=True)
|
||||||
|
|
||||||
|
async def cleanup(self):
|
||||||
|
"""Clean up resources when processing is complete."""
|
||||||
|
logger.info("Starting cleanup of AudioProcessor resources.")
|
||||||
|
for task in self.all_tasks_for_cleanup:
|
||||||
|
if task and not task.done():
|
||||||
|
task.cancel()
|
||||||
|
|
||||||
|
created_tasks = [t for t in self.all_tasks_for_cleanup if t]
|
||||||
|
if created_tasks:
|
||||||
|
await asyncio.gather(*created_tasks, return_exceptions=True)
|
||||||
|
logger.info("All processing tasks cancelled or finished.")
|
||||||
|
await self.ffmpeg_manager.stop()
|
||||||
|
logger.info("FFmpeg manager stopped.")
|
||||||
|
if self.args.diarization and hasattr(self, 'diarization') and hasattr(self.diarization, 'close'):
|
||||||
|
self.diarization.close()
|
||||||
|
logger.info("AudioProcessor cleanup complete.")
|
||||||
|
|
||||||
|
|
||||||
|
async def process_audio(self, message):
|
||||||
|
"""Process incoming audio data."""
|
||||||
|
|
||||||
|
if not self.beg_loop:
|
||||||
|
self.beg_loop = time()
|
||||||
|
|
||||||
|
if not message:
|
||||||
|
logger.info("Empty audio message received, initiating stop sequence.")
|
||||||
|
self.is_stopping = True
|
||||||
|
# Signal FFmpeg manager to stop accepting data
|
||||||
|
await self.ffmpeg_manager.stop()
|
||||||
|
return
|
||||||
|
|
||||||
|
if self.is_stopping:
|
||||||
|
logger.warning("AudioProcessor is stopping. Ignoring incoming audio.")
|
||||||
|
return
|
||||||
|
|
||||||
|
success = await self.ffmpeg_manager.write_data(message)
|
||||||
|
if not success:
|
||||||
|
ffmpeg_state = await self.ffmpeg_manager.get_state()
|
||||||
|
if ffmpeg_state == FFmpegState.FAILED:
|
||||||
|
logger.error("FFmpeg is in FAILED state, cannot process audio")
|
||||||
|
else:
|
||||||
|
logger.warning("Failed to write audio data to FFmpeg")
|
||||||
125
whisperlivekit/basic_server.py
Normal file
125
whisperlivekit/basic_server.py
Normal file
@@ -0,0 +1,125 @@
|
|||||||
|
from contextlib import asynccontextmanager
|
||||||
|
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
|
||||||
|
from fastapi.responses import HTMLResponse
|
||||||
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
|
from whisperlivekit import TranscriptionEngine, AudioProcessor, get_web_interface_html, parse_args
|
||||||
|
import asyncio
|
||||||
|
import logging
|
||||||
|
from starlette.staticfiles import StaticFiles
|
||||||
|
import pathlib
|
||||||
|
import whisperlivekit.web as webpkg
|
||||||
|
|
||||||
|
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
||||||
|
logging.getLogger().setLevel(logging.WARNING)
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
logger.setLevel(logging.DEBUG)
|
||||||
|
|
||||||
|
args = parse_args()
|
||||||
|
transcription_engine = None
|
||||||
|
|
||||||
|
@asynccontextmanager
|
||||||
|
async def lifespan(app: FastAPI):
|
||||||
|
global transcription_engine
|
||||||
|
transcription_engine = TranscriptionEngine(
|
||||||
|
**vars(args),
|
||||||
|
)
|
||||||
|
yield
|
||||||
|
|
||||||
|
app = FastAPI(lifespan=lifespan)
|
||||||
|
app.add_middleware(
|
||||||
|
CORSMiddleware,
|
||||||
|
allow_origins=["*"],
|
||||||
|
allow_credentials=True,
|
||||||
|
allow_methods=["*"],
|
||||||
|
allow_headers=["*"],
|
||||||
|
)
|
||||||
|
web_dir = pathlib.Path(webpkg.__file__).parent
|
||||||
|
app.mount("/web", StaticFiles(directory=str(web_dir)), name="web")
|
||||||
|
|
||||||
|
@app.get("/")
|
||||||
|
async def get():
|
||||||
|
return HTMLResponse(get_web_interface_html())
|
||||||
|
|
||||||
|
|
||||||
|
async def handle_websocket_results(websocket, results_generator):
|
||||||
|
"""Consumes results from the audio processor and sends them via WebSocket."""
|
||||||
|
try:
|
||||||
|
async for response in results_generator:
|
||||||
|
await websocket.send_json(response)
|
||||||
|
# when the results_generator finishes it means all audio has been processed
|
||||||
|
logger.info("Results generator finished. Sending 'ready_to_stop' to client.")
|
||||||
|
await websocket.send_json({"type": "ready_to_stop"})
|
||||||
|
except WebSocketDisconnect:
|
||||||
|
logger.info("WebSocket disconnected while handling results (client likely closed connection).")
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error in WebSocket results handler: {e}")
|
||||||
|
|
||||||
|
|
||||||
|
@app.websocket("/asr")
|
||||||
|
async def websocket_endpoint(websocket: WebSocket):
|
||||||
|
global transcription_engine
|
||||||
|
audio_processor = AudioProcessor(
|
||||||
|
transcription_engine=transcription_engine,
|
||||||
|
)
|
||||||
|
await websocket.accept()
|
||||||
|
logger.info("WebSocket connection opened.")
|
||||||
|
|
||||||
|
results_generator = await audio_processor.create_tasks()
|
||||||
|
websocket_task = asyncio.create_task(handle_websocket_results(websocket, results_generator))
|
||||||
|
|
||||||
|
try:
|
||||||
|
while True:
|
||||||
|
message = await websocket.receive_bytes()
|
||||||
|
await audio_processor.process_audio(message)
|
||||||
|
except KeyError as e:
|
||||||
|
if 'bytes' in str(e):
|
||||||
|
logger.warning(f"Client has closed the connection.")
|
||||||
|
else:
|
||||||
|
logger.error(f"Unexpected KeyError in websocket_endpoint: {e}", exc_info=True)
|
||||||
|
except WebSocketDisconnect:
|
||||||
|
logger.info("WebSocket disconnected by client during message receiving loop.")
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Unexpected error in websocket_endpoint main loop: {e}", exc_info=True)
|
||||||
|
finally:
|
||||||
|
logger.info("Cleaning up WebSocket endpoint...")
|
||||||
|
if not websocket_task.done():
|
||||||
|
websocket_task.cancel()
|
||||||
|
try:
|
||||||
|
await websocket_task
|
||||||
|
except asyncio.CancelledError:
|
||||||
|
logger.info("WebSocket results handler task was cancelled.")
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"Exception while awaiting websocket_task completion: {e}")
|
||||||
|
|
||||||
|
await audio_processor.cleanup()
|
||||||
|
logger.info("WebSocket endpoint cleaned up successfully.")
|
||||||
|
|
||||||
|
def main():
|
||||||
|
"""Entry point for the CLI command."""
|
||||||
|
import uvicorn
|
||||||
|
|
||||||
|
uvicorn_kwargs = {
|
||||||
|
"app": "whisperlivekit.basic_server:app",
|
||||||
|
"host":args.host,
|
||||||
|
"port":args.port,
|
||||||
|
"reload": False,
|
||||||
|
"log_level": "info",
|
||||||
|
"lifespan": "on",
|
||||||
|
}
|
||||||
|
|
||||||
|
ssl_kwargs = {}
|
||||||
|
if args.ssl_certfile or args.ssl_keyfile:
|
||||||
|
if not (args.ssl_certfile and args.ssl_keyfile):
|
||||||
|
raise ValueError("Both --ssl-certfile and --ssl-keyfile must be specified together.")
|
||||||
|
ssl_kwargs = {
|
||||||
|
"ssl_certfile": args.ssl_certfile,
|
||||||
|
"ssl_keyfile": args.ssl_keyfile
|
||||||
|
}
|
||||||
|
|
||||||
|
if ssl_kwargs:
|
||||||
|
uvicorn_kwargs = {**uvicorn_kwargs, **ssl_kwargs}
|
||||||
|
|
||||||
|
uvicorn.run(**uvicorn_kwargs)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
155
whisperlivekit/core.py
Normal file
155
whisperlivekit/core.py
Normal file
@@ -0,0 +1,155 @@
|
|||||||
|
try:
|
||||||
|
from whisperlivekit.whisper_streaming_custom.whisper_online import backend_factory
|
||||||
|
from whisperlivekit.whisper_streaming_custom.online_asr import OnlineASRProcessor
|
||||||
|
except ImportError:
|
||||||
|
from .whisper_streaming_custom.whisper_online import backend_factory
|
||||||
|
from .whisper_streaming_custom.online_asr import OnlineASRProcessor
|
||||||
|
from whisperlivekit.warmup import warmup_asr, warmup_online
|
||||||
|
from argparse import Namespace
|
||||||
|
import sys
|
||||||
|
|
||||||
|
class TranscriptionEngine:
|
||||||
|
_instance = None
|
||||||
|
_initialized = False
|
||||||
|
|
||||||
|
def __new__(cls, *args, **kwargs):
|
||||||
|
if cls._instance is None:
|
||||||
|
cls._instance = super().__new__(cls)
|
||||||
|
return cls._instance
|
||||||
|
|
||||||
|
def __init__(self, **kwargs):
|
||||||
|
if TranscriptionEngine._initialized:
|
||||||
|
return
|
||||||
|
|
||||||
|
defaults = {
|
||||||
|
"host": "localhost",
|
||||||
|
"port": 8000,
|
||||||
|
"warmup_file": None,
|
||||||
|
"diarization": False,
|
||||||
|
"punctuation_split": False,
|
||||||
|
"min_chunk_size": 0.5,
|
||||||
|
"model": "tiny",
|
||||||
|
"model_cache_dir": None,
|
||||||
|
"model_dir": None,
|
||||||
|
"lan": "auto",
|
||||||
|
"task": "transcribe",
|
||||||
|
"backend": "faster-whisper",
|
||||||
|
"vac": True,
|
||||||
|
"vac_chunk_size": 0.04,
|
||||||
|
"log_level": "DEBUG",
|
||||||
|
"ssl_certfile": None,
|
||||||
|
"ssl_keyfile": None,
|
||||||
|
"transcription": True,
|
||||||
|
"vad": True,
|
||||||
|
# whisperstreaming params:
|
||||||
|
"buffer_trimming": "segment",
|
||||||
|
"confidence_validation": False,
|
||||||
|
"buffer_trimming_sec": 15,
|
||||||
|
# simulstreaming params:
|
||||||
|
"frame_threshold": 25,
|
||||||
|
"beams": 1,
|
||||||
|
"decoder_type": None,
|
||||||
|
"audio_max_len": 20.0,
|
||||||
|
"audio_min_len": 0.0,
|
||||||
|
"cif_ckpt_path": None,
|
||||||
|
"never_fire": False,
|
||||||
|
"init_prompt": None,
|
||||||
|
"static_init_prompt": None,
|
||||||
|
"max_context_tokens": None,
|
||||||
|
"model_path": './base.pt',
|
||||||
|
"diarization_backend": "diart",
|
||||||
|
# diart params:
|
||||||
|
"segmentation_model": "pyannote/segmentation-3.0",
|
||||||
|
"embedding_model": "pyannote/embedding",
|
||||||
|
}
|
||||||
|
|
||||||
|
config_dict = {**defaults, **kwargs}
|
||||||
|
|
||||||
|
if 'no_transcription' in kwargs:
|
||||||
|
config_dict['transcription'] = not kwargs['no_transcription']
|
||||||
|
if 'no_vad' in kwargs:
|
||||||
|
config_dict['vad'] = not kwargs['no_vad']
|
||||||
|
if 'no_vac' in kwargs:
|
||||||
|
config_dict['vac'] = not kwargs['no_vac']
|
||||||
|
|
||||||
|
config_dict.pop('no_transcription', None)
|
||||||
|
config_dict.pop('no_vad', None)
|
||||||
|
|
||||||
|
if 'language' in kwargs:
|
||||||
|
config_dict['lan'] = kwargs['language']
|
||||||
|
config_dict.pop('language', None)
|
||||||
|
|
||||||
|
self.args = Namespace(**config_dict)
|
||||||
|
|
||||||
|
self.asr = None
|
||||||
|
self.tokenizer = None
|
||||||
|
self.diarization = None
|
||||||
|
self.vac_model = None
|
||||||
|
|
||||||
|
if self.args.vac:
|
||||||
|
import torch
|
||||||
|
self.vac_model, _ = torch.hub.load(repo_or_dir="snakers4/silero-vad", model="silero_vad")
|
||||||
|
|
||||||
|
if self.args.transcription:
|
||||||
|
if self.args.backend == "simulstreaming":
|
||||||
|
from whisperlivekit.simul_whisper import SimulStreamingASR
|
||||||
|
self.tokenizer = None
|
||||||
|
simulstreaming_kwargs = {}
|
||||||
|
for attr in ['frame_threshold', 'beams', 'decoder_type', 'audio_max_len', 'audio_min_len',
|
||||||
|
'cif_ckpt_path', 'never_fire', 'init_prompt', 'static_init_prompt',
|
||||||
|
'max_context_tokens', 'model_path', 'warmup_file', 'preload_model_count']:
|
||||||
|
if hasattr(self.args, attr):
|
||||||
|
simulstreaming_kwargs[attr] = getattr(self.args, attr)
|
||||||
|
|
||||||
|
# Add segment_length from min_chunk_size
|
||||||
|
simulstreaming_kwargs['segment_length'] = getattr(self.args, 'min_chunk_size', 0.5)
|
||||||
|
simulstreaming_kwargs['task'] = self.args.task
|
||||||
|
|
||||||
|
size = self.args.model
|
||||||
|
self.asr = SimulStreamingASR(
|
||||||
|
modelsize=size,
|
||||||
|
lan=self.args.lan,
|
||||||
|
cache_dir=getattr(self.args, 'model_cache_dir', None),
|
||||||
|
model_dir=getattr(self.args, 'model_dir', None),
|
||||||
|
**simulstreaming_kwargs
|
||||||
|
)
|
||||||
|
|
||||||
|
else:
|
||||||
|
self.asr, self.tokenizer = backend_factory(self.args)
|
||||||
|
warmup_asr(self.asr, self.args.warmup_file) #for simulstreaming, warmup should be done in the online class not here
|
||||||
|
|
||||||
|
if self.args.diarization:
|
||||||
|
if self.args.diarization_backend == "diart":
|
||||||
|
from whisperlivekit.diarization.diart_backend import DiartDiarization
|
||||||
|
self.diarization = DiartDiarization(
|
||||||
|
block_duration=self.args.min_chunk_size,
|
||||||
|
segmentation_model_name=self.args.segmentation_model,
|
||||||
|
embedding_model_name=self.args.embedding_model
|
||||||
|
)
|
||||||
|
elif self.args.diarization_backend == "sortformer":
|
||||||
|
raise ValueError('Sortformer backend in developement')
|
||||||
|
else:
|
||||||
|
raise ValueError(f"Unknown diarization backend: {self.args.diarization_backend}")
|
||||||
|
|
||||||
|
TranscriptionEngine._initialized = True
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def online_factory(args, asr, tokenizer, logfile=sys.stderr):
|
||||||
|
if args.backend == "simulstreaming":
|
||||||
|
from whisperlivekit.simul_whisper import SimulStreamingOnlineProcessor
|
||||||
|
online = SimulStreamingOnlineProcessor(
|
||||||
|
asr,
|
||||||
|
logfile=logfile,
|
||||||
|
)
|
||||||
|
# warmup_online(online, args.warmup_file)
|
||||||
|
else:
|
||||||
|
online = OnlineASRProcessor(
|
||||||
|
asr,
|
||||||
|
tokenizer,
|
||||||
|
logfile=logfile,
|
||||||
|
buffer_trimming=(args.buffer_trimming, args.buffer_trimming_sec),
|
||||||
|
confidence_validation = args.confidence_validation
|
||||||
|
)
|
||||||
|
return online
|
||||||
|
|
||||||
0
whisperlivekit/diarization/__init__.py
Normal file
0
whisperlivekit/diarization/__init__.py
Normal file
315
whisperlivekit/diarization/diart_backend.py
Normal file
315
whisperlivekit/diarization/diart_backend.py
Normal file
@@ -0,0 +1,315 @@
|
|||||||
|
import asyncio
|
||||||
|
import re
|
||||||
|
import threading
|
||||||
|
import numpy as np
|
||||||
|
import logging
|
||||||
|
import time
|
||||||
|
from queue import SimpleQueue, Empty
|
||||||
|
|
||||||
|
from diart import SpeakerDiarization, SpeakerDiarizationConfig
|
||||||
|
from diart.inference import StreamingInference
|
||||||
|
from diart.sources import AudioSource
|
||||||
|
from whisperlivekit.timed_objects import SpeakerSegment
|
||||||
|
from diart.sources import MicrophoneAudioSource
|
||||||
|
from rx.core import Observer
|
||||||
|
from typing import Tuple, Any, List
|
||||||
|
from pyannote.core import Annotation
|
||||||
|
import diart.models as m
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
def extract_number(s: str) -> int:
|
||||||
|
m = re.search(r'\d+', s)
|
||||||
|
return int(m.group()) if m else None
|
||||||
|
|
||||||
|
class DiarizationObserver(Observer):
|
||||||
|
"""Observer that logs all data emitted by the diarization pipeline and stores speaker segments."""
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.speaker_segments = []
|
||||||
|
self.processed_time = 0
|
||||||
|
self.segment_lock = threading.Lock()
|
||||||
|
self.global_time_offset = 0.0
|
||||||
|
|
||||||
|
def on_next(self, value: Tuple[Annotation, Any]):
|
||||||
|
annotation, audio = value
|
||||||
|
|
||||||
|
logger.debug("\n--- New Diarization Result ---")
|
||||||
|
|
||||||
|
duration = audio.extent.end - audio.extent.start
|
||||||
|
logger.debug(f"Audio segment: {audio.extent.start:.2f}s - {audio.extent.end:.2f}s (duration: {duration:.2f}s)")
|
||||||
|
logger.debug(f"Audio shape: {audio.data.shape}")
|
||||||
|
|
||||||
|
with self.segment_lock:
|
||||||
|
if audio.extent.end > self.processed_time:
|
||||||
|
self.processed_time = audio.extent.end
|
||||||
|
if annotation and len(annotation._labels) > 0:
|
||||||
|
logger.debug("\nSpeaker segments:")
|
||||||
|
for speaker, label in annotation._labels.items():
|
||||||
|
for start, end in zip(label.segments_boundaries_[:-1], label.segments_boundaries_[1:]):
|
||||||
|
print(f" {speaker}: {start:.2f}s-{end:.2f}s")
|
||||||
|
self.speaker_segments.append(SpeakerSegment(
|
||||||
|
speaker=speaker,
|
||||||
|
start=start + self.global_time_offset,
|
||||||
|
end=end + self.global_time_offset
|
||||||
|
))
|
||||||
|
else:
|
||||||
|
logger.debug("\nNo speakers detected in this segment")
|
||||||
|
|
||||||
|
def get_segments(self) -> List[SpeakerSegment]:
|
||||||
|
"""Get a copy of the current speaker segments."""
|
||||||
|
with self.segment_lock:
|
||||||
|
return self.speaker_segments.copy()
|
||||||
|
|
||||||
|
def clear_old_segments(self, older_than: float = 30.0):
|
||||||
|
"""Clear segments older than the specified time."""
|
||||||
|
with self.segment_lock:
|
||||||
|
current_time = self.processed_time
|
||||||
|
self.speaker_segments = [
|
||||||
|
segment for segment in self.speaker_segments
|
||||||
|
if current_time - segment.end < older_than
|
||||||
|
]
|
||||||
|
|
||||||
|
def on_error(self, error):
|
||||||
|
"""Handle an error in the stream."""
|
||||||
|
logger.debug(f"Error in diarization stream: {error}")
|
||||||
|
|
||||||
|
def on_completed(self):
|
||||||
|
"""Handle the completion of the stream."""
|
||||||
|
logger.debug("Diarization stream completed")
|
||||||
|
|
||||||
|
|
||||||
|
class WebSocketAudioSource(AudioSource):
|
||||||
|
"""
|
||||||
|
Buffers incoming audio and releases it in fixed-size chunks at regular intervals.
|
||||||
|
"""
|
||||||
|
def __init__(self, uri: str = "websocket", sample_rate: int = 16000, block_duration: float = 0.5):
|
||||||
|
super().__init__(uri, sample_rate)
|
||||||
|
self.block_duration = block_duration
|
||||||
|
self.block_size = int(np.rint(block_duration * sample_rate))
|
||||||
|
self._queue = SimpleQueue()
|
||||||
|
self._buffer = np.array([], dtype=np.float32)
|
||||||
|
self._buffer_lock = threading.Lock()
|
||||||
|
self._closed = False
|
||||||
|
self._close_event = threading.Event()
|
||||||
|
self._processing_thread = None
|
||||||
|
self._last_chunk_time = time.time()
|
||||||
|
|
||||||
|
def read(self):
|
||||||
|
"""Start processing buffered audio and emit fixed-size chunks."""
|
||||||
|
self._processing_thread = threading.Thread(target=self._process_chunks)
|
||||||
|
self._processing_thread.daemon = True
|
||||||
|
self._processing_thread.start()
|
||||||
|
|
||||||
|
self._close_event.wait()
|
||||||
|
if self._processing_thread:
|
||||||
|
self._processing_thread.join(timeout=2.0)
|
||||||
|
|
||||||
|
def _process_chunks(self):
|
||||||
|
"""Process audio from queue and emit fixed-size chunks at regular intervals."""
|
||||||
|
while not self._closed:
|
||||||
|
try:
|
||||||
|
audio_chunk = self._queue.get(timeout=0.1)
|
||||||
|
|
||||||
|
with self._buffer_lock:
|
||||||
|
self._buffer = np.concatenate([self._buffer, audio_chunk])
|
||||||
|
|
||||||
|
while len(self._buffer) >= self.block_size:
|
||||||
|
chunk = self._buffer[:self.block_size]
|
||||||
|
self._buffer = self._buffer[self.block_size:]
|
||||||
|
|
||||||
|
current_time = time.time()
|
||||||
|
time_since_last = current_time - self._last_chunk_time
|
||||||
|
if time_since_last < self.block_duration:
|
||||||
|
time.sleep(self.block_duration - time_since_last)
|
||||||
|
|
||||||
|
chunk_reshaped = chunk.reshape(1, -1)
|
||||||
|
self.stream.on_next(chunk_reshaped)
|
||||||
|
self._last_chunk_time = time.time()
|
||||||
|
|
||||||
|
except Empty:
|
||||||
|
with self._buffer_lock:
|
||||||
|
if len(self._buffer) > 0 and time.time() - self._last_chunk_time > self.block_duration:
|
||||||
|
padded_chunk = np.zeros(self.block_size, dtype=np.float32)
|
||||||
|
padded_chunk[:len(self._buffer)] = self._buffer
|
||||||
|
self._buffer = np.array([], dtype=np.float32)
|
||||||
|
|
||||||
|
chunk_reshaped = padded_chunk.reshape(1, -1)
|
||||||
|
self.stream.on_next(chunk_reshaped)
|
||||||
|
self._last_chunk_time = time.time()
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error in audio processing thread: {e}")
|
||||||
|
self.stream.on_error(e)
|
||||||
|
break
|
||||||
|
|
||||||
|
with self._buffer_lock:
|
||||||
|
if len(self._buffer) > 0:
|
||||||
|
padded_chunk = np.zeros(self.block_size, dtype=np.float32)
|
||||||
|
padded_chunk[:len(self._buffer)] = self._buffer
|
||||||
|
chunk_reshaped = padded_chunk.reshape(1, -1)
|
||||||
|
self.stream.on_next(chunk_reshaped)
|
||||||
|
|
||||||
|
self.stream.on_completed()
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
if not self._closed:
|
||||||
|
self._closed = True
|
||||||
|
self._close_event.set()
|
||||||
|
|
||||||
|
def push_audio(self, chunk: np.ndarray):
|
||||||
|
"""Add audio chunk to the processing queue."""
|
||||||
|
if not self._closed:
|
||||||
|
if chunk.ndim > 1:
|
||||||
|
chunk = chunk.flatten()
|
||||||
|
self._queue.put(chunk)
|
||||||
|
logger.debug(f'Added chunk to queue with {len(chunk)} samples')
|
||||||
|
|
||||||
|
|
||||||
|
class DiartDiarization:
|
||||||
|
def __init__(self, sample_rate: int = 16000, config : SpeakerDiarizationConfig = None, use_microphone: bool = False, block_duration: float = 1.5, segmentation_model_name: str = "pyannote/segmentation-3.0", embedding_model_name: str = "pyannote/embedding"):
|
||||||
|
segmentation_model = m.SegmentationModel.from_pretrained(segmentation_model_name)
|
||||||
|
embedding_model = m.EmbeddingModel.from_pretrained(embedding_model_name)
|
||||||
|
|
||||||
|
if config is None:
|
||||||
|
config = SpeakerDiarizationConfig(
|
||||||
|
segmentation=segmentation_model,
|
||||||
|
embedding=embedding_model,
|
||||||
|
)
|
||||||
|
|
||||||
|
self.pipeline = SpeakerDiarization(config=config)
|
||||||
|
self.observer = DiarizationObserver()
|
||||||
|
self.lag_diart = None
|
||||||
|
|
||||||
|
if use_microphone:
|
||||||
|
self.source = MicrophoneAudioSource(block_duration=block_duration)
|
||||||
|
self.custom_source = None
|
||||||
|
else:
|
||||||
|
self.custom_source = WebSocketAudioSource(
|
||||||
|
uri="websocket_source",
|
||||||
|
sample_rate=sample_rate,
|
||||||
|
block_duration=block_duration
|
||||||
|
)
|
||||||
|
self.source = self.custom_source
|
||||||
|
|
||||||
|
self.inference = StreamingInference(
|
||||||
|
pipeline=self.pipeline,
|
||||||
|
source=self.source,
|
||||||
|
do_plot=False,
|
||||||
|
show_progress=False,
|
||||||
|
)
|
||||||
|
self.inference.attach_observers(self.observer)
|
||||||
|
asyncio.get_event_loop().run_in_executor(None, self.inference)
|
||||||
|
|
||||||
|
def insert_silence(self, silence_duration):
|
||||||
|
self.observer.global_time_offset += silence_duration
|
||||||
|
|
||||||
|
async def diarize(self, pcm_array: np.ndarray):
|
||||||
|
"""
|
||||||
|
Process audio data for diarization.
|
||||||
|
Only used when working with WebSocketAudioSource.
|
||||||
|
"""
|
||||||
|
if self.custom_source:
|
||||||
|
self.custom_source.push_audio(pcm_array)
|
||||||
|
# self.observer.clear_old_segments()
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
"""Close the audio source."""
|
||||||
|
if self.custom_source:
|
||||||
|
self.custom_source.close()
|
||||||
|
|
||||||
|
def assign_speakers_to_tokens(self, tokens: list, use_punctuation_split: bool = False) -> float:
|
||||||
|
"""
|
||||||
|
Assign speakers to tokens based on timing overlap with speaker segments.
|
||||||
|
Uses the segments collected by the observer.
|
||||||
|
|
||||||
|
If use_punctuation_split is True, uses punctuation marks to refine speaker boundaries.
|
||||||
|
"""
|
||||||
|
segments = self.observer.get_segments()
|
||||||
|
|
||||||
|
# Debug logging
|
||||||
|
logger.debug(f"assign_speakers_to_tokens called with {len(tokens)} tokens")
|
||||||
|
logger.debug(f"Available segments: {len(segments)}")
|
||||||
|
for i, seg in enumerate(segments[:5]): # Show first 5 segments
|
||||||
|
logger.debug(f" Segment {i}: {seg.speaker} [{seg.start:.2f}-{seg.end:.2f}]")
|
||||||
|
|
||||||
|
if not self.lag_diart and segments and tokens:
|
||||||
|
self.lag_diart = segments[0].start - tokens[0].start
|
||||||
|
|
||||||
|
if not use_punctuation_split:
|
||||||
|
for token in tokens:
|
||||||
|
for segment in segments:
|
||||||
|
if not (segment.end <= token.start + self.lag_diart or segment.start >= token.end + self.lag_diart):
|
||||||
|
token.speaker = extract_number(segment.speaker) + 1
|
||||||
|
else:
|
||||||
|
tokens = add_speaker_to_tokens(segments, tokens)
|
||||||
|
return tokens
|
||||||
|
|
||||||
|
def concatenate_speakers(segments):
|
||||||
|
segments_concatenated = [{"speaker": 1, "begin": 0.0, "end": 0.0}]
|
||||||
|
for segment in segments:
|
||||||
|
speaker = extract_number(segment.speaker) + 1
|
||||||
|
if segments_concatenated[-1]['speaker'] != speaker:
|
||||||
|
segments_concatenated.append({"speaker": speaker, "begin": segment.start, "end": segment.end})
|
||||||
|
else:
|
||||||
|
segments_concatenated[-1]['end'] = segment.end
|
||||||
|
# print("Segments concatenated:")
|
||||||
|
# for entry in segments_concatenated:
|
||||||
|
# print(f"Speaker {entry['speaker']}: {entry['begin']:.2f}s - {entry['end']:.2f}s")
|
||||||
|
return segments_concatenated
|
||||||
|
|
||||||
|
|
||||||
|
def add_speaker_to_tokens(segments, tokens):
|
||||||
|
"""
|
||||||
|
Assign speakers to tokens based on diarization segments, with punctuation-aware boundary adjustment.
|
||||||
|
"""
|
||||||
|
punctuation_marks = {'.', '!', '?'}
|
||||||
|
punctuation_tokens = [token for token in tokens if token.text.strip() in punctuation_marks]
|
||||||
|
segments_concatenated = concatenate_speakers(segments)
|
||||||
|
for ind, segment in enumerate(segments_concatenated):
|
||||||
|
for i, punctuation_token in enumerate(punctuation_tokens):
|
||||||
|
if punctuation_token.start > segment['end']:
|
||||||
|
after_length = punctuation_token.start - segment['end']
|
||||||
|
before_length = segment['end'] - punctuation_tokens[i - 1].end
|
||||||
|
if before_length > after_length:
|
||||||
|
segment['end'] = punctuation_token.start
|
||||||
|
if i < len(punctuation_tokens) - 1 and ind + 1 < len(segments_concatenated):
|
||||||
|
segments_concatenated[ind + 1]['begin'] = punctuation_token.start
|
||||||
|
else:
|
||||||
|
segment['end'] = punctuation_tokens[i - 1].end
|
||||||
|
if i < len(punctuation_tokens) - 1 and ind - 1 >= 0:
|
||||||
|
segments_concatenated[ind - 1]['begin'] = punctuation_tokens[i - 1].end
|
||||||
|
break
|
||||||
|
|
||||||
|
last_end = 0.0
|
||||||
|
for token in tokens:
|
||||||
|
start = max(last_end + 0.01, token.start)
|
||||||
|
token.start = start
|
||||||
|
token.end = max(start, token.end)
|
||||||
|
last_end = token.end
|
||||||
|
|
||||||
|
ind_last_speaker = 0
|
||||||
|
for segment in segments_concatenated:
|
||||||
|
for i, token in enumerate(tokens[ind_last_speaker:]):
|
||||||
|
if token.end <= segment['end']:
|
||||||
|
token.speaker = segment['speaker']
|
||||||
|
ind_last_speaker = i + 1
|
||||||
|
# print(
|
||||||
|
# f"Token '{token.text}' ('begin': {token.start:.2f}, 'end': {token.end:.2f}) "
|
||||||
|
# f"assigned to Speaker {segment['speaker']} ('segment': {segment['begin']:.2f}-{segment['end']:.2f})"
|
||||||
|
# )
|
||||||
|
elif token.start > segment['end']:
|
||||||
|
break
|
||||||
|
return tokens
|
||||||
|
|
||||||
|
|
||||||
|
def visualize_tokens(tokens):
|
||||||
|
conversation = [{"speaker": -1, "text": ""}]
|
||||||
|
for token in tokens:
|
||||||
|
speaker = conversation[-1]['speaker']
|
||||||
|
if token.speaker != speaker:
|
||||||
|
conversation.append({"speaker": token.speaker, "text": token.text})
|
||||||
|
else:
|
||||||
|
conversation[-1]['text'] += token.text
|
||||||
|
print("Conversation:")
|
||||||
|
for entry in conversation:
|
||||||
|
print(f"Speaker {entry['speaker']}: {entry['text']}")
|
||||||
145
whisperlivekit/diarization/sortformer_backend.py
Normal file
145
whisperlivekit/diarization/sortformer_backend.py
Normal file
@@ -0,0 +1,145 @@
|
|||||||
|
import numpy as np
|
||||||
|
import torch
|
||||||
|
import logging
|
||||||
|
from whisperlivekit.timed_objects import SpeakerSegment
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
try:
|
||||||
|
from nemo.collections.asr.models import SortformerEncLabelModel
|
||||||
|
except ImportError:
|
||||||
|
raise SystemExit("""Please use `pip install "git+https://github.com/NVIDIA/NeMo.git@main#egg=nemo_toolkit[asr]"` to use the Sortformer diarization""")
|
||||||
|
|
||||||
|
class SortformerDiarization:
|
||||||
|
def __init__(self, model_name="nvidia/diar_streaming_sortformer_4spk-v2"):
|
||||||
|
self.diar_model = SortformerEncLabelModel.from_pretrained(model_name)
|
||||||
|
self.diar_model.eval()
|
||||||
|
|
||||||
|
if torch.cuda.is_available():
|
||||||
|
self.diar_model.to(torch.device("cuda"))
|
||||||
|
|
||||||
|
# Streaming parameters for speed
|
||||||
|
self.diar_model.sortformer_modules.chunk_len = 12
|
||||||
|
self.diar_model.sortformer_modules.chunk_right_context = 1
|
||||||
|
self.diar_model.sortformer_modules.spkcache_len = 188
|
||||||
|
self.diar_model.sortformer_modules.fifo_len = 188
|
||||||
|
self.diar_model.sortformer_modules.spkcache_update_period = 144
|
||||||
|
self.diar_model.sortformer_modules.log = False
|
||||||
|
self.diar_model.sortformer_modules._check_streaming_parameters()
|
||||||
|
|
||||||
|
self.batch_size = 1
|
||||||
|
self.processed_signal_offset = torch.zeros((self.batch_size,), dtype=torch.long, device=self.diar_model.device)
|
||||||
|
|
||||||
|
self.audio_buffer = np.array([], dtype=np.float32)
|
||||||
|
self.sample_rate = 16000
|
||||||
|
self.speaker_segments = []
|
||||||
|
|
||||||
|
self.streaming_state = self.diar_model.sortformer_modules.init_streaming_state(
|
||||||
|
batch_size=self.batch_size,
|
||||||
|
async_streaming=True,
|
||||||
|
device=self.diar_model.device
|
||||||
|
)
|
||||||
|
self.total_preds = torch.zeros((self.batch_size, 0, self.diar_model.sortformer_modules.n_spk), device=self.diar_model.device)
|
||||||
|
|
||||||
|
|
||||||
|
def _prepare_audio_signal(self, signal):
|
||||||
|
audio_signal = torch.tensor(signal).unsqueeze(0).to(self.diar_model.device)
|
||||||
|
audio_signal_length = torch.tensor([audio_signal.shape[1]]).to(self.diar_model.device)
|
||||||
|
processed_signal, processed_signal_length = self.diar_model.preprocessor(input_signal=audio_signal, length=audio_signal_length)
|
||||||
|
return processed_signal, processed_signal_length
|
||||||
|
|
||||||
|
def _create_streaming_loader(self, processed_signal, processed_signal_length):
|
||||||
|
streaming_loader = self.diar_model.sortformer_modules.streaming_feat_loader(
|
||||||
|
feat_seq=processed_signal,
|
||||||
|
feat_seq_length=processed_signal_length,
|
||||||
|
feat_seq_offset=self.processed_signal_offset,
|
||||||
|
)
|
||||||
|
return streaming_loader
|
||||||
|
|
||||||
|
async def diarize(self, pcm_array: np.ndarray):
|
||||||
|
"""
|
||||||
|
Process an incoming audio chunk for diarization.
|
||||||
|
"""
|
||||||
|
self.audio_buffer = np.concatenate([self.audio_buffer, pcm_array])
|
||||||
|
|
||||||
|
# Process in fixed-size chunks (e.g., 1 second)
|
||||||
|
chunk_size = self.sample_rate # 1 second of audio
|
||||||
|
|
||||||
|
while len(self.audio_buffer) >= chunk_size:
|
||||||
|
chunk_to_process = self.audio_buffer[:chunk_size]
|
||||||
|
self.audio_buffer = self.audio_buffer[chunk_size:]
|
||||||
|
|
||||||
|
processed_signal, processed_signal_length = self._prepare_audio_signal(chunk_to_process)
|
||||||
|
|
||||||
|
current_offset_seconds = self.processed_signal_offset.item() * self.diar_model.preprocessor._cfg.window_stride
|
||||||
|
|
||||||
|
streaming_loader = self._create_streaming_loader(processed_signal, processed_signal_length)
|
||||||
|
|
||||||
|
frame_duration_s = self.diar_model.sortformer_modules.subsampling_factor * self.diar_model.preprocessor._cfg.window_stride
|
||||||
|
chunk_duration_seconds = self.diar_model.sortformer_modules.chunk_len * frame_duration_s
|
||||||
|
|
||||||
|
for i, chunk_feat_seq_t, feat_lengths, left_offset, right_offset in streaming_loader:
|
||||||
|
with torch.inference_mode():
|
||||||
|
self.streaming_state, self.total_preds = self.diar_model.forward_streaming_step(
|
||||||
|
processed_signal=chunk_feat_seq_t,
|
||||||
|
processed_signal_length=feat_lengths,
|
||||||
|
streaming_state=self.streaming_state,
|
||||||
|
total_preds=self.total_preds,
|
||||||
|
left_offset=left_offset,
|
||||||
|
right_offset=right_offset,
|
||||||
|
)
|
||||||
|
|
||||||
|
num_new_frames = feat_lengths[0].item()
|
||||||
|
|
||||||
|
# Get predictions for the current chunk from the end of total_preds
|
||||||
|
preds_np = self.total_preds[0, -num_new_frames:].cpu().numpy()
|
||||||
|
active_speakers = np.argmax(preds_np, axis=1)
|
||||||
|
|
||||||
|
for idx, spk in enumerate(active_speakers):
|
||||||
|
start_time = current_offset_seconds + (i * chunk_duration_seconds) + (idx * frame_duration_s)
|
||||||
|
end_time = start_time + frame_duration_s
|
||||||
|
|
||||||
|
if self.speaker_segments and self.speaker_segments[-1].speaker == spk + 1:
|
||||||
|
self.speaker_segments[-1].end = end_time
|
||||||
|
else:
|
||||||
|
self.speaker_segments.append(SpeakerSegment(
|
||||||
|
speaker=int(spk + 1),
|
||||||
|
start=start_time,
|
||||||
|
end=end_time
|
||||||
|
))
|
||||||
|
|
||||||
|
self.processed_signal_offset += processed_signal_length
|
||||||
|
|
||||||
|
|
||||||
|
def assign_speakers_to_tokens(self, tokens: list, **kwargs) -> list:
|
||||||
|
"""
|
||||||
|
Assign speakers to tokens based on timing overlap with speaker segments.
|
||||||
|
"""
|
||||||
|
for token in tokens:
|
||||||
|
for segment in self.speaker_segments:
|
||||||
|
if not (segment.end <= token.start or segment.start >= token.end):
|
||||||
|
token.speaker = segment.speaker
|
||||||
|
return tokens
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
"""
|
||||||
|
Cleanup resources.
|
||||||
|
"""
|
||||||
|
logger.info("Closing SortformerDiarization.")
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import librosa
|
||||||
|
an4_audio = 'new_audio_test.mp3'
|
||||||
|
signal, sr = librosa.load(an4_audio, sr=16000)
|
||||||
|
|
||||||
|
diarization_pipeline = SortformerDiarization()
|
||||||
|
|
||||||
|
# Simulate streaming
|
||||||
|
chunk_size = 16000 # 1 second
|
||||||
|
for i in range(0, len(signal), chunk_size):
|
||||||
|
chunk = signal[i:i+chunk_size]
|
||||||
|
import asyncio
|
||||||
|
asyncio.run(diarization_pipeline.diarize(chunk))
|
||||||
|
|
||||||
|
for segment in diarization_pipeline.speaker_segments:
|
||||||
|
print(f"Speaker {segment.speaker}: {segment.start:.2f}s - {segment.end:.2f}s")
|
||||||
257
whisperlivekit/diarization/sortformer_backend_2.py
Normal file
257
whisperlivekit/diarization/sortformer_backend_2.py
Normal file
@@ -0,0 +1,257 @@
|
|||||||
|
import numpy as np
|
||||||
|
import torch
|
||||||
|
import logging
|
||||||
|
import math
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
try:
|
||||||
|
from nemo.collections.asr.models import SortformerEncLabelModel
|
||||||
|
except ImportError:
|
||||||
|
raise SystemExit("""Please use `pip install "git+https://github.com/NVIDIA/NeMo.git@main#egg=nemo_toolkit[asr]"` to use the Sortformer diarization""")
|
||||||
|
|
||||||
|
|
||||||
|
diar_model = SortformerEncLabelModel.from_pretrained("nvidia/diar_streaming_sortformer_4spk-v2")
|
||||||
|
diar_model.eval()
|
||||||
|
|
||||||
|
if torch.cuda.is_available():
|
||||||
|
diar_model.to(torch.device("cuda"))
|
||||||
|
|
||||||
|
# Set the streaming parameters corresponding to 1.04s latency setup. This will affect the streaming feat loader.
|
||||||
|
# diar_model.sortformer_modules.chunk_len = 6
|
||||||
|
# diar_model.sortformer_modules.spkcache_len = 188
|
||||||
|
# diar_model.sortformer_modules.chunk_right_context = 7
|
||||||
|
# diar_model.sortformer_modules.fifo_len = 188
|
||||||
|
# diar_model.sortformer_modules.spkcache_update_period = 144
|
||||||
|
# diar_model.sortformer_modules.log = False
|
||||||
|
|
||||||
|
|
||||||
|
# here we change the settings for our goal: speed!
|
||||||
|
# we want batches of around 1 second. one frame is 0.08s, so 1s is 12.5 frames. we take 12.
|
||||||
|
diar_model.sortformer_modules.chunk_len = 12
|
||||||
|
|
||||||
|
# for more speed, we reduce the 'right context'. it's like looking less into the future.
|
||||||
|
diar_model.sortformer_modules.chunk_right_context = 1
|
||||||
|
|
||||||
|
# we keep the rest same for now
|
||||||
|
diar_model.sortformer_modules.spkcache_len = 188
|
||||||
|
diar_model.sortformer_modules.fifo_len = 188
|
||||||
|
diar_model.sortformer_modules.spkcache_update_period = 144
|
||||||
|
diar_model.sortformer_modules.log = False
|
||||||
|
diar_model.sortformer_modules._check_streaming_parameters()
|
||||||
|
|
||||||
|
batch_size = 1
|
||||||
|
processed_signal_offset = torch.zeros((batch_size,), dtype=torch.long, device=diar_model.device)
|
||||||
|
|
||||||
|
# from nemo.collections.asr.parts.preprocessing.features import FilterbankFeatures
|
||||||
|
# from nemo.collections.asr.modules.audio_preprocessing import get_features
|
||||||
|
from nemo.collections.asr.modules.audio_preprocessing import AudioToMelSpectrogramPreprocessor
|
||||||
|
|
||||||
|
|
||||||
|
def prepare_audio_signal(signal):
|
||||||
|
audio_signal = torch.tensor(signal).unsqueeze(0).to(diar_model.device)
|
||||||
|
audio_signal_length = torch.tensor([audio_signal.shape[1]]).to(diar_model.device)
|
||||||
|
processed_signal, processed_signal_length = AudioToMelSpectrogramPreprocessor(
|
||||||
|
window_size= 0.025,
|
||||||
|
normalize="NA",
|
||||||
|
n_fft=512,
|
||||||
|
features=128).get_features(audio_signal, audio_signal_length)
|
||||||
|
return processed_signal, processed_signal_length
|
||||||
|
|
||||||
|
|
||||||
|
def streaming_feat_loader(
|
||||||
|
feat_seq, feat_seq_length, feat_seq_offset
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
Load a chunk of feature sequence for streaming inference.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
feat_seq (torch.Tensor): Tensor containing feature sequence
|
||||||
|
Shape: (batch_size, feat_dim, feat frame count)
|
||||||
|
feat_seq_length (torch.Tensor): Tensor containing feature sequence lengths
|
||||||
|
Shape: (batch_size,)
|
||||||
|
feat_seq_offset (torch.Tensor): Tensor containing feature sequence offsets
|
||||||
|
Shape: (batch_size,)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
chunk_idx (int): Index of the current chunk
|
||||||
|
chunk_feat_seq (torch.Tensor): Tensor containing the chunk of feature sequence
|
||||||
|
Shape: (batch_size, diar frame count, feat_dim)
|
||||||
|
feat_lengths (torch.Tensor): Tensor containing lengths of the chunk of feature sequence
|
||||||
|
Shape: (batch_size,)
|
||||||
|
"""
|
||||||
|
feat_len = feat_seq.shape[2]
|
||||||
|
num_chunks = math.ceil(feat_len / (diar_model.sortformer_modules.chunk_len * diar_model.sortformer_modules.subsampling_factor))
|
||||||
|
if False:
|
||||||
|
logging.info(
|
||||||
|
f"feat_len={feat_len}, num_chunks={num_chunks}, "
|
||||||
|
f"feat_seq_length={feat_seq_length}, feat_seq_offset={feat_seq_offset}"
|
||||||
|
)
|
||||||
|
|
||||||
|
stt_feat, end_feat, chunk_idx = 0, 0, 0
|
||||||
|
while end_feat < feat_len:
|
||||||
|
left_offset = min(diar_model.sortformer_modules.chunk_left_context * diar_model.sortformer_modules.subsampling_factor, stt_feat)
|
||||||
|
end_feat = min(stt_feat + diar_model.sortformer_modules.chunk_len * diar_model.sortformer_modules.subsampling_factor, feat_len)
|
||||||
|
right_offset = min(diar_model.sortformer_modules.chunk_right_context * diar_model.sortformer_modules.subsampling_factor, feat_len - end_feat)
|
||||||
|
chunk_feat_seq = feat_seq[:, :, stt_feat - left_offset : end_feat + right_offset]
|
||||||
|
feat_lengths = (feat_seq_length + feat_seq_offset - stt_feat + left_offset).clamp(
|
||||||
|
0, chunk_feat_seq.shape[2]
|
||||||
|
)
|
||||||
|
feat_lengths = feat_lengths * (feat_seq_offset < end_feat)
|
||||||
|
stt_feat = end_feat
|
||||||
|
chunk_feat_seq_t = torch.transpose(chunk_feat_seq, 1, 2)
|
||||||
|
if False:
|
||||||
|
logging.info(
|
||||||
|
f"chunk_idx: {chunk_idx}, "
|
||||||
|
f"chunk_feat_seq_t shape: {chunk_feat_seq_t.shape}, "
|
||||||
|
f"chunk_feat_lengths: {feat_lengths}"
|
||||||
|
)
|
||||||
|
yield chunk_idx, chunk_feat_seq_t, feat_lengths, left_offset, right_offset
|
||||||
|
chunk_idx += 1
|
||||||
|
|
||||||
|
|
||||||
|
class StreamingSortformerState:
|
||||||
|
"""
|
||||||
|
This class creates a class instance that will be used to store the state of the
|
||||||
|
streaming Sortformer model.
|
||||||
|
|
||||||
|
Attributes:
|
||||||
|
spkcache (torch.Tensor): Speaker cache to store embeddings from start
|
||||||
|
spkcache_lengths (torch.Tensor): Lengths of the speaker cache
|
||||||
|
spkcache_preds (torch.Tensor): The speaker predictions for the speaker cache parts
|
||||||
|
fifo (torch.Tensor): FIFO queue to save the embedding from the latest chunks
|
||||||
|
fifo_lengths (torch.Tensor): Lengths of the FIFO queue
|
||||||
|
fifo_preds (torch.Tensor): The speaker predictions for the FIFO queue parts
|
||||||
|
spk_perm (torch.Tensor): Speaker permutation information for the speaker cache
|
||||||
|
mean_sil_emb (torch.Tensor): Mean silence embedding
|
||||||
|
n_sil_frames (torch.Tensor): Number of silence frames
|
||||||
|
"""
|
||||||
|
|
||||||
|
spkcache = None # Speaker cache to store embeddings from start
|
||||||
|
spkcache_lengths = None #
|
||||||
|
spkcache_preds = None # speaker cache predictions
|
||||||
|
fifo = None # to save the embedding from the latest chunks
|
||||||
|
fifo_lengths = None
|
||||||
|
fifo_preds = None
|
||||||
|
spk_perm = None
|
||||||
|
mean_sil_emb = None
|
||||||
|
n_sil_frames = None
|
||||||
|
|
||||||
|
|
||||||
|
def init_streaming_state(self, batch_size: int = 1, async_streaming: bool = False, device: torch.device = None):
|
||||||
|
"""
|
||||||
|
Initializes StreamingSortformerState with empty tensors or zero-valued tensors.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
batch_size (int): Batch size for tensors in streaming state
|
||||||
|
async_streaming (bool): True for asynchronous update, False for synchronous update
|
||||||
|
device (torch.device): Device for tensors in streaming state
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
streaming_state (SortformerStreamingState): initialized streaming state
|
||||||
|
"""
|
||||||
|
streaming_state = StreamingSortformerState()
|
||||||
|
if async_streaming:
|
||||||
|
streaming_state.spkcache = torch.zeros((batch_size, self.spkcache_len, self.fc_d_model), device=device)
|
||||||
|
streaming_state.spkcache_preds = torch.zeros((batch_size, self.spkcache_len, self.n_spk), device=device)
|
||||||
|
streaming_state.spkcache_lengths = torch.zeros((batch_size,), dtype=torch.long, device=device)
|
||||||
|
streaming_state.fifo = torch.zeros((batch_size, self.fifo_len, self.fc_d_model), device=device)
|
||||||
|
streaming_state.fifo_lengths = torch.zeros((batch_size,), dtype=torch.long, device=device)
|
||||||
|
else:
|
||||||
|
streaming_state.spkcache = torch.zeros((batch_size, 0, self.fc_d_model), device=device)
|
||||||
|
streaming_state.fifo = torch.zeros((batch_size, 0, self.fc_d_model), device=device)
|
||||||
|
streaming_state.mean_sil_emb = torch.zeros((batch_size, self.fc_d_model), device=device)
|
||||||
|
streaming_state.n_sil_frames = torch.zeros((batch_size,), dtype=torch.long, device=device)
|
||||||
|
return streaming_state
|
||||||
|
|
||||||
|
def process_diarization(signal, chunks):
|
||||||
|
|
||||||
|
audio_signal = torch.tensor(signal).unsqueeze(0).to(diar_model.device)
|
||||||
|
audio_signal_length = torch.tensor([audio_signal.shape[1]]).to(diar_model.device)
|
||||||
|
processed_signal, processed_signal_length = AudioToMelSpectrogramPreprocessor(
|
||||||
|
window_size= 0.025,
|
||||||
|
normalize="NA",
|
||||||
|
n_fft=512,
|
||||||
|
features=128).get_features(audio_signal, audio_signal_length)
|
||||||
|
|
||||||
|
|
||||||
|
streaming_loader = streaming_feat_loader(processed_signal, processed_signal_length, processed_signal_offset)
|
||||||
|
|
||||||
|
|
||||||
|
streaming_state = init_streaming_state(diar_model.sortformer_modules,
|
||||||
|
batch_size = batch_size,
|
||||||
|
async_streaming = True,
|
||||||
|
device = diar_model.device
|
||||||
|
)
|
||||||
|
total_preds = torch.zeros((batch_size, 0, diar_model.sortformer_modules.n_spk), device=diar_model.device)
|
||||||
|
|
||||||
|
|
||||||
|
chunk_duration_seconds = diar_model.sortformer_modules.chunk_len * diar_model.sortformer_modules.subsampling_factor * diar_model.preprocessor._cfg.window_stride
|
||||||
|
print(f"Chunk duration: {chunk_duration_seconds} seconds")
|
||||||
|
|
||||||
|
l_speakers = [
|
||||||
|
{'start_time': 0,
|
||||||
|
'end_time': 0,
|
||||||
|
'speaker': 0
|
||||||
|
}
|
||||||
|
]
|
||||||
|
len_prediction = None
|
||||||
|
left_offset = 0
|
||||||
|
right_offset = 8
|
||||||
|
for i, chunk_feat_seq_t, _, _, _ in streaming_loader:
|
||||||
|
with torch.inference_mode():
|
||||||
|
streaming_state, total_preds = diar_model.forward_streaming_step(
|
||||||
|
processed_signal=chunk_feat_seq_t,
|
||||||
|
processed_signal_length=torch.tensor([chunk_feat_seq_t.shape[1]]),
|
||||||
|
streaming_state=streaming_state,
|
||||||
|
total_preds=total_preds,
|
||||||
|
left_offset=left_offset,
|
||||||
|
right_offset=right_offset,
|
||||||
|
)
|
||||||
|
left_offset = 8
|
||||||
|
preds_np = total_preds[0].cpu().numpy()
|
||||||
|
active_speakers = np.argmax(preds_np, axis=1)
|
||||||
|
if len_prediction is None:
|
||||||
|
len_prediction = len(active_speakers) # we want to get the len of 1 prediction
|
||||||
|
frame_duration = chunk_duration_seconds / len_prediction
|
||||||
|
active_speakers = active_speakers[-len_prediction:]
|
||||||
|
print(chunk_feat_seq_t.shape, total_preds.shape)
|
||||||
|
for idx, spk in enumerate(active_speakers):
|
||||||
|
if spk != l_speakers[-1]['speaker']:
|
||||||
|
l_speakers.append(
|
||||||
|
{'start_time': i * chunk_duration_seconds + idx * frame_duration,
|
||||||
|
'end_time': i * chunk_duration_seconds + (idx + 1) * frame_duration,
|
||||||
|
'speaker': spk
|
||||||
|
})
|
||||||
|
else:
|
||||||
|
l_speakers[-1]['end_time'] = i * chunk_duration_seconds + (idx + 1) * frame_duration
|
||||||
|
|
||||||
|
print(l_speakers)
|
||||||
|
"""
|
||||||
|
Should print
|
||||||
|
[{'start_time': 0, 'end_time': 8.72, 'speaker': 0},
|
||||||
|
{'start_time': 8.72, 'end_time': 18.88, 'speaker': 1},
|
||||||
|
{'start_time': 18.88, 'end_time': 24.96, 'speaker': 2},
|
||||||
|
{'start_time': 24.96, 'end_time': 31.68, 'speaker': 0}]
|
||||||
|
"""
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import librosa
|
||||||
|
an4_audio = 'new_audio_test.mp3'
|
||||||
|
signal, sr = librosa.load(an4_audio,sr=16000)
|
||||||
|
|
||||||
|
"""
|
||||||
|
ground truth:
|
||||||
|
speaker 0 : 0:00 - 0:09
|
||||||
|
speaker 1 : 0:09 - 0:19
|
||||||
|
speaker 2 : 0:19 - 0:25
|
||||||
|
speaker 0 : 0:25 - end
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Simulate streaming
|
||||||
|
chunk_size = 16000 # 1 second
|
||||||
|
chunks = []
|
||||||
|
for i in range(0, len(signal), chunk_size):
|
||||||
|
chunk = signal[i:i+chunk_size]
|
||||||
|
chunks.append(chunk)
|
||||||
|
|
||||||
|
process_diarization(signal, chunks)
|
||||||
193
whisperlivekit/ffmpeg_manager.py
Normal file
193
whisperlivekit/ffmpeg_manager.py
Normal file
@@ -0,0 +1,193 @@
|
|||||||
|
import asyncio
|
||||||
|
import logging
|
||||||
|
from enum import Enum
|
||||||
|
from typing import Optional, Callable
|
||||||
|
import contextlib
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
logging.basicConfig(level=logging.INFO)
|
||||||
|
|
||||||
|
ERROR_INSTALL_INSTRUCTIONS = """
|
||||||
|
FFmpeg is not installed or not found in your system's PATH.
|
||||||
|
Please install FFmpeg to enable audio processing.
|
||||||
|
|
||||||
|
Installation instructions:
|
||||||
|
|
||||||
|
# Ubuntu/Debian:
|
||||||
|
sudo apt update && sudo apt install ffmpeg
|
||||||
|
|
||||||
|
# macOS (using Homebrew):
|
||||||
|
brew install ffmpeg
|
||||||
|
|
||||||
|
# Windows:
|
||||||
|
# 1. Download the latest static build from https://ffmpeg.org/download.html
|
||||||
|
# 2. Extract the archive (e.g., to C:\\FFmpeg).
|
||||||
|
# 3. Add the 'bin' directory (e.g., C:\\FFmpeg\\bin) to your system's PATH environment variable.
|
||||||
|
|
||||||
|
After installation, please restart the application.
|
||||||
|
"""
|
||||||
|
|
||||||
|
class FFmpegState(Enum):
|
||||||
|
STOPPED = "stopped"
|
||||||
|
STARTING = "starting"
|
||||||
|
RUNNING = "running"
|
||||||
|
RESTARTING = "restarting"
|
||||||
|
FAILED = "failed"
|
||||||
|
|
||||||
|
class FFmpegManager:
|
||||||
|
def __init__(self, sample_rate: int = 16000, channels: int = 1):
|
||||||
|
self.sample_rate = sample_rate
|
||||||
|
self.channels = channels
|
||||||
|
|
||||||
|
self.process: Optional[asyncio.subprocess.Process] = None
|
||||||
|
self._stderr_task: Optional[asyncio.Task] = None
|
||||||
|
|
||||||
|
self.on_error_callback: Optional[Callable[[str], None]] = None
|
||||||
|
|
||||||
|
self.state = FFmpegState.STOPPED
|
||||||
|
self._state_lock = asyncio.Lock()
|
||||||
|
|
||||||
|
async def start(self) -> bool:
|
||||||
|
async with self._state_lock:
|
||||||
|
if self.state != FFmpegState.STOPPED:
|
||||||
|
logger.warning(f"FFmpeg already running in state: {self.state}")
|
||||||
|
return False
|
||||||
|
self.state = FFmpegState.STARTING
|
||||||
|
|
||||||
|
try:
|
||||||
|
cmd = [
|
||||||
|
"ffmpeg",
|
||||||
|
"-hide_banner",
|
||||||
|
"-loglevel", "error",
|
||||||
|
"-i", "pipe:0",
|
||||||
|
"-f", "s16le",
|
||||||
|
"-acodec", "pcm_s16le",
|
||||||
|
"-ac", str(self.channels),
|
||||||
|
"-ar", str(self.sample_rate),
|
||||||
|
"pipe:1"
|
||||||
|
]
|
||||||
|
|
||||||
|
self.process = await asyncio.create_subprocess_exec(
|
||||||
|
*cmd,
|
||||||
|
stdin=asyncio.subprocess.PIPE,
|
||||||
|
stdout=asyncio.subprocess.PIPE,
|
||||||
|
stderr=asyncio.subprocess.PIPE
|
||||||
|
)
|
||||||
|
|
||||||
|
self._stderr_task = asyncio.create_task(self._drain_stderr())
|
||||||
|
|
||||||
|
async with self._state_lock:
|
||||||
|
self.state = FFmpegState.RUNNING
|
||||||
|
|
||||||
|
logger.info("FFmpeg started.")
|
||||||
|
return True
|
||||||
|
|
||||||
|
except FileNotFoundError:
|
||||||
|
logger.error(ERROR_INSTALL_INSTRUCTIONS)
|
||||||
|
async with self._state_lock:
|
||||||
|
self.state = FFmpegState.FAILED
|
||||||
|
if self.on_error_callback:
|
||||||
|
await self.on_error_callback("ffmpeg_not_found")
|
||||||
|
return False
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error starting FFmpeg: {e}")
|
||||||
|
async with self._state_lock:
|
||||||
|
self.state = FFmpegState.FAILED
|
||||||
|
if self.on_error_callback:
|
||||||
|
await self.on_error_callback("start_failed")
|
||||||
|
return False
|
||||||
|
|
||||||
|
async def stop(self):
|
||||||
|
async with self._state_lock:
|
||||||
|
if self.state == FFmpegState.STOPPED:
|
||||||
|
return
|
||||||
|
self.state = FFmpegState.STOPPED
|
||||||
|
|
||||||
|
if self.process:
|
||||||
|
if self.process.stdin and not self.process.stdin.is_closing():
|
||||||
|
self.process.stdin.close()
|
||||||
|
await self.process.stdin.wait_closed()
|
||||||
|
await self.process.wait()
|
||||||
|
self.process = None
|
||||||
|
|
||||||
|
if self._stderr_task:
|
||||||
|
self._stderr_task.cancel()
|
||||||
|
with contextlib.suppress(asyncio.CancelledError):
|
||||||
|
await self._stderr_task
|
||||||
|
|
||||||
|
logger.info("FFmpeg stopped.")
|
||||||
|
|
||||||
|
async def write_data(self, data: bytes) -> bool:
|
||||||
|
async with self._state_lock:
|
||||||
|
if self.state != FFmpegState.RUNNING:
|
||||||
|
logger.warning(f"Cannot write, FFmpeg state: {self.state}")
|
||||||
|
return False
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.process.stdin.write(data)
|
||||||
|
await self.process.stdin.drain()
|
||||||
|
return True
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error writing to FFmpeg: {e}")
|
||||||
|
if self.on_error_callback:
|
||||||
|
await self.on_error_callback("write_error")
|
||||||
|
return False
|
||||||
|
|
||||||
|
async def read_data(self, size: int) -> Optional[bytes]:
|
||||||
|
async with self._state_lock:
|
||||||
|
if self.state != FFmpegState.RUNNING:
|
||||||
|
logger.warning(f"Cannot read, FFmpeg state: {self.state}")
|
||||||
|
return None
|
||||||
|
|
||||||
|
try:
|
||||||
|
data = await asyncio.wait_for(
|
||||||
|
self.process.stdout.read(size),
|
||||||
|
timeout=20.0
|
||||||
|
)
|
||||||
|
return data
|
||||||
|
except asyncio.TimeoutError:
|
||||||
|
logger.warning("FFmpeg read timeout.")
|
||||||
|
return None
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error reading from FFmpeg: {e}")
|
||||||
|
if self.on_error_callback:
|
||||||
|
await self.on_error_callback("read_error")
|
||||||
|
return None
|
||||||
|
|
||||||
|
async def get_state(self) -> FFmpegState:
|
||||||
|
async with self._state_lock:
|
||||||
|
return self.state
|
||||||
|
|
||||||
|
async def restart(self) -> bool:
|
||||||
|
async with self._state_lock:
|
||||||
|
if self.state == FFmpegState.RESTARTING:
|
||||||
|
logger.warning("Restart already in progress.")
|
||||||
|
return False
|
||||||
|
self.state = FFmpegState.RESTARTING
|
||||||
|
|
||||||
|
logger.info("Restarting FFmpeg...")
|
||||||
|
|
||||||
|
try:
|
||||||
|
await self.stop()
|
||||||
|
await asyncio.sleep(1) # short delay before restarting
|
||||||
|
return await self.start()
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error during FFmpeg restart: {e}")
|
||||||
|
async with self._state_lock:
|
||||||
|
self.state = FFmpegState.FAILED
|
||||||
|
if self.on_error_callback:
|
||||||
|
await self.on_error_callback("restart_failed")
|
||||||
|
return False
|
||||||
|
|
||||||
|
async def _drain_stderr(self):
|
||||||
|
try:
|
||||||
|
while True:
|
||||||
|
line = await self.process.stderr.readline()
|
||||||
|
if not line:
|
||||||
|
break
|
||||||
|
logger.debug(f"FFmpeg stderr: {line.decode(errors='ignore').strip()}")
|
||||||
|
except asyncio.CancelledError:
|
||||||
|
logger.info("FFmpeg stderr drain task cancelled.")
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error draining FFmpeg stderr: {e}")
|
||||||
269
whisperlivekit/parse_args.py
Normal file
269
whisperlivekit/parse_args.py
Normal file
@@ -0,0 +1,269 @@
|
|||||||
|
|
||||||
|
from argparse import ArgumentParser
|
||||||
|
|
||||||
|
def parse_args():
|
||||||
|
parser = ArgumentParser(description="Whisper FastAPI Online Server")
|
||||||
|
parser.add_argument(
|
||||||
|
"--host",
|
||||||
|
type=str,
|
||||||
|
default="localhost",
|
||||||
|
help="The host address to bind the server to.",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--port", type=int, default=8000, help="The port number to bind the server to."
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--warmup-file",
|
||||||
|
type=str,
|
||||||
|
default=None,
|
||||||
|
dest="warmup_file",
|
||||||
|
help="""
|
||||||
|
The path to a speech audio wav file to warm up Whisper so that the very first chunk processing is fast.
|
||||||
|
If not set, uses https://github.com/ggerganov/whisper.cpp/raw/master/samples/jfk.wav.
|
||||||
|
If False, no warmup is performed.
|
||||||
|
""",
|
||||||
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
"--confidence-validation",
|
||||||
|
action="store_true",
|
||||||
|
help="Accelerates validation of tokens using confidence scores. Transcription will be faster but punctuation might be less accurate.",
|
||||||
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
"--diarization",
|
||||||
|
action="store_true",
|
||||||
|
default=False,
|
||||||
|
help="Enable speaker diarization.",
|
||||||
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
"--punctuation-split",
|
||||||
|
action="store_true",
|
||||||
|
default=False,
|
||||||
|
help="Use punctuation marks from transcription to improve speaker boundary detection. Requires both transcription and diarization to be enabled.",
|
||||||
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
"--segmentation-model",
|
||||||
|
type=str,
|
||||||
|
default="pyannote/segmentation-3.0",
|
||||||
|
help="Hugging Face model ID for pyannote.audio segmentation model.",
|
||||||
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
"--embedding-model",
|
||||||
|
type=str,
|
||||||
|
default="pyannote/embedding",
|
||||||
|
help="Hugging Face model ID for pyannote.audio embedding model.",
|
||||||
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
"--diarization-backend",
|
||||||
|
type=str,
|
||||||
|
default="diart",
|
||||||
|
choices=["sortformer", "diart"],
|
||||||
|
help="The diarization backend to use.",
|
||||||
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
"--no-transcription",
|
||||||
|
action="store_true",
|
||||||
|
help="Disable transcription to only see live diarization results.",
|
||||||
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
"--min-chunk-size",
|
||||||
|
type=float,
|
||||||
|
default=0.5,
|
||||||
|
help="Minimum audio chunk size in seconds. It waits up to this time to do processing. If the processing takes shorter time, it waits, otherwise it processes the whole segment that was received by this time.",
|
||||||
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
"--model",
|
||||||
|
type=str,
|
||||||
|
default="small",
|
||||||
|
help="Name size of the Whisper model to use (default: tiny). Suggested values: tiny.en,tiny,base.en,base,small.en,small,medium.en,medium,large-v1,large-v2,large-v3,large,large-v3-turbo. The model is automatically downloaded from the model hub if not present in model cache dir.",
|
||||||
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
"--model_cache_dir",
|
||||||
|
type=str,
|
||||||
|
default=None,
|
||||||
|
help="Overriding the default model cache dir where models downloaded from the hub are saved",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--model_dir",
|
||||||
|
type=str,
|
||||||
|
default=None,
|
||||||
|
help="Dir where Whisper model.bin and other files are saved. This option overrides --model and --model_cache_dir parameter.",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--lan",
|
||||||
|
"--language",
|
||||||
|
type=str,
|
||||||
|
default="auto",
|
||||||
|
help="Source language code, e.g. en,de,cs, or 'auto' for language detection.",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--task",
|
||||||
|
type=str,
|
||||||
|
default="transcribe",
|
||||||
|
choices=["transcribe", "translate"],
|
||||||
|
help="Transcribe or translate.",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--backend",
|
||||||
|
type=str,
|
||||||
|
default="simulstreaming",
|
||||||
|
choices=["faster-whisper", "whisper_timestamped", "mlx-whisper", "openai-api", "simulstreaming"],
|
||||||
|
help="Load only this backend for Whisper processing.",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--no-vac",
|
||||||
|
action="store_true",
|
||||||
|
default=False,
|
||||||
|
help="Disable VAC = voice activity controller.",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--vac-chunk-size", type=float, default=0.04, help="VAC sample size in seconds."
|
||||||
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
"--no-vad",
|
||||||
|
action="store_true",
|
||||||
|
help="Disable VAD (voice activity detection).",
|
||||||
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
"--buffer_trimming",
|
||||||
|
type=str,
|
||||||
|
default="segment",
|
||||||
|
choices=["sentence", "segment"],
|
||||||
|
help='Buffer trimming strategy -- trim completed sentences marked with punctuation mark and detected by sentence segmenter, or the completed segments returned by Whisper. Sentence segmenter must be installed for "sentence" option.',
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--buffer_trimming_sec",
|
||||||
|
type=float,
|
||||||
|
default=15,
|
||||||
|
help="Buffer trimming length threshold in seconds. If buffer length is longer, trimming sentence/segment is triggered.",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"-l",
|
||||||
|
"--log-level",
|
||||||
|
dest="log_level",
|
||||||
|
choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
|
||||||
|
help="Set the log level",
|
||||||
|
default="DEBUG",
|
||||||
|
)
|
||||||
|
parser.add_argument("--ssl-certfile", type=str, help="Path to the SSL certificate file.", default=None)
|
||||||
|
parser.add_argument("--ssl-keyfile", type=str, help="Path to the SSL private key file.", default=None)
|
||||||
|
|
||||||
|
# SimulStreaming-specific arguments
|
||||||
|
simulstreaming_group = parser.add_argument_group('SimulStreaming arguments (only used with --backend simulstreaming)')
|
||||||
|
|
||||||
|
simulstreaming_group.add_argument(
|
||||||
|
"--frame-threshold",
|
||||||
|
type=int,
|
||||||
|
default=25,
|
||||||
|
dest="frame_threshold",
|
||||||
|
help="Threshold for the attention-guided decoding. The AlignAtt policy will decode only until this number of frames from the end of audio. In frames: one frame is 0.02 seconds for large-v3 model.",
|
||||||
|
)
|
||||||
|
|
||||||
|
simulstreaming_group.add_argument(
|
||||||
|
"--beams",
|
||||||
|
"-b",
|
||||||
|
type=int,
|
||||||
|
default=1,
|
||||||
|
help="Number of beams for beam search decoding. If 1, GreedyDecoder is used.",
|
||||||
|
)
|
||||||
|
|
||||||
|
simulstreaming_group.add_argument(
|
||||||
|
"--decoder",
|
||||||
|
type=str,
|
||||||
|
default=None,
|
||||||
|
dest="decoder_type",
|
||||||
|
choices=["beam", "greedy"],
|
||||||
|
help="Override automatic selection of beam or greedy decoder. If beams > 1 and greedy: invalid.",
|
||||||
|
)
|
||||||
|
|
||||||
|
simulstreaming_group.add_argument(
|
||||||
|
"--audio-max-len",
|
||||||
|
type=float,
|
||||||
|
default=30.0,
|
||||||
|
dest="audio_max_len",
|
||||||
|
help="Max length of the audio buffer, in seconds.",
|
||||||
|
)
|
||||||
|
|
||||||
|
simulstreaming_group.add_argument(
|
||||||
|
"--audio-min-len",
|
||||||
|
type=float,
|
||||||
|
default=0.0,
|
||||||
|
dest="audio_min_len",
|
||||||
|
help="Skip processing if the audio buffer is shorter than this length, in seconds. Useful when the --min-chunk-size is small.",
|
||||||
|
)
|
||||||
|
|
||||||
|
simulstreaming_group.add_argument(
|
||||||
|
"--cif-ckpt-path",
|
||||||
|
type=str,
|
||||||
|
default=None,
|
||||||
|
dest="cif_ckpt_path",
|
||||||
|
help="The file path to the Simul-Whisper's CIF model checkpoint that detects whether there is end of word at the end of the chunk. If not, the last decoded space-separated word is truncated because it is often wrong -- transcribing a word in the middle. The CIF model adapted for the Whisper model version should be used. Find the models in https://github.com/backspacetg/simul_whisper/tree/main/cif_models . Note that there is no model for large-v3.",
|
||||||
|
)
|
||||||
|
|
||||||
|
simulstreaming_group.add_argument(
|
||||||
|
"--never-fire",
|
||||||
|
action="store_true",
|
||||||
|
default=False,
|
||||||
|
dest="never_fire",
|
||||||
|
help="Override the CIF model. If True, the last word is NEVER truncated, no matter what the CIF model detects. If False: if CIF model path is set, the last word is SOMETIMES truncated, depending on the CIF detection. Otherwise, if the CIF model path is not set, the last word is ALWAYS trimmed.",
|
||||||
|
)
|
||||||
|
|
||||||
|
simulstreaming_group.add_argument(
|
||||||
|
"--init-prompt",
|
||||||
|
type=str,
|
||||||
|
default=None,
|
||||||
|
dest="init_prompt",
|
||||||
|
help="Init prompt for the model. It should be in the target language.",
|
||||||
|
)
|
||||||
|
|
||||||
|
simulstreaming_group.add_argument(
|
||||||
|
"--static-init-prompt",
|
||||||
|
type=str,
|
||||||
|
default=None,
|
||||||
|
dest="static_init_prompt",
|
||||||
|
help="Do not scroll over this text. It can contain terminology that should be relevant over all document.",
|
||||||
|
)
|
||||||
|
|
||||||
|
simulstreaming_group.add_argument(
|
||||||
|
"--max-context-tokens",
|
||||||
|
type=int,
|
||||||
|
default=None,
|
||||||
|
dest="max_context_tokens",
|
||||||
|
help="Max context tokens for the model. Default is 0.",
|
||||||
|
)
|
||||||
|
|
||||||
|
simulstreaming_group.add_argument(
|
||||||
|
"--model-path",
|
||||||
|
type=str,
|
||||||
|
default=None,
|
||||||
|
dest="model_path",
|
||||||
|
help="Direct path to the SimulStreaming Whisper .pt model file. Overrides --model for SimulStreaming backend.",
|
||||||
|
)
|
||||||
|
|
||||||
|
simulstreaming_group.add_argument(
|
||||||
|
"--preloaded_model_count",
|
||||||
|
type=int,
|
||||||
|
default=1,
|
||||||
|
dest="preloaded_model_count",
|
||||||
|
help="Optional. Number of models to preload in memory to speed up loading (set up to the expected number of concurrent instances).",
|
||||||
|
)
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
args.transcription = not args.no_transcription
|
||||||
|
args.vad = not args.no_vad
|
||||||
|
delattr(args, 'no_transcription')
|
||||||
|
delattr(args, 'no_vad')
|
||||||
|
|
||||||
|
return args
|
||||||
110
whisperlivekit/remove_silences.py
Normal file
110
whisperlivekit/remove_silences.py
Normal file
@@ -0,0 +1,110 @@
|
|||||||
|
from whisperlivekit.timed_objects import ASRToken
|
||||||
|
import re
|
||||||
|
|
||||||
|
MIN_SILENCE_DURATION = 4 #in seconds
|
||||||
|
END_SILENCE_DURATION = 8 #in seconds. you should keep it important to not have false positive when the model lag is important
|
||||||
|
END_SILENCE_DURATION_VAC = 3 #VAC is good at detecting silences, but we want to skip the smallest silences
|
||||||
|
|
||||||
|
def blank_to_silence(tokens):
|
||||||
|
full_string = ''.join([t.text for t in tokens])
|
||||||
|
patterns = [re.compile(r'(?:\s*\[BLANK_AUDIO\]\s*)+'), re.compile(r'(?:\s*\[typing\]\s*)+')]
|
||||||
|
matches = []
|
||||||
|
for pattern in patterns:
|
||||||
|
for m in pattern.finditer(full_string):
|
||||||
|
matches.append({
|
||||||
|
'start': m.start(),
|
||||||
|
'end': m.end()
|
||||||
|
})
|
||||||
|
if matches:
|
||||||
|
# cleaned = pattern.sub(' ', full_string).strip()
|
||||||
|
# print("Cleaned:", cleaned)
|
||||||
|
cumulated_len = 0
|
||||||
|
silence_token = None
|
||||||
|
cleaned_tokens = []
|
||||||
|
for token in tokens:
|
||||||
|
if matches:
|
||||||
|
start = cumulated_len
|
||||||
|
end = cumulated_len + len(token.text)
|
||||||
|
cumulated_len = end
|
||||||
|
if start >= matches[0]['start'] and end <= matches[0]['end']:
|
||||||
|
if silence_token: #previous token was already silence
|
||||||
|
silence_token.start = min(silence_token.start, token.start)
|
||||||
|
silence_token.end = max(silence_token.end, token.end)
|
||||||
|
else: #new silence
|
||||||
|
silence_token = ASRToken(
|
||||||
|
start=token.start,
|
||||||
|
end=token.end,
|
||||||
|
speaker=-2,
|
||||||
|
probability=0.95
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
if silence_token: #there was silence but no more
|
||||||
|
if silence_token.end - silence_token.start >= MIN_SILENCE_DURATION:
|
||||||
|
cleaned_tokens.append(
|
||||||
|
silence_token
|
||||||
|
)
|
||||||
|
silence_token = None
|
||||||
|
matches.pop(0)
|
||||||
|
cleaned_tokens.append(token)
|
||||||
|
# print(cleaned_tokens)
|
||||||
|
return cleaned_tokens
|
||||||
|
return tokens
|
||||||
|
|
||||||
|
def no_token_to_silence(tokens):
|
||||||
|
new_tokens = []
|
||||||
|
silence_token = None
|
||||||
|
for token in tokens:
|
||||||
|
if token.speaker == -2:
|
||||||
|
if new_tokens and new_tokens[-1].speaker == -2: #if token is silence and previous one too
|
||||||
|
new_tokens[-1].end = token.end
|
||||||
|
else:
|
||||||
|
new_tokens.append(token)
|
||||||
|
|
||||||
|
last_end = new_tokens[-1].end if new_tokens else 0.0
|
||||||
|
if token.start - last_end >= MIN_SILENCE_DURATION: #if token is not silence but important gap
|
||||||
|
if new_tokens and new_tokens[-1].speaker == -2:
|
||||||
|
new_tokens[-1].end = token.start
|
||||||
|
else:
|
||||||
|
silence_token = ASRToken(
|
||||||
|
start=last_end,
|
||||||
|
end=token.start,
|
||||||
|
speaker=-2,
|
||||||
|
probability=0.95
|
||||||
|
)
|
||||||
|
new_tokens.append(silence_token)
|
||||||
|
|
||||||
|
if token.speaker != -2:
|
||||||
|
new_tokens.append(token)
|
||||||
|
return new_tokens
|
||||||
|
|
||||||
|
def ends_with_silence(tokens, buffer_transcription, buffer_diarization, current_time, vac_detected_silence):
|
||||||
|
if not tokens:
|
||||||
|
return [], buffer_transcription, buffer_diarization
|
||||||
|
last_token = tokens[-1]
|
||||||
|
if tokens and (
|
||||||
|
current_time - last_token.end >= END_SILENCE_DURATION
|
||||||
|
or
|
||||||
|
(current_time - last_token.end >= 3 and vac_detected_silence)
|
||||||
|
):
|
||||||
|
if last_token.speaker == -2:
|
||||||
|
last_token.end = current_time
|
||||||
|
else:
|
||||||
|
tokens.append(
|
||||||
|
ASRToken(
|
||||||
|
start=tokens[-1].end,
|
||||||
|
end=current_time,
|
||||||
|
speaker=-2,
|
||||||
|
probability=0.95
|
||||||
|
)
|
||||||
|
)
|
||||||
|
buffer_transcription = "" # for whisperstreaming backend, we should probably validate the buffer has because of the silence
|
||||||
|
buffer_diarization = ""
|
||||||
|
return tokens, buffer_transcription, buffer_diarization
|
||||||
|
|
||||||
|
|
||||||
|
def handle_silences(tokens, buffer_transcription, buffer_diarization, current_time, vac_detected_silence):
|
||||||
|
tokens = blank_to_silence(tokens) #useful for simulstreaming backend which tends to generate [BLANK_AUDIO] text
|
||||||
|
tokens = no_token_to_silence(tokens)
|
||||||
|
tokens, buffer_transcription, buffer_diarization = ends_with_silence(tokens, buffer_transcription, buffer_diarization, current_time, vac_detected_silence)
|
||||||
|
return tokens, buffer_transcription, buffer_diarization
|
||||||
|
|
||||||
163
whisperlivekit/silero_vad_iterator.py
Normal file
163
whisperlivekit/silero_vad_iterator.py
Normal file
@@ -0,0 +1,163 @@
|
|||||||
|
import torch
|
||||||
|
|
||||||
|
# This is copied from silero-vad's vad_utils.py:
|
||||||
|
# https://github.com/snakers4/silero-vad/blob/f6b1294cb27590fb2452899df98fb234dfef1134/utils_vad.py#L340
|
||||||
|
# (except changed defaults)
|
||||||
|
|
||||||
|
# Their licence is MIT, same as ours: https://github.com/snakers4/silero-vad/blob/f6b1294cb27590fb2452899df98fb234dfef1134/LICENSE
|
||||||
|
|
||||||
|
|
||||||
|
class VADIterator:
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
model,
|
||||||
|
threshold: float = 0.5,
|
||||||
|
sampling_rate: int = 16000,
|
||||||
|
min_silence_duration_ms: int = 500, # makes sense on one recording that I checked
|
||||||
|
speech_pad_ms: int = 100, # same
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
Class for stream imitation
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
model: preloaded .jit silero VAD model
|
||||||
|
|
||||||
|
threshold: float (default - 0.5)
|
||||||
|
Speech threshold. Silero VAD outputs speech probabilities for each audio chunk, probabilities ABOVE this value are considered as SPEECH.
|
||||||
|
It is better to tune this parameter for each dataset separately, but "lazy" 0.5 is pretty good for most datasets.
|
||||||
|
|
||||||
|
sampling_rate: int (default - 16000)
|
||||||
|
Currently silero VAD models support 8000 and 16000 sample rates
|
||||||
|
|
||||||
|
min_silence_duration_ms: int (default - 100 milliseconds)
|
||||||
|
In the end of each speech chunk wait for min_silence_duration_ms before separating it
|
||||||
|
|
||||||
|
speech_pad_ms: int (default - 30 milliseconds)
|
||||||
|
Final speech chunks are padded by speech_pad_ms each side
|
||||||
|
"""
|
||||||
|
|
||||||
|
self.model = model
|
||||||
|
self.threshold = threshold
|
||||||
|
self.sampling_rate = sampling_rate
|
||||||
|
|
||||||
|
if sampling_rate not in [8000, 16000]:
|
||||||
|
raise ValueError(
|
||||||
|
"VADIterator does not support sampling rates other than [8000, 16000]"
|
||||||
|
)
|
||||||
|
|
||||||
|
self.min_silence_samples = sampling_rate * min_silence_duration_ms / 1000
|
||||||
|
self.speech_pad_samples = sampling_rate * speech_pad_ms / 1000
|
||||||
|
self.reset_states()
|
||||||
|
|
||||||
|
def reset_states(self):
|
||||||
|
|
||||||
|
self.model.reset_states()
|
||||||
|
self.triggered = False
|
||||||
|
self.temp_end = 0
|
||||||
|
self.current_sample = 0
|
||||||
|
|
||||||
|
def __call__(self, x, return_seconds=False):
|
||||||
|
"""
|
||||||
|
x: torch.Tensor
|
||||||
|
audio chunk (see examples in repo)
|
||||||
|
|
||||||
|
return_seconds: bool (default - False)
|
||||||
|
whether return timestamps in seconds (default - samples)
|
||||||
|
"""
|
||||||
|
|
||||||
|
if not torch.is_tensor(x):
|
||||||
|
try:
|
||||||
|
x = torch.Tensor(x)
|
||||||
|
except:
|
||||||
|
raise TypeError("Audio cannot be casted to tensor. Cast it manually")
|
||||||
|
|
||||||
|
window_size_samples = len(x[0]) if x.dim() == 2 else len(x)
|
||||||
|
self.current_sample += window_size_samples
|
||||||
|
|
||||||
|
speech_prob = self.model(x, self.sampling_rate).item()
|
||||||
|
|
||||||
|
if (speech_prob >= self.threshold) and self.temp_end:
|
||||||
|
self.temp_end = 0
|
||||||
|
|
||||||
|
if (speech_prob >= self.threshold) and not self.triggered:
|
||||||
|
self.triggered = True
|
||||||
|
speech_start = self.current_sample - self.speech_pad_samples
|
||||||
|
return {
|
||||||
|
"start": (
|
||||||
|
int(speech_start)
|
||||||
|
if not return_seconds
|
||||||
|
else round(speech_start / self.sampling_rate, 1)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (speech_prob < self.threshold - 0.15) and self.triggered:
|
||||||
|
if not self.temp_end:
|
||||||
|
self.temp_end = self.current_sample
|
||||||
|
if self.current_sample - self.temp_end < self.min_silence_samples:
|
||||||
|
return None
|
||||||
|
else:
|
||||||
|
speech_end = self.temp_end + self.speech_pad_samples
|
||||||
|
self.temp_end = 0
|
||||||
|
self.triggered = False
|
||||||
|
return {
|
||||||
|
"end": (
|
||||||
|
int(speech_end)
|
||||||
|
if not return_seconds
|
||||||
|
else round(speech_end / self.sampling_rate, 1)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
#######################
|
||||||
|
# because Silero now requires exactly 512-sized audio chunks
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
|
||||||
|
class FixedVADIterator(VADIterator):
|
||||||
|
"""It fixes VADIterator by allowing to process any audio length, not only exactly 512 frames at once.
|
||||||
|
If audio to be processed at once is long and multiple voiced segments detected,
|
||||||
|
then __call__ returns the start of the first segment, and end (or middle, which means no end) of the last segment.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def reset_states(self):
|
||||||
|
super().reset_states()
|
||||||
|
self.buffer = np.array([], dtype=np.float32)
|
||||||
|
|
||||||
|
def __call__(self, x, return_seconds=False):
|
||||||
|
self.buffer = np.append(self.buffer, x)
|
||||||
|
ret = None
|
||||||
|
while len(self.buffer) >= 512:
|
||||||
|
r = super().__call__(self.buffer[:512], return_seconds=return_seconds)
|
||||||
|
self.buffer = self.buffer[512:]
|
||||||
|
if ret is None:
|
||||||
|
ret = r
|
||||||
|
elif r is not None:
|
||||||
|
if "end" in r:
|
||||||
|
ret["end"] = r["end"] # the latter end
|
||||||
|
if "start" in r and "end" in ret: # there is an earlier start.
|
||||||
|
# Remove end, merging this segment with the previous one.
|
||||||
|
del ret["end"]
|
||||||
|
return ret if ret != {} else None
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
# test/demonstrate the need for FixedVADIterator:
|
||||||
|
|
||||||
|
import torch
|
||||||
|
|
||||||
|
model, _ = torch.hub.load(repo_or_dir="snakers4/silero-vad", model="silero_vad")
|
||||||
|
vac = FixedVADIterator(model)
|
||||||
|
# vac = VADIterator(model) # the second case crashes with this
|
||||||
|
|
||||||
|
# this works: for both
|
||||||
|
audio_buffer = np.array([0] * (512), dtype=np.float32)
|
||||||
|
vac(audio_buffer)
|
||||||
|
|
||||||
|
# this crashes on the non FixedVADIterator with
|
||||||
|
# ops.prim.RaiseException("Input audio chunk is too short", "builtins.ValueError")
|
||||||
|
audio_buffer = np.array([0] * (512 - 1), dtype=np.float32)
|
||||||
|
vac(audio_buffer)
|
||||||
6
whisperlivekit/simul_whisper/__init__.py
Normal file
6
whisperlivekit/simul_whisper/__init__.py
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
from .backend import SimulStreamingASR, SimulStreamingOnlineProcessor
|
||||||
|
|
||||||
|
__all__ = [
|
||||||
|
"SimulStreamingASR",
|
||||||
|
"SimulStreamingOnlineProcessor",
|
||||||
|
]
|
||||||
315
whisperlivekit/simul_whisper/backend.py
Normal file
315
whisperlivekit/simul_whisper/backend.py
Normal file
@@ -0,0 +1,315 @@
|
|||||||
|
import sys
|
||||||
|
import numpy as np
|
||||||
|
import logging
|
||||||
|
from typing import List, Tuple, Optional
|
||||||
|
import logging
|
||||||
|
from whisperlivekit.timed_objects import ASRToken, Transcript
|
||||||
|
from whisperlivekit.warmup import load_file
|
||||||
|
from whisperlivekit.simul_whisper.license_simulstreaming import SIMULSTREAMING_LICENSE
|
||||||
|
from .whisper import load_model, tokenizer
|
||||||
|
from .whisper.audio import TOKENS_PER_SECOND
|
||||||
|
|
||||||
|
import os
|
||||||
|
import gc
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
try:
|
||||||
|
import torch
|
||||||
|
from whisperlivekit.simul_whisper.config import AlignAttConfig
|
||||||
|
from whisperlivekit.simul_whisper.simul_whisper import PaddedAlignAttWhisper
|
||||||
|
from whisperlivekit.simul_whisper.whisper import tokenizer
|
||||||
|
except ImportError as e:
|
||||||
|
raise ImportError(
|
||||||
|
"""SimulStreaming dependencies are not available.
|
||||||
|
Please install WhisperLiveKit using pip install "whisperlivekit[simulstreaming]".""")
|
||||||
|
|
||||||
|
# TOO_MANY_REPETITIONS = 3
|
||||||
|
|
||||||
|
class SimulStreamingOnlineProcessor:
|
||||||
|
SAMPLING_RATE = 16000
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
asr,
|
||||||
|
logfile=sys.stderr,
|
||||||
|
warmup_file=None
|
||||||
|
):
|
||||||
|
self.asr = asr
|
||||||
|
self.logfile = logfile
|
||||||
|
self.end = 0.0
|
||||||
|
self.global_time_offset = 0.0
|
||||||
|
|
||||||
|
self.committed: List[ASRToken] = []
|
||||||
|
self.last_result_tokens: List[ASRToken] = []
|
||||||
|
self.load_new_backend()
|
||||||
|
if asr.tokenizer:
|
||||||
|
self.model.tokenizer = asr.tokenizer
|
||||||
|
|
||||||
|
def load_new_backend(self):
|
||||||
|
model = self.asr.get_new_model_instance()
|
||||||
|
self.model = PaddedAlignAttWhisper(
|
||||||
|
cfg=self.asr.cfg,
|
||||||
|
loaded_model=model)
|
||||||
|
|
||||||
|
def insert_silence(self, silence_duration, offset):
|
||||||
|
"""
|
||||||
|
If silences are > 5s, we do a complete context clear. Otherwise, we just insert a small silence and shift the last_attend_frame
|
||||||
|
"""
|
||||||
|
if silence_duration < 5:
|
||||||
|
gap_silence = torch.zeros(int(16000*silence_duration))
|
||||||
|
self.model.insert_audio(gap_silence)
|
||||||
|
# self.global_time_offset += silence_duration
|
||||||
|
else:
|
||||||
|
self.process_iter(is_last=True) #we want to totally process what remains in the buffer.
|
||||||
|
self.model.refresh_segment(complete=True)
|
||||||
|
self.global_time_offset += silence_duration + offset
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def insert_audio_chunk(self, audio: np.ndarray, audio_stream_end_time):
|
||||||
|
"""Append an audio chunk to be processed by SimulStreaming."""
|
||||||
|
|
||||||
|
# Convert numpy array to torch tensor
|
||||||
|
audio_tensor = torch.from_numpy(audio).float()
|
||||||
|
self.end = audio_stream_end_time #Only to be aligned with what happens in whisperstreaming backend.
|
||||||
|
self.model.insert_audio(audio_tensor)
|
||||||
|
|
||||||
|
def get_buffer(self):
|
||||||
|
return Transcript(
|
||||||
|
start=None,
|
||||||
|
end=None,
|
||||||
|
text='',
|
||||||
|
probability=None
|
||||||
|
)
|
||||||
|
|
||||||
|
def timestamped_text(self, tokens, generation):
|
||||||
|
"""
|
||||||
|
generate timestamped text from tokens and generation data.
|
||||||
|
|
||||||
|
args:
|
||||||
|
tokens: List of tokens to process
|
||||||
|
generation: Dictionary containing generation progress and optionally results
|
||||||
|
|
||||||
|
returns:
|
||||||
|
List of tuples containing (start_time, end_time, word) for each word
|
||||||
|
"""
|
||||||
|
FRAME_DURATION = 0.02
|
||||||
|
if "result" in generation:
|
||||||
|
split_words = generation["result"]["split_words"]
|
||||||
|
split_tokens = generation["result"]["split_tokens"]
|
||||||
|
else:
|
||||||
|
split_words, split_tokens = self.model.tokenizer.split_to_word_tokens(tokens)
|
||||||
|
progress = generation["progress"]
|
||||||
|
frames = [p["most_attended_frames"][0] for p in progress]
|
||||||
|
absolute_timestamps = [p["absolute_timestamps"][0] for p in progress]
|
||||||
|
tokens_queue = tokens.copy()
|
||||||
|
timestamped_words = []
|
||||||
|
|
||||||
|
for word, word_tokens in zip(split_words, split_tokens):
|
||||||
|
# start_frame = None
|
||||||
|
# end_frame = None
|
||||||
|
for expected_token in word_tokens:
|
||||||
|
if not tokens_queue or not frames:
|
||||||
|
raise ValueError(f"Insufficient tokens or frames for word '{word}'")
|
||||||
|
|
||||||
|
actual_token = tokens_queue.pop(0)
|
||||||
|
current_frame = frames.pop(0)
|
||||||
|
current_timestamp = absolute_timestamps.pop(0)
|
||||||
|
if actual_token != expected_token:
|
||||||
|
raise ValueError(
|
||||||
|
f"Token mismatch: expected '{expected_token}', "
|
||||||
|
f"got '{actual_token}' at frame {current_frame}"
|
||||||
|
)
|
||||||
|
# if start_frame is None:
|
||||||
|
# start_frame = current_frame
|
||||||
|
# end_frame = current_frame
|
||||||
|
# start_time = start_frame * FRAME_DURATION
|
||||||
|
# end_time = end_frame * FRAME_DURATION
|
||||||
|
start_time = current_timestamp
|
||||||
|
end_time = current_timestamp + 0.1
|
||||||
|
timestamp_entry = (start_time, end_time, word)
|
||||||
|
timestamped_words.append(timestamp_entry)
|
||||||
|
logger.debug(f"TS-WORD:\t{start_time:.2f}\t{end_time:.2f}\t{word}")
|
||||||
|
return timestamped_words
|
||||||
|
|
||||||
|
def process_iter(self, is_last=False) -> Tuple[List[ASRToken], float]:
|
||||||
|
"""
|
||||||
|
Process accumulated audio chunks using SimulStreaming.
|
||||||
|
|
||||||
|
Returns a tuple: (list of committed ASRToken objects, float representing the audio processed up to time).
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
tokens, generation_progress = self.model.infer(is_last=is_last)
|
||||||
|
ts_words = self.timestamped_text(tokens, generation_progress)
|
||||||
|
|
||||||
|
new_tokens = []
|
||||||
|
for ts_word in ts_words:
|
||||||
|
|
||||||
|
start, end, word = ts_word
|
||||||
|
token = ASRToken(
|
||||||
|
start=start,
|
||||||
|
end=end,
|
||||||
|
text=word,
|
||||||
|
probability=0.95 # fake prob. Maybe we can extract it from the model?
|
||||||
|
).with_offset(
|
||||||
|
self.global_time_offset
|
||||||
|
)
|
||||||
|
new_tokens.append(token)
|
||||||
|
|
||||||
|
# identical_tokens = 0
|
||||||
|
# n_new_tokens = len(new_tokens)
|
||||||
|
# if n_new_tokens:
|
||||||
|
|
||||||
|
self.committed.extend(new_tokens)
|
||||||
|
|
||||||
|
# if token in self.committed:
|
||||||
|
# pos = len(self.committed) - 1 - self.committed[::-1].index(token)
|
||||||
|
# if pos:
|
||||||
|
# for i in range(len(self.committed) - n_new_tokens, -1, -n_new_tokens):
|
||||||
|
# commited_segment = self.committed[i:i+n_new_tokens]
|
||||||
|
# if commited_segment == new_tokens:
|
||||||
|
# identical_segments +=1
|
||||||
|
# if identical_tokens >= TOO_MANY_REPETITIONS:
|
||||||
|
# logger.warning('Too many repetition, model is stuck. Load a new one')
|
||||||
|
# self.committed = self.committed[:i]
|
||||||
|
# self.load_new_backend()
|
||||||
|
# return [], self.end
|
||||||
|
|
||||||
|
# pos = self.committed.rindex(token)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return new_tokens, self.end
|
||||||
|
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.exception(f"SimulStreaming processing error: {e}")
|
||||||
|
return [], self.end
|
||||||
|
|
||||||
|
def warmup(self, audio, init_prompt=""):
|
||||||
|
"""Warmup the SimulStreaming model."""
|
||||||
|
try:
|
||||||
|
self.model.insert_audio(audio)
|
||||||
|
self.model.infer(True)
|
||||||
|
self.model.refresh_segment(complete=True)
|
||||||
|
logger.info("SimulStreaming model warmed up successfully")
|
||||||
|
except Exception as e:
|
||||||
|
logger.exception(f"SimulStreaming warmup failed: {e}")
|
||||||
|
|
||||||
|
def __del__(self):
|
||||||
|
# free the model and add a new model to stack.
|
||||||
|
# del self.model
|
||||||
|
gc.collect()
|
||||||
|
torch.cuda.empty_cache()
|
||||||
|
# self.asr.new_model_to_stack()
|
||||||
|
self.model.remove_hooks()
|
||||||
|
|
||||||
|
class SimulStreamingASR():
|
||||||
|
"""SimulStreaming backend with AlignAtt policy."""
|
||||||
|
sep = ""
|
||||||
|
|
||||||
|
def __init__(self, lan, modelsize=None, cache_dir=None, model_dir=None, logfile=sys.stderr, **kwargs):
|
||||||
|
logger.warning(SIMULSTREAMING_LICENSE)
|
||||||
|
self.logfile = logfile
|
||||||
|
self.transcribe_kargs = {}
|
||||||
|
self.original_language = None if lan == "auto" else lan
|
||||||
|
|
||||||
|
self.model_path = kwargs.get('model_path', './large-v3.pt')
|
||||||
|
self.frame_threshold = kwargs.get('frame_threshold', 25)
|
||||||
|
self.audio_max_len = kwargs.get('audio_max_len', 20.0)
|
||||||
|
self.audio_min_len = kwargs.get('audio_min_len', 0.0)
|
||||||
|
self.segment_length = kwargs.get('segment_length', 0.5)
|
||||||
|
self.beams = kwargs.get('beams', 1)
|
||||||
|
self.decoder_type = kwargs.get('decoder_type', 'greedy' if self.beams == 1 else 'beam')
|
||||||
|
self.task = kwargs.get('task', 'transcribe')
|
||||||
|
self.cif_ckpt_path = kwargs.get('cif_ckpt_path', None)
|
||||||
|
self.never_fire = kwargs.get('never_fire', False)
|
||||||
|
self.init_prompt = kwargs.get('init_prompt', None)
|
||||||
|
self.static_init_prompt = kwargs.get('static_init_prompt', None)
|
||||||
|
self.max_context_tokens = kwargs.get('max_context_tokens', None)
|
||||||
|
self.warmup_file = kwargs.get('warmup_file', None)
|
||||||
|
self.preload_model_count = kwargs.get('preload_model_count', 1)
|
||||||
|
|
||||||
|
if model_dir is not None:
|
||||||
|
self.model_path = model_dir
|
||||||
|
elif modelsize is not None:
|
||||||
|
model_mapping = {
|
||||||
|
'tiny': './tiny.pt',
|
||||||
|
'base': './base.pt',
|
||||||
|
'small': './small.pt',
|
||||||
|
'medium': './medium.pt',
|
||||||
|
'medium.en': './medium.en.pt',
|
||||||
|
'large-v1': './large-v1.pt',
|
||||||
|
'base.en': './base.en.pt',
|
||||||
|
'small.en': './small.en.pt',
|
||||||
|
'tiny.en': './tiny.en.pt',
|
||||||
|
'large-v2': './large-v2.pt',
|
||||||
|
'large-v3': './large-v3.pt',
|
||||||
|
'large': './large-v3.pt'
|
||||||
|
}
|
||||||
|
self.model_path = model_mapping.get(modelsize, f'./{modelsize}.pt')
|
||||||
|
|
||||||
|
# Set up tokenizer for translation if needed
|
||||||
|
if self.task == "translate":
|
||||||
|
self.tokenizer = self.set_translate_task()
|
||||||
|
else:
|
||||||
|
self.tokenizer = None
|
||||||
|
self.cfg = AlignAttConfig(
|
||||||
|
model_path=self.model_path,
|
||||||
|
segment_length=self.segment_length,
|
||||||
|
frame_threshold=self.frame_threshold,
|
||||||
|
language=self.original_language,
|
||||||
|
audio_max_len=self.audio_max_len,
|
||||||
|
audio_min_len=self.audio_min_len,
|
||||||
|
cif_ckpt_path=self.cif_ckpt_path,
|
||||||
|
decoder_type="beam",
|
||||||
|
beam_size=self.beams,
|
||||||
|
task=self.task,
|
||||||
|
never_fire=self.never_fire,
|
||||||
|
init_prompt=self.init_prompt,
|
||||||
|
max_context_tokens=self.max_context_tokens,
|
||||||
|
static_init_prompt=self.static_init_prompt,
|
||||||
|
)
|
||||||
|
|
||||||
|
self.model_name = os.path.basename(self.cfg.model_path).replace(".pt", "")
|
||||||
|
self.model_path = os.path.dirname(os.path.abspath(self.cfg.model_path))
|
||||||
|
self.models = [self.load_model() for i in range(self.preload_model_count)]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def load_model(self):
|
||||||
|
whisper_model = load_model(name=self.model_name, download_root=self.model_path)
|
||||||
|
warmup_audio = load_file(self.warmup_file)
|
||||||
|
whisper_model.transcribe(warmup_audio, language=self.original_language)
|
||||||
|
return whisper_model
|
||||||
|
|
||||||
|
def get_new_model_instance(self):
|
||||||
|
"""
|
||||||
|
SimulStreaming cannot share the same backend because it uses global forward hooks on the attention layers.
|
||||||
|
Therefore, each user requires a separate model instance, which can be memory-intensive. To maintain speed, we preload the models into memory.
|
||||||
|
"""
|
||||||
|
if len(self.models) == 0:
|
||||||
|
self.models.append(self.load_model())
|
||||||
|
new_model = self.models.pop()
|
||||||
|
return new_model
|
||||||
|
# self.models[0]
|
||||||
|
|
||||||
|
def new_model_to_stack(self):
|
||||||
|
self.models.append(self.load_model())
|
||||||
|
|
||||||
|
|
||||||
|
def set_translate_task(self):
|
||||||
|
"""Set up translation task."""
|
||||||
|
return tokenizer.get_tokenizer(
|
||||||
|
multilingual=True,
|
||||||
|
language=self.model.cfg.language,
|
||||||
|
num_languages=self.model.model.num_languages,
|
||||||
|
task="translate"
|
||||||
|
)
|
||||||
|
|
||||||
|
def transcribe(self, audio):
|
||||||
|
"""
|
||||||
|
Warmup is done directly in load_model
|
||||||
|
"""
|
||||||
|
pass
|
||||||
17
whisperlivekit/simul_whisper/beam.py
Normal file
17
whisperlivekit/simul_whisper/beam.py
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
from .whisper.decoding import PyTorchInference
|
||||||
|
|
||||||
|
# extention of PyTorchInference for beam search
|
||||||
|
class BeamPyTorchInference(PyTorchInference):
|
||||||
|
|
||||||
|
def _kv_modules(self):
|
||||||
|
key_modules = [block.attn.key.cache_id for block in self.model.decoder.blocks]
|
||||||
|
value_modules = [block.attn.value.cache_id for block in self.model.decoder.blocks]
|
||||||
|
return key_modules + value_modules
|
||||||
|
|
||||||
|
def rearrange_kv_cache(self, source_indices):
|
||||||
|
if source_indices != list(range(len(source_indices))):
|
||||||
|
for module_cache_id in self._kv_modules():
|
||||||
|
self.kv_cache[module_cache_id] = self.kv_cache[module_cache_id][source_indices].detach()
|
||||||
|
from torch import Tensor
|
||||||
|
def logits(self, tokens: Tensor, audio_features: Tensor) -> Tensor:
|
||||||
|
return self.model.decoder(tokens, audio_features, kv_cache=self.kv_cache)
|
||||||
29
whisperlivekit/simul_whisper/config.py
Normal file
29
whisperlivekit/simul_whisper/config.py
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
# This code was originally in simul_whisper/transcriber/simul_whisper.py . It is adapted a lot for SimulStreaming.
|
||||||
|
|
||||||
|
from dataclasses import dataclass, field
|
||||||
|
from typing import Literal
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class SimulWhisperConfig:
|
||||||
|
'''Options that are common for all simul policies that could be implemented in SimulWhisper.'''
|
||||||
|
model_path: str
|
||||||
|
language: str = field(default="zh")
|
||||||
|
nonspeech_prob: float = 0.5
|
||||||
|
audio_min_len: float = 1.0
|
||||||
|
decoder_type: Literal["greedy","beam"] = "greedy"
|
||||||
|
beam_size: int = 5
|
||||||
|
task: Literal["transcribe","translate"] = "transcribe"
|
||||||
|
init_prompt: str = field(default=None)
|
||||||
|
static_init_prompt: str = field(default=None)
|
||||||
|
max_context_tokens: int = field(default=None)
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class AlignAttConfig(SimulWhisperConfig):
|
||||||
|
'''Options specific to the AlignAtt policy.'''
|
||||||
|
eval_data_path: str = "tmp"
|
||||||
|
segment_length: float = field(default=1.0, metadata = {"help": "in second"})
|
||||||
|
frame_threshold: int = 4
|
||||||
|
rewind_threshold: int = 200
|
||||||
|
audio_max_len: float = 20.0
|
||||||
|
cif_ckpt_path: str = ""
|
||||||
|
never_fire: bool = False
|
||||||
65
whisperlivekit/simul_whisper/eow_detection.py
Normal file
65
whisperlivekit/simul_whisper/eow_detection.py
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
import torch
|
||||||
|
|
||||||
|
# code for the end-of-word detection based on the CIF model proposed in Simul-Whisper
|
||||||
|
|
||||||
|
def load_cif(cfg, n_audio_state, device):
|
||||||
|
"""cfg: AlignAttConfig, n_audio_state: int, device: torch.device"""
|
||||||
|
cif_linear = torch.nn.Linear(n_audio_state, 1)
|
||||||
|
if cfg.cif_ckpt_path is None or not cfg.cif_ckpt_path:
|
||||||
|
if cfg.never_fire:
|
||||||
|
never_fire = True
|
||||||
|
always_fire = False
|
||||||
|
else:
|
||||||
|
always_fire = True
|
||||||
|
never_fire = False
|
||||||
|
else:
|
||||||
|
always_fire = False
|
||||||
|
never_fire = cfg.never_fire
|
||||||
|
checkpoint = torch.load(cfg.cif_ckpt_path)
|
||||||
|
cif_linear.load_state_dict(checkpoint)
|
||||||
|
cif_linear.to(device)
|
||||||
|
return cif_linear, always_fire, never_fire
|
||||||
|
|
||||||
|
|
||||||
|
# from https://github.com/dqqcasia/mosst/blob/master/fairseq/models/speech_to_text/convtransformer_wav2vec_cif.py
|
||||||
|
def resize(alphas, target_lengths, threshold=0.999):
|
||||||
|
"""
|
||||||
|
alpha in thresh=1.0 | (0.0, +0.21)
|
||||||
|
target_lengths: if None, apply round and resize, else apply scaling
|
||||||
|
"""
|
||||||
|
# sum
|
||||||
|
_num = alphas.sum(-1)
|
||||||
|
num = target_lengths.float()
|
||||||
|
# scaling
|
||||||
|
_alphas = alphas * (num / _num)[:, None].repeat(1, alphas.size(1))
|
||||||
|
# rm attention value that exceeds threashold
|
||||||
|
count = 0
|
||||||
|
while len(torch.where(_alphas > threshold)[0]):
|
||||||
|
count += 1
|
||||||
|
if count > 10:
|
||||||
|
break
|
||||||
|
xs, ys = torch.where(_alphas > threshold)
|
||||||
|
for x, y in zip(xs, ys):
|
||||||
|
if _alphas[x][y] >= threshold:
|
||||||
|
mask = _alphas[x].ne(0).float()
|
||||||
|
mean = 0.5 * _alphas[x].sum() / mask.sum()
|
||||||
|
_alphas[x] = _alphas[x] * 0.5 + mean * mask
|
||||||
|
|
||||||
|
return _alphas, _num
|
||||||
|
|
||||||
|
def fire_at_boundary(chunked_encoder_feature: torch.Tensor, cif_linear):
|
||||||
|
content_mel_len = chunked_encoder_feature.shape[1] # B, T, D
|
||||||
|
alphas = cif_linear(chunked_encoder_feature).squeeze(dim=2) # B, T
|
||||||
|
alphas = torch.sigmoid(alphas)
|
||||||
|
decode_length = torch.round(alphas.sum(-1)).int()
|
||||||
|
alphas, _ = resize(alphas, decode_length)
|
||||||
|
alphas = alphas.squeeze(0) # (T, )
|
||||||
|
threshold = 0.999
|
||||||
|
integrate = torch.cumsum(alphas[:-1], dim=0) # ignore the peak value at the end of the content chunk
|
||||||
|
exceed_count = integrate[-1] // threshold
|
||||||
|
integrate = integrate - exceed_count*1.0 # minus 1 every time intergrate exceed the threshold
|
||||||
|
important_positions = (integrate >= 0).nonzero(as_tuple=True)[0]
|
||||||
|
if important_positions.numel() == 0:
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
return important_positions[0] >= content_mel_len-2
|
||||||
43
whisperlivekit/simul_whisper/generation_progress.py
Normal file
43
whisperlivekit/simul_whisper/generation_progress.py
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
class Tokens:
|
||||||
|
def __init__(self, tokens):
|
||||||
|
self.tokens = tokens
|
||||||
|
|
||||||
|
# def clone(self):
|
||||||
|
# return Tokens(self.tokens.clone())
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return str(self.tokens.tolist())
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return self.__str__()
|
||||||
|
|
||||||
|
class BeamTokens(Tokens):
|
||||||
|
def __init__(self, tokens, beam_size):
|
||||||
|
self.tokens = tokens
|
||||||
|
self.beam_size = beam_size
|
||||||
|
|
||||||
|
def clone(self):
|
||||||
|
return BeamTokens(self.tokens.clone())
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return f"BeamTokens({self.tokens.tolist()}, beam_size={self.beam_size})"
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return self.__str__()
|
||||||
|
|
||||||
|
def as_text(self, tokenizer):
|
||||||
|
return tokenizer.decode(self.tokens)
|
||||||
|
|
||||||
|
class Logits(Tokens):
|
||||||
|
def __init__(self, logits):
|
||||||
|
super().__init__(logits)
|
||||||
|
|
||||||
|
# def clone(self):
|
||||||
|
# return Logits(self.tokens.clone(), self.beam_size)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
# return "abc"
|
||||||
|
return f"Logits({self.tokens.shape})"
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return self.__str__()
|
||||||
5
whisperlivekit/simul_whisper/license_simulstreaming.py
Normal file
5
whisperlivekit/simul_whisper/license_simulstreaming.py
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
SIMULSTREAMING_LICENSE = f"""
|
||||||
|
SimulStreaming backend is dual-licensed:
|
||||||
|
• Non-Commercial Use: PolyForm Noncommercial License 1.0.0.
|
||||||
|
• Commercial Use: Check SimulStreaming README (github.com/ufal/SimulStreaming) for more details.
|
||||||
|
"""
|
||||||
621
whisperlivekit/simul_whisper/simul_whisper.py
Normal file
621
whisperlivekit/simul_whisper/simul_whisper.py
Normal file
@@ -0,0 +1,621 @@
|
|||||||
|
# This code was originally in simul_whisper/transcriber/simul_whisper.py . It is adapted a lot for SimulStreaming.
|
||||||
|
|
||||||
|
import os
|
||||||
|
import logging
|
||||||
|
|
||||||
|
import torch
|
||||||
|
import torch.nn.functional as F
|
||||||
|
|
||||||
|
from .whisper import load_model, DecodingOptions, tokenizer
|
||||||
|
from .config import AlignAttConfig
|
||||||
|
from .whisper.audio import log_mel_spectrogram, TOKENS_PER_SECOND, pad_or_trim, N_SAMPLES, N_FRAMES
|
||||||
|
from .whisper.timing import median_filter
|
||||||
|
from .whisper.decoding import GreedyDecoder, BeamSearchDecoder, SuppressTokens, detect_language
|
||||||
|
from .beam import BeamPyTorchInference
|
||||||
|
from .eow_detection import fire_at_boundary, load_cif
|
||||||
|
import os
|
||||||
|
|
||||||
|
from .token_buffer import TokenBuffer
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
from .generation_progress import *
|
||||||
|
|
||||||
|
DEC_PAD = 50257
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import wave
|
||||||
|
|
||||||
|
# New features added to the original version of Simul-Whisper:
|
||||||
|
# - large-v3 model support
|
||||||
|
# - translation support
|
||||||
|
# - beam search
|
||||||
|
# - prompt -- static vs. non-static
|
||||||
|
# - context
|
||||||
|
class PaddedAlignAttWhisper:
|
||||||
|
def __init__(self, cfg: AlignAttConfig, loaded_model=None) -> None:
|
||||||
|
self.log_segments = 0
|
||||||
|
model_name = os.path.basename(cfg.model_path).replace(".pt", "")
|
||||||
|
model_path = os.path.dirname(os.path.abspath(cfg.model_path))
|
||||||
|
if loaded_model:
|
||||||
|
self.model = loaded_model
|
||||||
|
else:
|
||||||
|
self.model = load_model(name=model_name, download_root=model_path)
|
||||||
|
|
||||||
|
logger.info(f"Model dimensions: {self.model.dims}")
|
||||||
|
|
||||||
|
self.decode_options = DecodingOptions(
|
||||||
|
language = cfg.language,
|
||||||
|
without_timestamps = True,
|
||||||
|
task=cfg.task
|
||||||
|
)
|
||||||
|
self.tokenizer_is_multilingual = not model_name.endswith(".en")
|
||||||
|
self.create_tokenizer(cfg.language if cfg.language != "auto" else None)
|
||||||
|
self.detected_language = cfg.language if cfg.language != "auto" else None
|
||||||
|
|
||||||
|
self.max_text_len = self.model.dims.n_text_ctx
|
||||||
|
self.num_decoder_layers = len(self.model.decoder.blocks)
|
||||||
|
self.cfg = cfg
|
||||||
|
self.l_hooks = []
|
||||||
|
|
||||||
|
# model to detect end-of-word boundary at the end of the segment
|
||||||
|
self.CIFLinear, self.always_fire, self.never_fire = load_cif(cfg,
|
||||||
|
n_audio_state=self.model.dims.n_audio_state,
|
||||||
|
device=self.model.device)
|
||||||
|
|
||||||
|
# install hooks to access encoder-decoder attention
|
||||||
|
self.dec_attns = []
|
||||||
|
def layer_hook(module, net_input, net_output):
|
||||||
|
# net_output[1]: B*num_head*token_len*audio_len
|
||||||
|
t = F.softmax(net_output[1], dim=-1)
|
||||||
|
self.dec_attns.append(t.squeeze(0))
|
||||||
|
for b in self.model.decoder.blocks:
|
||||||
|
hook = b.cross_attn.register_forward_hook(layer_hook)
|
||||||
|
self.l_hooks.append(hook)
|
||||||
|
|
||||||
|
self.kv_cache = {}
|
||||||
|
def kv_hook(module: torch.nn.Linear, _, net_output: torch.Tensor):
|
||||||
|
if module.cache_id not in self.kv_cache or net_output.shape[1] > self.max_text_len:
|
||||||
|
# save as-is, for the first token or cross attention
|
||||||
|
self.kv_cache[module.cache_id] = net_output
|
||||||
|
else:
|
||||||
|
x = self.kv_cache[module.cache_id]
|
||||||
|
self.kv_cache[module.cache_id] = torch.cat([x, net_output], dim=1).detach()
|
||||||
|
return self.kv_cache[module.cache_id]
|
||||||
|
|
||||||
|
for i,b in enumerate(self.model.decoder.blocks):
|
||||||
|
hooks = [
|
||||||
|
b.attn.key.register_forward_hook(kv_hook),
|
||||||
|
b.attn.value.register_forward_hook(kv_hook),
|
||||||
|
b.cross_attn.key.register_forward_hook(kv_hook),
|
||||||
|
b.cross_attn.value.register_forward_hook(kv_hook),
|
||||||
|
]
|
||||||
|
self.l_hooks.extend(hooks)
|
||||||
|
|
||||||
|
self.align_source = {}
|
||||||
|
self.num_align_heads = 0
|
||||||
|
for layer_rank, head_id in self.model.alignment_heads.indices().T:
|
||||||
|
layer_rank = layer_rank.item()
|
||||||
|
heads = self.align_source.get(layer_rank, [])
|
||||||
|
heads.append((self.num_align_heads, head_id.item()))
|
||||||
|
self.align_source[layer_rank] = heads
|
||||||
|
self.num_align_heads += 1
|
||||||
|
|
||||||
|
|
||||||
|
# tokens to be suppressed from decoding, to prevent hallucinations
|
||||||
|
suppress_tokens = [
|
||||||
|
self.tokenizer.transcribe,
|
||||||
|
self.tokenizer.translate,
|
||||||
|
self.tokenizer.sot,
|
||||||
|
self.tokenizer.sot_prev,
|
||||||
|
self.tokenizer.sot_lm,
|
||||||
|
# self.tokenizer.eot
|
||||||
|
self.tokenizer.no_timestamps, # added by DM
|
||||||
|
] + list(self.tokenizer.all_language_tokens) # added by DM
|
||||||
|
if self.tokenizer.no_speech is not None:
|
||||||
|
suppress_tokens.append(self.tokenizer.no_speech)
|
||||||
|
suppress_tokens = tuple(sorted(set(suppress_tokens)))
|
||||||
|
logger.debug(f"Suppress tokens: {suppress_tokens}")
|
||||||
|
sup_tokens = SuppressTokens(suppress_tokens)
|
||||||
|
self.suppress_tokens = lambda logits: sup_tokens.apply(logits, None)
|
||||||
|
# blank tokens are suppresed for new segments near the line 334
|
||||||
|
|
||||||
|
# it's going to be regenerated after lang id
|
||||||
|
self.segments = []
|
||||||
|
self.init_tokens()
|
||||||
|
|
||||||
|
self.last_attend_frame = -self.cfg.rewind_threshold
|
||||||
|
self.cumulative_time_offset = 0.0
|
||||||
|
|
||||||
|
if self.cfg.max_context_tokens is None:
|
||||||
|
self.max_context_tokens = self.max_text_len
|
||||||
|
else:
|
||||||
|
self.max_context_tokens = self.cfg.max_context_tokens
|
||||||
|
self.init_context()
|
||||||
|
|
||||||
|
# decoder type: greedy or beam
|
||||||
|
if cfg.decoder_type == "greedy":
|
||||||
|
logger.info("Using greedy decoder")
|
||||||
|
self.token_decoder = GreedyDecoder(0.0, self.tokenizer.eot)
|
||||||
|
self.decoder_type = "greedy"
|
||||||
|
|
||||||
|
elif cfg.decoder_type == "beam":
|
||||||
|
self.decoder_type = "beam"
|
||||||
|
self.inference = BeamPyTorchInference(self.model, self.initial_token_length)
|
||||||
|
self.inference.kv_cache = self.kv_cache
|
||||||
|
|
||||||
|
self.token_decoder = BeamSearchDecoder(inference=self.inference, eot=self.tokenizer.eot, beam_size=cfg.beam_size)
|
||||||
|
|
||||||
|
def remove_hooks(self):
|
||||||
|
print('remove hook')
|
||||||
|
for hook in self.l_hooks:
|
||||||
|
hook.remove()
|
||||||
|
|
||||||
|
def create_tokenizer(self, language=None):
|
||||||
|
self.tokenizer = tokenizer.get_tokenizer(
|
||||||
|
multilingual=self.tokenizer_is_multilingual,
|
||||||
|
language=language,
|
||||||
|
num_languages=self.model.num_languages,
|
||||||
|
task=self.decode_options.task
|
||||||
|
)
|
||||||
|
|
||||||
|
def init_context(self):
|
||||||
|
kw = {'tokenizer': self.tokenizer,
|
||||||
|
'device': self.model.device,
|
||||||
|
'prefix_token_ids': [self.tokenizer.sot_prev]}
|
||||||
|
self.context = TokenBuffer.empty(**kw)
|
||||||
|
if self.cfg.static_init_prompt is not None:
|
||||||
|
self.context = TokenBuffer.from_text(self.cfg.static_init_prompt, **kw)
|
||||||
|
if self.cfg.init_prompt is not None:
|
||||||
|
self.context.text += self.cfg.init_prompt
|
||||||
|
|
||||||
|
def init_tokens(self):
|
||||||
|
logger.debug(f"init tokens, {len(self.segments)}")
|
||||||
|
# init tokens (mandatory prompt)
|
||||||
|
self.initial_tokens = torch.tensor(
|
||||||
|
self.tokenizer.sot_sequence_including_notimestamps,
|
||||||
|
dtype=torch.long,
|
||||||
|
device=self.model.device).unsqueeze(0)
|
||||||
|
self.initial_token_length = self.initial_tokens.shape[1]
|
||||||
|
self.sot_index = self.tokenizer.sot_sequence.index(self.tokenizer.sot)
|
||||||
|
# self.segments = []
|
||||||
|
logger.debug(f"init tokens after, {len(self.segments)}")
|
||||||
|
self.tokens = [self.initial_tokens]
|
||||||
|
|
||||||
|
def trim_context(self):
|
||||||
|
logger.info("Trimming context")
|
||||||
|
c = len(self.context.as_token_ids()) - len(self.context.prefix_token_ids)
|
||||||
|
# logger.debug(f"c= {len(self.context.as_token_ids())}, {len(self.context.prefix_token_ids)}")
|
||||||
|
logger.info(f"Context text: {self.context.as_text()}")
|
||||||
|
# logger.debug(f"Context tensor: {self.context.as_tensor()}")
|
||||||
|
l = sum(t.shape[1] for t in self.tokens) + c
|
||||||
|
# logger.debug(f"len {l}, c {c}, max_context_tokens {self.max_context_tokens}")
|
||||||
|
if self.cfg.static_init_prompt is None:
|
||||||
|
after = 0
|
||||||
|
else:
|
||||||
|
after = len(self.cfg.static_init_prompt)
|
||||||
|
# logger.debug(f"len {l}, c {c}, max_context_tokens {self.max_context_tokens}")
|
||||||
|
while c > self.max_context_tokens or l > self.max_text_len - 20:
|
||||||
|
t = self.context.trim_words(after=after)
|
||||||
|
l -= t
|
||||||
|
c -= t
|
||||||
|
logger.debug(f"len {l}, c {c}, max_context_tokens {self.max_context_tokens}")
|
||||||
|
if t == 0:
|
||||||
|
break
|
||||||
|
# logger.debug(f"len {l}, c {c}, max_context_tokens {self.max_context_tokens}")
|
||||||
|
logger.info(f"Context after trim: {self.context.text} (len: {l})")
|
||||||
|
|
||||||
|
|
||||||
|
def logits(self, tokens: torch.Tensor, audio_features: torch.Tensor) -> torch.Tensor:
|
||||||
|
if self.cfg.decoder_type == "greedy":
|
||||||
|
logit = self.model.decoder(tokens, audio_features, kv_cache=self.kv_cache)
|
||||||
|
else:
|
||||||
|
logger.debug(f"Logits shape: {tokens.shape}")
|
||||||
|
logit = self.inference.logits(tokens, audio_features)
|
||||||
|
return logit
|
||||||
|
|
||||||
|
|
||||||
|
def refresh_segment(self, complete=False):
|
||||||
|
|
||||||
|
logger.debug("Refreshing segment:")
|
||||||
|
self.init_tokens()
|
||||||
|
self.last_attend_frame = -self.cfg.rewind_threshold
|
||||||
|
self.detected_language = None
|
||||||
|
self.cumulative_time_offset = 0.0
|
||||||
|
self.init_context()
|
||||||
|
logger.debug(f"Context: {self.context}")
|
||||||
|
if not complete and len(self.segments) > 2:
|
||||||
|
logger.debug("keeping last two segments because they are and it is not complete.")
|
||||||
|
self.segments = self.segments[-2:]
|
||||||
|
else:
|
||||||
|
logger.debug("removing all segments.")
|
||||||
|
self.segments = []
|
||||||
|
self.log_segments += 1
|
||||||
|
|
||||||
|
|
||||||
|
def fire_at_boundary(self, chunked_encoder_feature: torch.Tensor):
|
||||||
|
if self.always_fire: return True
|
||||||
|
if self.never_fire: return False
|
||||||
|
return fire_at_boundary(chunked_encoder_feature, self.CIFLinear)
|
||||||
|
|
||||||
|
|
||||||
|
def _current_tokens(self):
|
||||||
|
|
||||||
|
toks = self.tokens
|
||||||
|
# very first infer: duplicate start of seq to beam_size
|
||||||
|
if toks[0].shape[0] == 1:
|
||||||
|
toks[0] = toks[0].repeat_interleave(self.cfg.beam_size,dim=0)
|
||||||
|
|
||||||
|
if not self.context.is_empty():
|
||||||
|
context_toks = self.context.as_tensor_beam(self.cfg.beam_size, device=self.model.device)
|
||||||
|
toks = [context_toks] + toks
|
||||||
|
|
||||||
|
# make it one tensor
|
||||||
|
if len(toks) > 1:
|
||||||
|
current_tokens = torch.cat(toks, dim=1)
|
||||||
|
else:
|
||||||
|
current_tokens = toks[0]
|
||||||
|
logger.debug("debug print current_tokens:")
|
||||||
|
self.debug_print_tokens(current_tokens)
|
||||||
|
return current_tokens
|
||||||
|
|
||||||
|
|
||||||
|
def debug_print_tokens(self, tokens):
|
||||||
|
for i in range(self.cfg.beam_size):
|
||||||
|
logger.debug(self.tokenizer.decode_with_timestamps(tokens[i].tolist()))
|
||||||
|
|
||||||
|
### audio buffer
|
||||||
|
|
||||||
|
def segments_len(self):
|
||||||
|
segments_len = sum(s.shape[0] for s in self.segments) / 16000
|
||||||
|
return segments_len
|
||||||
|
|
||||||
|
def _apply_minseglen(self):
|
||||||
|
segments_len = self.segments_len()
|
||||||
|
# wait for long enough audio to start
|
||||||
|
if segments_len < self.cfg.audio_min_len:
|
||||||
|
logger.debug("waiting for next segment")
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
def insert_audio(self, segment=None):
|
||||||
|
if segment is not None:
|
||||||
|
self.segments.append(segment)
|
||||||
|
|
||||||
|
removed_len = 0
|
||||||
|
# len of audio is bigger than buffer_len. Going to remove the first segment
|
||||||
|
segments_len = self.segments_len()
|
||||||
|
while len(self.segments) > 1 and segments_len > self.cfg.audio_max_len:
|
||||||
|
removed_len = self.segments[0].shape[0] / 16000
|
||||||
|
segments_len -= removed_len
|
||||||
|
self.last_attend_frame -= int(TOKENS_PER_SECOND*removed_len)
|
||||||
|
self.cumulative_time_offset += removed_len # Track cumulative time removed
|
||||||
|
self.segments = self.segments[1:]
|
||||||
|
logger.debug(f"remove segments: {len(self.segments)} {len(self.tokens)}, cumulative offset: {self.cumulative_time_offset:.2f}s")
|
||||||
|
if len(self.tokens) > 1:
|
||||||
|
self.context.append_token_ids(self.tokens[1][0,:])
|
||||||
|
self.tokens = [self.initial_tokens] + self.tokens[2:]
|
||||||
|
return removed_len
|
||||||
|
|
||||||
|
def _clean_cache(self):
|
||||||
|
'''clean the cache that stores the attention matrices and kv_cache.
|
||||||
|
It must be called every time after generation with the model.'''
|
||||||
|
# cleaning cache
|
||||||
|
self.dec_attns = []
|
||||||
|
self.kv_cache = {}
|
||||||
|
if self.decoder_type == "beam":
|
||||||
|
self.inference.kv_cache = self.kv_cache
|
||||||
|
self.token_decoder.reset()
|
||||||
|
|
||||||
|
@torch.no_grad()
|
||||||
|
def lang_id(self, encoder_features):
|
||||||
|
"""Language detection from encoder features.
|
||||||
|
This code is trimmed and copy-pasted from whisper.decoding.detect_language .
|
||||||
|
"""
|
||||||
|
|
||||||
|
# forward pass using a single token, startoftranscript
|
||||||
|
n_audio = encoder_features.shape[0]
|
||||||
|
x = torch.tensor([[self.tokenizer.sot]] * n_audio).to(self.model.device) # [n_audio, 1]
|
||||||
|
logits = self.model.logits(x, encoder_features)[:, 0]
|
||||||
|
|
||||||
|
# collect detected languages; suppress all non-language tokens
|
||||||
|
mask = torch.ones(logits.shape[-1], dtype=torch.bool)
|
||||||
|
mask[list(self.tokenizer.all_language_tokens)] = False
|
||||||
|
logits[:, mask] = -np.inf
|
||||||
|
language_tokens = logits.argmax(dim=-1)
|
||||||
|
language_token_probs = logits.softmax(dim=-1).cpu()
|
||||||
|
language_probs = [
|
||||||
|
{
|
||||||
|
c: language_token_probs[i, j].item()
|
||||||
|
for j, c in zip(self.tokenizer.all_language_tokens, self.tokenizer.all_language_codes)
|
||||||
|
}
|
||||||
|
for i in range(n_audio)
|
||||||
|
]
|
||||||
|
|
||||||
|
single = encoder_features.ndim == 2
|
||||||
|
if single:
|
||||||
|
language_tokens = language_tokens[0]
|
||||||
|
language_probs = language_probs[0]
|
||||||
|
|
||||||
|
self._clean_cache()
|
||||||
|
return language_tokens, language_probs
|
||||||
|
|
||||||
|
### transcription / translation
|
||||||
|
|
||||||
|
@torch.no_grad()
|
||||||
|
def infer(self, is_last=False):
|
||||||
|
new_segment = True
|
||||||
|
if len(self.segments) == 0:
|
||||||
|
logger.debug("No segments, nothing to do")
|
||||||
|
return [], {}
|
||||||
|
if not self._apply_minseglen():
|
||||||
|
logger.debug(f"applied minseglen {self.cfg.audio_min_len} > {self.segments_len()}.")
|
||||||
|
input_segments = torch.cat(self.segments, dim=0)
|
||||||
|
return [], {}
|
||||||
|
|
||||||
|
# input_segments is concatenation of audio, it's one array
|
||||||
|
if len(self.segments) > 1:
|
||||||
|
input_segments = torch.cat(self.segments, dim=0)
|
||||||
|
else:
|
||||||
|
input_segments = self.segments[0]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# mel + padding to 30s
|
||||||
|
mel_padded = log_mel_spectrogram(input_segments, n_mels=self.model.dims.n_mels, padding=N_SAMPLES,
|
||||||
|
device=self.model.device).unsqueeze(0)
|
||||||
|
# trim to 3000
|
||||||
|
mel = pad_or_trim(mel_padded, N_FRAMES)
|
||||||
|
|
||||||
|
# the len of actual audio
|
||||||
|
content_mel_len = int((mel_padded.shape[2] - mel.shape[2])/2)
|
||||||
|
|
||||||
|
# encode
|
||||||
|
encoder_feature = self.model.encoder(mel)
|
||||||
|
|
||||||
|
# logger.debug(f"Encoder feature shape: {encoder_feature.shape}")
|
||||||
|
# if mel.shape[-2:] != (self.model.dims.n_audio_ctx, self.model.dims.n_audio_state):
|
||||||
|
# logger.debug("mel ")
|
||||||
|
if self.cfg.language == "auto" and self.detected_language is None:
|
||||||
|
language_tokens, language_probs = self.lang_id(encoder_feature)
|
||||||
|
logger.debug(f"Language tokens: {language_tokens}, probs: {language_probs}")
|
||||||
|
top_lan, p = max(language_probs[0].items(), key=lambda x: x[1])
|
||||||
|
logger.info(f"Detected language: {top_lan} with p={p:.4f}")
|
||||||
|
#self.tokenizer.language = top_lan
|
||||||
|
#self.tokenizer.__post_init__()
|
||||||
|
self.create_tokenizer(top_lan)
|
||||||
|
self.detected_language = top_lan
|
||||||
|
self.init_tokens()
|
||||||
|
logger.info(f"Tokenizer language: {self.tokenizer.language}, {self.tokenizer.sot_sequence_including_notimestamps}")
|
||||||
|
|
||||||
|
self.trim_context()
|
||||||
|
current_tokens = self._current_tokens()
|
||||||
|
#
|
||||||
|
fire_detected = self.fire_at_boundary(encoder_feature[:, :content_mel_len, :])
|
||||||
|
|
||||||
|
|
||||||
|
####################### Decoding loop
|
||||||
|
logger.info("Decoding loop starts\n")
|
||||||
|
|
||||||
|
sum_logprobs = torch.zeros(self.cfg.beam_size, device=mel.device)
|
||||||
|
completed = False
|
||||||
|
|
||||||
|
attn_of_alignment_heads = None
|
||||||
|
most_attended_frame = None
|
||||||
|
|
||||||
|
token_len_before_decoding = current_tokens.shape[1]
|
||||||
|
|
||||||
|
generation_progress = []
|
||||||
|
generation = {
|
||||||
|
"starting_tokens": BeamTokens(current_tokens[0,:].clone(), self.cfg.beam_size),
|
||||||
|
"token_len_before_decoding": token_len_before_decoding,
|
||||||
|
#"fire_detected": fire_detected,
|
||||||
|
"frames_len": content_mel_len,
|
||||||
|
"frames_threshold": 4 if is_last else self.cfg.frame_threshold,
|
||||||
|
|
||||||
|
# to be filled later
|
||||||
|
"logits_starting": None,
|
||||||
|
|
||||||
|
# to be filled later
|
||||||
|
"no_speech_prob": None,
|
||||||
|
"no_speech": False,
|
||||||
|
|
||||||
|
# to be filled in the loop
|
||||||
|
"progress": generation_progress,
|
||||||
|
}
|
||||||
|
while not completed and current_tokens.shape[1] < self.max_text_len: # bos is 3 tokens
|
||||||
|
generation_progress_loop = []
|
||||||
|
|
||||||
|
if new_segment:
|
||||||
|
tokens_for_logits = current_tokens
|
||||||
|
else:
|
||||||
|
# only need to use the last token except in the first forward pass
|
||||||
|
tokens_for_logits = current_tokens[:,-1:]
|
||||||
|
|
||||||
|
logits = self.logits(tokens_for_logits, encoder_feature) # B, len(tokens), token dict size
|
||||||
|
if new_segment:
|
||||||
|
generation["logits_starting"] = Logits(logits[:,:,:])
|
||||||
|
|
||||||
|
if new_segment and self.tokenizer.no_speech is not None:
|
||||||
|
probs_at_sot = logits[:, self.sot_index, :].float().softmax(dim=-1)
|
||||||
|
no_speech_probs = probs_at_sot[:, self.tokenizer.no_speech].tolist()
|
||||||
|
generation["no_speech_prob"] = no_speech_probs[0]
|
||||||
|
if no_speech_probs[0] > self.cfg.nonspeech_prob:
|
||||||
|
generation["no_speech"] = True
|
||||||
|
logger.info("no speech, stop")
|
||||||
|
break
|
||||||
|
|
||||||
|
logits = logits[:, -1, :] # logits for the last token
|
||||||
|
generation_progress_loop.append(("logits_before_suppress",Logits(logits)))
|
||||||
|
|
||||||
|
# supress blank tokens only at the beginning of the segment
|
||||||
|
if new_segment:
|
||||||
|
logits[:, self.tokenizer.encode(" ") + [self.tokenizer.eot]] = -np.inf
|
||||||
|
new_segment = False
|
||||||
|
self.suppress_tokens(logits)
|
||||||
|
#generation_progress_loop.append(("logits_after_suppres",BeamLogits(logits[0,:].clone(), self.cfg.beam_size)))
|
||||||
|
generation_progress_loop.append(("logits_after_suppress",Logits(logits)))
|
||||||
|
|
||||||
|
current_tokens, completed = self.token_decoder.update(current_tokens, logits, sum_logprobs)
|
||||||
|
generation_progress_loop.append(("beam_tokens",Tokens(current_tokens[:,-1].clone())))
|
||||||
|
generation_progress_loop.append(("sum_logprobs",sum_logprobs.tolist()))
|
||||||
|
generation_progress_loop.append(("completed",completed))
|
||||||
|
|
||||||
|
logger.debug(f"Decoding completed: {completed}, sum_logprobs: {sum_logprobs.tolist()}, tokens: ")
|
||||||
|
self.debug_print_tokens(current_tokens)
|
||||||
|
|
||||||
|
|
||||||
|
# if self.decoder_type == "beam":
|
||||||
|
# logger.debug(f"Finished sequences: {self.token_decoder.finished_sequences}")
|
||||||
|
|
||||||
|
# logprobs = F.log_softmax(logits.float(), dim=-1)
|
||||||
|
# idx = 0
|
||||||
|
# logger.debug(f"Beam search topk: {logprobs[idx].topk(self.cfg.beam_size + 1)}")
|
||||||
|
# logger.debug(f"Greedy search argmax: {logits.argmax(dim=-1)}")
|
||||||
|
# if completed:
|
||||||
|
# self.debug_print_tokens(current_tokens)
|
||||||
|
|
||||||
|
# logger.debug("decode stopped because decoder completed")
|
||||||
|
|
||||||
|
attn_of_alignment_heads = [[] for _ in range(self.num_align_heads)]
|
||||||
|
for i, attn_mat in enumerate(self.dec_attns):
|
||||||
|
layer_rank = int(i % len(self.model.decoder.blocks))
|
||||||
|
align_heads_in_layer = self.align_source.get(layer_rank, [])
|
||||||
|
if len(align_heads_in_layer) == 0:
|
||||||
|
continue
|
||||||
|
for align_head_rank, head_id in align_heads_in_layer:
|
||||||
|
if self.cfg.beam_size == 1:
|
||||||
|
a = attn_mat[head_id, :, :]
|
||||||
|
a = a.unsqueeze(0)
|
||||||
|
else:
|
||||||
|
a = attn_mat[:, head_id, :, :]
|
||||||
|
attn_of_alignment_heads[align_head_rank].append(a)
|
||||||
|
tmp = []
|
||||||
|
for mat in attn_of_alignment_heads:
|
||||||
|
t = torch.cat(mat, dim=1)
|
||||||
|
tmp.append(t)
|
||||||
|
attn_of_alignment_heads = torch.stack(tmp, dim=1)
|
||||||
|
# logger.debug(str(attn_of_alignment_heads.shape) + " tttady")
|
||||||
|
std, mean = torch.std_mean(attn_of_alignment_heads, dim=-2, keepdim=True, unbiased=False)
|
||||||
|
attn_of_alignment_heads = (attn_of_alignment_heads - mean) / std
|
||||||
|
attn_of_alignment_heads = median_filter(attn_of_alignment_heads, 7) # from whisper.timing
|
||||||
|
attn_of_alignment_heads = attn_of_alignment_heads.mean(dim=1)
|
||||||
|
# logger.debug(str(attn_of_alignment_heads.shape) + " po mean")
|
||||||
|
attn_of_alignment_heads = attn_of_alignment_heads[:,:, :content_mel_len]
|
||||||
|
# logger.debug(str(attn_of_alignment_heads.shape) + " pak ")
|
||||||
|
|
||||||
|
# for each beam, the most attended frame is:
|
||||||
|
most_attended_frames = torch.argmax(attn_of_alignment_heads[:,-1,:], dim=-1)
|
||||||
|
generation_progress_loop.append(("most_attended_frames",most_attended_frames.clone().tolist()))
|
||||||
|
|
||||||
|
# Calculate absolute timestamps accounting for cumulative offset
|
||||||
|
absolute_timestamps = [(frame * 0.02 + self.cumulative_time_offset) for frame in most_attended_frames.tolist()]
|
||||||
|
generation_progress_loop.append(("absolute_timestamps", absolute_timestamps))
|
||||||
|
|
||||||
|
logger.debug(str(most_attended_frames.tolist()) + " most att frames")
|
||||||
|
logger.debug(f"Absolute timestamps: {absolute_timestamps} (offset: {self.cumulative_time_offset:.2f}s)")
|
||||||
|
|
||||||
|
most_attended_frame = most_attended_frames[0].item()
|
||||||
|
|
||||||
|
|
||||||
|
generation_progress.append(dict(generation_progress_loop))
|
||||||
|
logger.debug("current tokens" + str(current_tokens.shape))
|
||||||
|
if completed:
|
||||||
|
# # stripping the last token, the eot
|
||||||
|
current_tokens = current_tokens[:, :-1]
|
||||||
|
break
|
||||||
|
|
||||||
|
# for some rare cases where the attention fails
|
||||||
|
if not is_last and self.last_attend_frame - most_attended_frame > self.cfg.rewind_threshold:
|
||||||
|
# TODO: check this
|
||||||
|
if current_tokens.shape[1] > 1 and current_tokens[0, -2] >= DEC_PAD:
|
||||||
|
logger.debug("ommit rewinding from special tokens")
|
||||||
|
self.last_attend_frame = most_attended_frame
|
||||||
|
else:
|
||||||
|
logger.debug(
|
||||||
|
f"[rewind detected] current attention pos: {most_attended_frame}, "
|
||||||
|
f"last attention pos: {self.last_attend_frame}; omit this segment")
|
||||||
|
self.last_attend_frame = -self.cfg.rewind_threshold
|
||||||
|
current_tokens = torch.cat(self.tokens, dim=1) if len(self.tokens) > 0 else self.tokens[0]
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
self.last_attend_frame = most_attended_frame
|
||||||
|
|
||||||
|
if content_mel_len - most_attended_frame <= (4 if is_last else self.cfg.frame_threshold):
|
||||||
|
logger.debug(f"attention reaches the end: {most_attended_frame}/{content_mel_len}")
|
||||||
|
# stripping the last token, the one that is attended too close to the end
|
||||||
|
current_tokens = current_tokens[:, :-1]
|
||||||
|
break
|
||||||
|
|
||||||
|
# debug print
|
||||||
|
for i in range(self.cfg.beam_size):
|
||||||
|
logger.debug("attn: {}, current pos: {}, current token: {}({})".format(
|
||||||
|
attn_of_alignment_heads.shape if attn_of_alignment_heads is not None else None,
|
||||||
|
most_attended_frames[i],
|
||||||
|
current_tokens[i, -1].item(),
|
||||||
|
self.tokenizer.decode([current_tokens[i, -1].item()])
|
||||||
|
))
|
||||||
|
|
||||||
|
# for k,v in generation.items():
|
||||||
|
# print(k,v,file=sys.stderr)
|
||||||
|
# for x in generation_progress:
|
||||||
|
# for y in x.items():
|
||||||
|
# print("\t\t",*y,file=sys.stderr)
|
||||||
|
# print("\t","----", file=sys.stderr)
|
||||||
|
# print("\t", "end of generation_progress_loop", file=sys.stderr)
|
||||||
|
# sys.exit(1)
|
||||||
|
####################### End of decoding loop
|
||||||
|
|
||||||
|
logger.info("End of decoding loop")
|
||||||
|
|
||||||
|
# if attn_of_alignment_heads is not None:
|
||||||
|
# seg_len = int(segment.shape[0] / 16000 * TOKENS_PER_SECOND)
|
||||||
|
|
||||||
|
# # Lets' now consider only the top hypothesis in the beam search
|
||||||
|
# top_beam_attn_of_alignment_heads = attn_of_alignment_heads[0]
|
||||||
|
|
||||||
|
# # debug print: how is the new token attended?
|
||||||
|
# new_token_attn = top_beam_attn_of_alignment_heads[token_len_before_decoding:, -seg_len:]
|
||||||
|
# logger.debug(f"New token attention shape: {new_token_attn.shape}")
|
||||||
|
# if new_token_attn.shape[0] == 0: # it's not attended in the current audio segment
|
||||||
|
# logger.debug("no token generated")
|
||||||
|
# else: # it is, and the max attention is:
|
||||||
|
# new_token_max_attn, _ = new_token_attn.max(dim=-1)
|
||||||
|
# logger.debug(f"segment max attention: {new_token_max_attn.mean().item()/len(self.segments)}")
|
||||||
|
|
||||||
|
|
||||||
|
# let's now operate only with the top beam hypothesis
|
||||||
|
tokens_to_split = current_tokens[0, token_len_before_decoding:]
|
||||||
|
if fire_detected or is_last:
|
||||||
|
new_hypothesis = tokens_to_split.flatten().tolist()
|
||||||
|
else:
|
||||||
|
# going to truncate the tokens after the last space
|
||||||
|
split_words, split_tokens = self.tokenizer.split_to_word_tokens(tokens_to_split.tolist())
|
||||||
|
generation["result"] = {"split_words": split_words[:-1], "split_tokens": split_tokens[:-1]}
|
||||||
|
generation["result_truncated"] = {"split_words": split_words[-1:], "split_tokens": split_tokens[-1:]}
|
||||||
|
|
||||||
|
# text_to_split = self.tokenizer.decode(tokens_to_split)
|
||||||
|
# logger.debug(f"text_to_split: {text_to_split}")
|
||||||
|
# logger.debug("text at current step: {}".format(text_to_split.replace(" ", "<space>")))
|
||||||
|
# text_before_space = " ".join(text_to_split.split(" ")[:-1])
|
||||||
|
# logger.debug("before the last space: {}".format(text_before_space.replace(" ", "<space>")))
|
||||||
|
if len(split_words) > 1:
|
||||||
|
new_hypothesis = [i for sublist in split_tokens[:-1] for i in sublist]
|
||||||
|
else:
|
||||||
|
new_hypothesis = []
|
||||||
|
|
||||||
|
|
||||||
|
### new hypothesis
|
||||||
|
logger.debug(f"new_hypothesis: {new_hypothesis}")
|
||||||
|
new_tokens = torch.tensor([new_hypothesis], dtype=torch.long).repeat_interleave(self.cfg.beam_size, dim=0).to(
|
||||||
|
device=self.model.device,
|
||||||
|
)
|
||||||
|
self.tokens.append(new_tokens)
|
||||||
|
# TODO: test if this is redundant or not
|
||||||
|
# ret = ret[ret<DEC_PAD]
|
||||||
|
|
||||||
|
logger.info(f"Output: {self.tokenizer.decode(new_hypothesis)}")
|
||||||
|
|
||||||
|
self._clean_cache()
|
||||||
|
|
||||||
|
return new_hypothesis, generation
|
||||||
73
whisperlivekit/simul_whisper/token_buffer.py
Normal file
73
whisperlivekit/simul_whisper/token_buffer.py
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
import torch
|
||||||
|
import sys
|
||||||
|
class TokenBuffer:
|
||||||
|
|
||||||
|
def __init__(self, text="", tokenizer=None, device=None, prefix_token_ids=[]):
|
||||||
|
self.text = text
|
||||||
|
self.prefix_token_ids = prefix_token_ids
|
||||||
|
self.tokenizer = tokenizer
|
||||||
|
self.device = device
|
||||||
|
|
||||||
|
def as_token_ids(self, tokenizer=None):
|
||||||
|
|
||||||
|
if tokenizer is None:
|
||||||
|
tokenizer = self.tokenizer
|
||||||
|
if tokenizer is None:
|
||||||
|
raise ValueError("Tokenizer is not set.")
|
||||||
|
return self.prefix_token_ids + tokenizer.encode(self.text)
|
||||||
|
|
||||||
|
def as_tensor(self, device=None):
|
||||||
|
if device is None:
|
||||||
|
device = self.device
|
||||||
|
if device is None:
|
||||||
|
raise ValueError("Device is not set.")
|
||||||
|
tok_ids = self.as_token_ids()
|
||||||
|
return torch.tensor(tok_ids,
|
||||||
|
dtype=torch.long, device=device).unsqueeze(0)
|
||||||
|
|
||||||
|
def as_tensor_beam(self, beam, device=None):
|
||||||
|
t = self.as_tensor(device=device)
|
||||||
|
return t.repeat_interleave(beam, dim=0)
|
||||||
|
|
||||||
|
|
||||||
|
def as_text(self):
|
||||||
|
return self.text
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def empty(*a, **kw):
|
||||||
|
return TokenBuffer(*a,**kw)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def from_text(text, *a, **kw):
|
||||||
|
return TokenBuffer(*a, text=text, **kw)
|
||||||
|
|
||||||
|
def is_empty(self):
|
||||||
|
return self.text is None or self.text == ""
|
||||||
|
|
||||||
|
def trim_words(self, num=1, after=0):
|
||||||
|
'''
|
||||||
|
num: how many words to trim from the beginning
|
||||||
|
after: how many characters to skip (length of the static prompt)
|
||||||
|
'''
|
||||||
|
tokenizer = self.tokenizer
|
||||||
|
assert tokenizer is not None, "Tokenizer is not set."
|
||||||
|
|
||||||
|
ids = tokenizer.encode(self.text[after:])
|
||||||
|
words, wids = self.tokenizer.split_to_word_tokens(ids)
|
||||||
|
# print(words, file=sys.stderr)
|
||||||
|
# print(wids, file=sys.stderr)
|
||||||
|
if not words:
|
||||||
|
return 0
|
||||||
|
self.text = self.text[:after] + "".join(words[num:])
|
||||||
|
return sum(len(wi) for wi in wids[:num])
|
||||||
|
|
||||||
|
def append_token_ids(self, token_ids):
|
||||||
|
tokenizer = self.tokenizer
|
||||||
|
assert tokenizer is not None, "Tokenizer is not set."
|
||||||
|
self.text += self.tokenizer.decode(token_ids)
|
||||||
|
|
||||||
|
def as_split_word_tokens(self):
|
||||||
|
tokenizer = self.tokenizer
|
||||||
|
assert tokenizer is not None, "Tokenizer is not set."
|
||||||
|
ids = tokenizer.encode(self.text)
|
||||||
|
return tokenizer.split_to_word_tokens(ids)
|
||||||
160
whisperlivekit/simul_whisper/whisper/__init__.py
Normal file
160
whisperlivekit/simul_whisper/whisper/__init__.py
Normal file
@@ -0,0 +1,160 @@
|
|||||||
|
import hashlib
|
||||||
|
import io
|
||||||
|
import os
|
||||||
|
import urllib
|
||||||
|
import warnings
|
||||||
|
from typing import List, Optional, Union
|
||||||
|
|
||||||
|
import torch
|
||||||
|
from tqdm import tqdm
|
||||||
|
|
||||||
|
from .audio import load_audio, log_mel_spectrogram, pad_or_trim
|
||||||
|
from .decoding import DecodingOptions, DecodingResult, decode, detect_language
|
||||||
|
from .model import ModelDimensions, Whisper
|
||||||
|
from .transcribe import transcribe
|
||||||
|
from .version import __version__
|
||||||
|
|
||||||
|
_MODELS = {
|
||||||
|
"tiny.en": "https://openaipublic.azureedge.net/main/whisper/models/d3dd57d32accea0b295c96e26691aa14d8822fac7d9d27d5dc00b4ca2826dd03/tiny.en.pt",
|
||||||
|
"tiny": "https://openaipublic.azureedge.net/main/whisper/models/65147644a518d12f04e32d6f3b26facc3f8dd46e5390956a9424a650c0ce22b9/tiny.pt",
|
||||||
|
"base.en": "https://openaipublic.azureedge.net/main/whisper/models/25a8566e1d0c1e2231d1c762132cd20e0f96a85d16145c3a00adf5d1ac670ead/base.en.pt",
|
||||||
|
"base": "https://openaipublic.azureedge.net/main/whisper/models/ed3a0b6b1c0edf879ad9b11b1af5a0e6ab5db9205f891f668f8b0e6c6326e34e/base.pt",
|
||||||
|
"small.en": "https://openaipublic.azureedge.net/main/whisper/models/f953ad0fd29cacd07d5a9eda5624af0f6bcf2258be67c92b79389873d91e0872/small.en.pt",
|
||||||
|
"small": "https://openaipublic.azureedge.net/main/whisper/models/9ecf779972d90ba49c06d968637d720dd632c55bbf19d441fb42bf17a411e794/small.pt",
|
||||||
|
"medium.en": "https://openaipublic.azureedge.net/main/whisper/models/d7440d1dc186f76616474e0ff0b3b6b879abc9d1a4926b7adfa41db2d497ab4f/medium.en.pt",
|
||||||
|
"medium": "https://openaipublic.azureedge.net/main/whisper/models/345ae4da62f9b3d59415adc60127b97c714f32e89e936602e85993674d08dcb1/medium.pt",
|
||||||
|
"large-v1": "https://openaipublic.azureedge.net/main/whisper/models/e4b87e7e0bf463eb8e6956e646f1e277e901512310def2c24bf0e11bd3c28e9a/large-v1.pt",
|
||||||
|
"large-v2": "https://openaipublic.azureedge.net/main/whisper/models/81f7c96c852ee8fc832187b0132e569d6c3065a3252ed18e56effd0b6a73e524/large-v2.pt",
|
||||||
|
"large-v3": "https://openaipublic.azureedge.net/main/whisper/models/e5b1a55b89c1367dacf97e3e19bfd829a01529dbfdeefa8caeb59b3f1b81dadb/large-v3.pt",
|
||||||
|
"large": "https://openaipublic.azureedge.net/main/whisper/models/e5b1a55b89c1367dacf97e3e19bfd829a01529dbfdeefa8caeb59b3f1b81dadb/large-v3.pt",
|
||||||
|
"large-v3-turbo": "https://openaipublic.azureedge.net/main/whisper/models/aff26ae408abcba5fbf8813c21e62b0941638c5f6eebfb145be0c9839262a19a/large-v3-turbo.pt",
|
||||||
|
"turbo": "https://openaipublic.azureedge.net/main/whisper/models/aff26ae408abcba5fbf8813c21e62b0941638c5f6eebfb145be0c9839262a19a/large-v3-turbo.pt",
|
||||||
|
}
|
||||||
|
|
||||||
|
# base85-encoded (n_layers, n_heads) boolean arrays indicating the cross-attention heads that are
|
||||||
|
# highly correlated to the word-level timing, i.e. the alignment between audio and text tokens.
|
||||||
|
_ALIGNMENT_HEADS = {
|
||||||
|
"tiny.en": b"ABzY8J1N>@0{>%R00Bk>$p{7v037`oCl~+#00",
|
||||||
|
"tiny": b"ABzY8bu8Lr0{>%RKn9Fp%m@SkK7Kt=7ytkO",
|
||||||
|
"base.en": b"ABzY8;40c<0{>%RzzG;p*o+Vo09|#PsxSZm00",
|
||||||
|
"base": b"ABzY8KQ!870{>%RzyTQH3`Q^yNP!>##QT-<FaQ7m",
|
||||||
|
"small.en": b"ABzY8>?_)10{>%RpeA61k&I|OI3I$65C{;;pbCHh0B{qLQ;+}v00",
|
||||||
|
"small": b"ABzY8DmU6=0{>%Rpa?J`kvJ6qF(V^F86#Xh7JUGMK}P<N0000",
|
||||||
|
"medium.en": b"ABzY8usPae0{>%R7<zz_OvQ{)4kMa0BMw6u5rT}kRKX;$NfYBv00*Hl@qhsU00",
|
||||||
|
"medium": b"ABzY8B0Jh+0{>%R7}kK1fFL7w6%<-Pf*t^=N)Qr&0RR9",
|
||||||
|
"large-v1": b"ABzY8r9j$a0{>%R7#4sLmoOs{s)o3~84-RPdcFk!JR<kSfC2yj",
|
||||||
|
"large-v2": b"ABzY8zd+h!0{>%R7=D0pU<_bnWW*tkYAhobTNnu$jnkEkXqp)j;w1Tzk)UH3X%SZd&fFZ2fC2yj",
|
||||||
|
"large-v3": b"ABzY8gWO1E0{>%R7(9S+Kn!D~%ngiGaR?*L!iJG9p-nab0JQ=-{D1-g00",
|
||||||
|
"large": b"ABzY8gWO1E0{>%R7(9S+Kn!D~%ngiGaR?*L!iJG9p-nab0JQ=-{D1-g00",
|
||||||
|
"large-v3-turbo": b"ABzY8j^C+e0{>%RARaKHP%t(lGR*)0g!tONPyhe`",
|
||||||
|
"turbo": b"ABzY8j^C+e0{>%RARaKHP%t(lGR*)0g!tONPyhe`",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def _download(url: str, root: str, in_memory: bool) -> Union[bytes, str]:
|
||||||
|
os.makedirs(root, exist_ok=True)
|
||||||
|
|
||||||
|
expected_sha256 = url.split("/")[-2]
|
||||||
|
download_target = os.path.join(root, os.path.basename(url))
|
||||||
|
|
||||||
|
if os.path.exists(download_target) and not os.path.isfile(download_target):
|
||||||
|
raise RuntimeError(f"{download_target} exists and is not a regular file")
|
||||||
|
|
||||||
|
if os.path.isfile(download_target):
|
||||||
|
with open(download_target, "rb") as f:
|
||||||
|
model_bytes = f.read()
|
||||||
|
if hashlib.sha256(model_bytes).hexdigest() == expected_sha256:
|
||||||
|
return model_bytes if in_memory else download_target
|
||||||
|
else:
|
||||||
|
warnings.warn(
|
||||||
|
f"{download_target} exists, but the SHA256 checksum does not match; re-downloading the file"
|
||||||
|
)
|
||||||
|
|
||||||
|
with urllib.request.urlopen(url) as source, open(download_target, "wb") as output:
|
||||||
|
with tqdm(
|
||||||
|
total=int(source.info().get("Content-Length")),
|
||||||
|
ncols=80,
|
||||||
|
unit="iB",
|
||||||
|
unit_scale=True,
|
||||||
|
unit_divisor=1024,
|
||||||
|
) as loop:
|
||||||
|
while True:
|
||||||
|
buffer = source.read(8192)
|
||||||
|
if not buffer:
|
||||||
|
break
|
||||||
|
|
||||||
|
output.write(buffer)
|
||||||
|
loop.update(len(buffer))
|
||||||
|
|
||||||
|
model_bytes = open(download_target, "rb").read()
|
||||||
|
if hashlib.sha256(model_bytes).hexdigest() != expected_sha256:
|
||||||
|
raise RuntimeError(
|
||||||
|
"Model has been downloaded but the SHA256 checksum does not not match. Please retry loading the model."
|
||||||
|
)
|
||||||
|
|
||||||
|
return model_bytes if in_memory else download_target
|
||||||
|
|
||||||
|
|
||||||
|
def available_models() -> List[str]:
|
||||||
|
"""Returns the names of available models"""
|
||||||
|
return list(_MODELS.keys())
|
||||||
|
|
||||||
|
|
||||||
|
def load_model(
|
||||||
|
name: str,
|
||||||
|
device: Optional[Union[str, torch.device]] = None,
|
||||||
|
download_root: str = None,
|
||||||
|
in_memory: bool = False,
|
||||||
|
) -> Whisper:
|
||||||
|
"""
|
||||||
|
Load a Whisper ASR model
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
name : str
|
||||||
|
one of the official model names listed by `whisper.available_models()`, or
|
||||||
|
path to a model checkpoint containing the model dimensions and the model state_dict.
|
||||||
|
device : Union[str, torch.device]
|
||||||
|
the PyTorch device to put the model into
|
||||||
|
download_root: str
|
||||||
|
path to download the model files; by default, it uses "~/.cache/whisper"
|
||||||
|
in_memory: bool
|
||||||
|
whether to preload the model weights into host memory
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
model : Whisper
|
||||||
|
The Whisper ASR model instance
|
||||||
|
"""
|
||||||
|
|
||||||
|
if device is None:
|
||||||
|
device = "cuda" if torch.cuda.is_available() else "cpu"
|
||||||
|
if download_root is None:
|
||||||
|
default = os.path.join(os.path.expanduser("~"), ".cache")
|
||||||
|
download_root = os.path.join(os.getenv("XDG_CACHE_HOME", default), "whisper")
|
||||||
|
|
||||||
|
if name in _MODELS:
|
||||||
|
checkpoint_file = _download(_MODELS[name], download_root, in_memory)
|
||||||
|
alignment_heads = _ALIGNMENT_HEADS[name]
|
||||||
|
elif os.path.isfile(name):
|
||||||
|
checkpoint_file = open(name, "rb").read() if in_memory else name
|
||||||
|
alignment_heads = None
|
||||||
|
else:
|
||||||
|
raise RuntimeError(
|
||||||
|
f"Model {name} not found; available models = {available_models()}"
|
||||||
|
)
|
||||||
|
|
||||||
|
with (
|
||||||
|
io.BytesIO(checkpoint_file) if in_memory else open(checkpoint_file, "rb")
|
||||||
|
) as fp:
|
||||||
|
checkpoint = torch.load(fp, map_location=device)
|
||||||
|
del checkpoint_file
|
||||||
|
|
||||||
|
dims = ModelDimensions(**checkpoint["dims"])
|
||||||
|
model = Whisper(dims)
|
||||||
|
model.load_state_dict(checkpoint["model_state_dict"])
|
||||||
|
|
||||||
|
if alignment_heads is not None:
|
||||||
|
model.set_alignment_heads(alignment_heads)
|
||||||
|
|
||||||
|
return model.to(device)
|
||||||
3
whisperlivekit/simul_whisper/whisper/__main__.py
Normal file
3
whisperlivekit/simul_whisper/whisper/__main__.py
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
from .transcribe import cli
|
||||||
|
|
||||||
|
cli()
|
||||||
50256
whisperlivekit/simul_whisper/whisper/assets/gpt2.tiktoken
Normal file
50256
whisperlivekit/simul_whisper/whisper/assets/gpt2.tiktoken
Normal file
File diff suppressed because it is too large
Load Diff
BIN
whisperlivekit/simul_whisper/whisper/assets/mel_filters.npz
Normal file
BIN
whisperlivekit/simul_whisper/whisper/assets/mel_filters.npz
Normal file
Binary file not shown.
50257
whisperlivekit/simul_whisper/whisper/assets/multilingual.tiktoken
Normal file
50257
whisperlivekit/simul_whisper/whisper/assets/multilingual.tiktoken
Normal file
File diff suppressed because it is too large
Load Diff
157
whisperlivekit/simul_whisper/whisper/audio.py
Normal file
157
whisperlivekit/simul_whisper/whisper/audio.py
Normal file
@@ -0,0 +1,157 @@
|
|||||||
|
import os
|
||||||
|
from functools import lru_cache
|
||||||
|
from subprocess import CalledProcessError, run
|
||||||
|
from typing import Optional, Union
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
import torch
|
||||||
|
import torch.nn.functional as F
|
||||||
|
|
||||||
|
from .utils import exact_div
|
||||||
|
|
||||||
|
# hard-coded audio hyperparameters
|
||||||
|
SAMPLE_RATE = 16000
|
||||||
|
N_FFT = 400
|
||||||
|
HOP_LENGTH = 160
|
||||||
|
CHUNK_LENGTH = 30
|
||||||
|
N_SAMPLES = CHUNK_LENGTH * SAMPLE_RATE # 480000 samples in a 30-second chunk
|
||||||
|
N_FRAMES = exact_div(N_SAMPLES, HOP_LENGTH) # 3000 frames in a mel spectrogram input
|
||||||
|
|
||||||
|
N_SAMPLES_PER_TOKEN = HOP_LENGTH * 2 # the initial convolutions has stride 2
|
||||||
|
FRAMES_PER_SECOND = exact_div(SAMPLE_RATE, HOP_LENGTH) # 10ms per audio frame
|
||||||
|
TOKENS_PER_SECOND = exact_div(SAMPLE_RATE, N_SAMPLES_PER_TOKEN) # 20ms per audio token
|
||||||
|
|
||||||
|
|
||||||
|
def load_audio(file: str, sr: int = SAMPLE_RATE):
|
||||||
|
"""
|
||||||
|
Open an audio file and read as mono waveform, resampling as necessary
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
file: str
|
||||||
|
The audio file to open
|
||||||
|
|
||||||
|
sr: int
|
||||||
|
The sample rate to resample the audio if necessary
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
A NumPy array containing the audio waveform, in float32 dtype.
|
||||||
|
"""
|
||||||
|
|
||||||
|
# This launches a subprocess to decode audio while down-mixing
|
||||||
|
# and resampling as necessary. Requires the ffmpeg CLI in PATH.
|
||||||
|
# fmt: off
|
||||||
|
cmd = [
|
||||||
|
"ffmpeg",
|
||||||
|
"-nostdin",
|
||||||
|
"-threads", "0",
|
||||||
|
"-i", file,
|
||||||
|
"-f", "s16le",
|
||||||
|
"-ac", "1",
|
||||||
|
"-acodec", "pcm_s16le",
|
||||||
|
"-ar", str(sr),
|
||||||
|
"-"
|
||||||
|
]
|
||||||
|
# fmt: on
|
||||||
|
try:
|
||||||
|
out = run(cmd, capture_output=True, check=True).stdout
|
||||||
|
except CalledProcessError as e:
|
||||||
|
raise RuntimeError(f"Failed to load audio: {e.stderr.decode()}") from e
|
||||||
|
|
||||||
|
return np.frombuffer(out, np.int16).flatten().astype(np.float32) / 32768.0
|
||||||
|
|
||||||
|
|
||||||
|
def pad_or_trim(array, length: int = N_SAMPLES, *, axis: int = -1):
|
||||||
|
"""
|
||||||
|
Pad or trim the audio array to N_SAMPLES, as expected by the encoder.
|
||||||
|
"""
|
||||||
|
if torch.is_tensor(array):
|
||||||
|
if array.shape[axis] > length:
|
||||||
|
array = array.index_select(
|
||||||
|
dim=axis, index=torch.arange(length, device=array.device)
|
||||||
|
)
|
||||||
|
|
||||||
|
if array.shape[axis] < length:
|
||||||
|
pad_widths = [(0, 0)] * array.ndim
|
||||||
|
pad_widths[axis] = (0, length - array.shape[axis])
|
||||||
|
array = F.pad(array, [pad for sizes in pad_widths[::-1] for pad in sizes])
|
||||||
|
else:
|
||||||
|
if array.shape[axis] > length:
|
||||||
|
array = array.take(indices=range(length), axis=axis)
|
||||||
|
|
||||||
|
if array.shape[axis] < length:
|
||||||
|
pad_widths = [(0, 0)] * array.ndim
|
||||||
|
pad_widths[axis] = (0, length - array.shape[axis])
|
||||||
|
array = np.pad(array, pad_widths)
|
||||||
|
|
||||||
|
return array
|
||||||
|
|
||||||
|
|
||||||
|
@lru_cache(maxsize=None)
|
||||||
|
def mel_filters(device, n_mels: int) -> torch.Tensor:
|
||||||
|
"""
|
||||||
|
load the mel filterbank matrix for projecting STFT into a Mel spectrogram.
|
||||||
|
Allows decoupling librosa dependency; saved using:
|
||||||
|
|
||||||
|
np.savez_compressed(
|
||||||
|
"mel_filters.npz",
|
||||||
|
mel_80=librosa.filters.mel(sr=16000, n_fft=400, n_mels=80),
|
||||||
|
mel_128=librosa.filters.mel(sr=16000, n_fft=400, n_mels=128),
|
||||||
|
)
|
||||||
|
"""
|
||||||
|
assert n_mels in {80, 128}, f"Unsupported n_mels: {n_mels}"
|
||||||
|
|
||||||
|
filters_path = os.path.join(os.path.dirname(__file__), "assets", "mel_filters.npz")
|
||||||
|
with np.load(filters_path, allow_pickle=False) as f:
|
||||||
|
return torch.from_numpy(f[f"mel_{n_mels}"]).to(device)
|
||||||
|
|
||||||
|
|
||||||
|
def log_mel_spectrogram(
|
||||||
|
audio: Union[str, np.ndarray, torch.Tensor],
|
||||||
|
n_mels: int = 80,
|
||||||
|
padding: int = 0,
|
||||||
|
device: Optional[Union[str, torch.device]] = None,
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
Compute the log-Mel spectrogram of
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
audio: Union[str, np.ndarray, torch.Tensor], shape = (*)
|
||||||
|
The path to audio or either a NumPy array or Tensor containing the audio waveform in 16 kHz
|
||||||
|
|
||||||
|
n_mels: int
|
||||||
|
The number of Mel-frequency filters, only 80 and 128 are supported
|
||||||
|
|
||||||
|
padding: int
|
||||||
|
Number of zero samples to pad to the right
|
||||||
|
|
||||||
|
device: Optional[Union[str, torch.device]]
|
||||||
|
If given, the audio tensor is moved to this device before STFT
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
torch.Tensor, shape = (n_mels, n_frames)
|
||||||
|
A Tensor that contains the Mel spectrogram
|
||||||
|
"""
|
||||||
|
if not torch.is_tensor(audio):
|
||||||
|
if isinstance(audio, str):
|
||||||
|
audio = load_audio(audio)
|
||||||
|
audio = torch.from_numpy(audio)
|
||||||
|
|
||||||
|
if device is not None:
|
||||||
|
audio = audio.to(device)
|
||||||
|
if padding > 0:
|
||||||
|
audio = F.pad(audio, (0, padding))
|
||||||
|
window = torch.hann_window(N_FFT).to(audio.device)
|
||||||
|
stft = torch.stft(audio, N_FFT, HOP_LENGTH, window=window, return_complex=True)
|
||||||
|
magnitudes = stft[..., :-1].abs() ** 2
|
||||||
|
|
||||||
|
filters = mel_filters(audio.device, n_mels)
|
||||||
|
mel_spec = filters @ magnitudes
|
||||||
|
|
||||||
|
log_spec = torch.clamp(mel_spec, min=1e-10).log10()
|
||||||
|
log_spec = torch.maximum(log_spec, log_spec.max() - 8.0)
|
||||||
|
log_spec = (log_spec + 4.0) / 4.0
|
||||||
|
return log_spec
|
||||||
826
whisperlivekit/simul_whisper/whisper/decoding.py
Normal file
826
whisperlivekit/simul_whisper/whisper/decoding.py
Normal file
@@ -0,0 +1,826 @@
|
|||||||
|
from dataclasses import dataclass, field, replace
|
||||||
|
from typing import TYPE_CHECKING, Dict, Iterable, List, Optional, Sequence, Tuple, Union
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
import torch
|
||||||
|
import torch.nn.functional as F
|
||||||
|
from torch import Tensor
|
||||||
|
from torch.distributions import Categorical
|
||||||
|
|
||||||
|
from .audio import CHUNK_LENGTH
|
||||||
|
from .tokenizer import Tokenizer, get_tokenizer
|
||||||
|
from .utils import compression_ratio
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from .model import Whisper
|
||||||
|
|
||||||
|
|
||||||
|
@torch.no_grad()
|
||||||
|
def detect_language(
|
||||||
|
model: "Whisper", mel: Tensor, tokenizer: Tokenizer = None
|
||||||
|
) -> Tuple[Tensor, List[dict]]:
|
||||||
|
"""
|
||||||
|
Detect the spoken language in the audio, and return them as list of strings, along with the ids
|
||||||
|
of the most probable language tokens and the probability distribution over all language tokens.
|
||||||
|
This is performed outside the main decode loop in order to not interfere with kv-caching.
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
language_tokens : Tensor, shape = (n_audio,)
|
||||||
|
ids of the most probable language tokens, which appears after the startoftranscript token.
|
||||||
|
language_probs : List[Dict[str, float]], length = n_audio
|
||||||
|
list of dictionaries containing the probability distribution over all languages.
|
||||||
|
"""
|
||||||
|
if tokenizer is None:
|
||||||
|
tokenizer = get_tokenizer(
|
||||||
|
model.is_multilingual, num_languages=model.num_languages
|
||||||
|
)
|
||||||
|
if (
|
||||||
|
tokenizer.language is None
|
||||||
|
or tokenizer.language_token not in tokenizer.sot_sequence
|
||||||
|
):
|
||||||
|
raise ValueError(
|
||||||
|
"This model doesn't have language tokens so it can't perform lang id"
|
||||||
|
)
|
||||||
|
|
||||||
|
single = mel.ndim == 2
|
||||||
|
if single:
|
||||||
|
mel = mel.unsqueeze(0)
|
||||||
|
|
||||||
|
# skip encoder forward pass if already-encoded audio features were given
|
||||||
|
if mel.shape[-2:] != (model.dims.n_audio_ctx, model.dims.n_audio_state):
|
||||||
|
mel = model.encoder(mel)
|
||||||
|
|
||||||
|
# forward pass using a single token, startoftranscript
|
||||||
|
n_audio = mel.shape[0]
|
||||||
|
x = torch.tensor([[tokenizer.sot]] * n_audio).to(mel.device) # [n_audio, 1]
|
||||||
|
logits = model.logits(x, mel)[:, 0]
|
||||||
|
|
||||||
|
# collect detected languages; suppress all non-language tokens
|
||||||
|
mask = torch.ones(logits.shape[-1], dtype=torch.bool)
|
||||||
|
mask[list(tokenizer.all_language_tokens)] = False
|
||||||
|
logits[:, mask] = -np.inf
|
||||||
|
language_tokens = logits.argmax(dim=-1)
|
||||||
|
language_token_probs = logits.softmax(dim=-1).cpu()
|
||||||
|
language_probs = [
|
||||||
|
{
|
||||||
|
c: language_token_probs[i, j].item()
|
||||||
|
for j, c in zip(tokenizer.all_language_tokens, tokenizer.all_language_codes)
|
||||||
|
}
|
||||||
|
for i in range(n_audio)
|
||||||
|
]
|
||||||
|
|
||||||
|
if single:
|
||||||
|
language_tokens = language_tokens[0]
|
||||||
|
language_probs = language_probs[0]
|
||||||
|
|
||||||
|
return language_tokens, language_probs
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class DecodingOptions:
|
||||||
|
# whether to perform X->X "transcribe" or X->English "translate"
|
||||||
|
task: str = "transcribe"
|
||||||
|
|
||||||
|
# language that the audio is in; uses detected language if None
|
||||||
|
language: Optional[str] = None
|
||||||
|
|
||||||
|
# sampling-related options
|
||||||
|
temperature: float = 0.0
|
||||||
|
sample_len: Optional[int] = None # maximum number of tokens to sample
|
||||||
|
best_of: Optional[int] = None # number of independent sample trajectories, if t > 0
|
||||||
|
beam_size: Optional[int] = None # number of beams in beam search, if t == 0
|
||||||
|
patience: Optional[float] = None # patience in beam search (arxiv:2204.05424)
|
||||||
|
|
||||||
|
# "alpha" in Google NMT, or None for length norm, when ranking generations
|
||||||
|
# to select which to return among the beams or best-of-N samples
|
||||||
|
length_penalty: Optional[float] = None
|
||||||
|
|
||||||
|
# text or tokens to feed as the prompt or the prefix; for more info:
|
||||||
|
# https://github.com/openai/whisper/discussions/117#discussioncomment-3727051
|
||||||
|
prompt: Optional[Union[str, List[int]]] = None # for the previous context
|
||||||
|
prefix: Optional[Union[str, List[int]]] = None # to prefix the current context
|
||||||
|
|
||||||
|
# list of tokens ids (or comma-separated token ids) to suppress
|
||||||
|
# "-1" will suppress a set of symbols as defined in `tokenizer.non_speech_tokens()`
|
||||||
|
suppress_tokens: Optional[Union[str, Iterable[int]]] = "-1"
|
||||||
|
suppress_blank: bool = True # this will suppress blank outputs
|
||||||
|
|
||||||
|
# timestamp sampling options
|
||||||
|
without_timestamps: bool = False # use <|notimestamps|> to sample text tokens only
|
||||||
|
max_initial_timestamp: Optional[float] = 1.0
|
||||||
|
|
||||||
|
# implementation details
|
||||||
|
fp16: bool = True # use fp16 for most of the calculation
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class DecodingResult:
|
||||||
|
audio_features: Tensor
|
||||||
|
language: str
|
||||||
|
language_probs: Optional[Dict[str, float]] = None
|
||||||
|
tokens: List[int] = field(default_factory=list)
|
||||||
|
text: str = ""
|
||||||
|
avg_logprob: float = np.nan
|
||||||
|
no_speech_prob: float = np.nan
|
||||||
|
temperature: float = np.nan
|
||||||
|
compression_ratio: float = np.nan
|
||||||
|
|
||||||
|
|
||||||
|
class Inference:
|
||||||
|
def logits(self, tokens: Tensor, audio_features: Tensor) -> Tensor:
|
||||||
|
"""Perform a forward pass on the decoder and return per-token logits"""
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def rearrange_kv_cache(self, source_indices) -> None:
|
||||||
|
"""Update the key-value cache according to the updated beams"""
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def cleanup_caching(self) -> None:
|
||||||
|
"""Clean up any resources or hooks after decoding is finished"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class PyTorchInference(Inference):
|
||||||
|
def __init__(self, model: "Whisper", initial_token_length: int):
|
||||||
|
self.model: "Whisper" = model
|
||||||
|
self.initial_token_length = initial_token_length
|
||||||
|
self.kv_cache = {}
|
||||||
|
self.hooks = []
|
||||||
|
|
||||||
|
key_modules = [block.attn.key for block in self.model.decoder.blocks]
|
||||||
|
value_modules = [block.attn.value for block in self.model.decoder.blocks]
|
||||||
|
self.kv_modules = key_modules + value_modules
|
||||||
|
|
||||||
|
def logits(self, tokens: Tensor, audio_features: Tensor) -> Tensor:
|
||||||
|
if not self.kv_cache:
|
||||||
|
self.kv_cache, self.hooks = self.model.install_kv_cache_hooks()
|
||||||
|
|
||||||
|
if tokens.shape[-1] > self.initial_token_length:
|
||||||
|
# only need to use the last token except in the first forward pass
|
||||||
|
tokens = tokens[:, -1:]
|
||||||
|
|
||||||
|
return self.model.decoder(tokens, audio_features, kv_cache=self.kv_cache)
|
||||||
|
|
||||||
|
def cleanup_caching(self):
|
||||||
|
for hook in self.hooks:
|
||||||
|
hook.remove()
|
||||||
|
|
||||||
|
self.kv_cache = {}
|
||||||
|
self.hooks = []
|
||||||
|
|
||||||
|
def rearrange_kv_cache(self, source_indices):
|
||||||
|
if source_indices != list(range(len(source_indices))):
|
||||||
|
for module in self.kv_modules:
|
||||||
|
# update the key/value cache to contain the selected sequences
|
||||||
|
self.kv_cache[module] = self.kv_cache[module][source_indices].detach()
|
||||||
|
|
||||||
|
|
||||||
|
class SequenceRanker:
|
||||||
|
def rank(
|
||||||
|
self, tokens: List[List[Tensor]], sum_logprobs: List[List[float]]
|
||||||
|
) -> List[int]:
|
||||||
|
"""
|
||||||
|
Given a list of groups of samples and their cumulative log probabilities,
|
||||||
|
return the indices of the samples in each group to select as the final result
|
||||||
|
"""
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
|
||||||
|
class MaximumLikelihoodRanker(SequenceRanker):
|
||||||
|
"""
|
||||||
|
Select the sample with the highest log probabilities, penalized using either
|
||||||
|
a simple length normalization or Google NMT paper's length penalty
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, length_penalty: Optional[float]):
|
||||||
|
self.length_penalty = length_penalty
|
||||||
|
|
||||||
|
def rank(self, tokens: List[List[Tensor]], sum_logprobs: List[List[float]]):
|
||||||
|
def scores(logprobs, lengths):
|
||||||
|
result = []
|
||||||
|
for logprob, length in zip(logprobs, lengths):
|
||||||
|
if self.length_penalty is None:
|
||||||
|
penalty = length
|
||||||
|
else:
|
||||||
|
# from the Google NMT paper
|
||||||
|
penalty = ((5 + length) / 6) ** self.length_penalty
|
||||||
|
result.append(logprob / penalty)
|
||||||
|
return result
|
||||||
|
|
||||||
|
# get the sequence with the highest score
|
||||||
|
lengths = [[len(t) for t in s] for s in tokens]
|
||||||
|
return [np.argmax(scores(p, l)) for p, l in zip(sum_logprobs, lengths)]
|
||||||
|
|
||||||
|
|
||||||
|
class TokenDecoder:
|
||||||
|
def reset(self):
|
||||||
|
"""Initialize any stateful variables for decoding a new sequence"""
|
||||||
|
|
||||||
|
def update(
|
||||||
|
self, tokens: Tensor, logits: Tensor, sum_logprobs: Tensor
|
||||||
|
) -> Tuple[Tensor, bool]:
|
||||||
|
"""Specify how to select the next token, based on the current trace and logits
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
tokens : Tensor, shape = (n_batch, current_sequence_length)
|
||||||
|
all tokens in the context so far, including the prefix and sot_sequence tokens
|
||||||
|
|
||||||
|
logits : Tensor, shape = (n_batch, vocab_size)
|
||||||
|
per-token logits of the probability distribution at the current step
|
||||||
|
|
||||||
|
sum_logprobs : Tensor, shape = (n_batch)
|
||||||
|
cumulative log probabilities for each sequence
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
tokens : Tensor, shape = (n_batch, current_sequence_length + 1)
|
||||||
|
the tokens, appended with the selected next token
|
||||||
|
|
||||||
|
completed : bool
|
||||||
|
True if all sequences has reached the end of text
|
||||||
|
|
||||||
|
"""
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def finalize(
|
||||||
|
self, tokens: Tensor, sum_logprobs: Tensor
|
||||||
|
) -> Tuple[Sequence[Sequence[Tensor]], List[List[float]]]:
|
||||||
|
"""Finalize search and return the final candidate sequences
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
tokens : Tensor, shape = (n_audio, n_group, current_sequence_length)
|
||||||
|
all tokens in the context so far, including the prefix and sot_sequence
|
||||||
|
|
||||||
|
sum_logprobs : Tensor, shape = (n_audio, n_group)
|
||||||
|
cumulative log probabilities for each sequence
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
tokens : Sequence[Sequence[Tensor]], length = n_audio
|
||||||
|
sequence of Tensors containing candidate token sequences, for each audio input
|
||||||
|
|
||||||
|
sum_logprobs : List[List[float]], length = n_audio
|
||||||
|
sequence of cumulative log probabilities corresponding to the above
|
||||||
|
|
||||||
|
"""
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
|
||||||
|
class GreedyDecoder(TokenDecoder):
|
||||||
|
def __init__(self, temperature: float, eot: int):
|
||||||
|
self.temperature = temperature
|
||||||
|
self.eot = eot
|
||||||
|
|
||||||
|
def update(
|
||||||
|
self, tokens: Tensor, logits: Tensor, sum_logprobs: Tensor
|
||||||
|
) -> Tuple[Tensor, bool]:
|
||||||
|
if self.temperature == 0:
|
||||||
|
next_tokens = logits.argmax(dim=-1)
|
||||||
|
else:
|
||||||
|
next_tokens = Categorical(logits=logits / self.temperature).sample()
|
||||||
|
|
||||||
|
logprobs = F.log_softmax(logits.float(), dim=-1)
|
||||||
|
current_logprobs = logprobs[torch.arange(logprobs.shape[0]), next_tokens]
|
||||||
|
sum_logprobs += current_logprobs * (tokens[:, -1] != self.eot)
|
||||||
|
|
||||||
|
next_tokens[tokens[:, -1] == self.eot] = self.eot
|
||||||
|
tokens = torch.cat([tokens, next_tokens[:, None]], dim=-1)
|
||||||
|
|
||||||
|
completed = (tokens[:, -1] == self.eot).all()
|
||||||
|
return tokens, completed
|
||||||
|
|
||||||
|
def finalize(self, tokens: Tensor, sum_logprobs: Tensor):
|
||||||
|
# make sure each sequence has at least one EOT token at the end
|
||||||
|
tokens = F.pad(tokens, (0, 1), value=self.eot)
|
||||||
|
return tokens, sum_logprobs.tolist()
|
||||||
|
|
||||||
|
|
||||||
|
class BeamSearchDecoder(TokenDecoder):
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
beam_size: int,
|
||||||
|
eot: int,
|
||||||
|
inference: Inference,
|
||||||
|
patience: Optional[float] = None,
|
||||||
|
):
|
||||||
|
self.beam_size = beam_size
|
||||||
|
self.eot = eot
|
||||||
|
self.inference = inference
|
||||||
|
self.patience = patience or 1.0
|
||||||
|
self.max_candidates: int = round(beam_size * self.patience)
|
||||||
|
self.finished_sequences = None
|
||||||
|
|
||||||
|
assert (
|
||||||
|
self.max_candidates > 0
|
||||||
|
), f"Invalid beam size ({beam_size}) or patience ({patience})"
|
||||||
|
|
||||||
|
def reset(self):
|
||||||
|
self.finished_sequences = None
|
||||||
|
|
||||||
|
def update(
|
||||||
|
self, tokens: Tensor, logits: Tensor, sum_logprobs: Tensor
|
||||||
|
) -> Tuple[Tensor, bool]:
|
||||||
|
if tokens.shape[0] % self.beam_size != 0:
|
||||||
|
raise ValueError(f"{tokens.shape}[0] % {self.beam_size} != 0")
|
||||||
|
|
||||||
|
n_audio = tokens.shape[0] // self.beam_size
|
||||||
|
if self.finished_sequences is None: # for the first update
|
||||||
|
self.finished_sequences = [{} for _ in range(n_audio)]
|
||||||
|
|
||||||
|
logprobs = F.log_softmax(logits.float(), dim=-1)
|
||||||
|
next_tokens, source_indices, finished_sequences = [], [], []
|
||||||
|
for i in range(n_audio):
|
||||||
|
scores, sources, finished = {}, {}, {}
|
||||||
|
|
||||||
|
# STEP 1: calculate the cumulative log probabilities for possible candidates
|
||||||
|
for j in range(self.beam_size):
|
||||||
|
idx = i * self.beam_size + j
|
||||||
|
prefix = tokens[idx].tolist()
|
||||||
|
for logprob, token in zip(*logprobs[idx].topk(self.beam_size + 1)):
|
||||||
|
new_logprob = (sum_logprobs[idx] + logprob).item()
|
||||||
|
sequence = tuple(prefix + [token.item()])
|
||||||
|
scores[sequence] = new_logprob
|
||||||
|
sources[sequence] = idx
|
||||||
|
|
||||||
|
# STEP 2: rank the candidates and keep the top beam_size sequences for each audio
|
||||||
|
saved = 0
|
||||||
|
for sequence in sorted(scores, key=scores.get, reverse=True):
|
||||||
|
if sequence[-1] == self.eot:
|
||||||
|
finished[sequence] = scores[sequence]
|
||||||
|
else:
|
||||||
|
sum_logprobs[len(next_tokens)] = scores[sequence]
|
||||||
|
next_tokens.append(sequence)
|
||||||
|
source_indices.append(sources[sequence])
|
||||||
|
|
||||||
|
saved += 1
|
||||||
|
if saved == self.beam_size:
|
||||||
|
break
|
||||||
|
|
||||||
|
finished_sequences.append(finished)
|
||||||
|
|
||||||
|
tokens = torch.tensor(next_tokens, device=tokens.device)
|
||||||
|
self.inference.rearrange_kv_cache(source_indices)
|
||||||
|
|
||||||
|
# add newly finished sequences to self.finished_sequences
|
||||||
|
assert len(self.finished_sequences) == len(finished_sequences)
|
||||||
|
for previously_finished, newly_finished in zip(
|
||||||
|
self.finished_sequences, finished_sequences
|
||||||
|
):
|
||||||
|
for seq in sorted(newly_finished, key=newly_finished.get, reverse=True):
|
||||||
|
if len(previously_finished) >= self.max_candidates:
|
||||||
|
break # the candidate list is full
|
||||||
|
previously_finished[seq] = newly_finished[seq]
|
||||||
|
|
||||||
|
# mark as completed if all audio has enough number of samples
|
||||||
|
completed = all(
|
||||||
|
len(sequences) >= self.max_candidates
|
||||||
|
for sequences in self.finished_sequences
|
||||||
|
)
|
||||||
|
return tokens, completed
|
||||||
|
|
||||||
|
def finalize(self, preceding_tokens: Tensor, sum_logprobs: Tensor):
|
||||||
|
# collect all finished sequences, including patience, and add unfinished ones if not enough
|
||||||
|
sum_logprobs = sum_logprobs.cpu()
|
||||||
|
for i, sequences in enumerate(self.finished_sequences):
|
||||||
|
if (
|
||||||
|
len(sequences) < self.beam_size
|
||||||
|
): # when not enough sequences are finished
|
||||||
|
for j in list(np.argsort(sum_logprobs[i]))[::-1]:
|
||||||
|
sequence = preceding_tokens[i, j].tolist() + [self.eot]
|
||||||
|
sequences[tuple(sequence)] = sum_logprobs[i][j].item()
|
||||||
|
if len(sequences) >= self.beam_size:
|
||||||
|
break
|
||||||
|
|
||||||
|
tokens: List[List[Tensor]] = [
|
||||||
|
[torch.tensor(seq) for seq in sequences.keys()]
|
||||||
|
for sequences in self.finished_sequences
|
||||||
|
]
|
||||||
|
sum_logprobs: List[List[float]] = [
|
||||||
|
list(sequences.values()) for sequences in self.finished_sequences
|
||||||
|
]
|
||||||
|
return tokens, sum_logprobs
|
||||||
|
|
||||||
|
|
||||||
|
class LogitFilter:
|
||||||
|
def apply(self, logits: Tensor, tokens: Tensor) -> None:
|
||||||
|
"""Apply any filtering or masking to logits in-place
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
logits : Tensor, shape = (n_batch, vocab_size)
|
||||||
|
per-token logits of the probability distribution at the current step
|
||||||
|
|
||||||
|
tokens : Tensor, shape = (n_batch, current_sequence_length)
|
||||||
|
all tokens in the context so far, including the prefix and sot_sequence tokens
|
||||||
|
|
||||||
|
"""
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
|
||||||
|
class SuppressBlank(LogitFilter):
|
||||||
|
def __init__(self, tokenizer: Tokenizer, sample_begin: int):
|
||||||
|
self.tokenizer = tokenizer
|
||||||
|
self.sample_begin = sample_begin
|
||||||
|
|
||||||
|
def apply(self, logits: Tensor, tokens: Tensor):
|
||||||
|
if tokens.shape[1] == self.sample_begin:
|
||||||
|
logits[:, self.tokenizer.encode(" ") + [self.tokenizer.eot]] = -np.inf
|
||||||
|
|
||||||
|
|
||||||
|
class SuppressTokens(LogitFilter):
|
||||||
|
def __init__(self, suppress_tokens: Sequence[int]):
|
||||||
|
self.suppress_tokens = list(suppress_tokens)
|
||||||
|
|
||||||
|
def apply(self, logits: Tensor, tokens: Tensor):
|
||||||
|
logits[:, self.suppress_tokens] = -np.inf
|
||||||
|
|
||||||
|
|
||||||
|
class ApplyTimestampRules(LogitFilter):
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
tokenizer: Tokenizer,
|
||||||
|
sample_begin: int,
|
||||||
|
max_initial_timestamp_index: Optional[int],
|
||||||
|
):
|
||||||
|
self.tokenizer = tokenizer
|
||||||
|
self.sample_begin = sample_begin
|
||||||
|
self.max_initial_timestamp_index = max_initial_timestamp_index
|
||||||
|
|
||||||
|
def apply(self, logits: Tensor, tokens: Tensor):
|
||||||
|
# suppress <|notimestamps|> which is handled by without_timestamps
|
||||||
|
if self.tokenizer.no_timestamps is not None:
|
||||||
|
logits[:, self.tokenizer.no_timestamps] = -np.inf
|
||||||
|
|
||||||
|
# timestamps have to appear in pairs, except directly before EOT; mask logits accordingly
|
||||||
|
for k in range(tokens.shape[0]):
|
||||||
|
sampled_tokens = tokens[k, self.sample_begin :]
|
||||||
|
seq = [t for t in sampled_tokens.tolist()]
|
||||||
|
last_was_timestamp = (
|
||||||
|
len(seq) >= 1 and seq[-1] >= self.tokenizer.timestamp_begin
|
||||||
|
)
|
||||||
|
penultimate_was_timestamp = (
|
||||||
|
len(seq) < 2 or seq[-2] >= self.tokenizer.timestamp_begin
|
||||||
|
)
|
||||||
|
|
||||||
|
if last_was_timestamp:
|
||||||
|
if penultimate_was_timestamp: # has to be non-timestamp
|
||||||
|
logits[k, self.tokenizer.timestamp_begin :] = -np.inf
|
||||||
|
else: # cannot be normal text tokens
|
||||||
|
logits[k, : self.tokenizer.eot] = -np.inf
|
||||||
|
|
||||||
|
timestamps = sampled_tokens[
|
||||||
|
sampled_tokens.ge(self.tokenizer.timestamp_begin)
|
||||||
|
]
|
||||||
|
if timestamps.numel() > 0:
|
||||||
|
# timestamps shouldn't decrease; forbid timestamp tokens smaller than the last
|
||||||
|
# also force each segment to have a nonzero length, to prevent infinite looping
|
||||||
|
if last_was_timestamp and not penultimate_was_timestamp:
|
||||||
|
timestamp_last = timestamps[-1]
|
||||||
|
else:
|
||||||
|
timestamp_last = timestamps[-1] + 1
|
||||||
|
logits[k, self.tokenizer.timestamp_begin : timestamp_last] = -np.inf
|
||||||
|
|
||||||
|
if tokens.shape[1] == self.sample_begin:
|
||||||
|
# suppress generating non-timestamp tokens at the beginning
|
||||||
|
logits[:, : self.tokenizer.timestamp_begin] = -np.inf
|
||||||
|
|
||||||
|
# apply the `max_initial_timestamp` option
|
||||||
|
if self.max_initial_timestamp_index is not None:
|
||||||
|
last_allowed = (
|
||||||
|
self.tokenizer.timestamp_begin + self.max_initial_timestamp_index
|
||||||
|
)
|
||||||
|
logits[:, last_allowed + 1 :] = -np.inf
|
||||||
|
|
||||||
|
# if sum of probability over timestamps is above any other token, sample timestamp
|
||||||
|
logprobs = F.log_softmax(logits.float(), dim=-1)
|
||||||
|
for k in range(tokens.shape[0]):
|
||||||
|
timestamp_logprob = logprobs[k, self.tokenizer.timestamp_begin :].logsumexp(
|
||||||
|
dim=-1
|
||||||
|
)
|
||||||
|
max_text_token_logprob = logprobs[k, : self.tokenizer.timestamp_begin].max()
|
||||||
|
if timestamp_logprob > max_text_token_logprob:
|
||||||
|
logits[k, : self.tokenizer.timestamp_begin] = -np.inf
|
||||||
|
|
||||||
|
|
||||||
|
class DecodingTask:
|
||||||
|
inference: Inference
|
||||||
|
sequence_ranker: SequenceRanker
|
||||||
|
decoder: TokenDecoder
|
||||||
|
logit_filters: List[LogitFilter]
|
||||||
|
|
||||||
|
def __init__(self, model: "Whisper", options: DecodingOptions):
|
||||||
|
self.model = model
|
||||||
|
|
||||||
|
language = options.language or "en"
|
||||||
|
tokenizer = get_tokenizer(
|
||||||
|
model.is_multilingual,
|
||||||
|
num_languages=model.num_languages,
|
||||||
|
language=language,
|
||||||
|
task=options.task,
|
||||||
|
)
|
||||||
|
self.tokenizer: Tokenizer = tokenizer
|
||||||
|
self.options: DecodingOptions = self._verify_options(options)
|
||||||
|
|
||||||
|
self.n_group: int = options.beam_size or options.best_of or 1
|
||||||
|
self.n_ctx: int = model.dims.n_text_ctx
|
||||||
|
self.sample_len: int = options.sample_len or model.dims.n_text_ctx // 2
|
||||||
|
|
||||||
|
self.sot_sequence: Tuple[int] = tokenizer.sot_sequence
|
||||||
|
if self.options.without_timestamps:
|
||||||
|
self.sot_sequence = tokenizer.sot_sequence_including_notimestamps
|
||||||
|
|
||||||
|
self.initial_tokens: Tuple[int] = self._get_initial_tokens()
|
||||||
|
self.sample_begin: int = len(self.initial_tokens)
|
||||||
|
self.sot_index: int = self.initial_tokens.index(tokenizer.sot)
|
||||||
|
|
||||||
|
# inference: implements the forward pass through the decoder, including kv caching
|
||||||
|
self.inference = PyTorchInference(model, len(self.initial_tokens))
|
||||||
|
|
||||||
|
# sequence ranker: implements how to rank a group of sampled sequences
|
||||||
|
self.sequence_ranker = MaximumLikelihoodRanker(options.length_penalty)
|
||||||
|
|
||||||
|
# decoder: implements how to select the next tokens, given the autoregressive distribution
|
||||||
|
if options.beam_size is not None:
|
||||||
|
self.decoder = BeamSearchDecoder(
|
||||||
|
options.beam_size, tokenizer.eot, self.inference, options.patience
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
self.decoder = GreedyDecoder(options.temperature, tokenizer.eot)
|
||||||
|
|
||||||
|
# logit filters: applies various rules to suppress or penalize certain tokens
|
||||||
|
self.logit_filters = []
|
||||||
|
if self.options.suppress_blank:
|
||||||
|
self.logit_filters.append(SuppressBlank(self.tokenizer, self.sample_begin))
|
||||||
|
if self.options.suppress_tokens:
|
||||||
|
self.logit_filters.append(SuppressTokens(self._get_suppress_tokens()))
|
||||||
|
if not options.without_timestamps:
|
||||||
|
precision = CHUNK_LENGTH / model.dims.n_audio_ctx # usually 0.02 seconds
|
||||||
|
max_initial_timestamp_index = None
|
||||||
|
if options.max_initial_timestamp:
|
||||||
|
max_initial_timestamp_index = round(
|
||||||
|
self.options.max_initial_timestamp / precision
|
||||||
|
)
|
||||||
|
self.logit_filters.append(
|
||||||
|
ApplyTimestampRules(
|
||||||
|
tokenizer, self.sample_begin, max_initial_timestamp_index
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
def _verify_options(self, options: DecodingOptions) -> DecodingOptions:
|
||||||
|
if options.beam_size is not None and options.best_of is not None:
|
||||||
|
raise ValueError("beam_size and best_of can't be given together")
|
||||||
|
if options.temperature == 0:
|
||||||
|
if options.best_of is not None:
|
||||||
|
raise ValueError("best_of with greedy sampling (T=0) is not compatible")
|
||||||
|
if options.patience is not None and options.beam_size is None:
|
||||||
|
raise ValueError("patience requires beam_size to be given")
|
||||||
|
if options.length_penalty is not None and not (
|
||||||
|
0 <= options.length_penalty <= 1
|
||||||
|
):
|
||||||
|
raise ValueError("length_penalty (alpha) should be a value between 0 and 1")
|
||||||
|
|
||||||
|
return options
|
||||||
|
|
||||||
|
def _get_initial_tokens(self) -> Tuple[int]:
|
||||||
|
tokens = list(self.sot_sequence)
|
||||||
|
|
||||||
|
if prefix := self.options.prefix:
|
||||||
|
prefix_tokens = (
|
||||||
|
self.tokenizer.encode(" " + prefix.strip())
|
||||||
|
if isinstance(prefix, str)
|
||||||
|
else prefix
|
||||||
|
)
|
||||||
|
if self.sample_len is not None:
|
||||||
|
max_prefix_len = self.n_ctx // 2 - self.sample_len
|
||||||
|
prefix_tokens = prefix_tokens[-max_prefix_len:]
|
||||||
|
tokens = tokens + prefix_tokens
|
||||||
|
|
||||||
|
if prompt := self.options.prompt:
|
||||||
|
prompt_tokens = (
|
||||||
|
self.tokenizer.encode(" " + prompt.strip())
|
||||||
|
if isinstance(prompt, str)
|
||||||
|
else prompt
|
||||||
|
)
|
||||||
|
tokens = (
|
||||||
|
[self.tokenizer.sot_prev]
|
||||||
|
+ prompt_tokens[-(self.n_ctx // 2 - 1) :]
|
||||||
|
+ tokens
|
||||||
|
)
|
||||||
|
|
||||||
|
return tuple(tokens)
|
||||||
|
|
||||||
|
def _get_suppress_tokens(self) -> Tuple[int]:
|
||||||
|
suppress_tokens = self.options.suppress_tokens
|
||||||
|
|
||||||
|
if isinstance(suppress_tokens, str):
|
||||||
|
suppress_tokens = [int(t) for t in suppress_tokens.split(",")]
|
||||||
|
|
||||||
|
if -1 in suppress_tokens:
|
||||||
|
suppress_tokens = [t for t in suppress_tokens if t >= 0]
|
||||||
|
suppress_tokens.extend(self.tokenizer.non_speech_tokens)
|
||||||
|
elif suppress_tokens is None or len(suppress_tokens) == 0:
|
||||||
|
suppress_tokens = [] # interpret empty string as an empty list
|
||||||
|
else:
|
||||||
|
assert isinstance(suppress_tokens, list), "suppress_tokens must be a list"
|
||||||
|
|
||||||
|
suppress_tokens.extend(
|
||||||
|
[
|
||||||
|
self.tokenizer.transcribe,
|
||||||
|
self.tokenizer.translate,
|
||||||
|
self.tokenizer.sot,
|
||||||
|
self.tokenizer.sot_prev,
|
||||||
|
self.tokenizer.sot_lm,
|
||||||
|
]
|
||||||
|
)
|
||||||
|
if self.tokenizer.no_speech is not None:
|
||||||
|
# no-speech probability is collected separately
|
||||||
|
suppress_tokens.append(self.tokenizer.no_speech)
|
||||||
|
|
||||||
|
return tuple(sorted(set(suppress_tokens)))
|
||||||
|
|
||||||
|
def _get_audio_features(self, mel: Tensor):
|
||||||
|
if self.options.fp16:
|
||||||
|
mel = mel.half()
|
||||||
|
|
||||||
|
if mel.shape[-2:] == (
|
||||||
|
self.model.dims.n_audio_ctx,
|
||||||
|
self.model.dims.n_audio_state,
|
||||||
|
):
|
||||||
|
# encoded audio features are given; skip audio encoding
|
||||||
|
audio_features = mel
|
||||||
|
else:
|
||||||
|
audio_features = self.model.encoder(mel)
|
||||||
|
|
||||||
|
if audio_features.dtype != (
|
||||||
|
torch.float16 if self.options.fp16 else torch.float32
|
||||||
|
):
|
||||||
|
return TypeError(
|
||||||
|
f"audio_features has an incorrect dtype: {audio_features.dtype}"
|
||||||
|
)
|
||||||
|
|
||||||
|
return audio_features
|
||||||
|
|
||||||
|
def _detect_language(self, audio_features: Tensor, tokens: Tensor):
|
||||||
|
languages = [self.options.language] * audio_features.shape[0]
|
||||||
|
lang_probs = None
|
||||||
|
|
||||||
|
if self.options.language is None or self.options.task == "lang_id":
|
||||||
|
lang_tokens, lang_probs = self.model.detect_language(
|
||||||
|
audio_features, self.tokenizer
|
||||||
|
)
|
||||||
|
languages = [max(probs, key=probs.get) for probs in lang_probs]
|
||||||
|
if self.options.language is None:
|
||||||
|
tokens[:, self.sot_index + 1] = lang_tokens # write language tokens
|
||||||
|
|
||||||
|
return languages, lang_probs
|
||||||
|
|
||||||
|
def _main_loop(self, audio_features: Tensor, tokens: Tensor):
|
||||||
|
n_batch = tokens.shape[0]
|
||||||
|
sum_logprobs: Tensor = torch.zeros(n_batch, device=audio_features.device)
|
||||||
|
no_speech_probs = [np.nan] * n_batch
|
||||||
|
|
||||||
|
try:
|
||||||
|
for i in range(self.sample_len):
|
||||||
|
logits = self.inference.logits(tokens, audio_features)
|
||||||
|
|
||||||
|
if (
|
||||||
|
i == 0 and self.tokenizer.no_speech is not None
|
||||||
|
): # save no_speech_probs
|
||||||
|
probs_at_sot = logits[:, self.sot_index].float().softmax(dim=-1)
|
||||||
|
no_speech_probs = probs_at_sot[:, self.tokenizer.no_speech].tolist()
|
||||||
|
|
||||||
|
# now we need to consider the logits at the last token only
|
||||||
|
logits = logits[:, -1]
|
||||||
|
|
||||||
|
# apply the logit filters, e.g. for suppressing or applying penalty to
|
||||||
|
for logit_filter in self.logit_filters:
|
||||||
|
logit_filter.apply(logits, tokens)
|
||||||
|
|
||||||
|
# expand the tokens tensor with the selected next tokens
|
||||||
|
tokens, completed = self.decoder.update(tokens, logits, sum_logprobs)
|
||||||
|
|
||||||
|
if completed or tokens.shape[-1] > self.n_ctx:
|
||||||
|
break
|
||||||
|
finally:
|
||||||
|
self.inference.cleanup_caching()
|
||||||
|
|
||||||
|
return tokens, sum_logprobs, no_speech_probs
|
||||||
|
|
||||||
|
@torch.no_grad()
|
||||||
|
def run(self, mel: Tensor) -> List[DecodingResult]:
|
||||||
|
self.decoder.reset()
|
||||||
|
tokenizer: Tokenizer = self.tokenizer
|
||||||
|
n_audio: int = mel.shape[0]
|
||||||
|
|
||||||
|
audio_features: Tensor = self._get_audio_features(mel) # encoder forward pass
|
||||||
|
tokens: Tensor = torch.tensor([self.initial_tokens]).repeat(n_audio, 1)
|
||||||
|
|
||||||
|
# detect language if requested, overwriting the language token
|
||||||
|
languages, language_probs = self._detect_language(audio_features, tokens)
|
||||||
|
if self.options.task == "lang_id":
|
||||||
|
return [
|
||||||
|
DecodingResult(
|
||||||
|
audio_features=features, language=language, language_probs=probs
|
||||||
|
)
|
||||||
|
for features, language, probs in zip(
|
||||||
|
audio_features, languages, language_probs
|
||||||
|
)
|
||||||
|
]
|
||||||
|
|
||||||
|
# repeat text tensors by the group size, for beam search or best-of-n sampling
|
||||||
|
tokens = tokens.repeat_interleave(self.n_group, dim=0).to(audio_features.device)
|
||||||
|
|
||||||
|
# call the main sampling loop
|
||||||
|
tokens, sum_logprobs, no_speech_probs = self._main_loop(audio_features, tokens)
|
||||||
|
|
||||||
|
# reshape the tensors to have (n_audio, n_group) as the first two dimensions
|
||||||
|
audio_features = audio_features[:: self.n_group]
|
||||||
|
no_speech_probs = no_speech_probs[:: self.n_group]
|
||||||
|
assert audio_features.shape[0] == len(no_speech_probs) == n_audio
|
||||||
|
|
||||||
|
tokens = tokens.reshape(n_audio, self.n_group, -1)
|
||||||
|
sum_logprobs = sum_logprobs.reshape(n_audio, self.n_group)
|
||||||
|
|
||||||
|
# get the final candidates for each group, and slice between the first sampled token and EOT
|
||||||
|
tokens, sum_logprobs = self.decoder.finalize(tokens, sum_logprobs)
|
||||||
|
tokens: List[List[Tensor]] = [
|
||||||
|
[t[self.sample_begin : (t == tokenizer.eot).nonzero()[0, 0]] for t in s]
|
||||||
|
for s in tokens
|
||||||
|
]
|
||||||
|
|
||||||
|
# select the top-ranked sample in each group
|
||||||
|
selected = self.sequence_ranker.rank(tokens, sum_logprobs)
|
||||||
|
tokens: List[List[int]] = [t[i].tolist() for i, t in zip(selected, tokens)]
|
||||||
|
texts: List[str] = [tokenizer.decode(t).strip() for t in tokens]
|
||||||
|
|
||||||
|
sum_logprobs: List[float] = [lp[i] for i, lp in zip(selected, sum_logprobs)]
|
||||||
|
avg_logprobs: List[float] = [
|
||||||
|
lp / (len(t) + 1) for t, lp in zip(tokens, sum_logprobs)
|
||||||
|
]
|
||||||
|
|
||||||
|
fields = (
|
||||||
|
texts,
|
||||||
|
languages,
|
||||||
|
tokens,
|
||||||
|
audio_features,
|
||||||
|
avg_logprobs,
|
||||||
|
no_speech_probs,
|
||||||
|
)
|
||||||
|
if len(set(map(len, fields))) != 1:
|
||||||
|
raise RuntimeError(f"inconsistent result lengths: {list(map(len, fields))}")
|
||||||
|
|
||||||
|
return [
|
||||||
|
DecodingResult(
|
||||||
|
audio_features=features,
|
||||||
|
language=language,
|
||||||
|
tokens=tokens,
|
||||||
|
text=text,
|
||||||
|
avg_logprob=avg_logprob,
|
||||||
|
no_speech_prob=no_speech_prob,
|
||||||
|
temperature=self.options.temperature,
|
||||||
|
compression_ratio=compression_ratio(text),
|
||||||
|
)
|
||||||
|
for text, language, tokens, features, avg_logprob, no_speech_prob in zip(
|
||||||
|
*fields
|
||||||
|
)
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@torch.no_grad()
|
||||||
|
def decode(
|
||||||
|
model: "Whisper",
|
||||||
|
mel: Tensor,
|
||||||
|
options: DecodingOptions = DecodingOptions(),
|
||||||
|
**kwargs,
|
||||||
|
) -> Union[DecodingResult, List[DecodingResult]]:
|
||||||
|
"""
|
||||||
|
Performs decoding of 30-second audio segment(s), provided as Mel spectrogram(s).
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
model: Whisper
|
||||||
|
the Whisper model instance
|
||||||
|
|
||||||
|
mel: torch.Tensor, shape = (80, 3000) or (*, 80, 3000)
|
||||||
|
A tensor containing the Mel spectrogram(s)
|
||||||
|
|
||||||
|
options: DecodingOptions
|
||||||
|
A dataclass that contains all necessary options for decoding 30-second segments
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
result: Union[DecodingResult, List[DecodingResult]]
|
||||||
|
The result(s) of decoding contained in `DecodingResult` dataclass instance(s)
|
||||||
|
"""
|
||||||
|
if single := mel.ndim == 2:
|
||||||
|
mel = mel.unsqueeze(0)
|
||||||
|
|
||||||
|
if kwargs:
|
||||||
|
options = replace(options, **kwargs)
|
||||||
|
|
||||||
|
result = DecodingTask(model, options).run(mel)
|
||||||
|
|
||||||
|
return result[0] if single else result
|
||||||
348
whisperlivekit/simul_whisper/whisper/model.py
Normal file
348
whisperlivekit/simul_whisper/whisper/model.py
Normal file
@@ -0,0 +1,348 @@
|
|||||||
|
import base64
|
||||||
|
import gzip
|
||||||
|
from contextlib import contextmanager
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from typing import Dict, Iterable, Optional, Tuple
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
import torch
|
||||||
|
import torch.nn.functional as F
|
||||||
|
from torch import Tensor, nn
|
||||||
|
|
||||||
|
from .decoding import decode as decode_function
|
||||||
|
from .decoding import detect_language as detect_language_function
|
||||||
|
from .transcribe import transcribe as transcribe_function
|
||||||
|
|
||||||
|
try:
|
||||||
|
from torch.nn.functional import scaled_dot_product_attention
|
||||||
|
|
||||||
|
SDPA_AVAILABLE = True
|
||||||
|
except (ImportError, RuntimeError, OSError):
|
||||||
|
scaled_dot_product_attention = None
|
||||||
|
SDPA_AVAILABLE = False
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class ModelDimensions:
|
||||||
|
n_mels: int
|
||||||
|
n_audio_ctx: int
|
||||||
|
n_audio_state: int
|
||||||
|
n_audio_head: int
|
||||||
|
n_audio_layer: int
|
||||||
|
n_vocab: int
|
||||||
|
n_text_ctx: int
|
||||||
|
n_text_state: int
|
||||||
|
n_text_head: int
|
||||||
|
n_text_layer: int
|
||||||
|
|
||||||
|
|
||||||
|
class LayerNorm(nn.LayerNorm):
|
||||||
|
def forward(self, x: Tensor) -> Tensor:
|
||||||
|
return super().forward(x.float()).type(x.dtype)
|
||||||
|
|
||||||
|
|
||||||
|
class Linear(nn.Linear):
|
||||||
|
def forward(self, x: Tensor) -> Tensor:
|
||||||
|
return F.linear(
|
||||||
|
x,
|
||||||
|
self.weight.to(x.dtype),
|
||||||
|
None if self.bias is None else self.bias.to(x.dtype),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class Conv1d(nn.Conv1d):
|
||||||
|
def _conv_forward(
|
||||||
|
self, x: Tensor, weight: Tensor, bias: Optional[Tensor]
|
||||||
|
) -> Tensor:
|
||||||
|
return super()._conv_forward(
|
||||||
|
x, weight.to(x.dtype), None if bias is None else bias.to(x.dtype)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def sinusoids(length, channels, max_timescale=10000):
|
||||||
|
"""Returns sinusoids for positional embedding"""
|
||||||
|
assert channels % 2 == 0
|
||||||
|
log_timescale_increment = np.log(max_timescale) / (channels // 2 - 1)
|
||||||
|
inv_timescales = torch.exp(-log_timescale_increment * torch.arange(channels // 2))
|
||||||
|
scaled_time = torch.arange(length)[:, np.newaxis] * inv_timescales[np.newaxis, :]
|
||||||
|
return torch.cat([torch.sin(scaled_time), torch.cos(scaled_time)], dim=1)
|
||||||
|
|
||||||
|
|
||||||
|
@contextmanager
|
||||||
|
def disable_sdpa():
|
||||||
|
prev_state = MultiHeadAttention.use_sdpa
|
||||||
|
try:
|
||||||
|
MultiHeadAttention.use_sdpa = False
|
||||||
|
yield
|
||||||
|
finally:
|
||||||
|
MultiHeadAttention.use_sdpa = prev_state
|
||||||
|
|
||||||
|
|
||||||
|
class MultiHeadAttention(nn.Module):
|
||||||
|
use_sdpa = False # Disable SDPA to ensure qk is always computed for hooks
|
||||||
|
|
||||||
|
def __init__(self, n_state: int, n_head: int, cache_id: str = ""):
|
||||||
|
super().__init__()
|
||||||
|
self.n_head = n_head
|
||||||
|
self.query = Linear(n_state, n_state)
|
||||||
|
self.key = Linear(n_state, n_state, bias=False)
|
||||||
|
self.value = Linear(n_state, n_state)
|
||||||
|
self.out = Linear(n_state, n_state)
|
||||||
|
self.cache_id = cache_id
|
||||||
|
self.key.cache_id = f"{cache_id}_key"
|
||||||
|
self.value.cache_id = f"{cache_id}_value"
|
||||||
|
|
||||||
|
def forward(
|
||||||
|
self,
|
||||||
|
x: Tensor,
|
||||||
|
xa: Optional[Tensor] = None,
|
||||||
|
mask: Optional[Tensor] = None,
|
||||||
|
kv_cache: Optional[dict] = None,
|
||||||
|
):
|
||||||
|
q = self.query(x)
|
||||||
|
|
||||||
|
if kv_cache is None or xa is None or self.key not in kv_cache:
|
||||||
|
# hooks, if installed (i.e. kv_cache is not None), will prepend the cached kv tensors;
|
||||||
|
# otherwise, perform key/value projections for self- or cross-attention as usual.
|
||||||
|
k = self.key(x if xa is None else xa)
|
||||||
|
v = self.value(x if xa is None else xa)
|
||||||
|
else:
|
||||||
|
# for cross-attention, calculate keys and values once and reuse in subsequent calls.
|
||||||
|
k = kv_cache[self.key]
|
||||||
|
v = kv_cache[self.value]
|
||||||
|
|
||||||
|
wv, qk = self.qkv_attention(q, k, v, mask)
|
||||||
|
return self.out(wv), qk
|
||||||
|
|
||||||
|
def qkv_attention(
|
||||||
|
self, q: Tensor, k: Tensor, v: Tensor, mask: Optional[Tensor] = None
|
||||||
|
) -> Tuple[torch.Tensor, Optional[torch.Tensor]]:
|
||||||
|
n_batch, n_ctx, n_state = q.shape
|
||||||
|
scale = (n_state // self.n_head) ** -0.25
|
||||||
|
q = q.view(*q.shape[:2], self.n_head, -1).permute(0, 2, 1, 3)
|
||||||
|
k = k.view(*k.shape[:2], self.n_head, -1).permute(0, 2, 1, 3)
|
||||||
|
v = v.view(*v.shape[:2], self.n_head, -1).permute(0, 2, 1, 3)
|
||||||
|
|
||||||
|
if SDPA_AVAILABLE and MultiHeadAttention.use_sdpa:
|
||||||
|
a = scaled_dot_product_attention(
|
||||||
|
q, k, v, is_causal=mask is not None and n_ctx > 1
|
||||||
|
)
|
||||||
|
out = a.permute(0, 2, 1, 3).flatten(start_dim=2)
|
||||||
|
qk = None
|
||||||
|
else:
|
||||||
|
qk = (q * scale) @ (k * scale).transpose(-1, -2)
|
||||||
|
if mask is not None:
|
||||||
|
qk = qk + mask[:n_ctx, :n_ctx]
|
||||||
|
qk = qk.float()
|
||||||
|
|
||||||
|
w = F.softmax(qk, dim=-1).to(q.dtype)
|
||||||
|
out = (w @ v).permute(0, 2, 1, 3).flatten(start_dim=2)
|
||||||
|
qk = qk.detach()
|
||||||
|
|
||||||
|
return out, qk
|
||||||
|
|
||||||
|
|
||||||
|
class ResidualAttentionBlock(nn.Module):
|
||||||
|
def __init__(self, n_state: int, n_head: int, cross_attention: bool = False, cache_id: str = ""):
|
||||||
|
super().__init__()
|
||||||
|
|
||||||
|
self.attn = MultiHeadAttention(n_state, n_head, cache_id=f"{cache_id}_self_attn")
|
||||||
|
self.attn_ln = LayerNorm(n_state)
|
||||||
|
|
||||||
|
self.cross_attn = (
|
||||||
|
MultiHeadAttention(n_state, n_head, cache_id=f"{cache_id}_cross_attn") if cross_attention else None
|
||||||
|
)
|
||||||
|
self.cross_attn_ln = LayerNorm(n_state) if cross_attention else None
|
||||||
|
|
||||||
|
n_mlp = n_state * 4
|
||||||
|
self.mlp = nn.Sequential(
|
||||||
|
Linear(n_state, n_mlp), nn.GELU(), Linear(n_mlp, n_state)
|
||||||
|
)
|
||||||
|
self.mlp_ln = LayerNorm(n_state)
|
||||||
|
|
||||||
|
def forward(
|
||||||
|
self,
|
||||||
|
x: Tensor,
|
||||||
|
xa: Optional[Tensor] = None,
|
||||||
|
mask: Optional[Tensor] = None,
|
||||||
|
kv_cache: Optional[dict] = None,
|
||||||
|
):
|
||||||
|
x = x + self.attn(self.attn_ln(x), mask=mask, kv_cache=kv_cache)[0]
|
||||||
|
if self.cross_attn:
|
||||||
|
x = x + self.cross_attn(self.cross_attn_ln(x), xa, kv_cache=kv_cache)[0]
|
||||||
|
x = x + self.mlp(self.mlp_ln(x))
|
||||||
|
return x
|
||||||
|
|
||||||
|
|
||||||
|
class AudioEncoder(nn.Module):
|
||||||
|
def __init__(
|
||||||
|
self, n_mels: int, n_ctx: int, n_state: int, n_head: int, n_layer: int
|
||||||
|
):
|
||||||
|
super().__init__()
|
||||||
|
self.conv1 = Conv1d(n_mels, n_state, kernel_size=3, padding=1)
|
||||||
|
self.conv2 = Conv1d(n_state, n_state, kernel_size=3, stride=2, padding=1)
|
||||||
|
self.register_buffer("positional_embedding", sinusoids(n_ctx, n_state))
|
||||||
|
|
||||||
|
self.blocks: Iterable[ResidualAttentionBlock] = nn.ModuleList(
|
||||||
|
[ResidualAttentionBlock(n_state, n_head, cache_id=f"enc_layer{i}") for i in range(n_layer)]
|
||||||
|
)
|
||||||
|
self.ln_post = LayerNorm(n_state)
|
||||||
|
|
||||||
|
def forward(self, x: Tensor):
|
||||||
|
"""
|
||||||
|
x : torch.Tensor, shape = (batch_size, n_mels, n_ctx)
|
||||||
|
the mel spectrogram of the audio
|
||||||
|
"""
|
||||||
|
x = F.gelu(self.conv1(x))
|
||||||
|
x = F.gelu(self.conv2(x))
|
||||||
|
x = x.permute(0, 2, 1)
|
||||||
|
|
||||||
|
assert x.shape[1:] == self.positional_embedding.shape, "incorrect audio shape"
|
||||||
|
x = (x + self.positional_embedding).to(x.dtype)
|
||||||
|
|
||||||
|
for block in self.blocks:
|
||||||
|
x = block(x)
|
||||||
|
|
||||||
|
x = self.ln_post(x)
|
||||||
|
return x
|
||||||
|
|
||||||
|
|
||||||
|
class TextDecoder(nn.Module):
|
||||||
|
def __init__(
|
||||||
|
self, n_vocab: int, n_ctx: int, n_state: int, n_head: int, n_layer: int
|
||||||
|
):
|
||||||
|
super().__init__()
|
||||||
|
|
||||||
|
self.token_embedding = nn.Embedding(n_vocab, n_state)
|
||||||
|
self.positional_embedding = nn.Parameter(torch.empty(n_ctx, n_state))
|
||||||
|
|
||||||
|
self.blocks: Iterable[ResidualAttentionBlock] = nn.ModuleList(
|
||||||
|
[
|
||||||
|
ResidualAttentionBlock(n_state, n_head, cross_attention=True, cache_id=f"dec_layer{i}")
|
||||||
|
for i in range(n_layer)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
self.ln = LayerNorm(n_state)
|
||||||
|
|
||||||
|
mask = torch.empty(n_ctx, n_ctx).fill_(-np.inf).triu_(1)
|
||||||
|
self.register_buffer("mask", mask, persistent=False)
|
||||||
|
|
||||||
|
def forward(self, x: Tensor, xa: Tensor, kv_cache: Optional[dict] = None):
|
||||||
|
"""
|
||||||
|
x : torch.LongTensor, shape = (batch_size, <= n_ctx)
|
||||||
|
the text tokens
|
||||||
|
xa : torch.Tensor, shape = (batch_size, n_audio_ctx, n_audio_state)
|
||||||
|
the encoded audio features to be attended on
|
||||||
|
"""
|
||||||
|
offset = next(iter(kv_cache.values())).shape[1] if kv_cache else 0
|
||||||
|
x = (
|
||||||
|
self.token_embedding(x)
|
||||||
|
+ self.positional_embedding[offset : offset + x.shape[-1]]
|
||||||
|
)
|
||||||
|
x = x.to(xa.dtype)
|
||||||
|
|
||||||
|
for block in self.blocks:
|
||||||
|
x = block(x, xa, mask=self.mask, kv_cache=kv_cache)
|
||||||
|
|
||||||
|
x = self.ln(x)
|
||||||
|
logits = (
|
||||||
|
x @ torch.transpose(self.token_embedding.weight.to(x.dtype), 0, 1)
|
||||||
|
).float()
|
||||||
|
|
||||||
|
return logits
|
||||||
|
|
||||||
|
|
||||||
|
class Whisper(nn.Module):
|
||||||
|
def __init__(self, dims: ModelDimensions):
|
||||||
|
super().__init__()
|
||||||
|
self.dims = dims
|
||||||
|
self.encoder = AudioEncoder(
|
||||||
|
self.dims.n_mels,
|
||||||
|
self.dims.n_audio_ctx,
|
||||||
|
self.dims.n_audio_state,
|
||||||
|
self.dims.n_audio_head,
|
||||||
|
self.dims.n_audio_layer,
|
||||||
|
)
|
||||||
|
self.decoder = TextDecoder(
|
||||||
|
self.dims.n_vocab,
|
||||||
|
self.dims.n_text_ctx,
|
||||||
|
self.dims.n_text_state,
|
||||||
|
self.dims.n_text_head,
|
||||||
|
self.dims.n_text_layer,
|
||||||
|
)
|
||||||
|
# use the last half among the decoder layers for time alignment by default;
|
||||||
|
# to use a specific set of heads, see `set_alignment_heads()` below.
|
||||||
|
all_heads = torch.zeros(
|
||||||
|
self.dims.n_text_layer, self.dims.n_text_head, dtype=torch.bool
|
||||||
|
)
|
||||||
|
all_heads[self.dims.n_text_layer // 2 :] = True
|
||||||
|
self.register_buffer("alignment_heads", all_heads.to_sparse(), persistent=False)
|
||||||
|
|
||||||
|
def set_alignment_heads(self, dump: bytes):
|
||||||
|
array = np.frombuffer(
|
||||||
|
gzip.decompress(base64.b85decode(dump)), dtype=bool
|
||||||
|
).copy()
|
||||||
|
mask = torch.from_numpy(array).reshape(
|
||||||
|
self.dims.n_text_layer, self.dims.n_text_head
|
||||||
|
)
|
||||||
|
self.register_buffer("alignment_heads", mask.to_sparse(), persistent=False)
|
||||||
|
|
||||||
|
def embed_audio(self, mel: torch.Tensor):
|
||||||
|
return self.encoder(mel)
|
||||||
|
|
||||||
|
def logits(self, tokens: torch.Tensor, audio_features: torch.Tensor):
|
||||||
|
return self.decoder(tokens, audio_features)
|
||||||
|
|
||||||
|
def forward(
|
||||||
|
self, mel: torch.Tensor, tokens: torch.Tensor
|
||||||
|
) -> Dict[str, torch.Tensor]:
|
||||||
|
return self.decoder(tokens, self.encoder(mel))
|
||||||
|
|
||||||
|
@property
|
||||||
|
def device(self):
|
||||||
|
return next(self.parameters()).device
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_multilingual(self):
|
||||||
|
return self.dims.n_vocab >= 51865
|
||||||
|
|
||||||
|
@property
|
||||||
|
def num_languages(self):
|
||||||
|
return self.dims.n_vocab - 51765 - int(self.is_multilingual)
|
||||||
|
|
||||||
|
def install_kv_cache_hooks(self, cache: Optional[dict] = None):
|
||||||
|
"""
|
||||||
|
The `MultiHeadAttention` module optionally accepts `kv_cache` which stores the key and value
|
||||||
|
tensors calculated for the previous positions. This method returns a dictionary that stores
|
||||||
|
all caches, and the necessary hooks for the key and value projection modules that save the
|
||||||
|
intermediate tensors to be reused during later calculations.
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
cache : Dict[nn.Module, torch.Tensor]
|
||||||
|
A dictionary object mapping the key/value projection modules to its cache
|
||||||
|
hooks : List[RemovableHandle]
|
||||||
|
List of PyTorch RemovableHandle objects to stop the hooks to be called
|
||||||
|
"""
|
||||||
|
cache = {**cache} if cache is not None else {}
|
||||||
|
hooks = []
|
||||||
|
|
||||||
|
def save_to_cache(module, _, output):
|
||||||
|
if module not in cache or output.shape[1] > self.dims.n_text_ctx:
|
||||||
|
# save as-is, for the first token or cross attention
|
||||||
|
cache[module] = output
|
||||||
|
else:
|
||||||
|
cache[module] = torch.cat([cache[module], output], dim=1).detach()
|
||||||
|
return cache[module]
|
||||||
|
|
||||||
|
def install_hooks(layer: nn.Module):
|
||||||
|
if isinstance(layer, MultiHeadAttention):
|
||||||
|
hooks.append(layer.key.register_forward_hook(save_to_cache))
|
||||||
|
hooks.append(layer.value.register_forward_hook(save_to_cache))
|
||||||
|
|
||||||
|
self.decoder.apply(install_hooks)
|
||||||
|
return cache, hooks
|
||||||
|
|
||||||
|
detect_language = detect_language_function
|
||||||
|
transcribe = transcribe_function
|
||||||
|
decode = decode_function
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
from .basic import BasicTextNormalizer as BasicTextNormalizer
|
||||||
|
from .english import EnglishTextNormalizer as EnglishTextNormalizer
|
||||||
80
whisperlivekit/simul_whisper/whisper/normalizers/basic.py
Normal file
80
whisperlivekit/simul_whisper/whisper/normalizers/basic.py
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
import re
|
||||||
|
import unicodedata
|
||||||
|
|
||||||
|
import regex
|
||||||
|
|
||||||
|
# non-ASCII letters that are not separated by "NFKD" normalization
|
||||||
|
ADDITIONAL_DIACRITICS = {
|
||||||
|
"œ": "oe",
|
||||||
|
"Œ": "OE",
|
||||||
|
"ø": "o",
|
||||||
|
"Ø": "O",
|
||||||
|
"æ": "ae",
|
||||||
|
"Æ": "AE",
|
||||||
|
"ß": "ss",
|
||||||
|
"ẞ": "SS",
|
||||||
|
"đ": "d",
|
||||||
|
"Đ": "D",
|
||||||
|
"ð": "d",
|
||||||
|
"Ð": "D",
|
||||||
|
"þ": "th",
|
||||||
|
"Þ": "th",
|
||||||
|
"ł": "l",
|
||||||
|
"Ł": "L",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def remove_symbols_and_diacritics(s: str, keep=""):
|
||||||
|
"""
|
||||||
|
Replace any other markers, symbols, and punctuations with a space,
|
||||||
|
and drop any diacritics (category 'Mn' and some manual mappings)
|
||||||
|
"""
|
||||||
|
return "".join(
|
||||||
|
(
|
||||||
|
c
|
||||||
|
if c in keep
|
||||||
|
else (
|
||||||
|
ADDITIONAL_DIACRITICS[c]
|
||||||
|
if c in ADDITIONAL_DIACRITICS
|
||||||
|
else (
|
||||||
|
""
|
||||||
|
if unicodedata.category(c) == "Mn"
|
||||||
|
else " " if unicodedata.category(c)[0] in "MSP" else c
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
for c in unicodedata.normalize("NFKD", s)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def remove_symbols(s: str):
|
||||||
|
"""
|
||||||
|
Replace any other markers, symbols, punctuations with a space, keeping diacritics
|
||||||
|
"""
|
||||||
|
return "".join(
|
||||||
|
" " if unicodedata.category(c)[0] in "MSP" else c
|
||||||
|
for c in unicodedata.normalize("NFKC", s)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class BasicTextNormalizer:
|
||||||
|
def __init__(self, remove_diacritics: bool = False, split_letters: bool = False):
|
||||||
|
self.clean = (
|
||||||
|
remove_symbols_and_diacritics if remove_diacritics else remove_symbols
|
||||||
|
)
|
||||||
|
self.split_letters = split_letters
|
||||||
|
|
||||||
|
def __call__(self, s: str):
|
||||||
|
s = s.lower()
|
||||||
|
s = re.sub(r"[<\[][^>\]]*[>\]]", "", s) # remove words between brackets
|
||||||
|
s = re.sub(r"\(([^)]+?)\)", "", s) # remove words between parenthesis
|
||||||
|
s = self.clean(s).lower()
|
||||||
|
|
||||||
|
if self.split_letters:
|
||||||
|
s = " ".join(regex.findall(r"\X", s, regex.U))
|
||||||
|
|
||||||
|
s = re.sub(
|
||||||
|
r"\s+", " ", s
|
||||||
|
) # replace any successive whitespace characters with a space
|
||||||
|
|
||||||
|
return s
|
||||||
1741
whisperlivekit/simul_whisper/whisper/normalizers/english.json
Normal file
1741
whisperlivekit/simul_whisper/whisper/normalizers/english.json
Normal file
File diff suppressed because it is too large
Load Diff
550
whisperlivekit/simul_whisper/whisper/normalizers/english.py
Normal file
550
whisperlivekit/simul_whisper/whisper/normalizers/english.py
Normal file
@@ -0,0 +1,550 @@
|
|||||||
|
import json
|
||||||
|
import os
|
||||||
|
import re
|
||||||
|
from fractions import Fraction
|
||||||
|
from typing import Iterator, List, Match, Optional, Union
|
||||||
|
|
||||||
|
from more_itertools import windowed
|
||||||
|
|
||||||
|
from .basic import remove_symbols_and_diacritics
|
||||||
|
|
||||||
|
|
||||||
|
class EnglishNumberNormalizer:
|
||||||
|
"""
|
||||||
|
Convert any spelled-out numbers into arabic numbers, while handling:
|
||||||
|
|
||||||
|
- remove any commas
|
||||||
|
- keep the suffixes such as: `1960s`, `274th`, `32nd`, etc.
|
||||||
|
- spell out currency symbols after the number. e.g. `$20 million` -> `20000000 dollars`
|
||||||
|
- spell out `one` and `ones`
|
||||||
|
- interpret successive single-digit numbers as nominal: `one oh one` -> `101`
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__()
|
||||||
|
|
||||||
|
self.zeros = {"o", "oh", "zero"}
|
||||||
|
self.ones = {
|
||||||
|
name: i
|
||||||
|
for i, name in enumerate(
|
||||||
|
[
|
||||||
|
"one",
|
||||||
|
"two",
|
||||||
|
"three",
|
||||||
|
"four",
|
||||||
|
"five",
|
||||||
|
"six",
|
||||||
|
"seven",
|
||||||
|
"eight",
|
||||||
|
"nine",
|
||||||
|
"ten",
|
||||||
|
"eleven",
|
||||||
|
"twelve",
|
||||||
|
"thirteen",
|
||||||
|
"fourteen",
|
||||||
|
"fifteen",
|
||||||
|
"sixteen",
|
||||||
|
"seventeen",
|
||||||
|
"eighteen",
|
||||||
|
"nineteen",
|
||||||
|
],
|
||||||
|
start=1,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
self.ones_plural = {
|
||||||
|
"sixes" if name == "six" else name + "s": (value, "s")
|
||||||
|
for name, value in self.ones.items()
|
||||||
|
}
|
||||||
|
self.ones_ordinal = {
|
||||||
|
"zeroth": (0, "th"),
|
||||||
|
"first": (1, "st"),
|
||||||
|
"second": (2, "nd"),
|
||||||
|
"third": (3, "rd"),
|
||||||
|
"fifth": (5, "th"),
|
||||||
|
"twelfth": (12, "th"),
|
||||||
|
**{
|
||||||
|
name + ("h" if name.endswith("t") else "th"): (value, "th")
|
||||||
|
for name, value in self.ones.items()
|
||||||
|
if value > 3 and value != 5 and value != 12
|
||||||
|
},
|
||||||
|
}
|
||||||
|
self.ones_suffixed = {**self.ones_plural, **self.ones_ordinal}
|
||||||
|
|
||||||
|
self.tens = {
|
||||||
|
"twenty": 20,
|
||||||
|
"thirty": 30,
|
||||||
|
"forty": 40,
|
||||||
|
"fifty": 50,
|
||||||
|
"sixty": 60,
|
||||||
|
"seventy": 70,
|
||||||
|
"eighty": 80,
|
||||||
|
"ninety": 90,
|
||||||
|
}
|
||||||
|
self.tens_plural = {
|
||||||
|
name.replace("y", "ies"): (value, "s") for name, value in self.tens.items()
|
||||||
|
}
|
||||||
|
self.tens_ordinal = {
|
||||||
|
name.replace("y", "ieth"): (value, "th")
|
||||||
|
for name, value in self.tens.items()
|
||||||
|
}
|
||||||
|
self.tens_suffixed = {**self.tens_plural, **self.tens_ordinal}
|
||||||
|
|
||||||
|
self.multipliers = {
|
||||||
|
"hundred": 100,
|
||||||
|
"thousand": 1_000,
|
||||||
|
"million": 1_000_000,
|
||||||
|
"billion": 1_000_000_000,
|
||||||
|
"trillion": 1_000_000_000_000,
|
||||||
|
"quadrillion": 1_000_000_000_000_000,
|
||||||
|
"quintillion": 1_000_000_000_000_000_000,
|
||||||
|
"sextillion": 1_000_000_000_000_000_000_000,
|
||||||
|
"septillion": 1_000_000_000_000_000_000_000_000,
|
||||||
|
"octillion": 1_000_000_000_000_000_000_000_000_000,
|
||||||
|
"nonillion": 1_000_000_000_000_000_000_000_000_000_000,
|
||||||
|
"decillion": 1_000_000_000_000_000_000_000_000_000_000_000,
|
||||||
|
}
|
||||||
|
self.multipliers_plural = {
|
||||||
|
name + "s": (value, "s") for name, value in self.multipliers.items()
|
||||||
|
}
|
||||||
|
self.multipliers_ordinal = {
|
||||||
|
name + "th": (value, "th") for name, value in self.multipliers.items()
|
||||||
|
}
|
||||||
|
self.multipliers_suffixed = {
|
||||||
|
**self.multipliers_plural,
|
||||||
|
**self.multipliers_ordinal,
|
||||||
|
}
|
||||||
|
self.decimals = {*self.ones, *self.tens, *self.zeros}
|
||||||
|
|
||||||
|
self.preceding_prefixers = {
|
||||||
|
"minus": "-",
|
||||||
|
"negative": "-",
|
||||||
|
"plus": "+",
|
||||||
|
"positive": "+",
|
||||||
|
}
|
||||||
|
self.following_prefixers = {
|
||||||
|
"pound": "£",
|
||||||
|
"pounds": "£",
|
||||||
|
"euro": "€",
|
||||||
|
"euros": "€",
|
||||||
|
"dollar": "$",
|
||||||
|
"dollars": "$",
|
||||||
|
"cent": "¢",
|
||||||
|
"cents": "¢",
|
||||||
|
}
|
||||||
|
self.prefixes = set(
|
||||||
|
list(self.preceding_prefixers.values())
|
||||||
|
+ list(self.following_prefixers.values())
|
||||||
|
)
|
||||||
|
self.suffixers = {
|
||||||
|
"per": {"cent": "%"},
|
||||||
|
"percent": "%",
|
||||||
|
}
|
||||||
|
self.specials = {"and", "double", "triple", "point"}
|
||||||
|
|
||||||
|
self.words = set(
|
||||||
|
[
|
||||||
|
key
|
||||||
|
for mapping in [
|
||||||
|
self.zeros,
|
||||||
|
self.ones,
|
||||||
|
self.ones_suffixed,
|
||||||
|
self.tens,
|
||||||
|
self.tens_suffixed,
|
||||||
|
self.multipliers,
|
||||||
|
self.multipliers_suffixed,
|
||||||
|
self.preceding_prefixers,
|
||||||
|
self.following_prefixers,
|
||||||
|
self.suffixers,
|
||||||
|
self.specials,
|
||||||
|
]
|
||||||
|
for key in mapping
|
||||||
|
]
|
||||||
|
)
|
||||||
|
self.literal_words = {"one", "ones"}
|
||||||
|
|
||||||
|
def process_words(self, words: List[str]) -> Iterator[str]:
|
||||||
|
prefix: Optional[str] = None
|
||||||
|
value: Optional[Union[str, int]] = None
|
||||||
|
skip = False
|
||||||
|
|
||||||
|
def to_fraction(s: str):
|
||||||
|
try:
|
||||||
|
return Fraction(s)
|
||||||
|
except ValueError:
|
||||||
|
return None
|
||||||
|
|
||||||
|
def output(result: Union[str, int]):
|
||||||
|
nonlocal prefix, value
|
||||||
|
result = str(result)
|
||||||
|
if prefix is not None:
|
||||||
|
result = prefix + result
|
||||||
|
value = None
|
||||||
|
prefix = None
|
||||||
|
return result
|
||||||
|
|
||||||
|
if len(words) == 0:
|
||||||
|
return
|
||||||
|
|
||||||
|
for prev, current, next in windowed([None] + words + [None], 3):
|
||||||
|
if skip:
|
||||||
|
skip = False
|
||||||
|
continue
|
||||||
|
|
||||||
|
next_is_numeric = next is not None and re.match(r"^\d+(\.\d+)?$", next)
|
||||||
|
has_prefix = current[0] in self.prefixes
|
||||||
|
current_without_prefix = current[1:] if has_prefix else current
|
||||||
|
if re.match(r"^\d+(\.\d+)?$", current_without_prefix):
|
||||||
|
# arabic numbers (potentially with signs and fractions)
|
||||||
|
f = to_fraction(current_without_prefix)
|
||||||
|
assert f is not None
|
||||||
|
if value is not None:
|
||||||
|
if isinstance(value, str) and value.endswith("."):
|
||||||
|
# concatenate decimals / ip address components
|
||||||
|
value = str(value) + str(current)
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
yield output(value)
|
||||||
|
|
||||||
|
prefix = current[0] if has_prefix else prefix
|
||||||
|
if f.denominator == 1:
|
||||||
|
value = f.numerator # store integers as int
|
||||||
|
else:
|
||||||
|
value = current_without_prefix
|
||||||
|
elif current not in self.words:
|
||||||
|
# non-numeric words
|
||||||
|
if value is not None:
|
||||||
|
yield output(value)
|
||||||
|
yield output(current)
|
||||||
|
elif current in self.zeros:
|
||||||
|
value = str(value or "") + "0"
|
||||||
|
elif current in self.ones:
|
||||||
|
ones = self.ones[current]
|
||||||
|
|
||||||
|
if value is None:
|
||||||
|
value = ones
|
||||||
|
elif isinstance(value, str) or prev in self.ones:
|
||||||
|
if (
|
||||||
|
prev in self.tens and ones < 10
|
||||||
|
): # replace the last zero with the digit
|
||||||
|
assert value[-1] == "0"
|
||||||
|
value = value[:-1] + str(ones)
|
||||||
|
else:
|
||||||
|
value = str(value) + str(ones)
|
||||||
|
elif ones < 10:
|
||||||
|
if value % 10 == 0:
|
||||||
|
value += ones
|
||||||
|
else:
|
||||||
|
value = str(value) + str(ones)
|
||||||
|
else: # eleven to nineteen
|
||||||
|
if value % 100 == 0:
|
||||||
|
value += ones
|
||||||
|
else:
|
||||||
|
value = str(value) + str(ones)
|
||||||
|
elif current in self.ones_suffixed:
|
||||||
|
# ordinal or cardinal; yield the number right away
|
||||||
|
ones, suffix = self.ones_suffixed[current]
|
||||||
|
if value is None:
|
||||||
|
yield output(str(ones) + suffix)
|
||||||
|
elif isinstance(value, str) or prev in self.ones:
|
||||||
|
if prev in self.tens and ones < 10:
|
||||||
|
assert value[-1] == "0"
|
||||||
|
yield output(value[:-1] + str(ones) + suffix)
|
||||||
|
else:
|
||||||
|
yield output(str(value) + str(ones) + suffix)
|
||||||
|
elif ones < 10:
|
||||||
|
if value % 10 == 0:
|
||||||
|
yield output(str(value + ones) + suffix)
|
||||||
|
else:
|
||||||
|
yield output(str(value) + str(ones) + suffix)
|
||||||
|
else: # eleven to nineteen
|
||||||
|
if value % 100 == 0:
|
||||||
|
yield output(str(value + ones) + suffix)
|
||||||
|
else:
|
||||||
|
yield output(str(value) + str(ones) + suffix)
|
||||||
|
value = None
|
||||||
|
elif current in self.tens:
|
||||||
|
tens = self.tens[current]
|
||||||
|
if value is None:
|
||||||
|
value = tens
|
||||||
|
elif isinstance(value, str):
|
||||||
|
value = str(value) + str(tens)
|
||||||
|
else:
|
||||||
|
if value % 100 == 0:
|
||||||
|
value += tens
|
||||||
|
else:
|
||||||
|
value = str(value) + str(tens)
|
||||||
|
elif current in self.tens_suffixed:
|
||||||
|
# ordinal or cardinal; yield the number right away
|
||||||
|
tens, suffix = self.tens_suffixed[current]
|
||||||
|
if value is None:
|
||||||
|
yield output(str(tens) + suffix)
|
||||||
|
elif isinstance(value, str):
|
||||||
|
yield output(str(value) + str(tens) + suffix)
|
||||||
|
else:
|
||||||
|
if value % 100 == 0:
|
||||||
|
yield output(str(value + tens) + suffix)
|
||||||
|
else:
|
||||||
|
yield output(str(value) + str(tens) + suffix)
|
||||||
|
elif current in self.multipliers:
|
||||||
|
multiplier = self.multipliers[current]
|
||||||
|
if value is None:
|
||||||
|
value = multiplier
|
||||||
|
elif isinstance(value, str) or value == 0:
|
||||||
|
f = to_fraction(value)
|
||||||
|
p = f * multiplier if f is not None else None
|
||||||
|
if f is not None and p.denominator == 1:
|
||||||
|
value = p.numerator
|
||||||
|
else:
|
||||||
|
yield output(value)
|
||||||
|
value = multiplier
|
||||||
|
else:
|
||||||
|
before = value // 1000 * 1000
|
||||||
|
residual = value % 1000
|
||||||
|
value = before + residual * multiplier
|
||||||
|
elif current in self.multipliers_suffixed:
|
||||||
|
multiplier, suffix = self.multipliers_suffixed[current]
|
||||||
|
if value is None:
|
||||||
|
yield output(str(multiplier) + suffix)
|
||||||
|
elif isinstance(value, str):
|
||||||
|
f = to_fraction(value)
|
||||||
|
p = f * multiplier if f is not None else None
|
||||||
|
if f is not None and p.denominator == 1:
|
||||||
|
yield output(str(p.numerator) + suffix)
|
||||||
|
else:
|
||||||
|
yield output(value)
|
||||||
|
yield output(str(multiplier) + suffix)
|
||||||
|
else: # int
|
||||||
|
before = value // 1000 * 1000
|
||||||
|
residual = value % 1000
|
||||||
|
value = before + residual * multiplier
|
||||||
|
yield output(str(value) + suffix)
|
||||||
|
value = None
|
||||||
|
elif current in self.preceding_prefixers:
|
||||||
|
# apply prefix (positive, minus, etc.) if it precedes a number
|
||||||
|
if value is not None:
|
||||||
|
yield output(value)
|
||||||
|
|
||||||
|
if next in self.words or next_is_numeric:
|
||||||
|
prefix = self.preceding_prefixers[current]
|
||||||
|
else:
|
||||||
|
yield output(current)
|
||||||
|
elif current in self.following_prefixers:
|
||||||
|
# apply prefix (dollars, cents, etc.) only after a number
|
||||||
|
if value is not None:
|
||||||
|
prefix = self.following_prefixers[current]
|
||||||
|
yield output(value)
|
||||||
|
else:
|
||||||
|
yield output(current)
|
||||||
|
elif current in self.suffixers:
|
||||||
|
# apply suffix symbols (percent -> '%')
|
||||||
|
if value is not None:
|
||||||
|
suffix = self.suffixers[current]
|
||||||
|
if isinstance(suffix, dict):
|
||||||
|
if next in suffix:
|
||||||
|
yield output(str(value) + suffix[next])
|
||||||
|
skip = True
|
||||||
|
else:
|
||||||
|
yield output(value)
|
||||||
|
yield output(current)
|
||||||
|
else:
|
||||||
|
yield output(str(value) + suffix)
|
||||||
|
else:
|
||||||
|
yield output(current)
|
||||||
|
elif current in self.specials:
|
||||||
|
if next not in self.words and not next_is_numeric:
|
||||||
|
# apply special handling only if the next word can be numeric
|
||||||
|
if value is not None:
|
||||||
|
yield output(value)
|
||||||
|
yield output(current)
|
||||||
|
elif current == "and":
|
||||||
|
# ignore "and" after hundreds, thousands, etc.
|
||||||
|
if prev not in self.multipliers:
|
||||||
|
if value is not None:
|
||||||
|
yield output(value)
|
||||||
|
yield output(current)
|
||||||
|
elif current == "double" or current == "triple":
|
||||||
|
if next in self.ones or next in self.zeros:
|
||||||
|
repeats = 2 if current == "double" else 3
|
||||||
|
ones = self.ones.get(next, 0)
|
||||||
|
value = str(value or "") + str(ones) * repeats
|
||||||
|
skip = True
|
||||||
|
else:
|
||||||
|
if value is not None:
|
||||||
|
yield output(value)
|
||||||
|
yield output(current)
|
||||||
|
elif current == "point":
|
||||||
|
if next in self.decimals or next_is_numeric:
|
||||||
|
value = str(value or "") + "."
|
||||||
|
else:
|
||||||
|
# should all have been covered at this point
|
||||||
|
raise ValueError(f"Unexpected token: {current}")
|
||||||
|
else:
|
||||||
|
# all should have been covered at this point
|
||||||
|
raise ValueError(f"Unexpected token: {current}")
|
||||||
|
|
||||||
|
if value is not None:
|
||||||
|
yield output(value)
|
||||||
|
|
||||||
|
def preprocess(self, s: str):
|
||||||
|
# replace "<number> and a half" with "<number> point five"
|
||||||
|
results = []
|
||||||
|
|
||||||
|
segments = re.split(r"\band\s+a\s+half\b", s)
|
||||||
|
for i, segment in enumerate(segments):
|
||||||
|
if len(segment.strip()) == 0:
|
||||||
|
continue
|
||||||
|
if i == len(segments) - 1:
|
||||||
|
results.append(segment)
|
||||||
|
else:
|
||||||
|
results.append(segment)
|
||||||
|
last_word = segment.rsplit(maxsplit=2)[-1]
|
||||||
|
if last_word in self.decimals or last_word in self.multipliers:
|
||||||
|
results.append("point five")
|
||||||
|
else:
|
||||||
|
results.append("and a half")
|
||||||
|
|
||||||
|
s = " ".join(results)
|
||||||
|
|
||||||
|
# put a space at number/letter boundary
|
||||||
|
s = re.sub(r"([a-z])([0-9])", r"\1 \2", s)
|
||||||
|
s = re.sub(r"([0-9])([a-z])", r"\1 \2", s)
|
||||||
|
|
||||||
|
# but remove spaces which could be a suffix
|
||||||
|
s = re.sub(r"([0-9])\s+(st|nd|rd|th|s)\b", r"\1\2", s)
|
||||||
|
|
||||||
|
return s
|
||||||
|
|
||||||
|
def postprocess(self, s: str):
|
||||||
|
def combine_cents(m: Match):
|
||||||
|
try:
|
||||||
|
currency = m.group(1)
|
||||||
|
integer = m.group(2)
|
||||||
|
cents = int(m.group(3))
|
||||||
|
return f"{currency}{integer}.{cents:02d}"
|
||||||
|
except ValueError:
|
||||||
|
return m.string
|
||||||
|
|
||||||
|
def extract_cents(m: Match):
|
||||||
|
try:
|
||||||
|
return f"¢{int(m.group(1))}"
|
||||||
|
except ValueError:
|
||||||
|
return m.string
|
||||||
|
|
||||||
|
# apply currency postprocessing; "$2 and ¢7" -> "$2.07"
|
||||||
|
s = re.sub(r"([€£$])([0-9]+) (?:and )?¢([0-9]{1,2})\b", combine_cents, s)
|
||||||
|
s = re.sub(r"[€£$]0.([0-9]{1,2})\b", extract_cents, s)
|
||||||
|
|
||||||
|
# write "one(s)" instead of "1(s)", just for the readability
|
||||||
|
s = re.sub(r"\b1(s?)\b", r"one\1", s)
|
||||||
|
|
||||||
|
return s
|
||||||
|
|
||||||
|
def __call__(self, s: str):
|
||||||
|
s = self.preprocess(s)
|
||||||
|
s = " ".join(word for word in self.process_words(s.split()) if word is not None)
|
||||||
|
s = self.postprocess(s)
|
||||||
|
|
||||||
|
return s
|
||||||
|
|
||||||
|
|
||||||
|
class EnglishSpellingNormalizer:
|
||||||
|
"""
|
||||||
|
Applies British-American spelling mappings as listed in [1].
|
||||||
|
|
||||||
|
[1] https://www.tysto.com/uk-us-spelling-list.html
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
mapping_path = os.path.join(os.path.dirname(__file__), "english.json")
|
||||||
|
self.mapping = json.load(open(mapping_path))
|
||||||
|
|
||||||
|
def __call__(self, s: str):
|
||||||
|
return " ".join(self.mapping.get(word, word) for word in s.split())
|
||||||
|
|
||||||
|
|
||||||
|
class EnglishTextNormalizer:
|
||||||
|
def __init__(self):
|
||||||
|
self.ignore_patterns = r"\b(hmm|mm|mhm|mmm|uh|um)\b"
|
||||||
|
self.replacers = {
|
||||||
|
# common contractions
|
||||||
|
r"\bwon't\b": "will not",
|
||||||
|
r"\bcan't\b": "can not",
|
||||||
|
r"\blet's\b": "let us",
|
||||||
|
r"\bain't\b": "aint",
|
||||||
|
r"\by'all\b": "you all",
|
||||||
|
r"\bwanna\b": "want to",
|
||||||
|
r"\bgotta\b": "got to",
|
||||||
|
r"\bgonna\b": "going to",
|
||||||
|
r"\bi'ma\b": "i am going to",
|
||||||
|
r"\bimma\b": "i am going to",
|
||||||
|
r"\bwoulda\b": "would have",
|
||||||
|
r"\bcoulda\b": "could have",
|
||||||
|
r"\bshoulda\b": "should have",
|
||||||
|
r"\bma'am\b": "madam",
|
||||||
|
# contractions in titles/prefixes
|
||||||
|
r"\bmr\b": "mister ",
|
||||||
|
r"\bmrs\b": "missus ",
|
||||||
|
r"\bst\b": "saint ",
|
||||||
|
r"\bdr\b": "doctor ",
|
||||||
|
r"\bprof\b": "professor ",
|
||||||
|
r"\bcapt\b": "captain ",
|
||||||
|
r"\bgov\b": "governor ",
|
||||||
|
r"\bald\b": "alderman ",
|
||||||
|
r"\bgen\b": "general ",
|
||||||
|
r"\bsen\b": "senator ",
|
||||||
|
r"\brep\b": "representative ",
|
||||||
|
r"\bpres\b": "president ",
|
||||||
|
r"\brev\b": "reverend ",
|
||||||
|
r"\bhon\b": "honorable ",
|
||||||
|
r"\basst\b": "assistant ",
|
||||||
|
r"\bassoc\b": "associate ",
|
||||||
|
r"\blt\b": "lieutenant ",
|
||||||
|
r"\bcol\b": "colonel ",
|
||||||
|
r"\bjr\b": "junior ",
|
||||||
|
r"\bsr\b": "senior ",
|
||||||
|
r"\besq\b": "esquire ",
|
||||||
|
# prefect tenses, ideally it should be any past participles, but it's harder..
|
||||||
|
r"'d been\b": " had been",
|
||||||
|
r"'s been\b": " has been",
|
||||||
|
r"'d gone\b": " had gone",
|
||||||
|
r"'s gone\b": " has gone",
|
||||||
|
r"'d done\b": " had done", # "'s done" is ambiguous
|
||||||
|
r"'s got\b": " has got",
|
||||||
|
# general contractions
|
||||||
|
r"n't\b": " not",
|
||||||
|
r"'re\b": " are",
|
||||||
|
r"'s\b": " is",
|
||||||
|
r"'d\b": " would",
|
||||||
|
r"'ll\b": " will",
|
||||||
|
r"'t\b": " not",
|
||||||
|
r"'ve\b": " have",
|
||||||
|
r"'m\b": " am",
|
||||||
|
}
|
||||||
|
self.standardize_numbers = EnglishNumberNormalizer()
|
||||||
|
self.standardize_spellings = EnglishSpellingNormalizer()
|
||||||
|
|
||||||
|
def __call__(self, s: str):
|
||||||
|
s = s.lower()
|
||||||
|
|
||||||
|
s = re.sub(r"[<\[][^>\]]*[>\]]", "", s) # remove words between brackets
|
||||||
|
s = re.sub(r"\(([^)]+?)\)", "", s) # remove words between parenthesis
|
||||||
|
s = re.sub(self.ignore_patterns, "", s)
|
||||||
|
s = re.sub(r"\s+'", "'", s) # when there's a space before an apostrophe
|
||||||
|
|
||||||
|
for pattern, replacement in self.replacers.items():
|
||||||
|
s = re.sub(pattern, replacement, s)
|
||||||
|
|
||||||
|
s = re.sub(r"(\d),(\d)", r"\1\2", s) # remove commas between digits
|
||||||
|
s = re.sub(r"\.([^0-9]|$)", r" \1", s) # remove periods not followed by numbers
|
||||||
|
s = remove_symbols_and_diacritics(s, keep=".%$¢€£") # keep numeric symbols
|
||||||
|
|
||||||
|
s = self.standardize_numbers(s)
|
||||||
|
s = self.standardize_spellings(s)
|
||||||
|
|
||||||
|
# now remove prefix/suffix symbols that are not preceded/followed by numbers
|
||||||
|
s = re.sub(r"[.$¢€£]([^0-9])", r" \1", s)
|
||||||
|
s = re.sub(r"([^0-9])%", r"\1 ", s)
|
||||||
|
|
||||||
|
s = re.sub(r"\s+", " ", s) # replace any successive whitespaces with a space
|
||||||
|
|
||||||
|
return s
|
||||||
388
whisperlivekit/simul_whisper/whisper/timing.py
Normal file
388
whisperlivekit/simul_whisper/whisper/timing.py
Normal file
@@ -0,0 +1,388 @@
|
|||||||
|
import itertools
|
||||||
|
import subprocess
|
||||||
|
import warnings
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from typing import TYPE_CHECKING, List
|
||||||
|
|
||||||
|
import numba
|
||||||
|
import numpy as np
|
||||||
|
import torch
|
||||||
|
import torch.nn.functional as F
|
||||||
|
|
||||||
|
from .audio import HOP_LENGTH, SAMPLE_RATE, TOKENS_PER_SECOND
|
||||||
|
from .tokenizer import Tokenizer
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from .model import Whisper
|
||||||
|
|
||||||
|
|
||||||
|
def median_filter(x: torch.Tensor, filter_width: int):
|
||||||
|
"""Apply a median filter of width `filter_width` along the last dimension of `x`"""
|
||||||
|
pad_width = filter_width // 2
|
||||||
|
if x.shape[-1] <= pad_width:
|
||||||
|
# F.pad requires the padding width to be smaller than the input dimension
|
||||||
|
return x
|
||||||
|
|
||||||
|
if (ndim := x.ndim) <= 2:
|
||||||
|
# `F.pad` does not support 1D or 2D inputs for reflect padding but supports 3D and 4D
|
||||||
|
x = x[None, None, :]
|
||||||
|
|
||||||
|
assert (
|
||||||
|
filter_width > 0 and filter_width % 2 == 1
|
||||||
|
), "`filter_width` should be an odd number"
|
||||||
|
|
||||||
|
result = None
|
||||||
|
x = F.pad(x, (filter_width // 2, filter_width // 2, 0, 0), mode="reflect")
|
||||||
|
if x.is_cuda:
|
||||||
|
try:
|
||||||
|
from .triton_ops import median_filter_cuda
|
||||||
|
|
||||||
|
result = median_filter_cuda(x, filter_width)
|
||||||
|
except (RuntimeError, subprocess.CalledProcessError):
|
||||||
|
warnings.warn(
|
||||||
|
"Failed to launch Triton kernels, likely due to missing CUDA toolkit; "
|
||||||
|
"falling back to a slower median kernel implementation..."
|
||||||
|
)
|
||||||
|
|
||||||
|
if result is None:
|
||||||
|
# sort() is faster than torch.median (https://github.com/pytorch/pytorch/issues/51450)
|
||||||
|
result = x.unfold(-1, filter_width, 1).sort()[0][..., filter_width // 2]
|
||||||
|
|
||||||
|
if ndim <= 2:
|
||||||
|
result = result[0, 0]
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
@numba.jit(nopython=True)
|
||||||
|
def backtrace(trace: np.ndarray):
|
||||||
|
i = trace.shape[0] - 1
|
||||||
|
j = trace.shape[1] - 1
|
||||||
|
trace[0, :] = 2
|
||||||
|
trace[:, 0] = 1
|
||||||
|
|
||||||
|
result = []
|
||||||
|
while i > 0 or j > 0:
|
||||||
|
result.append((i - 1, j - 1))
|
||||||
|
|
||||||
|
if trace[i, j] == 0:
|
||||||
|
i -= 1
|
||||||
|
j -= 1
|
||||||
|
elif trace[i, j] == 1:
|
||||||
|
i -= 1
|
||||||
|
elif trace[i, j] == 2:
|
||||||
|
j -= 1
|
||||||
|
else:
|
||||||
|
raise ValueError("Unexpected trace[i, j]")
|
||||||
|
|
||||||
|
result = np.array(result)
|
||||||
|
return result[::-1, :].T
|
||||||
|
|
||||||
|
|
||||||
|
@numba.jit(nopython=True, parallel=True)
|
||||||
|
def dtw_cpu(x: np.ndarray):
|
||||||
|
N, M = x.shape
|
||||||
|
cost = np.ones((N + 1, M + 1), dtype=np.float32) * np.inf
|
||||||
|
trace = -np.ones((N + 1, M + 1), dtype=np.float32)
|
||||||
|
|
||||||
|
cost[0, 0] = 0
|
||||||
|
for j in range(1, M + 1):
|
||||||
|
for i in range(1, N + 1):
|
||||||
|
c0 = cost[i - 1, j - 1]
|
||||||
|
c1 = cost[i - 1, j]
|
||||||
|
c2 = cost[i, j - 1]
|
||||||
|
|
||||||
|
if c0 < c1 and c0 < c2:
|
||||||
|
c, t = c0, 0
|
||||||
|
elif c1 < c0 and c1 < c2:
|
||||||
|
c, t = c1, 1
|
||||||
|
else:
|
||||||
|
c, t = c2, 2
|
||||||
|
|
||||||
|
cost[i, j] = x[i - 1, j - 1] + c
|
||||||
|
trace[i, j] = t
|
||||||
|
|
||||||
|
return backtrace(trace)
|
||||||
|
|
||||||
|
|
||||||
|
def dtw_cuda(x, BLOCK_SIZE=1024):
|
||||||
|
from .triton_ops import dtw_kernel
|
||||||
|
|
||||||
|
M, N = x.shape
|
||||||
|
assert M < BLOCK_SIZE, f"M should be smaller than {BLOCK_SIZE=}"
|
||||||
|
|
||||||
|
x_skew = (
|
||||||
|
F.pad(x, (0, M + 1), value=np.inf).flatten()[: M * (N + M)].reshape(M, N + M)
|
||||||
|
)
|
||||||
|
x_skew = x_skew.T.contiguous()
|
||||||
|
cost = torch.ones(N + M + 2, M + 2) * np.inf
|
||||||
|
cost[0, 0] = 0
|
||||||
|
cost = cost.to(x.device)
|
||||||
|
trace = torch.zeros_like(cost, dtype=torch.int32)
|
||||||
|
|
||||||
|
dtw_kernel[(1,)](
|
||||||
|
cost,
|
||||||
|
trace,
|
||||||
|
x_skew,
|
||||||
|
x_skew.stride(0),
|
||||||
|
cost.stride(0),
|
||||||
|
trace.stride(0),
|
||||||
|
N,
|
||||||
|
M,
|
||||||
|
BLOCK_SIZE=BLOCK_SIZE,
|
||||||
|
)
|
||||||
|
|
||||||
|
trace = trace.T.flatten()[: (M + 1) * (M + N + 3)].reshape(M + 1, M + N + 3)[
|
||||||
|
:, : N + 1
|
||||||
|
]
|
||||||
|
return backtrace(trace.cpu().numpy())
|
||||||
|
|
||||||
|
|
||||||
|
def dtw(x: torch.Tensor) -> np.ndarray:
|
||||||
|
if x.is_cuda:
|
||||||
|
try:
|
||||||
|
return dtw_cuda(x)
|
||||||
|
except (RuntimeError, subprocess.CalledProcessError):
|
||||||
|
warnings.warn(
|
||||||
|
"Failed to launch Triton kernels, likely due to missing CUDA toolkit; "
|
||||||
|
"falling back to a slower DTW implementation..."
|
||||||
|
)
|
||||||
|
|
||||||
|
return dtw_cpu(x.double().cpu().numpy())
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class WordTiming:
|
||||||
|
word: str
|
||||||
|
tokens: List[int]
|
||||||
|
start: float
|
||||||
|
end: float
|
||||||
|
probability: float
|
||||||
|
|
||||||
|
|
||||||
|
def find_alignment(
|
||||||
|
model: "Whisper",
|
||||||
|
tokenizer: Tokenizer,
|
||||||
|
text_tokens: List[int],
|
||||||
|
mel: torch.Tensor,
|
||||||
|
num_frames: int,
|
||||||
|
*,
|
||||||
|
medfilt_width: int = 7,
|
||||||
|
qk_scale: float = 1.0,
|
||||||
|
) -> List[WordTiming]:
|
||||||
|
if len(text_tokens) == 0:
|
||||||
|
return []
|
||||||
|
|
||||||
|
tokens = torch.tensor(
|
||||||
|
[
|
||||||
|
*tokenizer.sot_sequence,
|
||||||
|
tokenizer.no_timestamps,
|
||||||
|
*text_tokens,
|
||||||
|
tokenizer.eot,
|
||||||
|
]
|
||||||
|
).to(model.device)
|
||||||
|
|
||||||
|
# install hooks on the cross attention layers to retrieve the attention weights
|
||||||
|
QKs = [None] * model.dims.n_text_layer
|
||||||
|
hooks = [
|
||||||
|
block.cross_attn.register_forward_hook(
|
||||||
|
lambda _, ins, outs, index=i: QKs.__setitem__(index, outs[-1][0])
|
||||||
|
)
|
||||||
|
for i, block in enumerate(model.decoder.blocks)
|
||||||
|
]
|
||||||
|
|
||||||
|
from .model import disable_sdpa
|
||||||
|
|
||||||
|
with torch.no_grad(), disable_sdpa():
|
||||||
|
logits = model(mel.unsqueeze(0), tokens.unsqueeze(0))[0]
|
||||||
|
sampled_logits = logits[len(tokenizer.sot_sequence) :, : tokenizer.eot]
|
||||||
|
token_probs = sampled_logits.softmax(dim=-1)
|
||||||
|
text_token_probs = token_probs[np.arange(len(text_tokens)), text_tokens]
|
||||||
|
text_token_probs = text_token_probs.tolist()
|
||||||
|
|
||||||
|
for hook in hooks:
|
||||||
|
hook.remove()
|
||||||
|
|
||||||
|
# heads * tokens * frames
|
||||||
|
weights = torch.stack([QKs[_l][_h] for _l, _h in model.alignment_heads.indices().T])
|
||||||
|
weights = weights[:, :, : num_frames // 2]
|
||||||
|
weights = (weights * qk_scale).softmax(dim=-1)
|
||||||
|
std, mean = torch.std_mean(weights, dim=-2, keepdim=True, unbiased=False)
|
||||||
|
weights = (weights - mean) / std
|
||||||
|
weights = median_filter(weights, medfilt_width)
|
||||||
|
|
||||||
|
matrix = weights.mean(axis=0)
|
||||||
|
matrix = matrix[len(tokenizer.sot_sequence) : -1]
|
||||||
|
text_indices, time_indices = dtw(-matrix)
|
||||||
|
|
||||||
|
words, word_tokens = tokenizer.split_to_word_tokens(text_tokens + [tokenizer.eot])
|
||||||
|
if len(word_tokens) <= 1:
|
||||||
|
# return on eot only
|
||||||
|
# >>> np.pad([], (1, 0))
|
||||||
|
# array([0.])
|
||||||
|
# This results in crashes when we lookup jump_times with float, like
|
||||||
|
# IndexError: arrays used as indices must be of integer (or boolean) type
|
||||||
|
return []
|
||||||
|
word_boundaries = np.pad(np.cumsum([len(t) for t in word_tokens[:-1]]), (1, 0))
|
||||||
|
|
||||||
|
jumps = np.pad(np.diff(text_indices), (1, 0), constant_values=1).astype(bool)
|
||||||
|
jump_times = time_indices[jumps] / TOKENS_PER_SECOND
|
||||||
|
start_times = jump_times[word_boundaries[:-1]]
|
||||||
|
end_times = jump_times[word_boundaries[1:]]
|
||||||
|
word_probabilities = [
|
||||||
|
np.mean(text_token_probs[i:j])
|
||||||
|
for i, j in zip(word_boundaries[:-1], word_boundaries[1:])
|
||||||
|
]
|
||||||
|
|
||||||
|
return [
|
||||||
|
WordTiming(word, tokens, start, end, probability)
|
||||||
|
for word, tokens, start, end, probability in zip(
|
||||||
|
words, word_tokens, start_times, end_times, word_probabilities
|
||||||
|
)
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def merge_punctuations(alignment: List[WordTiming], prepended: str, appended: str):
|
||||||
|
# merge prepended punctuations
|
||||||
|
i = len(alignment) - 2
|
||||||
|
j = len(alignment) - 1
|
||||||
|
while i >= 0:
|
||||||
|
previous = alignment[i]
|
||||||
|
following = alignment[j]
|
||||||
|
if previous.word.startswith(" ") and previous.word.strip() in prepended:
|
||||||
|
# prepend it to the following word
|
||||||
|
following.word = previous.word + following.word
|
||||||
|
following.tokens = previous.tokens + following.tokens
|
||||||
|
previous.word = ""
|
||||||
|
previous.tokens = []
|
||||||
|
else:
|
||||||
|
j = i
|
||||||
|
i -= 1
|
||||||
|
|
||||||
|
# merge appended punctuations
|
||||||
|
i = 0
|
||||||
|
j = 1
|
||||||
|
while j < len(alignment):
|
||||||
|
previous = alignment[i]
|
||||||
|
following = alignment[j]
|
||||||
|
if not previous.word.endswith(" ") and following.word in appended:
|
||||||
|
# append it to the previous word
|
||||||
|
previous.word = previous.word + following.word
|
||||||
|
previous.tokens = previous.tokens + following.tokens
|
||||||
|
following.word = ""
|
||||||
|
following.tokens = []
|
||||||
|
else:
|
||||||
|
i = j
|
||||||
|
j += 1
|
||||||
|
|
||||||
|
|
||||||
|
def add_word_timestamps(
|
||||||
|
*,
|
||||||
|
segments: List[dict],
|
||||||
|
model: "Whisper",
|
||||||
|
tokenizer: Tokenizer,
|
||||||
|
mel: torch.Tensor,
|
||||||
|
num_frames: int,
|
||||||
|
prepend_punctuations: str = "\"'“¿([{-",
|
||||||
|
append_punctuations: str = "\"'.。,,!!??::”)]}、",
|
||||||
|
last_speech_timestamp: float,
|
||||||
|
**kwargs,
|
||||||
|
):
|
||||||
|
if len(segments) == 0:
|
||||||
|
return
|
||||||
|
|
||||||
|
text_tokens_per_segment = [
|
||||||
|
[token for token in segment["tokens"] if token < tokenizer.eot]
|
||||||
|
for segment in segments
|
||||||
|
]
|
||||||
|
|
||||||
|
text_tokens = list(itertools.chain.from_iterable(text_tokens_per_segment))
|
||||||
|
alignment = find_alignment(model, tokenizer, text_tokens, mel, num_frames, **kwargs)
|
||||||
|
word_durations = np.array([t.end - t.start for t in alignment])
|
||||||
|
word_durations = word_durations[word_durations.nonzero()]
|
||||||
|
median_duration = np.median(word_durations) if len(word_durations) > 0 else 0.0
|
||||||
|
median_duration = min(0.7, float(median_duration))
|
||||||
|
max_duration = median_duration * 2
|
||||||
|
|
||||||
|
# hack: truncate long words at sentence boundaries.
|
||||||
|
# a better segmentation algorithm based on VAD should be able to replace this.
|
||||||
|
if len(word_durations) > 0:
|
||||||
|
sentence_end_marks = ".。!!??"
|
||||||
|
# ensure words at sentence boundaries are not longer than twice the median word duration.
|
||||||
|
for i in range(1, len(alignment)):
|
||||||
|
if alignment[i].end - alignment[i].start > max_duration:
|
||||||
|
if alignment[i].word in sentence_end_marks:
|
||||||
|
alignment[i].end = alignment[i].start + max_duration
|
||||||
|
elif alignment[i - 1].word in sentence_end_marks:
|
||||||
|
alignment[i].start = alignment[i].end - max_duration
|
||||||
|
|
||||||
|
merge_punctuations(alignment, prepend_punctuations, append_punctuations)
|
||||||
|
|
||||||
|
time_offset = segments[0]["seek"] * HOP_LENGTH / SAMPLE_RATE
|
||||||
|
word_index = 0
|
||||||
|
|
||||||
|
for segment, text_tokens in zip(segments, text_tokens_per_segment):
|
||||||
|
saved_tokens = 0
|
||||||
|
words = []
|
||||||
|
|
||||||
|
while word_index < len(alignment) and saved_tokens < len(text_tokens):
|
||||||
|
timing = alignment[word_index]
|
||||||
|
|
||||||
|
if timing.word:
|
||||||
|
words.append(
|
||||||
|
dict(
|
||||||
|
word=timing.word,
|
||||||
|
start=round(time_offset + timing.start, 2),
|
||||||
|
end=round(time_offset + timing.end, 2),
|
||||||
|
probability=timing.probability,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
saved_tokens += len(timing.tokens)
|
||||||
|
word_index += 1
|
||||||
|
|
||||||
|
# hack: truncate long words at segment boundaries.
|
||||||
|
# a better segmentation algorithm based on VAD should be able to replace this.
|
||||||
|
if len(words) > 0:
|
||||||
|
# ensure the first and second word after a pause is not longer than
|
||||||
|
# twice the median word duration.
|
||||||
|
if words[0]["end"] - last_speech_timestamp > median_duration * 4 and (
|
||||||
|
words[0]["end"] - words[0]["start"] > max_duration
|
||||||
|
or (
|
||||||
|
len(words) > 1
|
||||||
|
and words[1]["end"] - words[0]["start"] > max_duration * 2
|
||||||
|
)
|
||||||
|
):
|
||||||
|
if (
|
||||||
|
len(words) > 1
|
||||||
|
and words[1]["end"] - words[1]["start"] > max_duration
|
||||||
|
):
|
||||||
|
boundary = max(words[1]["end"] / 2, words[1]["end"] - max_duration)
|
||||||
|
words[0]["end"] = words[1]["start"] = boundary
|
||||||
|
words[0]["start"] = max(0, words[0]["end"] - max_duration)
|
||||||
|
|
||||||
|
# prefer the segment-level start timestamp if the first word is too long.
|
||||||
|
if (
|
||||||
|
segment["start"] < words[0]["end"]
|
||||||
|
and segment["start"] - 0.5 > words[0]["start"]
|
||||||
|
):
|
||||||
|
words[0]["start"] = max(
|
||||||
|
0, min(words[0]["end"] - median_duration, segment["start"])
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
segment["start"] = words[0]["start"]
|
||||||
|
|
||||||
|
# prefer the segment-level end timestamp if the last word is too long.
|
||||||
|
if (
|
||||||
|
segment["end"] > words[-1]["start"]
|
||||||
|
and segment["end"] + 0.5 < words[-1]["end"]
|
||||||
|
):
|
||||||
|
words[-1]["end"] = max(
|
||||||
|
words[-1]["start"] + median_duration, segment["end"]
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
segment["end"] = words[-1]["end"]
|
||||||
|
|
||||||
|
last_speech_timestamp = segment["end"]
|
||||||
|
|
||||||
|
segment["words"] = words
|
||||||
395
whisperlivekit/simul_whisper/whisper/tokenizer.py
Normal file
395
whisperlivekit/simul_whisper/whisper/tokenizer.py
Normal file
@@ -0,0 +1,395 @@
|
|||||||
|
import base64
|
||||||
|
import os
|
||||||
|
import string
|
||||||
|
from dataclasses import dataclass, field
|
||||||
|
from functools import cached_property, lru_cache
|
||||||
|
from typing import Dict, List, Optional, Tuple
|
||||||
|
|
||||||
|
import tiktoken
|
||||||
|
|
||||||
|
LANGUAGES = {
|
||||||
|
"en": "english",
|
||||||
|
"zh": "chinese",
|
||||||
|
"de": "german",
|
||||||
|
"es": "spanish",
|
||||||
|
"ru": "russian",
|
||||||
|
"ko": "korean",
|
||||||
|
"fr": "french",
|
||||||
|
"ja": "japanese",
|
||||||
|
"pt": "portuguese",
|
||||||
|
"tr": "turkish",
|
||||||
|
"pl": "polish",
|
||||||
|
"ca": "catalan",
|
||||||
|
"nl": "dutch",
|
||||||
|
"ar": "arabic",
|
||||||
|
"sv": "swedish",
|
||||||
|
"it": "italian",
|
||||||
|
"id": "indonesian",
|
||||||
|
"hi": "hindi",
|
||||||
|
"fi": "finnish",
|
||||||
|
"vi": "vietnamese",
|
||||||
|
"he": "hebrew",
|
||||||
|
"uk": "ukrainian",
|
||||||
|
"el": "greek",
|
||||||
|
"ms": "malay",
|
||||||
|
"cs": "czech",
|
||||||
|
"ro": "romanian",
|
||||||
|
"da": "danish",
|
||||||
|
"hu": "hungarian",
|
||||||
|
"ta": "tamil",
|
||||||
|
"no": "norwegian",
|
||||||
|
"th": "thai",
|
||||||
|
"ur": "urdu",
|
||||||
|
"hr": "croatian",
|
||||||
|
"bg": "bulgarian",
|
||||||
|
"lt": "lithuanian",
|
||||||
|
"la": "latin",
|
||||||
|
"mi": "maori",
|
||||||
|
"ml": "malayalam",
|
||||||
|
"cy": "welsh",
|
||||||
|
"sk": "slovak",
|
||||||
|
"te": "telugu",
|
||||||
|
"fa": "persian",
|
||||||
|
"lv": "latvian",
|
||||||
|
"bn": "bengali",
|
||||||
|
"sr": "serbian",
|
||||||
|
"az": "azerbaijani",
|
||||||
|
"sl": "slovenian",
|
||||||
|
"kn": "kannada",
|
||||||
|
"et": "estonian",
|
||||||
|
"mk": "macedonian",
|
||||||
|
"br": "breton",
|
||||||
|
"eu": "basque",
|
||||||
|
"is": "icelandic",
|
||||||
|
"hy": "armenian",
|
||||||
|
"ne": "nepali",
|
||||||
|
"mn": "mongolian",
|
||||||
|
"bs": "bosnian",
|
||||||
|
"kk": "kazakh",
|
||||||
|
"sq": "albanian",
|
||||||
|
"sw": "swahili",
|
||||||
|
"gl": "galician",
|
||||||
|
"mr": "marathi",
|
||||||
|
"pa": "punjabi",
|
||||||
|
"si": "sinhala",
|
||||||
|
"km": "khmer",
|
||||||
|
"sn": "shona",
|
||||||
|
"yo": "yoruba",
|
||||||
|
"so": "somali",
|
||||||
|
"af": "afrikaans",
|
||||||
|
"oc": "occitan",
|
||||||
|
"ka": "georgian",
|
||||||
|
"be": "belarusian",
|
||||||
|
"tg": "tajik",
|
||||||
|
"sd": "sindhi",
|
||||||
|
"gu": "gujarati",
|
||||||
|
"am": "amharic",
|
||||||
|
"yi": "yiddish",
|
||||||
|
"lo": "lao",
|
||||||
|
"uz": "uzbek",
|
||||||
|
"fo": "faroese",
|
||||||
|
"ht": "haitian creole",
|
||||||
|
"ps": "pashto",
|
||||||
|
"tk": "turkmen",
|
||||||
|
"nn": "nynorsk",
|
||||||
|
"mt": "maltese",
|
||||||
|
"sa": "sanskrit",
|
||||||
|
"lb": "luxembourgish",
|
||||||
|
"my": "myanmar",
|
||||||
|
"bo": "tibetan",
|
||||||
|
"tl": "tagalog",
|
||||||
|
"mg": "malagasy",
|
||||||
|
"as": "assamese",
|
||||||
|
"tt": "tatar",
|
||||||
|
"haw": "hawaiian",
|
||||||
|
"ln": "lingala",
|
||||||
|
"ha": "hausa",
|
||||||
|
"ba": "bashkir",
|
||||||
|
"jw": "javanese",
|
||||||
|
"su": "sundanese",
|
||||||
|
"yue": "cantonese",
|
||||||
|
}
|
||||||
|
|
||||||
|
# language code lookup by name, with a few language aliases
|
||||||
|
TO_LANGUAGE_CODE = {
|
||||||
|
**{language: code for code, language in LANGUAGES.items()},
|
||||||
|
"burmese": "my",
|
||||||
|
"valencian": "ca",
|
||||||
|
"flemish": "nl",
|
||||||
|
"haitian": "ht",
|
||||||
|
"letzeburgesch": "lb",
|
||||||
|
"pushto": "ps",
|
||||||
|
"panjabi": "pa",
|
||||||
|
"moldavian": "ro",
|
||||||
|
"moldovan": "ro",
|
||||||
|
"sinhalese": "si",
|
||||||
|
"castilian": "es",
|
||||||
|
"mandarin": "zh",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Tokenizer:
|
||||||
|
"""A thin wrapper around `tiktoken` providing quick access to special tokens"""
|
||||||
|
|
||||||
|
encoding: tiktoken.Encoding
|
||||||
|
num_languages: int
|
||||||
|
language: Optional[str] = None
|
||||||
|
task: Optional[str] = None
|
||||||
|
sot_sequence: Tuple[int] = ()
|
||||||
|
special_tokens: Dict[str, int] = field(default_factory=dict)
|
||||||
|
|
||||||
|
def __post_init__(self):
|
||||||
|
for special in self.encoding.special_tokens_set:
|
||||||
|
special_token = self.encoding.encode_single_token(special)
|
||||||
|
self.special_tokens[special] = special_token
|
||||||
|
|
||||||
|
sot: int = self.special_tokens["<|startoftranscript|>"]
|
||||||
|
translate: int = self.special_tokens["<|translate|>"]
|
||||||
|
transcribe: int = self.special_tokens["<|transcribe|>"]
|
||||||
|
|
||||||
|
langs = tuple(LANGUAGES.keys())[: self.num_languages]
|
||||||
|
sot_sequence = [sot]
|
||||||
|
if self.language is not None:
|
||||||
|
sot_sequence.append(sot + 1 + langs.index(self.language))
|
||||||
|
if self.task is not None:
|
||||||
|
task_token: int = transcribe if self.task == "transcribe" else translate
|
||||||
|
sot_sequence.append(task_token)
|
||||||
|
|
||||||
|
self.sot_sequence = tuple(sot_sequence)
|
||||||
|
|
||||||
|
def encode(self, text, **kwargs):
|
||||||
|
return self.encoding.encode(text, **kwargs)
|
||||||
|
|
||||||
|
def decode(self, token_ids: List[int], **kwargs) -> str:
|
||||||
|
token_ids = [t for t in token_ids if t < self.timestamp_begin]
|
||||||
|
return self.encoding.decode(token_ids, **kwargs)
|
||||||
|
|
||||||
|
def decode_with_timestamps(self, token_ids: List[int], **kwargs) -> str:
|
||||||
|
"""
|
||||||
|
Timestamp tokens are above other special tokens' id range and are ignored by `decode()`.
|
||||||
|
This method decodes given tokens with timestamps tokens annotated, e.g. "<|1.08|>".
|
||||||
|
"""
|
||||||
|
return self.encoding.decode(token_ids, **kwargs)
|
||||||
|
|
||||||
|
@cached_property
|
||||||
|
def eot(self) -> int:
|
||||||
|
return self.encoding.eot_token
|
||||||
|
|
||||||
|
@cached_property
|
||||||
|
def transcribe(self) -> int:
|
||||||
|
return self.special_tokens["<|transcribe|>"]
|
||||||
|
|
||||||
|
@cached_property
|
||||||
|
def translate(self) -> int:
|
||||||
|
return self.special_tokens["<|translate|>"]
|
||||||
|
|
||||||
|
@cached_property
|
||||||
|
def sot(self) -> int:
|
||||||
|
return self.special_tokens["<|startoftranscript|>"]
|
||||||
|
|
||||||
|
@cached_property
|
||||||
|
def sot_lm(self) -> int:
|
||||||
|
return self.special_tokens["<|startoflm|>"]
|
||||||
|
|
||||||
|
@cached_property
|
||||||
|
def sot_prev(self) -> int:
|
||||||
|
return self.special_tokens["<|startofprev|>"]
|
||||||
|
|
||||||
|
@cached_property
|
||||||
|
def no_speech(self) -> int:
|
||||||
|
return self.special_tokens["<|nospeech|>"]
|
||||||
|
|
||||||
|
@cached_property
|
||||||
|
def no_timestamps(self) -> int:
|
||||||
|
return self.special_tokens["<|notimestamps|>"]
|
||||||
|
|
||||||
|
@cached_property
|
||||||
|
def timestamp_begin(self) -> int:
|
||||||
|
return self.special_tokens["<|0.00|>"]
|
||||||
|
|
||||||
|
@cached_property
|
||||||
|
def language_token(self) -> int:
|
||||||
|
"""Returns the token id corresponding to the value of the `language` field"""
|
||||||
|
if self.language is None:
|
||||||
|
raise ValueError("This tokenizer does not have language token configured")
|
||||||
|
|
||||||
|
return self.to_language_token(self.language)
|
||||||
|
|
||||||
|
def to_language_token(self, language):
|
||||||
|
if token := self.special_tokens.get(f"<|{language}|>", None):
|
||||||
|
return token
|
||||||
|
|
||||||
|
raise KeyError(f"Language {language} not found in tokenizer.")
|
||||||
|
|
||||||
|
@cached_property
|
||||||
|
def all_language_tokens(self) -> Tuple[int]:
|
||||||
|
result = []
|
||||||
|
for token, token_id in self.special_tokens.items():
|
||||||
|
if token.strip("<|>") in LANGUAGES:
|
||||||
|
result.append(token_id)
|
||||||
|
return tuple(result)[: self.num_languages]
|
||||||
|
|
||||||
|
@cached_property
|
||||||
|
def all_language_codes(self) -> Tuple[str]:
|
||||||
|
return tuple(self.decode([_l]).strip("<|>") for _l in self.all_language_tokens)
|
||||||
|
|
||||||
|
@cached_property
|
||||||
|
def sot_sequence_including_notimestamps(self) -> Tuple[int]:
|
||||||
|
return tuple(list(self.sot_sequence) + [self.no_timestamps])
|
||||||
|
|
||||||
|
@cached_property
|
||||||
|
def non_speech_tokens(self) -> Tuple[int]:
|
||||||
|
"""
|
||||||
|
Returns the list of tokens to suppress in order to avoid any speaker tags or non-speech
|
||||||
|
annotations, to prevent sampling texts that are not actually spoken in the audio, e.g.
|
||||||
|
|
||||||
|
- ♪♪♪
|
||||||
|
- ( SPEAKING FOREIGN LANGUAGE )
|
||||||
|
- [DAVID] Hey there,
|
||||||
|
|
||||||
|
keeping basic punctuations like commas, periods, question marks, exclamation points, etc.
|
||||||
|
"""
|
||||||
|
symbols = list('"#()*+/:;<=>@[\\]^_`{|}~「」『』')
|
||||||
|
symbols += (
|
||||||
|
"<< >> <<< >>> -- --- -( -[ (' (\" (( )) ((( ))) [[ ]] {{ }} ♪♪ ♪♪♪".split()
|
||||||
|
)
|
||||||
|
|
||||||
|
# symbols that may be a single token or multiple tokens depending on the tokenizer.
|
||||||
|
# In case they're multiple tokens, suppress the first token, which is safe because:
|
||||||
|
# These are between U+2640 and U+267F miscellaneous symbols that are okay to suppress
|
||||||
|
# in generations, and in the 3-byte UTF-8 representation they share the first two bytes.
|
||||||
|
miscellaneous = set("♩♪♫♬♭♮♯")
|
||||||
|
assert all(0x2640 <= ord(c) <= 0x267F for c in miscellaneous)
|
||||||
|
|
||||||
|
# allow hyphens "-" and single quotes "'" between words, but not at the beginning of a word
|
||||||
|
result = {self.encoding.encode(" -")[0], self.encoding.encode(" '")[0]}
|
||||||
|
for symbol in symbols + list(miscellaneous):
|
||||||
|
for tokens in [
|
||||||
|
self.encoding.encode(symbol),
|
||||||
|
self.encoding.encode(" " + symbol),
|
||||||
|
]:
|
||||||
|
if len(tokens) == 1 or symbol in miscellaneous:
|
||||||
|
result.add(tokens[0])
|
||||||
|
|
||||||
|
return tuple(sorted(result))
|
||||||
|
|
||||||
|
def split_to_word_tokens(self, tokens: List[int]):
|
||||||
|
if self.language in {"zh", "ja", "th", "lo", "my", "yue"}:
|
||||||
|
# These languages don't typically use spaces, so it is difficult to split words
|
||||||
|
# without morpheme analysis. Here, we instead split words at any
|
||||||
|
# position where the tokens are decoded as valid unicode points
|
||||||
|
return self.split_tokens_on_unicode(tokens)
|
||||||
|
|
||||||
|
return self.split_tokens_on_spaces(tokens)
|
||||||
|
|
||||||
|
def split_tokens_on_unicode(self, tokens: List[int]):
|
||||||
|
decoded_full = self.decode_with_timestamps(tokens)
|
||||||
|
replacement_char = "\ufffd"
|
||||||
|
|
||||||
|
words = []
|
||||||
|
word_tokens = []
|
||||||
|
current_tokens = []
|
||||||
|
unicode_offset = 0
|
||||||
|
|
||||||
|
for token in tokens:
|
||||||
|
current_tokens.append(token)
|
||||||
|
decoded = self.decode_with_timestamps(current_tokens)
|
||||||
|
|
||||||
|
if (
|
||||||
|
replacement_char not in decoded
|
||||||
|
or decoded_full[unicode_offset + decoded.index(replacement_char)]
|
||||||
|
== replacement_char
|
||||||
|
):
|
||||||
|
words.append(decoded)
|
||||||
|
word_tokens.append(current_tokens)
|
||||||
|
current_tokens = []
|
||||||
|
unicode_offset += len(decoded)
|
||||||
|
|
||||||
|
return words, word_tokens
|
||||||
|
|
||||||
|
def split_tokens_on_spaces(self, tokens: List[int]):
|
||||||
|
subwords, subword_tokens_list = self.split_tokens_on_unicode(tokens)
|
||||||
|
words = []
|
||||||
|
word_tokens = []
|
||||||
|
|
||||||
|
for subword, subword_tokens in zip(subwords, subword_tokens_list):
|
||||||
|
special = subword_tokens[0] >= self.eot
|
||||||
|
with_space = subword.startswith(" ")
|
||||||
|
punctuation = subword.strip() in string.punctuation
|
||||||
|
if special or with_space or punctuation or len(words) == 0:
|
||||||
|
words.append(subword)
|
||||||
|
word_tokens.append(subword_tokens)
|
||||||
|
else:
|
||||||
|
words[-1] = words[-1] + subword
|
||||||
|
word_tokens[-1].extend(subword_tokens)
|
||||||
|
|
||||||
|
return words, word_tokens
|
||||||
|
|
||||||
|
|
||||||
|
@lru_cache(maxsize=None)
|
||||||
|
def get_encoding(name: str = "gpt2", num_languages: int = 99):
|
||||||
|
vocab_path = os.path.join(os.path.dirname(__file__), "assets", f"{name}.tiktoken")
|
||||||
|
ranks = {
|
||||||
|
base64.b64decode(token): int(rank)
|
||||||
|
for token, rank in (line.split() for line in open(vocab_path) if line)
|
||||||
|
}
|
||||||
|
n_vocab = len(ranks)
|
||||||
|
special_tokens = {}
|
||||||
|
|
||||||
|
specials = [
|
||||||
|
"<|endoftext|>",
|
||||||
|
"<|startoftranscript|>",
|
||||||
|
*[f"<|{lang}|>" for lang in list(LANGUAGES.keys())[:num_languages]],
|
||||||
|
"<|translate|>",
|
||||||
|
"<|transcribe|>",
|
||||||
|
"<|startoflm|>",
|
||||||
|
"<|startofprev|>",
|
||||||
|
"<|nospeech|>",
|
||||||
|
"<|notimestamps|>",
|
||||||
|
*[f"<|{i * 0.02:.2f}|>" for i in range(1501)],
|
||||||
|
]
|
||||||
|
|
||||||
|
for token in specials:
|
||||||
|
special_tokens[token] = n_vocab
|
||||||
|
n_vocab += 1
|
||||||
|
|
||||||
|
return tiktoken.Encoding(
|
||||||
|
name=os.path.basename(vocab_path),
|
||||||
|
explicit_n_vocab=n_vocab,
|
||||||
|
pat_str=r"""'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+""",
|
||||||
|
mergeable_ranks=ranks,
|
||||||
|
special_tokens=special_tokens,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@lru_cache(maxsize=None)
|
||||||
|
def get_tokenizer(
|
||||||
|
multilingual: bool,
|
||||||
|
*,
|
||||||
|
num_languages: int = 99,
|
||||||
|
language: Optional[str] = None,
|
||||||
|
task: Optional[str] = None, # Literal["transcribe", "translate", None]
|
||||||
|
) -> Tokenizer:
|
||||||
|
if language is not None:
|
||||||
|
language = language.lower()
|
||||||
|
if language not in LANGUAGES:
|
||||||
|
if language in TO_LANGUAGE_CODE:
|
||||||
|
language = TO_LANGUAGE_CODE[language]
|
||||||
|
else:
|
||||||
|
raise ValueError(f"Unsupported language: {language}")
|
||||||
|
|
||||||
|
if multilingual:
|
||||||
|
encoding_name = "multilingual"
|
||||||
|
language = language or "en"
|
||||||
|
task = task or "transcribe"
|
||||||
|
else:
|
||||||
|
encoding_name = "gpt2"
|
||||||
|
language = None
|
||||||
|
task = None
|
||||||
|
|
||||||
|
encoding = get_encoding(name=encoding_name, num_languages=num_languages)
|
||||||
|
|
||||||
|
return Tokenizer(
|
||||||
|
encoding=encoding, num_languages=num_languages, language=language, task=task
|
||||||
|
)
|
||||||
623
whisperlivekit/simul_whisper/whisper/transcribe.py
Normal file
623
whisperlivekit/simul_whisper/whisper/transcribe.py
Normal file
@@ -0,0 +1,623 @@
|
|||||||
|
import argparse
|
||||||
|
import os
|
||||||
|
import traceback
|
||||||
|
import warnings
|
||||||
|
from typing import TYPE_CHECKING, List, Optional, Tuple, Union
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
import torch
|
||||||
|
import tqdm
|
||||||
|
|
||||||
|
from .audio import (
|
||||||
|
FRAMES_PER_SECOND,
|
||||||
|
HOP_LENGTH,
|
||||||
|
N_FRAMES,
|
||||||
|
N_SAMPLES,
|
||||||
|
SAMPLE_RATE,
|
||||||
|
log_mel_spectrogram,
|
||||||
|
pad_or_trim,
|
||||||
|
)
|
||||||
|
from .decoding import DecodingOptions, DecodingResult
|
||||||
|
from .timing import add_word_timestamps
|
||||||
|
from .tokenizer import LANGUAGES, TO_LANGUAGE_CODE, get_tokenizer
|
||||||
|
from .utils import (
|
||||||
|
exact_div,
|
||||||
|
format_timestamp,
|
||||||
|
get_end,
|
||||||
|
get_writer,
|
||||||
|
make_safe,
|
||||||
|
optional_float,
|
||||||
|
optional_int,
|
||||||
|
str2bool,
|
||||||
|
)
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from .model import Whisper
|
||||||
|
|
||||||
|
|
||||||
|
def transcribe(
|
||||||
|
model: "Whisper",
|
||||||
|
audio: Union[str, np.ndarray, torch.Tensor],
|
||||||
|
*,
|
||||||
|
verbose: Optional[bool] = None,
|
||||||
|
temperature: Union[float, Tuple[float, ...]] = (0.0, 0.2, 0.4, 0.6, 0.8, 1.0),
|
||||||
|
compression_ratio_threshold: Optional[float] = 2.4,
|
||||||
|
logprob_threshold: Optional[float] = -1.0,
|
||||||
|
no_speech_threshold: Optional[float] = 0.6,
|
||||||
|
condition_on_previous_text: bool = True,
|
||||||
|
initial_prompt: Optional[str] = None,
|
||||||
|
carry_initial_prompt: bool = False,
|
||||||
|
word_timestamps: bool = False,
|
||||||
|
prepend_punctuations: str = "\"'“¿([{-",
|
||||||
|
append_punctuations: str = "\"'.。,,!!??::”)]}、",
|
||||||
|
clip_timestamps: Union[str, List[float]] = "0",
|
||||||
|
hallucination_silence_threshold: Optional[float] = None,
|
||||||
|
**decode_options,
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
Transcribe an audio file using Whisper
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
model: Whisper
|
||||||
|
The Whisper model instance
|
||||||
|
|
||||||
|
audio: Union[str, np.ndarray, torch.Tensor]
|
||||||
|
The path to the audio file to open, or the audio waveform
|
||||||
|
|
||||||
|
verbose: bool
|
||||||
|
Whether to display the text being decoded to the console. If True, displays all the details,
|
||||||
|
If False, displays minimal details. If None, does not display anything
|
||||||
|
|
||||||
|
temperature: Union[float, Tuple[float, ...]]
|
||||||
|
Temperature for sampling. It can be a tuple of temperatures, which will be successively used
|
||||||
|
upon failures according to either `compression_ratio_threshold` or `logprob_threshold`.
|
||||||
|
|
||||||
|
compression_ratio_threshold: float
|
||||||
|
If the gzip compression ratio is above this value, treat as failed
|
||||||
|
|
||||||
|
logprob_threshold: float
|
||||||
|
If the average log probability over sampled tokens is below this value, treat as failed
|
||||||
|
|
||||||
|
no_speech_threshold: float
|
||||||
|
If the no_speech probability is higher than this value AND the average log probability
|
||||||
|
over sampled tokens is below `logprob_threshold`, consider the segment as silent
|
||||||
|
|
||||||
|
condition_on_previous_text: bool
|
||||||
|
if True, the previous output of the model is provided as a prompt for the next window;
|
||||||
|
disabling may make the text inconsistent across windows, but the model becomes less prone to
|
||||||
|
getting stuck in a failure loop, such as repetition looping or timestamps going out of sync.
|
||||||
|
|
||||||
|
word_timestamps: bool
|
||||||
|
Extract word-level timestamps using the cross-attention pattern and dynamic time warping,
|
||||||
|
and include the timestamps for each word in each segment.
|
||||||
|
|
||||||
|
prepend_punctuations: str
|
||||||
|
If word_timestamps is True, merge these punctuation symbols with the next word
|
||||||
|
|
||||||
|
append_punctuations: str
|
||||||
|
If word_timestamps is True, merge these punctuation symbols with the previous word
|
||||||
|
|
||||||
|
initial_prompt: Optional[str]
|
||||||
|
Optional text to provide as a prompt for the first window. This can be used to provide, or
|
||||||
|
"prompt-engineer" a context for transcription, e.g. custom vocabularies or proper nouns
|
||||||
|
to make it more likely to predict those word correctly.
|
||||||
|
|
||||||
|
carry_initial_prompt: bool
|
||||||
|
If carry_initial_prompt is True, `initial_prompt` is prepended to the prompt of each internal
|
||||||
|
`decode()` call. If there is not enough context space at the start of the prompt, it is
|
||||||
|
left-sliced to make space.
|
||||||
|
|
||||||
|
decode_options: dict
|
||||||
|
Keyword arguments to construct `DecodingOptions` instances
|
||||||
|
|
||||||
|
clip_timestamps: Union[str, List[float]]
|
||||||
|
Comma-separated list start,end,start,end,... timestamps (in seconds) of clips to process.
|
||||||
|
The last end timestamp defaults to the end of the file.
|
||||||
|
|
||||||
|
hallucination_silence_threshold: Optional[float]
|
||||||
|
When word_timestamps is True, skip silent periods longer than this threshold (in seconds)
|
||||||
|
when a possible hallucination is detected
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
A dictionary containing the resulting text ("text") and segment-level details ("segments"), and
|
||||||
|
the spoken language ("language"), which is detected when `decode_options["language"]` is None.
|
||||||
|
"""
|
||||||
|
dtype = torch.float16 if decode_options.get("fp16", True) else torch.float32
|
||||||
|
if model.device == torch.device("cpu"):
|
||||||
|
if torch.cuda.is_available():
|
||||||
|
warnings.warn("Performing inference on CPU when CUDA is available")
|
||||||
|
if dtype == torch.float16:
|
||||||
|
warnings.warn("FP16 is not supported on CPU; using FP32 instead")
|
||||||
|
dtype = torch.float32
|
||||||
|
|
||||||
|
if dtype == torch.float32:
|
||||||
|
decode_options["fp16"] = False
|
||||||
|
|
||||||
|
# Pad 30-seconds of silence to the input audio, for slicing
|
||||||
|
mel = log_mel_spectrogram(audio, model.dims.n_mels, padding=N_SAMPLES)
|
||||||
|
content_frames = mel.shape[-1] - N_FRAMES
|
||||||
|
content_duration = float(content_frames * HOP_LENGTH / SAMPLE_RATE)
|
||||||
|
|
||||||
|
if decode_options.get("language", None) is None:
|
||||||
|
if not model.is_multilingual:
|
||||||
|
decode_options["language"] = "en"
|
||||||
|
else:
|
||||||
|
if verbose:
|
||||||
|
print(
|
||||||
|
"Detecting language using up to the first 30 seconds. Use `--language` to specify the language"
|
||||||
|
)
|
||||||
|
mel_segment = pad_or_trim(mel, N_FRAMES).to(model.device).to(dtype)
|
||||||
|
_, probs = model.detect_language(mel_segment)
|
||||||
|
decode_options["language"] = max(probs, key=probs.get)
|
||||||
|
if verbose is not None:
|
||||||
|
print(
|
||||||
|
f"Detected language: {LANGUAGES[decode_options['language']].title()}"
|
||||||
|
)
|
||||||
|
|
||||||
|
language: str = decode_options["language"]
|
||||||
|
task: str = decode_options.get("task", "transcribe")
|
||||||
|
tokenizer = get_tokenizer(
|
||||||
|
model.is_multilingual,
|
||||||
|
num_languages=model.num_languages,
|
||||||
|
language=language,
|
||||||
|
task=task,
|
||||||
|
)
|
||||||
|
|
||||||
|
if isinstance(clip_timestamps, str):
|
||||||
|
clip_timestamps = [
|
||||||
|
float(ts) for ts in (clip_timestamps.split(",") if clip_timestamps else [])
|
||||||
|
]
|
||||||
|
seek_points: List[int] = [round(ts * FRAMES_PER_SECOND) for ts in clip_timestamps]
|
||||||
|
if len(seek_points) == 0:
|
||||||
|
seek_points.append(0)
|
||||||
|
if len(seek_points) % 2 == 1:
|
||||||
|
seek_points.append(content_frames)
|
||||||
|
seek_clips: List[Tuple[int, int]] = list(zip(seek_points[::2], seek_points[1::2]))
|
||||||
|
|
||||||
|
punctuation = "\"'“¿([{-\"'.。,,!!??::”)]}、"
|
||||||
|
|
||||||
|
if word_timestamps and task == "translate":
|
||||||
|
warnings.warn("Word-level timestamps on translations may not be reliable.")
|
||||||
|
|
||||||
|
def decode_with_fallback(segment: torch.Tensor) -> DecodingResult:
|
||||||
|
temperatures = (
|
||||||
|
[temperature] if isinstance(temperature, (int, float)) else temperature
|
||||||
|
)
|
||||||
|
decode_result = None
|
||||||
|
|
||||||
|
for t in temperatures:
|
||||||
|
kwargs = {**decode_options}
|
||||||
|
if t > 0:
|
||||||
|
# disable beam_size and patience when t > 0
|
||||||
|
kwargs.pop("beam_size", None)
|
||||||
|
kwargs.pop("patience", None)
|
||||||
|
else:
|
||||||
|
# disable best_of when t == 0
|
||||||
|
kwargs.pop("best_of", None)
|
||||||
|
|
||||||
|
options = DecodingOptions(**kwargs, temperature=t)
|
||||||
|
decode_result = model.decode(segment, options)
|
||||||
|
|
||||||
|
needs_fallback = False
|
||||||
|
if (
|
||||||
|
compression_ratio_threshold is not None
|
||||||
|
and decode_result.compression_ratio > compression_ratio_threshold
|
||||||
|
):
|
||||||
|
needs_fallback = True # too repetitive
|
||||||
|
if (
|
||||||
|
logprob_threshold is not None
|
||||||
|
and decode_result.avg_logprob < logprob_threshold
|
||||||
|
):
|
||||||
|
needs_fallback = True # average log probability is too low
|
||||||
|
if (
|
||||||
|
no_speech_threshold is not None
|
||||||
|
and decode_result.no_speech_prob > no_speech_threshold
|
||||||
|
and logprob_threshold is not None
|
||||||
|
and decode_result.avg_logprob < logprob_threshold
|
||||||
|
):
|
||||||
|
needs_fallback = False # silence
|
||||||
|
if not needs_fallback:
|
||||||
|
break
|
||||||
|
|
||||||
|
return decode_result
|
||||||
|
|
||||||
|
clip_idx = 0
|
||||||
|
seek = seek_clips[clip_idx][0]
|
||||||
|
input_stride = exact_div(
|
||||||
|
N_FRAMES, model.dims.n_audio_ctx
|
||||||
|
) # mel frames per output token: 2
|
||||||
|
time_precision = (
|
||||||
|
input_stride * HOP_LENGTH / SAMPLE_RATE
|
||||||
|
) # time per output token: 0.02 (seconds)
|
||||||
|
all_tokens = []
|
||||||
|
all_segments = []
|
||||||
|
prompt_reset_since = 0
|
||||||
|
|
||||||
|
remaining_prompt_length = model.dims.n_text_ctx // 2 - 1
|
||||||
|
if initial_prompt is not None:
|
||||||
|
initial_prompt_tokens = tokenizer.encode(" " + initial_prompt.strip())
|
||||||
|
all_tokens.extend(initial_prompt_tokens)
|
||||||
|
remaining_prompt_length -= len(initial_prompt_tokens)
|
||||||
|
else:
|
||||||
|
initial_prompt_tokens = []
|
||||||
|
|
||||||
|
def new_segment(
|
||||||
|
*, start: float, end: float, tokens: torch.Tensor, result: DecodingResult
|
||||||
|
):
|
||||||
|
tokens = tokens.tolist()
|
||||||
|
text_tokens = [token for token in tokens if token < tokenizer.eot]
|
||||||
|
return {
|
||||||
|
"seek": seek,
|
||||||
|
"start": start,
|
||||||
|
"end": end,
|
||||||
|
"text": tokenizer.decode(text_tokens),
|
||||||
|
"tokens": tokens,
|
||||||
|
"temperature": result.temperature,
|
||||||
|
"avg_logprob": result.avg_logprob,
|
||||||
|
"compression_ratio": result.compression_ratio,
|
||||||
|
"no_speech_prob": result.no_speech_prob,
|
||||||
|
}
|
||||||
|
|
||||||
|
# show the progress bar when verbose is False (if True, transcribed text will be printed)
|
||||||
|
with tqdm.tqdm(
|
||||||
|
total=content_frames, unit="frames", disable=verbose is not False
|
||||||
|
) as pbar:
|
||||||
|
last_speech_timestamp = 0.0
|
||||||
|
# NOTE: This loop is obscurely flattened to make the diff readable.
|
||||||
|
# A later commit should turn this into a simpler nested loop.
|
||||||
|
# for seek_clip_start, seek_clip_end in seek_clips:
|
||||||
|
# while seek < seek_clip_end
|
||||||
|
while clip_idx < len(seek_clips):
|
||||||
|
seek_clip_start, seek_clip_end = seek_clips[clip_idx]
|
||||||
|
if seek < seek_clip_start:
|
||||||
|
seek = seek_clip_start
|
||||||
|
if seek >= seek_clip_end:
|
||||||
|
clip_idx += 1
|
||||||
|
if clip_idx < len(seek_clips):
|
||||||
|
seek = seek_clips[clip_idx][0]
|
||||||
|
continue
|
||||||
|
time_offset = float(seek * HOP_LENGTH / SAMPLE_RATE)
|
||||||
|
window_end_time = float((seek + N_FRAMES) * HOP_LENGTH / SAMPLE_RATE)
|
||||||
|
segment_size = min(N_FRAMES, content_frames - seek, seek_clip_end - seek)
|
||||||
|
mel_segment = mel[:, seek : seek + segment_size]
|
||||||
|
segment_duration = segment_size * HOP_LENGTH / SAMPLE_RATE
|
||||||
|
mel_segment = pad_or_trim(mel_segment, N_FRAMES).to(model.device).to(dtype)
|
||||||
|
|
||||||
|
if carry_initial_prompt:
|
||||||
|
nignored = max(len(initial_prompt_tokens), prompt_reset_since)
|
||||||
|
remaining_prompt = all_tokens[nignored:][-remaining_prompt_length:]
|
||||||
|
decode_options["prompt"] = initial_prompt_tokens + remaining_prompt
|
||||||
|
else:
|
||||||
|
decode_options["prompt"] = all_tokens[prompt_reset_since:]
|
||||||
|
|
||||||
|
result: DecodingResult = decode_with_fallback(mel_segment)
|
||||||
|
tokens = torch.tensor(result.tokens)
|
||||||
|
|
||||||
|
if no_speech_threshold is not None:
|
||||||
|
# no voice activity check
|
||||||
|
should_skip = result.no_speech_prob > no_speech_threshold
|
||||||
|
if (
|
||||||
|
logprob_threshold is not None
|
||||||
|
and result.avg_logprob > logprob_threshold
|
||||||
|
):
|
||||||
|
# don't skip if the logprob is high enough, despite the no_speech_prob
|
||||||
|
should_skip = False
|
||||||
|
|
||||||
|
if should_skip:
|
||||||
|
seek += segment_size # fast-forward to the next segment boundary
|
||||||
|
continue
|
||||||
|
|
||||||
|
previous_seek = seek
|
||||||
|
current_segments = []
|
||||||
|
|
||||||
|
# anomalous words are very long/short/improbable
|
||||||
|
def word_anomaly_score(word: dict) -> float:
|
||||||
|
probability = word.get("probability", 0.0)
|
||||||
|
duration = word["end"] - word["start"]
|
||||||
|
score = 0.0
|
||||||
|
if probability < 0.15:
|
||||||
|
score += 1.0
|
||||||
|
if duration < 0.133:
|
||||||
|
score += (0.133 - duration) * 15
|
||||||
|
if duration > 2.0:
|
||||||
|
score += duration - 2.0
|
||||||
|
return score
|
||||||
|
|
||||||
|
def is_segment_anomaly(segment: Optional[dict]) -> bool:
|
||||||
|
if segment is None or not segment["words"]:
|
||||||
|
return False
|
||||||
|
words = [w for w in segment["words"] if w["word"] not in punctuation]
|
||||||
|
words = words[:8]
|
||||||
|
score = sum(word_anomaly_score(w) for w in words)
|
||||||
|
return score >= 3 or score + 0.01 >= len(words)
|
||||||
|
|
||||||
|
def next_words_segment(segments: List[dict]) -> Optional[dict]:
|
||||||
|
return next((s for s in segments if s["words"]), None)
|
||||||
|
|
||||||
|
timestamp_tokens: torch.Tensor = tokens.ge(tokenizer.timestamp_begin)
|
||||||
|
single_timestamp_ending = timestamp_tokens[-2:].tolist() == [False, True]
|
||||||
|
|
||||||
|
consecutive = torch.where(timestamp_tokens[:-1] & timestamp_tokens[1:])[0]
|
||||||
|
consecutive.add_(1)
|
||||||
|
if len(consecutive) > 0:
|
||||||
|
# if the output contains two consecutive timestamp tokens
|
||||||
|
slices = consecutive.tolist()
|
||||||
|
if single_timestamp_ending:
|
||||||
|
slices.append(len(tokens))
|
||||||
|
|
||||||
|
last_slice = 0
|
||||||
|
for current_slice in slices:
|
||||||
|
sliced_tokens = tokens[last_slice:current_slice]
|
||||||
|
start_timestamp_pos = (
|
||||||
|
sliced_tokens[0].item() - tokenizer.timestamp_begin
|
||||||
|
)
|
||||||
|
end_timestamp_pos = (
|
||||||
|
sliced_tokens[-1].item() - tokenizer.timestamp_begin
|
||||||
|
)
|
||||||
|
current_segments.append(
|
||||||
|
new_segment(
|
||||||
|
start=time_offset + start_timestamp_pos * time_precision,
|
||||||
|
end=time_offset + end_timestamp_pos * time_precision,
|
||||||
|
tokens=sliced_tokens,
|
||||||
|
result=result,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
last_slice = current_slice
|
||||||
|
|
||||||
|
if single_timestamp_ending:
|
||||||
|
# single timestamp at the end means no speech after the last timestamp.
|
||||||
|
seek += segment_size
|
||||||
|
else:
|
||||||
|
# otherwise, ignore the unfinished segment and seek to the last timestamp
|
||||||
|
last_timestamp_pos = (
|
||||||
|
tokens[last_slice - 1].item() - tokenizer.timestamp_begin
|
||||||
|
)
|
||||||
|
seek += last_timestamp_pos * input_stride
|
||||||
|
else:
|
||||||
|
duration = segment_duration
|
||||||
|
timestamps = tokens[timestamp_tokens.nonzero().flatten()]
|
||||||
|
if (
|
||||||
|
len(timestamps) > 0
|
||||||
|
and timestamps[-1].item() != tokenizer.timestamp_begin
|
||||||
|
):
|
||||||
|
# no consecutive timestamps but it has a timestamp; use the last one.
|
||||||
|
last_timestamp_pos = (
|
||||||
|
timestamps[-1].item() - tokenizer.timestamp_begin
|
||||||
|
)
|
||||||
|
duration = last_timestamp_pos * time_precision
|
||||||
|
|
||||||
|
current_segments.append(
|
||||||
|
new_segment(
|
||||||
|
start=time_offset,
|
||||||
|
end=time_offset + duration,
|
||||||
|
tokens=tokens,
|
||||||
|
result=result,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
seek += segment_size
|
||||||
|
|
||||||
|
if word_timestamps:
|
||||||
|
add_word_timestamps(
|
||||||
|
segments=current_segments,
|
||||||
|
model=model,
|
||||||
|
tokenizer=tokenizer,
|
||||||
|
mel=mel_segment,
|
||||||
|
num_frames=segment_size,
|
||||||
|
prepend_punctuations=prepend_punctuations,
|
||||||
|
append_punctuations=append_punctuations,
|
||||||
|
last_speech_timestamp=last_speech_timestamp,
|
||||||
|
)
|
||||||
|
|
||||||
|
if not single_timestamp_ending:
|
||||||
|
last_word_end = get_end(current_segments)
|
||||||
|
if last_word_end is not None and last_word_end > time_offset:
|
||||||
|
seek = round(last_word_end * FRAMES_PER_SECOND)
|
||||||
|
|
||||||
|
# skip silence before possible hallucinations
|
||||||
|
if hallucination_silence_threshold is not None:
|
||||||
|
threshold = hallucination_silence_threshold
|
||||||
|
if not single_timestamp_ending:
|
||||||
|
last_word_end = get_end(current_segments)
|
||||||
|
if last_word_end is not None and last_word_end > time_offset:
|
||||||
|
remaining_duration = window_end_time - last_word_end
|
||||||
|
if remaining_duration > threshold:
|
||||||
|
seek = round(last_word_end * FRAMES_PER_SECOND)
|
||||||
|
else:
|
||||||
|
seek = previous_seek + segment_size
|
||||||
|
|
||||||
|
# if first segment might be a hallucination, skip leading silence
|
||||||
|
first_segment = next_words_segment(current_segments)
|
||||||
|
if first_segment is not None and is_segment_anomaly(first_segment):
|
||||||
|
gap = first_segment["start"] - time_offset
|
||||||
|
if gap > threshold:
|
||||||
|
seek = previous_seek + round(gap * FRAMES_PER_SECOND)
|
||||||
|
continue
|
||||||
|
|
||||||
|
# skip silence before any possible hallucination that is surrounded
|
||||||
|
# by silence or more hallucinations
|
||||||
|
hal_last_end = last_speech_timestamp
|
||||||
|
for si in range(len(current_segments)):
|
||||||
|
segment = current_segments[si]
|
||||||
|
if not segment["words"]:
|
||||||
|
continue
|
||||||
|
if is_segment_anomaly(segment):
|
||||||
|
next_segment = next_words_segment(
|
||||||
|
current_segments[si + 1 :]
|
||||||
|
)
|
||||||
|
if next_segment is not None:
|
||||||
|
hal_next_start = next_segment["words"][0]["start"]
|
||||||
|
else:
|
||||||
|
hal_next_start = time_offset + segment_duration
|
||||||
|
silence_before = (
|
||||||
|
segment["start"] - hal_last_end > threshold
|
||||||
|
or segment["start"] < threshold
|
||||||
|
or segment["start"] - time_offset < 2.0
|
||||||
|
)
|
||||||
|
silence_after = (
|
||||||
|
hal_next_start - segment["end"] > threshold
|
||||||
|
or is_segment_anomaly(next_segment)
|
||||||
|
or window_end_time - segment["end"] < 2.0
|
||||||
|
)
|
||||||
|
if silence_before and silence_after:
|
||||||
|
seek = round(
|
||||||
|
max(time_offset + 1, segment["start"])
|
||||||
|
* FRAMES_PER_SECOND
|
||||||
|
)
|
||||||
|
if content_duration - segment["end"] < threshold:
|
||||||
|
seek = content_frames
|
||||||
|
current_segments[si:] = []
|
||||||
|
break
|
||||||
|
hal_last_end = segment["end"]
|
||||||
|
|
||||||
|
last_word_end = get_end(current_segments)
|
||||||
|
if last_word_end is not None:
|
||||||
|
last_speech_timestamp = last_word_end
|
||||||
|
|
||||||
|
if verbose:
|
||||||
|
for segment in current_segments:
|
||||||
|
start, end, text = segment["start"], segment["end"], segment["text"]
|
||||||
|
line = f"[{format_timestamp(start)} --> {format_timestamp(end)}] {text}"
|
||||||
|
print(make_safe(line))
|
||||||
|
|
||||||
|
# if a segment is instantaneous or does not contain text, clear it
|
||||||
|
for i, segment in enumerate(current_segments):
|
||||||
|
if segment["start"] == segment["end"] or segment["text"].strip() == "":
|
||||||
|
segment["text"] = ""
|
||||||
|
segment["tokens"] = []
|
||||||
|
segment["words"] = []
|
||||||
|
|
||||||
|
all_segments.extend(
|
||||||
|
[
|
||||||
|
{"id": i, **segment}
|
||||||
|
for i, segment in enumerate(
|
||||||
|
current_segments, start=len(all_segments)
|
||||||
|
)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
all_tokens.extend(
|
||||||
|
[token for segment in current_segments for token in segment["tokens"]]
|
||||||
|
)
|
||||||
|
|
||||||
|
if not condition_on_previous_text or result.temperature > 0.5:
|
||||||
|
# do not feed the prompt tokens if a high temperature was used
|
||||||
|
prompt_reset_since = len(all_tokens)
|
||||||
|
|
||||||
|
# update progress bar
|
||||||
|
pbar.update(min(content_frames, seek) - previous_seek)
|
||||||
|
|
||||||
|
return dict(
|
||||||
|
text=tokenizer.decode(all_tokens[len(initial_prompt_tokens) :]),
|
||||||
|
segments=all_segments,
|
||||||
|
language=language,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def cli():
|
||||||
|
from . import available_models
|
||||||
|
|
||||||
|
def valid_model_name(name):
|
||||||
|
if name in available_models() or os.path.exists(name):
|
||||||
|
return name
|
||||||
|
raise ValueError(
|
||||||
|
f"model should be one of {available_models()} or path to a model checkpoint"
|
||||||
|
)
|
||||||
|
|
||||||
|
# fmt: off
|
||||||
|
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
|
||||||
|
parser.add_argument("audio", nargs="+", type=str, help="audio file(s) to transcribe")
|
||||||
|
parser.add_argument("--model", default="turbo", type=valid_model_name, help="name of the Whisper model to use")
|
||||||
|
parser.add_argument("--model_dir", type=str, default=None, help="the path to save model files; uses ~/.cache/whisper by default")
|
||||||
|
parser.add_argument("--device", default="cuda" if torch.cuda.is_available() else "cpu", help="device to use for PyTorch inference")
|
||||||
|
parser.add_argument("--output_dir", "-o", type=str, default=".", help="directory to save the outputs")
|
||||||
|
parser.add_argument("--output_format", "-f", type=str, default="all", choices=["txt", "vtt", "srt", "tsv", "json", "all"], help="format of the output file; if not specified, all available formats will be produced")
|
||||||
|
parser.add_argument("--verbose", type=str2bool, default=True, help="whether to print out the progress and debug messages")
|
||||||
|
|
||||||
|
parser.add_argument("--task", type=str, default="transcribe", choices=["transcribe", "translate"], help="whether to perform X->X speech recognition ('transcribe') or X->English translation ('translate')")
|
||||||
|
parser.add_argument("--language", type=str, default=None, choices=sorted(LANGUAGES.keys()) + sorted([k.title() for k in TO_LANGUAGE_CODE.keys()]), help="language spoken in the audio, specify None to perform language detection")
|
||||||
|
|
||||||
|
parser.add_argument("--temperature", type=float, default=0, help="temperature to use for sampling")
|
||||||
|
parser.add_argument("--best_of", type=optional_int, default=5, help="number of candidates when sampling with non-zero temperature")
|
||||||
|
parser.add_argument("--beam_size", type=optional_int, default=5, help="number of beams in beam search, only applicable when temperature is zero")
|
||||||
|
parser.add_argument("--patience", type=float, default=None, help="optional patience value to use in beam decoding, as in https://arxiv.org/abs/2204.05424, the default (1.0) is equivalent to conventional beam search")
|
||||||
|
parser.add_argument("--length_penalty", type=float, default=None, help="optional token length penalty coefficient (alpha) as in https://arxiv.org/abs/1609.08144, uses simple length normalization by default")
|
||||||
|
|
||||||
|
parser.add_argument("--suppress_tokens", type=str, default="-1", help="comma-separated list of token ids to suppress during sampling; '-1' will suppress most special characters except common punctuations")
|
||||||
|
parser.add_argument("--initial_prompt", type=str, default=None, help="optional text to provide as a prompt for the first window.")
|
||||||
|
parser.add_argument("--carry_initial_prompt", type=str2bool, default=False, help="if True, prepend initial_prompt to every internal decode() call. May reduce the effectiveness of condition_on_previous_text")
|
||||||
|
|
||||||
|
parser.add_argument("--condition_on_previous_text", type=str2bool, default=True, help="if True, provide the previous output of the model as a prompt for the next window; disabling may make the text inconsistent across windows, but the model becomes less prone to getting stuck in a failure loop")
|
||||||
|
parser.add_argument("--fp16", type=str2bool, default=True, help="whether to perform inference in fp16; True by default")
|
||||||
|
|
||||||
|
parser.add_argument("--temperature_increment_on_fallback", type=optional_float, default=0.2, help="temperature to increase when falling back when the decoding fails to meet either of the thresholds below")
|
||||||
|
parser.add_argument("--compression_ratio_threshold", type=optional_float, default=2.4, help="if the gzip compression ratio is higher than this value, treat the decoding as failed")
|
||||||
|
parser.add_argument("--logprob_threshold", type=optional_float, default=-1.0, help="if the average log probability is lower than this value, treat the decoding as failed")
|
||||||
|
parser.add_argument("--no_speech_threshold", type=optional_float, default=0.6, help="if the probability of the <|nospeech|> token is higher than this value AND the decoding has failed due to `logprob_threshold`, consider the segment as silence")
|
||||||
|
parser.add_argument("--word_timestamps", type=str2bool, default=False, help="(experimental) extract word-level timestamps and refine the results based on them")
|
||||||
|
parser.add_argument("--prepend_punctuations", type=str, default="\"\'“¿([{-", help="if word_timestamps is True, merge these punctuation symbols with the next word")
|
||||||
|
parser.add_argument("--append_punctuations", type=str, default="\"\'.。,,!!??::”)]}、", help="if word_timestamps is True, merge these punctuation symbols with the previous word")
|
||||||
|
parser.add_argument("--highlight_words", type=str2bool, default=False, help="(requires --word_timestamps True) underline each word as it is spoken in srt and vtt")
|
||||||
|
parser.add_argument("--max_line_width", type=optional_int, default=None, help="(requires --word_timestamps True) the maximum number of characters in a line before breaking the line")
|
||||||
|
parser.add_argument("--max_line_count", type=optional_int, default=None, help="(requires --word_timestamps True) the maximum number of lines in a segment")
|
||||||
|
parser.add_argument("--max_words_per_line", type=optional_int, default=None, help="(requires --word_timestamps True, no effect with --max_line_width) the maximum number of words in a segment")
|
||||||
|
parser.add_argument("--threads", type=optional_int, default=0, help="number of threads used by torch for CPU inference; supercedes MKL_NUM_THREADS/OMP_NUM_THREADS")
|
||||||
|
parser.add_argument("--clip_timestamps", type=str, default="0", help="comma-separated list start,end,start,end,... timestamps (in seconds) of clips to process, where the last end timestamp defaults to the end of the file")
|
||||||
|
parser.add_argument("--hallucination_silence_threshold", type=optional_float, help="(requires --word_timestamps True) skip silent periods longer than this threshold (in seconds) when a possible hallucination is detected")
|
||||||
|
# fmt: on
|
||||||
|
|
||||||
|
args = parser.parse_args().__dict__
|
||||||
|
model_name: str = args.pop("model")
|
||||||
|
model_dir: str = args.pop("model_dir")
|
||||||
|
output_dir: str = args.pop("output_dir")
|
||||||
|
output_format: str = args.pop("output_format")
|
||||||
|
device: str = args.pop("device")
|
||||||
|
os.makedirs(output_dir, exist_ok=True)
|
||||||
|
|
||||||
|
if model_name.endswith(".en") and args["language"] not in {"en", "English"}:
|
||||||
|
if args["language"] is not None:
|
||||||
|
warnings.warn(
|
||||||
|
f"{model_name} is an English-only model but receipted '{args['language']}'; using English instead."
|
||||||
|
)
|
||||||
|
args["language"] = "en"
|
||||||
|
|
||||||
|
temperature = args.pop("temperature")
|
||||||
|
if (increment := args.pop("temperature_increment_on_fallback")) is not None:
|
||||||
|
temperature = tuple(np.arange(temperature, 1.0 + 1e-6, increment))
|
||||||
|
else:
|
||||||
|
temperature = [temperature]
|
||||||
|
|
||||||
|
if (threads := args.pop("threads")) > 0:
|
||||||
|
torch.set_num_threads(threads)
|
||||||
|
|
||||||
|
from . import load_model
|
||||||
|
|
||||||
|
model = load_model(model_name, device=device, download_root=model_dir)
|
||||||
|
|
||||||
|
writer = get_writer(output_format, output_dir)
|
||||||
|
word_options = [
|
||||||
|
"highlight_words",
|
||||||
|
"max_line_count",
|
||||||
|
"max_line_width",
|
||||||
|
"max_words_per_line",
|
||||||
|
]
|
||||||
|
if not args["word_timestamps"]:
|
||||||
|
for option in word_options:
|
||||||
|
if args[option]:
|
||||||
|
parser.error(f"--{option} requires --word_timestamps True")
|
||||||
|
if args["max_line_count"] and not args["max_line_width"]:
|
||||||
|
warnings.warn("--max_line_count has no effect without --max_line_width")
|
||||||
|
if args["max_words_per_line"] and args["max_line_width"]:
|
||||||
|
warnings.warn("--max_words_per_line has no effect with --max_line_width")
|
||||||
|
writer_args = {arg: args.pop(arg) for arg in word_options}
|
||||||
|
for audio_path in args.pop("audio"):
|
||||||
|
try:
|
||||||
|
result = transcribe(model, audio_path, temperature=temperature, **args)
|
||||||
|
writer(result, audio_path, **writer_args)
|
||||||
|
except Exception as e:
|
||||||
|
traceback.print_exc()
|
||||||
|
print(f"Skipping {audio_path} due to {type(e).__name__}: {str(e)}")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
cli()
|
||||||
117
whisperlivekit/simul_whisper/whisper/triton_ops.py
Normal file
117
whisperlivekit/simul_whisper/whisper/triton_ops.py
Normal file
@@ -0,0 +1,117 @@
|
|||||||
|
from functools import lru_cache
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
import torch
|
||||||
|
|
||||||
|
try:
|
||||||
|
import triton
|
||||||
|
import triton.language as tl
|
||||||
|
except ImportError:
|
||||||
|
raise RuntimeError("triton import failed; try `pip install --pre triton`")
|
||||||
|
|
||||||
|
|
||||||
|
@triton.jit
|
||||||
|
def dtw_kernel(
|
||||||
|
cost, trace, x, x_stride, cost_stride, trace_stride, N, M, BLOCK_SIZE: tl.constexpr
|
||||||
|
):
|
||||||
|
offsets = tl.arange(0, BLOCK_SIZE)
|
||||||
|
mask = offsets < M
|
||||||
|
|
||||||
|
for k in range(1, N + M + 1): # k = i + j
|
||||||
|
tl.debug_barrier()
|
||||||
|
|
||||||
|
p0 = cost + (k - 1) * cost_stride
|
||||||
|
p1 = cost + k * cost_stride
|
||||||
|
p2 = cost + k * cost_stride + 1
|
||||||
|
|
||||||
|
c0 = tl.load(p0 + offsets, mask=mask)
|
||||||
|
c1 = tl.load(p1 + offsets, mask=mask)
|
||||||
|
c2 = tl.load(p2 + offsets, mask=mask)
|
||||||
|
|
||||||
|
x_row = tl.load(x + (k - 1) * x_stride + offsets, mask=mask, other=0)
|
||||||
|
cost_row = x_row + tl.minimum(tl.minimum(c0, c1), c2)
|
||||||
|
|
||||||
|
cost_ptr = cost + (k + 1) * cost_stride + 1
|
||||||
|
tl.store(cost_ptr + offsets, cost_row, mask=mask)
|
||||||
|
|
||||||
|
trace_ptr = trace + (k + 1) * trace_stride + 1
|
||||||
|
tl.store(trace_ptr + offsets, 2, mask=mask & (c2 <= c0) & (c2 <= c1))
|
||||||
|
tl.store(trace_ptr + offsets, 1, mask=mask & (c1 <= c0) & (c1 <= c2))
|
||||||
|
tl.store(trace_ptr + offsets, 0, mask=mask & (c0 <= c1) & (c0 <= c2))
|
||||||
|
|
||||||
|
|
||||||
|
@lru_cache(maxsize=None)
|
||||||
|
def median_kernel(filter_width: int):
|
||||||
|
@triton.jit
|
||||||
|
def kernel(
|
||||||
|
y, x, x_stride, y_stride, BLOCK_SIZE: tl.constexpr
|
||||||
|
): # x.shape[-1] == filter_width
|
||||||
|
row_idx = tl.program_id(0)
|
||||||
|
offsets = tl.arange(0, BLOCK_SIZE)
|
||||||
|
mask = offsets < y_stride
|
||||||
|
|
||||||
|
x_ptr = x + row_idx * x_stride # noqa: F841
|
||||||
|
y_ptr = y + row_idx * y_stride
|
||||||
|
|
||||||
|
LOAD_ALL_ROWS_HERE # noqa: F821
|
||||||
|
|
||||||
|
BUBBLESORT_HERE # noqa: F821
|
||||||
|
|
||||||
|
tl.store(y_ptr + offsets, MIDDLE_ROW_HERE, mask=mask) # noqa: F821
|
||||||
|
|
||||||
|
kernel = triton.JITFunction(kernel.fn)
|
||||||
|
new_kernel = kernel.src.replace(
|
||||||
|
" LOAD_ALL_ROWS_HERE",
|
||||||
|
"\n".join(
|
||||||
|
[
|
||||||
|
f" row{i} = tl.load(x_ptr + offsets + {i}, mask=mask)"
|
||||||
|
for i in range(filter_width)
|
||||||
|
]
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
new_kernel = new_kernel.replace(
|
||||||
|
" BUBBLESORT_HERE",
|
||||||
|
"\n\n".join(
|
||||||
|
[
|
||||||
|
"\n\n".join(
|
||||||
|
[
|
||||||
|
"\n".join(
|
||||||
|
[
|
||||||
|
f" smaller = tl.where(row{j} < row{j + 1}, row{j}, row{j + 1})",
|
||||||
|
f" larger = tl.where(row{j} > row{j + 1}, row{j}, row{j + 1})",
|
||||||
|
f" row{j} = smaller",
|
||||||
|
f" row{j + 1} = larger",
|
||||||
|
]
|
||||||
|
)
|
||||||
|
for j in range(filter_width - i - 1)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
for i in range(filter_width // 2 + 1)
|
||||||
|
]
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
new_kernel = new_kernel.replace("MIDDLE_ROW_HERE", f"row{filter_width // 2}")
|
||||||
|
|
||||||
|
if hasattr(kernel, "_unsafe_update_src") is True:
|
||||||
|
kernel._unsafe_update_src(new_kernel)
|
||||||
|
kernel.hash = None
|
||||||
|
else:
|
||||||
|
kernel.src = new_kernel
|
||||||
|
|
||||||
|
return kernel
|
||||||
|
|
||||||
|
|
||||||
|
def median_filter_cuda(x: torch.Tensor, filter_width: int):
|
||||||
|
"""Apply a median filter of given width along the last dimension of x"""
|
||||||
|
slices = x.contiguous().unfold(-1, filter_width, 1)
|
||||||
|
grid = np.prod(slices.shape[:-2])
|
||||||
|
|
||||||
|
kernel = median_kernel(filter_width)
|
||||||
|
y = torch.empty_like(slices[..., 0])
|
||||||
|
|
||||||
|
BLOCK_SIZE = 1 << (y.stride(-2) - 1).bit_length()
|
||||||
|
kernel[(grid,)](y, x, x.stride(-2), y.stride(-2), BLOCK_SIZE=BLOCK_SIZE)
|
||||||
|
|
||||||
|
return y
|
||||||
318
whisperlivekit/simul_whisper/whisper/utils.py
Normal file
318
whisperlivekit/simul_whisper/whisper/utils.py
Normal file
@@ -0,0 +1,318 @@
|
|||||||
|
import json
|
||||||
|
import os
|
||||||
|
import re
|
||||||
|
import sys
|
||||||
|
import zlib
|
||||||
|
from typing import Callable, List, Optional, TextIO
|
||||||
|
|
||||||
|
system_encoding = sys.getdefaultencoding()
|
||||||
|
|
||||||
|
if system_encoding != "utf-8":
|
||||||
|
|
||||||
|
def make_safe(string):
|
||||||
|
# replaces any character not representable using the system default encoding with an '?',
|
||||||
|
# avoiding UnicodeEncodeError (https://github.com/openai/whisper/discussions/729).
|
||||||
|
return string.encode(system_encoding, errors="replace").decode(system_encoding)
|
||||||
|
|
||||||
|
else:
|
||||||
|
|
||||||
|
def make_safe(string):
|
||||||
|
# utf-8 can encode any Unicode code point, so no need to do the round-trip encoding
|
||||||
|
return string
|
||||||
|
|
||||||
|
|
||||||
|
def exact_div(x, y):
|
||||||
|
assert x % y == 0
|
||||||
|
return x // y
|
||||||
|
|
||||||
|
|
||||||
|
def str2bool(string):
|
||||||
|
str2val = {"True": True, "False": False}
|
||||||
|
if string in str2val:
|
||||||
|
return str2val[string]
|
||||||
|
else:
|
||||||
|
raise ValueError(f"Expected one of {set(str2val.keys())}, got {string}")
|
||||||
|
|
||||||
|
|
||||||
|
def optional_int(string):
|
||||||
|
return None if string == "None" else int(string)
|
||||||
|
|
||||||
|
|
||||||
|
def optional_float(string):
|
||||||
|
return None if string == "None" else float(string)
|
||||||
|
|
||||||
|
|
||||||
|
def compression_ratio(text) -> float:
|
||||||
|
text_bytes = text.encode("utf-8")
|
||||||
|
return len(text_bytes) / len(zlib.compress(text_bytes))
|
||||||
|
|
||||||
|
|
||||||
|
def format_timestamp(
|
||||||
|
seconds: float, always_include_hours: bool = False, decimal_marker: str = "."
|
||||||
|
):
|
||||||
|
assert seconds >= 0, "non-negative timestamp expected"
|
||||||
|
milliseconds = round(seconds * 1000.0)
|
||||||
|
|
||||||
|
hours = milliseconds // 3_600_000
|
||||||
|
milliseconds -= hours * 3_600_000
|
||||||
|
|
||||||
|
minutes = milliseconds // 60_000
|
||||||
|
milliseconds -= minutes * 60_000
|
||||||
|
|
||||||
|
seconds = milliseconds // 1_000
|
||||||
|
milliseconds -= seconds * 1_000
|
||||||
|
|
||||||
|
hours_marker = f"{hours:02d}:" if always_include_hours or hours > 0 else ""
|
||||||
|
return (
|
||||||
|
f"{hours_marker}{minutes:02d}:{seconds:02d}{decimal_marker}{milliseconds:03d}"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def get_start(segments: List[dict]) -> Optional[float]:
|
||||||
|
return next(
|
||||||
|
(w["start"] for s in segments for w in s["words"]),
|
||||||
|
segments[0]["start"] if segments else None,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def get_end(segments: List[dict]) -> Optional[float]:
|
||||||
|
return next(
|
||||||
|
(w["end"] for s in reversed(segments) for w in reversed(s["words"])),
|
||||||
|
segments[-1]["end"] if segments else None,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class ResultWriter:
|
||||||
|
extension: str
|
||||||
|
|
||||||
|
def __init__(self, output_dir: str):
|
||||||
|
self.output_dir = output_dir
|
||||||
|
|
||||||
|
def __call__(
|
||||||
|
self, result: dict, audio_path: str, options: Optional[dict] = None, **kwargs
|
||||||
|
):
|
||||||
|
audio_basename = os.path.basename(audio_path)
|
||||||
|
audio_basename = os.path.splitext(audio_basename)[0]
|
||||||
|
output_path = os.path.join(
|
||||||
|
self.output_dir, audio_basename + "." + self.extension
|
||||||
|
)
|
||||||
|
|
||||||
|
with open(output_path, "w", encoding="utf-8") as f:
|
||||||
|
self.write_result(result, file=f, options=options, **kwargs)
|
||||||
|
|
||||||
|
def write_result(
|
||||||
|
self, result: dict, file: TextIO, options: Optional[dict] = None, **kwargs
|
||||||
|
):
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
|
||||||
|
class WriteTXT(ResultWriter):
|
||||||
|
extension: str = "txt"
|
||||||
|
|
||||||
|
def write_result(
|
||||||
|
self, result: dict, file: TextIO, options: Optional[dict] = None, **kwargs
|
||||||
|
):
|
||||||
|
for segment in result["segments"]:
|
||||||
|
print(segment["text"].strip(), file=file, flush=True)
|
||||||
|
|
||||||
|
|
||||||
|
class SubtitlesWriter(ResultWriter):
|
||||||
|
always_include_hours: bool
|
||||||
|
decimal_marker: str
|
||||||
|
|
||||||
|
def iterate_result(
|
||||||
|
self,
|
||||||
|
result: dict,
|
||||||
|
options: Optional[dict] = None,
|
||||||
|
*,
|
||||||
|
max_line_width: Optional[int] = None,
|
||||||
|
max_line_count: Optional[int] = None,
|
||||||
|
highlight_words: bool = False,
|
||||||
|
max_words_per_line: Optional[int] = None,
|
||||||
|
):
|
||||||
|
options = options or {}
|
||||||
|
max_line_width = max_line_width or options.get("max_line_width")
|
||||||
|
max_line_count = max_line_count or options.get("max_line_count")
|
||||||
|
highlight_words = highlight_words or options.get("highlight_words", False)
|
||||||
|
max_words_per_line = max_words_per_line or options.get("max_words_per_line")
|
||||||
|
preserve_segments = max_line_count is None or max_line_width is None
|
||||||
|
max_line_width = max_line_width or 1000
|
||||||
|
max_words_per_line = max_words_per_line or 1000
|
||||||
|
|
||||||
|
def iterate_subtitles():
|
||||||
|
line_len = 0
|
||||||
|
line_count = 1
|
||||||
|
# the next subtitle to yield (a list of word timings with whitespace)
|
||||||
|
subtitle: List[dict] = []
|
||||||
|
last: float = get_start(result["segments"]) or 0.0
|
||||||
|
for segment in result["segments"]:
|
||||||
|
chunk_index = 0
|
||||||
|
words_count = max_words_per_line
|
||||||
|
while chunk_index < len(segment["words"]):
|
||||||
|
remaining_words = len(segment["words"]) - chunk_index
|
||||||
|
if max_words_per_line > len(segment["words"]) - chunk_index:
|
||||||
|
words_count = remaining_words
|
||||||
|
for i, original_timing in enumerate(
|
||||||
|
segment["words"][chunk_index : chunk_index + words_count]
|
||||||
|
):
|
||||||
|
timing = original_timing.copy()
|
||||||
|
long_pause = (
|
||||||
|
not preserve_segments and timing["start"] - last > 3.0
|
||||||
|
)
|
||||||
|
has_room = line_len + len(timing["word"]) <= max_line_width
|
||||||
|
seg_break = i == 0 and len(subtitle) > 0 and preserve_segments
|
||||||
|
if (
|
||||||
|
line_len > 0
|
||||||
|
and has_room
|
||||||
|
and not long_pause
|
||||||
|
and not seg_break
|
||||||
|
):
|
||||||
|
# line continuation
|
||||||
|
line_len += len(timing["word"])
|
||||||
|
else:
|
||||||
|
# new line
|
||||||
|
timing["word"] = timing["word"].strip()
|
||||||
|
if (
|
||||||
|
len(subtitle) > 0
|
||||||
|
and max_line_count is not None
|
||||||
|
and (long_pause or line_count >= max_line_count)
|
||||||
|
or seg_break
|
||||||
|
):
|
||||||
|
# subtitle break
|
||||||
|
yield subtitle
|
||||||
|
subtitle = []
|
||||||
|
line_count = 1
|
||||||
|
elif line_len > 0:
|
||||||
|
# line break
|
||||||
|
line_count += 1
|
||||||
|
timing["word"] = "\n" + timing["word"]
|
||||||
|
line_len = len(timing["word"].strip())
|
||||||
|
subtitle.append(timing)
|
||||||
|
last = timing["start"]
|
||||||
|
chunk_index += max_words_per_line
|
||||||
|
if len(subtitle) > 0:
|
||||||
|
yield subtitle
|
||||||
|
|
||||||
|
if len(result["segments"]) > 0 and "words" in result["segments"][0]:
|
||||||
|
for subtitle in iterate_subtitles():
|
||||||
|
subtitle_start = self.format_timestamp(subtitle[0]["start"])
|
||||||
|
subtitle_end = self.format_timestamp(subtitle[-1]["end"])
|
||||||
|
subtitle_text = "".join([word["word"] for word in subtitle])
|
||||||
|
if highlight_words:
|
||||||
|
last = subtitle_start
|
||||||
|
all_words = [timing["word"] for timing in subtitle]
|
||||||
|
for i, this_word in enumerate(subtitle):
|
||||||
|
start = self.format_timestamp(this_word["start"])
|
||||||
|
end = self.format_timestamp(this_word["end"])
|
||||||
|
if last != start:
|
||||||
|
yield last, start, subtitle_text
|
||||||
|
|
||||||
|
yield start, end, "".join(
|
||||||
|
[
|
||||||
|
(
|
||||||
|
re.sub(r"^(\s*)(.*)$", r"\1<u>\2</u>", word)
|
||||||
|
if j == i
|
||||||
|
else word
|
||||||
|
)
|
||||||
|
for j, word in enumerate(all_words)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
last = end
|
||||||
|
else:
|
||||||
|
yield subtitle_start, subtitle_end, subtitle_text
|
||||||
|
else:
|
||||||
|
for segment in result["segments"]:
|
||||||
|
segment_start = self.format_timestamp(segment["start"])
|
||||||
|
segment_end = self.format_timestamp(segment["end"])
|
||||||
|
segment_text = segment["text"].strip().replace("-->", "->")
|
||||||
|
yield segment_start, segment_end, segment_text
|
||||||
|
|
||||||
|
def format_timestamp(self, seconds: float):
|
||||||
|
return format_timestamp(
|
||||||
|
seconds=seconds,
|
||||||
|
always_include_hours=self.always_include_hours,
|
||||||
|
decimal_marker=self.decimal_marker,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class WriteVTT(SubtitlesWriter):
|
||||||
|
extension: str = "vtt"
|
||||||
|
always_include_hours: bool = False
|
||||||
|
decimal_marker: str = "."
|
||||||
|
|
||||||
|
def write_result(
|
||||||
|
self, result: dict, file: TextIO, options: Optional[dict] = None, **kwargs
|
||||||
|
):
|
||||||
|
print("WEBVTT\n", file=file)
|
||||||
|
for start, end, text in self.iterate_result(result, options, **kwargs):
|
||||||
|
print(f"{start} --> {end}\n{text}\n", file=file, flush=True)
|
||||||
|
|
||||||
|
|
||||||
|
class WriteSRT(SubtitlesWriter):
|
||||||
|
extension: str = "srt"
|
||||||
|
always_include_hours: bool = True
|
||||||
|
decimal_marker: str = ","
|
||||||
|
|
||||||
|
def write_result(
|
||||||
|
self, result: dict, file: TextIO, options: Optional[dict] = None, **kwargs
|
||||||
|
):
|
||||||
|
for i, (start, end, text) in enumerate(
|
||||||
|
self.iterate_result(result, options, **kwargs), start=1
|
||||||
|
):
|
||||||
|
print(f"{i}\n{start} --> {end}\n{text}\n", file=file, flush=True)
|
||||||
|
|
||||||
|
|
||||||
|
class WriteTSV(ResultWriter):
|
||||||
|
"""
|
||||||
|
Write a transcript to a file in TSV (tab-separated values) format containing lines like:
|
||||||
|
<start time in integer milliseconds>\t<end time in integer milliseconds>\t<transcript text>
|
||||||
|
|
||||||
|
Using integer milliseconds as start and end times means there's no chance of interference from
|
||||||
|
an environment setting a language encoding that causes the decimal in a floating point number
|
||||||
|
to appear as a comma; also is faster and more efficient to parse & store, e.g., in C++.
|
||||||
|
"""
|
||||||
|
|
||||||
|
extension: str = "tsv"
|
||||||
|
|
||||||
|
def write_result(
|
||||||
|
self, result: dict, file: TextIO, options: Optional[dict] = None, **kwargs
|
||||||
|
):
|
||||||
|
print("start", "end", "text", sep="\t", file=file)
|
||||||
|
for segment in result["segments"]:
|
||||||
|
print(round(1000 * segment["start"]), file=file, end="\t")
|
||||||
|
print(round(1000 * segment["end"]), file=file, end="\t")
|
||||||
|
print(segment["text"].strip().replace("\t", " "), file=file, flush=True)
|
||||||
|
|
||||||
|
|
||||||
|
class WriteJSON(ResultWriter):
|
||||||
|
extension: str = "json"
|
||||||
|
|
||||||
|
def write_result(
|
||||||
|
self, result: dict, file: TextIO, options: Optional[dict] = None, **kwargs
|
||||||
|
):
|
||||||
|
json.dump(result, file)
|
||||||
|
|
||||||
|
|
||||||
|
def get_writer(
|
||||||
|
output_format: str, output_dir: str
|
||||||
|
) -> Callable[[dict, TextIO, dict], None]:
|
||||||
|
writers = {
|
||||||
|
"txt": WriteTXT,
|
||||||
|
"vtt": WriteVTT,
|
||||||
|
"srt": WriteSRT,
|
||||||
|
"tsv": WriteTSV,
|
||||||
|
"json": WriteJSON,
|
||||||
|
}
|
||||||
|
|
||||||
|
if output_format == "all":
|
||||||
|
all_writers = [writer(output_dir) for writer in writers.values()]
|
||||||
|
|
||||||
|
def write_all(
|
||||||
|
result: dict, file: TextIO, options: Optional[dict] = None, **kwargs
|
||||||
|
):
|
||||||
|
for writer in all_writers:
|
||||||
|
writer(result, file, options, **kwargs)
|
||||||
|
|
||||||
|
return write_all
|
||||||
|
|
||||||
|
return writers[output_format](output_dir)
|
||||||
1
whisperlivekit/simul_whisper/whisper/version.py
Normal file
1
whisperlivekit/simul_whisper/whisper/version.py
Normal file
@@ -0,0 +1 @@
|
|||||||
|
__version__ = "20250625"
|
||||||
36
whisperlivekit/timed_objects.py
Normal file
36
whisperlivekit/timed_objects.py
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
from dataclasses import dataclass
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class TimedText:
|
||||||
|
start: Optional[float]
|
||||||
|
end: Optional[float]
|
||||||
|
text: Optional[str] = ''
|
||||||
|
speaker: Optional[int] = -1
|
||||||
|
probability: Optional[float] = None
|
||||||
|
is_dummy: Optional[bool] = False
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class ASRToken(TimedText):
|
||||||
|
def with_offset(self, offset: float) -> "ASRToken":
|
||||||
|
"""Return a new token with the time offset added."""
|
||||||
|
return ASRToken(self.start + offset, self.end + offset, self.text, self.speaker, self.probability)
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Sentence(TimedText):
|
||||||
|
pass
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Transcript(TimedText):
|
||||||
|
pass
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class SpeakerSegment(TimedText):
|
||||||
|
"""Represents a segment of audio attributed to a specific speaker.
|
||||||
|
No text nor probability is associated with this segment.
|
||||||
|
"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Silence():
|
||||||
|
duration: float
|
||||||
60
whisperlivekit/trail_repetition.py
Normal file
60
whisperlivekit/trail_repetition.py
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
from typing import Sequence, Callable, Any, Optional, Dict
|
||||||
|
|
||||||
|
def _detect_tail_repetition(
|
||||||
|
seq: Sequence[Any],
|
||||||
|
key: Callable[[Any], Any] = lambda x: x, # extract comparable value
|
||||||
|
min_block: int = 1, # set to 2 to ignore 1-token loops like "."
|
||||||
|
max_tail: int = 300, # search window from the end for speed
|
||||||
|
prefer: str = "longest", # "longest" coverage or "smallest" block
|
||||||
|
) -> Optional[Dict]:
|
||||||
|
vals = [key(x) for x in seq][-max_tail:]
|
||||||
|
n = len(vals)
|
||||||
|
best = None
|
||||||
|
|
||||||
|
# try every possible block length
|
||||||
|
for b in range(min_block, n // 2 + 1):
|
||||||
|
block = vals[-b:]
|
||||||
|
# count how many times this block repeats contiguously at the very end
|
||||||
|
count, i = 0, n
|
||||||
|
while i - b >= 0 and vals[i - b:i] == block:
|
||||||
|
count += 1
|
||||||
|
i -= b
|
||||||
|
|
||||||
|
if count >= 2:
|
||||||
|
cand = {
|
||||||
|
"block_size": b,
|
||||||
|
"count": count,
|
||||||
|
"start_index": len(seq) - count * b, # in original seq
|
||||||
|
"end_index": len(seq),
|
||||||
|
}
|
||||||
|
if (best is None or
|
||||||
|
(prefer == "longest" and count * b > best["count"] * best["block_size"]) or
|
||||||
|
(prefer == "smallest" and b < best["block_size"])):
|
||||||
|
best = cand
|
||||||
|
return best
|
||||||
|
|
||||||
|
def trim_tail_repetition(
|
||||||
|
seq: Sequence[Any],
|
||||||
|
key: Callable[[Any], Any] = lambda x: x,
|
||||||
|
min_block: int = 1,
|
||||||
|
max_tail: int = 300,
|
||||||
|
prefer: str = "longest",
|
||||||
|
keep: int = 1, # how many copies of the repeating block to keep at the end (0 or 1 are common)
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
Returns a new sequence with repeated tail trimmed.
|
||||||
|
keep=1 -> keep a single copy of the repeated block.
|
||||||
|
keep=0 -> remove all copies of the repeated block.
|
||||||
|
"""
|
||||||
|
rep = _detect_tail_repetition(seq, key, min_block, max_tail, prefer)
|
||||||
|
if not rep:
|
||||||
|
return seq, False # nothing to trim
|
||||||
|
|
||||||
|
b, c = rep["block_size"], rep["count"]
|
||||||
|
if keep < 0:
|
||||||
|
keep = 0
|
||||||
|
if keep >= c:
|
||||||
|
return seq, False # nothing to trim (already <= keep copies)
|
||||||
|
# new length = total - (copies_to_remove * block_size)
|
||||||
|
new_len = len(seq) - (c - keep) * b
|
||||||
|
return seq[:new_len], True
|
||||||
62
whisperlivekit/warmup.py
Normal file
62
whisperlivekit/warmup.py
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
|
||||||
|
import logging
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
def load_file(warmup_file=None, timeout=5):
|
||||||
|
import os
|
||||||
|
import tempfile
|
||||||
|
import librosa
|
||||||
|
|
||||||
|
if warmup_file is None:
|
||||||
|
# Download JFK sample if not already present
|
||||||
|
jfk_url = "https://github.com/ggerganov/whisper.cpp/raw/master/samples/jfk.wav"
|
||||||
|
temp_dir = tempfile.gettempdir()
|
||||||
|
warmup_file = os.path.join(temp_dir, "whisper_warmup_jfk.wav")
|
||||||
|
|
||||||
|
if not os.path.exists(warmup_file):
|
||||||
|
logger.debug(f"Downloading warmup file from {jfk_url}")
|
||||||
|
print(f"Downloading warmup file from {jfk_url}")
|
||||||
|
import time
|
||||||
|
import urllib.request
|
||||||
|
import urllib.error
|
||||||
|
import socket
|
||||||
|
|
||||||
|
original_timeout = socket.getdefaulttimeout()
|
||||||
|
socket.setdefaulttimeout(timeout)
|
||||||
|
|
||||||
|
start_time = time.time()
|
||||||
|
try:
|
||||||
|
urllib.request.urlretrieve(jfk_url, warmup_file)
|
||||||
|
logger.debug(f"Download successful in {time.time() - start_time:.2f}s")
|
||||||
|
except (urllib.error.URLError, socket.timeout) as e:
|
||||||
|
logger.warning(f"Download failed: {e}. Proceeding without warmup.")
|
||||||
|
return False
|
||||||
|
finally:
|
||||||
|
socket.setdefaulttimeout(original_timeout)
|
||||||
|
elif not warmup_file:
|
||||||
|
return False
|
||||||
|
|
||||||
|
if not warmup_file or not os.path.exists(warmup_file) or os.path.getsize(warmup_file) == 0:
|
||||||
|
logger.warning(f"Warmup file {warmup_file} invalid or missing.")
|
||||||
|
return False
|
||||||
|
|
||||||
|
try:
|
||||||
|
audio, sr = librosa.load(warmup_file, sr=16000)
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"Failed to load audio file: {e}")
|
||||||
|
return False
|
||||||
|
return audio
|
||||||
|
|
||||||
|
def warmup_asr(asr, warmup_file=None, timeout=5):
|
||||||
|
"""
|
||||||
|
Warmup the ASR model by transcribing a short audio file.
|
||||||
|
"""
|
||||||
|
audio = load_file(warmup_file=None, timeout=5)
|
||||||
|
asr.transcribe(audio)
|
||||||
|
logger.info("ASR model is warmed up")
|
||||||
|
|
||||||
|
def warmup_online(online, warmup_file=None, timeout=5):
|
||||||
|
audio = load_file(warmup_file=None, timeout=5)
|
||||||
|
online.warmup(audio)
|
||||||
|
logger.warning("ASR is warmed up")
|
||||||
0
whisperlivekit/web/__init__.py
Normal file
0
whisperlivekit/web/__init__.py
Normal file
402
whisperlivekit/web/live_transcription.css
Normal file
402
whisperlivekit/web/live_transcription.css
Normal file
@@ -0,0 +1,402 @@
|
|||||||
|
:root {
|
||||||
|
--bg: #ffffff;
|
||||||
|
--text: #111111;
|
||||||
|
--muted: #666666;
|
||||||
|
--border: #e5e5e5;
|
||||||
|
--chip-bg: rgba(0, 0, 0, 0.04);
|
||||||
|
--chip-text: #000000;
|
||||||
|
--spinner-border: #8d8d8d5c;
|
||||||
|
--spinner-top: #b0b0b0;
|
||||||
|
--silence-bg: #f3f3f3;
|
||||||
|
--loading-bg: rgba(255, 77, 77, 0.06);
|
||||||
|
--button-bg: #ffffff;
|
||||||
|
--button-border: #e9e9e9;
|
||||||
|
--wave-stroke: #000000;
|
||||||
|
--label-dia-text: #868686;
|
||||||
|
--label-trans-text: #111111;
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (prefers-color-scheme: dark) {
|
||||||
|
:root:not([data-theme="light"]) {
|
||||||
|
--bg: #0b0b0b;
|
||||||
|
--text: #e6e6e6;
|
||||||
|
--muted: #9aa0a6;
|
||||||
|
--border: #333333;
|
||||||
|
--chip-bg: rgba(255, 255, 255, 0.08);
|
||||||
|
--chip-text: #e6e6e6;
|
||||||
|
--spinner-border: #555555;
|
||||||
|
--spinner-top: #dddddd;
|
||||||
|
--silence-bg: #1a1a1a;
|
||||||
|
--loading-bg: rgba(255, 77, 77, 0.12);
|
||||||
|
--button-bg: #111111;
|
||||||
|
--button-border: #333333;
|
||||||
|
--wave-stroke: #e6e6e6;
|
||||||
|
--label-dia-text: #b3b3b3;
|
||||||
|
--label-trans-text: #ffffff;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
:root[data-theme="dark"] {
|
||||||
|
--bg: #0b0b0b;
|
||||||
|
--text: #e6e6e6;
|
||||||
|
--muted: #9aa0a6;
|
||||||
|
--border: #333333;
|
||||||
|
--chip-bg: rgba(255, 255, 255, 0.08);
|
||||||
|
--chip-text: #e6e6e6;
|
||||||
|
--spinner-border: #555555;
|
||||||
|
--spinner-top: #dddddd;
|
||||||
|
--silence-bg: #1a1a1a;
|
||||||
|
--loading-bg: rgba(255, 77, 77, 0.12);
|
||||||
|
--button-bg: #111111;
|
||||||
|
--button-border: #333333;
|
||||||
|
--wave-stroke: #e6e6e6;
|
||||||
|
--label-dia-text: #b3b3b3;
|
||||||
|
--label-trans-text: #ffffff;
|
||||||
|
}
|
||||||
|
|
||||||
|
:root[data-theme="light"] {
|
||||||
|
--bg: #ffffff;
|
||||||
|
--text: #111111;
|
||||||
|
--muted: #666666;
|
||||||
|
--border: #e5e5e5;
|
||||||
|
--chip-bg: rgba(0, 0, 0, 0.04);
|
||||||
|
--chip-text: #000000;
|
||||||
|
--spinner-border: #8d8d8d5c;
|
||||||
|
--spinner-top: #b0b0b0;
|
||||||
|
--silence-bg: #f3f3f3;
|
||||||
|
--loading-bg: rgba(255, 77, 77, 0.06);
|
||||||
|
--button-bg: #ffffff;
|
||||||
|
--button-border: #e9e9e9;
|
||||||
|
--wave-stroke: #000000;
|
||||||
|
--label-dia-text: #868686;
|
||||||
|
--label-trans-text: #111111;
|
||||||
|
}
|
||||||
|
|
||||||
|
body {
|
||||||
|
font-family: ui-sans-serif, system-ui, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
|
||||||
|
margin: 20px;
|
||||||
|
text-align: center;
|
||||||
|
background-color: var(--bg);
|
||||||
|
color: var(--text);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Record button */
|
||||||
|
#recordButton {
|
||||||
|
width: 50px;
|
||||||
|
height: 50px;
|
||||||
|
border: none;
|
||||||
|
border-radius: 50%;
|
||||||
|
background-color: var(--button-bg);
|
||||||
|
cursor: pointer;
|
||||||
|
transition: all 0.3s ease;
|
||||||
|
border: 1px solid var(--button-border);
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
position: relative;
|
||||||
|
}
|
||||||
|
|
||||||
|
#recordButton.recording {
|
||||||
|
width: 180px;
|
||||||
|
border-radius: 40px;
|
||||||
|
justify-content: flex-start;
|
||||||
|
padding-left: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
#recordButton:active {
|
||||||
|
transform: scale(0.95);
|
||||||
|
}
|
||||||
|
|
||||||
|
.shape-container {
|
||||||
|
width: 25px;
|
||||||
|
height: 25px;
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
flex-shrink: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.shape {
|
||||||
|
width: 25px;
|
||||||
|
height: 25px;
|
||||||
|
background-color: rgb(209, 61, 53);
|
||||||
|
border-radius: 50%;
|
||||||
|
transition: all 0.3s ease;
|
||||||
|
}
|
||||||
|
|
||||||
|
#recordButton:disabled .shape {
|
||||||
|
background-color: #6e6d6d;
|
||||||
|
}
|
||||||
|
|
||||||
|
#recordButton.recording .shape {
|
||||||
|
border-radius: 5px;
|
||||||
|
width: 25px;
|
||||||
|
height: 25px;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Recording elements */
|
||||||
|
.recording-info {
|
||||||
|
display: none;
|
||||||
|
align-items: center;
|
||||||
|
margin-left: 15px;
|
||||||
|
flex-grow: 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
#recordButton.recording .recording-info {
|
||||||
|
display: flex;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wave-container {
|
||||||
|
width: 60px;
|
||||||
|
height: 30px;
|
||||||
|
position: relative;
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
#waveCanvas {
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.timer {
|
||||||
|
font-size: 14px;
|
||||||
|
font-weight: 500;
|
||||||
|
color: var(--text);
|
||||||
|
margin-left: 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
#status {
|
||||||
|
margin-top: 20px;
|
||||||
|
font-size: 16px;
|
||||||
|
color: var(--text);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Settings */
|
||||||
|
.settings-container {
|
||||||
|
display: flex;
|
||||||
|
justify-content: center;
|
||||||
|
align-items: center;
|
||||||
|
gap: 15px;
|
||||||
|
margin-top: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.settings {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
align-items: flex-start;
|
||||||
|
gap: 12px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.field {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
align-items: flex-start;
|
||||||
|
gap: 3px;
|
||||||
|
}
|
||||||
|
|
||||||
|
#chunkSelector,
|
||||||
|
#websocketInput,
|
||||||
|
#themeSelector {
|
||||||
|
font-size: 16px;
|
||||||
|
padding: 5px 8px;
|
||||||
|
border-radius: 8px;
|
||||||
|
border: 1px solid var(--border);
|
||||||
|
background-color: var(--button-bg);
|
||||||
|
color: var(--text);
|
||||||
|
max-height: 34px;
|
||||||
|
}
|
||||||
|
|
||||||
|
#websocketInput {
|
||||||
|
width: 220px;
|
||||||
|
}
|
||||||
|
|
||||||
|
#chunkSelector:focus,
|
||||||
|
#websocketInput:focus,
|
||||||
|
#themeSelector:focus {
|
||||||
|
outline: none;
|
||||||
|
border-color: #007bff;
|
||||||
|
box-shadow: 0 0 0 3px rgba(0, 123, 255, 0.15);
|
||||||
|
}
|
||||||
|
|
||||||
|
label {
|
||||||
|
font-size: 13px;
|
||||||
|
color: var(--muted);
|
||||||
|
}
|
||||||
|
|
||||||
|
.ws-default {
|
||||||
|
font-size: 12px;
|
||||||
|
color: var(--muted);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Segmented pill control for Theme */
|
||||||
|
.segmented {
|
||||||
|
display: inline-flex;
|
||||||
|
align-items: stretch;
|
||||||
|
border: 1px solid var(--button-border);
|
||||||
|
background-color: var(--button-bg);
|
||||||
|
border-radius: 999px;
|
||||||
|
overflow: hidden;
|
||||||
|
}
|
||||||
|
|
||||||
|
.segmented input[type="radio"] {
|
||||||
|
position: absolute;
|
||||||
|
opacity: 0;
|
||||||
|
pointer-events: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.theme-selector-container {
|
||||||
|
position: absolute;
|
||||||
|
top: 20px;
|
||||||
|
right: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.segmented label {
|
||||||
|
display: inline-flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 6px;
|
||||||
|
padding: 6px 12px;
|
||||||
|
font-size: 14px;
|
||||||
|
color: var(--muted);
|
||||||
|
cursor: pointer;
|
||||||
|
user-select: none;
|
||||||
|
transition: background-color 0.2s ease, color 0.2s ease;
|
||||||
|
}
|
||||||
|
|
||||||
|
.segmented label span {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.segmented label:hover span {
|
||||||
|
display: inline;
|
||||||
|
}
|
||||||
|
|
||||||
|
.segmented label:hover {
|
||||||
|
background-color: var(--chip-bg);
|
||||||
|
}
|
||||||
|
|
||||||
|
.segmented img {
|
||||||
|
width: 16px;
|
||||||
|
height: 16px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.segmented input[type="radio"]:checked + label {
|
||||||
|
background-color: var(--chip-bg);
|
||||||
|
color: var(--text);
|
||||||
|
}
|
||||||
|
|
||||||
|
.segmented input[type="radio"]:focus-visible + label,
|
||||||
|
.segmented input[type="radio"]:focus + label {
|
||||||
|
outline: 2px solid #007bff;
|
||||||
|
outline-offset: 2px;
|
||||||
|
border-radius: 999px;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Transcript area */
|
||||||
|
#linesTranscript {
|
||||||
|
margin: 20px auto;
|
||||||
|
max-width: 700px;
|
||||||
|
text-align: left;
|
||||||
|
font-size: 16px;
|
||||||
|
}
|
||||||
|
|
||||||
|
#linesTranscript p {
|
||||||
|
margin: 0px 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
#linesTranscript strong {
|
||||||
|
color: var(--text);
|
||||||
|
}
|
||||||
|
|
||||||
|
#speaker {
|
||||||
|
border: 1px solid var(--border);
|
||||||
|
border-radius: 100px;
|
||||||
|
padding: 2px 10px;
|
||||||
|
font-size: 14px;
|
||||||
|
margin-bottom: 0px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.label_diarization {
|
||||||
|
background-color: var(--chip-bg);
|
||||||
|
border-radius: 8px 8px 8px 8px;
|
||||||
|
padding: 2px 10px;
|
||||||
|
margin-left: 10px;
|
||||||
|
display: inline-block;
|
||||||
|
white-space: nowrap;
|
||||||
|
font-size: 14px;
|
||||||
|
margin-bottom: 0px;
|
||||||
|
color: var(--label-dia-text);
|
||||||
|
}
|
||||||
|
|
||||||
|
.label_transcription {
|
||||||
|
background-color: var(--chip-bg);
|
||||||
|
border-radius: 8px 8px 8px 8px;
|
||||||
|
padding: 2px 10px;
|
||||||
|
display: inline-block;
|
||||||
|
white-space: nowrap;
|
||||||
|
margin-left: 10px;
|
||||||
|
font-size: 14px;
|
||||||
|
margin-bottom: 0px;
|
||||||
|
color: var(--label-trans-text);
|
||||||
|
}
|
||||||
|
|
||||||
|
#timeInfo {
|
||||||
|
color: var(--muted);
|
||||||
|
margin-left: 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.textcontent {
|
||||||
|
font-size: 16px;
|
||||||
|
padding-left: 10px;
|
||||||
|
margin-bottom: 10px;
|
||||||
|
margin-top: 1px;
|
||||||
|
padding-top: 5px;
|
||||||
|
border-radius: 0px 0px 0px 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.buffer_diarization {
|
||||||
|
color: var(--label-dia-text);
|
||||||
|
margin-left: 4px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.buffer_transcription {
|
||||||
|
color: #7474748c;
|
||||||
|
margin-left: 4px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.spinner {
|
||||||
|
display: inline-block;
|
||||||
|
width: 8px;
|
||||||
|
height: 8px;
|
||||||
|
border: 2px solid var(--spinner-border);
|
||||||
|
border-top: 2px solid var(--spinner-top);
|
||||||
|
border-radius: 50%;
|
||||||
|
animation: spin 0.7s linear infinite;
|
||||||
|
vertical-align: middle;
|
||||||
|
margin-bottom: 2px;
|
||||||
|
margin-right: 5px;
|
||||||
|
}
|
||||||
|
|
||||||
|
@keyframes spin {
|
||||||
|
to {
|
||||||
|
transform: rotate(360deg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
.silence {
|
||||||
|
color: var(--muted);
|
||||||
|
background-color: var(--silence-bg);
|
||||||
|
font-size: 13px;
|
||||||
|
border-radius: 30px;
|
||||||
|
padding: 2px 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.loading {
|
||||||
|
color: var(--muted);
|
||||||
|
background-color: var(--loading-bg);
|
||||||
|
border-radius: 8px 8px 8px 0px;
|
||||||
|
padding: 2px 10px;
|
||||||
|
font-size: 14px;
|
||||||
|
margin-bottom: 0px;
|
||||||
|
}
|
||||||
61
whisperlivekit/web/live_transcription.html
Normal file
61
whisperlivekit/web/live_transcription.html
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8" />
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
|
<title>WhisperLiveKit</title>
|
||||||
|
<link rel="stylesheet" href="/web/live_transcription.css" />
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="settings-container">
|
||||||
|
<button id="recordButton">
|
||||||
|
<div class="shape-container">
|
||||||
|
<div class="shape"></div>
|
||||||
|
</div>
|
||||||
|
<div class="recording-info">
|
||||||
|
<div class="wave-container">
|
||||||
|
<canvas id="waveCanvas"></canvas>
|
||||||
|
</div>
|
||||||
|
<div class="timer">00:00</div>
|
||||||
|
</div>
|
||||||
|
</button>
|
||||||
|
|
||||||
|
<div class="settings">
|
||||||
|
<div class="field">
|
||||||
|
<label for="websocketInput">WebSocket URL</label>
|
||||||
|
<input id="websocketInput" type="text" placeholder="ws://host:port/asr" />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="theme-selector-container">
|
||||||
|
<div class="segmented" role="radiogroup" aria-label="Theme selector">
|
||||||
|
<input type="radio" id="theme-system" name="theme" value="system" />
|
||||||
|
<label for="theme-system" title="System">
|
||||||
|
<img src="/web/src/system_mode.svg" alt="" />
|
||||||
|
<span>System</span>
|
||||||
|
</label>
|
||||||
|
|
||||||
|
<input type="radio" id="theme-light" name="theme" value="light" />
|
||||||
|
<label for="theme-light" title="Light">
|
||||||
|
<img src="/web/src/light_mode.svg" alt="" />
|
||||||
|
<span>Light</span>
|
||||||
|
</label>
|
||||||
|
|
||||||
|
<input type="radio" id="theme-dark" name="theme" value="dark" />
|
||||||
|
<label for="theme-dark" title="Dark">
|
||||||
|
<img src="/web/src/dark_mode.svg" alt="" />
|
||||||
|
<span>Dark</span>
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<p id="status"></p>
|
||||||
|
|
||||||
|
<div id="linesTranscript"></div>
|
||||||
|
|
||||||
|
<script src="/web/live_transcription.js"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
513
whisperlivekit/web/live_transcription.js
Normal file
513
whisperlivekit/web/live_transcription.js
Normal file
@@ -0,0 +1,513 @@
|
|||||||
|
/* Theme, WebSocket, recording, rendering logic extracted from inline script and adapted for segmented theme control and WS caption */
|
||||||
|
|
||||||
|
let isRecording = false;
|
||||||
|
let websocket = null;
|
||||||
|
let recorder = null;
|
||||||
|
let chunkDuration = 100;
|
||||||
|
let websocketUrl = "ws://localhost:8000/asr";
|
||||||
|
let userClosing = false;
|
||||||
|
let wakeLock = null;
|
||||||
|
let startTime = null;
|
||||||
|
let timerInterval = null;
|
||||||
|
let audioContext = null;
|
||||||
|
let analyser = null;
|
||||||
|
let microphone = null;
|
||||||
|
let waveCanvas = document.getElementById("waveCanvas");
|
||||||
|
let waveCtx = waveCanvas.getContext("2d");
|
||||||
|
let animationFrame = null;
|
||||||
|
let waitingForStop = false;
|
||||||
|
let lastReceivedData = null;
|
||||||
|
let lastSignature = null;
|
||||||
|
|
||||||
|
waveCanvas.width = 60 * (window.devicePixelRatio || 1);
|
||||||
|
waveCanvas.height = 30 * (window.devicePixelRatio || 1);
|
||||||
|
waveCtx.scale(window.devicePixelRatio || 1, window.devicePixelRatio || 1);
|
||||||
|
|
||||||
|
const statusText = document.getElementById("status");
|
||||||
|
const recordButton = document.getElementById("recordButton");
|
||||||
|
const chunkSelector = document.getElementById("chunkSelector");
|
||||||
|
const websocketInput = document.getElementById("websocketInput");
|
||||||
|
const websocketDefaultSpan = document.getElementById("wsDefaultUrl");
|
||||||
|
const linesTranscriptDiv = document.getElementById("linesTranscript");
|
||||||
|
const timerElement = document.querySelector(".timer");
|
||||||
|
const themeRadios = document.querySelectorAll('input[name="theme"]');
|
||||||
|
|
||||||
|
function getWaveStroke() {
|
||||||
|
const styles = getComputedStyle(document.documentElement);
|
||||||
|
const v = styles.getPropertyValue("--wave-stroke").trim();
|
||||||
|
return v || "#000";
|
||||||
|
}
|
||||||
|
|
||||||
|
let waveStroke = getWaveStroke();
|
||||||
|
function updateWaveStroke() {
|
||||||
|
waveStroke = getWaveStroke();
|
||||||
|
}
|
||||||
|
|
||||||
|
function applyTheme(pref) {
|
||||||
|
if (pref === "light") {
|
||||||
|
document.documentElement.setAttribute("data-theme", "light");
|
||||||
|
} else if (pref === "dark") {
|
||||||
|
document.documentElement.setAttribute("data-theme", "dark");
|
||||||
|
} else {
|
||||||
|
document.documentElement.removeAttribute("data-theme");
|
||||||
|
}
|
||||||
|
updateWaveStroke();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Persisted theme preference
|
||||||
|
const savedThemePref = localStorage.getItem("themePreference") || "system";
|
||||||
|
applyTheme(savedThemePref);
|
||||||
|
if (themeRadios.length) {
|
||||||
|
themeRadios.forEach((r) => {
|
||||||
|
r.checked = r.value === savedThemePref;
|
||||||
|
r.addEventListener("change", () => {
|
||||||
|
if (r.checked) {
|
||||||
|
localStorage.setItem("themePreference", r.value);
|
||||||
|
applyTheme(r.value);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// React to OS theme changes when in "system" mode
|
||||||
|
const darkMq = window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)");
|
||||||
|
const handleOsThemeChange = () => {
|
||||||
|
const pref = localStorage.getItem("themePreference") || "system";
|
||||||
|
if (pref === "system") updateWaveStroke();
|
||||||
|
};
|
||||||
|
if (darkMq && darkMq.addEventListener) {
|
||||||
|
darkMq.addEventListener("change", handleOsThemeChange);
|
||||||
|
} else if (darkMq && darkMq.addListener) {
|
||||||
|
// deprecated, but included for Safari compatibility
|
||||||
|
darkMq.addListener(handleOsThemeChange);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helpers
|
||||||
|
function fmt1(x) {
|
||||||
|
const n = Number(x);
|
||||||
|
return Number.isFinite(n) ? n.toFixed(1) : x;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Default WebSocket URL computation
|
||||||
|
const host = window.location.hostname || "localhost";
|
||||||
|
const port = window.location.port;
|
||||||
|
const protocol = window.location.protocol === "https:" ? "wss" : "ws";
|
||||||
|
const defaultWebSocketUrl = `${protocol}://${host}${port ? ":" + port : ""}/asr`;
|
||||||
|
|
||||||
|
// Populate default caption and input
|
||||||
|
if (websocketDefaultSpan) websocketDefaultSpan.textContent = defaultWebSocketUrl;
|
||||||
|
websocketInput.value = defaultWebSocketUrl;
|
||||||
|
websocketUrl = defaultWebSocketUrl;
|
||||||
|
|
||||||
|
// Optional chunk selector (guard for presence)
|
||||||
|
if (chunkSelector) {
|
||||||
|
chunkSelector.addEventListener("change", () => {
|
||||||
|
chunkDuration = parseInt(chunkSelector.value);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// WebSocket input change handling
|
||||||
|
websocketInput.addEventListener("change", () => {
|
||||||
|
const urlValue = websocketInput.value.trim();
|
||||||
|
if (!urlValue.startsWith("ws://") && !urlValue.startsWith("wss://")) {
|
||||||
|
statusText.textContent = "Invalid WebSocket URL (must start with ws:// or wss://)";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
websocketUrl = urlValue;
|
||||||
|
statusText.textContent = "WebSocket URL updated. Ready to connect.";
|
||||||
|
});
|
||||||
|
|
||||||
|
function setupWebSocket() {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
try {
|
||||||
|
websocket = new WebSocket(websocketUrl);
|
||||||
|
} catch (error) {
|
||||||
|
statusText.textContent = "Invalid WebSocket URL. Please check and try again.";
|
||||||
|
reject(error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
websocket.onopen = () => {
|
||||||
|
statusText.textContent = "Connected to server.";
|
||||||
|
resolve();
|
||||||
|
};
|
||||||
|
|
||||||
|
websocket.onclose = () => {
|
||||||
|
if (userClosing) {
|
||||||
|
if (waitingForStop) {
|
||||||
|
statusText.textContent = "Processing finalized or connection closed.";
|
||||||
|
if (lastReceivedData) {
|
||||||
|
renderLinesWithBuffer(
|
||||||
|
lastReceivedData.lines || [],
|
||||||
|
lastReceivedData.buffer_diarization || "",
|
||||||
|
lastReceivedData.buffer_transcription || "",
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
true
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
statusText.textContent = "Disconnected from the WebSocket server. (Check logs if model is loading.)";
|
||||||
|
if (isRecording) {
|
||||||
|
stopRecording();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
isRecording = false;
|
||||||
|
waitingForStop = false;
|
||||||
|
userClosing = false;
|
||||||
|
lastReceivedData = null;
|
||||||
|
websocket = null;
|
||||||
|
updateUI();
|
||||||
|
};
|
||||||
|
|
||||||
|
websocket.onerror = () => {
|
||||||
|
statusText.textContent = "Error connecting to WebSocket.";
|
||||||
|
reject(new Error("Error connecting to WebSocket"));
|
||||||
|
};
|
||||||
|
|
||||||
|
websocket.onmessage = (event) => {
|
||||||
|
const data = JSON.parse(event.data);
|
||||||
|
|
||||||
|
if (data.type === "ready_to_stop") {
|
||||||
|
console.log("Ready to stop received, finalizing display and closing WebSocket.");
|
||||||
|
waitingForStop = false;
|
||||||
|
|
||||||
|
if (lastReceivedData) {
|
||||||
|
renderLinesWithBuffer(
|
||||||
|
lastReceivedData.lines || [],
|
||||||
|
lastReceivedData.buffer_diarization || "",
|
||||||
|
lastReceivedData.buffer_transcription || "",
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
true
|
||||||
|
);
|
||||||
|
}
|
||||||
|
statusText.textContent = "Finished processing audio! Ready to record again.";
|
||||||
|
recordButton.disabled = false;
|
||||||
|
|
||||||
|
if (websocket) {
|
||||||
|
websocket.close();
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
lastReceivedData = data;
|
||||||
|
|
||||||
|
const {
|
||||||
|
lines = [],
|
||||||
|
buffer_transcription = "",
|
||||||
|
buffer_diarization = "",
|
||||||
|
remaining_time_transcription = 0,
|
||||||
|
remaining_time_diarization = 0,
|
||||||
|
status = "active_transcription",
|
||||||
|
} = data;
|
||||||
|
|
||||||
|
renderLinesWithBuffer(
|
||||||
|
lines,
|
||||||
|
buffer_diarization,
|
||||||
|
buffer_transcription,
|
||||||
|
remaining_time_diarization,
|
||||||
|
remaining_time_transcription,
|
||||||
|
false,
|
||||||
|
status
|
||||||
|
);
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function renderLinesWithBuffer(
|
||||||
|
lines,
|
||||||
|
buffer_diarization,
|
||||||
|
buffer_transcription,
|
||||||
|
remaining_time_diarization,
|
||||||
|
remaining_time_transcription,
|
||||||
|
isFinalizing = false,
|
||||||
|
current_status = "active_transcription"
|
||||||
|
) {
|
||||||
|
if (current_status === "no_audio_detected") {
|
||||||
|
linesTranscriptDiv.innerHTML =
|
||||||
|
"<p style='text-align: center; color: var(--muted); margin-top: 20px;'><em>No audio detected...</em></p>";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const showLoading = !isFinalizing && (lines || []).some((it) => it.speaker == 0);
|
||||||
|
const showTransLag = !isFinalizing && remaining_time_transcription > 0;
|
||||||
|
const showDiaLag = !isFinalizing && !!buffer_diarization && remaining_time_diarization > 0;
|
||||||
|
const signature = JSON.stringify({
|
||||||
|
lines: (lines || []).map((it) => ({ speaker: it.speaker, text: it.text, beg: it.beg, end: it.end })),
|
||||||
|
buffer_transcription: buffer_transcription || "",
|
||||||
|
buffer_diarization: buffer_diarization || "",
|
||||||
|
status: current_status,
|
||||||
|
showLoading,
|
||||||
|
showTransLag,
|
||||||
|
showDiaLag,
|
||||||
|
isFinalizing: !!isFinalizing,
|
||||||
|
});
|
||||||
|
if (lastSignature === signature) {
|
||||||
|
const t = document.querySelector(".lag-transcription-value");
|
||||||
|
if (t) t.textContent = fmt1(remaining_time_transcription);
|
||||||
|
const d = document.querySelector(".lag-diarization-value");
|
||||||
|
if (d) d.textContent = fmt1(remaining_time_diarization);
|
||||||
|
const ld = document.querySelector(".loading-diarization-value");
|
||||||
|
if (ld) ld.textContent = fmt1(remaining_time_diarization);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
lastSignature = signature;
|
||||||
|
|
||||||
|
const linesHtml = (lines || [])
|
||||||
|
.map((item, idx) => {
|
||||||
|
let timeInfo = "";
|
||||||
|
if (item.beg !== undefined && item.end !== undefined) {
|
||||||
|
timeInfo = ` ${item.beg} - ${item.end}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
let speakerLabel = "";
|
||||||
|
if (item.speaker === -2) {
|
||||||
|
speakerLabel = `<span class="silence">Silence<span id='timeInfo'>${timeInfo}</span></span>`;
|
||||||
|
} else if (item.speaker == 0 && !isFinalizing) {
|
||||||
|
speakerLabel = `<span class='loading'><span class="spinner"></span><span id='timeInfo'><span class="loading-diarization-value">${fmt1(
|
||||||
|
remaining_time_diarization
|
||||||
|
)}</span> second(s) of audio are undergoing diarization</span></span>`;
|
||||||
|
} else if (item.speaker !== 0) {
|
||||||
|
speakerLabel = `<span id="speaker">Speaker ${item.speaker}<span id='timeInfo'>${timeInfo}</span></span>`;
|
||||||
|
}
|
||||||
|
|
||||||
|
let currentLineText = item.text || "";
|
||||||
|
|
||||||
|
if (idx === lines.length - 1) {
|
||||||
|
if (!isFinalizing && item.speaker !== -2) {
|
||||||
|
if (remaining_time_transcription > 0) {
|
||||||
|
speakerLabel += `<span class="label_transcription"><span class="spinner"></span>Transcription lag <span id='timeInfo'><span class="lag-transcription-value">${fmt1(
|
||||||
|
remaining_time_transcription
|
||||||
|
)}</span>s</span></span>`;
|
||||||
|
}
|
||||||
|
if (buffer_diarization && remaining_time_diarization > 0) {
|
||||||
|
speakerLabel += `<span class="label_diarization"><span class="spinner"></span>Diarization lag<span id='timeInfo'><span class="lag-diarization-value">${fmt1(
|
||||||
|
remaining_time_diarization
|
||||||
|
)}</span>s</span></span>`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (buffer_diarization) {
|
||||||
|
if (isFinalizing) {
|
||||||
|
currentLineText +=
|
||||||
|
(currentLineText.length > 0 && buffer_diarization.trim().length > 0 ? " " : "") + buffer_diarization.trim();
|
||||||
|
} else {
|
||||||
|
currentLineText += `<span class="buffer_diarization">${buffer_diarization}</span>`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (buffer_transcription) {
|
||||||
|
if (isFinalizing) {
|
||||||
|
currentLineText +=
|
||||||
|
(currentLineText.length > 0 && buffer_transcription.trim().length > 0 ? " " : "") +
|
||||||
|
buffer_transcription.trim();
|
||||||
|
} else {
|
||||||
|
currentLineText += `<span class="buffer_transcription">${buffer_transcription}</span>`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return currentLineText.trim().length > 0 || speakerLabel.length > 0
|
||||||
|
? `<p>${speakerLabel}<br/><div class='textcontent'>${currentLineText}</div></p>`
|
||||||
|
: `<p>${speakerLabel}<br/></p>`;
|
||||||
|
})
|
||||||
|
.join("");
|
||||||
|
|
||||||
|
linesTranscriptDiv.innerHTML = linesHtml;
|
||||||
|
window.scrollTo({ top: document.body.scrollHeight, behavior: "smooth" });
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateTimer() {
|
||||||
|
if (!startTime) return;
|
||||||
|
|
||||||
|
const elapsed = Math.floor((Date.now() - startTime) / 1000);
|
||||||
|
const minutes = Math.floor(elapsed / 60).toString().padStart(2, "0");
|
||||||
|
const seconds = (elapsed % 60).toString().padStart(2, "0");
|
||||||
|
timerElement.textContent = `${minutes}:${seconds}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
function drawWaveform() {
|
||||||
|
if (!analyser) return;
|
||||||
|
|
||||||
|
const bufferLength = analyser.frequencyBinCount;
|
||||||
|
const dataArray = new Uint8Array(bufferLength);
|
||||||
|
analyser.getByteTimeDomainData(dataArray);
|
||||||
|
|
||||||
|
waveCtx.clearRect(
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
waveCanvas.width / (window.devicePixelRatio || 1),
|
||||||
|
waveCanvas.height / (window.devicePixelRatio || 1)
|
||||||
|
);
|
||||||
|
waveCtx.lineWidth = 1;
|
||||||
|
waveCtx.strokeStyle = waveStroke;
|
||||||
|
waveCtx.beginPath();
|
||||||
|
|
||||||
|
const sliceWidth = (waveCanvas.width / (window.devicePixelRatio || 1)) / bufferLength;
|
||||||
|
let x = 0;
|
||||||
|
|
||||||
|
for (let i = 0; i < bufferLength; i++) {
|
||||||
|
const v = dataArray[i] / 128.0;
|
||||||
|
const y = (v * (waveCanvas.height / (window.devicePixelRatio || 1))) / 2;
|
||||||
|
|
||||||
|
if (i === 0) {
|
||||||
|
waveCtx.moveTo(x, y);
|
||||||
|
} else {
|
||||||
|
waveCtx.lineTo(x, y);
|
||||||
|
}
|
||||||
|
|
||||||
|
x += sliceWidth;
|
||||||
|
}
|
||||||
|
|
||||||
|
waveCtx.lineTo(
|
||||||
|
waveCanvas.width / (window.devicePixelRatio || 1),
|
||||||
|
(waveCanvas.height / (window.devicePixelRatio || 1)) / 2
|
||||||
|
);
|
||||||
|
waveCtx.stroke();
|
||||||
|
|
||||||
|
animationFrame = requestAnimationFrame(drawWaveform);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function startRecording() {
|
||||||
|
try {
|
||||||
|
try {
|
||||||
|
wakeLock = await navigator.wakeLock.request("screen");
|
||||||
|
} catch (err) {
|
||||||
|
console.log("Error acquiring wake lock.");
|
||||||
|
}
|
||||||
|
|
||||||
|
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
||||||
|
|
||||||
|
audioContext = new (window.AudioContext || window.webkitAudioContext)();
|
||||||
|
analyser = audioContext.createAnalyser();
|
||||||
|
analyser.fftSize = 256;
|
||||||
|
microphone = audioContext.createMediaStreamSource(stream);
|
||||||
|
microphone.connect(analyser);
|
||||||
|
|
||||||
|
recorder = new MediaRecorder(stream, { mimeType: "audio/webm" });
|
||||||
|
recorder.ondataavailable = (e) => {
|
||||||
|
if (websocket && websocket.readyState === WebSocket.OPEN) {
|
||||||
|
websocket.send(e.data);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
recorder.start(chunkDuration);
|
||||||
|
|
||||||
|
startTime = Date.now();
|
||||||
|
timerInterval = setInterval(updateTimer, 1000);
|
||||||
|
drawWaveform();
|
||||||
|
|
||||||
|
isRecording = true;
|
||||||
|
updateUI();
|
||||||
|
} catch (err) {
|
||||||
|
statusText.textContent = "Error accessing microphone. Please allow microphone access.";
|
||||||
|
console.error(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function stopRecording() {
|
||||||
|
if (wakeLock) {
|
||||||
|
try {
|
||||||
|
await wakeLock.release();
|
||||||
|
} catch (e) {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
wakeLock = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
userClosing = true;
|
||||||
|
waitingForStop = true;
|
||||||
|
|
||||||
|
if (websocket && websocket.readyState === WebSocket.OPEN) {
|
||||||
|
const emptyBlob = new Blob([], { type: "audio/webm" });
|
||||||
|
websocket.send(emptyBlob);
|
||||||
|
statusText.textContent = "Recording stopped. Processing final audio...";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (recorder) {
|
||||||
|
recorder.stop();
|
||||||
|
recorder = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (microphone) {
|
||||||
|
microphone.disconnect();
|
||||||
|
microphone = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (analyser) {
|
||||||
|
analyser = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (audioContext && audioContext.state !== "closed") {
|
||||||
|
try {
|
||||||
|
await audioContext.close();
|
||||||
|
} catch (e) {
|
||||||
|
console.warn("Could not close audio context:", e);
|
||||||
|
}
|
||||||
|
audioContext = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (animationFrame) {
|
||||||
|
cancelAnimationFrame(animationFrame);
|
||||||
|
animationFrame = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (timerInterval) {
|
||||||
|
clearInterval(timerInterval);
|
||||||
|
timerInterval = null;
|
||||||
|
}
|
||||||
|
timerElement.textContent = "00:00";
|
||||||
|
startTime = null;
|
||||||
|
|
||||||
|
isRecording = false;
|
||||||
|
updateUI();
|
||||||
|
}
|
||||||
|
|
||||||
|
async function toggleRecording() {
|
||||||
|
if (!isRecording) {
|
||||||
|
if (waitingForStop) {
|
||||||
|
console.log("Waiting for stop, early return");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
console.log("Connecting to WebSocket");
|
||||||
|
try {
|
||||||
|
if (websocket && websocket.readyState === WebSocket.OPEN) {
|
||||||
|
await startRecording();
|
||||||
|
} else {
|
||||||
|
await setupWebSocket();
|
||||||
|
await startRecording();
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
statusText.textContent = "Could not connect to WebSocket or access mic. Aborted.";
|
||||||
|
console.error(err);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
console.log("Stopping recording");
|
||||||
|
stopRecording();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateUI() {
|
||||||
|
recordButton.classList.toggle("recording", isRecording);
|
||||||
|
recordButton.disabled = waitingForStop;
|
||||||
|
|
||||||
|
if (waitingForStop) {
|
||||||
|
if (statusText.textContent !== "Recording stopped. Processing final audio...") {
|
||||||
|
statusText.textContent = "Please wait for processing to complete...";
|
||||||
|
}
|
||||||
|
} else if (isRecording) {
|
||||||
|
statusText.textContent = "Recording...";
|
||||||
|
} else {
|
||||||
|
if (
|
||||||
|
statusText.textContent !== "Finished processing audio! Ready to record again." &&
|
||||||
|
statusText.textContent !== "Processing finalized or connection closed."
|
||||||
|
) {
|
||||||
|
statusText.textContent = "Click to start transcription";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!waitingForStop) {
|
||||||
|
recordButton.disabled = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
recordButton.addEventListener("click", toggleRecording);
|
||||||
1
whisperlivekit/web/src/dark_mode.svg
Normal file
1
whisperlivekit/web/src/dark_mode.svg
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<svg xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 -960 960 960" width="24px" fill="#5f6368"><path d="M480-120q-151 0-255.5-104.5T120-480q0-138 90-239.5T440-838q13-2 23 3.5t16 14.5q6 9 6.5 21t-7.5 23q-17 26-25.5 55t-8.5 61q0 90 63 153t153 63q31 0 61.5-9t54.5-25q11-7 22.5-6.5T819-479q10 5 15.5 15t3.5 24q-14 138-117.5 229T480-120Zm0-80q88 0 158-48.5T740-375q-20 5-40 8t-40 3q-123 0-209.5-86.5T364-660q0-20 3-40t8-40q-78 32-126.5 102T200-480q0 116 82 198t198 82Zm-10-270Z"/></svg>
|
||||||
|
After Width: | Height: | Size: 493 B |
1
whisperlivekit/web/src/light_mode.svg
Normal file
1
whisperlivekit/web/src/light_mode.svg
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<svg xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 -960 960 960" width="24px" fill="#5f6368"><path d="M480-360q50 0 85-35t35-85q0-50-35-85t-85-35q-50 0-85 35t-35 85q0 50 35 85t85 35Zm0 80q-83 0-141.5-58.5T280-480q0-83 58.5-141.5T480-680q83 0 141.5 58.5T680-480q0 83-58.5 141.5T480-280ZM80-440q-17 0-28.5-11.5T40-480q0-17 11.5-28.5T80-520h80q17 0 28.5 11.5T200-480q0 17-11.5 28.5T160-440H80Zm720 0q-17 0-28.5-11.5T760-480q0-17 11.5-28.5T800-520h80q17 0 28.5 11.5T920-480q0 17-11.5 28.5T880-440h-80ZM480-760q-17 0-28.5-11.5T440-800v-80q0-17 11.5-28.5T480-920q17 0 28.5 11.5T520-880v80q0 17-11.5 28.5T480-760Zm0 720q-17 0-28.5-11.5T440-80v-80q0-17 11.5-28.5T480-200q17 0 28.5 11.5T520-160v80q0 17-11.5 28.5T480-40ZM226-678l-43-42q-12-11-11.5-28t11.5-29q12-12 29-12t28 12l42 43q11 12 11 28t-11 28q-11 12-27.5 11.5T226-678Zm494 495-42-43q-11-12-11-28.5t11-27.5q11-12 27.5-11.5T734-282l43 42q12 11 11.5 28T777-183q-12 12-29 12t-28-12Zm-42-495q-12-11-11.5-27.5T678-734l42-43q11-12 28-11.5t29 11.5q12 12 12 29t-12 28l-43 42q-12 11-28 11t-28-11ZM183-183q-12-12-12-29t12-28l43-42q12-11 28.5-11t27.5 11q12 11 11.5 27.5T282-226l-42 43q-11 12-28 11.5T183-183Zm297-297Z"/></svg>
|
||||||
|
After Width: | Height: | Size: 1.2 KiB |
1
whisperlivekit/web/src/system_mode.svg
Normal file
1
whisperlivekit/web/src/system_mode.svg
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<svg xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 -960 960 960" width="24px" fill="#5f6368"><path d="M396-396q-32-32-58.5-67T289-537q-5 14-6.5 28.5T281-480q0 83 58 141t141 58q14 0 28.5-2t28.5-6q-39-22-74-48.5T396-396Zm85 196q-56 0-107-21t-91-61q-40-40-61-91t-21-107q0-51 17-97.5t50-84.5q13-14 32-9.5t27 24.5q21 55 52.5 104t73.5 91q42 42 91 73.5T648-326q20 8 24.5 27t-9.5 32q-38 33-84.5 50T481-200Zm223-192q-16-5-23-20.5t-4-32.5q9-48-6-94.5T621-621q-35-35-80.5-49.5T448-677q-17 3-32-4t-21-23q-6-16 1.5-31t23.5-19q69-15 138 4.5T679-678q51 51 71 120t5 138q-4 17-19 25t-32 3ZM480-840q-17 0-28.5-11.5T440-880v-40q0-17 11.5-28.5T480-960q17 0 28.5 11.5T520-920v40q0 17-11.5 28.5T480-840Zm0 840q-17 0-28.5-11.5T440-40v-40q0-17 11.5-28.5T480-120q17 0 28.5 11.5T520-80v40q0 17-11.5 28.5T480 0Zm255-734q-12-12-12-28.5t12-28.5l28-28q11-11 27.5-11t28.5 11q12 12 12 28.5T819-762l-28 28q-12 12-28 12t-28-12ZM141-141q-12-12-12-28.5t12-28.5l28-28q12-12 28-12t28 12q12 12 12 28.5T225-169l-28 28q-11 11-27.5 11T141-141Zm739-299q-17 0-28.5-11.5T840-480q0-17 11.5-28.5T880-520h40q17 0 28.5 11.5T960-480q0 17-11.5 28.5T920-440h-40Zm-840 0q-17 0-28.5-11.5T0-480q0-17 11.5-28.5T40-520h40q17 0 28.5 11.5T120-480q0 17-11.5 28.5T80-440H40Zm779 299q-12 12-28.5 12T762-141l-28-28q-12-12-12-28t12-28q12-12 28.5-12t28.5 12l28 28q11 11 11 27.5T819-141ZM226-735q-12 12-28.5 12T169-735l-28-28q-11-11-11-27.5t11-28.5q12-12 28.5-12t28.5 12l28 28q12 12 12 28t-12 28Zm170 339Z"/></svg>
|
||||||
|
After Width: | Height: | Size: 1.4 KiB |
33
whisperlivekit/web/web_interface.py
Normal file
33
whisperlivekit/web/web_interface.py
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
import logging
|
||||||
|
import importlib.resources as resources
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
def get_web_interface_html():
|
||||||
|
"""Loads the HTML for the web interface using importlib.resources."""
|
||||||
|
try:
|
||||||
|
with resources.files('whisperlivekit.web').joinpath('live_transcription.html').open('r', encoding='utf-8') as f:
|
||||||
|
return f.read()
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error loading web interface HTML: {e}")
|
||||||
|
return "<html><body><h1>Error loading interface</h1></body></html>"
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
|
||||||
|
from fastapi import FastAPI
|
||||||
|
from fastapi.responses import HTMLResponse
|
||||||
|
import uvicorn
|
||||||
|
from starlette.staticfiles import StaticFiles
|
||||||
|
import pathlib
|
||||||
|
import whisperlivekit.web as webpkg
|
||||||
|
|
||||||
|
app = FastAPI()
|
||||||
|
web_dir = pathlib.Path(webpkg.__file__).parent
|
||||||
|
app.mount("/web", StaticFiles(directory=str(web_dir)), name="web")
|
||||||
|
|
||||||
|
@app.get("/")
|
||||||
|
async def get():
|
||||||
|
return HTMLResponse(get_web_interface_html())
|
||||||
|
|
||||||
|
uvicorn.run(app=app)
|
||||||
0
whisperlivekit/whisper_streaming_custom/__init__.py
Normal file
0
whisperlivekit/whisper_streaming_custom/__init__.py
Normal file
290
whisperlivekit/whisper_streaming_custom/backends.py
Normal file
290
whisperlivekit/whisper_streaming_custom/backends.py
Normal file
@@ -0,0 +1,290 @@
|
|||||||
|
import sys
|
||||||
|
import logging
|
||||||
|
import io
|
||||||
|
import soundfile as sf
|
||||||
|
import math
|
||||||
|
from typing import List
|
||||||
|
import numpy as np
|
||||||
|
from whisperlivekit.timed_objects import ASRToken
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
class ASRBase:
|
||||||
|
sep = " " # join transcribe words with this character (" " for whisper_timestamped,
|
||||||
|
# "" for faster-whisper because it emits the spaces when needed)
|
||||||
|
|
||||||
|
def __init__(self, lan, modelsize=None, cache_dir=None, model_dir=None, logfile=sys.stderr):
|
||||||
|
self.logfile = logfile
|
||||||
|
self.transcribe_kargs = {}
|
||||||
|
if lan == "auto":
|
||||||
|
self.original_language = None
|
||||||
|
else:
|
||||||
|
self.original_language = lan
|
||||||
|
self.model = self.load_model(modelsize, cache_dir, model_dir)
|
||||||
|
|
||||||
|
def with_offset(self, offset: float) -> ASRToken:
|
||||||
|
# This method is kept for compatibility (typically you will use ASRToken.with_offset)
|
||||||
|
return ASRToken(self.start + offset, self.end + offset, self.text)
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f"ASRToken(start={self.start:.2f}, end={self.end:.2f}, text={self.text!r})"
|
||||||
|
|
||||||
|
def load_model(self, modelsize, cache_dir, model_dir):
|
||||||
|
raise NotImplementedError("must be implemented in the child class")
|
||||||
|
|
||||||
|
def transcribe(self, audio, init_prompt=""):
|
||||||
|
raise NotImplementedError("must be implemented in the child class")
|
||||||
|
|
||||||
|
def use_vad(self):
|
||||||
|
raise NotImplementedError("must be implemented in the child class")
|
||||||
|
|
||||||
|
|
||||||
|
class WhisperTimestampedASR(ASRBase):
|
||||||
|
"""Uses whisper_timestamped as the backend."""
|
||||||
|
sep = " "
|
||||||
|
|
||||||
|
def load_model(self, modelsize=None, cache_dir=None, model_dir=None):
|
||||||
|
import whisper
|
||||||
|
import whisper_timestamped
|
||||||
|
from whisper_timestamped import transcribe_timestamped
|
||||||
|
|
||||||
|
self.transcribe_timestamped = transcribe_timestamped
|
||||||
|
if model_dir is not None:
|
||||||
|
logger.debug("ignoring model_dir, not implemented")
|
||||||
|
return whisper.load_model(modelsize, download_root=cache_dir)
|
||||||
|
|
||||||
|
def transcribe(self, audio, init_prompt=""):
|
||||||
|
result = self.transcribe_timestamped(
|
||||||
|
self.model,
|
||||||
|
audio,
|
||||||
|
language=self.original_language,
|
||||||
|
initial_prompt=init_prompt,
|
||||||
|
verbose=None,
|
||||||
|
condition_on_previous_text=True,
|
||||||
|
**self.transcribe_kargs,
|
||||||
|
)
|
||||||
|
return result
|
||||||
|
|
||||||
|
def ts_words(self, r) -> List[ASRToken]:
|
||||||
|
"""
|
||||||
|
Converts the whisper_timestamped result to a list of ASRToken objects.
|
||||||
|
"""
|
||||||
|
tokens = []
|
||||||
|
for segment in r["segments"]:
|
||||||
|
for word in segment["words"]:
|
||||||
|
token = ASRToken(word["start"], word["end"], word["text"])
|
||||||
|
tokens.append(token)
|
||||||
|
return tokens
|
||||||
|
|
||||||
|
def segments_end_ts(self, res) -> List[float]:
|
||||||
|
return [segment["end"] for segment in res["segments"]]
|
||||||
|
|
||||||
|
def use_vad(self):
|
||||||
|
self.transcribe_kargs["vad"] = True
|
||||||
|
|
||||||
|
def set_translate_task(self):
|
||||||
|
self.transcribe_kargs["task"] = "translate"
|
||||||
|
|
||||||
|
|
||||||
|
class FasterWhisperASR(ASRBase):
|
||||||
|
"""Uses faster-whisper as the backend."""
|
||||||
|
sep = ""
|
||||||
|
|
||||||
|
def load_model(self, modelsize=None, cache_dir=None, model_dir=None):
|
||||||
|
from faster_whisper import WhisperModel
|
||||||
|
|
||||||
|
if model_dir is not None:
|
||||||
|
logger.debug(f"Loading whisper model from model_dir {model_dir}. "
|
||||||
|
f"modelsize and cache_dir parameters are not used.")
|
||||||
|
model_size_or_path = model_dir
|
||||||
|
elif modelsize is not None:
|
||||||
|
model_size_or_path = modelsize
|
||||||
|
else:
|
||||||
|
raise ValueError("Either modelsize or model_dir must be set")
|
||||||
|
device = "auto" # Allow CTranslate2 to decide available device
|
||||||
|
compute_type = "auto" # Allow CTranslate2 to decide faster compute type
|
||||||
|
|
||||||
|
|
||||||
|
model = WhisperModel(
|
||||||
|
model_size_or_path,
|
||||||
|
device=device,
|
||||||
|
compute_type=compute_type,
|
||||||
|
download_root=cache_dir,
|
||||||
|
)
|
||||||
|
return model
|
||||||
|
|
||||||
|
def transcribe(self, audio: np.ndarray, init_prompt: str = "") -> list:
|
||||||
|
segments, info = self.model.transcribe(
|
||||||
|
audio,
|
||||||
|
language=self.original_language,
|
||||||
|
initial_prompt=init_prompt,
|
||||||
|
beam_size=5,
|
||||||
|
word_timestamps=True,
|
||||||
|
condition_on_previous_text=True,
|
||||||
|
**self.transcribe_kargs,
|
||||||
|
)
|
||||||
|
return list(segments)
|
||||||
|
|
||||||
|
def ts_words(self, segments) -> List[ASRToken]:
|
||||||
|
tokens = []
|
||||||
|
for segment in segments:
|
||||||
|
if segment.no_speech_prob > 0.9:
|
||||||
|
continue
|
||||||
|
for word in segment.words:
|
||||||
|
token = ASRToken(word.start, word.end, word.word, probability=word.probability)
|
||||||
|
tokens.append(token)
|
||||||
|
return tokens
|
||||||
|
|
||||||
|
def segments_end_ts(self, segments) -> List[float]:
|
||||||
|
return [segment.end for segment in segments]
|
||||||
|
|
||||||
|
def use_vad(self):
|
||||||
|
self.transcribe_kargs["vad_filter"] = True
|
||||||
|
|
||||||
|
def set_translate_task(self):
|
||||||
|
self.transcribe_kargs["task"] = "translate"
|
||||||
|
|
||||||
|
|
||||||
|
class MLXWhisper(ASRBase):
|
||||||
|
"""
|
||||||
|
Uses MLX Whisper optimized for Apple Silicon.
|
||||||
|
"""
|
||||||
|
sep = ""
|
||||||
|
|
||||||
|
def load_model(self, modelsize=None, cache_dir=None, model_dir=None):
|
||||||
|
from mlx_whisper.transcribe import ModelHolder, transcribe
|
||||||
|
import mlx.core as mx
|
||||||
|
|
||||||
|
if model_dir is not None:
|
||||||
|
logger.debug(f"Loading whisper model from model_dir {model_dir}. modelsize parameter is not used.")
|
||||||
|
model_size_or_path = model_dir
|
||||||
|
elif modelsize is not None:
|
||||||
|
model_size_or_path = self.translate_model_name(modelsize)
|
||||||
|
logger.debug(f"Loading whisper model {modelsize}. You use mlx whisper, so {model_size_or_path} will be used.")
|
||||||
|
else:
|
||||||
|
raise ValueError("Either modelsize or model_dir must be set")
|
||||||
|
|
||||||
|
self.model_size_or_path = model_size_or_path
|
||||||
|
dtype = mx.float16
|
||||||
|
ModelHolder.get_model(model_size_or_path, dtype)
|
||||||
|
return transcribe
|
||||||
|
|
||||||
|
def translate_model_name(self, model_name):
|
||||||
|
model_mapping = {
|
||||||
|
"tiny.en": "mlx-community/whisper-tiny.en-mlx",
|
||||||
|
"tiny": "mlx-community/whisper-tiny-mlx",
|
||||||
|
"base.en": "mlx-community/whisper-base.en-mlx",
|
||||||
|
"base": "mlx-community/whisper-base-mlx",
|
||||||
|
"small.en": "mlx-community/whisper-small.en-mlx",
|
||||||
|
"small": "mlx-community/whisper-small-mlx",
|
||||||
|
"medium.en": "mlx-community/whisper-medium.en-mlx",
|
||||||
|
"medium": "mlx-community/whisper-medium-mlx",
|
||||||
|
"large-v1": "mlx-community/whisper-large-v1-mlx",
|
||||||
|
"large-v2": "mlx-community/whisper-large-v2-mlx",
|
||||||
|
"large-v3": "mlx-community/whisper-large-v3-mlx",
|
||||||
|
"large-v3-turbo": "mlx-community/whisper-large-v3-turbo",
|
||||||
|
"large": "mlx-community/whisper-large-mlx",
|
||||||
|
}
|
||||||
|
mlx_model_path = model_mapping.get(model_name)
|
||||||
|
if mlx_model_path:
|
||||||
|
return mlx_model_path
|
||||||
|
else:
|
||||||
|
raise ValueError(f"Model name '{model_name}' is not recognized or not supported.")
|
||||||
|
|
||||||
|
def transcribe(self, audio, init_prompt=""):
|
||||||
|
if self.transcribe_kargs:
|
||||||
|
logger.warning("Transcribe kwargs (vad, task) are not compatible with MLX Whisper and will be ignored.")
|
||||||
|
segments = self.model(
|
||||||
|
audio,
|
||||||
|
language=self.original_language,
|
||||||
|
initial_prompt=init_prompt,
|
||||||
|
word_timestamps=True,
|
||||||
|
condition_on_previous_text=True,
|
||||||
|
path_or_hf_repo=self.model_size_or_path,
|
||||||
|
)
|
||||||
|
return segments.get("segments", [])
|
||||||
|
|
||||||
|
def ts_words(self, segments) -> List[ASRToken]:
|
||||||
|
tokens = []
|
||||||
|
for segment in segments:
|
||||||
|
if segment.get("no_speech_prob", 0) > 0.9:
|
||||||
|
continue
|
||||||
|
for word in segment.get("words", []):
|
||||||
|
token = ASRToken(word["start"], word["end"], word["word"], probability=word["probability"])
|
||||||
|
tokens.append(token)
|
||||||
|
return tokens
|
||||||
|
|
||||||
|
def segments_end_ts(self, res) -> List[float]:
|
||||||
|
return [s["end"] for s in res]
|
||||||
|
|
||||||
|
def use_vad(self):
|
||||||
|
self.transcribe_kargs["vad_filter"] = True
|
||||||
|
|
||||||
|
def set_translate_task(self):
|
||||||
|
self.transcribe_kargs["task"] = "translate"
|
||||||
|
|
||||||
|
|
||||||
|
class OpenaiApiASR(ASRBase):
|
||||||
|
"""Uses OpenAI's Whisper API for transcription."""
|
||||||
|
def __init__(self, lan=None, temperature=0, logfile=sys.stderr):
|
||||||
|
self.logfile = logfile
|
||||||
|
self.modelname = "whisper-1"
|
||||||
|
self.original_language = None if lan == "auto" else lan
|
||||||
|
self.response_format = "verbose_json"
|
||||||
|
self.temperature = temperature
|
||||||
|
self.load_model()
|
||||||
|
self.use_vad_opt = False
|
||||||
|
self.task = "transcribe"
|
||||||
|
|
||||||
|
def load_model(self, *args, **kwargs):
|
||||||
|
from openai import OpenAI
|
||||||
|
self.client = OpenAI()
|
||||||
|
self.transcribed_seconds = 0
|
||||||
|
|
||||||
|
def ts_words(self, segments) -> List[ASRToken]:
|
||||||
|
"""
|
||||||
|
Converts OpenAI API response words into ASRToken objects while
|
||||||
|
optionally skipping words that fall into no-speech segments.
|
||||||
|
"""
|
||||||
|
no_speech_segments = []
|
||||||
|
if self.use_vad_opt:
|
||||||
|
for segment in segments.segments:
|
||||||
|
if segment.no_speech_prob > 0.8:
|
||||||
|
no_speech_segments.append((segment.start, segment.end))
|
||||||
|
tokens = []
|
||||||
|
for word in segments.words:
|
||||||
|
start = word.start
|
||||||
|
end = word.end
|
||||||
|
if any(s[0] <= start <= s[1] for s in no_speech_segments):
|
||||||
|
continue
|
||||||
|
tokens.append(ASRToken(start, end, word.word))
|
||||||
|
return tokens
|
||||||
|
|
||||||
|
def segments_end_ts(self, res) -> List[float]:
|
||||||
|
return [s.end for s in res.words]
|
||||||
|
|
||||||
|
def transcribe(self, audio_data, prompt=None, *args, **kwargs):
|
||||||
|
buffer = io.BytesIO()
|
||||||
|
buffer.name = "temp.wav"
|
||||||
|
sf.write(buffer, audio_data, samplerate=16000, format="WAV", subtype="PCM_16")
|
||||||
|
buffer.seek(0)
|
||||||
|
self.transcribed_seconds += math.ceil(len(audio_data) / 16000)
|
||||||
|
params = {
|
||||||
|
"model": self.modelname,
|
||||||
|
"file": buffer,
|
||||||
|
"response_format": self.response_format,
|
||||||
|
"temperature": self.temperature,
|
||||||
|
"timestamp_granularities": ["word", "segment"],
|
||||||
|
}
|
||||||
|
if self.task != "translate" and self.original_language:
|
||||||
|
params["language"] = self.original_language
|
||||||
|
if prompt:
|
||||||
|
params["prompt"] = prompt
|
||||||
|
proc = self.client.audio.translations if self.task == "translate" else self.client.audio.transcriptions
|
||||||
|
transcript = proc.create(**params)
|
||||||
|
logger.debug(f"OpenAI API processed accumulated {self.transcribed_seconds} seconds")
|
||||||
|
return transcript
|
||||||
|
|
||||||
|
def use_vad(self):
|
||||||
|
self.use_vad_opt = True
|
||||||
|
|
||||||
|
def set_translate_task(self):
|
||||||
|
self.task = "translate"
|
||||||
412
whisperlivekit/whisper_streaming_custom/online_asr.py
Normal file
412
whisperlivekit/whisper_streaming_custom/online_asr.py
Normal file
@@ -0,0 +1,412 @@
|
|||||||
|
import sys
|
||||||
|
import numpy as np
|
||||||
|
import logging
|
||||||
|
from typing import List, Tuple, Optional
|
||||||
|
from whisperlivekit.timed_objects import ASRToken, Sentence, Transcript
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
class HypothesisBuffer:
|
||||||
|
"""
|
||||||
|
Buffer to store and process ASR hypothesis tokens.
|
||||||
|
|
||||||
|
It holds:
|
||||||
|
- committed_in_buffer: tokens that have been confirmed (committed)
|
||||||
|
- buffer: the last hypothesis that is not yet committed
|
||||||
|
- new: new tokens coming from the recognizer
|
||||||
|
"""
|
||||||
|
def __init__(self, logfile=sys.stderr, confidence_validation=False):
|
||||||
|
self.confidence_validation = confidence_validation
|
||||||
|
self.committed_in_buffer: List[ASRToken] = []
|
||||||
|
self.buffer: List[ASRToken] = []
|
||||||
|
self.new: List[ASRToken] = []
|
||||||
|
self.last_committed_time = 0.0
|
||||||
|
self.last_committed_word: Optional[str] = None
|
||||||
|
self.logfile = logfile
|
||||||
|
|
||||||
|
def insert(self, new_tokens: List[ASRToken], offset: float):
|
||||||
|
"""
|
||||||
|
Insert new tokens (after applying a time offset) and compare them with the
|
||||||
|
already committed tokens. Only tokens that extend the committed hypothesis
|
||||||
|
are added.
|
||||||
|
"""
|
||||||
|
# Apply the offset to each token.
|
||||||
|
new_tokens = [token.with_offset(offset) for token in new_tokens]
|
||||||
|
# Only keep tokens that are roughly “new”
|
||||||
|
self.new = [token for token in new_tokens if token.start > self.last_committed_time - 0.1]
|
||||||
|
|
||||||
|
if self.new:
|
||||||
|
first_token = self.new[0]
|
||||||
|
if abs(first_token.start - self.last_committed_time) < 1:
|
||||||
|
if self.committed_in_buffer:
|
||||||
|
committed_len = len(self.committed_in_buffer)
|
||||||
|
new_len = len(self.new)
|
||||||
|
# Try to match 1 to 5 consecutive tokens
|
||||||
|
max_ngram = min(min(committed_len, new_len), 5)
|
||||||
|
for i in range(1, max_ngram + 1):
|
||||||
|
committed_ngram = " ".join(token.text for token in self.committed_in_buffer[-i:])
|
||||||
|
new_ngram = " ".join(token.text for token in self.new[:i])
|
||||||
|
if committed_ngram == new_ngram:
|
||||||
|
removed = []
|
||||||
|
for _ in range(i):
|
||||||
|
removed_token = self.new.pop(0)
|
||||||
|
removed.append(repr(removed_token))
|
||||||
|
logger.debug(f"Removing last {i} words: {' '.join(removed)}")
|
||||||
|
break
|
||||||
|
|
||||||
|
def flush(self) -> List[ASRToken]:
|
||||||
|
"""
|
||||||
|
Returns the committed chunk, defined as the longest common prefix
|
||||||
|
between the previous hypothesis and the new tokens.
|
||||||
|
"""
|
||||||
|
committed: List[ASRToken] = []
|
||||||
|
while self.new:
|
||||||
|
current_new = self.new[0]
|
||||||
|
if self.confidence_validation and current_new.probability and current_new.probability > 0.95:
|
||||||
|
committed.append(current_new)
|
||||||
|
self.last_committed_word = current_new.text
|
||||||
|
self.last_committed_time = current_new.end
|
||||||
|
self.new.pop(0)
|
||||||
|
self.buffer.pop(0) if self.buffer else None
|
||||||
|
elif not self.buffer:
|
||||||
|
break
|
||||||
|
elif current_new.text == self.buffer[0].text:
|
||||||
|
committed.append(current_new)
|
||||||
|
self.last_committed_word = current_new.text
|
||||||
|
self.last_committed_time = current_new.end
|
||||||
|
self.buffer.pop(0)
|
||||||
|
self.new.pop(0)
|
||||||
|
else:
|
||||||
|
break
|
||||||
|
self.buffer = self.new
|
||||||
|
self.new = []
|
||||||
|
self.committed_in_buffer.extend(committed)
|
||||||
|
return committed
|
||||||
|
|
||||||
|
def pop_committed(self, time: float):
|
||||||
|
"""
|
||||||
|
Remove tokens (from the beginning) that have ended before `time`.
|
||||||
|
"""
|
||||||
|
while self.committed_in_buffer and self.committed_in_buffer[0].end <= time:
|
||||||
|
self.committed_in_buffer.pop(0)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class OnlineASRProcessor:
|
||||||
|
"""
|
||||||
|
Processes incoming audio in a streaming fashion, calling the ASR system
|
||||||
|
periodically, and uses a hypothesis buffer to commit and trim recognized text.
|
||||||
|
|
||||||
|
The processor supports two types of buffer trimming:
|
||||||
|
- "sentence": trims at sentence boundaries (using a sentence tokenizer)
|
||||||
|
- "segment": trims at fixed segment durations.
|
||||||
|
"""
|
||||||
|
SAMPLING_RATE = 16000
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
asr,
|
||||||
|
tokenize_method: Optional[callable] = None,
|
||||||
|
buffer_trimming: Tuple[str, float] = ("segment", 15),
|
||||||
|
confidence_validation = False,
|
||||||
|
logfile=sys.stderr,
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
asr: An ASR system object (for example, a WhisperASR instance) that
|
||||||
|
provides a `transcribe` method, a `ts_words` method (to extract tokens),
|
||||||
|
a `segments_end_ts` method, and a separator attribute `sep`.
|
||||||
|
tokenize_method: A function that receives text and returns a list of sentence strings.
|
||||||
|
buffer_trimming: A tuple (option, seconds), where option is either "sentence" or "segment".
|
||||||
|
"""
|
||||||
|
self.asr = asr
|
||||||
|
self.tokenize = tokenize_method
|
||||||
|
self.logfile = logfile
|
||||||
|
self.confidence_validation = confidence_validation
|
||||||
|
self.global_time_offset = 0.0
|
||||||
|
self.init()
|
||||||
|
|
||||||
|
self.buffer_trimming_way, self.buffer_trimming_sec = buffer_trimming
|
||||||
|
|
||||||
|
if self.buffer_trimming_way not in ["sentence", "segment"]:
|
||||||
|
raise ValueError("buffer_trimming must be either 'sentence' or 'segment'")
|
||||||
|
if self.buffer_trimming_sec <= 0:
|
||||||
|
raise ValueError("buffer_trimming_sec must be positive")
|
||||||
|
elif self.buffer_trimming_sec > 30:
|
||||||
|
logger.warning(
|
||||||
|
f"buffer_trimming_sec is set to {self.buffer_trimming_sec}, which is very long. It may cause OOM."
|
||||||
|
)
|
||||||
|
|
||||||
|
def init(self, offset: Optional[float] = None):
|
||||||
|
"""Initialize or reset the processing buffers."""
|
||||||
|
self.audio_buffer = np.array([], dtype=np.float32)
|
||||||
|
self.transcript_buffer = HypothesisBuffer(logfile=self.logfile, confidence_validation=self.confidence_validation)
|
||||||
|
self.buffer_time_offset = offset if offset is not None else 0.0
|
||||||
|
self.transcript_buffer.last_committed_time = self.buffer_time_offset
|
||||||
|
self.committed: List[ASRToken] = []
|
||||||
|
self.time_of_last_asr_output = 0.0
|
||||||
|
|
||||||
|
def get_audio_buffer_end_time(self) -> float:
|
||||||
|
"""Returns the absolute end time of the current audio_buffer."""
|
||||||
|
return self.buffer_time_offset + (len(self.audio_buffer) / self.SAMPLING_RATE)
|
||||||
|
|
||||||
|
def insert_audio_chunk(self, audio: np.ndarray, audio_stream_end_time: Optional[float] = None):
|
||||||
|
"""Append an audio chunk (a numpy array) to the current audio buffer."""
|
||||||
|
self.audio_buffer = np.append(self.audio_buffer, audio)
|
||||||
|
|
||||||
|
def insert_silence(self, silence_duration, offset):
|
||||||
|
"""
|
||||||
|
If silences are > 5s, we do a complete context clear. Otherwise, we just insert a small silence and shift the last_attend_frame
|
||||||
|
"""
|
||||||
|
# if self.transcript_buffer.buffer:
|
||||||
|
# self.committed.extend(self.transcript_buffer.buffer)
|
||||||
|
# self.transcript_buffer.buffer = []
|
||||||
|
|
||||||
|
if True: #silence_duration < 3: #we want the last audio to be treated to not have a gap. could also be handled in the future in ends_with_silence.
|
||||||
|
gap_silence = np.zeros(int(16000 * silence_duration), dtype=np.int16)
|
||||||
|
self.insert_audio_chunk(gap_silence)
|
||||||
|
else:
|
||||||
|
self.init(offset=silence_duration + offset)
|
||||||
|
self.global_time_offset += silence_duration
|
||||||
|
|
||||||
|
def prompt(self) -> Tuple[str, str]:
|
||||||
|
"""
|
||||||
|
Returns a tuple: (prompt, context), where:
|
||||||
|
- prompt is a 200-character suffix of committed text that falls
|
||||||
|
outside the current audio buffer.
|
||||||
|
- context is the committed text within the current audio buffer.
|
||||||
|
"""
|
||||||
|
k = len(self.committed)
|
||||||
|
while k > 0 and self.committed[k - 1].end > self.buffer_time_offset:
|
||||||
|
k -= 1
|
||||||
|
|
||||||
|
prompt_tokens = self.committed[:k]
|
||||||
|
prompt_words = [token.text for token in prompt_tokens]
|
||||||
|
prompt_list = []
|
||||||
|
length_count = 0
|
||||||
|
# Use the last words until reaching 200 characters.
|
||||||
|
while prompt_words and length_count < 200:
|
||||||
|
word = prompt_words.pop(-1)
|
||||||
|
length_count += len(word) + 1
|
||||||
|
prompt_list.append(word)
|
||||||
|
non_prompt_tokens = self.committed[k:]
|
||||||
|
context_text = self.asr.sep.join(token.text for token in non_prompt_tokens)
|
||||||
|
return self.asr.sep.join(prompt_list[::-1]), context_text
|
||||||
|
|
||||||
|
def get_buffer(self):
|
||||||
|
"""
|
||||||
|
Get the unvalidated buffer in string format.
|
||||||
|
"""
|
||||||
|
return self.concatenate_tokens(self.transcript_buffer.buffer)
|
||||||
|
|
||||||
|
|
||||||
|
def process_iter(self) -> Tuple[List[ASRToken], float]:
|
||||||
|
"""
|
||||||
|
Processes the current audio buffer.
|
||||||
|
|
||||||
|
Returns a tuple: (list of committed ASRToken objects, float representing the audio processed up to time).
|
||||||
|
"""
|
||||||
|
current_audio_processed_upto = self.get_audio_buffer_end_time()
|
||||||
|
prompt_text, _ = self.prompt()
|
||||||
|
logger.debug(
|
||||||
|
f"Transcribing {len(self.audio_buffer)/self.SAMPLING_RATE:.2f} seconds from {self.buffer_time_offset:.2f}"
|
||||||
|
)
|
||||||
|
res = self.asr.transcribe(self.audio_buffer, init_prompt=prompt_text)
|
||||||
|
tokens = self.asr.ts_words(res)
|
||||||
|
self.transcript_buffer.insert(tokens, self.buffer_time_offset)
|
||||||
|
committed_tokens = self.transcript_buffer.flush()
|
||||||
|
self.committed.extend(committed_tokens)
|
||||||
|
|
||||||
|
if committed_tokens:
|
||||||
|
self.time_of_last_asr_output = self.committed[-1].end
|
||||||
|
|
||||||
|
completed = self.concatenate_tokens(committed_tokens)
|
||||||
|
logger.debug(f">>>> COMPLETE NOW: {completed.text}")
|
||||||
|
incomp = self.concatenate_tokens(self.transcript_buffer.buffer)
|
||||||
|
logger.debug(f"INCOMPLETE: {incomp.text}")
|
||||||
|
|
||||||
|
buffer_duration = len(self.audio_buffer) / self.SAMPLING_RATE
|
||||||
|
if not committed_tokens and buffer_duration > self.buffer_trimming_sec:
|
||||||
|
time_since_last_output = self.get_audio_buffer_end_time() - self.time_of_last_asr_output
|
||||||
|
if time_since_last_output > self.buffer_trimming_sec:
|
||||||
|
logger.warning(
|
||||||
|
f"No ASR output for {time_since_last_output:.2f}s. "
|
||||||
|
f"Resetting buffer to prevent freezing."
|
||||||
|
)
|
||||||
|
self.init(offset=self.get_audio_buffer_end_time())
|
||||||
|
return [], current_audio_processed_upto
|
||||||
|
|
||||||
|
if committed_tokens and self.buffer_trimming_way == "sentence":
|
||||||
|
if len(self.audio_buffer) / self.SAMPLING_RATE > self.buffer_trimming_sec:
|
||||||
|
self.chunk_completed_sentence()
|
||||||
|
|
||||||
|
s = self.buffer_trimming_sec if self.buffer_trimming_way == "segment" else 30
|
||||||
|
if len(self.audio_buffer) / self.SAMPLING_RATE > s:
|
||||||
|
self.chunk_completed_segment(res)
|
||||||
|
logger.debug("Chunking segment")
|
||||||
|
logger.debug(
|
||||||
|
f"Length of audio buffer now: {len(self.audio_buffer)/self.SAMPLING_RATE:.2f} seconds"
|
||||||
|
)
|
||||||
|
if self.global_time_offset:
|
||||||
|
for token in committed_tokens:
|
||||||
|
token = token.with_offset(self.global_time_offset)
|
||||||
|
return committed_tokens, current_audio_processed_upto
|
||||||
|
|
||||||
|
def chunk_completed_sentence(self):
|
||||||
|
"""
|
||||||
|
If the committed tokens form at least two sentences, chunk the audio
|
||||||
|
buffer at the end time of the penultimate sentence.
|
||||||
|
Also ensures chunking happens if audio buffer exceeds a time limit.
|
||||||
|
"""
|
||||||
|
buffer_duration = len(self.audio_buffer) / self.SAMPLING_RATE
|
||||||
|
if not self.committed:
|
||||||
|
if buffer_duration > self.buffer_trimming_sec:
|
||||||
|
chunk_time = self.buffer_time_offset + (buffer_duration / 2)
|
||||||
|
logger.debug(f"--- No speech detected, forced chunking at {chunk_time:.2f}")
|
||||||
|
self.chunk_at(chunk_time)
|
||||||
|
return
|
||||||
|
|
||||||
|
logger.debug("COMPLETED SENTENCE: " + " ".join(token.text for token in self.committed))
|
||||||
|
sentences = self.words_to_sentences(self.committed)
|
||||||
|
for sentence in sentences:
|
||||||
|
logger.debug(f"\tSentence: {sentence.text}")
|
||||||
|
|
||||||
|
chunk_done = False
|
||||||
|
if len(sentences) >= 2:
|
||||||
|
while len(sentences) > 2:
|
||||||
|
sentences.pop(0)
|
||||||
|
chunk_time = sentences[-2].end
|
||||||
|
logger.debug(f"--- Sentence chunked at {chunk_time:.2f}")
|
||||||
|
self.chunk_at(chunk_time)
|
||||||
|
chunk_done = True
|
||||||
|
|
||||||
|
if not chunk_done and buffer_duration > self.buffer_trimming_sec:
|
||||||
|
last_committed_time = self.committed[-1].end
|
||||||
|
logger.debug(f"--- Not enough sentences, chunking at last committed time {last_committed_time:.2f}")
|
||||||
|
self.chunk_at(last_committed_time)
|
||||||
|
|
||||||
|
def chunk_completed_segment(self, res):
|
||||||
|
"""
|
||||||
|
Chunk the audio buffer based on segment-end timestamps reported by the ASR.
|
||||||
|
Also ensures chunking happens if audio buffer exceeds a time limit.
|
||||||
|
"""
|
||||||
|
buffer_duration = len(self.audio_buffer) / self.SAMPLING_RATE
|
||||||
|
if not self.committed:
|
||||||
|
if buffer_duration > self.buffer_trimming_sec:
|
||||||
|
chunk_time = self.buffer_time_offset + (buffer_duration / 2)
|
||||||
|
logger.debug(f"--- No speech detected, forced chunking at {chunk_time:.2f}")
|
||||||
|
self.chunk_at(chunk_time)
|
||||||
|
return
|
||||||
|
|
||||||
|
logger.debug("Processing committed tokens for segmenting")
|
||||||
|
ends = self.asr.segments_end_ts(res)
|
||||||
|
last_committed_time = self.committed[-1].end
|
||||||
|
chunk_done = False
|
||||||
|
if len(ends) > 1:
|
||||||
|
logger.debug("Multiple segments available for chunking")
|
||||||
|
e = ends[-2] + self.buffer_time_offset
|
||||||
|
while len(ends) > 2 and e > last_committed_time:
|
||||||
|
ends.pop(-1)
|
||||||
|
e = ends[-2] + self.buffer_time_offset
|
||||||
|
if e <= last_committed_time:
|
||||||
|
logger.debug(f"--- Segment chunked at {e:.2f}")
|
||||||
|
self.chunk_at(e)
|
||||||
|
chunk_done = True
|
||||||
|
else:
|
||||||
|
logger.debug("--- Last segment not within committed area")
|
||||||
|
else:
|
||||||
|
logger.debug("--- Not enough segments to chunk")
|
||||||
|
|
||||||
|
if not chunk_done and buffer_duration > self.buffer_trimming_sec:
|
||||||
|
logger.debug(f"--- Buffer too large, chunking at last committed time {last_committed_time:.2f}")
|
||||||
|
self.chunk_at(last_committed_time)
|
||||||
|
|
||||||
|
logger.debug("Segment chunking complete")
|
||||||
|
|
||||||
|
def chunk_at(self, time: float):
|
||||||
|
"""
|
||||||
|
Trim both the hypothesis and audio buffer at the given time.
|
||||||
|
"""
|
||||||
|
logger.debug(f"Chunking at {time:.2f}s")
|
||||||
|
logger.debug(
|
||||||
|
f"Audio buffer length before chunking: {len(self.audio_buffer)/self.SAMPLING_RATE:.2f}s"
|
||||||
|
)
|
||||||
|
self.transcript_buffer.pop_committed(time)
|
||||||
|
cut_seconds = time - self.buffer_time_offset
|
||||||
|
self.audio_buffer = self.audio_buffer[int(cut_seconds * self.SAMPLING_RATE):]
|
||||||
|
self.buffer_time_offset = time
|
||||||
|
logger.debug(
|
||||||
|
f"Audio buffer length after chunking: {len(self.audio_buffer)/self.SAMPLING_RATE:.2f}s"
|
||||||
|
)
|
||||||
|
|
||||||
|
def words_to_sentences(self, tokens: List[ASRToken]) -> List[Sentence]:
|
||||||
|
"""
|
||||||
|
Converts a list of tokens to a list of Sentence objects using the provided
|
||||||
|
sentence tokenizer.
|
||||||
|
"""
|
||||||
|
if not tokens:
|
||||||
|
return []
|
||||||
|
|
||||||
|
full_text = " ".join(token.text for token in tokens)
|
||||||
|
|
||||||
|
if self.tokenize:
|
||||||
|
try:
|
||||||
|
sentence_texts = self.tokenize(full_text)
|
||||||
|
except Exception as e:
|
||||||
|
# Some tokenizers (e.g., MosesSentenceSplitter) expect a list input.
|
||||||
|
try:
|
||||||
|
sentence_texts = self.tokenize([full_text])
|
||||||
|
except Exception as e2:
|
||||||
|
raise ValueError("Tokenization failed") from e2
|
||||||
|
else:
|
||||||
|
sentence_texts = [full_text]
|
||||||
|
|
||||||
|
sentences: List[Sentence] = []
|
||||||
|
token_index = 0
|
||||||
|
for sent_text in sentence_texts:
|
||||||
|
sent_text = sent_text.strip()
|
||||||
|
if not sent_text:
|
||||||
|
continue
|
||||||
|
sent_tokens = []
|
||||||
|
accumulated = ""
|
||||||
|
# Accumulate tokens until roughly matching the length of the sentence text.
|
||||||
|
while token_index < len(tokens) and len(accumulated) < len(sent_text):
|
||||||
|
token = tokens[token_index]
|
||||||
|
accumulated = (accumulated + " " + token.text).strip() if accumulated else token.text
|
||||||
|
sent_tokens.append(token)
|
||||||
|
token_index += 1
|
||||||
|
if sent_tokens:
|
||||||
|
sentence = Sentence(
|
||||||
|
start=sent_tokens[0].start,
|
||||||
|
end=sent_tokens[-1].end,
|
||||||
|
text=" ".join(t.text for t in sent_tokens),
|
||||||
|
)
|
||||||
|
sentences.append(sentence)
|
||||||
|
return sentences
|
||||||
|
|
||||||
|
def finish(self) -> Tuple[List[ASRToken], float]:
|
||||||
|
"""
|
||||||
|
Flush the remaining transcript when processing ends.
|
||||||
|
Returns a tuple: (list of remaining ASRToken objects, float representing the final audio processed up to time).
|
||||||
|
"""
|
||||||
|
remaining_tokens = self.transcript_buffer.buffer
|
||||||
|
logger.debug(f"Final non-committed tokens: {remaining_tokens}")
|
||||||
|
final_processed_upto = self.buffer_time_offset + (len(self.audio_buffer) / self.SAMPLING_RATE)
|
||||||
|
self.buffer_time_offset = final_processed_upto
|
||||||
|
return remaining_tokens, final_processed_upto
|
||||||
|
|
||||||
|
def concatenate_tokens(
|
||||||
|
self,
|
||||||
|
tokens: List[ASRToken],
|
||||||
|
sep: Optional[str] = None,
|
||||||
|
offset: float = 0
|
||||||
|
) -> Transcript:
|
||||||
|
sep = sep if sep is not None else self.asr.sep
|
||||||
|
text = sep.join(token.text for token in tokens)
|
||||||
|
probability = sum(token.probability for token in tokens if token.probability) / len(tokens) if tokens else None
|
||||||
|
if tokens:
|
||||||
|
start = offset + tokens[0].start
|
||||||
|
end = offset + tokens[-1].end
|
||||||
|
else:
|
||||||
|
start = None
|
||||||
|
end = None
|
||||||
|
return Transcript(start, end, text, probability=probability)
|
||||||
110
whisperlivekit/whisper_streaming_custom/whisper_online.py
Normal file
110
whisperlivekit/whisper_streaming_custom/whisper_online.py
Normal file
@@ -0,0 +1,110 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
import sys
|
||||||
|
import numpy as np
|
||||||
|
import librosa
|
||||||
|
from functools import lru_cache
|
||||||
|
import time
|
||||||
|
import logging
|
||||||
|
from .backends import FasterWhisperASR, MLXWhisper, WhisperTimestampedASR, OpenaiApiASR
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
WHISPER_LANG_CODES = "af,am,ar,as,az,ba,be,bg,bn,bo,br,bs,ca,cs,cy,da,de,el,en,es,et,eu,fa,fi,fo,fr,gl,gu,ha,haw,he,hi,hr,ht,hu,hy,id,is,it,ja,jw,ka,kk,km,kn,ko,la,lb,ln,lo,lt,lv,mg,mi,mk,ml,mn,mr,ms,mt,my,ne,nl,nn,no,oc,pa,pl,ps,pt,ro,ru,sa,sd,si,sk,sl,sn,so,sq,sr,su,sv,sw,ta,te,tg,th,tk,tl,tr,tt,uk,ur,uz,vi,yi,yo,zh".split(
|
||||||
|
","
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def create_tokenizer(lan):
|
||||||
|
"""returns an object that has split function that works like the one of MosesTokenizer"""
|
||||||
|
|
||||||
|
assert (
|
||||||
|
lan in WHISPER_LANG_CODES
|
||||||
|
), "language must be Whisper's supported lang code: " + " ".join(WHISPER_LANG_CODES)
|
||||||
|
|
||||||
|
if lan == "uk":
|
||||||
|
import tokenize_uk
|
||||||
|
|
||||||
|
class UkrainianTokenizer:
|
||||||
|
def split(self, text):
|
||||||
|
return tokenize_uk.tokenize_sents(text)
|
||||||
|
|
||||||
|
return UkrainianTokenizer()
|
||||||
|
|
||||||
|
# supported by fast-mosestokenizer
|
||||||
|
if (
|
||||||
|
lan
|
||||||
|
in "as bn ca cs de el en es et fi fr ga gu hi hu is it kn lt lv ml mni mr nl or pa pl pt ro ru sk sl sv ta te yue zh".split()
|
||||||
|
):
|
||||||
|
from mosestokenizer import MosesSentenceSplitter
|
||||||
|
|
||||||
|
return MosesSentenceSplitter(lan)
|
||||||
|
|
||||||
|
# the following languages are in Whisper, but not in wtpsplit:
|
||||||
|
if (
|
||||||
|
lan
|
||||||
|
in "as ba bo br bs fo haw hr ht jw lb ln lo mi nn oc sa sd sn so su sw tk tl tt".split()
|
||||||
|
):
|
||||||
|
logger.debug(
|
||||||
|
f"{lan} code is not supported by wtpsplit. Going to use None lang_code option."
|
||||||
|
)
|
||||||
|
lan = None
|
||||||
|
|
||||||
|
from wtpsplit import WtP
|
||||||
|
|
||||||
|
# downloads the model from huggingface on the first use
|
||||||
|
wtp = WtP("wtp-canine-s-12l-no-adapters")
|
||||||
|
|
||||||
|
class WtPtok:
|
||||||
|
def split(self, sent):
|
||||||
|
return wtp.split(sent, lang_code=lan)
|
||||||
|
|
||||||
|
return WtPtok()
|
||||||
|
|
||||||
|
|
||||||
|
def backend_factory(args):
|
||||||
|
backend = args.backend
|
||||||
|
if backend == "openai-api":
|
||||||
|
logger.debug("Using OpenAI API.")
|
||||||
|
asr = OpenaiApiASR(lan=args.lan)
|
||||||
|
else:
|
||||||
|
if backend == "faster-whisper":
|
||||||
|
asr_cls = FasterWhisperASR
|
||||||
|
elif backend == "mlx-whisper":
|
||||||
|
asr_cls = MLXWhisper
|
||||||
|
else:
|
||||||
|
asr_cls = WhisperTimestampedASR
|
||||||
|
|
||||||
|
# Only for FasterWhisperASR and WhisperTimestampedASR
|
||||||
|
size = args.model
|
||||||
|
t = time.time()
|
||||||
|
logger.info(f"Loading Whisper {size} model for language {args.lan}...")
|
||||||
|
asr = asr_cls(
|
||||||
|
modelsize=size,
|
||||||
|
lan=args.lan,
|
||||||
|
cache_dir=getattr(args, 'model_cache_dir', None),
|
||||||
|
model_dir=getattr(args, 'model_dir', None),
|
||||||
|
)
|
||||||
|
e = time.time()
|
||||||
|
logger.info(f"done. It took {round(e-t,2)} seconds.")
|
||||||
|
|
||||||
|
# Apply common configurations
|
||||||
|
if getattr(args, "vad", False): # Checks if VAD argument is present and True
|
||||||
|
logger.info("Setting VAD filter")
|
||||||
|
asr.use_vad()
|
||||||
|
|
||||||
|
language = args.lan
|
||||||
|
if args.task == "translate":
|
||||||
|
if backend != "simulstreaming":
|
||||||
|
asr.set_translate_task()
|
||||||
|
tgt_language = "en" # Whisper translates into English
|
||||||
|
else:
|
||||||
|
tgt_language = language # Whisper transcribes in this language
|
||||||
|
|
||||||
|
# Create the tokenizer
|
||||||
|
if args.buffer_trimming == "sentence":
|
||||||
|
tokenizer = create_tokenizer(tgt_language)
|
||||||
|
else:
|
||||||
|
tokenizer = None
|
||||||
|
return asr, tokenizer
|
||||||
Reference in New Issue
Block a user