-
Notifications
You must be signed in to change notification settings - Fork 0
/
tornado_scikit_learn.py
executable file
·82 lines (65 loc) · 2.97 KB
/
tornado_scikit_learn.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
#!/usr/bin/python
'''Starts and runs the scikit learn server'''
# For this to run properly, MongoDB must be running
# Navigate to where mongo db is installed and run
# something like $./mongod --dbpath "../data/db"
# might need to use sudo (yikes!)
# database imports
from pymongo import MongoClient
from pymongo.errors import ServerSelectionTimeoutError
# tornado imports
import tornado.web
from tornado.web import HTTPError
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
from tornado.options import define, options
# custom imports
from basehandler import BaseHandler
import sklearnhandlers as skh
# Setup information for tornado class
define("port", default=8000, help="run on the given port", type=int)
# Utility to be used when creating the Tornado server
# Contains the handlers and the database connection
class Application(tornado.web.Application):
def __init__(self):
'''Store necessary handlers,
connect to database
'''
handlers = [(r"/[/]?", BaseHandler),
(r"/Handlers[/]?", skh.PrintHandlers),
(r"/GetClasses[/]?", skh.GetClasses),
(r"/AddDataPoint[/]?", skh.UploadLabeledDatapointsHandler),
(r"/ClearDataset[/]?", skh.ClearDataset),
(r"/UpdateModel[/]?", skh.UpdateModel),
(r"/PredictOne[/]?", skh.PredictOne),
(r"/SetParameters[/]?", skh.SetParameters),
(r"/GetParameters[/]?", skh.GetParameters),
]
self.handlers_string = str(handlers)
try:
self.client = MongoClient(serverSelectionTimeoutMS=50) # local host, default port
print(self.client.server_info()) # force pymongo to look for possible running servers, error if none running
# if we get here, at least one instance of pymongo is running
self.db = self.client.sklearndatabase # database with labeledinstances, models
except ServerSelectionTimeoutError as inst:
print('Could not initialize database connection, stopping execution')
print('Are you running a valid local-hosted instance of mongodb?')
#raise inst
self.clf = {} # the classifier model (in-class assignment, you might need to change this line!)
# but depending on your implementation, you may not need to change it ¯\_(ツ)_/¯
self.KNeighborsNParam = 3
self.KNeighborsAlgorithmParam = 'auto'
self.RandomForestNParam = 10
settings = {'debug':True}
tornado.web.Application.__init__(self, handlers, **settings)
def __exit__(self):
self.client.close() # just in case
def main():
'''Create server, begin IOLoop
'''
tornado.options.parse_command_line()
http_server = HTTPServer(Application(), xheaders=True)
http_server.listen(options.port)
IOLoop.instance().start()
if __name__ == "__main__":
main()