-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.py
78 lines (57 loc) · 1.86 KB
/
app.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
#!/usr/bin/python
import sys
import json
import collections
import time
from flask import Flask, request
from engine import classify, load_resources
from werkzeug.wsgi import LimitedStream
reload(sys)
sys.setdefaultencoding('utf-8')
app = Flask(__name__)
app.config.from_object('config')
# Use this metod to create nested JSON
def tree():
return collections.defaultdict(tree)
# Begin routing
@app.route('/classify', methods=['POST'])
def getarticle():
# Get the item
article_post = str(request.form['post'].lower())
start_time = time.time()
# Begin prediction
label = classify(article_post)
# Create the JSON from the returned prediction value
data = tree()
data['article'] = article_post
data['label'] = label
data['process_time'] = process_time(start_time)
data = json.dumps(data)
# Get item detail from Facetly
returnValue = data
# Return the value back to the web
return(returnValue, 201)
def process_time(start_time):
process = time.strftime('%H:%M:%S', time.gmtime((time.time() - start_time)))
return process
class StreamConsumingMiddleware(object):
def __init__(self, app):
self.app = app
def __call__(self, environ, start_response):
stream = LimitedStream(environ['wsgi.input'],
int(environ['CONTENT_LENGTH'] or 0))
environ['wsgi.input'] = stream
app_iter = self.app(environ, start_response)
try:
stream.exhaust()
for event in app_iter:
yield event
finally:
if hasattr(app_iter, 'close'):
app_iter.close()
if __name__ == '__main__':
app.wsgi_app = StreamConsumingMiddleware(app.wsgi_app)
# Load pickle resources first
load_resources()
# app.run(host="127.0.0.1", port=1234, debug=True)
app.run(host="0.0.0.0", port=5050, debug=True)