From b477a9937ce11d3ce34a8165cc1f02da3bbd404a Mon Sep 17 00:00:00 2001 From: Sean Sube Date: Thu, 12 Jan 2023 22:10:46 -0600 Subject: [PATCH] feat: move API routes under prefix --- api/onnx_web/serve.py | 36 ++++++++++++++++++++++++++---------- gui/src/api/client.ts | 14 +++++++------- 2 files changed, 33 insertions(+), 17 deletions(-) diff --git a/api/onnx_web/serve.py b/api/onnx_web/serve.py index b91a0a8e..fbea7adf 100644 --- a/api/onnx_web/serve.py +++ b/api/onnx_web/serve.py @@ -19,7 +19,7 @@ from diffusers import ( # types DiffusionPipeline, ) -from flask import Flask, jsonify, request, send_from_directory, url_for +from flask import Flask, jsonify, request, send_file, send_from_directory, url_for from flask_executor import Executor from hashlib import sha256 from io import BytesIO @@ -48,6 +48,7 @@ max_height = 512 max_width = 512 # paths +bundle_path = environ.get('ONNX_WEB_BUNDLE_PATH', path.join('..', '..', 'gui', 'out')) model_path = environ.get('ONNX_WEB_MODEL_PATH', path.join('..', 'models')) output_path = environ.get('ONNX_WEB_OUTPUT_PATH', path.join('..', 'outputs')) params_path = environ.get('ONNX_WEB_PARAMS_PATH', 'params.json') @@ -314,9 +315,24 @@ executor = Executor(app) # routes +def serve_file(filename = 'index.html'): + file = path.join(bundle_path, filename) + print('index', file) + return send_file(file) + @app.route('/') def index(): + return serve_file() + + +@app.route('/') +def index_path(filename): + return serve_file(filename) + + +@app.route('/api') +def introspect(): return { 'name': 'onnx-web', 'routes': [{ @@ -326,27 +342,27 @@ def index(): } -@app.route('/settings/models') +@app.route('/api/settings/models') def list_models(): return json_with_cors(available_models) -@app.route('/settings/params') +@app.route('/api/settings/params') def list_params(): return json_with_cors(config_params) -@app.route('/settings/platforms') +@app.route('/api/settings/platforms') def list_platforms(): return json_with_cors(list(platform_providers.keys())) -@app.route('/settings/schedulers') +@app.route('/api/settings/schedulers') def list_schedulers(): return json_with_cors(list(pipeline_schedulers.keys())) -@app.route('/img2img', methods=['POST']) +@app.route('/api/img2img', methods=['POST']) def img2img(): input_file = request.files.get('source') input_image = Image.open(BytesIO(input_file.read())).convert('RGB') @@ -381,7 +397,7 @@ def img2img(): }) -@app.route('/txt2img', methods=['POST']) +@app.route('/api/txt2img', methods=['POST']) def txt2img(): (model, provider, scheduler, prompt, negative_prompt, cfg, steps, height, width, seed) = pipeline_from_request() @@ -410,7 +426,7 @@ def txt2img(): }) -@app.route('/inpaint', methods=['POST']) +@app.route('/api/inpaint', methods=['POST']) def inpaint(): source_file = request.files.get('source') source_image = Image.open(BytesIO(source_file.read())).convert('RGB') @@ -447,7 +463,7 @@ def inpaint(): }) -@app.route('/ready') +@app.route('/api/ready') def ready(): output_file = request.args.get('output', None) @@ -456,6 +472,6 @@ def ready(): }) -@app.route('/output/') +@app.route('/api/output/') def output(filename: str): return send_from_directory(path.join('..', output_path), filename, as_attachment=False) diff --git a/gui/src/api/client.ts b/gui/src/api/client.ts index 6b8b108d..5a81bcb9 100644 --- a/gui/src/api/client.ts +++ b/gui/src/api/client.ts @@ -103,7 +103,7 @@ export async function imageFromResponse(root: string, res: Response): Promise> { - const path = new URL(joinPath('settings', 'models'), root); + const path = new URL(joinPath('api', 'settings', 'models'), root); const res = await f(path); return await res.json() as Array; }, async params(): Promise { - const path = new URL(joinPath('settings', 'params'), root); + const path = new URL(joinPath('api', 'settings', 'params'), root); const res = await f(path); return await res.json() as ConfigParams; }, async schedulers(): Promise> { - const path = new URL(joinPath('settings', 'schedulers'), root); + const path = new URL(joinPath('api', 'settings', 'schedulers'), root); const res = await f(path); return await res.json() as Array; }, async platforms(): Promise> { - const path = new URL(joinPath('settings', 'platforms'), root); + const path = new URL(joinPath('api', 'settings', 'platforms'), root); const res = await f(path); return await res.json() as Array; }, @@ -240,7 +240,7 @@ export function makeClient(root: string, f = fetch): ApiClient { throw new NotImplementedError(); }, async ready(params: ApiResponse): Promise<{ready: boolean}> { - const path = new URL('ready', root); + const path = new URL(joinPath('api', 'ready'), root); path.searchParams.append('output', params.output.key); const res = await f(path);