From 1dcda47013e20c7b64110adcf0b7820f72000f00 Mon Sep 17 00:00:00 2001 From: josc146 Date: Sat, 4 Nov 2023 20:21:55 +0800 Subject: [PATCH] improve startup process --- backend-python/main.py | 66 ++++++++++++++++++++++-------------------- 1 file changed, 35 insertions(+), 31 deletions(-) diff --git a/backend-python/main.py b/backend-python/main.py index ddb9145..58e472a 100644 --- a/backend-python/main.py +++ b/backend-python/main.py @@ -2,16 +2,47 @@ import time start_time = time.time() -import setuptools # avoid warnings +import argparse +from typing import Union, Sequence + + +def get_args(args: Union[Sequence[str], None] = None): + parser = argparse.ArgumentParser() + group = parser.add_argument_group(title="server arguments") + group.add_argument( + "--port", + type=int, + default=8000, + help="port to run the server on (default: 8000)", + ) + group.add_argument( + "--host", + type=str, + default="127.0.0.1", + help="host to run the server on (default: 127.0.0.1)", + ) + group = parser.add_argument_group(title="mode arguments") + group.add_argument( + "--rwkv-beta", + action="store_true", + help="whether to use rwkv-beta (default: False)", + ) + args = parser.parse_args(args) + + return args + + +if __name__ == "__main__": + args = get_args() + + import os import sys -import argparse -from typing import Sequence -from contextlib import asynccontextmanager sys.path.append(os.path.dirname(os.path.realpath(__file__))) import psutil +from contextlib import asynccontextmanager from fastapi import Depends, FastAPI from fastapi.middleware.cors import CORSMiddleware import uvicorn @@ -77,34 +108,7 @@ def exit(): parent.kill() -def get_args(args: Union[Sequence[str], None] = None): - parser = argparse.ArgumentParser() - group = parser.add_argument_group(title="server arguments") - group.add_argument( - "--port", - type=int, - default=8000, - help="port to run the server on (default: 8000)", - ) - group.add_argument( - "--host", - type=str, - default="127.0.0.1", - help="host to run the server on (default: 127.0.0.1)", - ) - group = parser.add_argument_group(title="mode arguments") - group.add_argument( - "--rwkv-beta", - action="store_true", - help="whether to use rwkv-beta (default: False)", - ) - args = parser.parse_args(args) - - return args - - if __name__ == "__main__": - args = get_args() os.environ["RWKV_RUNNER_PARAMS"] = " ".join(sys.argv[1:]) print("--- %s seconds ---" % (time.time() - start_time)) uvicorn.run("main:app", port=args.port, host=args.host, workers=1)