Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,14 +2,24 @@ import os
|
|
| 2 |
import sys
|
| 3 |
import uuid
|
| 4 |
import logging
|
|
|
|
|
|
|
| 5 |
from typing import Optional
|
| 6 |
|
| 7 |
-
# CRITICAL: Set
|
| 8 |
os.environ['NUMBA_CACHE_DIR'] = '/tmp/numba_cache'
|
| 9 |
os.environ['NUMBA_DISABLE_JIT'] = '0' # Keep JIT enabled but control cache
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
os.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
# Set up logging
|
| 15 |
logging.basicConfig(level=logging.INFO)
|
|
@@ -28,9 +38,8 @@ try:
|
|
| 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
|
| 34 |
from neuttsair.neutts import NeuTTSAir
|
| 35 |
logger.info("✅ All imports successful")
|
| 36 |
|
|
@@ -114,7 +123,8 @@ async def test_endpoint():
|
|
| 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")
|
|
@@ -209,7 +219,6 @@ async def synthesize_speech_base64(
|
|
| 209 |
sf.write(buffer, wav, 24000, format='WAV')
|
| 210 |
buffer.seek(0)
|
| 211 |
|
| 212 |
-
import base64
|
| 213 |
audio_b64 = base64.b64encode(buffer.read()).decode('utf-8')
|
| 214 |
|
| 215 |
# Clean up
|
|
@@ -231,6 +240,56 @@ async def synthesize_speech_base64(
|
|
| 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)
|
|
|
|
| 2 |
import sys
|
| 3 |
import uuid
|
| 4 |
import logging
|
| 5 |
+
import io
|
| 6 |
+
import base64
|
| 7 |
from typing import Optional
|
| 8 |
|
| 9 |
+
# CRITICAL: Set environment variables BEFORE any imports to fix PyTorch user issues
|
| 10 |
os.environ['NUMBA_CACHE_DIR'] = '/tmp/numba_cache'
|
| 11 |
os.environ['NUMBA_DISABLE_JIT'] = '0' # Keep JIT enabled but control cache
|
| 12 |
+
os.environ['TORCH_HOME'] = '/tmp/torch_cache'
|
| 13 |
+
os.environ['TRANSFORMERS_CACHE'] = '/tmp/transformers_cache'
|
| 14 |
+
os.environ['HF_HOME'] = '/tmp/huggingface_cache'
|
| 15 |
|
| 16 |
+
# Set user environment variables to avoid getpwuid errors
|
| 17 |
+
os.environ['USER'] = 'appuser'
|
| 18 |
+
os.environ['LOGNAME'] = 'appuser'
|
| 19 |
+
|
| 20 |
+
# Create cache directories
|
| 21 |
+
for cache_dir in ['/tmp/numba_cache', '/tmp/torch_cache', '/tmp/transformers_cache', '/tmp/huggingface_cache']:
|
| 22 |
+
os.makedirs(cache_dir, exist_ok=True)
|
| 23 |
|
| 24 |
# Set up logging
|
| 25 |
logging.basicConfig(level=logging.INFO)
|
|
|
|
| 38 |
from fastapi.responses import FileResponse, JSONResponse
|
| 39 |
from fastapi.middleware.cors import CORSMiddleware
|
| 40 |
import soundfile as sf
|
|
|
|
| 41 |
|
| 42 |
+
# Now import NeuTTS - this should work with the cache fixes
|
| 43 |
from neuttsair.neutts import NeuTTSAir
|
| 44 |
logger.info("✅ All imports successful")
|
| 45 |
|
|
|
|
| 123 |
"status": "success",
|
| 124 |
"message": "API is working",
|
| 125 |
"model_loaded": tts is not None,
|
| 126 |
+
"numba_cache": os.environ.get('NUMBA_CACHE_DIR', 'not set'),
|
| 127 |
+
"torch_cache": os.environ.get('TORCH_HOME', 'not set')
|
| 128 |
}
|
| 129 |
|
| 130 |
@app.post("/api/v1/synthesize")
|
|
|
|
| 219 |
sf.write(buffer, wav, 24000, format='WAV')
|
| 220 |
buffer.seek(0)
|
| 221 |
|
|
|
|
| 222 |
audio_b64 = base64.b64encode(buffer.read()).decode('utf-8')
|
| 223 |
|
| 224 |
# Clean up
|
|
|
|
| 240 |
traceback.print_exc()
|
| 241 |
raise HTTPException(500, f"Synthesis failed: {str(e)}")
|
| 242 |
|
| 243 |
+
# Batch processing endpoint
|
| 244 |
+
@app.post("/api/v1/batch-synthesize")
|
| 245 |
+
async def batch_synthesize(
|
| 246 |
+
ref_text: str = Form(...),
|
| 247 |
+
ref_audio: UploadFile = File(...),
|
| 248 |
+
texts: str = Form(..., description="JSON array of texts to synthesize")
|
| 249 |
+
):
|
| 250 |
+
"""
|
| 251 |
+
Synthesize multiple texts with the same voice
|
| 252 |
+
"""
|
| 253 |
+
try:
|
| 254 |
+
import json
|
| 255 |
+
text_list = json.loads(texts)
|
| 256 |
+
|
| 257 |
+
# Initialize model if needed
|
| 258 |
+
tts_model = initialize_model()
|
| 259 |
+
|
| 260 |
+
# Save reference audio
|
| 261 |
+
os.makedirs("uploads", exist_ok=True)
|
| 262 |
+
upload_path = f"uploads/{uuid.uuid4()}.wav"
|
| 263 |
+
with open(upload_path, "wb") as f:
|
| 264 |
+
content = await ref_audio.read()
|
| 265 |
+
f.write(content)
|
| 266 |
+
|
| 267 |
+
# Encode reference once
|
| 268 |
+
ref_codes = tts_model.encode_reference(upload_path)
|
| 269 |
+
|
| 270 |
+
results = []
|
| 271 |
+
for i, text in enumerate(text_list):
|
| 272 |
+
wav = tts_model.infer(text, ref_codes, ref_text)
|
| 273 |
+
output_path = f"outputs/{uuid.uuid4()}.wav"
|
| 274 |
+
sf.write(output_path, wav, 24000)
|
| 275 |
+
results.append({
|
| 276 |
+
"text": text,
|
| 277 |
+
"audio_file": output_path,
|
| 278 |
+
"index": i
|
| 279 |
+
})
|
| 280 |
+
|
| 281 |
+
# Clean up upload file
|
| 282 |
+
try:
|
| 283 |
+
os.remove(upload_path)
|
| 284 |
+
except:
|
| 285 |
+
pass
|
| 286 |
+
|
| 287 |
+
return {"generated_files": results}
|
| 288 |
+
|
| 289 |
+
except Exception as e:
|
| 290 |
+
logger.error(f"Batch synthesis error: {str(e)}")
|
| 291 |
+
raise HTTPException(500, f"Batch synthesis failed: {str(e)}")
|
| 292 |
+
|
| 293 |
if __name__ == "__main__":
|
| 294 |
import uvicorn
|
| 295 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|