learnable-ai / examples /text_to speech_example.py
ishworrsubedii's picture
Added new features and improved code formatting:
32a0eda
"""
Created By: ishwor subedi
Date: 2024-08-13
"""
import base64
import random
from src.services.speech.text_to_speech import TextToSpeech
if __name__ == "__main__":
tts = TextToSpeech()
paragraph = "Nepal, nestled between India and China, is renowned for its breathtaking landscapes and rich cultural heritage. Home to the Himalayas, including the world’s highest peak, Mount Everest, Nepal’s terrain varies from lush Terai plains to towering alpine regions. The country boasts a rich history with ancient temples and royal palaces, reflecting a blend of Hindu and Buddhist influences. Kathmandu Valley, a UNESCO World Heritage Site, showcases the artistic splendor of the Malla kings. Nepali, the official language, is spoken alongside over 120 other languages. Hinduism is the predominant religion, followed by Buddhism, Islam, and Christianity. Festivals like Dashain and Tihar highlight the vibrant culture. Agriculture remains the backbone of Nepal’s economy, though tourism also plays a crucial role, with visitors drawn to trekking and mountaineering adventures. Despite challenges such as political instability and natural disasters, including the 2015 earthquake, Nepal is making strides in development and recovery. The country’s commitment to sustainable growth and eco-tourism aims to preserve its natural beauty while promoting economic progress. Key attractions include Everest, Kathmandu’s historic sites, Pokhara’s stunning lakes, and Chitwan National Park’s diverse wildlife."
lang = 'en'
tld = 'com'
# Use the sentence_audio_generator to get base64-encoded audio for each sentence
for base64_audio in tts.sentence_audio_generator(paragraph, lang, tld):
with open(f"audio{random.randint(0, 1000000)}.mp3", "wb") as f:
f.write(base64.b64decode(base64_audio))