gpt4 book ai didi

python - iOS+GoogleChrome 上的“内容处置”[Google App Engine 上的 Flask]

转载 作者:行者123 更新时间:2023-11-29 05:09:18 25 4
gpt4 key购买 nike

我是编码初学者。我想使用 Google Cloud Text to Speech API 制作一个简单的 Web 应用程序。

  1. 带有文本框的网站
  2. 您在文本框中输入一个句子,然后单击“提交”按钮
  3. 您可以将 Google Cloud Text 制作的 mp3 文件下载到语音API

我是日本的一名英语老师,所以我希望我的学生使用这个网站来提高他们的英语发音。

首先,我想向您展示我的代码。我在 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(Google Chrome)访问我的网络应用程序时,下载没有开始。

从 PC Chrome 浏览器:

第 1 步:如果我在浏览器(Windows10 上的 Google Chrome)上输入应用程序的 URL,我会看到这个简单的表单。 form

第2步:我输入文本并按“提交”。 submit

第三步:下载mp3文件到本地下载文件夹。 download

从 iPhone10 Chrome 开始:

步骤1和2与PC Chrome相同

第 3 步:下载无法开始。我希望在"file"应用程序(iPhone 的 native 文件管理器应用程序)上下载 mp3 文件。 enter image description here

我认为这是'content-disposition'的问题,所以我搜索了“iOS上的content-disposition”。但是,我无法得到答案。

您能给我任何信息或建议吗?

提前谢谢您。

此致,卡祖

最佳答案

我尝试使用 Safafi 而不是 Chrome,效果非常好!

关于python - iOS+GoogleChrome 上的“内容处置”[Google App Engine 上的 Flask],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59837489/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com