File size: 3,239 Bytes
04f3f01
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53d6ba9
fcbbf4a
 
53d6ba9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fcbbf4a
53d6ba9
 
 
fcbbf4a
 
04f3f01
53d6ba9
 
 
 
 
 
04f3f01
 
 
53d6ba9
 
 
 
 
 
04f3f01
 
53d6ba9
 
 
 
 
 
04f3f01
 
 
 
 
 
 
 
fcbbf4a
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
import tempfile
import edge_tts
import gradio as gr
import asyncio

language_dict = {
    "Amharic": {
        "Ameha": "am-ET-AmehaNeural",
        "Mekdes": "am-ET-MekdesNeural"
    }
}

async def text_to_speech_edge(text, speaker):
    voice = language_dict["Amharic"][speaker]
    
    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 = "αˆ΅αˆ…α‰°α‰΅: αŒŠα‹œ αŠ αˆα‰‹αˆα’ αŠ₯α‰£αŠ­α‹Ž αŠ₯αŠ•α‹°αŒˆαŠ“ α‹­αˆžαŠ­αˆ©α’ (Timeout)"
        raise gr.Error(error_msg)
    except Exception as e:
        error_msg = f"αˆ΅αˆ…α‰°α‰΅: α‹΅αˆα… መፍጠር αŠ αˆα‰°α‰»αˆˆαˆα’\nError: {str(e)}"
        raise gr.Error(error_msg)

with gr.Blocks(title="Amharic 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: 30px;
            font-size: 2.5em;
        }
        .gradio-container {
            background: #f8f9fa !important;
        }
        .gradio-button { 
            background: linear-gradient(45deg, #FF007F, #2E86C1) !important; 
            color: white !important;
            border-radius: 8px !important;
            padding: 12px 24px !important;
        }
        .gradio-textbox, .gradio-dropdown { 
            border-color: #2E86C1 !important;
            border-radius: 8px !important;
            padding: 12px !important;
        }
        .gradio-label {
            color: #2E86C1 !important;
            font-weight: 600 !important;
        }
        .prose {
            max-width: 800px;
            margin: 0 auto;
        }
        .gradio-row {
            gap: 20px;
        }
    </style>
    <center>
        <h1>α‹¨αŠ αˆ›αˆ­αŠ› αŒ½αˆ‘α α‹ˆα‹° α‹΅αˆα… α‰€α‹­αˆ­</h1>
    </center>
    """)

    with gr.Row():
        with gr.Column(scale=1):
            input_text = gr.Textbox(
                lines=5, 
                label="αŒ½αˆ‘α α‹«αˆ΅αŒˆα‰‘",
                placeholder="α‹΅αˆα… ለመፍጠር αŒ½αˆ‘αα‹ŽαŠ• α‹­αŒ»α‰..."
            )
            speaker = gr.Dropdown(
                choices=["Ameha", "Mekdes"],
                value="Ameha",
                label="α‹΅αˆααŠ• α‹¨αˆšα‹«αˆ°αˆ› αŠ αˆ­α‰²αˆ΅α‰΅"
            )
            run_btn = gr.Button(
                value="α‹΅αˆα… ፍጠር", 
                variant="primary",
                scale=1
            )

        with gr.Column(scale=1):
            output_audio = gr.Audio(
                type="filepath",
                label="α‹¨α‰°αˆαŒ αˆ¨ α‹΅αˆα…",
                elem_classes="output-audio"
            )

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

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