|
from gtts import gTTS |
|
import gradio as gr |
|
import os |
|
import pygame |
|
|
|
|
|
|
|
|
|
|
|
import random |
|
|
|
|
|
def text_to_speech(text, language="hr"): |
|
|
|
tts = gTTS(text=text, lang=language, slow=False) |
|
|
|
|
|
audio_file = "output.mp3" |
|
tts.save(audio_file) |
|
|
|
|
|
return audio_file |
|
|
|
|
|
def tts_demo(text, language="hr"): |
|
|
|
audio_file = text_to_speech(text, language) |
|
|
|
|
|
return audio_file |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def select_and_speak(state): |
|
if not state: |
|
return "", "No names provided.", [] |
|
|
|
names_list = state |
|
names_list = names_list.split("\n") |
|
if not names_list: |
|
return "", "List is empty. Please reset.", [] |
|
|
|
selected_name = random.choice(names_list) |
|
names_list.remove(selected_name) |
|
tts_demo(selected_name) |
|
|
|
|
|
play_sound() |
|
return names_list, selected_name |
|
|
|
def play_sound(): |
|
pygame.mixer.init() |
|
pygame.mixer.music.load("output.mp3") |
|
pygame.mixer.music.play() |
|
while pygame.mixer.music.get_busy() == True: |
|
continue |
|
|
|
|
|
os.remove("output.mp3") |
|
|
|
|
|
|
|
def reset_names(names): |
|
return names.split('\n') |
|
|
|
demo = gr.Interface( |
|
fn=select_and_speak, |
|
inputs=["text"], |
|
outputs=[gr.Textbox(label="Remaining Names"), gr.Textbox(label="Selected Name")], |
|
title="Random Name Selector and Speaker", |
|
description="Enter a list of names separated by newlines in the 'Reset' tab to start.", |
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
demo.launch() |
|
|