forked from SevaSk/ecoute
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathTranscriberModels.py
39 lines (34 loc) · 1.13 KB
/
TranscriberModels.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
from openai import OpenAI
from keys import OPENAI_API_KEY
import whisper
import os
import torch
client = OpenAI(api_key=OPENAI_API_KEY)
def get_model(use_api):
if use_api:
return APIWhisperTranscriber()
else:
return WhisperTranscriber()
class WhisperTranscriber:
def __init__(self):
self.audio_model = whisper.load_model(os.path.join(os.getcwd(), 'tiny.en.pt'))
print(f"[INFO] Whisper using GPU: " + str(torch.cuda.is_available()))
def get_transcription(self, wav_file_path):
try:
result = self.audio_model.transcribe(wav_file_path, fp16=torch.cuda.is_available())
except Exception as e:
print(e)
return ''
return result.text.strip()
class APIWhisperTranscriber:
def get_transcription(self, wav_file_path):
try:
with open(wav_file_path, "rb") as audio_file:
result = client.audio.transcriptions.create(
model="whisper-1",
file=audio_file,
)
except Exception as e:
print(e)
return ''
return result.text.strip()