File size: 10,878 Bytes
37a7804
 
 
 
 
 
 
 
 
 
58b0f90
37a7804
 
 
8a6294a
37a7804
 
58b0f90
37a7804
 
 
d512c0d
37a7804
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58b0f90
 
37a7804
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
afe82fe
 
 
 
 
 
37a7804
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58b0f90
ff7d020
37a7804
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26c5cf5
37a7804
 
 
 
 
 
58b0f90
37a7804
58b0f90
37a7804
 
 
 
 
 
 
 
 
 
 
 
 
58b0f90
37a7804
 
 
 
 
 
 
 
 
 
 
 
 
 
cea1dd5
37a7804
 
 
 
 
 
 
 
 
 
 
 
 
58b0f90
37a7804
 
58b0f90
37a7804
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58b0f90
37a7804
 
 
 
 
 
 
 
 
 
 
 
 
58b0f90
37a7804
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cea1dd5
37a7804
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff7d020
37a7804
 
 
 
 
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
import os
import sys
import time
import gc
import torch
import numpy as np
import aiofiles
from fastapi import FastAPI, UploadFile, File, Form, HTTPException
from fastapi.responses import JSONResponse, FileResponse
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from typing import Optional, Dict, Any
import psutil
import logging

# Add NeuTTS Air to path
sys.path.append("neutts-air")

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

app = FastAPI(
    title="NeuTTS Air API",
    description="High-quality on-device Text-to-Speech with instant voice cloning",
    version="1.0.0"
)

# CORS middleware
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Global model instance
tts_model = None
model_loading = False

# Pydantic models
class TTSRequest(BaseModel):
    text: str
    reference_text: str
    reference_audio_path: Optional[str] = None

class TTSResponse(BaseModel):
    success: bool
    audio_url: Optional[str] = None
    message: Optional[str] = None
    processing_time: Optional[float] = None
    audio_duration: Optional[float] = None

class HealthResponse(BaseModel):
    status: str
    model_loaded: bool
    memory_usage: Dict[str, float]
    disk_usage: Dict[str, float]

def load_tts_model():
    global tts_model, model_loading
    
    if tts_model is not None or model_loading:
        return
    
    model_loading = True
    try:
        logger.info("Loading NeuTTS Air model...")
        
        # Try to import with fallbacks
        try:
            from neuttsair.neutts import NeuTTSAir
        except ImportError as e:
            logger.error(f"Failed to import NeuTTS Air: {e}")
            # Try alternative import path
            sys.path.insert(0, "/app/neutts-air")
            from neuttsair.neutts import NeuTTSAir
        
        # Use CPU for Hugging Face free tier with fallback models
        tts_model = NeuTTSAir(
            backbone_repo="neuphonic/neutts-air",
            backbone_device="cpu",
            codec_repo="neuphonic/neucodec",
            codec_device="cpu"
        )
        
        logger.info("NeuTTS Air model loaded successfully!")
        
    except Exception as e:
        logger.error(f"Failed to load model: {str(e)}")
        model_loading = False
        raise e
    
    model_loading = False

@app.on_event("startup")
async def startup_event():
    """Load model on startup with error handling"""
    try:
        load_tts_model()
    except Exception as e:
        logger.error(f"Startup model loading failed: {e}")

@app.get("/")
async def root():
    return {"message": "NeuTTS Air API is running!", "status": "healthy"}

@app.get("/health")
async def health_check():
    """Health check endpoint"""
    try:
        memory = psutil.virtual_memory()
        disk = psutil.disk_usage('/')
        
        return HealthResponse(
            status="healthy",
            model_loaded=tts_model is not None,
            memory_usage={
                "total_gb": round(memory.total / (1024**3), 2),
                "available_gb": round(memory.available / (1024**3), 2),
                "used_percent": round(memory.percent, 2)
            },
            disk_usage={
                "total_gb": round(disk.total / (1024**3), 2),
                "free_gb": round(disk.free / (1024**3), 2),
                "used_percent": round(disk.percent, 2)
            }
        )
    except Exception as e:
        return HealthResponse(
            status="degraded",
            model_loaded=tts_model is not None,
            memory_usage={"error": str(e)},
            disk_usage={"error": str(e)}
        )

