23 lines
658 B
Python
23 lines
658 B
Python
import os
|
|
|
|
from fastapi.concurrency import run_in_threadpool
|
|
|
|
from whisper import Whisper
|
|
from app.services.abc import ISpeechToTextService
|
|
|
|
|
|
class OpenAIWhisper(ISpeechToTextService):
|
|
|
|
def __init__(self, model: Whisper):
|
|
self._model = model
|
|
|
|
async def speech_to_text(self, file_path):
|
|
if os.path.exists(file_path):
|
|
result = await run_in_threadpool(
|
|
self._model.transcribe, file_path, fp16=False, language='English', verbose=False
|
|
)
|
|
return result["text"]
|
|
else:
|
|
print("File not found:", file_path)
|
|
raise Exception("File " + file_path + " not found.")
|