File size: 4,562 Bytes
2bbabc3
 
 
 
 
51e442c
2bbabc3
 
 
 
 
 
 
 
9f383ea
 
51e442c
 
2bbabc3
 
 
 
51e442c
f88a8c0
51e442c
2bbabc3
51e442c
 
 
 
 
 
 
 
2bbabc3
 
 
 
 
 
 
 
51e442c
 
 
2bbabc3
 
51e442c
 
 
2bbabc3
 
 
51e442c
 
 
 
 
 
2bbabc3
51e442c
2bbabc3
 
 
 
 
 
 
 
51e442c
2bbabc3
51e442c
 
 
 
 
 
2bbabc3
51e442c
 
 
2bbabc3
 
51e442c
 
 
 
 
 
2bbabc3
 
 
 
 
9f383ea
2bbabc3
 
 
 
51e442c
2bbabc3
 
 
 
51e442c
 
 
 
 
 
2bbabc3
 
 
 
 
51e442c
 
2bbabc3
 
51e442c
 
 
 
 
 
 
2bbabc3
 
 
 
 
 
 
 
 
 
 
 
 
51e442c
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import tempfile
import edge_tts
import gradio as gr
import asyncio

# Updated language dictionary with clear voice labeling
language_dict = {
    "Amharic": {
        "Ameha": "am-ET-AmehaNeural",
        "Mekdes": "am-ET-MekdesNeural"
    },
    "English": {
        "Ryan": "en-GB-RyanNeural",
        "Clara": "en-CA-ClaraNeural"
    },
    "Tigrinya": {
        "Ameha (Amharic Voice)": "am-ET-AmehaNeural",
        "Mekdes (Amharic Voice)": "am-ET-MekdesNeural"
    }
}

async def text_to_speech_edge(text, language, speaker):
    # Clean speaker name if it's a Tigrinya selection
    if language == "Tigrinya":
        speaker = speaker.replace(" (Amharic Voice)", "")
    
    try:
        voice = language_dict[language][speaker]
    except KeyError:
        error_msg = (f"αˆ΅αˆ…α‰°α‰΅: α‹΅αˆα… '{speaker}' αˆˆα‰‹αŠ•α‰‹ '{language}' αŠ αˆα‰°αŒˆαŠ˜αˆα’"
                    if language in ["Amharic", "Tigrinya"]
                    else f"Error: Voice '{speaker}' not available for '{language}'")
        raise gr.Error(error_msg)

    try:
        communicate = edge_tts.Communicate(text, voice)
        with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
            tmp_path = tmp_file.name
            await asyncio.wait_for(communicate.save(tmp_path), timeout=30)
        return tmp_path
        
    except asyncio.TimeoutError:
        error_msg = ("αˆ΅αˆ…α‰°α‰΅: αŒŠα‹œ αŠ αˆα‰‹αˆα’ αŠ₯α‰£αŠ­α‹Ž αŠ₯αŠ•α‹°αŒˆαŠ“ α‹­αˆžαŠ­αˆ©α’"
                    if language in ["Amharic", "Tigrinya"]
                    else "Error: Timeout. Please try again.")
        raise gr.Error(error_msg)
    except Exception as e:
        error_msg = (f"αˆ΅αˆ…α‰°α‰΅: {str(e)}"
                    if language in ["Amharic", "Tigrinya"]
                    else f"Error: {str(e)}")
        raise gr.Error(error_msg)

def update_speakers(language):
    speakers = list(language_dict[language].keys())
    return gr.Dropdown(
        choices=speakers,
        value=speakers[0],
        label=f"Select Speaker {'(Amharic Voices)' if language == 'Tigrinya' else ''}"
    )

with gr.Blocks(title="Amharic, English & Tigrinya TTS", theme=gr.themes.Soft()) as demo:
    gr.HTML("""
    <style>
        h1 { 
            color: #2E86C1; 
            text-align: center;
            background: linear-gradient(45deg, #FF007F, #2E86C1);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            margin-bottom: 20px;
        }
        .notice {
            font-size: 0.9em;
            color: #666;
            text-align: center;
            margin: 10px 0;
            font-style: italic;
        }
        .gradio-button {
            background: linear-gradient(45deg, #FF007F, #2E86C1) !important;
            color: white !important;
        }
    </style>
    <center>
        <h1>Amharic, English & Tigrinya Text-to-Speech</h1>
        <div class="notice">
            Note: Tigrinya uses Amharic-accented voices until dedicated models become available
        </div>
    </center>
    """)

    with gr.Row():
        with gr.Column():
            language = gr.Dropdown(
                choices=["Amharic", "English", "Tigrinya"],
                value="Amharic",
                label="Select Language / α‰‹αŠ•α‰‹ α‹­αˆαˆ¨αŒ‘"
            )
            input_text = gr.Textbox(
                lines=5,
                label="Enter Text / αŒ½αˆ‘α α‹«αˆ΅αŒˆα‰‘",
                placeholder="Type your text here... / αŒ½αˆ‘αα‹ŽαŠ• α‹­αŒ»α‰..."
            )
            speaker = gr.Dropdown(
                label="Select Speaker / αŠ αˆ­α‰²αˆ΅α‰΅ α‹­αˆαˆ¨αŒ‘",
                interactive=True
            )
            run_btn = gr.Button(
                value="Generate Audio / α‹΅αˆα… ፍጠር",
                variant="primary"
            )

        with gr.Column():
            output_audio = gr.Audio(
                type="filepath",
                label="Generated Audio / α‹¨α‰°αˆαŒ αˆ¨ α‹΅αˆα…",
                interactive=False
            )

    # Initialize speakers dropdown
    demo.load(
        fn=lambda: gr.Dropdown(choices=list(language_dict["Amharic"].keys()),
        outputs=speaker
    )

    # Update speakers when language changes
    language.change(
        update_speakers,
        inputs=language,
        outputs=speaker
    )

    run_btn.click(
        text_to_speech_edge,
        inputs=[input_text, language, speaker],
        outputs=output_audio
    )

if __name__ == "__main__":
    demo.launch(server_port=7860, share=False)