@app.post("/synthesize")
async def synthesize_speech(
    reference_text: str = Form(...),
    text: str = Form(...),
    reference_audio: UploadFile = File(...)
):
    """
    Synthesize speech using reference audio and text
    """
    start_time = time.time()
    
    if tts_model is None:
        raise HTTPException(status_code=503, detail="Model not loaded yet")
    
    # Validate inputs
    if not reference_text.strip() or not text.strip():
        raise HTTPException(status_code=400, detail="Text fields cannot be empty")
    
    if len(text) > 1000:
        raise HTTPException(status_code=400, detail="Text too long. Maximum 1000 characters allowed.")
    
    temp_ref_path = None
    try:
        # Save uploaded file temporarily
        temp_dir = "temp_audio"
        os.makedirs(temp_dir, exist_ok=True)
        
        file_extension = os.path.splitext(reference_audio.filename)[1] or ".wav"
        temp_ref_path = os.path.join(temp_dir, f"ref_{int(time.time())}{file_extension}")
        
        async with aiofiles.open(temp_ref_path, 'wb') as out_file:
            content = await reference_audio.read()
            await out_file.write(content)
        
        # Validate audio file
        try:
            import librosa
            audio_duration = librosa.get_duration(path=temp_ref_path)
            if audio_duration < 2 or audio_duration > 30:
                raise HTTPException(
                    status_code=400, 
                    detail=f"Audio duration ({audio_duration:.1f}s) should be between 3-15 seconds"
                )
        except Exception as e:
            raise HTTPException(status_code=400, detail=f"Invalid audio file: {str(e)}")
        
        # Perform TTS
        logger.info(f"Starting synthesis for text: {text[:50]}...")
        
        # Encode reference
        ref_codes = tts_model.encode_reference(temp_ref_path)
        
        # Generate speech
        wav = tts_model.infer(text, ref_codes, reference_text)
        
        # Save output
        output_dir = "generated_audio"
        os.makedirs(output_dir, exist_ok=True)
        output_filename = f"output_{int(time.time())}.wav"
        output_path = os.path.join(output_dir, output_filename)
        
        import soundfile as sf
        sf.write(output_path, wav, 24000)
        
        processing_time = time.time() - start_time
        audio_duration = len(wav) / 24000
        
        logger.info(f"Synthesis completed in {processing_time:.2f}s")
        
        return TTSResponse(
            success=True,
            audio_url=f"/audio/{output_filename}",
            message="Speech synthesized successfully",
            processing_time=round(processing_time, 2),
            audio_duration=round(audio_duration, 2)
        )
        
    except Exception as e:
        logger.error(f"Synthesis error: {str(e)}")
        raise HTTPException(status_code=500, detail=f"Synthesis failed: {str(e)}")
    
    finally:
        # Clean up temporary file
        if temp_ref_path and os.path.exists(temp_ref_path):
            try:
                os.remove(temp_ref_path)
            except:
                pass

@app.get("/audio/{filename}")
async def get_audio_file(filename: str):
    """Serve generated audio files"""
    file_path = os.path.join("generated_audio", filename)
    
    if not os.path.exists(file_path):
        raise HTTPException(status_code=404, detail="Audio file not found")
    
    return FileResponse(
        file_path,
        media_type="audio/wav",
        filename=f"generated_speech_{filename}"
    )

@app.post("/synthesize-with-url")
async def synthesize_with_url(request: TTSRequest):
    """
    Synthesize speech using a pre-uploaded reference audio file path
    """
    start_time = time.time()
    
    if tts_model is None:
        raise HTTPException(status_code=503, detail="Model not loaded yet")
    
    if not request.reference_audio_path or not os.path.exists(request.reference_audio_path):
        raise HTTPException(status_code=400, detail="Reference audio path not found")
    
    try:
        # Validate audio file
        import librosa
        audio_duration = librosa.get_duration(path=request.reference_audio_path)
        if audio_duration < 2 or audio_duration > 30:
            raise HTTPException(
                status_code=400, 
                detail=f"Audio duration ({audio_duration:.1f}s) should be between 3-15 seconds"
            )
        
        # Perform TTS
        logger.info(f"Starting synthesis for text: {request.text[:50]}...")
        
        # Encode reference
        ref_codes = tts_model.encode_reference(request.reference_audio_path)
        
        # Generate speech
        wav = tts_model.infer(request.text, ref_codes, request.reference_text)
        
        # Save output
        output_dir = "generated_audio"
        os.makedirs(output_dir, exist_ok=True)
        output_filename = f"output_{int(time.time())}.wav"
        output_path = os.path.join(output_dir, output_filename)
        
        import soundfile as sf
        sf.write(output_path, wav, 24000)
        
        processing_time = time.time() - start_time
        audio_duration = len(wav) / 24000
        
        return TTSResponse(
            success=True,
            audio_url=f"/audio/{output_filename}",
            message="Speech synthesized successfully",
            processing_time=round(processing_time, 2),
            audio_duration=round(audio_duration, 2)
        )
        
    except Exception as e:
        logger.error(f"Synthesis error: {str(e)}")
        raise HTTPException(status_code=500, detail=f"Synthesis failed: {str(e)}")

@app.delete("/cleanup")
async def cleanup_audio_files():
    """Clean up generated audio files older than 1 hour"""
    try:
        output_dir = "generated_audio"
        temp_dir = "temp_audio"
        
        deleted_count = 0
        current_time = time.time()
        
        # Clean generated audio
        if os.path.exists(output_dir):
            for filename in os.listdir(output_dir):
                file_path = os.path.join(output_dir, filename)
                if os.path.isfile(file_path):
                    file_age = current_time - os.path.getctime(file_path)
                    if file_age > 3600:  # 1 hour
                        os.remove(file_path)
                        deleted_count += 1
        
        # Clean temp audio
        if os.path.exists(temp_dir):
            for filename in os.listdir(temp_dir):
                file_path = os.path.join(temp_dir, filename)
                if os.path.isfile(file_path):
                    file_age = current_time - os.path.getctime(file_path)
                    if file_age > 3600:  # 1 hour
                        os.remove(file_path)
                        deleted_count += 1
        
        return {"message": f"Cleaned up {deleted_count} files"}
    
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Cleanup failed: {str(e)}")

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=7860)