我是编码的初学者。我想使用 Google Cloud Text to Speech API 制作一个简单的 Web 应用程序。
- 带有文本框的网站
- 您在文本框中输入一个句子,然后单击“提交”按钮
- 您可以下载由 Google Cloud Text to Speech API 制作的 mp3 文件
我是日本的英语老师,所以我希望我的学生使用这个网站来提高他们的英语发音。
首先,我想向您展示我的代码。我在 Google App Engine 标准环境 Python3.7 上使用 Flask。
这是目录结构。
.
├── app.yaml
├── credentials.json
├── main.py
├── requirements.txt
└── templates
└── index.html
这是 main.py。
from flask import Flask
from flask import render_template
from flask import request
from flask import send_file
import os
from google.cloud import texttospeech
app = Flask(__name__)
@app.route("/", methods=['POST', 'GET'])
def index():
if request.method == "POST":
text = request.form['text']
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="credentials.json"
client = texttospeech.TextToSpeechClient()
input_text = texttospeech.types.SynthesisInput(text=text)
voice = texttospeech.types.VoiceSelectionParams(
language_code='en-US',
name='en-US-Wavenet-A')
audio_config = texttospeech.types.AudioConfig(
audio_encoding=texttospeech.enums.AudioEncoding.MP3)
response = client.synthesize_speech(input_text, voice, audio_config)
# The response's audio_content is binary.
with open('/tmp/output.mp3', 'wb') as out:
out.write(response.audio_content)
return send_file("/tmp/output.mp3",as_attachment=True)
else:
return render_template("index.html")
if __name__ == "__main__":
app.run()
这是 index.html
<html>
<head>
<title>practice pronunciation</title>
<style>
#text {width: 100%; height: 300px;}
</style>
</head>
<body>
<h1>Let's practice pronunciation!</h1>
<form action="/" method="POST">
<textarea id="text" name="text" placeholder="Type some text here..."></textarea>
<br/>
<input type="submit">
</form>
</body>
</html>
这是 requirements.txt。
Flask==1.1.1
future==0.18.2
google-cloud-texttospeech==0.5.0
grpcio==1.26.0
gunicorn
这是 app.yaml。
runtime: python37
entrypoint: gunicorn -b :$PORT main:app
当我通过 PC(Windows10 上的 Google Chrome)访问我的应用程序时,它运行良好。但是,当我从 iPhone10(谷歌浏览器)访问我的网络应用程序时,下载没有开始。
从 PC 铬:
第 1 步:如果我在浏览器(Windows10 上的 Google Chrome)上输入我的应用程序的 URL,我会看到这个简单的表单。
从 iPhone10 铬:
Step1 和 2 与 PC Chrome 相同
步骤 3:下载未开始。我希望在“文件”应用程序(iPhone 的本机文件管理器应用程序)上下载 mp3 文件。
我认为这是“内容处置”的问题,所以我搜索了“iOS 上的内容处置”。但是,我无法得到答案。
你能给我任何信息或建议吗?
先感谢您。
真诚的,卡祖