11import collections , json , logging , os , requests , signal , time
22from tornado import httpserver , ioloop , web
3+ from tornado .options import define , options , parse_command_line
34
5+ # Command Line Options
6+ define ("port" , default = 8088 , help = "Port the web app will run on" )
7+ define ("ml-endpoint" , default = "http://localhost:5000" , help = "The Image Caption Generator REST endpoint" )
48
59# Setup Logging
610logging .basicConfig (level = os .environ .get ("LOGLEVEL" , "INFO" ), format = '%(levelname)s: %(message)s' )
711
812# Global variables
9- ml_endpoint = "http://localhost:5000/model/predict"
1013static_img_path = "static/img/images/"
1114temp_img_prefix = "MAX-"
1215image_captions = collections .OrderedDict ()
@@ -80,7 +83,7 @@ def signal_handler(sig, frame):
8083def shutdown ():
8184 logging .info ("Cleaning up image files" )
8285 clean_up ()
83- logging .info ("Stopping server" )
86+ logging .info ("Stopping web server" )
8487 server .stop ()
8588 ioloop .IOLoop .current ().stop ()
8689
@@ -101,27 +104,32 @@ def make_app():
101104
102105
103106def main ():
107+ parse_command_line ()
108+
109+ global ml_endpoint
110+ ml_endpoint = options .ml_endpoint + "/model/predict"
111+ logging .debug ("Connecting to ML endpoint at %s" , ml_endpoint )
112+
104113 try :
105114 resp = requests .get (ml_endpoint )
106115 except requests .exceptions .ConnectionError :
107- logging .error ("Cannot connect to the Object Detection API" )
108- logging .error ("Please run the Object Detection API docker image first" )
116+ logging .error ("Cannot connect to the Image Caption Generator REST endpoint at %s" , options .ml_endpoint )
109117 raise SystemExit
110118
111- logging .info ("Starting Server" )
112- global server
119+ logging .info ("Starting web server" )
113120 app = make_app ()
121+ global server
114122 server = httpserver .HTTPServer (app )
115- server .listen (8088 )
123+ server .listen (options . port )
116124 signal .signal (signal .SIGINT , signal_handler )
117125
118- logging .info ("Preparing ML Metadata " )
126+ logging .info ("Preparing ML metadata " )
119127 start = time .time ()
120128 prepare_metadata ()
121129 end = time .time ()
122130 logging .info ("Metadata prepared in %s seconds" , end - start )
123131
124- logging .info ("Use Ctrl+C to stop server" )
132+ logging .info ("Use Ctrl+C to stop web server" )
125133 ioloop .IOLoop .current ().start ()
126134
127135
0 commit comments