aazankhanYousafzai's picture
Create tts.py
bd0c01b verified
raw
history blame contribute delete
373 Bytes
# tts.py
from gtts import gTTS
from pydub import AudioSegment
import uuid
def text_to_speech(text: str) -> str:
uid = uuid.uuid4().hex
mp3_path = f"/tmp/{uid}.mp3"
wav_path = f"/tmp/{uid}.wav"
tts = gTTS(text=text, lang="en")
tts.save(mp3_path)
audio = AudioSegment.from_mp3(mp3_path)
audio.export(wav_path, format="wav")
return wav_path