简体   繁体   中英

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

I'm an English teacher in Japan. Because of COVID-19, my school has been shut down for a month and will be for another month. So I made a web app which my students can use to improve their English pronunciation at home.

The web app is very simple.

  1. a web site with a text box

  2. you input a sentence in the text box and click a button "submit"

  3. you can download a mp3 file which is made by Google Cloud Text to Speech API

Here is my app's source code. https://github.com/k2kszk/speech-synthesizer I use Flask on Google App Engine standard environment Python3.7.

My students started using the web app, then I found error messages in Google Cloud Platform's console. Google Cloud Platform's console "Error Reporting" page. Here is the message.

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)

I searched the Internet, but I couldn't find even what is the problem. How can I solve this error? Could you give me any advice or information?

Thank you in advance.

Sincerely, Kazu

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

Here is my 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()

In this case the issue is on the voiceid line.

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

Which is raising StopIteration because its selection criteria is not satisfied. You can change it to

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

Then, voiceid will be set to None (or some other value you provide) in that situation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM