Newer
Older
import argparse
from multiprocessing import Process

Christopher Randolph Rhodes
committed
import requests

Christopher Randolph Rhodes
committed
import webbrowser
from conf.defaults import server_conf
def parse_args():
parser = argparse.ArgumentParser(
description='Start model server with optional arguments',
)
parser.add_argument(
'--host',
help='bind socket to this host'
)
parser.add_argument(
'--port',
default=str(server_conf['port']),
help='bind socket to this port',
)
parser.add_argument(
'--debug',
action='store_true',
help='display extra information that is helpful for debugging'
)
return parser.parse_args()
if __name__ == '__main__':
args = parse_args()
print('CLI args:\n' + str(args))
server_process = Process(
target=uvicorn.run,
args=('model_server.api:app',),
kwargs={
'app_dir': '.',
'host': args.host,
'port': int(args.port),
'log_level': 'debug',
},
daemon=True,
)

Christopher Randolph Rhodes
committed
url = f'http://{args.host}:{int(args.port):04d}/status'
print(url)

Christopher Randolph Rhodes
committed
server_process.start()
try:
resp = requests.get(url)
assert resp.status_code == 200
except Exception:
print('Error starting server')
server_process.terminate()
exit()

Christopher Randolph Rhodes
committed
webbrowser.open(url, new=1, autoraise=True)
if args.debug:
print('Running in debug mode')
print('Type "STOP" to stop server')
input_str = ''
while input_str.upper() != 'STOP':
input_str = input()
server_process.terminate()