(base) root@VM-0-80-ubuntu:/workspace/index-tts# python sendflask.py {'audio_url': '/outputs/spk_1742982706.wav', 'status': 'success', '字数': 70, '耗时': 38.229} (base) root@VM-0-80-ubuntu:/workspace/index-tts# python sendflask.py {'audio_url': '/outputs/spk_1742982758.wav', 'status': 'success', '字数': 70, '耗时': 89.507} (base) root@VM-0-80-ubuntu:/workspace/index-tts#
import os import time from flask import Flask, request, jsonify, send_from_directory from indextts.infer import IndexTTS import torch
app = Flask(name)
tts = IndexTTS(model_dir="checkpoints", cfg_path="checkpoints/config.yaml")
os.makedirs("outputs", exist_ok=True)
@app.route("/infer", methods=["POST"]) def infer(): data = request.json voice = data.get("voice") text = data.get("text")
if not voice or not text:
return jsonify({"error": "缺少 voice 或 text"}), 400
output_filename = f"spk_{int(time.time())}.wav"
output_path = os.path.join("outputs", output_filename)
text_length = len(text)
start_time = time.time()
try:
with torch.cuda.amp.autocast():
tts.infer(voice, text, output_path)
end_time = time.time()
synth_time = round(end_time - start_time, 3)
return jsonify({
"status": "success",
"audio_url": f"/outputs/{output_filename}",
"字数": text_length,
"耗时": synth_time
})
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route("/outputs/", methods=["GET"]) def get_audio(filename): return send_from_directory("outputs", filename)
if name == "main": app.run(host="0.0.0.0", port=5000)