-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
125 lines (106 loc) · 3.89 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
113
114
115
116
117
118
119
120
121
122
123
124
125
import json
import os
import traceback
import tornado.httpserver
import tornado.ioloop
import tornado.web
from motor import motor_tornado
from tornado.gen import coroutine
from tornado.options import define, options
import tornado.escape
import base64
from send import send
from utility import predict
from io import BytesIO
from google.cloud import firestore
import base64
# import firebase_admin
# from firebase_admin import credentials
import numpy as np
from PIL import Image
define("port", default=8000, help="runs on the given port", type=int)
class MyAppException(tornado.web.HTTPError):
pass
class BaseHandler(tornado.web.RequestHandler):
def db(self):
clientz = self.settings['db_client']
db = clientz.projectx
return db
def write_error(self, status_code, **kwargs):
self.set_header('Content-Type', 'application/json')
if self.settings.get("serve_traceback") and "exc_info" in kwargs:
# in debug mode, try to send a traceback
lines = []
for line in traceback.format_exception(*kwargs["exc_info"]):
lines.append(line)
self.write(json.dumps({
'status_code': status_code,
'message': self._reason,
'traceback': lines,
}))
else:
self.write(json.dumps({
'status_code': status_code,
'message': self._reason,
}))
class my404handler(BaseHandler):
def get(self):
self.set_header('Content-Type', 'application/json')
self.write(json.dumps({
'status_code': 404,
'message': 'illegal call.'
}))
class test(BaseHandler):
def get(self):
self.write(json.dumps({"abc": "def"}))
class MLHandler(BaseHandler):
def set_default_headers(self):
self.set_header("Access-Control-Allow-Origin", "*")
self.set_header("Access-Control-Allow-Headers", "x-requested-with")
self.set_header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS')
self.set_header('Content-type', 'application/json')
@coroutine
def post(self):
file_body = self.request.files['pic'][0]['body']
img = Image.open(BytesIO(file_body))
img.save("current.jpg")
img = np.array(img).astype('uint8')
ml_response = yield predict.predict(img)
gp = self.get_body_argument("gps")
print(ml_response)
# db = self.db()
# details = yield db.find_one({"District": "Vellore"})
with open("current.jpg", "rb") as f:
ig = base64.b64encode(f.read())
if int(send(gp, ig)) != 202:
self.write(json.dumps({"status": "something went wrong"}))
if not ml_response:
if int(send(gp, ig, em="svineth.face@gmail.com")) != 202 and int(send(gp, ig)) != 202:
self.write(json.dumps({"status": "something went wrong"}))
fire = firestore.Client()
x = fire.collection("data").document("1")
payload = {
"gps": gp,
"label": ml_response['img_label'],
"img": ig
}
y = x.get().to_dict()['data']
y.append(payload)
x.set({"data": y})
self.write(json.dumps({'flag': 'Everthing is coool'}))
if __name__ == "__main__":
options.parse_command_line()
client = motor_tornado.MotorClient("mongodb://user:pass@ds143030.mlab.com:43030/projectx")
app = tornado.web.Application(
handlers=[
(r"/", test),
(r"/mlpredict", MLHandler)
],
default_handler_class=my404handler,
debug=True,
db_client=client
)
http_server = tornado.httpserver.HTTPServer(app)
http_server.listen(os.environ.get("PORT", options.port))
print('listening on 8000')
tornado.ioloop.IOLoop.instance().start()