Spaces:
Sahaltech
/
Running on CPU Upgrade

AI-API / functions /speech_to_text.py
Anicet
update: review config for gpu machine
405b2f1
Raw
History Blame Contribute Delete
902 Bytes
from faster_whisper import WhisperModel
import base64, tempfile, os
import torch
model = WhisperModel(
"base",
device="cuda" if torch.cuda.is_available() else "cpu",
compute_type="float16" if torch.cuda.is_available() else "int8",
)
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)