-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
88 lines (72 loc) · 2.23 KB
/
api.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
import logging
import flask
from flasgger import Swagger
from flask import Flask, request, jsonify, Response
from flask_cors import CORS
import predict
from predict import predicting
from predict import load_model
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.DEBUG)
logger = logging.getLogger(__name__)
# NOTE this import needs to happen after the logger is configured
# Initialize the Flask application
application = Flask(__name__)
application.config['ALLOWED_EXTENSIONS'] = set(['pdf'])
application.config['CONTENT_TYPES'] = {"pdf": "application/pdf"}
application.config["Access-Control-Allow-Origin"] = "*"
CORS(application)
swagger = Swagger(application)
model, char2idx, idx2char = load_model()
def clienterror(error):
resp = jsonify(error)
resp.status_code = 400
return resp
def notfound(error):
resp = jsonify(error)
resp.status_code = 404
return resp
@application.route('/Stephen-King-Bot', methods=['POST'])
def sentiment_classification():
"""Run Stephen King text generator given text.
---
parameters:
- name: body
in: body
schema:
id: text
required:
- text
properties:
text:
type: string
description: Starter string for the bot to generate text in the style of Stephen King from
required: true
definitions:
SentimentResponse:
Project:
properties:
status:
type: string
ml-result:
type: object
responses:
Generated Text:
description: Example Output
examples:
[
{
"Input": "Stephen",
"Output": "Stephen King is the greatest author."
},]
"""
json_request = request.get_json()
if not json_request:
return Response("No json provided.", status=400)
text = json_request['text']
if text is None:
return Response("No text provided.", status=400)
else:
label = predicting(model, char2idx, idx2char, text)
return label
if __name__ == '__main__':
application.run(debug=True, use_reloader=True)