add python webui server

This commit is contained in:
josc146 2023-11-07 22:22:29 +08:00
parent 46f52923c3
commit db6fbe8366
2 changed files with 45 additions and 14 deletions

View File

@ -22,6 +22,11 @@ def get_args(args: Union[Sequence[str], None] = None):
help="host to run the server on (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 = parser.add_argument_group(title="mode arguments")
group.add_argument(
"--webui",
action="store_true",
help="whether to enable WebUI (default: False)",
)
group.add_argument( group.add_argument(
"--rwkv-beta", "--rwkv-beta",
action="store_true", action="store_true",
@ -79,6 +84,32 @@ app.include_router(misc.router)
app.include_router(state_cache.router) app.include_router(state_cache.router)
@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!"}
def init(): def init():
global_var.init() global_var.init()
cmd_params = os.environ["RWKV_RUNNER_PARAMS"] cmd_params = os.environ["RWKV_RUNNER_PARAMS"]
@ -94,20 +125,6 @@ def init():
ngrok_connect() ngrok_connect()
@app.get("/", tags=["Root"])
def read_root():
return {"Hello": "World!"}
@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()
if __name__ == "__main__": if __name__ == "__main__":
os.environ["RWKV_RUNNER_PARAMS"] = " ".join(sys.argv[1:]) os.environ["RWKV_RUNNER_PARAMS"] = " ".join(sys.argv[1:])
print("--- %s seconds ---" % (time.time() - start_time)) print("--- %s seconds ---" % (time.time() - start_time))

View File

@ -0,0 +1,14 @@
from fastapi import FastAPI
from fastapi.middleware.gzip import GZipMiddleware
from fastapi.staticfiles import StaticFiles
import uvicorn
webui_server = FastAPI()
webui_server.add_middleware(GZipMiddleware, minimum_size=1000)
webui_server.mount(
"/", StaticFiles(directory="frontend/dist", html=True), name="static"
)
if __name__ == "__main__":
uvicorn.run("webui_server:webui_server")