dispatch_request(/env/lib/python3.7/site-packages/flask/app.py:1935)[Google App Engine上的Flask]

问题描述 投票:0回答:1

我是日本的英语老师。由于COVID-19,我的学校已经关闭了一个月,并且将再关闭一个月。因此,我制作了一个网络应用程序,我的学生可以在家里改善他们的英语发音。

Web应用程序非常简单。

  1. 一个带有文本框的网站

  2. 您在文本框中输入一个句子,然后单击按钮“提交”

  3. 您可以将Google Cloud Text制作的mp3文件下载到语音API

这是我的应用程序的源代码。https://github.com/k2kszk/speech-synthesizer我在Google App Engine标准环境Python3.7上使用Flask。

我的学生开始使用Web应用程序,然后我在Google Cloud Platform的控制台中发现了错误消息。Google Cloud Platform的控制台“错误报告”页面。这是消息。

File "/srv/main.py", line 20, in index: voiceid = next(voice for voice in request.form.getlist('voiceId[]') if accent in voice)
at dispatch_request (/env/lib/python3.7/site-packages/flask/app.py:1935)
at full_dispatch_request (/env/lib/python3.7/site-packages/flask/app.py:1949)
at reraise (/env/lib/python3.7/site-packages/flask/_compat.py:39)
at handle_user_exception (/env/lib/python3.7/site-packages/flask/app.py:1820)
at full_dispatch_request (/env/lib/python3.7/site-packages/flask/app.py:1951)
at wsgi_app (/env/lib/python3.7/site-packages/flask/app.py:2446)

我搜索了Internet,但什至没有找到问题所在。我该如何解决这个错误?您能给我任何建议或信息吗?

先谢谢您。

此致,Kazu

++++++++++++++++

这是我的main.py。

#./advance/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":
        if request.form['Radio'] == 'normal':
            ssml = '<speak><prosody rate="slow">' + request.form['text'] + '</prosody></speak>'
        else:
            ssml = '<speak>' + request.form['text'] + '</speak>'

        accent = request.form['accent']
        voiceid = next(voice for voice in request.form.getlist('voiceId[]') if accent in voice)
        os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="credentials.json"

        client = texttospeech.TextToSpeechClient()
        input_text = texttospeech.types.SynthesisInput(ssml=ssml)
        voice = texttospeech.types.VoiceSelectionParams(
            language_code=accent,
            name=voiceid)

        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()
python google-app-engine flask text-to-speech google-text-to-speech
1个回答
0
投票

在这种情况下,问题出在voiceid行上。

voiceid = next(voice for voice in request.form.getlist('voiceId[]') if accent in voice)

正在提高StopIteration,因为不满足其选择标准。您可以将其更改为

voiceid = voiceid = next((voice for voice in request.form.getlist('voiceId[]') if accent in voice), None) 

然后,在这种情况下,voiceid将设置为None(或您提供的其他值)。

© www.soinside.com 2019 - 2024. All rights reserved.