Spaces:
Sleeping
Sleeping
from fastapi import FastAPI, Form | |
from fastapi.responses import FileResponse | |
import wave | |
from piper.voice import PiperVoice | |
import os | |
app = FastAPI() | |
# Load the voice model | |
model_path = 'model.onnx' | |
voice = PiperVoice.load(model_path) | |
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. | |
""" | |
output_file = "output.wav" | |
# 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."} | |
def read_root(): | |
return {"message": "Welcome to the Piper TTS API. Use /synthesize/ to synthesize speech."} |