Update app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091from fastapi import FastAPI, HTTPException, Requestfrom fastapi.responses import FileResponsefrom pathlib import Pathimport uuidimport subprocessimport osimport shutilapp = FastAPI()# ========== Dynamic VOICES_DIR fix ==========# Try /tmp/tts_app/voices firstbase_dirs = [Path("/tmp/tts_app/voices"), Path("/data/voices"), Path("/home/user/voices"), Path.cwd() / "voices"]for path in base_dirs:try:path.mkdir(parents=True, exist_ok=True)test_file = path / ".write_test"test_file.write_text("ok")test_file.unlink()VOICES_DIR = pathbreakexcept Exception:continueelse:raise RuntimeError("❌ Could not find any writable directory for VOICES_DIR.")FILES_DIR = VOICES_DIR.parent / "files"FILES_DIR.mkdir(parents=True, exist_ok=True)# ========== Piper engine config ==========VOICE_TAGS = ["en_US-libritts-high", # Humanlike, CPU-friendly (VITS-based)"en_US-amy-medium", # Classic Piper Amy"en_US-lessac-high", # LJSpeech-derived VITS]@app.get("/health")def health():return {"ok": True,"engine": "piper-tts (CLI, CPU)","voice_dir": str(VOICES_DIR),"files_dir": str(FILES_DIR),"default_voice": None,"available_voices": VOICE_TAGS,}@app.post("/speak")async def speak(request: Request):try:payload = await request.json()text = payload.get("text", "").strip()voice = payload.get("voice", VOICE_TAGS[0])length_scale = float(payload.get("length_scale", 1.08))noise_scale = float(payload.get("noise_scale", 0.33))noise_w = float(payload.get("noise_w", 0.8))if not text:raise HTTPException(status_code=400, detail="No text provided")output_file = FILES_DIR / f"{uuid.uuid4().hex}.wav"command = ["piper","--model", f"voices/{voice}.onnx","--output_file", str(output_file),"--text", text,"--length_scale", str(length_scale),"--noise_scale", str(noise_scale),"--noise_w", str(noise_w),]result = subprocess.run(command, capture_output=True, text=True)if result.returncode != 0 or not output_file.exists():raise HTTPException(status_code=500, detail=f"TTS failed: {result.stderr.strip()}")return {"ok": True,"voice": voice,"audio_url": f"/file/{output_file.name}"}except Exception as e:raise HTTPException(status_code=500, detail=str(e))@app.get("/file/{filename}")async def get_file(filename: str):path = FILES_DIR / filenameif path.exists():return FileResponse(path)raise HTTPException(status_code=404, detail="File not found")123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117# app.pyimport os, io, time, uuid, shutil, tempfilefrom pathlib import Pathfrom fastapi import FastAPI, Requestfrom fastapi.responses import FileResponse, StreamingResponse, JSONResponsefrom fastapi.websockets import WebSocketfrom pydantic import BaseModelimport subprocessimport waveapp = FastAPI()# ========== CONFIG ==========ROOT_DIR = Path(_file_).parent.resolve()VOICES_DIR = ROOT_DIR / "voices"FILES_DIR = ROOT_DIR / "files"VOICES_DIR.mkdir(parents=True, exist_ok=True)FILES_DIR.mkdir(parents=True, exist_ok=True)DEFAULT_VOICE = "en_US-libritts-high" # Replace with actual voice fileDEFAULT_SR = 22050# ========== HEALTH ==========@app.get("/health")async def health():return {"ok": True,"engine": "piper-tts (CLI, CPU)","default_voice": DEFAULT_VOICE,"voice_dir": str(VOICES_DIR),"available_voices": [f.stem for f in VOICES_DIR.glob("*.onnx")],"files_dir": str(FILES_DIR),}# ========== SPEAK (HTTP) ==========class SpeakRequest(BaseModel):text: strvoice: str = DEFAULT_VOICErate_wpm: int = 170length_scale: float = 1.0noise_scale: float = 0.33noise_w: float = 0.5@app.post("/speak")async def speak(req: SpeakRequest):out_path = FILES_DIR / f"{uuid.uuid4().hex}.wav"voice_path = VOICES_DIR / f"{req.voice}.onnx"if not voice_path.exists():return JSONResponse({"error": "Voice not found."}, status_code=404)cmd = ["piper","--model", str(voice_path),"--output_file", str(out_path),"--text", req.text,"--length_scale", str(req.length_scale),"--noise_scale", str(req.noise_scale),"--noise_w", str(req.noise_w),]subprocess.run(cmd, check=True)return FileResponse(out_path, media_type="audio/wav")# ========== STREAM (WebSocket) ==========@app.websocket("/ws/tts")async def tts_stream(websocket: WebSocket):await websocket.accept()voice = DEFAULT_VOICEsettings = {"length_scale": 1.0,"noise_scale": 0.33,"noise_w": 0.5}temp_file = FILES_DIR / f"{uuid.uuid4().hex}.wav"wave_writer = wave.open(str(temp_file), 'wb')wave_writer.setnchannels(1)wave_writer.setsampwidth(2)wave_writer.setframerate(DEFAULT_SR)try:while True:data = await websocket.receive_text()if data.startswith("{") and "text" in data:import jsonpayload = json.loads(data)text = payload.get("text", "")voice = payload.get("voice", DEFAULT_VOICE)settings["length_scale"] = float(payload.get("length_scale", 1.0))settings["noise_scale"] = float(payload.get("noise_scale", 0.33))settings["noise_w"] = float(payload.get("noise_w", 0.5))tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")cmd = ["piper","--model", str(VOICES_DIR / f"{voice}.onnx"),"--output_file", tmp.name,"--text", text,"--length_scale", str(settings["length_scale"]),"--noise_scale", str(settings["noise_scale"]),"--noise_w", str(settings["noise_w"]),]subprocess.run(cmd, check=True)with open(tmp.name, "rb") as f:audio = f.read()await websocket.send_bytes(audio)tmp.close()os.unlink(tmp.name)except Exception as e:print(f"[TTS WS Error] {e}")finally:wave_writer.close()await websocket.close()
app.py+98-72