File size: 13,498 Bytes
d07649b
 
 
 
 
 
 
 
fb0aff8
d07649b
fb0aff8
d07649b
 
 
fb0aff8
 
 
 
 
 
081af21
fb0aff8
ed2234c
fb0aff8
081af21
fb0aff8
 
d07649b
 
 
 
 
 
 
 
 
 
 
fb0aff8
081af21
d07649b
 
 
 
 
3b35f2f
 
 
 
 
 
 
 
 
 
d07649b
3b35f2f
 
 
 
 
 
 
d07649b
3b35f2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d07649b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed2234c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d07649b
081af21
d07649b
 
 
 
 
fb0aff8
d07649b
fb0aff8
d07649b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fb0aff8
d07649b
fb0aff8
d07649b
 
 
fb0aff8
 
081af21
d07649b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fb0aff8
 
 
 
 
081af21
 
 
 
 
d07649b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fb0aff8
 
d07649b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54606ae
 
fb0aff8
 
d07649b
 
fb0aff8
 
 
 
54606ae
fb0aff8
 
 
 
 
54606ae
 
 
c8e234d
fb0aff8
 
 
54606ae
fb0aff8
c8e234d
fb0aff8
081af21
 
 
 
 
 
 
 
 
 
fb0aff8
 
d07649b
 
 
 
fb0aff8
d07649b
 
 
fb0aff8
54606ae
d07649b
 
 
fb0aff8
c8e234d
d07649b
 
 
 
 
 
 
 
 
 
 
 
 
fb0aff8
 
d07649b
 
 
 
 
 
 
 
 
 
fb0aff8
d07649b
fb0aff8
d07649b
 
 
c8e234d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fb0aff8
 
d07649b
 
 
 
 
 
fb0aff8
 
 
 
081af21
 
fb0aff8
 
 
d07649b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
from __future__ import annotations

from datetime import datetime, timezone
from pathlib import Path
import socket
import time
from uuid import uuid4

from fastapi import FastAPI, HTTPException, Request as FastApiRequest
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse, HTMLResponse
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel, Field

from tts_engine import (
    DEFAULT_KOKORO_LANG,
    DEFAULT_KOKORO_VOICE,
    DEFAULT_TEXT,
    DEFAULT_VOICE,
    get_kokoro_voices,
    get_supertonic_voices,
    get_voices,
    prewarm_kokoro,
    synthesize_kokoro_to_file,
    synthesize_supertonic_to_file,
    synthesize_to_file,
)


APP_DIR = Path(__file__).resolve().parent
GENERATED_DIR = APP_DIR / "generated"
STATIC_DIR = APP_DIR / "static"
TEMPLATE_DIR = APP_DIR / "templates"
INDEX_FILE = TEMPLATE_DIR / "index.html"

GENERATED_DIR.mkdir(parents=True, exist_ok=True)

MAX_TEXT_LENGTH = 3000
DEFAULT_LANG = "en"
DEFAULT_SUPERTONIC_VOICE = "M1"
VOICE_CACHE_TTL_SECONDS = 900

