Last active
August 29, 2015 14:20
-
-
Save vierja/91e41401a3b8368d6cb4 to your computer and use it in GitHub Desktop.
Voice API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, request, jsonify | |
import argparse | |
import grequests | |
import json | |
app = Flask(__name__) | |
SLAVES = data = [line.strip() for line in open('SLAVES', 'r')] | |
def res_meaning(r): | |
if r is None: | |
return 'DEAD' | |
elif r.status_code == 200: | |
return 'OK' | |
else: | |
return 'ERROR' | |
@app.route('/') | |
def homepage(): | |
return 'UI missing, talk to /talk.' | |
@app.route('/talk', methods=['POST']) | |
def talk(): | |
msj = request.get_json(force=True) | |
rs = (grequests.post(slave, data=json.dumps(msj)) for slave in SLAVES) | |
res = grequests.map(rs) | |
return jsonify(dict( | |
[ | |
('Slave: {}'.format(num), res_meaning(r)) | |
for num, r in enumerate(res) | |
] | |
)) | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser() | |
parser.add_argument("-d", action="store_true", dest="debug", default=False) | |
parser.add_argument("-p", dest="port", type=int, default=5000) | |
cmd_args = parser.parse_args() | |
app.run(host='0.0.0.0', debug=cmd_args.debug, port=cmd_args.port) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Flask | |
pyttsx | |
grequests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, request, jsonify | |
import argparse | |
import os | |
import pyttsx | |
import sys | |
if sys.platform == "darwin": | |
USE_PYTTSX = False | |
else: | |
USE_PYTTSX = True | |
app = Flask(__name__) | |
@app.route('/', methods=['POST']) | |
def talk(): | |
json = request.get_json(force=True) | |
text = json.get('text', '') | |
voice = json.get('voice', 'spanish-latam-am') | |
rate = json.get('rate', 140) | |
if USE_PYTTSX: | |
engine = pyttsx.init() | |
engine.setProperty('voice', voice) | |
engine.setProperty('rate', rate) | |
engine.say(text) | |
engine.runAndWait() | |
else: | |
os.system('say {}'.format(text)) | |
return jsonify({'said': True, 'text': text}) | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser() | |
parser.add_argument("-d", action="store_true", dest="debug", default=False) | |
parser.add_argument("-p", dest="port", type=int, default=5000) | |
cmd_args = parser.parse_args() | |
app.run(host='0.0.0.0', debug=cmd_args.debug, port=cmd_args.port) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
http://127.0.0.1:5000/ | |
http://127.0.0.1:5001/ | |
http://127.0.0.1:5002/ | |
http://127.0.0.1:5003/ | |
http://127.0.0.1:5004/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment