How do I get sentence level timestamps?

#18
by teamm - opened

Maybe I am overlooking something, but I am not seeing a way to get sentence level timestamps. With the openai whisper library, when you use "transcribe" you will get a timestamp for each text chunk (not word level, text chunk). I am not seeing an equivalent here. Can you please guide me?

Hello @teamm , try something like this
pipeline("automatic-speech-recognition", model="distil-whisper/distil-large-v2", return_timestamps=True)

Whisper Distillation org

Thanks @DnzzL ! Here's a full code snippet for this. You can see how we change the call to the pipe to add the argument return_timestamps=True:

import torch
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
from datasets import load_dataset


device = "cuda:0" if torch.cuda.is_available() else "cpu"
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32

model_id = "distil-whisper/distil-large-v2"

model = AutoModelForSpeechSeq2Seq.from_pretrained(
    model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True
)
model.to(device)

processor = AutoProcessor.from_pretrained(model_id)

pipe = pipeline(
    "automatic-speech-recognition",
    model=model,
    tokenizer=processor.tokenizer,
    feature_extractor=processor.feature_extractor,
    max_new_tokens=128,
    chunk_length_s=15,
    batch_size=16,
    torch_dtype=torch_dtype,
    device=device,
)

dataset = load_dataset("distil-whisper/librispeech_long", "clean", split="validation")
sample = dataset[0]["audio"]

result = pipe(sample, return_timestamps=True)
print(result["chunks"])

Sign up or log in to comment