-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
34 lines (29 loc) · 1.12 KB
/
Copy pathmodel.py
File metadata and controls
34 lines (29 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import torch
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
MODEL_ID = "openai/whisper-large-v3"
class AudioAnalyzer:
def __init__(self):
self.device = "cuda:0" if torch.cuda.is_available() else "cpu"
self.torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
self.model = AutoModelForSpeechSeq2Seq.from_pretrained(
MODEL_ID,
torch_dtype=self.torch_dtype,
low_cpu_mem_usage=True,
use_safetensors=True,
)
self.model.to(self.device)
self.processor = AutoProcessor.from_pretrained(MODEL_ID)
def transcribe(self, audio_path):
pipe = pipeline(
"automatic-speech-recognition",
model=self.model,
tokenizer=self.processor.tokenizer,
feature_extractor=self.processor.feature_extractor,
max_new_tokens=128,
chunk_length_s=30,
batch_size=16,
return_timestamps=True,
torch_dtype=self.torch_dtype,
device=self.device,
)
return pipe(audio_path)["text"]