RWKV-Runner/backend-python/main.py

132 lines
3.0 KiB
Python
Raw Normal View History

2023-08-14 22:07:15 +08:00
import time
start_time = time.time()
2023-11-04 20:21:55 +08:00
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")
2023-11-07 22:22:29 +08:00
group.add_argument(
"--webui",
action="store_true",
help="whether to enable WebUI (default: False)",
)
2023-11-04 20:21:55 +08:00
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()
2023-05-06 20:17:39 +08:00
import os
2023-05-17 11:39:00 +08:00
import sys
2023-05-06 20:17:39 +08:00
2023-05-20 23:34:33 +08:00
sys.path.append(os.path.dirname(os.path.realpath(__file__)))
import psutil
2023-11-04 20:21:55 +08:00
from contextlib import asynccontextmanager
2023-06-03 17:12:59 +08:00
from fastapi import Depends, FastAPI
2023-05-06 20:17:39 +08:00
from fastapi.middleware.cors import CORSMiddleware
import uvicorn
2023-05-07 17:27:54 +08:00
from utils.rwkv import *
from utils.torch import *
from utils.ngrok import *
2023-06-03 17:12:59 +08:00
from utils.log import log_middleware
2023-10-25 17:14:33 +08:00
from routes import completion, config, state_cache, midi, misc, file_process
2023-05-07 17:27:54 +08:00
import global_var
2023-05-06 20:17:39 +08:00
2023-10-25 17:07:40 +08:00
@asynccontextmanager
async def lifespan(app: FastAPI):
init()
yield
app = FastAPI(lifespan=lifespan, dependencies=[Depends(log_middleware)])
2023-05-06 20:17:39 +08:00
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
2023-05-07 17:27:54 +08:00
app.include_router(completion.router)
app.include_router(config.router)
2023-07-25 16:11:17 +08:00
app.include_router(midi.router)
2023-10-25 17:14:33 +08:00
app.include_router(file_process.router)
app.include_router(misc.router)
app.include_router(state_cache.router)
2023-05-07 17:27:54 +08:00
2023-05-06 20:17:39 +08:00
2023-11-07 22:22:29 +08:00
@app.post("/exit", tags=["Root"])
def exit():
parent_pid = os.getpid()
parent = psutil.Process(parent_pid)
for child in parent.children(recursive=True):
child.kill()
parent.kill()
try:
if (
"RWKV_RUNNER_PARAMS" in os.environ
and "--webui" in os.environ["RWKV_RUNNER_PARAMS"].split(" ")
) or args.webui:
from webui_server import webui_server
app.mount("/", webui_server)
except NameError:
pass
@app.get("/", tags=["Root"])
def read_root():
return {"Hello": "World!"}
2023-05-06 20:17:39 +08:00
def init():
2023-05-07 17:27:54 +08:00
global_var.init()
2023-08-14 22:07:15 +08:00
cmd_params = os.environ["RWKV_RUNNER_PARAMS"]
global_var.set(
global_var.Args, get_args(cmd_params.split(" ") if cmd_params else None)
)
state_cache.init()
2023-05-06 20:17:39 +08:00
set_torch()
if os.environ.get("ngrok_token") is not None:
ngrok_connect()
2023-08-14 22:07:15 +08:00
if __name__ == "__main__":
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)