-1

i'm trying to make an app in flask-python that using a json post send an audio using the gtts google library as the answer, but my method doesn't work. My code is the following.

from gtts import gTTS
from flask import Flask, send_file, request



app = Flask(__name__)

@app.route("/")
def t2s():
    text = request.get_json()
    obj = gTTS(text = text, slow = False, lang = 'en')    
    obj.save('audio.wav')
    return send_file('audio.wav')


if __name__ == "__main__":
    app.run(port=3000  , debug=True, use_reloader=True, host='0.0.0.0') 

any suggestions?, i'm using postman.

Thanks a lot for the possible help

0xTochi
  • 13
  • 5
  • (You really need a better explanation of what goes wrong than "doesn't work"!) What does your JSON data look like? It could quite easily be something that isn't appropriate as the `text=` parameter to `gTTS()`. – jasonharper Dec 11 '19 at 21:54
  • what means `doesn't work`? We can't read in your mind and don't expect that we will run code to see how it works. Besides it can works correctly on our computers. – furas Dec 11 '19 at 22:03
  • always put full error message (starting at word "Traceback") in question (not comment) as text (not screenshot). There are other useful information. – furas Dec 11 '19 at 22:03
  • if you want to send `POST` to this Flask then you have to use `@app.route("/", methods=['POST', 'GET'])` to get it. – furas Dec 11 '19 at 22:08

1 Answers1

3

Flask as default doesn't get POST requests and you have to use methods=['POST', 'GET']

from gtts import gTTS
from flask import Flask, send_file, request

app = Flask(__name__)

@app.route("/", methods=['POST', 'GET'])
def t2s():
    text = request.get_json()
    print(text)
    obj = gTTS(text = text, slow = False, lang = 'en')    
    obj.save('audio.wav')
    return send_file('audio.wav')

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=3000) 

And test (using mplayer on Linux):

import requests
import os

data = 'Hello World'

r = requests.post('http://127.0.0.1:3000/', json=data)

open('output.wav', 'wb').write(r.content)

os.system('mplayer output.wav')
furas
  • 134,197
  • 12
  • 106
  • 148