File size: 15,210 Bytes
8a8224c
1b03734
8a8224c
 
 
 
 
 
 
 
 
 
 
 
 
 
1b03734
 
8a8224c
 
 
1b03734
8a8224c
 
1b03734
 
8a8224c
 
 
1b03734
 
8a8224c
1b03734
8a8224c
1b03734
8a8224c
 
1b03734
 
8a8224c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b03734
 
 
8a8224c
 
 
 
 
 
1b03734
 
8a8224c
 
 
 
 
 
 
1b03734
 
 
8a8224c
1b03734
 
 
8a8224c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b03734
 
8a8224c
 
 
1b03734
8a8224c
 
 
 
1b03734
 
8a8224c
 
 
1b03734
 
8a8224c
 
 
1b03734
 
8a8224c
 
1b03734
 
 
8a8224c
1b03734
8a8224c
1b03734
8a8224c
1b03734
8a8224c
1b03734
8a8224c
1b03734
8a8224c
 
1b03734
8a8224c
 
1b03734
 
 
 
 
8a8224c
 
 
1b03734
 
8a8224c
 
 
 
 
 
 
 
 
 
1b03734
 
8a8224c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b03734
 
8a8224c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b03734
 
 
8a8224c
 
 
 
 
 
 
 
1b03734
 
 
 
8a8224c
1b03734
8a8224c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b03734
 
8a8224c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b03734
 
8a8224c
 
 
 
 
 
1b03734
 
 
8a8224c
 
 
 
 
 
 
1b03734
 
8a8224c
 
 
 
 
 
 
 
 
 
 
1b03734
 
8a8224c
 
 
 
1b03734
8a8224c
 
 
 
 
 
 
 
 
 
 
 
 
1b03734
 
 
8a8224c
 
 
1b03734
8a8224c
 
 
 
1b03734
 
 
8a8224c
 
 
1b03734
8a8224c
 
 
 
 
1b03734
8a8224c
 
 
1b03734
 
 
8a8224c
 
1b03734
 
8a8224c
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
# -*- coding: utf-8 -*-
"""
Updated FastAPI backend for GPT-SoVITS (*April 2025*)
---------------------------------------------------
Changes compared with the previous version shipped on 30 Apr 2025
=================================================================
1. **URL / S3 audio support** — `process_audio_path()` downloads `ref_audio_path` and
   each entry in `aux_ref_audio_paths` when they are HTTP(S) or S3 URLs, storing them
   as temporary files that are cleaned up afterwards.
2. **CUDA memory hygiene** — `torch.cuda.empty_cache()` is invoked after each request
   (success *or* error) to release GPU memory.
3. **Temporary‑file cleanup** — all files created by `process_audio_path()` are
   removed in `finally` blocks so they are guaranteed to disappear no matter how the
   request terminates.

The public API surface (**end‑points and query parameters**) is unchanged.
"""

from __future__ import annotations

import argparse
import os
import signal
import subprocess
import sys
import traceback
import urllib.parse
from io import BytesIO
from typing import Generator, List, Tuple

import numpy as np
import requests
import soundfile as sf
import torch
import uvicorn
from fastapi import FastAPI, HTTPException, Response
from fastapi.responses import JSONResponse, StreamingResponse
from pydantic import BaseModel

# ---------------------------------------------------------------------------
# Local package imports – keep *after* sys.path manipulation so relative import
# resolution continues to work when this file is executed from any directory.
# ---------------------------------------------------------------------------

NOW_DIR = os.getcwd()
sys.path.extend([NOW_DIR, f"{NOW_DIR}/GPT_SoVITS"])

from GPT_SoVITS.TTS_infer_pack.TTS import TTS, TTS_Config  # noqa: E402
from GPT_SoVITS.TTS_infer_pack.text_segmentation_method import (  # noqa: E402
    get_method_names as get_cut_method_names,
)
from tools.i18n.i18n import I18nAuto  # noqa: E402

# ---------------------------------------------------------------------------
# CLI arguments & global objects
# ---------------------------------------------------------------------------

