Newer
Older
import argparse
from multiprocessing import Process

Christopher Randolph Rhodes
committed
import requests

Christopher Randolph Rhodes
committed
from requests.adapters import HTTPAdapter
from urllib3 import Retry

Christopher Randolph Rhodes
committed
import webbrowser
from model_server.conf.defaults import server_conf

Christopher Randolph Rhodes
committed
def parse_args():
parser = argparse.ArgumentParser(
description='Start model server with optional arguments',
)

Christopher Randolph Rhodes
committed
parser.add_argument(
'--confpath',
default='model_server.conf.fastapi',

Christopher Randolph Rhodes
committed
help='path to server startup configuration',
)
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'
)
parser.add_argument(
'--reload',
action='store_true',
help='automatically restart server when changes are noticed, for development purposes'
)
return parser.parse_args()

Christopher Randolph Rhodes
committed
def main(args) -> None:
print('CLI args:\n' + str(args))
server_process = Process(
target=uvicorn.run,

Christopher Randolph Rhodes
committed
args=(f'{args.confpath}:app',),
# 'app_dir': Path('..').resolve().__str__(),
'app_dir': '..',
'host': args.host,
'port': int(args.port),
'log_level': 'debug',
'reload': args.reload,
daemon=(args.reload is False),

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

Christopher Randolph Rhodes
committed
server_process.start()
try:

Christopher Randolph Rhodes
committed
sesh = requests.Session()
retries = Retry(
total=5,
backoff_factor=0.1,
)
sesh.mount('http://', HTTPAdapter(max_retries=retries))
resp = sesh.get(url)

Christopher Randolph Rhodes
committed
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()
if __name__ == '__main__':