-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathwhisper_client.py
34 lines (27 loc) · 941 Bytes
/
whisper_client.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
import torch
import whisper
class WhisperClient:
def __init__(self, model_name="medium.en"):
self.model_name = model_name
self.model = None
def load_model(self):
if self.model is None:
self.model = whisper.load_model(self.model_name)
def unload_model(self):
if self.model is not None:
# Clear CUDA cache
if torch.cuda.is_available():
torch.cuda.empty_cache()
# Delete model and clear from memory
del self.model
self.model = None
# Force garbage collection
import gc
gc.collect()
if torch.cuda.is_available():
torch.cuda.empty_cache()
def transcribe(self, audio_path):
if self.model is None:
self.load_model()
result = self.model.transcribe(audio_path)
return result["text"]