i18n = I18nAuto()
cut_method_names = get_cut_method_names()

parser = argparse.ArgumentParser(description="GPT‑SoVITS API")
parser.add_argument(
    "-c", "--tts_config", default="GPT_SoVITS/configs/tts_infer.yaml", help="TTS‑infer config path"
)
parser.add_argument("-a", "--bind_addr", default="127.0.0.1", help="Bind address (default 127.0.0.1)")
parser.add_argument("-p", "--port", type=int, default=9880, help="Port (default 9880)")
args = parser.parse_args()

config_path = args.tts_config or "GPT-SoVITS/configs/tts_infer.yaml"
PORT = args.port
HOST = None if args.bind_addr == "None" else args.bind_addr

# ---------------------------------------------------------------------------
# TTS initialisation
# ---------------------------------------------------------------------------

tts_config = TTS_Config(config_path)
print(tts_config)
TTS_PIPELINE = TTS(tts_config)

APP = FastAPI()

# ---------------------------------------------------------------------------
# Helper utilities
# ---------------------------------------------------------------------------

TEMP_DIR = os.path.join(NOW_DIR, "_tmp_audio")
os.makedirs(TEMP_DIR, exist_ok=True)

def _empty_cuda_cache() -> None:
    """Release GPU memory if CUDA is available."""
    if torch.cuda.is_available():
        torch.cuda.empty_cache()


def _download_to_temp(url: str) -> str:
    """Download *url* to a unique file inside ``TEMP_DIR`` and return the path."""
    parsed = urllib.parse.urlparse(url)
    filename = os.path.basename(parsed.path) or f"audio_{abs(hash(url))}.wav"
    local_path = os.path.join(TEMP_DIR, filename)

    if url.startswith("s3://"):
        # Lazy‑load boto3 if/when the first S3 request arrives.
        import importlib

        boto3 = importlib.import_module("boto3")  # pylint: disable=import-error
        s3_client = boto3.client("s3")
        s3_client.download_file(parsed.netloc, parsed.path.lstrip("/"), local_path)
    else:
        with requests.get(url, stream=True, timeout=30) as r:
            r.raise_for_status()
            with open(local_path, "wb") as f_out:
                for chunk in r.iter_content(chunk_size=8192):
                    f_out.write(chunk)

    return local_path


def process_audio_path(audio_path: str | None) -> Tuple[str | None, bool]:
    """Return a *local* path for *audio_path* and whether it is temporary."""
    if not audio_path:
        return audio_path, False

    if audio_path.startswith(("http://", "https://", "s3://")):
        try:
            local = _download_to_temp(audio_path)
            return local, True
        except Exception as exc:  # pragma: no‑cover
            raise HTTPException(status_code=400, detail=f"Failed to download audio: {exc}") from exc
    return audio_path, False


# ---------------------------------------------------------------------------
# Audio (de)serialisation helpers
# ---------------------------------------------------------------------------

def _pack_ogg(buf: BytesIO, data: np.ndarray, rate: int):
    with sf.SoundFile(buf, mode="w", samplerate=rate, channels=1, format="ogg") as f:
        f.write(data)
    return buf


def _pack_raw(buf: BytesIO, data: np.ndarray, _rate: int):
    buf.write(data.tobytes())
    return buf


def _pack_wav(buf: BytesIO, data: np.ndarray, rate: int):
    sf.write(buf, data, rate, format="wav")
    return buf


def _pack_aac(buf: BytesIO, data: np.ndarray, rate: int):
    proc = subprocess.Popen(
        [
            "ffmpeg",
            "-f",
            "s16le",
            "-ar",
            str(rate),
            "-ac",
            "1",
            "-i",
            "pipe:0",
            "-c:a",
            "aac",
            "-b:a",
            "192k",
            "-vn",
            "-f",
            "adts",
            "pipe:1",
        ],
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
    )
    out, _ = proc.communicate(input=data.tobytes())
    buf.write(out)
    return buf


