-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasr.py
43 lines (31 loc) · 1.04 KB
/
asr.py
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
39
40
41
42
43
from transformers import pipeline
import torch
import os
import nvidia_smi
num_devices = torch.cuda.device_count()
device = -1
nvidia_smi.nvmlInit()
for i in range(0, num_devices):
handle = nvidia_smi.nvmlDeviceGetHandleByIndex(i)
info = nvidia_smi.nvmlDeviceGetMemoryInfo(handle)
total_memory = info.free / 1073741824
print("Total Memory (ASR): ", total_memory)
if total_memory > 10:
device = i
break
else:
torch.cuda.empty_cache()
continue
nvidia_smi.nvmlShutdown()
torch.cuda.empty_cache()
pipe_en = pipeline("automatic-speech-recognition", "ASR/models/english", device=device)
pipe_hi = pipeline("automatic-speech-recognition", "ASR/models/hindi", device=device)
print("ASR Models loaded on device ", device)
def asr(audio, lang):
if lang == "en":
output = pipe_en(audio, device=device)
elif lang == "hi":
output = pipe_hi(audio, device=device)
output_text = output["text"]
output_text = output_text.lower().replace("<s>" , "")
return output_text