TRANSLATE_LANGUAGES = [
    {"code": "en", "label": "English"},
    {"code": "ar", "label": "Arabic"},
    {"code": "af", "label": "Afrikaans"},
    {"code": "sq", "label": "Albanian"},
    {"code": "am", "label": "Amharic"},
    {"code": "hy", "label": "Armenian"},
    {"code": "az", "label": "Azerbaijani"},
    {"code": "bn", "label": "Bengali"},
    {"code": "bs", "label": "Bosnian"},
    {"code": "bg", "label": "Bulgarian"},
    {"code": "ca", "label": "Catalan"},
    {"code": "zh-TW", "label": "Chinese (Traditional)"},
    {"code": "zh-CN", "label": "Chinese (Simplified)"},
    {"code": "hr", "label": "Croatian"},
    {"code": "cs", "label": "Czech"},
    {"code": "da", "label": "Danish"},
    {"code": "nl", "label": "Dutch"},
    {"code": "et", "label": "Estonian"},
    {"code": "fil", "label": "Filipino"},
    {"code": "fi", "label": "Finnish"},
    {"code": "fr", "label": "French"},
    {"code": "de", "label": "German"},
    {"code": "el", "label": "Greek"},
    {"code": "gu", "label": "Gujarati"},
    {"code": "he", "label": "Hebrew"},
    {"code": "hi", "label": "Hindi"},
    {"code": "hu", "label": "Hungarian"},
    {"code": "id", "label": "Indonesian"},
    {"code": "it", "label": "Italian"},
    {"code": "ja", "label": "Japanese"},
    {"code": "kn", "label": "Kannada"},
    {"code": "kk", "label": "Kazakh"},
    {"code": "ko", "label": "Korean"},
    {"code": "lv", "label": "Latvian"},
    {"code": "lt", "label": "Lithuanian"},
    {"code": "mk", "label": "Macedonian"},
    {"code": "ms", "label": "Malay"},
    {"code": "ml", "label": "Malayalam"},
    {"code": "mr", "label": "Marathi"},
    {"code": "ne", "label": "Nepali"},
    {"code": "no", "label": "Norwegian"},
    {"code": "fa", "label": "Persian"},
    {"code": "pl", "label": "Polish"},
    {"code": "pt", "label": "Portuguese"},
    {"code": "pa", "label": "Punjabi"},
    {"code": "ro", "label": "Romanian"},
    {"code": "ru", "label": "Russian"},
    {"code": "sr", "label": "Serbian"},
    {"code": "sk", "label": "Slovak"},
    {"code": "sl", "label": "Slovenian"},
    {"code": "es", "label": "Spanish"},
    {"code": "sw", "label": "Swahili"},
    {"code": "sv", "label": "Swedish"},
    {"code": "ta", "label": "Tamil"},
    {"code": "te", "label": "Telugu"},
    {"code": "th", "label": "Thai"},
    {"code": "tr", "label": "Turkish"},
    {"code": "uk", "label": "Ukrainian"},
    {"code": "ur", "label": "Urdu"},
    {"code": "uz", "label": "Uzbek"},
    {"code": "vi", "label": "Vietnamese"},
    {"code": "cy", "label": "Welsh"},
]

VOICE_CACHE: dict[str, dict[str, object]] = {}

app = FastAPI(title="EG Autonomous TTS Studio", version="3.0.0")
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=False,
    allow_methods=["*"],
    allow_headers=["*"],
)
app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")


@app.on_event("startup")
async def prewarm_models_on_startup() -> None:
    # Preload all runtime-critical assets so the UI is fully ready on first open.
    try:
        prewarm_kokoro()
    except Exception as error:
        print(f"[startup] Kokoro prewarm failed: {error}")

    for provider in ("edge", "kokoro"):
        try:
            await get_cached_voices_(provider, None)
        except Exception as error:
            print(f"[startup] voices prewarm failed for {provider}: {error}")


class SynthesizeRequest(BaseModel):
    provider: str = Field(default="edge", pattern="^(edge|kokoro|supertonic)$")
    text: str = Field(..., min_length=1, max_length=MAX_TEXT_LENGTH)
    voice: str = Field(default=DEFAULT_VOICE, min_length=1)
    rate: str = Field(default="+0%")
    volume: str = Field(default="+0%")
    pitch: str = Field(default="+0Hz")
    lang: str = Field(default=DEFAULT_LANG, min_length=2, max_length=12)
    slow: bool = Field(default=False)
    speed: float = Field(default=1.0, ge=0.5, le=2.0)


@app.get("/", response_class=HTMLResponse)
async def index() -> HTMLResponse:
    return HTMLResponse(INDEX_FILE.read_text(encoding="utf-8"))


@app.get("/health")
async def health() -> dict[str, str]:
    return {"status": "ok"}