def _pack_audio(buf: BytesIO, data: np.ndarray, rate: int, media_type: str):
    dispatch = {
        "ogg": _pack_ogg,
        "aac": _pack_aac,
        "wav": _pack_wav,
        "raw": _pack_raw,
    }
    buf = dispatch.get(media_type, _pack_raw)(buf, data, rate)
    buf.seek(0)
    return buf


# ---------------------------------------------------------------------------
# Schemas
# ---------------------------------------------------------------------------

class TTSRequest(BaseModel):
    text: str | None = None
    text_lang: str | None = None
    ref_audio_path: str | None = None
    aux_ref_audio_paths: List[str] | None = None
    prompt_lang: str | None = None
    prompt_text: str = ""
    top_k: int = 5
    top_p: float = 1.0
    temperature: float = 1.0
    text_split_method: str = "cut5"
    batch_size: int = 1
    batch_threshold: float = 0.75
    split_bucket: bool = True
    speed_factor: float = 1.0
    fragment_interval: float = 0.3
    seed: int = -1
    media_type: str = "wav"
    streaming_mode: bool = False
    parallel_infer: bool = True
    repetition_penalty: float = 1.35
    sample_steps: int = 32
    super_sampling: bool = False


# ---------------------------------------------------------------------------
# Validation helpers
# ---------------------------------------------------------------------------

def _validate_request(req: dict):
    if not req.get("text"):
        return "text is required"
    if not req.get("text_lang"):
        return "text_lang is required"
    if req["text_lang"].lower() not in tts_config.languages:
        return f"text_lang {req['text_lang']} not supported"
    if not req.get("prompt_lang"):
        return "prompt_lang is required"
    if req["prompt_lang"].lower() not in tts_config.languages:
        return f"prompt_lang {req['prompt_lang']} not supported"
    if not req.get("ref_audio_path"):
        return "ref_audio_path is required"
    mt = req.get("media_type", "wav")
    if mt not in {"wav", "raw", "ogg", "aac"}:
        return f"media_type {mt} not supported"
    if (not req.get("streaming_mode") and mt == "ogg"):
        return "ogg is only supported in streaming mode"
    if req.get("text_split_method", "cut5") not in cut_method_names:
        return f"text_split_method {req['text_split_method']} not supported"
    return None


# ---------------------------------------------------------------------------
# Core handler
# ---------------------------------------------------------------------------

async def _tts_handle(req: dict):
    error = _validate_request(req)
    if error:
        return JSONResponse(status_code=400, content={"message": error})

    streaming_mode = req.get("streaming_mode", False)
    media_type = req.get("media_type", "wav")

    temp_files: List[str] = []
    try:
        # --- resolve & download audio paths ----------------------------------
        ref_path, is_tmp = process_audio_path(req["ref_audio_path"])
        req["ref_audio_path"] = ref_path
        if is_tmp:
            temp_files.append(ref_path)

        if req.get("aux_ref_audio_paths"):
            resolved: List[str] = []
            for p in req["aux_ref_audio_paths"]:
                lp, tmp = process_audio_path(p)
                resolved.append(lp)
                if tmp:
                    temp_files.append(lp)
            req["aux_ref_audio_paths"] = resolved

        # --- run inference ----------------------------------------------------
        generator = TTS_PIPELINE.run(req)

        if streaming_mode:
            async def _gen(gen: Generator, _media_type: str):
                first = True
                try:
                    for sr, chunk in gen:
                        if first and _media_type == "wav":
                            # Prepend a WAV header so clients can play immediately.
                            header = _wave_header_chunk(sample_rate=sr)
                            yield header
                            _media_type = "raw"
                            first = False
                        yield _pack_audio(BytesIO(), chunk, sr, _media_type).getvalue()
                finally:
                    _cleanup(temp_files)
            return StreamingResponse(_gen(generator, media_type), media_type=f"audio/{media_type}")

        # non‑streaming --------------------------------------------------------
        sr, data = next(generator)
        payload = _pack_audio(BytesIO(), data, sr, media_type).getvalue()
        resp = Response(payload, media_type=f"audio/{media_type}")
        _cleanup(temp_files)
        return resp

    except Exception as exc:  # noqa: BLE001
        _cleanup(temp_files)
        return JSONResponse(status_code=400, content={"message": "tts failed", "Exception": str(exc)})


