File size: 4,064 Bytes
35f1d7f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import gradio as gr
import edge_tts
import asyncio
import tempfile

from piper_engine import PiperEngine


# -----------------------------
# Load Piper Once
# -----------------------------

piper_engine = PiperEngine(
    "models/en_US-amy-medium.onnx"
)


# -----------------------------
# Voice Loader (Edge)
# -----------------------------

async def get_voices():

    voices = await edge_tts.list_voices()

    return {
        f"{v['ShortName']} - {v['Locale']} ({v['Gender']})": v["ShortName"]
        for v in voices
    }


# -----------------------------
# Edge TTS (Neutral)
# -----------------------------

async def edge_tts_engine(text, voice, rate, pitch):

    if not text.strip():
        return None, "Please enter text."

    if not voice:
        return None, "Please select a voice."

    voice_short = voice.split(" - ")[0]

    rate_str = f"{rate:+d}%"
    pitch_str = f"{pitch:+d}Hz"

    communicate = edge_tts.Communicate(
        text,
        voice_short,
        rate=rate_str,
        pitch=pitch_str,
    )

    with tempfile.NamedTemporaryFile(
        delete=False,
        suffix=".mp3",
    ) as tmp:

        path = tmp.name
        await communicate.save(path)

    return path, None


# -----------------------------
# Piper TTS (Expressive)
# -----------------------------

def piper_engine_wrapper(text, rate, pitch):
    if not text.strip():
        return None, "Please enter text."

    speed = 1.0 + rate / 100      # rate from slider
    pitch_scale = 1.0 + pitch / 50
    audio = piper_engine.synthesize(text, speed, pitch_scale)

    return audio, None
    
# -----------------------------
# Unified Interface
# -----------------------------

async def unified_tts(
    text,
    voice,
    rate,
    pitch,
    engine,
):

    if engine == "Edge (Neutral)":

        return await edge_tts_engine(
            text,
            voice,
            rate,
            pitch,
        )

    else:

        return piper_engine_wrapper(
            text,
            rate,
            pitch,
        )


# -----------------------------
# UI Builder
# -----------------------------

async def create_demo():

    voices = await get_voices()

    with gr.Blocks(analytics_enabled=False) as demo:

        gr.Markdown("""
        # 🎙️ AI Text-to-Speech Lab
        Compare traditional and expressive AI voices.
        """)

        # Text
        text_input = gr.Textbox(
            label="Text",
            lines=6,
            placeholder="Paste lecture notes, narration, or scripts here...",
        )

        # Engine Selector
        engine_radio = gr.Radio(
            ["Edge (Neutral)", "Piper (Expressive)"],
            value="Edge (Neutral)",
            label="Generation",
        )

        # Voice (Edge Only)
        voice_dropdown = gr.Dropdown(
            choices=[""] + list(voices.keys()),
            label="Edge Voice",
            value="",
        )

        with gr.Row():

            rate_slider = gr.Slider(
                -50, 50, 0,
                step=1,
                label="Emotion / Speed",
            )

            pitch_slider = gr.Slider(
                -20, 20, 0,
                step=1,
                label="Intonation / Pitch",
            )

        generate_btn = gr.Button(
            "Generate Audio",
            variant="primary",
        )

        audio_output = gr.Audio(
            label="Output",
        )

        warning_md = gr.Markdown()


        generate_btn.click(
            fn=unified_tts,
            inputs=[
                text_input,
                voice_dropdown,
                rate_slider,
                pitch_slider,
                engine_radio,
            ],
            outputs=[
                audio_output,
                warning_md,
            ],
        )


    return demo


# -----------------------------
# Runner
# -----------------------------

async def main():

    demo = await create_demo()

    demo.queue(default_concurrency_limit=20)

    demo.launch()


if __name__ == "__main__":
    asyncio.run(main())