File size: 3,084 Bytes
c9297fa
b23d86c
c9297fa
7e6edcc
b23d86c
df5b239
c9297fa
51023f8
 
 
 
 
 
 
 
 
 
 
 
 
34960c0
51023f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c9297fa
b23d86c
c9297fa
7e6edcc
 
 
 
 
 
 
 
c9297fa
7e6edcc
c9297fa
 
 
7e6edcc
 
b6aed11
7e6edcc
 
 
6222f8f
7e6edcc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f6a5f10
c9297fa
7e6edcc
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
import edge_tts
import gradio as gr
import tempfile
import asyncio


language_dict = {
    'Abeo': 'en-NG-AbeoNeural',
    'Ana': 'en-US-AnaNeural',
    'Andrew': 'en-US-AndrewNeural',
    'Aria': 'en-US-AriaNeural',
    'Asilia': 'en-KE-AsiliaNeural',
    'Ava': 'en-US-AvaNeural',
    'Brian': 'en-US-BrianNeural',
    'Christopher': 'en-US-ChristopherNeural',
    'Chilemba': 'en-KE-ChilembaNeural',
    'Clara': 'en-CA-ClaraNeural',
    'Connor': 'en-IE-ConnorNeural',
    'Emily': 'en-IE-EmilyNeural',
    'Elimu': 'en-TZ-ElimuNeural',
    'Emma': 'en-US-EmmaMultilingualNeural',
    'Eric': 'en-US-EricNeural',
    'Ezinne': 'en-NG-EzinneNeural',
    'Guy': 'en-US-GuyNeural',
    'Imani': 'en-TZ-ImaniNeural',
    'James': 'en-PH-JamesNeural',
    'Jenny': 'en-US-JennyNeural',
    'Leah': 'en-ZA-LeahNeural',
    'Libby': 'en-GB-LibbyNeural',
    'Liam': 'en-CA-LiamNeural',
    'Luke': 'en-ZA-LukeNeural',
    'Luna': 'en-SG-LunaNeural',
    'Maisie': 'en-GB-MaisieNeural',
    'Michelle': 'en-US-MichelleNeural',
    'Mitchell': 'en-NZ-MitchellNeural',
    'Molly': 'en-NZ-MollyNeural',
    'Natasha': 'en-AU-NatashaNeural',
    'Neerja': 'en-IN-NeerjaExpressiveNeural',
    'Prabhat': 'en-IN-PrabhatNeural',
    'Roger': 'en-US-RogerNeural',
    'Rosa': 'en-PH-RosaNeural',
    'Ryan': 'en-GB-RyanNeural',
    'Sam': 'en-HK-SamNeural',
    'Sonia': 'en-GB-SoniaNeural',
    'Steffan': 'en-US-SteffanNeural',
    'Thomas': 'en-GB-ThomasNeural',
    'Wayne': 'en-SG-WayneNeural',
    'William': 'en-AU-WilliamNeural',
    'Yan': 'en-HK-YanNeural'
}

async def text_to_speech_edge(text, language_code):
    """Convert text to speech using Edge TTS"""
    if not text:
        return "Please enter some text", None
    
    if not language_code:
        return "Please select a voice model", None
    
    voice = language_dict.get(language_code, "en-US-AriaNeural")
    communicate = edge_tts.Communicate(text, voice)
    
    with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
        tmp_path = tmp_file.name
        await communicate.save(tmp_path)
    
    return f"Speech synthesis completed for: {text[:50]}...", tmp_path

def text_to_speech_sync(text, language_code):
    """Synchronous wrapper for the async function"""
    return asyncio.run(text_to_speech_edge(text, language_code))

# Create Gradio interface
iface = gr.Interface(
    fn=text_to_speech_sync,
    inputs=[
        gr.Textbox(lines=5, label="Input Text", placeholder="Enter the text you want to convert to speech..."),
        gr.Dropdown(choices=list(language_dict.keys()), label="Choose the Voice Model", value="Aria")
    ],
    outputs=[
        gr.Textbox(label="Status"),
        gr.Audio(label="Generated Audio")
    ],
    title="Edge TTS - Text to Speech",
    description="Microsoft Edge Text-To-Speech (Forked & Fixed Ilaria TTS)",
    theme=gr.themes.Soft(),
    examples=[
        ["Hello, this is a test of the text to speech system.", "Aria"],
        ["Welcome to Edge TTS! This is an example.", "Brian"],
    ]
)

if __name__ == "__main__":
    iface.launch()