Views
No views yet
1from transformers import WhisperProcessor, WhisperForConditionalGeneration
2from datasets import load_dataset
3
4# load model and processor
5processor = WhisperProcessor.from_pretrained("Val123val/ru_whisper_small")
6model = WhisperForConditionalGeneration.from_pretrained("Val123val/ru_whisper_small")
7model.config.forced_decoder_ids = None
8
9# load dataset and read audio files
10ds = load_dataset("bond005/sberdevices_golos_10h_crowd", split="validation", token=True)
11sample = ds[0]["audio"]
12input_features = processor(sample["array"], sampling_rate=sample["sampling_rate"], return_tensors="pt").input_features
13
14# generate token ids
15predicted_ids = model.generate(input_features)
16# decode token ids to text
17transcription = processor.batch_decode(predicted_ids, skip_special_tokens=False)
18
19transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)1import torch
2from transformers import pipeline
3from datasets import load_dataset
4
5device = "cuda:0" if torch.cuda.is_available() else "cpu"
6
7pipe = pipeline(
8 "automatic-speech-recognition",
9 model="Val123val/ru_whisper_small",
10 chunk_length_s=30,
11 device=device,
12)
13
14ds = load_dataset("bond005/sberdevices_golos_10h_crowd", split="validation", token=True)
15sample = ds[0]["audio"]
16
17prediction = pipe(sample.copy(), batch_size=8)["text"]
18
19# we can also return timestamps for the predictions
20prediction = pipe(sample.copy(), batch_size=8, return_timestamps=True)["chunks"]1import torch
2from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor
3from transformers import pipeline
4
5device = "cuda:0" if torch.cuda.is_available() else "cpu"
6torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
7
8# load dataset
9dataset = load_dataset("bond005/sberdevices_golos_10h_crowd", split="validation", token=True)
10
11# load model
12model_id = "Val123val/ru_whisper_small"
13
14model = AutoModelForSpeechSeq2Seq.from_pretrained(
15 model_id,
16 torch_dtype=torch_dtype,
17 low_cpu_mem_usage=True,
18 use_safetensors=True,
19 attn_implementation="sdpa",
20)
21model.to(device)
22
23processor = AutoProcessor.from_pretrained(model_id)
24
25# load assistant model
26assistant_model_id = "openai/whisper-tiny"
27
28assistant_model = AutoModelForSpeechSeq2Seq.from_pretrained(
29 assistant_model_id,
30 torch_dtype=torch_dtype,
31 low_cpu_mem_usage=True,
32 use_safetensors=True,
33 attn_implementation="sdpa",
34)
35
36assistant_model.to(device);
37
38# make pipe
39pipe = pipeline(
40 "automatic-speech-recognition",
41 model=model,
42 tokenizer=processor.tokenizer,
43 feature_extractor=processor.feature_extractor,
44 max_new_tokens=128,
45 chunk_length_s=15,
46 batch_size=4,
47 generate_kwargs={"assistant_model": assistant_model},
48 torch_dtype=torch_dtype,
49 device=device,
50)
51
52sample = dataset[0]["audio"]
53result = pipe(sample)
54print(result["text"])