簡體   English   中英

dispatch_request (/env/lib/python3.7/site-packages/flask/app.py:1935) [谷歌應用引擎上的燒瓶]

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

我是日本的英語老師。 因為 COVID-19,我的學校已經停課一個月,而且還會再停一個月。 所以我做了一個 web 應用程序,我的學生可以用它來提高他們在家中的英語發音。

web 應用程序非常簡單。

  1. 帶有文本框的 web 站點

  2. 您在文本框中輸入一個句子,然后單擊“提交”按鈕

  3. 您可以下載由 Google Cloud Text to Speech API 制作的 mp3 文件

這是我的應用程序的源代碼。 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)

我搜索了互聯網,但我什至找不到問題所在。 我該如何解決這個錯誤? 你能給我一些建議或信息嗎?

先感謝您。

真誠的,卡祖

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

這是我的 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()

在這種情況下,問題出在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 (或您提供的其他值)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM