mirror of
https://github.com/QuentinFuxa/WhisperLiveKit.git
synced 2026-03-21 16:40:35 +00:00
Compare commits
20 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3bd2122eb4 | ||
|
|
50b0527858 | ||
|
|
b044fcdec2 | ||
|
|
b0508fcf2c | ||
|
|
ce89b0aebc | ||
|
|
d5008ed828 | ||
|
|
d467716e26 | ||
|
|
199e21b3ef | ||
|
|
1d926f2e67 | ||
|
|
4a71a391b8 | ||
|
|
d3ed4e46e2 | ||
|
|
057a1026d7 | ||
|
|
1ba171a58d | ||
|
|
1adac67155 | ||
|
|
42be1a3773 | ||
|
|
0a49fafa0d | ||
|
|
4a5d5e1f3b | ||
|
|
583a2ec2e4 | ||
|
|
19765e89e9 | ||
|
|
9895bc83bf |
70
DEV_NOTES.md
Normal file
70
DEV_NOTES.md
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
# 1. Simulstreaming: Decouple the encoder for faster inference
|
||||||
|
|
||||||
|
Simulstreaming encoder time (whisperlivekit/simul_whisper/simul_whisper.py l. 397) experimentations :
|
||||||
|
|
||||||
|
On macOS Apple Silicon M4 :
|
||||||
|
|
||||||
|
| Encoder | base.en | small |
|
||||||
|
|--------|---------|-------|
|
||||||
|
| WHISPER (no modification) | 0.35s | 1.09s |
|
||||||
|
| FASTER_WHISPER | 0.4s | 1.20s |
|
||||||
|
| MLX_WHISPER | 0.07s | 0.20s |
|
||||||
|
|
||||||
|
Memory saved by only loading encoder for optimized framework:
|
||||||
|
|
||||||
|
For tiny.en, mlx whisper:
|
||||||
|
Sizes MLX whisper:
|
||||||
|
Decoder weights: 59110771 bytes
|
||||||
|
Encoder weights: 15268874 bytes
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# 2. SortFormer Diarization: 4-to-2 Speaker Constraint Algorithm
|
||||||
|
|
||||||
|
Transform a diarization model that predicts up to 4 speakers into one that predicts up to 2 speakers by mapping the output predictions.
|
||||||
|
|
||||||
|
## Problem Statement
|
||||||
|
- Input: `self.total_preds` with shape `(x, x, 4)` - predictions for 4 speakers
|
||||||
|
- Output: Constrained predictions with shape `(x, x, 2)` - predictions for 2 speakers
|
||||||
|
|
||||||
|
#
|
||||||
|
### Initial Setup
|
||||||
|
For each time step `i`, we have a ranking of 4 speaker predictions (1-4). When only 2 speakers are present, the model will have close predictions for the 2 active speaker positions.
|
||||||
|
|
||||||
|
Instead of `np.argmax(preds_np, axis=1)`, we take the top 2 predictions and build a dynamic 4→2 mapping that can evolve over time.
|
||||||
|
|
||||||
|
### Algorithm
|
||||||
|
|
||||||
|
```python
|
||||||
|
top_2_speakers = np.argsort(preds_np, axis=1)[:, -2:]
|
||||||
|
```
|
||||||
|
|
||||||
|
- `DS_a_{i}`: Top detected speaker for prediction i
|
||||||
|
- `DS_b_{i}`: Second detected speaker for prediction i
|
||||||
|
- `AS_{i}`: Attributed speaker for prediction i
|
||||||
|
- `GTS_A`: Ground truth speaker A
|
||||||
|
- `GTS_B`: Ground truth speaker B
|
||||||
|
- `DIST(a, b)`: Distance between detected speakers a and b
|
||||||
|
|
||||||
|
3. **Attribution Logic**
|
||||||
|
|
||||||
|
```
|
||||||
|
AS_0 ← A
|
||||||
|
|
||||||
|
AS_1 ← B
|
||||||
|
|
||||||
|
IF DIST(DS_a_0, DS_a_1) < DIST(DS_a_0, DS_a_2) AND
|
||||||
|
DIST(DS_a_0, DS_a_1) < DIST(DS_a_1, DS_a_2):
|
||||||
|
# Likely that DS_a_0 = DS_a_1 (same speaker)
|
||||||
|
AS_1 ← A
|
||||||
|
AS_2 ← B
|
||||||
|
|
||||||
|
ELIF DIST(DS_a_0, DS_a_2) < DIST(DS_a_0, DS_a_1) AND
|
||||||
|
DIST(DS_a_0, DS_a_2) < DIST(DS_a_1, DS_a_2):
|
||||||
|
AS_2 ← A
|
||||||
|
|
||||||
|
ELSE:
|
||||||
|
AS_2 ← B
|
||||||
|
|
||||||
|
to finish
|
||||||
|
```
|
||||||
31
Dockerfile
31
Dockerfile
@@ -17,18 +17,26 @@ RUN apt-get update && \
|
|||||||
ffmpeg \
|
ffmpeg \
|
||||||
git \
|
git \
|
||||||
build-essential \
|
build-essential \
|
||||||
python3-dev && \
|
python3-dev \
|
||||||
|
ca-certificates && \
|
||||||
rm -rf /var/lib/apt/lists/*
|
rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
RUN python3 -m venv /opt/venv
|
RUN python3 -m venv /opt/venv
|
||||||
ENV PATH="/opt/venv/bin:$PATH"
|
ENV PATH="/opt/venv/bin:$PATH"
|
||||||
RUN pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu129
|
|
||||||
|
# timeout/retries for large torch wheels
|
||||||
|
RUN pip3 install --upgrade pip setuptools wheel && \
|
||||||
|
pip3 --disable-pip-version-check install --timeout=120 --retries=5 \
|
||||||
|
--index-url https://download.pytorch.org/whl/cu129 \
|
||||||
|
torch torchaudio \
|
||||||
|
|| (echo "Initial install failed — retrying with extended timeout..." && \
|
||||||
|
pip3 --disable-pip-version-check install --timeout=300 --retries=3 \
|
||||||
|
--index-url https://download.pytorch.org/whl/cu129 \
|
||||||
|
torch torchvision torchaudio)
|
||||||
|
|
||||||
COPY . .
|
COPY . .
|
||||||
|
|
||||||
# Install WhisperLiveKit directly, allowing for optional dependencies
|
# 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 \
|
RUN if [ -n "$EXTRAS" ]; then \
|
||||||
echo "Installing with extras: [$EXTRAS]"; \
|
echo "Installing with extras: [$EXTRAS]"; \
|
||||||
pip install --no-cache-dir whisperlivekit[$EXTRAS]; \
|
pip install --no-cache-dir whisperlivekit[$EXTRAS]; \
|
||||||
@@ -37,16 +45,14 @@ RUN if [ -n "$EXTRAS" ]; then \
|
|||||||
pip install --no-cache-dir whisperlivekit; \
|
pip install --no-cache-dir whisperlivekit; \
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Enable in-container caching for Hugging Face models by:
|
# 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.
|
# A) Make the cache directory persistent via an anonymous volume.
|
||||||
# Note: This only persists for a single, named container. This is
|
# Note: This only persists for a single, named container. This is
|
||||||
# only for convenience at de/test stage.
|
# only for convenience at de/test stage.
|
||||||
# For prod, it is better to use a named volume via host mount/k8s.
|
# For prod, it is better to use a named volume via host mount/k8s.
|
||||||
VOLUME ["/root/.cache/huggingface/hub"]
|
VOLUME ["/root/.cache/huggingface/hub"]
|
||||||
|
|
||||||
|
|
||||||
# or
|
# or
|
||||||
# B) Conditionally copy a local pre-cache from the build context to the
|
# B) Conditionally copy a local pre-cache from the build context to the
|
||||||
# container's cache via the HF_PRECACHE_DIR build-arg.
|
# container's cache via the HF_PRECACHE_DIR build-arg.
|
||||||
@@ -61,8 +67,7 @@ RUN if [ -n "$HF_PRECACHE_DIR" ]; then \
|
|||||||
echo "No local Hugging Face cache specified, skipping copy"; \
|
echo "No local Hugging Face cache specified, skipping copy"; \
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Conditionally copy a Hugging Face token if provided
|
# Conditionally copy a Hugging Face token if provided. Useful for Diart backend (pyannote audio models)
|
||||||
|
|
||||||
RUN if [ -n "$HF_TKN_FILE" ]; then \
|
RUN if [ -n "$HF_TKN_FILE" ]; then \
|
||||||
echo "Copying Hugging Face token from $HF_TKN_FILE"; \
|
echo "Copying Hugging Face token from $HF_TKN_FILE"; \
|
||||||
mkdir -p /root/.cache/huggingface && \
|
mkdir -p /root/.cache/huggingface && \
|
||||||
@@ -70,11 +75,9 @@ RUN if [ -n "$HF_TKN_FILE" ]; then \
|
|||||||
else \
|
else \
|
||||||
echo "No Hugging Face token file specified, skipping token setup"; \
|
echo "No Hugging Face token file specified, skipping token setup"; \
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Expose port for the transcription server
|
|
||||||
EXPOSE 8000
|
EXPOSE 8000
|
||||||
|
|
||||||
ENTRYPOINT ["whisperlivekit-server", "--host", "0.0.0.0"]
|
ENTRYPOINT ["whisperlivekit-server", "--host", "0.0.0.0"]
|
||||||
|
|
||||||
# Default args
|
CMD ["--model", "medium"]
|
||||||
CMD ["--model", "medium"]
|
|
||||||
|
|||||||
60
README.md
60
README.md
@@ -9,7 +9,7 @@
|
|||||||
<p align="center">
|
<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://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=installations"></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=installations"></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://pypi.org/project/whisperlivekit/"><img alt="Python Versions" src="https://img.shields.io/badge/python-3.9--3.15-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>
|
<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>
|
</p>
|
||||||
|
|
||||||
@@ -66,30 +66,20 @@ pip install whisperlivekit
|
|||||||
|
|
||||||
| Optional | `pip install` |
|
| Optional | `pip install` |
|
||||||
|-----------|-------------|
|
|-----------|-------------|
|
||||||
| Speaker diarization with Sortformer | `git+https://github.com/NVIDIA/NeMo.git@main#egg=nemo_toolkit[asr]` |
|
| **Speaker diarization with Sortformer** | `git+https://github.com/NVIDIA/NeMo.git@main#egg=nemo_toolkit[asr]` |
|
||||||
| Speaker diarization with Diart | `diart` |
|
| **Apple Silicon optimized backend** | `mlx-whisper` |
|
||||||
| Original Whisper backend | `whisper` |
|
| *[Not recommanded]* Speaker diarization with Diart | `diart` |
|
||||||
| Improved timestamps backend | `whisper-timestamped` |
|
| *[Not recommanded]* Original Whisper backend | `whisper` |
|
||||||
| Apple Silicon optimization backend | `mlx-whisper` |
|
| *[Not recommanded]* Improved timestamps backend | `whisper-timestamped` |
|
||||||
| OpenAI API backend | `openai` |
|
| OpenAI API backend | `openai` |
|
||||||
|
|
||||||
See **Parameters & Configuration** below on how to use them.
|
See **Parameters & Configuration** below on how to use them.
|
||||||
|
|
||||||
|
|
||||||
> **Pyannote Models Setup** For diarization, you need access to pyannote.audio models:
|
|
||||||
> 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
|
|
||||||
> ```
|
|
||||||
|
|
||||||
## 💻 Usage Examples
|
|
||||||
|
|
||||||
#### Command-line Interface
|
### Usage Examples
|
||||||
|
|
||||||
Start the transcription server with various options:
|
**Command-line Interface**: Start the transcription server with various options:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
# Use better model than default (small)
|
# Use better model than default (small)
|
||||||
@@ -100,8 +90,7 @@ whisperlivekit-server --host 0.0.0.0 --port 8000 --model medium --diarization --
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
#### Python API Integration (Backend)
|
**Python API Integration**: 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.
|
||||||
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.
|
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from whisperlivekit import TranscriptionEngine, AudioProcessor, parse_args
|
from whisperlivekit import TranscriptionEngine, AudioProcessor, parse_args
|
||||||
@@ -139,18 +128,17 @@ async def websocket_endpoint(websocket: WebSocket):
|
|||||||
await audio_processor.process_audio(message)
|
await audio_processor.process_audio(message)
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Frontend Implementation
|
**Frontend Implementation**: 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_inline_ui_html` & `page = get_inline_ui_html()`
|
||||||
|
|
||||||
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()`
|
|
||||||
|
|
||||||
|
|
||||||
### ⚙️ Parameters & Configuration
|
## Parameters & Configuration
|
||||||
|
|
||||||
An important list of parameters can be changed. But what *should* you change?
|
An important list of parameters can be changed. But what *should* you change?
|
||||||
- the `--model` size. List and recommandations [here](https://github.com/QuentinFuxa/WhisperLiveKit/blob/main/available_models.md)
|
- the `--model` size. List and recommandations [here](https://github.com/QuentinFuxa/WhisperLiveKit/blob/main/available_models.md)
|
||||||
- the `--language`. List [here](https://github.com/QuentinFuxa/WhisperLiveKit/blob/main/whisperlivekit/simul_whisper/whisper/tokenizer.py)
|
- the `--language`. List [here](https://github.com/QuentinFuxa/WhisperLiveKit/blob/main/whisperlivekit/simul_whisper/whisper/tokenizer.py). If you use `auto`, the model attempts to detect the language automatically, but it tends to bias towards English.
|
||||||
- the `--backend` ? you can switch to `--backend faster-whisper` if `simulstreaming` does not work correctly or if you prefer to avoid the dual-license requirements.
|
- the `--backend` ? you can switch to `--backend faster-whisper` if `simulstreaming` does not work correctly or if you prefer to avoid the dual-license requirements.
|
||||||
- `--warmup-file`, if you have one
|
- `--warmup-file`, if you have one
|
||||||
|
- `--task translate`, to translate in english
|
||||||
- `--host`, `--port`, `--ssl-certfile`, `--ssl-keyfile`, if you set up a server
|
- `--host`, `--port`, `--ssl-certfile`, `--ssl-keyfile`, if you set up a server
|
||||||
- `--diarization`, if you want to use it.
|
- `--diarization`, if you want to use it.
|
||||||
|
|
||||||
@@ -159,7 +147,7 @@ The rest I don't recommend. But below are your options.
|
|||||||
| Parameter | Description | Default |
|
| Parameter | Description | Default |
|
||||||
|-----------|-------------|---------|
|
|-----------|-------------|---------|
|
||||||
| `--model` | Whisper model size. | `small` |
|
| `--model` | Whisper model size. | `small` |
|
||||||
| `--language` | Source language code or `auto` | `en` |
|
| `--language` | Source language code or `auto` | `auto` |
|
||||||
| `--task` | `transcribe` or `translate` | `transcribe` |
|
| `--task` | `transcribe` or `translate` | `transcribe` |
|
||||||
| `--backend` | Processing backend | `simulstreaming` |
|
| `--backend` | Processing backend | `simulstreaming` |
|
||||||
| `--min-chunk-size` | Minimum audio chunk size (seconds) | `1.0` |
|
| `--min-chunk-size` | Minimum audio chunk size (seconds) | `1.0` |
|
||||||
@@ -172,14 +160,9 @@ The rest I don't recommend. But below are your options.
|
|||||||
| `--ssl-keyfile` | Path to the SSL private key 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 |
|
| SimulStreaming backend options | Description | Default |
|
||||||
|-----------|-------------|---------|
|
|-----------|-------------|---------|
|
||||||
|
| `--disable-fast-encoder` | Disable Faster Whisper or MLX Whisper backends for the encoder (if installed). Inference can be slower but helpful when GPU memory is limited | `False` |
|
||||||
| `--frame-threshold` | AlignAtt frame threshold (lower = faster, higher = more accurate) | `25` |
|
| `--frame-threshold` | AlignAtt frame threshold (lower = faster, higher = more accurate) | `25` |
|
||||||
| `--beams` | Number of beams for beam search (1 = greedy decoding) | `1` |
|
| `--beams` | Number of beams for beam search (1 = greedy decoding) | `1` |
|
||||||
| `--decoder` | Force decoder type (`beam` or `greedy`) | `auto` |
|
| `--decoder` | Force decoder type (`beam` or `greedy`) | `auto` |
|
||||||
@@ -193,6 +176,12 @@ The rest I don't recommend. But below are your options.
|
|||||||
| `--model-path` | Direct path to .pt model file. Download it if not found | `./base.pt` |
|
| `--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` |
|
| `--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` |
|
||||||
|
|
||||||
|
|
||||||
|
| WhisperStreaming backend options | Description | Default |
|
||||||
|
|-----------|-------------|---------|
|
||||||
|
| `--confidence-validation` | Use confidence scores for faster validation | `False` |
|
||||||
|
| `--buffer_trimming` | Buffer trimming strategy (`sentence` or `segment`) | `segment` |
|
||||||
|
|
||||||
| Diarization options | Description | Default |
|
| Diarization options | Description | Default |
|
||||||
|-----------|-------------|---------|
|
|-----------|-------------|---------|
|
||||||
| `--diarization` | Enable speaker identification | `False` |
|
| `--diarization` | Enable speaker identification | `False` |
|
||||||
@@ -200,6 +189,13 @@ The rest I don't recommend. But below are your options.
|
|||||||
| `--segmentation-model` | Hugging Face model ID for Diart segmentation model. [Available models](https://github.com/juanmc2005/diart/tree/main?tab=readme-ov-file#pre-trained-models) | `pyannote/segmentation-3.0` |
|
| `--segmentation-model` | Hugging Face model ID for Diart 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 Diart embedding model. [Available models](https://github.com/juanmc2005/diart/tree/main?tab=readme-ov-file#pre-trained-models) | `speechbrain/spkrec-ecapa-voxceleb` |
|
| `--embedding-model` | Hugging Face model ID for Diart embedding model. [Available models](https://github.com/juanmc2005/diart/tree/main?tab=readme-ov-file#pre-trained-models) | `speechbrain/spkrec-ecapa-voxceleb` |
|
||||||
|
|
||||||
|
|
||||||
|
> For diarization using Diart, you need access to pyannote.audio models:
|
||||||
|
> 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: `huggingface-cli login`
|
||||||
|
|
||||||
### 🚀 Deployment Guide
|
### 🚀 Deployment Guide
|
||||||
|
|
||||||
To deploy WhisperLiveKit in production:
|
To deploy WhisperLiveKit in production:
|
||||||
|
|||||||
258
ReadmeJP.md
Normal file
258
ReadmeJP.md
Normal file
@@ -0,0 +1,258 @@
|
|||||||
|
<h1 align="center">WhisperLiveKit</h1>
|
||||||
|
|
||||||
|
<p align="center">
|
||||||
|
<img src="https://raw.githubusercontent.com/QuentinFuxa/WhisperLiveKit/refs/heads/main/demo.png" alt="WhisperLiveKit Demo" width="730">
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<p align="center"><b>話者識別機能付き、リアルタイム、完全ローカルな音声テキスト変換</b></p>
|
||||||
|
|
||||||
|
<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=installations"></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>
|
||||||
|
|
||||||
|
すぐに使えるバックエンド+サーバーとシンプルなフロントエンドで、リアルタイムの音声文字起こしをブラウザに直接提供します。✨
|
||||||
|
|
||||||
|
#### 主要な研究による技術:
|
||||||
|
|
||||||
|
- [SimulStreaming](https://github.com/ufal/SimulStreaming) (SOTA 2025) - AlignAttポリシーによる超低遅延文字起こし
|
||||||
|
- [WhisperStreaming](https://github.com/ufal/whisper_streaming) (SOTA 2023) - LocalAgreementポリシーによる低遅延文字起こし
|
||||||
|
- [Streaming Sortformer](https://arxiv.org/abs/2507.18446) (SOTA 2025) - 高度なリアルタイム話者ダイアライゼーション
|
||||||
|
- [Diart](https://github.com/juanmc2005/diart) (SOTA 2021) - リアルタイム話者ダイアライゼーション
|
||||||
|
- [Silero VAD](https://github.com/snakers4/silero-vad) (2024) - エンタープライズグレードの音声区間検出
|
||||||
|
|
||||||
|
> **なぜ各音声バッチで単純なWhisperモデルを実行しないのか?** Whisperは完全な発話向けに設計されており、リアルタイムのチャンク向けではありません。小さなセグメントを処理するとコンテキストが失われ、単語が音節の途中で途切れ、質の悪い文字起こしになります。WhisperLiveKitは、インテリジェントなバッファリングとインクリメンタルな処理のために、最先端の同時音声研究を利用しています。
|
||||||
|
|
||||||
|
### アーキテクチャ
|
||||||
|
|
||||||
|
<img alt="Architecture" src="https://raw.githubusercontent.com/QuentinFuxa/WhisperLiveKit/refs/heads/main/architecture.png" />
|
||||||
|
|
||||||
|
*バックエンドは複数の同時ユーザーをサポートします。音声が検出されない場合、音声区間検出がオーバーヘッドを削減します。*
|
||||||
|
|
||||||
|
### インストールとクイックスタート
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pip install whisperlivekit
|
||||||
|
```
|
||||||
|
|
||||||
|
> **FFmpegが必要です** WhisperLiveKitを使用する前にインストールする必要があります。
|
||||||
|
>
|
||||||
|
> | OS | インストール方法 |
|
||||||
|
> |-----------|-------------|
|
||||||
|
> | Ubuntu/Debian | `sudo apt install ffmpeg` |
|
||||||
|
> | MacOS | `brew install ffmpeg` |
|
||||||
|
> | Windows | https://ffmpeg.org/download.html から.exeをダウンロードし、PATHに追加 |
|
||||||
|
|
||||||
|
#### クイックスタート
|
||||||
|
1. **文字起こしサーバーを起動します:**
|
||||||
|
```bash
|
||||||
|
whisperlivekit-server --model base --language en
|
||||||
|
```
|
||||||
|
|
||||||
|
2. **ブラウザを開き** `http://localhost:8000` にアクセスします。話し始めると、あなたの言葉がリアルタイムで表示されます!
|
||||||
|
|
||||||
|
|
||||||
|
> - 利用可能なすべての言語のリストについては、[tokenizer.py](https://github.com/QuentinFuxa/WhisperLiveKit/blob/main/whisperlivekit/simul_whisper/whisper/tokenizer.py) を参照してください。
|
||||||
|
> - HTTPSの要件については、**パラメータ**セクションのSSL設定オプションを参照してください。
|
||||||
|
|
||||||
|
#### オプションの依存関係
|
||||||
|
|
||||||
|
| オプション | `pip install` |
|
||||||
|
|-----------|-------------|
|
||||||
|
| **Sortformerによる話者ダイアライゼーション** | `git+https://github.com/NVIDIA/NeMo.git@main#egg=nemo_toolkit[asr]` |
|
||||||
|
| Diartによる話者ダイアライゼーション | `diart` |
|
||||||
|
| オリジナルのWhisperバックエンド | `whisper` |
|
||||||
|
| タイムスタンプ改善バックエンド | `whisper-timestamped` |
|
||||||
|
| Apple Silicon最適化バックエンド | `mlx-whisper` |
|
||||||
|
| OpenAI APIバックエンド | `openai` |
|
||||||
|
|
||||||
|
それらの使用方法については、以下の**パラメータと設定**を参照してください。
|
||||||
|
|
||||||
|
### 使用例
|
||||||
|
|
||||||
|
**コマンドラインインターフェース**: 様々なオプションで文字起こしサーバーを起動します:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# デフォルト(small)より良いモデルを使用
|
||||||
|
whisperlivekit-server --model large-v3
|
||||||
|
|
||||||
|
# ダイアライゼーションと言語を指定した高度な設定
|
||||||
|
whisperlivekit-server --host 0.0.0.0 --port 8000 --model medium --diarization --language fr
|
||||||
|
```
|
||||||
|
|
||||||
|
**Python API連携**: 関数やクラスの使用方法のより完全な例については、[basic_server](https://github.com/QuentinFuxa/WhisperLiveKit/blob/main/whisperlivekit/basic_server.py) を確認してください。
|
||||||
|
|
||||||
|
```python
|
||||||
|
from whisperlivekit import TranscriptionEngine, AudioProcessor, parse_args
|
||||||
|
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
|
||||||
|
from fastapi.responses import HTMLResponse
|
||||||
|
from contextlib import asynccontextmanager
|
||||||
|
import asyncio
|
||||||
|
|
||||||
|
transcription_engine = None
|
||||||
|
|
||||||
|
@asynccontextmanager
|
||||||
|
async def lifespan(app: FastAPI):
|
||||||
|
global transcription_engine
|
||||||
|
transcription_engine = TranscriptionEngine(model="medium", diarization=True, lan="en")
|
||||||
|
yield
|
||||||
|
|
||||||
|
app = FastAPI(lifespan=lifespan)
|
||||||
|
|
||||||
|
async def handle_websocket_results(websocket: WebSocket, results_generator):
|
||||||
|
async for response in results_generator:
|
||||||
|
await websocket.send_json(response)
|
||||||
|
await websocket.send_json({"type": "ready_to_stop"})
|
||||||
|
|
||||||
|
@app.websocket("/asr")
|
||||||
|
async def websocket_endpoint(websocket: WebSocket):
|
||||||
|
global transcription_engine
|
||||||
|
|
||||||
|
# 接続ごとに新しいAudioProcessorを作成し、共有エンジンを渡す
|
||||||
|
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)
|
||||||
|
```
|
||||||
|
|
||||||
|
**フロントエンド実装**: パッケージにはHTML/JavaScript実装が[ここ](https://github.com/QuentinFuxa/WhisperLiveKit/blob/main/whisperlivekit/web/live_transcription.html)に含まれています。`from whisperlivekit import get_web_interface_html` & `page = get_web_interface_html()` を使ってインポートすることもできます。
|
||||||
|
|
||||||
|
|
||||||
|
## パラメータと設定
|
||||||
|
|
||||||
|
重要なパラメータのリストを変更できます。しかし、何を*変更すべき*でしょうか?
|
||||||
|
- `--model` サイズ。リストと推奨事項は[こちら](https://github.com/QuentinFuxa/WhisperLiveKit/blob/main/available_models.md)
|
||||||
|
- `--language`。リストは[こちら](https://github.com/QuentinFuxa/WhisperLiveKit/blob/main/whisperlivekit/simul_whisper/whisper/tokenizer.py)。`auto`を使用すると、モデルは自動的に言語を検出しようとしますが、英語に偏る傾向があります。
|
||||||
|
- `--backend`? `simulstreaming`が正しく動作しない場合や、デュアルライセンス要件を避けたい場合は`--backend faster-whisper`に切り替えることができます。
|
||||||
|
- `--warmup-file`、もしあれば
|
||||||
|
- `--host`, `--port`, `--ssl-certfile`, `--ssl-keyfile`、サーバーをセットアップする場合
|
||||||
|
- `--diarization`、使用したい場合。
|
||||||
|
|
||||||
|
残りは推奨しません。しかし、以下があなたのオプションです。
|
||||||
|
|
||||||
|
| パラメータ | 説明 | デフォルト |
|
||||||
|
|-----------|-------------|---------|
|
||||||
|
| `--model` | Whisperモデルのサイズ。 | `small` |
|
||||||
|
| `--language` | ソース言語コードまたは`auto` | `auto` |
|
||||||
|
| `--task` | `transcribe`または`translate` | `transcribe` |
|
||||||
|
| `--backend` | 処理バックエンド | `simulstreaming` |
|
||||||
|
| `--min-chunk-size` | 最小音声チャンクサイズ(秒) | `1.0` |
|
||||||
|
| `--no-vac` | 音声アクティビティコントローラーを無効化 | `False` |
|
||||||
|
| `--no-vad` | 音声区間検出を無効化 | `False` |
|
||||||
|
| `--warmup-file` | モデルのウォームアップ用音声ファイルパス | `jfk.wav` |
|
||||||
|
| `--host` | サーバーホストアドレス | `localhost` |
|
||||||
|
| `--port` | サーバーポート | `8000` |
|
||||||
|
| `--ssl-certfile` | SSL証明書ファイルへのパス(HTTPSサポート用) | `None` |
|
||||||
|
| `--ssl-keyfile` | SSL秘密鍵ファイルへのパス(HTTPSサポート用) | `None` |
|
||||||
|
|
||||||
|
|
||||||
|
| WhisperStreamingバックエンドオプション | 説明 | デフォルト |
|
||||||
|
|-----------|-------------|---------|
|
||||||
|
| `--confidence-validation` | 高速な検証のために信頼スコアを使用 | `False` |
|
||||||
|
| `--buffer_trimming` | バッファトリミング戦略(`sentence`または`segment`) | `segment` |
|
||||||
|
|
||||||
|
|
||||||
|
| SimulStreamingバックエンドオプション | 説明 | デフォルト |
|
||||||
|
|-----------|-------------|---------|
|
||||||
|
| `--frame-threshold` | AlignAttフレームしきい値(低いほど速く、高いほど正確) | `25` |
|
||||||
|
| `--beams` | ビームサーチのビーム数(1 = 貪欲デコーディング) | `1` |
|
||||||
|
| `--decoder` | デコーダタイプを強制(`beam`または`greedy`) | `auto` |
|
||||||
|
| `--audio-max-len` | 最大音声バッファ長(秒) | `30.0` |
|
||||||
|
| `--audio-min-len` | 処理する最小音声長(秒) | `0.0` |
|
||||||
|
| `--cif-ckpt-path` | 単語境界検出用CIFモデルへのパス | `None` |
|
||||||
|
| `--never-fire` | 未完了の単語を決して切り捨てない | `False` |
|
||||||
|
| `--init-prompt` | モデルの初期プロンプト | `None` |
|
||||||
|
| `--static-init-prompt` | スクロールしない静的プロンプト | `None` |
|
||||||
|
| `--max-context-tokens` | 最大コンテキストトークン数 | `None` |
|
||||||
|
| `--model-path` | .ptモデルファイルへの直接パス。見つからない場合はダウンロード | `./base.pt` |
|
||||||
|
| `--preloaded-model-count` | オプション。メモリにプリロードするモデルの数(予想される同時ユーザー数まで設定) | `1` |
|
||||||
|
|
||||||
|
| ダイアライゼーションオプション | 説明 | デフォルト |
|
||||||
|
|-----------|-------------|---------|
|
||||||
|
| `--diarization` | 話者識別を有効化 | `False` |
|
||||||
|
| `--diarization-backend` | `diart`または`sortformer` | `sortformer` |
|
||||||
|
| `--segmentation-model` | DiartセグメンテーションモデルのHugging FaceモデルID。[利用可能なモデル](https://github.com/juanmc2005/diart/tree/main?tab=readme-ov-file#pre-trained-models) | `pyannote/segmentation-3.0` |
|
||||||
|
| `--embedding-model` | Diart埋め込みモデルのHugging FaceモデルID。[利用可能なモデル](https://github.com/juanmc2005/diart/tree/main?tab=readme-ov-file#pre-trained-models) | `speechbrain/spkrec-ecapa-voxceleb` |
|
||||||
|
|
||||||
|
|
||||||
|
> Diartを使用したダイアライゼーションには、pyannote.audioモデルへのアクセスが必要です:
|
||||||
|
> 1. `pyannote/segmentation`モデルの[ユーザー条件に同意](https://huggingface.co/pyannote/segmentation)
|
||||||
|
> 2. `pyannote/segmentation-3.0`モデルの[ユーザー条件に同意](https://huggingface.co/pyannote/segmentation-3.0)
|
||||||
|
> 3. `pyannote/embedding`モデルの[ユーザー条件に同意](https://huggingface.co/pyannote/embedding)
|
||||||
|
>4. HuggingFaceでログイン: `huggingface-cli login`
|
||||||
|
|
||||||
|
### 🚀 デプロイガイド
|
||||||
|
|
||||||
|
WhisperLiveKitを本番環境にデプロイするには:
|
||||||
|
|
||||||
|
1. **サーバーセットアップ**: 本番用ASGIサーバーをインストールし、複数のワーカーで起動します
|
||||||
|
```bash
|
||||||
|
pip install uvicorn gunicorn
|
||||||
|
gunicorn -k uvicorn.workers.UvicornWorker -w 4 your_app:app
|
||||||
|
```
|
||||||
|
|
||||||
|
2. **フロントエンド**: カスタマイズした`html`のバージョンをホストし、WebSocket接続が正しくポイントするようにします
|
||||||
|
|
||||||
|
3. **Nginx設定** (本番環境で推奨):
|
||||||
|
```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サポート**: 安全なデプロイメントのために、WebSocket URLで "ws://" の代わりに "wss://" を使用します
|
||||||
|
|
||||||
|
## 🐋 Docker
|
||||||
|
|
||||||
|
GPUまたはCPUサポート付きでDockerを使用してアプリケーションを簡単にデプロイします。
|
||||||
|
|
||||||
|
### 前提条件
|
||||||
|
- Dockerがシステムにインストールされていること
|
||||||
|
- GPUサポートの場合: NVIDIA Dockerランタイムがインストールされていること
|
||||||
|
|
||||||
|
### クイックスタート
|
||||||
|
|
||||||
|
**GPUアクセラレーション付き (推奨):**
|
||||||
|
```bash
|
||||||
|
docker build -t wlk .
|
||||||
|
docker run --gpus all -p 8000:8000 --name wlk wlk
|
||||||
|
```
|
||||||
|
|
||||||
|
**CPUのみ:**
|
||||||
|
```bash
|
||||||
|
docker build -f Dockerfile.cpu -t wlk .
|
||||||
|
docker run -p 8000:8000 --name wlk wlk
|
||||||
|
```
|
||||||
|
|
||||||
|
### 高度な使用法
|
||||||
|
|
||||||
|
**カスタム設定:**
|
||||||
|
```bash
|
||||||
|
# カスタムモデルと言語の例
|
||||||
|
docker run --gpus all -p 8000:8000 --name wlk wlk --model large-v3 --language fr
|
||||||
|
```
|
||||||
|
|
||||||
|
### メモリ要件
|
||||||
|
- **大規模モデル**: Dockerランタイムに十分なメモリが割り当てられていることを確認してください
|
||||||
|
|
||||||
|
|
||||||
|
#### カスタマイズ
|
||||||
|
|
||||||
|
- `--build-arg` オプション:
|
||||||
|
- `EXTRAS="whisper-timestamped"` - イメージのインストールにエクストラを追加します(スペースなし)。必要なコンテナオプションを設定することを忘れないでください!
|
||||||
|
- `HF_PRECACHE_DIR="./.cache/"` - 初回起動を高速化するためにモデルキャッシュをプリロードします
|
||||||
|
- `HF_TKN_FILE="./token"` - ゲート付きモデルをダウンロードするためにHugging Face Hubアクセストークンを追加します
|
||||||
|
|
||||||
|
## 🔮 ユースケース
|
||||||
|
会議の文字起こしのためにリアルタイムで議論をキャプチャする、聴覚障害のあるユーザーがアクセシビリティツールを通じて会話を追うのを助ける、コンテンツ作成のためにポッドキャストやビデオを自動的に文字起こしする、カスタマーサービスのために話者識別付きでサポートコールを文字起こしする...
|
||||||
BIN
architecture.png
BIN
architecture.png
Binary file not shown.
|
Before Width: | Height: | Size: 388 KiB After Width: | Height: | Size: 368 KiB |
BIN
demo.png
BIN
demo.png
Binary file not shown.
|
Before Width: | Height: | Size: 423 KiB After Width: | Height: | Size: 449 KiB |
@@ -4,8 +4,8 @@ build-backend = "setuptools.build_meta"
|
|||||||
|
|
||||||
[project]
|
[project]
|
||||||
name = "whisperlivekit"
|
name = "whisperlivekit"
|
||||||
version = "0.2.7"
|
version = "0.2.8"
|
||||||
description = "Real-time, Fully Local Whisper's Speech-to-Text and Speaker Diarization"
|
description = "Real-time speech-to-text with speaker diarization using Whisper"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
authors = [
|
authors = [
|
||||||
{ name = "Quentin Fuxa" }
|
{ name = "Quentin Fuxa" }
|
||||||
@@ -18,6 +18,11 @@ classifiers = [
|
|||||||
"License :: OSI Approved :: MIT License",
|
"License :: OSI Approved :: MIT License",
|
||||||
"Programming Language :: Python :: 3.9",
|
"Programming Language :: Python :: 3.9",
|
||||||
"Programming Language :: Python :: 3.10",
|
"Programming Language :: Python :: 3.10",
|
||||||
|
"Programming Language :: Python :: 3.11",
|
||||||
|
"Programming Language :: Python :: 3.12",
|
||||||
|
"Programming Language :: Python :: 3.13",
|
||||||
|
"Programming Language :: Python :: 3.14",
|
||||||
|
"Programming Language :: Python :: 3.15",
|
||||||
"Topic :: Scientific/Engineering :: Artificial Intelligence",
|
"Topic :: Scientific/Engineering :: Artificial Intelligence",
|
||||||
"Topic :: Multimedia :: Sound/Audio :: Speech"
|
"Topic :: Multimedia :: Sound/Audio :: Speech"
|
||||||
]
|
]
|
||||||
@@ -28,10 +33,11 @@ dependencies = [
|
|||||||
"faster-whisper",
|
"faster-whisper",
|
||||||
"uvicorn",
|
"uvicorn",
|
||||||
"websockets",
|
"websockets",
|
||||||
"torch",
|
"torchaudio>=2.0.0",
|
||||||
|
"torch>=2.0.0",
|
||||||
"tqdm",
|
"tqdm",
|
||||||
"tiktoken",
|
"tiktoken",
|
||||||
'triton>=2.0.0,<3; platform_machine == "x86_64" and (sys_platform == "linux" or sys_platform == "linux2")'
|
'triton>=2.0.0; platform_machine == "x86_64" and (sys_platform == "linux" or sys_platform == "linux2")'
|
||||||
]
|
]
|
||||||
|
|
||||||
[project.optional-dependencies]
|
[project.optional-dependencies]
|
||||||
|
|||||||
@@ -1,12 +1,13 @@
|
|||||||
from .audio_processor import AudioProcessor
|
from .audio_processor import AudioProcessor
|
||||||
from .core import TranscriptionEngine
|
from .core import TranscriptionEngine
|
||||||
from .parse_args import parse_args
|
from .parse_args import parse_args
|
||||||
from .web.web_interface import get_web_interface_html
|
from .web.web_interface import get_web_interface_html, get_inline_ui_html
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
"TranscriptionEngine",
|
"TranscriptionEngine",
|
||||||
"AudioProcessor",
|
"AudioProcessor",
|
||||||
"parse_args",
|
"parse_args",
|
||||||
"get_web_interface_html",
|
"get_web_interface_html",
|
||||||
|
"get_inline_ui_html",
|
||||||
"download_simulstreaming_backend",
|
"download_simulstreaming_backend",
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -120,7 +120,7 @@ class AudioProcessor:
|
|||||||
async def add_dummy_token(self):
|
async def add_dummy_token(self):
|
||||||
"""Placeholder token when no transcription is available."""
|
"""Placeholder token when no transcription is available."""
|
||||||
async with self.lock:
|
async with self.lock:
|
||||||
current_time = time() - self.beg_loop
|
current_time = time() - self.beg_loop if self.beg_loop else 0
|
||||||
self.tokens.append(ASRToken(
|
self.tokens.append(ASRToken(
|
||||||
start=current_time, end=current_time + 1,
|
start=current_time, end=current_time + 1,
|
||||||
text=".", speaker=-1, is_dummy=True
|
text=".", speaker=-1, is_dummy=True
|
||||||
@@ -295,7 +295,7 @@ class AudioProcessor:
|
|||||||
|
|
||||||
if type(item) is Silence:
|
if type(item) is Silence:
|
||||||
cumulative_pcm_duration_stream_time += item.duration
|
cumulative_pcm_duration_stream_time += item.duration
|
||||||
self.online.insert_silence(item.duration, self.tokens[-1].end)
|
self.online.insert_silence(item.duration, self.tokens[-1].end if self.tokens else 0)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if isinstance(item, np.ndarray):
|
if isinstance(item, np.ndarray):
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ from contextlib import asynccontextmanager
|
|||||||
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
|
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
|
||||||
from fastapi.responses import HTMLResponse
|
from fastapi.responses import HTMLResponse
|
||||||
from fastapi.middleware.cors import CORSMiddleware
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
from whisperlivekit import TranscriptionEngine, AudioProcessor, get_web_interface_html, parse_args
|
from whisperlivekit import TranscriptionEngine, AudioProcessor, get_inline_ui_html, parse_args
|
||||||
import asyncio
|
import asyncio
|
||||||
import logging
|
import logging
|
||||||
from starlette.staticfiles import StaticFiles
|
from starlette.staticfiles import StaticFiles
|
||||||
@@ -19,6 +19,15 @@ transcription_engine = None
|
|||||||
|
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def lifespan(app: FastAPI):
|
async def lifespan(app: FastAPI):
|
||||||
|
|
||||||
|
#to remove after 0.2.8
|
||||||
|
if args.backend == "simulstreaming" and not args.disable_fast_encoder:
|
||||||
|
logger.warning(f"""
|
||||||
|
{'='*50}
|
||||||
|
WhisperLiveKit 0.2.8 has introduced a new fast encoder feature using MLX Whisper or Faster Whisper for improved speed. Use --disable-fast-encoder to disable if you encounter issues.
|
||||||
|
{'='*50}
|
||||||
|
""")
|
||||||
|
|
||||||
global transcription_engine
|
global transcription_engine
|
||||||
transcription_engine = TranscriptionEngine(
|
transcription_engine = TranscriptionEngine(
|
||||||
**vars(args),
|
**vars(args),
|
||||||
@@ -38,7 +47,7 @@ app.mount("/web", StaticFiles(directory=str(web_dir)), name="web")
|
|||||||
|
|
||||||
@app.get("/")
|
@app.get("/")
|
||||||
async def get():
|
async def get():
|
||||||
return HTMLResponse(get_web_interface_html())
|
return HTMLResponse(get_inline_ui_html())
|
||||||
|
|
||||||
|
|
||||||
async def handle_websocket_results(websocket, results_generator):
|
async def handle_websocket_results(websocket, results_generator):
|
||||||
|
|||||||
@@ -46,6 +46,7 @@ class TranscriptionEngine:
|
|||||||
"confidence_validation": False,
|
"confidence_validation": False,
|
||||||
"buffer_trimming_sec": 15,
|
"buffer_trimming_sec": 15,
|
||||||
# simulstreaming params:
|
# simulstreaming params:
|
||||||
|
"disable_fast_encoder": False,
|
||||||
"frame_threshold": 25,
|
"frame_threshold": 25,
|
||||||
"beams": 1,
|
"beams": 1,
|
||||||
"decoder_type": None,
|
"decoder_type": None,
|
||||||
@@ -60,7 +61,7 @@ class TranscriptionEngine:
|
|||||||
"diarization_backend": "sortformer",
|
"diarization_backend": "sortformer",
|
||||||
# diart params:
|
# diart params:
|
||||||
"segmentation_model": "pyannote/segmentation-3.0",
|
"segmentation_model": "pyannote/segmentation-3.0",
|
||||||
"embedding_model": "pyannote/embedding",
|
"embedding_model": "pyannote/embedding",
|
||||||
}
|
}
|
||||||
|
|
||||||
config_dict = {**defaults, **kwargs}
|
config_dict = {**defaults, **kwargs}
|
||||||
@@ -97,7 +98,7 @@ class TranscriptionEngine:
|
|||||||
simulstreaming_kwargs = {}
|
simulstreaming_kwargs = {}
|
||||||
for attr in ['frame_threshold', 'beams', 'decoder_type', 'audio_max_len', 'audio_min_len',
|
for attr in ['frame_threshold', 'beams', 'decoder_type', 'audio_max_len', 'audio_min_len',
|
||||||
'cif_ckpt_path', 'never_fire', 'init_prompt', 'static_init_prompt',
|
'cif_ckpt_path', 'never_fire', 'init_prompt', 'static_init_prompt',
|
||||||
'max_context_tokens', 'model_path', 'warmup_file', 'preload_model_count']:
|
'max_context_tokens', 'model_path', 'warmup_file', 'preload_model_count', 'disable_fast_encoder']:
|
||||||
if hasattr(self.args, attr):
|
if hasattr(self.args, attr):
|
||||||
simulstreaming_kwargs[attr] = getattr(self.args, attr)
|
simulstreaming_kwargs[attr] = getattr(self.args, attr)
|
||||||
|
|
||||||
|
|||||||
@@ -161,6 +161,14 @@ def parse_args():
|
|||||||
|
|
||||||
# SimulStreaming-specific arguments
|
# SimulStreaming-specific arguments
|
||||||
simulstreaming_group = parser.add_argument_group('SimulStreaming arguments (only used with --backend simulstreaming)')
|
simulstreaming_group = parser.add_argument_group('SimulStreaming arguments (only used with --backend simulstreaming)')
|
||||||
|
|
||||||
|
simulstreaming_group.add_argument(
|
||||||
|
"--disable-fast-encoder",
|
||||||
|
action="store_true",
|
||||||
|
default=False,
|
||||||
|
dest="disable_fast_encoder",
|
||||||
|
help="Disable Faster Whisper or MLX Whisper backends for encoding (if installed). Slower but helpful when GPU memory is limited",
|
||||||
|
)
|
||||||
|
|
||||||
simulstreaming_group.add_argument(
|
simulstreaming_group.add_argument(
|
||||||
"--frame-threshold",
|
"--frame-threshold",
|
||||||
|
|||||||
@@ -81,7 +81,7 @@ def ends_with_silence(tokens, buffer_transcription, buffer_diarization, current_
|
|||||||
if not tokens:
|
if not tokens:
|
||||||
return [], buffer_transcription, buffer_diarization
|
return [], buffer_transcription, buffer_diarization
|
||||||
last_token = tokens[-1]
|
last_token = tokens[-1]
|
||||||
if tokens and (
|
if tokens and current_time and (
|
||||||
current_time - last_token.end >= END_SILENCE_DURATION
|
current_time - last_token.end >= END_SILENCE_DURATION
|
||||||
or
|
or
|
||||||
(current_time - last_token.end >= 3 and vac_detected_silence)
|
(current_time - last_token.end >= 3 and vac_detected_silence)
|
||||||
|
|||||||
@@ -13,15 +13,25 @@ import os
|
|||||||
import gc
|
import gc
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
import torch
|
||||||
|
from whisperlivekit.simul_whisper.config import AlignAttConfig
|
||||||
|
from whisperlivekit.simul_whisper.simul_whisper import PaddedAlignAttWhisper
|
||||||
|
from whisperlivekit.simul_whisper.whisper import tokenizer
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import torch
|
from .mlx_encoder import mlx_model_mapping, load_mlx_encoder
|
||||||
from whisperlivekit.simul_whisper.config import AlignAttConfig
|
HAS_MLX_WHISPER = True
|
||||||
from whisperlivekit.simul_whisper.simul_whisper import PaddedAlignAttWhisper
|
except ImportError:
|
||||||
from whisperlivekit.simul_whisper.whisper import tokenizer
|
HAS_MLX_WHISPER = False
|
||||||
except ImportError as e:
|
if HAS_MLX_WHISPER:
|
||||||
raise ImportError(
|
HAS_FASTER_WHISPER = False
|
||||||
"""SimulStreaming dependencies are not available.
|
else:
|
||||||
Please install WhisperLiveKit using pip install "whisperlivekit[simulstreaming]".""")
|
try:
|
||||||
|
from faster_whisper import WhisperModel
|
||||||
|
HAS_FASTER_WHISPER = True
|
||||||
|
except ImportError:
|
||||||
|
HAS_FASTER_WHISPER = False
|
||||||
|
|
||||||
|
|
||||||
# TOO_MANY_REPETITIONS = 3
|
# TOO_MANY_REPETITIONS = 3
|
||||||
|
|
||||||
@@ -42,6 +52,8 @@ class SimulStreamingOnlineProcessor:
|
|||||||
self.committed: List[ASRToken] = []
|
self.committed: List[ASRToken] = []
|
||||||
self.last_result_tokens: List[ASRToken] = []
|
self.last_result_tokens: List[ASRToken] = []
|
||||||
self.load_new_backend()
|
self.load_new_backend()
|
||||||
|
|
||||||
|
#can be moved
|
||||||
if asr.tokenizer:
|
if asr.tokenizer:
|
||||||
self.model.tokenizer = asr.tokenizer
|
self.model.tokenizer = asr.tokenizer
|
||||||
|
|
||||||
@@ -49,7 +61,10 @@ class SimulStreamingOnlineProcessor:
|
|||||||
model = self.asr.get_new_model_instance()
|
model = self.asr.get_new_model_instance()
|
||||||
self.model = PaddedAlignAttWhisper(
|
self.model = PaddedAlignAttWhisper(
|
||||||
cfg=self.asr.cfg,
|
cfg=self.asr.cfg,
|
||||||
loaded_model=model)
|
loaded_model=model,
|
||||||
|
mlx_encoder=self.asr.mlx_encoder,
|
||||||
|
fw_encoder=self.asr.fw_encoder,
|
||||||
|
)
|
||||||
|
|
||||||
def insert_silence(self, silence_duration, offset):
|
def insert_silence(self, silence_duration, offset):
|
||||||
"""
|
"""
|
||||||
@@ -212,7 +227,7 @@ class SimulStreamingASR():
|
|||||||
logger.warning(SIMULSTREAMING_LICENSE)
|
logger.warning(SIMULSTREAMING_LICENSE)
|
||||||
self.logfile = logfile
|
self.logfile = logfile
|
||||||
self.transcribe_kargs = {}
|
self.transcribe_kargs = {}
|
||||||
self.original_language = None if lan == "auto" else lan
|
self.original_language = lan
|
||||||
|
|
||||||
self.model_path = kwargs.get('model_path', './large-v3.pt')
|
self.model_path = kwargs.get('model_path', './large-v3.pt')
|
||||||
self.frame_threshold = kwargs.get('frame_threshold', 25)
|
self.frame_threshold = kwargs.get('frame_threshold', 25)
|
||||||
@@ -229,7 +244,8 @@ class SimulStreamingASR():
|
|||||||
self.max_context_tokens = kwargs.get('max_context_tokens', None)
|
self.max_context_tokens = kwargs.get('max_context_tokens', None)
|
||||||
self.warmup_file = kwargs.get('warmup_file', None)
|
self.warmup_file = kwargs.get('warmup_file', None)
|
||||||
self.preload_model_count = kwargs.get('preload_model_count', 1)
|
self.preload_model_count = kwargs.get('preload_model_count', 1)
|
||||||
|
self.disable_fast_encoder = kwargs.get('disable_fast_encoder', False)
|
||||||
|
self.fast_encoder = False
|
||||||
if model_dir is not None:
|
if model_dir is not None:
|
||||||
self.model_path = model_dir
|
self.model_path = model_dir
|
||||||
elif modelsize is not None:
|
elif modelsize is not None:
|
||||||
@@ -249,11 +265,6 @@ class SimulStreamingASR():
|
|||||||
}
|
}
|
||||||
self.model_path = model_mapping.get(modelsize, f'./{modelsize}.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(
|
self.cfg = AlignAttConfig(
|
||||||
model_path=self.model_path,
|
model_path=self.model_path,
|
||||||
segment_length=self.segment_length,
|
segment_length=self.segment_length,
|
||||||
@@ -271,17 +282,52 @@ class SimulStreamingASR():
|
|||||||
static_init_prompt=self.static_init_prompt,
|
static_init_prompt=self.static_init_prompt,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Set up tokenizer for translation if needed
|
||||||
|
if self.task == "translate":
|
||||||
|
self.tokenizer = self.set_translate_task()
|
||||||
|
else:
|
||||||
|
self.tokenizer = None
|
||||||
|
|
||||||
self.model_name = os.path.basename(self.cfg.model_path).replace(".pt", "")
|
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.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)]
|
|
||||||
|
|
||||||
|
self.mlx_encoder, self.fw_encoder = None, None
|
||||||
|
if not self.disable_fast_encoder:
|
||||||
|
if HAS_MLX_WHISPER:
|
||||||
|
print('Simulstreaming will use MLX whisper for a faster encoder.')
|
||||||
|
mlx_model_name = mlx_model_mapping[self.model_name]
|
||||||
|
self.mlx_encoder = load_mlx_encoder(path_or_hf_repo=mlx_model_name)
|
||||||
|
self.fast_encoder = True
|
||||||
|
elif HAS_FASTER_WHISPER:
|
||||||
|
print('Simulstreaming will use Faster Whisper for the encoder.')
|
||||||
|
self.fw_encoder = WhisperModel(
|
||||||
|
self.model_name,
|
||||||
|
device='auto',
|
||||||
|
compute_type='auto',
|
||||||
|
)
|
||||||
|
self.fast_encoder = True
|
||||||
|
|
||||||
|
self.models = [self.load_model() for i in range(self.preload_model_count)]
|
||||||
|
|
||||||
|
|
||||||
def load_model(self):
|
def load_model(self):
|
||||||
whisper_model = load_model(name=self.model_name, download_root=self.model_path)
|
whisper_model = load_model(name=self.model_name, download_root=self.model_path, decoder_only=self.fast_encoder)
|
||||||
warmup_audio = load_file(self.warmup_file)
|
warmup_audio = load_file(self.warmup_file)
|
||||||
whisper_model.transcribe(warmup_audio, language=self.original_language)
|
if warmup_audio is not None:
|
||||||
|
warmup_audio = torch.from_numpy(warmup_audio).float()
|
||||||
|
if self.fast_encoder:
|
||||||
|
temp_model = PaddedAlignAttWhisper(
|
||||||
|
cfg=self.cfg,
|
||||||
|
loaded_model=whisper_model,
|
||||||
|
mlx_encoder=self.mlx_encoder,
|
||||||
|
fw_encoder=self.fw_encoder,
|
||||||
|
)
|
||||||
|
temp_model.warmup(warmup_audio)
|
||||||
|
temp_model.remove_hooks()
|
||||||
|
else:
|
||||||
|
# For standard encoder, use the original transcribe warmup
|
||||||
|
warmup_audio = load_file(self.warmup_file)
|
||||||
|
whisper_model.transcribe(warmup_audio, language=self.original_language if self.original_language != 'auto' else None)
|
||||||
return whisper_model
|
return whisper_model
|
||||||
|
|
||||||
def get_new_model_instance(self):
|
def get_new_model_instance(self):
|
||||||
@@ -301,10 +347,12 @@ class SimulStreamingASR():
|
|||||||
|
|
||||||
def set_translate_task(self):
|
def set_translate_task(self):
|
||||||
"""Set up translation task."""
|
"""Set up translation task."""
|
||||||
|
if self.cfg.language == 'auto':
|
||||||
|
raise Exception('Translation cannot be done with language = auto')
|
||||||
return tokenizer.get_tokenizer(
|
return tokenizer.get_tokenizer(
|
||||||
multilingual=True,
|
multilingual=True,
|
||||||
language=self.model.cfg.language,
|
language=self.cfg.language,
|
||||||
num_languages=self.model.model.num_languages,
|
num_languages=99,
|
||||||
task="translate"
|
task="translate"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
72
whisperlivekit/simul_whisper/mlx_encoder.py
Normal file
72
whisperlivekit/simul_whisper/mlx_encoder.py
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
import json
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import mlx.core as mx
|
||||||
|
import mlx.nn as nn
|
||||||
|
from huggingface_hub import snapshot_download
|
||||||
|
from mlx.utils import tree_unflatten
|
||||||
|
|
||||||
|
from mlx_whisper import whisper
|
||||||
|
|
||||||
|
mlx_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",
|
||||||
|
}
|
||||||
|
|
||||||
|
def load_mlx_encoder(
|
||||||
|
path_or_hf_repo: str,
|
||||||
|
dtype: mx.Dtype = mx.float32,
|
||||||
|
) -> whisper.Whisper:
|
||||||
|
model_path = Path(path_or_hf_repo)
|
||||||
|
if not model_path.exists():
|
||||||
|
model_path = Path(snapshot_download(repo_id=path_or_hf_repo))
|
||||||
|
|
||||||
|
with open(str(model_path / "config.json"), "r") as f:
|
||||||
|
config = json.loads(f.read())
|
||||||
|
config.pop("model_type", None)
|
||||||
|
quantization = config.pop("quantization", None)
|
||||||
|
|
||||||
|
model_args = whisper.ModelDimensions(**config)
|
||||||
|
|
||||||
|
wf = model_path / "weights.safetensors"
|
||||||
|
if not wf.exists():
|
||||||
|
wf = model_path / "weights.npz"
|
||||||
|
weights = mx.load(str(wf))
|
||||||
|
|
||||||
|
model = whisper.Whisper(model_args, dtype)
|
||||||
|
|
||||||
|
if quantization is not None:
|
||||||
|
class_predicate = (
|
||||||
|
lambda p, m: isinstance(m, (nn.Linear, nn.Embedding))
|
||||||
|
and f"{p}.scales" in weights
|
||||||
|
)
|
||||||
|
nn.quantize(model, **quantization, class_predicate=class_predicate)
|
||||||
|
|
||||||
|
weights = tree_unflatten(list(weights.items()))
|
||||||
|
|
||||||
|
# we only want to load the encoder weights here.
|
||||||
|
# Size examples: for tiny.en,
|
||||||
|
# Decoder weights: 59110771 bytes
|
||||||
|
# Encoder weights: 15268874 bytes
|
||||||
|
|
||||||
|
|
||||||
|
encoder_weights = {}
|
||||||
|
encoder_weights['encoder'] = weights['encoder']
|
||||||
|
del(weights)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
model.update(encoder_weights)
|
||||||
|
mx.eval(model.parameters())
|
||||||
|
return model
|
||||||
@@ -14,7 +14,7 @@ from .whisper.decoding import GreedyDecoder, BeamSearchDecoder, SuppressTokens,
|
|||||||
from .beam import BeamPyTorchInference
|
from .beam import BeamPyTorchInference
|
||||||
from .eow_detection import fire_at_boundary, load_cif
|
from .eow_detection import fire_at_boundary, load_cif
|
||||||
import os
|
import os
|
||||||
|
from time import time
|
||||||
from .token_buffer import TokenBuffer
|
from .token_buffer import TokenBuffer
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
@@ -23,8 +23,22 @@ from .generation_progress import *
|
|||||||
DEC_PAD = 50257
|
DEC_PAD = 50257
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
import sys
|
|
||||||
import wave
|
try:
|
||||||
|
from mlx_whisper.audio import log_mel_spectrogram as mlx_log_mel_spectrogram
|
||||||
|
from mlx_whisper.transcribe import pad_or_trim as mlx_pad_or_trim
|
||||||
|
HAS_MLX_WHISPER = True
|
||||||
|
except ImportError:
|
||||||
|
HAS_MLX_WHISPER = False
|
||||||
|
if HAS_MLX_WHISPER:
|
||||||
|
HAS_FASTER_WHISPER = False
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
from faster_whisper.audio import pad_or_trim as fw_pad_or_trim
|
||||||
|
from faster_whisper.feature_extractor import FeatureExtractor
|
||||||
|
HAS_FASTER_WHISPER = True
|
||||||
|
except ImportError:
|
||||||
|
HAS_FASTER_WHISPER = False
|
||||||
|
|
||||||
# New features added to the original version of Simul-Whisper:
|
# New features added to the original version of Simul-Whisper:
|
||||||
# - large-v3 model support
|
# - large-v3 model support
|
||||||
@@ -33,7 +47,13 @@ import wave
|
|||||||
# - prompt -- static vs. non-static
|
# - prompt -- static vs. non-static
|
||||||
# - context
|
# - context
|
||||||
class PaddedAlignAttWhisper:
|
class PaddedAlignAttWhisper:
|
||||||
def __init__(self, cfg: AlignAttConfig, loaded_model=None) -> None:
|
def __init__(
|
||||||
|
self,
|
||||||
|
cfg: AlignAttConfig,
|
||||||
|
loaded_model=None,
|
||||||
|
mlx_encoder=None,
|
||||||
|
fw_encoder=None,
|
||||||
|
) -> None:
|
||||||
self.log_segments = 0
|
self.log_segments = 0
|
||||||
model_name = os.path.basename(cfg.model_path).replace(".pt", "")
|
model_name = os.path.basename(cfg.model_path).replace(".pt", "")
|
||||||
model_path = os.path.dirname(os.path.abspath(cfg.model_path))
|
model_path = os.path.dirname(os.path.abspath(cfg.model_path))
|
||||||
@@ -42,6 +62,11 @@ class PaddedAlignAttWhisper:
|
|||||||
else:
|
else:
|
||||||
self.model = load_model(name=model_name, download_root=model_path)
|
self.model = load_model(name=model_name, download_root=model_path)
|
||||||
|
|
||||||
|
self.mlx_encoder = mlx_encoder
|
||||||
|
self.fw_encoder = fw_encoder
|
||||||
|
if fw_encoder:
|
||||||
|
self.fw_feature_extractor = FeatureExtractor(feature_size=self.model.dims.n_mels)
|
||||||
|
|
||||||
logger.info(f"Model dimensions: {self.model.dims}")
|
logger.info(f"Model dimensions: {self.model.dims}")
|
||||||
|
|
||||||
self.decode_options = DecodingOptions(
|
self.decode_options = DecodingOptions(
|
||||||
@@ -151,6 +176,15 @@ class PaddedAlignAttWhisper:
|
|||||||
for hook in self.l_hooks:
|
for hook in self.l_hooks:
|
||||||
hook.remove()
|
hook.remove()
|
||||||
|
|
||||||
|
def warmup(self, audio):
|
||||||
|
try:
|
||||||
|
self.insert_audio(audio)
|
||||||
|
self.infer(is_last=True)
|
||||||
|
self.refresh_segment(complete=True)
|
||||||
|
logger.info("Model warmed up successfully")
|
||||||
|
except Exception as e:
|
||||||
|
logger.exception(f"Model warmup failed: {e}")
|
||||||
|
|
||||||
def create_tokenizer(self, language=None):
|
def create_tokenizer(self, language=None):
|
||||||
self.tokenizer = tokenizer.get_tokenizer(
|
self.tokenizer = tokenizer.get_tokenizer(
|
||||||
multilingual=self.tokenizer_is_multilingual,
|
multilingual=self.tokenizer_is_multilingual,
|
||||||
@@ -359,20 +393,36 @@ class PaddedAlignAttWhisper:
|
|||||||
else:
|
else:
|
||||||
input_segments = self.segments[0]
|
input_segments = self.segments[0]
|
||||||
|
|
||||||
|
# NEW : we can use a different encoder, before using standart whisper for cross attention with the hooks on the decoder
|
||||||
|
beg_encode = time()
|
||||||
# mel + padding to 30s
|
if self.mlx_encoder:
|
||||||
mel_padded = log_mel_spectrogram(input_segments, n_mels=self.model.dims.n_mels, padding=N_SAMPLES,
|
mlx_mel_padded = mlx_log_mel_spectrogram(audio=input_segments.detach(), n_mels=self.model.dims.n_mels, padding=N_SAMPLES)
|
||||||
device=self.model.device).unsqueeze(0)
|
mlx_mel = mlx_pad_or_trim(mlx_mel_padded, N_FRAMES, axis=-2)
|
||||||
# trim to 3000
|
mlx_encoder_feature = self.mlx_encoder.encoder(mlx_mel[None])
|
||||||
mel = pad_or_trim(mel_padded, N_FRAMES)
|
encoder_feature = torch.tensor(np.array(mlx_encoder_feature))
|
||||||
|
content_mel_len = int((mlx_mel_padded.shape[0] - mlx_mel.shape[0])/2)
|
||||||
# the len of actual audio
|
device = 'cpu'
|
||||||
content_mel_len = int((mel_padded.shape[2] - mel.shape[2])/2)
|
elif self.fw_encoder:
|
||||||
|
audio_length_seconds = len(input_segments) / 16000
|
||||||
# encode
|
content_mel_len = int(audio_length_seconds * 100)//2
|
||||||
encoder_feature = self.model.encoder(mel)
|
mel_padded_2 = self.fw_feature_extractor(waveform=input_segments.numpy(), padding=N_SAMPLES)[None, :]
|
||||||
|
mel = fw_pad_or_trim(mel_padded_2, N_FRAMES, axis=-1)
|
||||||
|
encoder_feature_ctranslate = self.fw_encoder.encode(mel)
|
||||||
|
encoder_feature = torch.Tensor(np.array(encoder_feature_ctranslate))
|
||||||
|
device = 'cpu'
|
||||||
|
else:
|
||||||
|
# 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)
|
||||||
|
encoder_feature = self.model.encoder(mel)
|
||||||
|
device = mel.device
|
||||||
|
end_encode = time()
|
||||||
|
# print('Encoder duration:', end_encode-beg_encode)
|
||||||
|
|
||||||
# logger.debug(f"Encoder feature shape: {encoder_feature.shape}")
|
# 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):
|
# if mel.shape[-2:] != (self.model.dims.n_audio_ctx, self.model.dims.n_audio_state):
|
||||||
# logger.debug("mel ")
|
# logger.debug("mel ")
|
||||||
@@ -397,7 +447,7 @@ class PaddedAlignAttWhisper:
|
|||||||
####################### Decoding loop
|
####################### Decoding loop
|
||||||
logger.info("Decoding loop starts\n")
|
logger.info("Decoding loop starts\n")
|
||||||
|
|
||||||
sum_logprobs = torch.zeros(self.cfg.beam_size, device=mel.device)
|
sum_logprobs = torch.zeros(self.cfg.beam_size, device=device)
|
||||||
completed = False
|
completed = False
|
||||||
|
|
||||||
attn_of_alignment_heads = None
|
attn_of_alignment_heads = None
|
||||||
|
|||||||
@@ -105,6 +105,7 @@ def load_model(
|
|||||||
device: Optional[Union[str, torch.device]] = None,
|
device: Optional[Union[str, torch.device]] = None,
|
||||||
download_root: str = None,
|
download_root: str = None,
|
||||||
in_memory: bool = False,
|
in_memory: bool = False,
|
||||||
|
decoder_only=False
|
||||||
) -> Whisper:
|
) -> Whisper:
|
||||||
"""
|
"""
|
||||||
Load a Whisper ASR model
|
Load a Whisper ASR model
|
||||||
@@ -151,7 +152,14 @@ def load_model(
|
|||||||
del checkpoint_file
|
del checkpoint_file
|
||||||
|
|
||||||
dims = ModelDimensions(**checkpoint["dims"])
|
dims = ModelDimensions(**checkpoint["dims"])
|
||||||
model = Whisper(dims)
|
model = Whisper(dims, decoder_only=decoder_only)
|
||||||
|
|
||||||
|
if decoder_only:
|
||||||
|
checkpoint["model_state_dict"] = {
|
||||||
|
k: v for k, v in checkpoint["model_state_dict"].items()
|
||||||
|
if 'encoder' not in k
|
||||||
|
}
|
||||||
|
|
||||||
model.load_state_dict(checkpoint["model_state_dict"])
|
model.load_state_dict(checkpoint["model_state_dict"])
|
||||||
|
|
||||||
if alignment_heads is not None:
|
if alignment_heads is not None:
|
||||||
|
|||||||
@@ -253,16 +253,18 @@ class TextDecoder(nn.Module):
|
|||||||
|
|
||||||
|
|
||||||
class Whisper(nn.Module):
|
class Whisper(nn.Module):
|
||||||
def __init__(self, dims: ModelDimensions):
|
def __init__(self, dims: ModelDimensions, decoder_only: bool = False):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.dims = dims
|
self.dims = dims
|
||||||
self.encoder = AudioEncoder(
|
|
||||||
self.dims.n_mels,
|
if not decoder_only:
|
||||||
self.dims.n_audio_ctx,
|
self.encoder = AudioEncoder(
|
||||||
self.dims.n_audio_state,
|
self.dims.n_mels,
|
||||||
self.dims.n_audio_head,
|
self.dims.n_audio_ctx,
|
||||||
self.dims.n_audio_layer,
|
self.dims.n_audio_state,
|
||||||
)
|
self.dims.n_audio_head,
|
||||||
|
self.dims.n_audio_layer,
|
||||||
|
)
|
||||||
self.decoder = TextDecoder(
|
self.decoder = TextDecoder(
|
||||||
self.dims.n_vocab,
|
self.dims.n_vocab,
|
||||||
self.dims.n_text_ctx,
|
self.dims.n_text_ctx,
|
||||||
|
|||||||
@@ -31,21 +31,21 @@ def load_file(warmup_file=None, timeout=5):
|
|||||||
logger.debug(f"Download successful in {time.time() - start_time:.2f}s")
|
logger.debug(f"Download successful in {time.time() - start_time:.2f}s")
|
||||||
except (urllib.error.URLError, socket.timeout) as e:
|
except (urllib.error.URLError, socket.timeout) as e:
|
||||||
logger.warning(f"Download failed: {e}. Proceeding without warmup.")
|
logger.warning(f"Download failed: {e}. Proceeding without warmup.")
|
||||||
return False
|
return None
|
||||||
finally:
|
finally:
|
||||||
socket.setdefaulttimeout(original_timeout)
|
socket.setdefaulttimeout(original_timeout)
|
||||||
elif not warmup_file:
|
elif not warmup_file:
|
||||||
return False
|
return None
|
||||||
|
|
||||||
if not warmup_file or not os.path.exists(warmup_file) or os.path.getsize(warmup_file) == 0:
|
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.")
|
logger.warning(f"Warmup file {warmup_file} invalid or missing.")
|
||||||
return False
|
return None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
audio, sr = librosa.load(warmup_file, sr=16000)
|
audio, sr = librosa.load(warmup_file, sr=16000)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.warning(f"Failed to load audio file: {e}")
|
logger.warning(f"Failed to load audio file: {e}")
|
||||||
return False
|
return None
|
||||||
return audio
|
return audio
|
||||||
|
|
||||||
def warmup_asr(asr, warmup_file=None, timeout=5):
|
def warmup_asr(asr, warmup_file=None, timeout=5):
|
||||||
|
|||||||
@@ -184,7 +184,7 @@ body {
|
|||||||
|
|
||||||
.settings {
|
.settings {
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: column;
|
flex-wrap: wrap;
|
||||||
align-items: flex-start;
|
align-items: flex-start;
|
||||||
gap: 12px;
|
gap: 12px;
|
||||||
}
|
}
|
||||||
@@ -198,23 +198,27 @@ body {
|
|||||||
|
|
||||||
#chunkSelector,
|
#chunkSelector,
|
||||||
#websocketInput,
|
#websocketInput,
|
||||||
#themeSelector {
|
#themeSelector,
|
||||||
|
#microphoneSelect {
|
||||||
font-size: 16px;
|
font-size: 16px;
|
||||||
padding: 5px 8px;
|
padding: 5px 8px;
|
||||||
border-radius: 8px;
|
border-radius: 8px;
|
||||||
border: 1px solid var(--border);
|
border: 1px solid var(--border);
|
||||||
background-color: var(--button-bg);
|
background-color: var(--button-bg);
|
||||||
color: var(--text);
|
color: var(--text);
|
||||||
max-height: 34px;
|
max-height: 30px;
|
||||||
}
|
}
|
||||||
|
|
||||||
#websocketInput {
|
#microphoneSelect {
|
||||||
width: 220px;
|
width: 100%;
|
||||||
|
max-width: 190px;
|
||||||
|
min-width: 120px;
|
||||||
}
|
}
|
||||||
|
|
||||||
#chunkSelector:focus,
|
#chunkSelector:focus,
|
||||||
#websocketInput:focus,
|
#websocketInput:focus,
|
||||||
#themeSelector:focus {
|
#themeSelector:focus,
|
||||||
|
#microphoneSelect:focus {
|
||||||
outline: none;
|
outline: none;
|
||||||
border-color: #007bff;
|
border-color: #007bff;
|
||||||
box-shadow: 0 0 0 3px rgba(0, 123, 255, 0.15);
|
box-shadow: 0 0 0 3px rgba(0, 123, 255, 0.15);
|
||||||
@@ -247,9 +251,9 @@ label {
|
|||||||
}
|
}
|
||||||
|
|
||||||
.theme-selector-container {
|
.theme-selector-container {
|
||||||
position: absolute;
|
display: flex;
|
||||||
top: 20px;
|
align-items: center;
|
||||||
right: 20px;
|
margin-top: 17px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.segmented label {
|
.segmented label {
|
||||||
@@ -400,3 +404,57 @@ label {
|
|||||||
font-size: 14px;
|
font-size: 14px;
|
||||||
margin-bottom: 0px;
|
margin-bottom: 0px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* for smaller screens */
|
||||||
|
@media (max-width: 768px) {
|
||||||
|
.settings-container {
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.settings {
|
||||||
|
justify-content: center;
|
||||||
|
gap: 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.field {
|
||||||
|
align-items: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
#websocketInput,
|
||||||
|
#microphoneSelect {
|
||||||
|
min-width: 100px;
|
||||||
|
max-width: 160px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.theme-selector-container {
|
||||||
|
margin-top: 10px;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (max-width: 480px) {
|
||||||
|
body {
|
||||||
|
margin: 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.settings {
|
||||||
|
flex-direction: column;
|
||||||
|
align-items: center;
|
||||||
|
gap: 6px;
|
||||||
|
}
|
||||||
|
|
||||||
|
#websocketInput,
|
||||||
|
#microphoneSelect {
|
||||||
|
max-width: 140px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.segmented label {
|
||||||
|
padding: 4px 8px;
|
||||||
|
font-size: 12px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.segmented img {
|
||||||
|
width: 14px;
|
||||||
|
height: 14px;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,61 +1,73 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
|
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8" />
|
<meta charset="UTF-8" />
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
<title>WhisperLiveKit</title>
|
<title>WhisperLiveKit</title>
|
||||||
<link rel="stylesheet" href="/web/live_transcription.css" />
|
<link rel="stylesheet" href="/web/live_transcription.css" />
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
<div class="settings-container">
|
<div class="settings-container">
|
||||||
<button id="recordButton">
|
<button id="recordButton">
|
||||||
<div class="shape-container">
|
<div class="shape-container">
|
||||||
<div class="shape"></div>
|
<div class="shape"></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="recording-info">
|
<div class="recording-info">
|
||||||
<div class="wave-container">
|
<div class="wave-container">
|
||||||
<canvas id="waveCanvas"></canvas>
|
<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 class="field">
|
||||||
|
<label id="microphoneSelectLabel" for="microphoneSelect">Select Microphone</label>
|
||||||
|
<select id="microphoneSelect">
|
||||||
|
<option value="">Default Microphone</option>
|
||||||
|
</select>
|
||||||
|
</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>
|
||||||
|
|
||||||
</div>
|
</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>
|
|
||||||
|
|
||||||
<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>
|
||||||
</div>
|
|
||||||
|
|
||||||
<p id="status"></p>
|
|
||||||
|
|
||||||
<div id="linesTranscript"></div>
|
|
||||||
|
|
||||||
<script src="/web/live_transcription.js"></script>
|
<p id="status"></p>
|
||||||
|
|
||||||
|
<div id="linesTranscript"></div>
|
||||||
|
|
||||||
|
<script src="/web/live_transcription.js"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
|
||||||
|
</html>
|
||||||
@@ -18,6 +18,8 @@ let animationFrame = null;
|
|||||||
let waitingForStop = false;
|
let waitingForStop = false;
|
||||||
let lastReceivedData = null;
|
let lastReceivedData = null;
|
||||||
let lastSignature = null;
|
let lastSignature = null;
|
||||||
|
let availableMicrophones = [];
|
||||||
|
let selectedMicrophoneId = null;
|
||||||
|
|
||||||
waveCanvas.width = 60 * (window.devicePixelRatio || 1);
|
waveCanvas.width = 60 * (window.devicePixelRatio || 1);
|
||||||
waveCanvas.height = 30 * (window.devicePixelRatio || 1);
|
waveCanvas.height = 30 * (window.devicePixelRatio || 1);
|
||||||
@@ -31,6 +33,7 @@ const websocketDefaultSpan = document.getElementById("wsDefaultUrl");
|
|||||||
const linesTranscriptDiv = document.getElementById("linesTranscript");
|
const linesTranscriptDiv = document.getElementById("linesTranscript");
|
||||||
const timerElement = document.querySelector(".timer");
|
const timerElement = document.querySelector(".timer");
|
||||||
const themeRadios = document.querySelectorAll('input[name="theme"]');
|
const themeRadios = document.querySelectorAll('input[name="theme"]');
|
||||||
|
const microphoneSelect = document.getElementById("microphoneSelect");
|
||||||
|
|
||||||
function getWaveStroke() {
|
function getWaveStroke() {
|
||||||
const styles = getComputedStyle(document.documentElement);
|
const styles = getComputedStyle(document.documentElement);
|
||||||
@@ -82,6 +85,61 @@ if (darkMq && darkMq.addEventListener) {
|
|||||||
darkMq.addListener(handleOsThemeChange);
|
darkMq.addListener(handleOsThemeChange);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function enumerateMicrophones() {
|
||||||
|
try {
|
||||||
|
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
||||||
|
stream.getTracks().forEach(track => track.stop());
|
||||||
|
|
||||||
|
const devices = await navigator.mediaDevices.enumerateDevices();
|
||||||
|
availableMicrophones = devices.filter(device => device.kind === 'audioinput');
|
||||||
|
|
||||||
|
populateMicrophoneSelect();
|
||||||
|
console.log(`Found ${availableMicrophones.length} microphone(s)`);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error enumerating microphones:', error);
|
||||||
|
statusText.textContent = "Error accessing microphones. Please grant permission.";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function populateMicrophoneSelect() {
|
||||||
|
if (!microphoneSelect) return;
|
||||||
|
|
||||||
|
microphoneSelect.innerHTML = '<option value="">Default Microphone</option>';
|
||||||
|
|
||||||
|
availableMicrophones.forEach((device, index) => {
|
||||||
|
const option = document.createElement('option');
|
||||||
|
option.value = device.deviceId;
|
||||||
|
option.textContent = device.label || `Microphone ${index + 1}`;
|
||||||
|
microphoneSelect.appendChild(option);
|
||||||
|
});
|
||||||
|
|
||||||
|
const savedMicId = localStorage.getItem('selectedMicrophone');
|
||||||
|
if (savedMicId && availableMicrophones.some(mic => mic.deviceId === savedMicId)) {
|
||||||
|
microphoneSelect.value = savedMicId;
|
||||||
|
selectedMicrophoneId = savedMicId;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleMicrophoneChange() {
|
||||||
|
selectedMicrophoneId = microphoneSelect.value || null;
|
||||||
|
localStorage.setItem('selectedMicrophone', selectedMicrophoneId || '');
|
||||||
|
|
||||||
|
const selectedDevice = availableMicrophones.find(mic => mic.deviceId === selectedMicrophoneId);
|
||||||
|
const deviceName = selectedDevice ? selectedDevice.label : 'Default Microphone';
|
||||||
|
|
||||||
|
console.log(`Selected microphone: ${deviceName}`);
|
||||||
|
statusText.textContent = `Microphone changed to: ${deviceName}`;
|
||||||
|
|
||||||
|
if (isRecording) {
|
||||||
|
statusText.textContent = "Switching microphone... Please wait.";
|
||||||
|
stopRecording().then(() => {
|
||||||
|
setTimeout(() => {
|
||||||
|
toggleRecording();
|
||||||
|
}, 1000);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Helpers
|
// Helpers
|
||||||
function fmt1(x) {
|
function fmt1(x) {
|
||||||
const n = Number(x);
|
const n = Number(x);
|
||||||
@@ -377,7 +435,11 @@ async function startRecording() {
|
|||||||
console.log("Error acquiring wake lock.");
|
console.log("Error acquiring wake lock.");
|
||||||
}
|
}
|
||||||
|
|
||||||
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
const audioConstraints = selectedMicrophoneId
|
||||||
|
? { audio: { deviceId: { exact: selectedMicrophoneId } } }
|
||||||
|
: { audio: true };
|
||||||
|
|
||||||
|
const stream = await navigator.mediaDevices.getUserMedia(audioConstraints);
|
||||||
|
|
||||||
audioContext = new (window.AudioContext || window.webkitAudioContext)();
|
audioContext = new (window.AudioContext || window.webkitAudioContext)();
|
||||||
analyser = audioContext.createAnalyser();
|
analyser = audioContext.createAnalyser();
|
||||||
@@ -516,3 +578,22 @@ function updateUI() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
recordButton.addEventListener("click", toggleRecording);
|
recordButton.addEventListener("click", toggleRecording);
|
||||||
|
|
||||||
|
if (microphoneSelect) {
|
||||||
|
microphoneSelect.addEventListener("change", handleMicrophoneChange);
|
||||||
|
}
|
||||||
|
document.addEventListener('DOMContentLoaded', async () => {
|
||||||
|
try {
|
||||||
|
await enumerateMicrophones();
|
||||||
|
} catch (error) {
|
||||||
|
console.log("Could not enumerate microphones on load:", error);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
navigator.mediaDevices.addEventListener('devicechange', async () => {
|
||||||
|
console.log('Device change detected, re-enumerating microphones');
|
||||||
|
try {
|
||||||
|
await enumerateMicrophones();
|
||||||
|
} catch (error) {
|
||||||
|
console.log("Error re-enumerating microphones:", error);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import logging
|
import logging
|
||||||
import importlib.resources as resources
|
import importlib.resources as resources
|
||||||
|
import base64
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -12,6 +13,60 @@ def get_web_interface_html():
|
|||||||
logger.error(f"Error loading web interface HTML: {e}")
|
logger.error(f"Error loading web interface HTML: {e}")
|
||||||
return "<html><body><h1>Error loading interface</h1></body></html>"
|
return "<html><body><h1>Error loading interface</h1></body></html>"
|
||||||
|
|
||||||
|
def get_inline_ui_html():
|
||||||
|
"""Returns the complete web interface HTML with all assets embedded in a single call."""
|
||||||
|
try:
|
||||||
|
with resources.files('whisperlivekit.web').joinpath('live_transcription.html').open('r', encoding='utf-8') as f:
|
||||||
|
html_content = f.read()
|
||||||
|
with resources.files('whisperlivekit.web').joinpath('live_transcription.css').open('r', encoding='utf-8') as f:
|
||||||
|
css_content = f.read()
|
||||||
|
with resources.files('whisperlivekit.web').joinpath('live_transcription.js').open('r', encoding='utf-8') as f:
|
||||||
|
js_content = f.read()
|
||||||
|
|
||||||
|
# SVG files
|
||||||
|
with resources.files('whisperlivekit.web').joinpath('src', 'system_mode.svg').open('r', encoding='utf-8') as f:
|
||||||
|
system_svg = f.read()
|
||||||
|
system_data_uri = f"data:image/svg+xml;base64,{base64.b64encode(system_svg.encode('utf-8')).decode('utf-8')}"
|
||||||
|
with resources.files('whisperlivekit.web').joinpath('src', 'light_mode.svg').open('r', encoding='utf-8') as f:
|
||||||
|
light_svg = f.read()
|
||||||
|
light_data_uri = f"data:image/svg+xml;base64,{base64.b64encode(light_svg.encode('utf-8')).decode('utf-8')}"
|
||||||
|
with resources.files('whisperlivekit.web').joinpath('src', 'dark_mode.svg').open('r', encoding='utf-8') as f:
|
||||||
|
dark_svg = f.read()
|
||||||
|
dark_data_uri = f"data:image/svg+xml;base64,{base64.b64encode(dark_svg.encode('utf-8')).decode('utf-8')}"
|
||||||
|
|
||||||
|
# Replace external references
|
||||||
|
html_content = html_content.replace(
|
||||||
|
'<link rel="stylesheet" href="/web/live_transcription.css" />',
|
||||||
|
f'<style>\n{css_content}\n</style>'
|
||||||
|
)
|
||||||
|
|
||||||
|
html_content = html_content.replace(
|
||||||
|
'<script src="/web/live_transcription.js"></script>',
|
||||||
|
f'<script>\n{js_content}\n</script>'
|
||||||
|
)
|
||||||
|
|
||||||
|
# Replace SVG references
|
||||||
|
html_content = html_content.replace(
|
||||||
|
'<img src="/web/src/system_mode.svg" alt="" />',
|
||||||
|
f'<img src="{system_data_uri}" alt="" />'
|
||||||
|
)
|
||||||
|
|
||||||
|
html_content = html_content.replace(
|
||||||
|
'<img src="/web/src/light_mode.svg" alt="" />',
|
||||||
|
f'<img src="{light_data_uri}" alt="" />'
|
||||||
|
)
|
||||||
|
|
||||||
|
html_content = html_content.replace(
|
||||||
|
'<img src="/web/src/dark_mode.svg" alt="" />',
|
||||||
|
f'<img src="{dark_data_uri}" alt="" />'
|
||||||
|
)
|
||||||
|
|
||||||
|
return html_content
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error creating embedded web interface: {e}")
|
||||||
|
return "<html><body><h1>Error loading embedded interface</h1></body></html>"
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
||||||
@@ -28,6 +83,6 @@ if __name__ == '__main__':
|
|||||||
|
|
||||||
@app.get("/")
|
@app.get("/")
|
||||||
async def get():
|
async def get():
|
||||||
return HTMLResponse(get_web_interface_html())
|
return HTMLResponse(get_inline_ui_html())
|
||||||
|
|
||||||
uvicorn.run(app=app)
|
uvicorn.run(app=app)
|
||||||
|
|||||||
Reference in New Issue
Block a user