Spaces:
Sleeping
Sleeping
File size: 1,018 Bytes
b368e21 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
"""
Created By: ishwor subedi
Date: 2024-07-31
"""
import base64
from io import BytesIO
from gtts import gTTS
from gtts.tokenizer import pre_processors
class TextToSpeech:
def __init__(self):
self.preprocessing = [pre_processors.tone_marks, pre_processors.end_of_line, pre_processors.word_sub,
pre_processors.abbreviations]
def conversion(self, text: str, lang: str, tld: str) -> str:
"""
Convert text to speech and return the Base64-encoded MP3 data.
:param text: The text to convert to speech.
:param lang: The language in which to convert the text.
:return: Base64-encoded MP3 data as a string.
"""
tts = gTTS(text=text, lang=lang, slow=False, tld=tld, pre_processor_funcs=self.preprocessing)
mp3_fp = BytesIO()
tts.write_to_fp(mp3_fp)
mp3_fp.seek(0)
mp3_binary = mp3_fp.getvalue()
base64_mp3 = base64.b64encode(mp3_binary).decode("utf-8")
return base64_mp3
|