AI-API / functions /text_to_speech.py
Anicet
update: add translate, TTS and STT support
7e3c986
Raw
History Blame Contribute Delete
513 Bytes
import time, edge_tts, base64, os
async def textToSpeech(text: str, voice: str) -> str:
outputFile = f"temp_{int(time.time())}.mp3"
try:
tts = edge_tts.Communicate(text=text, voice=voice)
await tts.save(outputFile)
with open(outputFile, "rb") as file:
audioBytes = file.read()
audioBase64 = base64.b64encode(audioBytes).decode("utf-8")
return audioBase64
finally:
if os.path.exists(outputFile):
os.remove(outputFile)