File size: 11,087 Bytes
a287c67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Genie-TTS OpenAI Compatible API Server
======================================

This server provides an OpenAI-compatible TTS API endpoint (/v1/audio/speech)
for the Genie-TTS engine.

Usage:
    POST /v1/audio/speech
    {
        "model": "liang",           # Voice model name
        "input": "要合成的文本",     # Text to synthesize
        "voice": "alloy",           # Ignored - for OpenAI compatibility
        "response_format": "wav",   # Only wav is supported
        "speed": 1.0                # Ignored - for OpenAI compatibility
    }
"""

import os
import sys
import io
import wave
import json
import logging
import asyncio
from pathlib import Path
from typing import Optional, Dict, Any, Union
from contextlib import asynccontextmanager

import numpy as np
from fastapi import FastAPI, HTTPException, Request
from fastapi.responses import Response, StreamingResponse, JSONResponse
from pydantic import BaseModel, Field

# Configure logging
logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger(__name__)

# Model configuration
MODELS_DIR = Path(os.environ.get("MODELS_DIR", "/app/models"))
VOICES: Dict[str, Dict[str, Any]] = {}

# Audio settings
SAMPLE_RATE = 32000
CHANNELS = 1
BYTES_PER_SAMPLE = 2


class SpeechRequest(BaseModel):
    """OpenAI-compatible speech request model."""
    model: str = Field(..., description="The voice model to use")
    input: str = Field(..., description="The text to synthesize")
    voice: Optional[str] = Field(default="alloy", description="Ignored - for OpenAI compatibility")
    response_format: Optional[str] = Field(default="wav", description="Only wav is supported")
    speed: Optional[float] = Field(default=1.0, description="Ignored - for OpenAI compatibility")


class ErrorResponse(BaseModel):
    """OpenAI-compatible error response."""
    error: Dict[str, Any]


def load_voice_config(voice_dir: Path) -> Optional[Dict[str, Any]]:
    """Load voice configuration from a directory."""
    config_path = voice_dir / "config.json"
    if not config_path.exists():
        logger.warning(f"Config file not found: {config_path}")
        return None
    
    try:
        with open(config_path, "r", encoding="utf-8") as f:
            config = json.load(f)
        
        # Validate required fields
        required_fields = ["reference_audio", "reference_text", "language"]
        for field in required_fields:
            if field not in config:
                logger.error(f"Missing required field '{field}' in {config_path}")
                return None
        
        # Check if ONNX models exist
        onnx_dir = voice_dir / "onnx"
        if not onnx_dir.exists():
            logger.error(f"ONNX model directory not found: {onnx_dir}")
            return None
        
        config["onnx_dir"] = str(onnx_dir)
        config["voice_dir"] = str(voice_dir)
        
        return config
    except Exception as e:
        logger.error(f"Failed to load config from {config_path}: {e}")
        return None


def discover_voices() -> Dict[str, Dict[str, Any]]:
    """Discover all available voice models."""
    voices = {}
    
    if not MODELS_DIR.exists():
        logger.warning(f"Models directory not found: {MODELS_DIR}")
        return voices
    
    for voice_dir in MODELS_DIR.iterdir():
        if voice_dir.is_dir():
            voice_name = voice_dir.name
            config = load_voice_config(voice_dir)
            if config:
                voices[voice_name] = config
                logger.info(f"Loaded voice: {voice_name} (language: {config.get('language', 'unknown')})")
    
    return voices


def initialize_genie():
    """Initialize Genie-TTS engine and load all voice models."""
    global VOICES
    
    logger.info("Initializing Genie-TTS engine...")
    
    # Import genie_tts
    try:
        import genie_tts as genie
    except ImportError as e:
        logger.error(f"Failed to import genie_tts: {e}")
        raise
    
    # Download Genie data if needed
    logger.info("Checking Genie data...")
    genie.download_genie_data()
    
    # Discover and load voices
    VOICES = discover_voices()
    
    if not VOICES:
        logger.warning("No voice models found!")
        return
    
    # Load each voice model
    for voice_name, config in VOICES.items():
        try:
            logger.info(f"Loading voice model: {voice_name}")
            genie.load_character(
                character_name=voice_name,
                onnx_model_dir=config["onnx_dir"],
                language=config["language"]
            )
            
            # Set reference audio
            ref_audio_path = os.path.join(config["voice_dir"], config["reference_audio"])
            genie.set_reference_audio(
                character_name=voice_name,
                audio_path=ref_audio_path,
                audio_text=config["reference_text"],
                language=config["language"]
            )
            
            logger.info(f"Voice model loaded successfully: {voice_name}")
        except Exception as e:
            logger.error(f"Failed to load voice model {voice_name}: {e}")
            del VOICES[voice_name]
    
    logger.info(f"Genie-TTS initialized with {len(VOICES)} voice(s)")


@asynccontextmanager
async def lifespan(app: FastAPI):
    """Application lifespan manager."""
    # Startup
    initialize_genie()
    yield
    # Shutdown
    logger.info("Shutting down Genie-TTS server...")


# Create FastAPI app
app = FastAPI(
    title="Genie-TTS OpenAI Compatible API",
    description="OpenAI-compatible Text-to-Speech API powered by Genie-TTS",
    version="1.0.0",
    lifespan=lifespan
)


@app.get("/")
async def root():
    """Root endpoint - health check."""
    return {
        "status": "healthy",
        "service": "Genie-TTS OpenAI Compatible API",
        "available_models": list(VOICES.keys())
    }


@app.get("/health")
async def health():
    """Health check endpoint."""
    return {
        "status": "healthy",
        "models_loaded": len(VOICES),
        "available_models": list(VOICES.keys())
    }


@app.get("/v1/models")
async def list_models():
    """List available models (OpenAI-compatible)."""
    import time
    
    models = []
    for voice_name in VOICES.keys():
        models.append({
            "id": voice_name,
            "object": "model",
            "created": int(time.time()),
            "owned_by": "genie-tts"
        })
    
    return {
        "object": "list",
        "data": models
    }


def generate_wav_header(data_size: int) -> bytes:
    """Generate WAV file header."""
    header = io.BytesIO()
    
    # RIFF header
    header.write(b'RIFF')
    header.write((data_size + 36).to_bytes(4, 'little'))  # File size - 8
    header.write(b'WAVE')
    
    # fmt chunk
    header.write(b'fmt ')
    header.write((16).to_bytes(4, 'little'))  # Chunk size
    header.write((1).to_bytes(2, 'little'))   # Audio format (PCM)
    header.write((CHANNELS).to_bytes(2, 'little'))  # Number of channels
    header.write((SAMPLE_RATE).to_bytes(4, 'little'))  # Sample rate
    header.write((SAMPLE_RATE * CHANNELS * BYTES_PER_SAMPLE).to_bytes(4, 'little'))  # Byte rate
    header.write((CHANNELS * BYTES_PER_SAMPLE).to_bytes(2, 'little'))  # Block align
    header.write((BYTES_PER_SAMPLE * 8).to_bytes(2, 'little'))  # Bits per sample
    
    # data chunk
    header.write(b'data')
    header.write(data_size.to_bytes(4, 'little'))
    
    return header.getvalue()


@app.post("/v1/audio/speech")
async def create_speech(request: SpeechRequest):
    """
    Generate speech from text (OpenAI-compatible endpoint).
    
    This endpoint is compatible with the OpenAI TTS API format.
    Only the 'model' and 'input' parameters are used.
    """
    import genie_tts as genie
    
    # Validate model
    if request.model not in VOICES:
        return JSONResponse(
            status_code=404,
            content={
                "error": {
                    "message": f"Model '{request.model}' not found. Available models: {list(VOICES.keys())}",
                    "type": "invalid_request_error",
                    "code": "model_not_found"
                }
            }
        )
    
    # Validate input
    if not request.input or not request.input.strip():
        return JSONResponse(
            status_code=400,
            content={
                "error": {
                    "message": "Input text cannot be empty",
                    "type": "invalid_request_error",
                    "code": "invalid_input"
                }
            }
        )
    
    try:
        # Collect audio chunks
        audio_chunks = []
        
        async for chunk in genie.tts_async(
            character_name=request.model,
            text=request.input.strip(),
            play=False,
            split_sentence=True
        ):
            audio_chunks.append(chunk)
        
        if not audio_chunks:
            return JSONResponse(
                status_code=500,
                content={
                    "error": {
                        "message": "Failed to generate audio",
                        "type": "server_error",
                        "code": "generation_failed"
                    }
                }
            )
        
        # Combine all chunks
        audio_data = b''.join(audio_chunks)
        
        # Generate complete WAV file
        wav_header = generate_wav_header(len(audio_data))
        wav_content = wav_header + audio_data
        
        return Response(
            content=wav_content,
            media_type="audio/wav",
            headers={
                "Content-Disposition": "attachment; filename=speech.wav"
            }
        )
    
    except Exception as e:
        logger.error(f"TTS generation failed: {e}", exc_info=True)
        return JSONResponse(
            status_code=500,
            content={
                "error": {
                    "message": f"TTS generation failed: {str(e)}",
                    "type": "server_error",
                    "code": "generation_failed"
                }
            }
        )


# Error handlers
@app.exception_handler(404)
async def not_found_handler(request: Request, exc: HTTPException):
    return JSONResponse(
        status_code=404,
        content={
            "error": {
                "message": "Not found",
                "type": "invalid_request_error",
                "code": "not_found"
            }
        }
    )


@app.exception_handler(500)
async def internal_error_handler(request: Request, exc: Exception):
    return JSONResponse(
        status_code=500,
        content={
            "error": {
                "message": "Internal server error",
                "type": "server_error",
                "code": "internal_error"
            }
        }
    )


if __name__ == "__main__":
    import uvicorn
    
    port = int(os.environ.get("PORT", 7860))
    uvicorn.run(app, host="0.0.0.0", port=port)