File size: 1,160 Bytes
bfe18f9
 
 
 
 
c8c3aff
bfe18f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c8c3aff
 
bfe18f9
 
 
 
 
 
 
 
 
 
 
 
 
c8c3aff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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."}