File size: 754 Bytes
7e3c986 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 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)
|