File size: 4,143 Bytes
8c6b704
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import tempfile
import edge_tts
import gradio as gr

language_dict = {
    "English": {
        "Jenny": "en-US-JennyNeural",
        "Guy": "en-US-GuyNeural",
        "Ana": "en-US-AnaNeural",
        "Aria": "en-US-AriaNeural"
    },
    "Arabic": {
        "Hamed": "ar-SA-HamedNeural",
        "Zariyah": "ar-SA-ZariyahNeural",
        "Fatima": "ar-AE-FatimaNeural",
        "Hamdan": "ar-AE-HamdanNeural"
    }
}

async def text_to_speech_edge(text, language_code, speaker):
    voice = language_dict[language_code][speaker]
    communicate = edge_tts.Communicate(text, voice)
    with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
        tmp_path = tmp_file.name
        await communicate.save(tmp_path)

    return tmp_path

def get_speakers(language):
    speakers = list(language_dict[language].keys())[:4]  # Limit to 4 speakers
    return gr.Dropdown(choices=speakers, value=speakers[0], interactive=True)

default_language = None
default_speaker = None
css_style = """
    body {
        background-image: url('https://cdna.artstation.com/p/assets/images/images/074/776/904/large/pietro-chiovaro-r1-castle-chp.jpg?1712916847');
        background-size: cover;
        background-position: center;
        color: #fff;  /* General text color */
        font-family: 'Arial', sans-serif;
    }
    .title {
        text-align: center;
        font-size: 3.5em;
        margin: 20px 0;
        text-shadow: 2px 2px 5px rgba(0, 0, 0, 0.7);
        color: #FFD700; /* Gold color for title */
    }
    .subtitle {
        text-align: center;
        font-size: 2.5em;
        color: #FFD700;
    }
    .output {
        margin-top: 20px;
        padding: 15px;
        border-radius: 8px;
        background: rgba(0, 0, 0, 0.7); /* Dark background for output area */
    }
    /* Styling for the examples */
    .gr-examples {
        background-color: transparent !important; /* Transparent background */
        color: white; /* White text for examples */
        padding: 10px; /* Optional padding */
    }
    .gr-examples button {
        color: white !important; /* White text for buttons */
        background-color: transparent !important; /* Transparent background for buttons */
        border: 1px solid white; /* Add a white border to buttons */
    }
"""

examples = [ ["Once upon a time in a small village, there lived a kind-hearted girl named Ella. Every day, she would help her neighbors and spread joy wherever she went.", "English", "Jenny"],
            ["ููŠ ู‚ุฏูŠู… ุงู„ุฒู…ุงู†ุŒ ูƒุงู† ู‡ู†ุงูƒ ูุชุงุฉ ุทูŠุจุฉ ุงู„ู‚ู„ุจ ุชุนูŠุด ููŠ ู‚ุฑูŠุฉ ุตุบูŠุฑุฉ ุชูุฏุนู‰ ู„ูŠู„ู‰. ูƒุงู†ุช ู„ูŠู„ู‰ ุชุณุงุนุฏ ุฌูŠุฑุงู†ู‡ุง ูƒู„ ูŠูˆู… ูˆุชู†ุดุฑ ุงู„ูุฑุญ ููŠ ูƒู„ ู…ูƒุงู† ุชุฐู‡ุจ ุฅู„ูŠู‡.", "Arabic", "Hamed"] ]
        
with gr.Blocks(css = css_style) as demo:
    
    gr.HTML("<div class='title'> Storytelling</div>")
    gr.HTML("<div class='subtitle'>Bring your stories to life with captivating voices!</div>")
    
    with gr.Row():
        with gr.Column():
            input_text = gr.Textbox(lines=5, label="Input Text", placeholder="Enter the story you want to tell...")
            language = gr.Dropdown(
                choices=list(language_dict.keys()), value=default_language, label="Select Language", interactive=True
            )
            speaker = gr.Dropdown(choices=[], value=default_speaker, label="Choose a Speaker", interactive=False)
            run_btn = gr.Button(value="Generate Magical Audio", variant="primary")

        with gr.Column():
            output_audio = gr.Audio(type="filepath", label="Audio Output", elem_id="output_audio", elem_classes="output")
    
    with gr.Row():
        with gr.Column():
            gr.Markdown("### Example Prompts")
            gr.Examples(examples,
                        inputs=[input_text,language, speaker],
                        cache_examples=False)   
    language.change(get_speakers, inputs=[language], outputs=[speaker])
    run_btn.click(text_to_speech_edge, inputs=[input_text, language, speaker], outputs=[output_audio])

if __name__ == "__main__":
    demo.queue().launch(share=False)