Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,45 +3,39 @@ import sys
|
|
| 3 |
import uuid
|
| 4 |
import logging
|
| 5 |
from typing import Optional
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
| 12 |
|
| 13 |
# Set up logging
|
| 14 |
logging.basicConfig(level=logging.INFO)
|
| 15 |
logger = logging.getLogger("neutts-api")
|
| 16 |
|
| 17 |
-
# Add neutts-air to Python path
|
| 18 |
neutts_path = os.path.join(os.getcwd(), "neutts-air")
|
| 19 |
sys.path.insert(0, neutts_path)
|
| 20 |
|
| 21 |
logger.info(f"Current directory: {os.getcwd()}")
|
| 22 |
-
logger.info(f"Python path: {sys.path}")
|
| 23 |
-
|
| 24 |
-
# Check if neutts-air exists and what's in it
|
| 25 |
-
if os.path.exists(neutts_path):
|
| 26 |
-
logger.info("neutts-air directory exists")
|
| 27 |
-
logger.info(f"Contents: {os.listdir(neutts_path)}")
|
| 28 |
-
|
| 29 |
-
# Check for the specific module structure
|
| 30 |
-
neuttsair_path = os.path.join(neutts_path, "neuttsair")
|
| 31 |
-
if os.path.exists(neuttsair_path):
|
| 32 |
-
logger.info(f"neuttsair subdirectory exists: {os.listdir(neuttsair_path)}")
|
| 33 |
-
else:
|
| 34 |
-
logger.error("neuttsair subdirectory not found!")
|
| 35 |
-
else:
|
| 36 |
-
logger.error("neutts-air directory not found!")
|
| 37 |
|
| 38 |
try:
|
| 39 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
from neuttsair.neutts import NeuTTSAir
|
| 41 |
-
logger.info("✅
|
|
|
|
| 42 |
except ImportError as e:
|
| 43 |
logger.error(f"❌ Import failed: {e}")
|
| 44 |
-
# Try to provide more detailed debug info
|
| 45 |
import traceback
|
| 46 |
traceback.print_exc()
|
| 47 |
raise
|
|
@@ -79,6 +73,8 @@ def initialize_model():
|
|
| 79 |
logger.info("✅ NeuTTS Air model initialized successfully")
|
| 80 |
except Exception as e:
|
| 81 |
logger.error(f"❌ Model initialization failed: {e}")
|
|
|
|
|
|
|
| 82 |
raise
|
| 83 |
return tts
|
| 84 |
|
|
@@ -87,8 +83,9 @@ async def startup_event():
|
|
| 87 |
"""Initialize model on startup"""
|
| 88 |
try:
|
| 89 |
initialize_model()
|
|
|
|
| 90 |
except Exception as e:
|
| 91 |
-
logger.
|
| 92 |
|
| 93 |
@app.get("/")
|
| 94 |
async def root():
|
|
@@ -101,14 +98,25 @@ async def root():
|
|
| 101 |
@app.get("/health")
|
| 102 |
async def health_check():
|
| 103 |
return {
|
| 104 |
-
"status": "healthy",
|
| 105 |
"model_loaded": tts is not None,
|
| 106 |
"endpoints": {
|
| 107 |
"synthesize": "/api/v1/synthesize",
|
| 108 |
-
"synthesize_b64": "/api/v1/synthesize/b64"
|
|
|
|
| 109 |
}
|
| 110 |
}
|
| 111 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
@app.post("/api/v1/synthesize")
|
| 113 |
async def synthesize_speech(
|
| 114 |
ref_text: str = Form(..., description="Reference audio transcript"),
|
|
@@ -119,8 +127,8 @@ async def synthesize_speech(
|
|
| 119 |
Synthesize speech using voice cloning - returns WAV file
|
| 120 |
"""
|
| 121 |
try:
|
| 122 |
-
|
| 123 |
-
|
| 124 |
|
| 125 |
# Validate input
|
| 126 |
if not ref_text.strip() or not gen_text.strip():
|
|
@@ -143,10 +151,10 @@ async def synthesize_speech(
|
|
| 143 |
|
| 144 |
# Perform inference
|
| 145 |
logger.info("Encoding reference audio...")
|
| 146 |
-
ref_codes =
|
| 147 |
|
| 148 |
logger.info("Generating speech...")
|
| 149 |
-
wav =
|
| 150 |
|
| 151 |
# Save output
|
| 152 |
os.makedirs("outputs", exist_ok=True)
|
|
@@ -169,6 +177,8 @@ async def synthesize_speech(
|
|
| 169 |
|
| 170 |
except Exception as e:
|
| 171 |
logger.error(f"Synthesis error: {str(e)}")
|
|
|
|
|
|
|
| 172 |
raise HTTPException(500, f"Synthesis failed: {str(e)}")
|
| 173 |
|
| 174 |
@app.post("/api/v1/synthesize/b64")
|
|
@@ -181,8 +191,7 @@ async def synthesize_speech_base64(
|
|
| 181 |
Synthesize speech and return as base64 encoded audio
|
| 182 |
"""
|
| 183 |
try:
|
| 184 |
-
|
| 185 |
-
initialize_model()
|
| 186 |
|
| 187 |
# Save uploaded file
|
| 188 |
os.makedirs("uploads", exist_ok=True)
|
|
@@ -192,8 +201,8 @@ async def synthesize_speech_base64(
|
|
| 192 |
f.write(content)
|
| 193 |
|
| 194 |
# Perform inference
|
| 195 |
-
ref_codes =
|
| 196 |
-
wav =
|
| 197 |
|
| 198 |
# Convert to base64
|
| 199 |
buffer = io.BytesIO()
|
|
@@ -218,27 +227,10 @@ async def synthesize_speech_base64(
|
|
| 218 |
|
| 219 |
except Exception as e:
|
| 220 |
logger.error(f"Base64 synthesis error: {str(e)}")
|
|
|
|
|
|
|
| 221 |
raise HTTPException(500, f"Synthesis failed: {str(e)}")
|
| 222 |
|
| 223 |
-
# Simple test endpoint
|
| 224 |
-
@app.get("/api/v1/test")
|
| 225 |
-
async def test_endpoint():
|
| 226 |
-
"""Test endpoint to verify basic functionality"""
|
| 227 |
-
try:
|
| 228 |
-
if tts is None:
|
| 229 |
-
initialize_model()
|
| 230 |
-
|
| 231 |
-
return {
|
| 232 |
-
"status": "success",
|
| 233 |
-
"message": "API is working",
|
| 234 |
-
"model_loaded": tts is not None
|
| 235 |
-
}
|
| 236 |
-
except Exception as e:
|
| 237 |
-
return {
|
| 238 |
-
"status": "error",
|
| 239 |
-
"message": str(e)
|
| 240 |
-
}
|
| 241 |
-
|
| 242 |
if __name__ == "__main__":
|
| 243 |
import uvicorn
|
| 244 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 3 |
import uuid
|
| 4 |
import logging
|
| 5 |
from typing import Optional
|
| 6 |
+
|
| 7 |
+
# CRITICAL: Set numba environment variables BEFORE any imports
|
| 8 |
+
os.environ['NUMBA_CACHE_DIR'] = '/tmp/numba_cache'
|
| 9 |
+
os.environ['NUMBA_DISABLE_JIT'] = '0' # Keep JIT enabled but control cache
|
| 10 |
+
|
| 11 |
+
# Create cache directory
|
| 12 |
+
os.makedirs('/tmp/numba_cache', exist_ok=True)
|
| 13 |
|
| 14 |
# Set up logging
|
| 15 |
logging.basicConfig(level=logging.INFO)
|
| 16 |
logger = logging.getLogger("neutts-api")
|
| 17 |
|
| 18 |
+
# Add neutts-air to Python path
|
| 19 |
neutts_path = os.path.join(os.getcwd(), "neutts-air")
|
| 20 |
sys.path.insert(0, neutts_path)
|
| 21 |
|
| 22 |
logger.info(f"Current directory: {os.getcwd()}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
try:
|
| 25 |
+
# Import after environment setup
|
| 26 |
+
import numpy as np
|
| 27 |
+
from fastapi import FastAPI, HTTPException, UploadFile, File, Form
|
| 28 |
+
from fastapi.responses import FileResponse, JSONResponse
|
| 29 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 30 |
+
import soundfile as sf
|
| 31 |
+
import io
|
| 32 |
+
|
| 33 |
+
# Now import NeuTTS - this should work with the numba cache fix
|
| 34 |
from neuttsair.neutts import NeuTTSAir
|
| 35 |
+
logger.info("✅ All imports successful")
|
| 36 |
+
|
| 37 |
except ImportError as e:
|
| 38 |
logger.error(f"❌ Import failed: {e}")
|
|
|
|
| 39 |
import traceback
|
| 40 |
traceback.print_exc()
|
| 41 |
raise
|
|
|
|
| 73 |
logger.info("✅ NeuTTS Air model initialized successfully")
|
| 74 |
except Exception as e:
|
| 75 |
logger.error(f"❌ Model initialization failed: {e}")
|
| 76 |
+
import traceback
|
| 77 |
+
traceback.print_exc()
|
| 78 |
raise
|
| 79 |
return tts
|
| 80 |
|
|
|
|
| 83 |
"""Initialize model on startup"""
|
| 84 |
try:
|
| 85 |
initialize_model()
|
| 86 |
+
logger.info("✅ Startup initialization successful")
|
| 87 |
except Exception as e:
|
| 88 |
+
logger.warning(f"⚠️ Startup initialization failed: {e}")
|
| 89 |
|
| 90 |
@app.get("/")
|
| 91 |
async def root():
|
|
|
|
| 98 |
@app.get("/health")
|
| 99 |
async def health_check():
|
| 100 |
return {
|
| 101 |
+
"status": "healthy" if tts is not None else "degraded",
|
| 102 |
"model_loaded": tts is not None,
|
| 103 |
"endpoints": {
|
| 104 |
"synthesize": "/api/v1/synthesize",
|
| 105 |
+
"synthesize_b64": "/api/v1/synthesize/b64",
|
| 106 |
+
"test": "/api/v1/test"
|
| 107 |
}
|
| 108 |
}
|
| 109 |
|
| 110 |
+
@app.get("/api/v1/test")
|
| 111 |
+
async def test_endpoint():
|
| 112 |
+
"""Test endpoint that doesn't require model initialization"""
|
| 113 |
+
return {
|
| 114 |
+
"status": "success",
|
| 115 |
+
"message": "API is working",
|
| 116 |
+
"model_loaded": tts is not None,
|
| 117 |
+
"numba_cache": os.environ.get('NUMBA_CACHE_DIR', 'not set')
|
| 118 |
+
}
|
| 119 |
+
|
| 120 |
@app.post("/api/v1/synthesize")
|
| 121 |
async def synthesize_speech(
|
| 122 |
ref_text: str = Form(..., description="Reference audio transcript"),
|
|
|
|
| 127 |
Synthesize speech using voice cloning - returns WAV file
|
| 128 |
"""
|
| 129 |
try:
|
| 130 |
+
# Initialize model if needed
|
| 131 |
+
tts_model = initialize_model()
|
| 132 |
|
| 133 |
# Validate input
|
| 134 |
if not ref_text.strip() or not gen_text.strip():
|
|
|
|
| 151 |
|
| 152 |
# Perform inference
|
| 153 |
logger.info("Encoding reference audio...")
|
| 154 |
+
ref_codes = tts_model.encode_reference(upload_path)
|
| 155 |
|
| 156 |
logger.info("Generating speech...")
|
| 157 |
+
wav = tts_model.infer(gen_text, ref_codes, ref_text)
|
| 158 |
|
| 159 |
# Save output
|
| 160 |
os.makedirs("outputs", exist_ok=True)
|
|
|
|
| 177 |
|
| 178 |
except Exception as e:
|
| 179 |
logger.error(f"Synthesis error: {str(e)}")
|
| 180 |
+
import traceback
|
| 181 |
+
traceback.print_exc()
|
| 182 |
raise HTTPException(500, f"Synthesis failed: {str(e)}")
|
| 183 |
|
| 184 |
@app.post("/api/v1/synthesize/b64")
|
|
|
|
| 191 |
Synthesize speech and return as base64 encoded audio
|
| 192 |
"""
|
| 193 |
try:
|
| 194 |
+
tts_model = initialize_model()
|
|
|
|
| 195 |
|
| 196 |
# Save uploaded file
|
| 197 |
os.makedirs("uploads", exist_ok=True)
|
|
|
|
| 201 |
f.write(content)
|
| 202 |
|
| 203 |
# Perform inference
|
| 204 |
+
ref_codes = tts_model.encode_reference(upload_path)
|
| 205 |
+
wav = tts_model.infer(gen_text, ref_codes, ref_text)
|
| 206 |
|
| 207 |
# Convert to base64
|
| 208 |
buffer = io.BytesIO()
|
|
|
|
| 227 |
|
| 228 |
except Exception as e:
|
| 229 |
logger.error(f"Base64 synthesis error: {str(e)}")
|
| 230 |
+
import traceback
|
| 231 |
+
traceback.print_exc()
|
| 232 |
raise HTTPException(500, f"Synthesis failed: {str(e)}")
|
| 233 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 234 |
if __name__ == "__main__":
|
| 235 |
import uvicorn
|
| 236 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|