diff --git a/apis/api.py b/apis/api.py index b77c41029..71e25e3ae 100644 --- a/apis/api.py +++ b/apis/api.py @@ -9,6 +9,7 @@ from apis.routes.generate import secure_router as generate from apis.routes.query import secure_router as query +from apis.routes.query import router from apis.utils import file_utils from apis.utils import api_utils @@ -25,6 +26,7 @@ app.include_router(query) app.include_router(generate) +app.include_router(router) @app.get("/", tags=["Query"]) @@ -46,8 +48,12 @@ def run_server(arguments): os.environ["WEBHOOK_URL"] = arguments.webhook_url try: - api_port = int(arguments.port) + 1 - except TypeError: - api_port = int(os.environ["GRADIO_SERVER_PORT"]) + 1 + api_port = int(os.environ['API_PORT']) + except KeyError: + try: + api_port = int(arguments.port) + 1 + except TypeError: + api_port = int(os.environ["GRADIO_SERVER_PORT"]) + 1 + file_utils.STATIC_SERVER_BASE = f"http://{arguments.base_url}:{api_port}" uvicorn.run(app, host=arguments.listen, port=api_port) diff --git a/apis/routes/query.py b/apis/routes/query.py index 84197dadd..5f99f2df6 100644 --- a/apis/routes/query.py +++ b/apis/routes/query.py @@ -69,6 +69,8 @@ async def tasks_info(task_id: str = None): dependencies=[Depends(api_key_auth)] ) +router = APIRouter() + @secure_router.get("/tasks", tags=["Query"]) async def get_tasks( @@ -147,7 +149,7 @@ async def get_task(task_id: str): return JSONResponse(await tasks_info(task_id)) -@secure_router.get("/outputs/{date}/{file_name}", tags=["Query"]) +@router.get("/outputs/{date}/{file_name}", tags=["Query"]) async def get_output(date: str, file_name: str, accept: str = Header(None)): """ Get a specific output by its ID. @@ -172,7 +174,7 @@ async def get_output(date: str, file_name: str, accept: str = Header(None)): return Response(content=img, media_type=f"image/{ext}") -@secure_router.get("/inputs/{file_name}", tags=["Query"]) +@router.get("/inputs/{file_name}", tags=["Query"]) async def get_input(file_name: str, accept: str = Header(None)): """ Get a specific input by its ID. diff --git a/docs/readme.md b/docs/readme.md index 7854831db..9018388f2 100644 --- a/docs/readme.md +++ b/docs/readme.md @@ -46,7 +46,9 @@ FastAPI 是一个现代、快速(高性能)的Web框架,用于构建APIs 和 Fooocus 相同的启动方式,使用 `--nowebui` 参数可以启动 API 而不启动 WebUI。 -默认的 API 端口是 WebUI 端口加 1,即 7866,使用 `--port` 修改 WebUI 端口将同时修改 API 端口。 +默认的 API 端口是 WebUI 端口加 1,即 7866 + +环境变量 API_PORT 用于指定 API 端口,优先级高于默认设置。 同时启动 WebUI 和 API 示例: diff --git a/readme.md b/readme.md index 85bb7b1ac..ee78a9f94 100644 --- a/readme.md +++ b/readme.md @@ -32,7 +32,7 @@ advantage: - Use X-API-KEY for authentication - all-in-one interface - use URL provide INPUT image -- streaming output, binary image, asynchronous task and syncronous task support +- streaming output, binary image, asynchronous task and synchronous task support - persistent task history - enhanced task history management - task query function @@ -48,7 +48,9 @@ Based on Fooocus, there are several dependencies, so you can install it in the s Same as Fooocus, use `--apikey` to specify the API authentication key. -Default API port is WebUI port plus 1, that is 7866, use `--port` to modify WebUI port to modify API port at the same time. +Default API port is WebUI port plus 1, that is 7866, use `--port` to modify WebUI port + +Environment variable API_PORT is used to specify the API port. It takes precedence over the default setting. example for WebUI and API: @@ -103,10 +105,10 @@ In addition, some API-specific parameters are also included: - `preset`, You can use this parameter to specify a preset that takes precedence over the global default and below the passed parameter, but if the passed parameter is equal to the default value, the preset parameter is used - `stream_output`, true for streaming output, default false - `require_base64`, not used -- `async_process`, async task, default false, a syncronous task will be returned when `stream_output` is false at the same time +- `async_process`, async task, default false, a synchronous task will be returned when `stream_output` is false at the same time - `webhook_url`, Webhook addr, if set, the task will be sent to the address after the task is completed. -> `stream_output` has a higher priority than `async_process`, that is, when both are `true`, return streaming output. When all are false, task will be syncronously returned. when you set Accepet: image/xxx in the request header, the response will be a binary image +> `stream_output` has a higher priority than `async_process`, that is, when both are `true`, return streaming output. When all are false, task will be synchronously returned. when you set `Accept: image/xxx` in the request header, the response will be a binary image ### Stop or Skip