### Install T-one with Demo Features using Poetry
Source: https://github.com/voicekit-team/t-one/blob/main/README.md
Installs the T-one package and its optional demo-related dependencies using Poetry. The `-E demo` flag enables the installation of extras needed for running the web service demo. Requires Poetry to be installed.
```bash
poetry install -E demo
```
--------------------------------
### Install Dependencies and Run Development Server with Make
Source: https://github.com/voicekit-team/t-one/blob/main/README.md
Uses the Makefile to install project dependencies and launch the development web service for T-one. This is a convenient way to set up and run the demo locally, assuming Make is installed.
```bash
make install
make up_dev
```
--------------------------------
### Run T-one Demo Web Service with Uvicorn
Source: https://github.com/voicekit-team/t-one/blob/main/README.md
Starts the T-one demo web service using Uvicorn, a high-performance ASGI server. It binds to all network interfaces on port 8080 and enables hot-reloading for development. This command is used after installing the package with Poetry.
```bash
uvicorn --host 0.0.0.0 --port 8080 tone.demo.website:app --reload
```
--------------------------------
### Load Example Audio and Process with Pipeline
Source: https://context7.com/voicekit-team/t-one/llms.txt
Loads built-in example audio (short or long) and processes it using the StreamingCTCPipeline. This is a basic example demonstrating the end-to-end pipeline flow for offline audio processing.
```python
from tone import StreamingCTCPipeline, read_example_audio
# Load built-in example audio
audio = read_example_audio(long_audio=False) # Short example
audio_long = read_example_audio(long_audio=True) # Long example
# Audio is ready for pipeline processing
pipeline = StreamingCTCPipeline.from_hugging_face()
result = pipeline.forward_offline(audio)
```
--------------------------------
### Server Initialization and Deployment
Source: https://context7.com/voicekit-team/t-one/llms.txt
FastAPI-based server setup for T-One WebSocket service. Demonstrates how to initialize and run the real-time transcription server with uvicorn.
```APIDOC
## POST /server/init
### Description
Initializes and starts the FastAPI server for T-One WebSocket transcription service. Configures the application for real-time audio streaming and transcription.
### Method
POST
### Endpoint
/server/init
### Parameters
#### Request Body
- **host** (string) - Optional - Server host address. Default: "0.0.0.0"
- **port** (integer) - Optional - Server port. Default: 8080
- **workers** (integer) - Optional - Number of worker processes. Default: 1
- **reload** (boolean) - Optional - Enable auto-reload on code changes. Default: false
### Request Example
```python
from fastapi import FastAPI
from tone.demo.website import get_application
app = get_application()
# Run with uvicorn:
# uvicorn tone.demo.website:app --host 0.0.0.0 --port 8080
```
### Command Line Usage
```bash
uvicorn tone.demo.website:app --host 0.0.0.0 --port 8080 --workers 1
```
### Response
#### Success Response (200)
- **status** (string) - Server status: "running", "initialized"
- **url** (string) - WebSocket endpoint URL
- **version** (string) - T-One model version
- **message** (string) - Confirmation message
#### Response Example
```json
{
"status": "running",
"url": "ws://0.0.0.0:8080/api/ws",
"version": "1.0.0",
"message": "T-One ASR service initialized and ready for connections"
}
```
```
--------------------------------
### Clone T-one Repository and Set Up Environment
Source: https://github.com/voicekit-team/t-one/blob/main/README.md
Clones the T-one project repository from GitHub and navigates into the project directory. It also sets up a Python virtual environment for managing project dependencies. This is the initial step for local installation and usage.
```bash
git clone https://github.com/voicekit-team/T-one.git
cd T-one
python -m venv .venv
source .venv/bin/activate # On Windows, use ".venv\Scripts\activate"
```
--------------------------------
### Install Fine-tuning Dependencies (Bash)
Source: https://github.com/voicekit-team/t-one/blob/main/README.md
Installs the T-one package along with its specific dependencies required for model fine-tuning. This command assumes you are using poetry for package management.
```bash
poetry install -E finetune
```
--------------------------------
### Run Docker Container for T-one Demo
Source: https://github.com/voicekit-team/t-one/blob/main/README.md
Starts a Docker container of the T-one service, exposing port 8080 for web access. This allows users to quickly try out the ASR pipeline through a web interface for transcribing audio files or using a microphone. Requires Docker to be installed.
```bash
docker run -it --rm -p 8080:8080 tinkoffcreditsystems/t-one:0.1.0
```
--------------------------------
### Start Microphone Capture and Real-Time Processing
Source: https://github.com/voicekit-team/t-one/blob/main/tone/demo/static/index.html
Initiates microphone access and sets up real-time audio processing pipeline. Captures audio using getUserMedia, creates a ScriptProcessor for chunk-based processing, buffers samples, converts to PCM16, and sends chunks via WebSocket. Includes elapsed time display and backlog monitoring for latency detection.
```JavaScript
async function startMicCapture() {
resetUI();
recordedChunks = [];
isRecordingChunks = true;
dom.micWaveform.style.display = 'block';
if (waveAnimationFrame) cancelAnimationFrame(waveAnimationFrame);
drawWaveform();
dom.timerDisplay.style.display = 'inline-block';
startTime = Date.now();
timerInterval = setInterval(() => {
const elapsed = Math.floor((Date.now() - startTime) / 1000);
const hours = String(Math.floor(elapsed / 3600)).padStart(2, '0');
const minutes = String(Math.floor((elapsed % 3600) / 60)).padStart(2, '0');
const seconds = String(elapsed % 60).padStart(2, '0');
dom.timerDisplay.textContent = `${hours}:${minutes}:${seconds}`;
}, 1000);
try {
mediaStream = await navigator.mediaDevices.getUserMedia({
audio: { echoCancellation: true, noiseSuppression: true },
video: false
});
audioCtx = new (window.AudioContext || window.webkitAudioContext)({ sampleRate: 8000 });
const input = audioCtx.createMediaStreamSource(mediaStream);
processor = (audioCtx.createScriptProcessor || audioCtx.createJavaScriptNode).call(audioCtx, 1024, 1, 1);
input.connect(processor);
processor.connect(audioCtx.destination);
initWebSocket();
let buffer8k = [];
processor.onaudioprocess = (ev) => {
const data = ev.inputBuffer.getChannelData(0);
buffer8k.push(...data);
if (isRecordingChunks) recordedChunks.push(new Float32Array(data));
updateWaveformData(data);
while (buffer8k.length >= CHUNK_SAMPLES) {
const chunk = buffer8k.slice(0, CHUNK_SAMPLES);
buffer8k = buffer8k.slice(CHUNK_SAMPLES);
const pcm16 = floatToPCM16(chunk);
ws.send(new Uint8Array(pcm16.buffer));
outstanding++;
}
};
dom.startRecBtn.disabled = true;
dom.stopRecBtn.disabled = false;
backlogChk = setInterval(() => {
const lagSec = outstanding * 0.3;
if (lagSec > MAX_BACKLOG_SEC) {
stopMicCapture(`The server is more than ${MAX_BACKLOG_SEC} s behind the real-time stream.`);
}
}, 1000);
} catch (err) {
alert('Unable to access microphone: ' + err.message);
console.error(err);
}
}
```
--------------------------------
### Perform Offline Audio Recognition with T-one Python SDK
Source: https://github.com/voicekit-team/t-one/blob/main/README.md
Demonstrates how to perform offline audio recognition using the T-one Python SDK. It loads an audio file (either an example or a custom one) and then uses the `StreamingCTCPipeline` to transcribe it. Requires the T-one Python package to be installed.
```python
from tone import StreamingCTCPipeline, read_audio, read_example_audio
audio = read_example_audio() # or read_audio("your_audio.flac")
pipeline = StreamingCTCPipeline.from_hugging_face()
print(pipeline.forward_offline(audio)) # run offline recognition
```
--------------------------------
### Launch Triton Inference Server with Docker
Source: https://github.com/voicekit-team/t-one/blob/main/docs/triton_inference_server.md
Starts Triton Inference Server container with model repository mounted and all required inference ports exposed (8000 for HTTP, 8001 for gRPC, 8002 for metrics). Automatically loads all models from the mounted models directory.
```bash
docker run --rm \
-p 8000:8000 -p 8001:8001 -p 8002:8002 \
-v $(pwd)/models:/models \
nvcr.io/nvidia/tritonserver:25.06-py3 \
tritonserver --model-repository=/models
```
--------------------------------
### Start Streaming Upload via WebSocket
Source: https://github.com/voicekit-team/t-one/blob/main/tone/demo/static/index.html
Initiates chunked audio upload over WebSocket connection with backpressure handling. Manages outstanding requests to prevent server overload, sends PCM data in chunks, and processes incoming transcript messages. Handles flow control using a counter system and adjusts progress display.
```JavaScript
function startStreamingUpload() {
if (!pcmBytes) return;
dom.processBtn.disabled = true;
setProgress(0);
transcripts = [];
dom.transcriptList.innerHTML = '';
recordedChunks = [];
const intPCM = new Int16Array(pcmBytes.buffer);
const floatPCM = new Float32Array(intPCM.length);
for (let i = 0; i < intPCM.length; i++) {
floatPCM[i] = intPCM[i] / 32768.0;
}
for (let i = 0; i < floatPCM.length; i += CHUNK_SAMPLES) {
recordedChunks.push(floatPCM.slice(i, i + CHUNK_SAMPLES));
}
ws = new WebSocket((location.protocol === 'https:' ? 'wss://' : 'ws://') + location.host + '/api/ws');
ws.binaryType = 'arraybuffer';
let offset = 0, sent = 0, outstanding = 1;
const pushCompleted = { val: false };
const pushChunks = () => {
if (pushCompleted.val) return;
while (outstanding < MAX_OUT) {
if (offset >= pcmBytes.length) {
pushCompleted.val = true;
ws.send(new Uint8Array());
break;
}
ws.send(pcmBytes.slice(offset, offset + CHUNK_BYTES));
offset += CHUNK_BYTES;
sent++;
outstanding++;
setProgress((sent / totalChunks) * 100);
}
};
ws.onmessage = (ev) => {
try {
const msg = JSON.parse(ev.data);
if (msg.event === 'ready') {
outstanding = Math.max(outstanding - 1, 0);
pushChunks();
} else if (msg.event === 'transcript') {
appendTranscript(msg.phrase);
}
} catch (err) {
console.error('Bad JSON', err);
}
};
ws.onclose = () => {
dom.processBtn.disabled = false;
setProgress(100);
};
ws.onerror = (e) => console.error('WebSocket error', e);
ws.onopen = () => pushChunks();
}
```
--------------------------------
### Microphone Input Handling
Source: https://github.com/voicekit-team/t-one/blob/main/tone/demo/static/index.html
Attaches event listeners to start and stop microphone capture. It assumes the existence of `startMicCapture` and `stopMicCapture` functions, which are not defined in this snippet.
```javascript
dom.startRecBtn.addEventListener('click', startMicCapture);
dom.stopRecBtn.addEventListener('click', () => stopMicCapture());
```
--------------------------------
### WebSocket Server for Real-time Transcription (Python)
Source: https://context7.com/voicekit-team/t-one/llms.txt
This Python snippet shows the server-side implementation of a FastAPI-based WebSocket endpoint for real-time audio streaming and transcription. It imports the necessary FastAPI components and a function to get the application, setting up the server to listen for WebSocket connections. The comment indicates how to run the server using uvicorn.
```python
# Server implementation (tone/demo/website.py)
from fastapi import FastAPI, WebSocket
from tone.demo.website import get_application
# Start server
app = get_application()
# Run with: uvicorn tone.demo.website:app --host 0.0.0.0 --port 8080
```
--------------------------------
### Simulate Streaming Audio with read_stream_example_audio
Source: https://context7.com/voicekit-team/t-one/llms.txt
Generates audio chunks from example files to simulate a streaming audio source. This function is useful for testing and development of streaming audio processing pipelines. It iterates over audio chunks and processes them with a pipeline, printing recognized phrases as they become available.
```python
from tone import StreamingCTCPipeline, read_stream_example_audio
pipeline = StreamingCTCPipeline.from_hugging_face()
state = None
# Iterate over audio chunks (2400 samples each)
for audio_chunk in read_stream_example_audio(long_audio=False):
new_phrases, state = pipeline.forward(audio_chunk, state)
# Process phrases as they become available
if new_phrases:
for phrase in new_phrases:
print(f"[{phrase.start_time:.2f}s] {phrase.text}")
# Don't forget to finalize
final_phrases, _ = pipeline.finalize(state)
for phrase in final_phrases:
print(f"[{phrase.start_time:.2f}s] {phrase.text}")
```
--------------------------------
### Offline audio transcription with StreamingCTCPipeline
Source: https://context7.com/voicekit-team/t-one/llms.txt
Process complete audio files in offline mode without maintaining external state. Automatically chunks audio and returns all detected phrases with precise start and end timestamps in seconds.
```python
from tone import StreamingCTCPipeline, read_audio
# Load pipeline
pipeline = StreamingCTCPipeline.from_hugging_face()
# Load audio file (automatically resampled to 8kHz mono)
audio = read_audio("speech.wav") # Returns numpy array of int32
# Process entire audio
phrases = pipeline.forward_offline(audio)
# Access transcription results
for phrase in phrases:
print(f"[{phrase.start_time:.2f}s - {phrase.end_time:.2f}s] {phrase.text}")
# Example output:
# [0.33s - 2.45s] добрый день
# [3.12s - 5.87s] как я могу вам помочь
```
--------------------------------
### Transcript Appending and Audio Blob Creation
Source: https://github.com/voicekit-team/t-one/blob/main/tone/demo/static/index.html
Appends a transcript object (containing text, start time, and end time) to a list and creates an audio blob from recorded chunks for playback. It generates a play button for each transcript entry.
```javascript
function appendTranscript(obj) {
transcripts.push(obj);
const { text, start_time, end_time } = obj;
const li = document.createElement('li');
li.className = 'list-group-item d-flex justify-content-between align-items-start';
const audioBlob = createAudioBlobFromChunks(recordedChunks, start_time, end_time);
const audioUrl = URL.createObjectURL(audioBlob);
li.innerHTML = `
${text}
${start_time.toFixed(2)} – ${end_time.toFixed(2)} s
`;
dom.transcriptList.appendChild(li);
li.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
}
```
--------------------------------
### Initialize Trainer for T-one Model Training
Source: https://github.com/voicekit-team/t-one/blob/main/examples/finetune_example.ipynb
Sets up the `Trainer` object, which orchestrates the fine-tuning process. It requires the model, data collator, training arguments, a function to compute metrics, datasets, and the tokenizer's feature extractor. This prepares the model for the training execution.
```python
from transformers import Trainer
trainer = Trainer(
model=model,
data_collator=data_collator,
args=training_args,
compute_metrics=compute_metrics,
train_dataset=asr_dataset["train"],
eval_dataset=asr_dataset["validation"],
tokenizer=processor.feature_extractor,
)
```
--------------------------------
### Initialize Wav2Vec2Processor
Source: https://github.com/voicekit-team/t-one/blob/main/examples/finetune_example.ipynb
Initializes a Wav2Vec2Processor by combining a Wav2Vec2FeatureExtractor and a Wav2Vec2CTCTokenizer. The feature extractor handles audio preprocessing like padding and normalization, while the tokenizer handles text processing.
```python
from transformers import Wav2Vec2FeatureExtractor
from transformers import Wav2Vec2Processor
feature_extractor = Wav2Vec2FeatureExtractor(
feature_size=1,
sampling_rate=SAMPLE_RATE,
padding_value=0.0,
return_attention_mask=False,
do_normalize=False,
)
processor = Wav2Vec2Processor(feature_extractor=feature_extractor, tokenizer=tokenizer)
```
--------------------------------
### Initialize Wav2Vec2CTCTokenizer
Source: https://github.com/voicekit-team/t-one/blob/main/examples/finetune_example.ipynb
Initializes a Wav2Vec2CTCTokenizer from a pre-trained model. It sets the padding token to '[PAD]' and the word delimiter token to '|', which are common configurations for CTC-based models.
```python
from transformers import Wav2Vec2CTCTokenizer
tokenizer = Wav2Vec2CTCTokenizer.from_pretrained(
"t-tech/T-one", pad_token="[PAD]", word_delimiter_token="|"
)
```
--------------------------------
### WebSocket Initialization and Message Handling - JavaScript
Source: https://github.com/voicekit-team/t-one/blob/main/tone/demo/static/index.html
Initializes WebSocket connection for real-time audio transcription and sets up message event handlers. Processes 'ready' and 'transcript' events from the server, manages outstanding request count, and handles connection errors.
```JavaScript
function initWebSocket() {
ws = new WebSocket((location.protocol === 'https:' ? 'wss://' : 'ws://') + location.host + '/api/ws');
ws.binaryType = 'arraybuffer';
outstanding = 0;
ws.onmessage = (ev) => {
try {
const msg = JSON.parse(ev.data);
if (msg.event === 'ready') {
outstanding = Math.max(outstanding - 1, 0);
} else if (msg.event === 'transcript') {
appendTranscript(msg.phrase);
}
} catch (err) {
console.error('Bad JSON', err);
}
};
ws.onerror = (e) => console.error('WebSocket error', e);
ws.onclose = () => console.log('WebSocket closed');
}
```
--------------------------------
### Define Training Arguments for T-one Fine-Tuning
Source: https://github.com/voicekit-team/t-one/blob/main/examples/finetune_example.ipynb
Configures essential parameters for the training process, such as output directory, batch sizes, learning rate, and saving/logging strategies. These arguments are crucial for optimizing fine-tuning performance. Dependencies include the `transformers` library.
```python
from transformers import TrainingArguments
training_args = TrainingArguments(
output_dir = "tone_ctc",
per_device_train_batch_size = 64,
per_device_eval_batch_size = 64,
dataloader_num_workers = 12,
eval_on_start = True,
num_train_epochs = 10,
bf16 = True,
lr_scheduler_type = "linear",
eval_strategy = "epoch",
save_strategy = "epoch",
logging_strategy = "epoch",
learning_rate = 5e-5,
weight_decay = 1e-6,
warmup_ratio = 0.05,
save_total_limit = 2,
)
```
--------------------------------
### Load T-one Dataset from Hugging Face
Source: https://github.com/voicekit-team/t-one/blob/main/examples/finetune_example.ipynb
Loads the 'Vikhrmodels/ToneBooks' dataset from the Hugging Face Hub, specifying the audio and text columns. This dataset contains recordings of books with labelled tone and timbre, but only audio and transcriptions are used.
```python
# This will save the dataset to the cache folder
asr_dataset = load_from_huggingface(
dataset_name="Vikhrmodels/ToneBooks",
audio_column="audio",
text_column="text",
)
```
--------------------------------
### Extract Audio Segment from Chunks - JavaScript
Source: https://github.com/voicekit-team/t-one/blob/main/tone/demo/static/index.html
Extracts a specific time range audio segment from an array of recorded audio chunks based on start and end times. Calculates byte offsets, iterates through chunks to find relevant data, and combines segments into a contiguous audio buffer.
```JavaScript
function createAudioBlobFromChunks(chunks, startTime, endTime) {
const sampleRate = 8000;
const startSample = Math.floor(startTime * sampleRate);
const endSample = Math.floor(endTime * sampleRate);
let totalSamples = 0, chunkIndex = 0;
while (chunkIndex < chunks.length && totalSamples + chunks[chunkIndex].length <= startSample) {
totalSamples += chunks[chunkIndex].length;
chunkIndex++;
}
let result = [], currentSample = startSample;
while (chunkIndex < chunks.length && currentSample < endSample) {
const chunk = chunks[chunkIndex++];
const available = chunk.length;
```
--------------------------------
### Load Audio Dataset from Local JSON-Lines
Source: https://github.com/voicekit-team/t-one/blob/main/examples/finetune_example.ipynb
Loads an audio dataset from local JSON-lines manifest files. Requires paths to training and validation files, and optionally allows specifying sample rate and column names for audio and text.
```python
from datasets import Audio, load_dataset
def load_from_local(
train_path,
validation_path,
sample_rate=SAMPLE_RATE,
audio_column="audio",
text_column="text",
):
"""
Load audios from a JSON-lines manifest containing the following features in each row: audio and text.
"""
manifest_dict = {"train": train_path, "validation": validation_path}
dataset = load_dataset("json", data_files=manifest_dict)
if text_column != "text":
dataset = dataset.rename_column(text_column, "text")
if audio_column != "audio":
dataset = dataset.rename_column(audio_column, "audio")
dataset = dataset.cast_column(audio_column, Audio(sampling_rate=sample_rate))
return dataset
```
--------------------------------
### Launch Triton SDK Container for perf_analyzer
Source: https://github.com/voicekit-team/t-one/blob/main/docs/performance_testing.md
Launches the NVIDIA Triton Inference Server SDK container with Python 3 support, necessary for running the perf_analyzer tool. It's important to use a specific SDK version (e.g., 24.02) to avoid known issues like Out-Of-Memory errors in newer versions.
```bash
docker run --rm -it --net host \
nvcr.io/nvidia/tritonserver:24.02-py3-sdk
```
--------------------------------
### Build Docker Image and Run T-one
Source: https://github.com/voicekit-team/t-one/blob/main/README.md
Manually builds a Docker image for T-one from the Dockerfile in the repository and then runs it, mapping port 8080 for web access. This provides an alternative to using the pre-built image from Docker Hub. Requires Docker and the T-one source code.
```bash
docker build -t t-one .
docker run -it --rm -p 8080:8080 t-one
```
--------------------------------
### Hugging Face Trainer for Fine-tuning (Python)
Source: https://context7.com/voicekit-team/t-one/llms.txt
This code illustrates how to set up the Hugging Face Trainer for fine-tuning the T-one model. It defines the necessary TrainingArguments, including output directory, number of epochs, batch size, and learning rate. The commented-out section shows the instantiation of the Trainer with the model, arguments, dataset, and data collator, ready for the training process.
```python
from transformers import Trainer, TrainingArguments
training_args = TrainingArguments(
output_dir="./results",
num_train_epochs=3,
per_device_train_batch_size=8,
learning_rate=5e-5
)
# trainer = Trainer(
# model=model,
# args=training_args,
# train_dataset=train_dataset,
# data_collator=data_collator
# )
# trainer.train()
```
--------------------------------
### Download ONNX model for Triton ONNX Runtime deployment
Source: https://github.com/voicekit-team/t-one/blob/main/docs/triton_inference_server.md
Downloads the acoustic model directly into the Triton model repository structure for ONNX Runtime backend. Places the model in the versioned directory (1) as required by Triton's model repository format.
```bash
python -m tone download $(pwd)/models/streaming_acoustic/1 --only-acoustic
```
--------------------------------
### Load Audio Dataset from Hugging Face Hub
Source: https://github.com/voicekit-team/t-one/blob/main/examples/finetune_example.ipynb
Loads an Automatic Speech Recognition (ASR) dataset directly from the Hugging Face Hub. Allows specifying dataset name, sample rate, and column mappings. It assumes the dataset structure is compatible or can be adapted.
```python
from datasets import Audio, load_dataset
def load_from_huggingface(
dataset_name,
sample_rate=SAMPLE_RATE,
audio_column="audio",
text_column="text",
**kwargs
):
"""
Load an ASR dataset from huggingface.
You might need to change this function if you use a custom dataset.
"""
dataset = load_dataset(dataset_name, **kwargs)
dataset = dataset.select_columns([audio_column, text_column])
if text_column != "text":
dataset = dataset.rename_column(text_column, "text")
if audio_column != "audio":
dataset = dataset.rename_column(audio_column, "audio")
dataset = dataset.cast_column(audio_column, Audio(sampling_rate=sample_rate))
return dataset
```
--------------------------------
### Load pretrained T-One model for CTC training
Source: https://github.com/voicekit-team/t-one/blob/main/examples/finetune_example.ipynb
Loads a pretrained T-One checkpoint configured for Connectionist Temporal Classification (CTC) speech recognition. Moves model to available GPU device or CPU. Ensures tokenizer pad token ID matches model's blank token for CTC loss.
```python
from tone.training.model_wrapper import ToneForCTC
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = ToneForCTC.from_pretrained("t-tech/T-one").to(device)
```
--------------------------------
### Define Audio Padding Constants
Source: https://github.com/voicekit-team/t-one/blob/main/examples/finetune_example.ipynb
Defines constants for audio padding parameters used during training. Includes sample rate, left/right padding in milliseconds, and calculated padding sizes in samples.
```python
# Sample rate used in the base model
SAMPLE_RATE = 8000
# Padding added to each audio during training to improve performance
LEFT_PAD_MS = 300
RIGHT_PAD_MS = 300
LEFT_PAD_SIZE = round(SAMPLE_RATE * LEFT_PAD_MS * 1e-3)
RIGHT_PAD_SIZE = round(SAMPLE_RATE * RIGHT_PAD_MS * 1e-3)
```
--------------------------------
### Initialize CTC data collator for batch padding
Source: https://github.com/voicekit-team/t-one/blob/main/examples/finetune_example.ipynb
Creates a data collator that pads audio and text sequences to maximum batch length separately, masking padding tokens with -100 so they are excluded from loss computation. Required for Hugging Face Trainer integration.
```python
from tone.training.data_collator import DataCollatorCTCWithPadding
data_collator = DataCollatorCTCWithPadding(processor=processor, padding=True)
```
--------------------------------
### Configure CPU processors and compile regex pattern for text normalization
Source: https://github.com/voicekit-team/t-one/blob/main/examples/finetune_example.ipynb
Sets up multiprocessing configuration using available CPU count and creates a regex pattern to extract Cyrillic characters from text. This is used for parallel dataset processing and cleaning text input for the speech recognition model.
```python
NUM_PROC = os.cpu_count()
REG = re.compile("[а-яё]+")
```
--------------------------------
### Prepare ASR dataset with audio padding and text tokenization
Source: https://github.com/voicekit-team/t-one/blob/main/examples/finetune_example.ipynb
Processes audio batches by extracting audio arrays, applying padding on both sides to improve model performance, and tokenizing normalized text. Returns batch with padded input values, input lengths, and label token IDs for CTC loss computation.
```python
def prepare_dataset(batch):
audio = batch["audio"]
audio_array = processor(audio["array"], sampling_rate=audio["sampling_rate"]).input_values[0]
# Add magic padding on both sides to improve model performance
batch["input_values"] = np.pad(audio_array, (LEFT_PAD_SIZE, RIGHT_PAD_SIZE), mode="constant")
batch["input_lengths"] = len(batch["input_values"])
text = " ".join(REG.findall(batch["text"].lower()))
batch["labels"] = processor(text=text).input_ids
return batch
```
--------------------------------
### Initialize StreamingCTCPipeline with decoder selection
Source: https://context7.com/voicekit-team/t-one/llms.txt
Creates a complete ASR pipeline with acoustic model, logprob splitter, and decoder. Supports loading from Hugging Face Hub or local storage with choice between beam search (more accurate) or greedy (faster) decoding strategies.
```python
from tone import StreamingCTCPipeline, DecoderType
# Load pipeline with beam search decoder (default, more accurate)
pipeline = StreamingCTCPipeline.from_hugging_face(decoder_type=DecoderType.BEAM_SEARCH)
# Or use greedy decoder (faster, less accurate)
pipeline = StreamingCTCPipeline.from_hugging_face(decoder_type=DecoderType.GREEDY)
# Load from local directory
pipeline = StreamingCTCPipeline.from_local(
dir_path="./models",
decoder_type=DecoderType.BEAM_SEARCH
)
# Download artifacts to local folder for offline use
StreamingCTCPipeline.download_from_hugging_face(
dir_path="./models",
only_acoustic=False # Set True to skip language model download
)
```
--------------------------------
### Download ONNX acoustic model using tone CLI
Source: https://github.com/voicekit-team/t-one/blob/main/docs/triton_inference_server.md
Downloads the pre-trained acoustic model in ONNX format to the specified models directory. The --only-acoustic flag ensures only the acoustic component is downloaded, excluding other model components.
```bash
python -m tone download $(pwd)/models/model.onnx --only-acoustic
```
--------------------------------
### Prepare Training Data and Compute CTC Loss (PyTorch)
Source: https://context7.com/voicekit-team/t-one/llms.txt
This snippet demonstrates preparing input data for a PyTorch model, including audio samples and their lengths, and labels for CTC loss computation. It then performs a forward pass to calculate the loss and output logits, showing how to access and print these values. This is a foundational step for training or evaluating the T-one model.
```python
import torch
# Prepare training data
input_values = torch.randn(2, 24000) # Batch of 2 audio samples
input_lengths = torch.tensor([24000, 20000])
labels = torch.tensor([[0, 1, 2, -100], [3, 4, -100, -100]])
# Forward pass with CTC loss computation
output = model.forward(
input_values=input_values,
input_lengths=input_lengths,
labels=labels
)
print(f"Loss: {output.loss.item()}")
print(f"Logits shape: {output.logits.shape}")
```
--------------------------------
### Triton model repository directory structure
Source: https://github.com/voicekit-team/t-one/blob/main/docs/triton_inference_server.md
Required directory layout for Triton Inference Server model repository. Contains model artifact (ONNX or TensorRT engine), version subdirectory, and config.pbtxt configuration file for model metadata and runtime settings.
```text
models
└── streaming_acoustic
├── 1
│ └── model.onnx or model.plan
└── config.pbtxt
```
--------------------------------
### Execute T-one Model Training
Source: https://github.com/voicekit-team/t-one/blob/main/examples/finetune_example.ipynb
Initiates the fine-tuning process for the T-one model using the configured `Trainer` object. This function executes the training loop, iterating through epochs and performing evaluations as specified in the `TrainingArguments`. The input is the `trainer` object, and the output is the training process itself.
```python
trainer.train()
```
--------------------------------
### Load audio file with read_audio utility
Source: https://context7.com/voicekit-team/t-one/llms.txt
Load audio files in common formats (WAV, MP3, FLAC, OGG) and automatically resample to 8kHz mono int32 format required by the ASR pipeline for consistent input handling.
```python
from tone import read_audio, read_example_audio
import numpy as np
# Load audio from file path
audio = read_audio("speech.flac")
print(f"Audio shape: {audio.shape}, dtype: {audio.dtype}")
# Output: Audio shape: (48000,), dtype: int32
```
--------------------------------
### Streaming ASR Pipeline Usage (Python)
Source: https://github.com/voicekit-team/t-one/blob/main/README.md
Demonstrates how to use the StreamingCTCPipeline for real-time ASR. It processes audio chunks from a stream, prints detected phrases, and finalizes the pipeline to retrieve any remaining phrases.
```python
from tone import StreamingCTCPipeline, read_stream_example_audio
pipeline = StreamingCTCPipeline.from_hugging_face()
state = None # Current state of the ASR pipeline (None - initial)
for audio_chunk in read_stream_example_audio(): # Use any source of audio chunks
new_phrases, state = pipeline.forward(audio_chunk, state)
print(new_phrases)
# Finalize the pipeline and get the remaining phrases
new_phrases, _ = pipeline.finalize(state)
print(new_phrases)
```
--------------------------------
### Application State Variables - JavaScript
Source: https://github.com/voicekit-team/t-one/blob/main/tone/demo/static/index.html
Declares and initializes global state variables for managing audio recording, WebSocket communication, timers, and transcription data. These variables track recording chunks, audio context, WebSocket connection status, and pending audio processing requests.
```JavaScript
let waveAnimationFrame = null;
let waveAudioData = new Float32Array(dom.micWaveform.width);
let recordedChunks = [];
let isRecordingChunks = false;
let timerInterval = null;
let startTime = null;
let pcmBytes;
let totalChunks = 0;
let transcripts = [];
let mediaStream = null;
let audioCtx = null;
let processor = null;
let ws = null;
let outstanding = 0;
let backlogChk = null;
```
--------------------------------
### Model Fine-Tuning with Hugging Face Trainer
Source: https://context7.com/voicekit-team/t-one/llms.txt
Enables domain adaptation of T-One model using Hugging Face Transformers Trainer API. Supports custom training configurations for call-center specific data while maintaining streaming architecture.
```APIDOC
## POST /model/train
### Description
Initiates fine-tuning of the T-One model using Hugging Face Trainer. Supports domain adaptation for specialized vocabularies and acoustic conditions while preserving the streaming inference capabilities.
### Method
POST
### Endpoint
/model/train
### Parameters
#### Request Body
- **model** (ToneForCTC) - Required - Pre-initialized T-One model wrapper
- **train_dataset** (Dataset) - Required - Training dataset with audio and transcription pairs
- **output_dir** (string) - Required - Directory to save fine-tuned model. Example: "./results"
- **num_train_epochs** (integer) - Optional - Number of training epochs. Default: 3
- **per_device_train_batch_size** (integer) - Optional - Batch size per device. Default: 8
- **learning_rate** (float) - Optional - Learning rate for optimizer. Default: 5e-5
- **data_collator** (callable) - Optional - Custom data collator function for batch preparation
### Request Example
```python
from transformers import Trainer, TrainingArguments
training_args = TrainingArguments(
output_dir="./results",
num_train_epochs=3,
per_device_train_batch_size=8,
learning_rate=5e-5
)
trainer = Trainer(
model=model,
args=training_args,
train_dataset=train_dataset,
data_collator=data_collator
)
trainer.train()
```
### Response
#### Success Response (200)
- **status** (string) - Training status: "completed", "in_progress"
- **model_path** (string) - Path to saved fine-tuned model
- **final_loss** (float) - Final training loss value
- **training_time** (float) - Total training duration in seconds
#### Response Example
```json
{
"status": "completed",
"model_path": "./results/checkpoint-final",
"final_loss": 1.245,
"training_time": 3600.5
}
```
```
--------------------------------
### Audio File Input Change Handler
Source: https://github.com/voicekit-team/t-one/blob/main/tone/demo/static/index.html
Processes an audio file selected via an input element. It resamples the audio to 8 kHz PCM format, creates a WAV blob, sets it as the source for an audio player, and enables a process button.
```javascript
dom.fileInput.addEventListener('change', async (e) => {
const file = e.target.files[0];
if (!file) return;
resetUI();
showOverlay('Decoding & resampling to 8 kHz...', true);
try {
pcmBytes = await resampleToPCM(file, 8000);
totalChunks = Math.ceil(pcmBytes.length / CHUNK_BYTES);
const wavBlob = pcmToWav(pcmBytes, 8000);
dom.audioPlayer.src = URL.createObjectURL(wavBlob);
dom.audioPlayerBox.style.display = 'block';
dom.processBtn.disabled = false;
} catch (err) {
alert('Failed to decode or resample the file: ' + err.message);
console.error(err);
} finally {
hideOverlay();
}
});
```
--------------------------------
### CSS Overlay and Audio Player Box Styling
Source: https://github.com/voicekit-team/t-one/blob/main/tone/demo/static/index.html
Styles modal overlay with semi-transparent black background and high z-index for layering, and defines hidden states for overlay and audio player box elements. Includes centered white text styling for overlay messages.
```css
#overlay {
background: rgba(0, 0, 0, 0.5);
display: none;
z-index: 1050;
}
#audioPlayerBox {
display: none;
}
#overlay .message {
text-align: center;
font-size: 1.5rem;
color: white;
}
```
--------------------------------
### Run trtexec for TensorRT Model Performance Tests
Source: https://github.com/voicekit-team/t-one/blob/main/docs/performance_testing.md
Utilizes the trtexec command-line utility to test the performance of acoustic models converted to the TensorRT format. Key flags control warm-up duration, measurement duration, and averaging of results for stability. Requires a pre-exported TensorRT engine file.
```bash
trtexec --loadEngine= --warmUp=2000 --duration=10 --avgRuns=5
```
--------------------------------
### Run perf_analyzer for ONNX/TensorRT Model Performance Tests
Source: https://github.com/voicekit-team/t-one/blob/main/docs/performance_testing.md
Executes the perf_analyzer tool within the Triton SDK container to benchmark streaming acoustic models. This command is configured for gRPC communication, enables streaming, and specifies parameters for sequence length, measurement mode, request rate, and stability.
```bash
perf_analyzer -u localhost:8001 \
-i grpc \
-a \
-m streaming_acoustic \
--streaming \
--sequence-length=50 \
--measurement-mode=count_windows \
--measurement-request-count=5000 \
--request-rate-range=2048:4096:256 \
--stability-percentage=100 \
--latency-threshold=100
```
--------------------------------
### WebSocket Client for Real-time Transcription (JavaScript)
Source: https://context7.com/voicekit-team/t-one/llms.txt
This JavaScript code demonstrates the client-side usage of the WebSocket API for real-time transcription. It establishes a connection to the ASR service, defines event handlers for connection opening and message receiving, and shows how to send audio chunks and process transcription results. The snippet also illustrates how to signal the end of the stream.
```javascript
// Client-side JavaScript usage
const ws = new WebSocket('ws://localhost:8080/api/ws');
ws.onopen = () => {
console.log('Connected to ASR service');
};
ws.onmessage = (event) => {
const data = JSON.parse(event.data);
if (data.event === 'ready') {
// Server ready for next audio chunk
const audioChunk = new Int16Array(4800); // 300ms at 16kHz
ws.send(audioChunk.buffer);
} else if (data.event === 'transcript') {
// Received transcription
const phrase = data.phrase;
console.log(`[${phrase.start_time}s - ${phrase.end_time}s] ${phrase.text}`);
}
};
// Send empty chunk to signal end of stream
ws.send(new ArrayBuffer(0));
```
--------------------------------
### PCM to WAV Format Conversion - JavaScript
Source: https://github.com/voicekit-team/t-one/blob/main/tone/demo/static/index.html
Converts raw PCM audio bytes into a valid WAV file blob with proper headers and metadata. Constructs RIFF/WAV format with 16-bit mono audio at specified sample rate and returns a Blob for audio playback or download.
```JavaScript
function pcmToWav(pcmBytes, sampleRate) {
const header = new ArrayBuffer(44);
const dv = new DataView(header);
const write = (o, s) => {
for (let i = 0; i < s.length; i++) dv.setUint8(o + i, s.charCodeAt(i));
};
write(0, 'RIFF');
dv.setUint32(4, 36 + pcmBytes.length, true);
write(8, 'WAVEfmt ');
dv.setUint32(16, 16, true);
dv.setUint16(20, 1, true);
dv.setUint16(22, 1, true);
dv.setUint32(24, sampleRate, true);
dv.setUint32(28, sampleRate * 2, true);
dv.setUint16(32, 2, true);
dv.setUint16(34, 16, true);
write(36, 'data');
dv.setUint32(40, pcmBytes.length, true);
return new Blob([header, pcmBytes], { type: 'audio/wav' });
}
```
--------------------------------
### Streaming audio transcription with state management
Source: https://context7.com/voicekit-team/t-one/llms.txt
Process audio chunks in real-time streaming mode maintaining state across chunks for continuous transcription with low latency. Requires 300ms audio chunks (2400 samples at 8kHz) and supports incremental phrase detection.
```python
from tone import StreamingCTCPipeline
import numpy as np
pipeline = StreamingCTCPipeline.from_hugging_face()
# Initialize state (None for first chunk)
state = None
# Process 300ms audio chunk (2400 samples at 8kHz)
audio_chunk = np.zeros(2400, dtype=np.int32) # Your audio data here
# Forward pass returns new phrases and updated state
new_phrases, state = pipeline.forward(
audio_chunk=audio_chunk,
state=state,
is_last=False # Set True for final chunk
)
# Process detected phrases
for phrase in new_phrases:
print(f"{phrase.text} ({phrase.start_time}s - {phrase.end_time}s)")
# Continue with next chunk using updated state
next_chunk = np.zeros(2400, dtype=np.int32)
new_phrases, state = pipeline.forward(next_chunk, state)
```
--------------------------------
### Configure CUDA and cuDNN for PyTorch
Source: https://github.com/voicekit-team/t-one/blob/main/examples/finetune_example.ipynb
Sets PyTorch backend configurations for CUDA and cuDNN to disable TF32 tensor cores. This can be useful for ensuring consistent behavior or compatibility with specific hardware/software versions.
```python
import torch
torch.backends.cuda.matmul.allow_tf32 = False
torch.backends.cudnn.allow_tf32 = False
```
--------------------------------
### Audio Resampling Helper
Source: https://github.com/voicekit-team/t-one/blob/main/tone/demo/static/index.html
Asynchronous function to resample an audio file to a target sample rate and convert it to PCM format. It reads the file as an ArrayBuffer and returns the raw PCM bytes.
```javascript
async function resampleToPCM(file, targetRate) {
const arrayBuffer = await file.arrayBuffer();
co
```
--------------------------------
### Overlay Display Control - JavaScript
Source: https://github.com/voicekit-team/t-one/blob/main/tone/demo/static/index.html
Manages the visibility and content of an overlay modal with optional spinner animation. Controls message display and loading indicator visibility for user feedback during asynchronous operations.
```JavaScript
function showOverlay(message, showSpinner) {
dom.overlayText.textContent = message;
if (showSpinner) {
dom.overlaySpinner.classList.remove('d-none');
} else {
dom.overlaySpinner.classList.add('d-none');
}
dom.overlay.style.display = 'flex';
}
function hideOverlay() {
dom.overlay.style.display = 'none';
}
```
--------------------------------
### Convert ONNX model to TensorRT engine format
Source: https://github.com/voicekit-team/t-one/blob/main/docs/triton_inference_server.md
Converts downloaded ONNX model to optimized TensorRT engine (.plan file) using NVIDIA's trtexec tool. Requires specifying min/opt/max batch shapes for the signal and state tensors. Performs hardware-specific compilation with optimization level 5 for maximum performance on target GPU.
```bash
docker run --rm -it \
-v $(pwd)/models:/models \
nvcr.io/nvidia/tritonserver:25.06-py3 \
/usr/src/tensorrt/bin/trtexec \
--onnx=/models/model.onnx \
--minShapes=signal:1x2400x1,state:1x219729 \
--optShapes=signal:$BATCH_SIZEx2400x1,state:$BATCH_SIZEx219729 \
--maxShapes=signal:$BATCH_SIZEx2400x1,state:$BATCH_SIZEx219729 \
--builderOptimizationLevel=5 \
--stronglyTyped \
--useSpinWait \
--noDataTransfers \
--saveEngine=/models/model.plan
```
--------------------------------
### BeamSearchCTCDecoder with Language Model
Source: https://context7.com/voicekit-team/t-one/llms.txt
An advanced CTC decoder that uses beam search and integrates with a KenLM language model for more accurate and natural transcriptions. It can be loaded from Hugging Face or a local KenLM binary file. This decoder is recommended for achieving higher transcription quality, especially on longer audio segments.
```python
from tone import BeamSearchCTCDecoder
import numpy as np
# Load decoder with language model from Hugging Face
decoder = BeamSearchCTCDecoder.from_hugging_face()
# Or from local KenLM binary file
decoder = BeamSearchCTCDecoder.from_local("./models/kenlm.bin")
# Logprobs from acoustic model: shape (num_frames, 35)
logprobs = np.random.randn(50, 35).astype(np.float32)
# Decode with beam search (beam_width=200)
text = decoder.forward(logprobs)
print(f"Decoded text: {text}")
# Produces more accurate results than greedy decoding
# Especially for longer utterances
```
--------------------------------
### Apply dataset transformation and filter long sequences
Source: https://github.com/voicekit-team/t-one/blob/main/examples/finetune_example.ipynb
Maps the prepare_dataset function across the dataset using multiprocessing, then filters out sequences longer than 20 seconds to reduce memory consumption. Self-attention models have quadratic memory scaling with sequence length.
```python
asr_dataset = asr_dataset.map(prepare_dataset, remove_columns=["audio", "text"], num_proc=NUM_PROC)
max_input_length_in_sec = 20.0
asr_dataset["train"] = asr_dataset["train"].filter(
lambda x: x < max_input_length_in_sec * processor.feature_extractor.sampling_rate, input_columns=["input_lengths"]
)
```
--------------------------------
### UI Event Listeners for Transcript and Copy
Source: https://github.com/voicekit-team/t-one/blob/main/tone/demo/static/index.html
Sets up event listeners for clearing the transcript list and copying transcript data to the clipboard. The copy functionality provides visual feedback on success.
```javascript
dom.clearTranscriptBtn.addEventListener('click', () => {
dom.transcriptList.innerHTML = '';
transcripts = [];
dom.segmentPlayer.pause();
dom.segmentPlayer.src = '';
dom.segmentPlayer.style.display = 'none';
});
dom.copyBtn.addEventListener('click', () => {
if (transcripts.length === 0) {
alert('Нет данных для копирования. Сначала загрузите аудио или запишите речь.');
return;
}
const jsonStr = JSON.stringify(transcripts, null, 2);
navigator.clipboard.writeText(jsonStr).then(() => {
dom.copyBtn.innerHTML = '';
dom.copyBtn.classList.remove('btn-outline-secondary');
dom.copyBtn.classList.add('btn-success');
setTimeout(() => {
dom.copyBtn.innerHTML = '';
dom.copyBtn.classList.remove('btn-success');
dom.copyBtn.classList.add('btn-outline-secondary');
}, 1500);
}).catch(err => {
console.error('Ошибка копирования в буфер обмена:', err);
alert('Не удалось скопировать данные в буфер обмена.');
});
});
```