# ---------------------------------------------------------------------------
# Cleanup helpers
# ---------------------------------------------------------------------------

def _cleanup(temp_files: List[str]):
    for fp in temp_files:
        try:
            os.remove(fp)
            # print(f"[cleanup] removed {fp}")
        except FileNotFoundError:
            pass
        except Exception as exc:  # pragma: no‑cover
            print(f"[cleanup‑warning] {exc}")
    _empty_cuda_cache()


# ---------------------------------------------------------------------------
# WAV header helper (for streaming WAV)
# ---------------------------------------------------------------------------

import wave  # placed here to keep top import section tidy

def _wave_header_chunk(frame: bytes = b"", *, channels: int = 1, width: int = 2, sample_rate: int = 32_000):
    buf = BytesIO()
    with wave.open(buf, "wb") as wav:
        wav.setnchannels(channels)
        wav.setsampwidth(width)
        wav.setframerate(sample_rate)
        wav.writeframes(frame)
    buf.seek(0)
    return buf.read()


# ---------------------------------------------------------------------------
# End‑points
# ---------------------------------------------------------------------------

@APP.get("/tts")
async def tts_get(**query):
    # Normalise language codes to lower‑case where applicable
    for k in ("text_lang", "prompt_lang"):
        if k in query and query[k] is not None:
            query[k] = query[k].lower()
    return await _tts_handle(query)


@APP.post("/tts")
async def tts_post(request: TTSRequest):
    payload = request.dict()
    if payload.get("text_lang"):
        payload["text_lang"] = payload["text_lang"].lower()
    if payload.get("prompt_lang"):
        payload["prompt_lang"] = payload["prompt_lang"].lower()
    return await _tts_handle(payload)


@APP.get("/control")
async def control(command: str | None = None):
    if not command:
        raise HTTPException(status_code=400, detail="command is required")
    if command == "restart":
        os.execl(sys.executable, sys.executable, *sys.argv)
    elif command == "exit":
        os.kill(os.getpid(), signal.SIGTERM)
    else:
        raise HTTPException(status_code=400, detail="unsupported command")
    return {"message": "ok"}


@APP.get("/set_refer_audio")
async def set_refer_audio(refer_audio_path: str | None = None):
    if not refer_audio_path:
        return JSONResponse(status_code=400, content={"message": "refer_audio_path is required"})

    temp_file = None
    try:
        local_path, is_tmp = process_audio_path(refer_audio_path)
        temp_file = local_path if is_tmp else None
        TTS_PIPELINE.set_ref_audio(local_path)
        return {"message": "success"}
    finally:
        if temp_file:
            try:
                os.remove(temp_file)
            except FileNotFoundError:
                pass
            _empty_cuda_cache()


@APP.get("/set_gpt_weights")
async def set_gpt_weights(weights_path: str | None = None):
    if not weights_path:
        return JSONResponse(status_code=400, content={"message": "gpt weight path is required"})
    try:
        TTS_PIPELINE.init_t2s_weights(weights_path)
        return {"message": "success"}
    except Exception as exc:  # noqa: BLE001
        return JSONResponse(status_code=400, content={"message": str(exc)})


@APP.get("/set_sovits_weights")
async def set_sovits_weights(weights_path: str | None = None):
    if not weights_path:
        return JSONResponse(status_code=400, content={"message": "sovits weight path is required"})
    try:
        TTS_PIPELINE.init_vits_weights(weights_path)
        return {"message": "success"}
    except Exception as exc:  # noqa: BLE001
        return JSONResponse(status_code=400, content={"message": str(exc)})


# ---------------------------------------------------------------------------
# Main entry point
# ---------------------------------------------------------------------------

if __name__ == "__main__":
    try:
        uvicorn.run(app=APP, host=HOST, port=PORT, workers=1)
    except Exception:  # pragma: no‑cover
        traceback.print_exc()
        os.kill(os.getpid(), signal.SIGTERM)
        sys.exit(0)