是否可以通过Google Cloud Engine中的Flask应用在本地计算机上播放音频?

问题描述

我正在尝试创建一个用户可以在其中输入文本的网站,该文本将在我的本地计算机上读出

为实现这一目标,我使用了一个简单的Flask应用程序和Google云文字进行语音转换,并通过Google App Engine进行了部署。 该应用程序正常运行,并且在应用程序引擎错误日志中未显示任何错误,但是音频不会播放

我想知道如何从应用程序发送音频以由本地计算机播放。可能吗?还是我需要使用完全不同的方法?我对所有这些工具都是新手。

这是我的main.py:

    from flask import Flask,render_template,send_file,request
    from gtts import gTTS
    from google.cloud import texttospeech
    import os
    import tempfile
    from playsound import playsound

    app = Flask(__name__)


    @app.route("/")
    def homepage():
        return render_template("page.html",title="HOME PAGE")

    @app.route('/',methods=['GET','POST'])
    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-Standard-C',ssml_gender=texttospeech.enums.SsmlVoiceGender.FEMALE)

            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)
                os.system("start output.mp3")
            thankyou = "thanks"
            return thankyou

    if __name__ == "__main__":
        app.run(debug=True)

这是page.html:

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Talk to Wednesday</title>
  </head>
  <body>
    <h1> Talk to Me </h1>
    <form method="POST">
      <input name="text">
      <input type="submit">
    </form>
  </body>
</html>

App.yml仅具有:“运行时:python37”

Requirements.txt:

   Flask==1.1.2
   pyttsx3
   gtts
   google-cloud-texttospeech==1.0.1
   gunicorn==20.0.4; python_version > '3.0'
   backports.tempfile
   playsound

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)