@app.get("/api/defaults")
async def get_defaults() -> dict[str, object]:
    return {
        "text": DEFAULT_TEXT,
        "provider": "edge",
        "voice": DEFAULT_VOICE,
        "kokoroVoice": DEFAULT_KOKORO_VOICE,
        "rate": "+0%",
        "volume": "+0%",
        "pitch": "+0Hz",
        "lang": DEFAULT_LANG,
        "kokoroLang": DEFAULT_KOKORO_LANG,
        "supertonicVoice": DEFAULT_SUPERTONIC_VOICE,
        "slow": False,
        "translateLanguages": TRANSLATE_LANGUAGES,
    }


@app.get("/api/bootstrap")
async def get_bootstrap_data(request: FastApiRequest) -> dict[str, object]:
    return {
        "defaults": await get_defaults(),
        "providers": (await get_providers())["items"],
        "network": (await get_network_info(request)),
    }


@app.get("/api/providers")
async def get_providers() -> dict[str, object]:
    return {
        "items": [
            {
                "id": "edge",
                "displayName": "Edge TTS Engine",
                "description": "Generate MP3 audio with edge-tts through the internal synthesis engine.",
            },
            {
                "id": "kokoro",
                "displayName": "Kokoro ONNX",
                "description": "Higher-quality ONNX speech model with multiple bundled voices.",
            },
            {
                "id": "supertonic",
                "displayName": "Supertonic 3",
                "description": "State-of-the-art on-device TTS with emotional and natural voice styles.",
            },
        ]
    }


@app.get("/api/network")
async def get_network_info(request: FastApiRequest) -> dict[str, object]:
    host_header = request.headers.get("host", "")
    current_host = host_header.split(":", 1)[0] if host_header else request.url.hostname or "127.0.0.1"
    current_port = request.url.port or 7860
    lan_urls = build_lan_urls_(current_port)

    return {
        "ok": True,
        "host": current_host,
        "port": current_port,
        "localUrl": f"http://127.0.0.1:{current_port}",
        "currentUrl": str(request.base_url).rstrip("/"),
        "lanUrls": lan_urls,
        "lanEnabledHint": f"python -m uvicorn app:app --host 0.0.0.0 --port {current_port}",
        "isLocalRequest": current_host in {"127.0.0.1", "localhost", "0.0.0.0"},
    }


@app.get("/api/voices")
async def list_voice_catalog(provider: str = "edge", filter: str | None = None) -> dict[str, object]:
    voices = await get_cached_voices_(provider, filter)
    return {
        "items": [
            {
                "name": voice["ShortName"],
                "locale": voice["Locale"],
                "gender": voice["Gender"],
                "friendlyName": voice.get("FriendlyName", voice["ShortName"]),
            }
            for voice in voices
        ]
    }


@app.post("/api/synthesize")
async def synthesize_audio(payload: SynthesizeRequest, request: FastApiRequest) -> dict[str, object]:
    text = normalize_text_(payload.text)
    if not text:
        raise HTTPException(status_code=400, detail="Text is required.")

    selected_voice = payload.voice.strip()

    extension = ".mp3" if payload.provider == "edge" else ".wav"
    file_name = datetime.now(timezone.utc).strftime("%Y%m%d%H%M%S") + "-" + uuid4().hex + extension
    output_path = GENERATED_DIR / file_name

    if payload.provider == "edge":
        await synthesize_to_file(
            text=text,
            output=output_path,
            voice=selected_voice,
            rate=payload.rate.strip(),
            volume=payload.volume.strip(),
            pitch=payload.pitch.strip(),
        )
    elif payload.provider == "kokoro":
        # Guard against stale UI values (e.g., an Edge voice sent while Kokoro is selected).
        if "_" not in selected_voice:
            selected_voice = DEFAULT_KOKORO_VOICE
        kokoro_lang = normalize_kokoro_lang_(payload.lang)
        await synthesize_kokoro_to_file(
            text=text,
            output=output_path,
            voice=(selected_voice or DEFAULT_KOKORO_VOICE).strip(),
            speed=payload.speed,
            lang=kokoro_lang,
        )
    elif payload.provider == "supertonic":
        # Ensure we have a valid supertonic voice style
        if not selected_voice or selected_voice not in {"F1", "F2", "F3", "F4", "F5", "M1", "M2", "M3", "M4", "M5"}:
            selected_voice = DEFAULT_SUPERTONIC_VOICE
        await synthesize_supertonic_to_file(
            text=text,
            output=output_path,
            voice=selected_voice,
            lang=payload.lang[:2], # Supertonic expects 2-letter lang usually
        )
    else:
        raise HTTPException(status_code=400, detail="Unsupported provider.")

    audio_url = str(request.url_for("get_audio_file", file_name=file_name))
    return {
        "ok": True,
        "provider": payload.provider,
        "audioUrl": audio_url,
        "downloadUrl": audio_url,
        "fileName": file_name,
        "contentType": "audio/mpeg" if payload.provider == "edge" else "audio/wav",
        "voice": selected_voice,
        "rate": payload.rate.strip(),
        "volume": payload.volume.strip(),
        "pitch": payload.pitch.strip(),
        "speed": payload.speed,
        "lang": normalize_kokoro_lang_(payload.lang) if payload.provider == "kokoro" else payload.lang,
        "textLength": len(text),
    }


