AI-API / functions /speech_to_text.py
Anicet
update: add translate, TTS and STT support
7e3c986
Raw
History Blame Contribute Delete
754 Bytes
from faster_whisper import WhisperModel
import base64, tempfile, os
model = WhisperModel("base")
def speechToText(audioBase64: str, sourceLang: str) -> dict:
tempAudioPath = None
try:
audioBytes = base64.b64decode(audioBase64)
with tempfile.NamedTemporaryFile(delete=False, suffix=".m4a") as tempFile:
tempFile.write(audioBytes)
tempAudioPath = tempFile.name
segments, info = model.transcribe(tempAudioPath, language=sourceLang)
text = " ".join(segment.text for segment in segments)
return {'text': text, 'language': info.language, 'duration': info.duration}
finally:
if tempAudioPath and os.path.exists(tempAudioPath):
os.remove(tempAudioPath)