| 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) | |