import gradio as gr from gtts import gTTS import os # Define your sentences here sents = [ "In the small mountain village of Echo Ridge, adventure was a part of everyday life. Nestled among towering peaks, the village was said to be protected by the 'Guardian of the Glen,' a massive eagle that supposedly watched over the villagers from its perch high in the mountains. The legend inspired many adventurous tales among the villagers, especially the children.", "Among these children was a bright-eyed eighth grader named Alex. Alex was known for his daring spirit and his love for exploring the rugged landscapes around Echo Ridge. He had a particular fascination with old maps and tales of hidden treasures that had been lost in the mountains centuries ago.", "One day, while exploring the local library, Alex stumbled upon an ancient map tucked inside a forgotten book on village lore. The map hinted at the location of a lost treasure, hidden deep within a cave known as Whispering Hollow. Excited by the prospect of a real adventure, Alex decided to seek out the treasure.", "Knowing the journey would be risky, he enlisted the help of his best friends, Mia and Sam. Together, they prepared for the expedition, gathering supplies and studying the map extensively. They planned their route, took note of landmarks, and readied themselves for any challenges they might face.", "Their journey began at dawn. They trekked through dense forests, crossed rushing streams, and climbed steep cliffs. Along the way, they encountered various wildlife and navigated through tricky terrain, their map guiding them every step of the way.", "After hours of hiking, they finally reached Whispering Hollow. The cave was more magnificent than they had imagined, filled with intricate stalactites and echoes of dripping water. Using their flashlights, they ventured deeper into the cave, guided by the markings on the map.", "As they reached the heart of the cave, they discovered an ancient chest hidden behind a fallen boulder. With hearts pounding, they moved the boulder and opened the chest. Inside, instead of gold or jewels, they found a collection of old artifacts: pottery, coins, and a beautifully carved statuette of an eagle — the Guardian of the Glen.", "Realizing the historical significance of their find, they decided to donate the artifacts to the local museum. The village celebrated their discovery, and the children were hailed as heroes. Their adventure brought the community together, sparking a renewed interest in the history and legends of Echo Ridge. Alex, Mia, and Sam became local legends, known not only for their daring but also for their spirit of discovery and respect for heritage. They continued to explore the mountains, each adventure strengthening their friendship and deepening their connection to their village.", "The legend of the Guardian of the Glen lived on, not just as a protector but as a symbol of adventure and discovery, inspiring future generations to explore the mysteries of Echo Ridge." ] def text_to_speech(selected_sentence, language): # Adjust the language code for the British English accent tld = 'co.uk' if language == "British English" else 'com' sn = int(selected_sentence.split(".")[0]) # Extract the sentence number mytext = sents[sn - 1] # Get the selected sentence # Create a gTTS object with the selected language and tld for accent tts = gTTS(text=mytext, lang='en', tld=tld, slow=False) # Save the speech to a temporary file filename = 'output.mp3' tts.save(filename) return filename iface = gr.Interface( fn=text_to_speech, inputs=[ gr.Dropdown(choices=[f"{i}. {sents[i-1]}" for i in range(1, len(sents) + 1)], label="Select Sentence"), gr.Radio(choices=['English', 'British English'], label="Language") ], outputs=gr.Audio(type="filepath", label="Output Audio"), title="Text-to-Speech Converter", description="Select a sentence and language to convert it to speech." ) iface.launch()