Spaces:
Runtime error
Runtime error
| from fastapi import FastAPI | |
| from fastapi.responses import FileResponse | |
| import aiofiles | |
| import os | |
| from TTS.api import TTS # install via: pip install TTS | |
| import uvicorn | |
| app = FastAPI() | |
| # Load the Tacotron2 + HiFi-GAN Indonesian model (from Coqui TTS hub) | |
| # Model ID on HF: "tts_models/id/id_tts_tacotron2" | |
| tts = TTS(model_name="tts_models/id/id_tts_tacotron2") | |
| OUTPUT_FILE = "output.wav" | |
| async def tts_api(payload: dict): | |
| text = payload.get("text", "").strip() | |
| if not text: | |
| return {"error": "Text is required"} | |
| # Generate speech and save to OUTPUT_FILE | |
| tts.tts_to_file(text=text, file_path=OUTPUT_FILE) | |
| async with aiofiles.open(OUTPUT_FILE, "rb") as f: | |
| audio_data = await f.read() | |
| return { | |
| "text": text, | |
| "file_url": f"/download/{os.path.basename(OUTPUT_FILE)}", | |
| "size": len(audio_data), | |
| } | |
| async def download_file(filename: str): | |
| fp = os.path.join(os.getcwd(), filename) | |
| if os.path.exists(fp): | |
| return FileResponse(fp, media_type="audio/wav", filename=filename) | |
| return {"error": "File not found"} | |
| if __name__ == "__main__": | |
| uvicorn.run("app:app", host="0.0.0.0", port=7860) |