File size: 2,875 Bytes
9c66e8b c02a317 1c9515c 9c66e8b 1c9515c 9c66e8b ea2a18d 9c66e8b 1c9515c 39003e9 9c66e8b 39003e9 9c66e8b 1c9515c 9c66e8b 39003e9 9c66e8b 1c9515c 9c66e8b 1c9515c 9c66e8b 1c9515c 39003e9 7e7806d af0b938 1c9515c 9c66e8b 090d3ac 1c9515c 9c66e8b af0b938 9c66e8b 39003e9 9c66e8b af0b938 2f093db 1c9515c 9c66e8b 39003e9 af0b938 2f093db 1c9515c 9c66e8b af0b938 7e7806d 1c9515c 9c66e8b 7e7806d 1c9515c 39003e9 9c66e8b d588da8 9c66e8b c02a317 5051c82 9c66e8b 1c9515c 9c66e8b c02a317 9c66e8b 39003e9 9c66e8b | 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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | from flask import Flask, request, send_file, jsonify
from pocket_tts import TTSModel
import scipy.io.wavfile
import tempfile
import torch
# =========================
# CPU Optimization
# =========================
torch.set_num_threads(1)
torch.set_num_interop_threads(1)
app = Flask(__name__)
# =========================
# Hardcoded Voice
# =========================
VOICE = "eve" #"jane" #"vera" #"eve" #"anna"
print("Loading TTS model...")
# Load model
model = TTSModel.load_model()
print("Preparing voice...")
# Built-in voice
voice_state = model.get_state_for_audio_prompt(
VOICE
)
print("TTS Server Ready!")
# =========================
# Home Route
# =========================
@app.route("/", methods=["GET"])
def home():
return {
"status": "running",
"voice": VOICE,
"usage": "/generate?q=hello"
}
# =========================
# Generate Route
# =========================
@app.route("/generate", methods=["GET"])
def generate_get():
text = request.args.get("q")
if not text:
return jsonify({
"error": "Missing ?q=text"
}), 400
try:
# Generate speech
audio = model.generate_audio(
voice_state,
text
)
# Temp WAV file
temp_wav = tempfile.NamedTemporaryFile(
suffix=".wav",
delete=False
)
# Save WAV
scipy.io.wavfile.write(
temp_wav.name,
model.sample_rate,
audio.numpy()
)
# Return audio
return send_file(
temp_wav.name,
mimetype="audio/wav",
as_attachment=False
)
except Exception as e:
return jsonify({
"error": str(e)
}), 500
@app.route("/generate", methods=["POST"])
def generate():
# Get JSON body
data = request.get_json()
# Get q from body
text = data.get("q") if data else None
if not text:
return jsonify({
"error": "Missing 'q' in JSON body"
}), 400
try:
# Generate speech
audio = model.generate_audio(
voice_state,
text
)
# Temp WAV file
temp_wav = tempfile.NamedTemporaryFile(
suffix=".wav",
delete=False
)
# Save WAV
scipy.io.wavfile.write(
temp_wav.name,
model.sample_rate,
audio.numpy()
)
# Return audio
return send_file(
temp_wav.name,
mimetype="audio/wav",
as_attachment=False
)
except Exception as e:
return jsonify({
"error": str(e)
}), 500
# =========================
# Start Server
# =========================
if __name__ == "__main__":
app.run(
host="0.0.0.0",
port=7860
) |