2023-05-30 00:34:23 +00:00
|
|
|
import openai
|
|
|
|
import whisper
|
|
|
|
import os
|
|
|
|
import torch
|
|
|
|
|
|
|
|
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)
|
2023-05-30 11:37:56 +00:00
|
|
|
return ''
|
2023-05-30 00:34:23 +00:00
|
|
|
return result['text'].strip()
|
|
|
|
|
|
|
|
class APIWhisperTranscriber:
|
|
|
|
def get_transcription(self, wav_file_path):
|
2023-05-30 23:04:28 +00:00
|
|
|
audio_file= open(wav_file_path, "rb")
|
2023-05-30 00:34:23 +00:00
|
|
|
try:
|
|
|
|
result = openai.Audio.translate("whisper-1", audio_file)
|
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
2023-05-30 11:37:56 +00:00
|
|
|
return ''
|
2023-05-30 00:34:23 +00:00
|
|
|
|
|
|
|
return result['text'].strip()
|