@app.get("/audio/{file_name}", name="get_audio_file")
async def get_audio_file(file_name: str) -> FileResponse:
    if "/" in file_name or "\\" in file_name:
        raise HTTPException(status_code=400, detail="Invalid file name.")

    file_path = GENERATED_DIR / file_name
    if not file_path.is_file():
        raise HTTPException(status_code=404, detail="Audio file not found.")

    media_type = "audio/mpeg" if file_path.suffix.lower() == ".mp3" else "audio/wav"
    return FileResponse(file_path, media_type=media_type, content_disposition_type="inline")


def normalize_text_(value: str) -> str:
    text = str(value or "").replace("\x00", "").replace("\r\n", "\n")
    text = "\n".join(line.strip() for line in text.split("\n"))
    text = text.strip()
    return text[:MAX_TEXT_LENGTH]


def normalize_lang_(value: str) -> str:
    lang = str(value or DEFAULT_LANG).strip()
    if not lang:
        return DEFAULT_LANG
    return lang[:12]


def normalize_kokoro_lang_(value: str) -> str:
    raw = (value or "").strip().lower()
    if not raw:
        return DEFAULT_KOKORO_LANG
    mapping = {
        "en": "en-us",
        "en-us": "en-us",
        "en_us": "en-us",
        "en-gb": "en-gb",
        "en_gb": "en-gb",
        "fr": "fr-fr",
        "fr-fr": "fr-fr",
        "fr_fr": "fr-fr",
        "ja": "ja",
        "zh": "zh",
    }
    return mapping.get(raw, DEFAULT_KOKORO_LANG)


async def get_cached_voices_(provider: str, keyword: str | None) -> list[dict[str, object]]:
    cache_key = provider + "::" + (keyword or "").strip().lower()
    cached = VOICE_CACHE.get(cache_key)
    now = time.time()

    if cached and now - float(cached["timestamp"]) < VOICE_CACHE_TTL_SECONDS:
        return cached["voices"]  # type: ignore[return-value]

    if provider == "edge":
        voices = await get_voices(keyword)
    elif provider == "kokoro":
        voices = get_kokoro_voices(keyword)
    elif provider == "supertonic":
        voices = get_supertonic_voices(keyword)
    else:
        raise HTTPException(status_code=400, detail="Unsupported provider for voices.")

    VOICE_CACHE[cache_key] = {
        "timestamp": now,
        "voices": voices,
    }
    return voices


def build_lan_urls_(port: int) -> list[str]:
    urls: list[str] = []
    candidates = {"127.0.0.1"}

    try:
        hostname = socket.gethostname()
        for result in socket.getaddrinfo(hostname, None, family=socket.AF_INET):
            ip = result[4][0]
            if ip:
                candidates.add(ip)
    except Exception:
        pass

    try:
        udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        udp_socket.connect(("8.8.8.8", 80))
        candidates.add(udp_socket.getsockname()[0])
        udp_socket.close()
    except Exception:
        pass

    for ip in sorted(candidates):
        if ip.startswith("127."):
            continue
        urls.append(f"http://{ip}:{port}")

    return urls