from fastapi import FastAPI, Form from fastapi.responses import FileResponse import wave from piper.voice import PiperVoice import os import tempfile app = FastAPI() # Load the voice model model_path = 'model.onnx' voice = PiperVoice.load(model_path) @app.post("/synthesize/") def synthesize_text(text: str = Form(...)): """ Endpoint to synthesize text to speech. Args: text (str): The text to synthesize. Returns: FileResponse: The WAV file containing the synthesized speech. """ with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_file: output_file = temp_file.name # Synthesize speech and save to a WAV file with wave.open(output_file, 'wb') as wav_file: audio = voice.synthesize(text, wav_file) # Ensure the file exists and return it as a response if os.path.exists(output_file): return FileResponse(output_file, media_type='audio/wav', filename='output.wav') else: return {"error": "Failed to generate audio."} @app.get("/") def read_root(): return {"message": "Welcome to the Piper TTS API. Use /synthesize/ to synthesize speech."}