Kotoba-Whisper is supported in the Hugging Face 🤗 Transformers library from version 4.39 onwards. To run the model, first
install the latest version of Transformers.
pip install --upgrade pippip install --upgrade transformers accelerate
The model can be used with the pipeline
class to transcribe short-form audio files (< 30-seconds) as follows:
import torchfrom transformers import pipelinefrom datasets import load_dataset # configmodel_id = "kotoba-tech/kotoba-whisper-v2.0"torch_dtype = torch.bfloat16 if torch.cuda.is_available() else torch.float32device = "cuda:0" if torch.cuda.is_available() else "cpu"model_kwargs = {"attn_implementation": "sdpa"} if torch.cuda.is_available() else {}generate_kwargs = {"language": "ja", "task": "transcribe"} # load modelpipe = pipeline( "automatic-speech-recognition", model=model_id, torch_dtype=torch_dtype, device=device, model_kwargs=model_kwargs) # load sample audiodataset = load_dataset("japanese-asr/ja_asr.reazonspeech_test", split="test")sample = dataset[0]["audio"] # run inferenceresult = pipe(sample, generate_kwargs=generate_kwargs)print(result["text"])
- To transcribe a local audio file, simply pass the path to your audio file when you call the pipeline (make sure the audio is sampled in 16kHz):
- result = pipe(sample, generate_kwargs=generate_kwargs)+ result = pipe("audio.mp3", generate_kwargs=generate_kwargs)
- For segment-level timestamps, pass the argument
return_timestamps=True and return the "chunks" output:
result = pipe(sample, return_timestamps=True, generate_kwargs=generate_kwargs)print(result["chunks"])
Sequential Long-Form: Kotoba-whisper is designed to be compatible with OpenAI's sequential long-form transcription algorithm. This algorithm uses a sliding window for buffered
inference of long audio files (> 30-seconds), and returns more accurate transcriptions compared to the chunked long-form algorithm.
As default, if long audio files are passed to the model, it will transcribes with the sequential long-form transcription.
The sequential long-form algorithm should be used in either of the following scenarios:
- Transcription accuracy is the most important factor, and latency is less of a consideration
- You are transcribing batches of long audio files, in which case the latency of sequential is comparable to chunked, while being up to 0.5% WER more accurate
If you are transcribing single long audio files and latency is the most important factor, you should use the chunked algorithm
described below. For a detailed explanation of the different algorithms, refer to Sections 5 of
the Distil-Whisper paper. The pipeline
class can be used to transcribe long audio files with the sequential algorithm as follows:
This algorithm should be used when a single large audio file is being transcribed and the fastest possible inference is required. In such circumstances,
the chunked algorithm is up to 9x faster than OpenAI's sequential long-form implementation (see Table 7 of the Distil-Whisper paper).
To enable chunking, pass the chunk_length_s parameter to the pipeline. For distil-large-v3, a chunk length of 25-seconds
is optimal. To activate batching over long audio files, pass the argument batch_size:
import torchfrom transformers import pipelinefrom datasets import load_dataset # configmodel_id = "kotoba-tech/kotoba-whisper-v2.0"torch_dtype = torch.bfloat16 if torch.cuda.is_available() else torch.float32device = "cuda:0" if torch.cuda.is_available() else "cpu"model_kwargs = {"attn_implementation": "sdpa"} if torch.cuda.is_available() else {}generate_kwargs = {"language": "ja", "task": "transcribe"} # load modelpipe = pipeline( "automatic-speech-recognition", model=model_id, torch_dtype=torch_dtype, device=device, model_kwargs=model_kwargs, batch_size=16) # load sample audio (concatenate instances to create a long audio)dataset = load_dataset("japanese-asr/ja_asr.reazonspeech_test", split="test")sample = {"array": np.concatenate([i["array"] for i in dataset[:20]["audio"]]), "sampling_rate": dataset[0]['audio']['sampling_rate']} # run inferenceresult = pipe(sample, chunk_length_s=15, generate_kwargs=generate_kwargs)print(result["text"])
Additional Speed & Memory Improvements
You can apply additional speed and memory improvements to further reduce the inference speed and VRAM
requirements. These optimisations primarily target the attention kernel, swapping it from an eager implementation to a
more efficient flash attention version.
Flash Attention 2
We recommend using Flash-Attention 2
if your GPU allows for it. To do so, you first need to install Flash Attention:
pip install flash-attn --no-build-isolation
Then pass attn_implementation="flash_attention_2" to from_pretrained:
- model_kwargs = {"attn_implementation": "sdpa"} if torch.cuda.is_available() else {}+ model_kwargs = {"attn_implementation": "flash_attention_2"} if torch.cuda.is_available() else {}
Model Details
See https://huggingface.co/distil-whisper/distil-large-v3#model-details.
Training
Please refer to https://github.com/kotoba-tech/kotoba-whisper for the model training detail.
Datasets used in distillation and the whole model variations can be found at https://huggingface.co/japanese-asr.
Evaluation
The following code-snippets demonstrates how to evaluate the kotoba-whisper model on the Japanese subset of the CommonVoice 8.0.
First, we need to install the required packages, including 🤗 Datasets to load the audio data, and 🤗 Evaluate to
perform the WER calculation:
pip install --upgrade pippip install --upgrade transformers datasets[audio] evaluate jiwer
Evaluation can then be run end-to-end with the following example:
import torchfrom transformers import pipelinefrom datasets import load_datasetfrom evaluate import loadfrom transformers.models.whisper.english_normalizer import BasicTextNormalizer # model configmodel_id = "kotoba-tech/kotoba-whisper-v2.0"torch_dtype = torch.bfloat16 if torch.cuda.is_available() else torch.float32device = "cuda:0" if torch.cuda.is_available() else "cpu"model_kwargs = {"attn_implementation": "sdpa"} if torch.cuda.is_available() else {}generate_kwargs = {"language": "japanese", "task": "transcribe"}normalizer = BasicTextNormalizer() # data configdataset_name = "japanese-asr/ja_asr.reazonspeech_test"audio_column = 'audio'text_column = 'transcription' # load modelpipe = pipeline( "automatic-speech-recognition", model=model_id, torch_dtype=torch_dtype, device=device, model_kwargs=model_kwargs, batch_size=16) # load the dataset and sample the audio with 16kHzdataset = load_dataset(dataset_name, split="test")transcriptions = pipe(dataset['audio'])transcriptions = [normalizer(i['text']).replace(" ", "") for i in transcriptions]references = [normalizer(i).replace(" ", "") for i in dataset['transcription']] # compute the CER metriccer_metric = load("cer")cer = 100 * cer_metric.compute(predictions=transcriptions, references=references)print(cer)
The huggingface links to the major Japanese ASR datasets for evaluation are summarized at here.
For example, to evaluate the model on JSUT Basic5000, change the dataset_name:
- dataset_name = "japanese-asr/ja_asr.reazonspeech_test"+ dataset_name = "japanese-asr/ja_asr.jsut_basic5000"
Acknowledgements