安装、引入 gTTS 库
pip install gTTS from gtts import gTTS
直接上代码
from flask import Flask, request, send_file
from gtts import gTTS
import io
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
@app.route('/')
def hello_world(): # put application's code here
return 'Hello World!'
@app.route('/index', methods=['GET'])
def getTest():
text = request.args['text']
tts = gTTS(text, lang='zh-CN');
byte_io = io.BytesIO()
tts.write_to_fp(byte_io)
byte_io.seek(0)
return send_file(byte_io, mimetype='audio/mpeg')
# 返回语音文件给前端
if __name__ == '__main__':
app.run()