-
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathspeech.py
More file actions
38 lines (29 loc) · 1.17 KB
/
speech.py
File metadata and controls
38 lines (29 loc) · 1.17 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
35
36
37
38
import torch
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
from global_variables import SPEECH_MODEL_PATH
def get_speech_transcription(audio_path):
runnning_device = "cuda:0" if torch.cuda.is_available() else "cpu"
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
model = AutoModelForSpeechSeq2Seq.from_pretrained(
SPEECH_MODEL_PATH, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True
)
model.to(runnning_device)
processor = AutoProcessor.from_pretrained(SPEECH_MODEL_PATH)
speech_pipeline = pipeline(
"automatic-speech-recognition",
model=model,
tokenizer=processor.tokenizer,
feature_extractor=processor.feature_extractor,
max_new_tokens=128,
chunk_length_s=20,
batch_size=8,
torch_dtype=torch_dtype,
device=runnning_device,
)
print(f"Transcribing...")
result = speech_pipeline(audio_path, return_timestamps=False)
print(f"Finished Transcribing...")
print(f"{result}")
del speech_pipeline, model, processor
torch.cuda.empty_cache()
return result['text']