-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
112 lines (91 loc) · 3.9 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
from botocore.exceptions import BotoCoreError, ClientError
from boto3 import Session
from flask import Flask, render_template, request, url_for, jsonify, send_file, Response
from flask_fontawesome import FontAwesome
from argparse import ArgumentParser
import os
import sys
# Init Flask App
app = Flask(__name__)
app.secret_key = os.urandom(12) # Generic key for dev purposes only
fa = FontAwesome(app)
# ======== AWS Polly Setup =========================================================== #
# Mapping possible user browser suported audio formats to their corresponding
# response code for AWS Polly
AUDIO_FORMATS = {"ogg_vorbis": "audio/ogg",
"mp3": "audio/mpeg",
"pcm": "audio/wave; codecs=1"}
# Create a client using the credentials and region defined in the adminuser
# section of the AWS credentials and configuration files
# For more information read the READEME.md file
session = Session(profile_name="adminuser")
polly = session.client("polly")
# ======== Simple Exception Handler =========================================================== #
class InvalidUsage(Exception):
status_code = 400
def __init__(self, message, status_code=None, payload=None):
Exception.__init__(self)
self.message = message
if status_code is not None:
self.status_code = status_code
self.payload = payload
def to_dict(self):
rv = dict(self.payload or ())
rv['message'] = self.message
return rv
@app.errorhandler(InvalidUsage)
def handle_invalid_usage(error):
response = jsonify(error.to_dict())
response.status_code = error.status_code
return response
# ======== Routing =========================================================== #
# -------- Home ---------------------------------------------------------- #
@app.route('/', methods=['GET'])
def index():
return render_template('index.html')
# -------- ASK ---------------------------------------------------------- #
@app.route('/ask', methods=['POST'])
def ask():
query = request.json['q']
# TODO: Analyse the query with infromation extraction,
# and add a dialog manager to keep track of teh conversation.
response = query
return jsonify(
response=response,
)
# -------- READ ---------------------------------------------------------- #
@app.route('/read', methods=['GET'])
def read():
"""
Handles routing for speech synthesis by Amazon Polly
"""
try:
outputFormat = request.args.get('outputFormat')
text = request.args.get('text')
voiceId = request.args.get('voiceId')
except TypeError:
raise InvalidUsage("Wrong parameters", status_code=400)
# Validate the parameters, set error flag in case of unexpected values
if len(text) == 0 or len(voiceId) == 0 or \
outputFormat not in AUDIO_FORMATS:
raise InvalidUsage("Wrong parameters", status_code=400)
else:
try:
# Request speech synthesis
response = polly.synthesize_speech(Text=text,
VoiceId=voiceId,
OutputFormat=outputFormat)
except (BotoCoreError, ClientError) as err:
# The service returned an error
raise InvalidUsage(str(err), status_code=500)
return send_file(response.get("AudioStream"),
AUDIO_FORMATS[outputFormat])
# ======== Main ============================================================== #
if __name__ == '__main__':
parser = ArgumentParser(description='Example Flask Application')
parser.add_argument("-p", "--port", type=int,
metavar="PORT", dest="port", default=5000, help='Port number')
parser.add_argument("--host", type=str, metavar="HOST",
dest="host", default="localhost")
args = parser.parse_args()
app.run(host=args.host, port=args.port, debug=True)