1
0
Fork 0

feat: move API routes under prefix

This commit is contained in:
Sean Sube 2023-01-12 22:10:46 -06:00
parent 600ebae73a
commit b477a9937c
2 changed files with 33 additions and 17 deletions

View File

@ -19,7 +19,7 @@ from diffusers import (
# types # types
DiffusionPipeline, 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 flask_executor import Executor
from hashlib import sha256 from hashlib import sha256
from io import BytesIO from io import BytesIO
@ -48,6 +48,7 @@ max_height = 512
max_width = 512 max_width = 512
# paths # paths
bundle_path = environ.get('ONNX_WEB_BUNDLE_PATH', path.join('..', '..', 'gui', 'out'))
model_path = environ.get('ONNX_WEB_MODEL_PATH', path.join('..', 'models')) model_path = environ.get('ONNX_WEB_MODEL_PATH', path.join('..', 'models'))
output_path = environ.get('ONNX_WEB_OUTPUT_PATH', path.join('..', 'outputs')) output_path = environ.get('ONNX_WEB_OUTPUT_PATH', path.join('..', 'outputs'))
params_path = environ.get('ONNX_WEB_PARAMS_PATH', 'params.json') params_path = environ.get('ONNX_WEB_PARAMS_PATH', 'params.json')
@ -314,9 +315,24 @@ executor = Executor(app)
# routes # routes
def serve_file(filename = 'index.html'):
file = path.join(bundle_path, filename)
print('index', file)
return send_file(file)
@app.route('/') @app.route('/')
def index(): def index():
return serve_file()
@app.route('/<path:filename>')
def index_path(filename):
return serve_file(filename)
@app.route('/api')
def introspect():
return { return {
'name': 'onnx-web', 'name': 'onnx-web',
'routes': [{ 'routes': [{
@ -326,27 +342,27 @@ def index():
} }
@app.route('/settings/models') @app.route('/api/settings/models')
def list_models(): def list_models():
return json_with_cors(available_models) return json_with_cors(available_models)
@app.route('/settings/params') @app.route('/api/settings/params')
def list_params(): def list_params():
return json_with_cors(config_params) return json_with_cors(config_params)
@app.route('/settings/platforms') @app.route('/api/settings/platforms')
def list_platforms(): def list_platforms():
return json_with_cors(list(platform_providers.keys())) return json_with_cors(list(platform_providers.keys()))
@app.route('/settings/schedulers') @app.route('/api/settings/schedulers')
def list_schedulers(): def list_schedulers():
return json_with_cors(list(pipeline_schedulers.keys())) return json_with_cors(list(pipeline_schedulers.keys()))
@app.route('/img2img', methods=['POST']) @app.route('/api/img2img', methods=['POST'])
def img2img(): def img2img():
input_file = request.files.get('source') input_file = request.files.get('source')
input_image = Image.open(BytesIO(input_file.read())).convert('RGB') 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(): def txt2img():
(model, provider, scheduler, prompt, negative_prompt, cfg, steps, height, (model, provider, scheduler, prompt, negative_prompt, cfg, steps, height,
width, seed) = pipeline_from_request() width, seed) = pipeline_from_request()
@ -410,7 +426,7 @@ def txt2img():
}) })
@app.route('/inpaint', methods=['POST']) @app.route('/api/inpaint', methods=['POST'])
def inpaint(): def inpaint():
source_file = request.files.get('source') source_file = request.files.get('source')
source_image = Image.open(BytesIO(source_file.read())).convert('RGB') 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(): def ready():
output_file = request.args.get('output', None) output_file = request.args.get('output', None)
@ -456,6 +472,6 @@ def ready():
}) })
@app.route('/output/<path:filename>') @app.route('/api/output/<path:filename>')
def output(filename: str): def output(filename: str):
return send_from_directory(path.join('..', output_path), filename, as_attachment=False) return send_from_directory(path.join('..', output_path), filename, as_attachment=False)

View File

@ -103,7 +103,7 @@ export async function imageFromResponse(root: string, res: Response): Promise<Ap
if (res.status === STATUS_SUCCESS) { if (res.status === STATUS_SUCCESS) {
const data = await res.json() as LimitedResponse; const data = await res.json() as LimitedResponse;
const url = new URL(joinPath('output', data.output), root).toString(); const url = new URL(joinPath('api', 'output', data.output), root).toString();
return { return {
output: { output: {
key: data.output, key: data.output,
@ -117,7 +117,7 @@ export async function imageFromResponse(root: string, res: Response): Promise<Ap
} }
export function makeImageURL(root: string, type: string, params: BaseImgParams): URL { export function makeImageURL(root: string, type: string, params: BaseImgParams): URL {
const url = new URL(type, root); const url = new URL(joinPath('api', type), root);
url.searchParams.append('cfg', params.cfg.toFixed(1)); url.searchParams.append('cfg', params.cfg.toFixed(1));
url.searchParams.append('steps', params.steps.toFixed(0)); url.searchParams.append('steps', params.steps.toFixed(0));
@ -158,22 +158,22 @@ export function makeClient(root: string, f = fetch): ApiClient {
return { return {
async models(): Promise<Array<string>> { async models(): Promise<Array<string>> {
const path = new URL(joinPath('settings', 'models'), root); const path = new URL(joinPath('api', 'settings', 'models'), root);
const res = await f(path); const res = await f(path);
return await res.json() as Array<string>; return await res.json() as Array<string>;
}, },
async params(): Promise<ConfigParams> { async params(): Promise<ConfigParams> {
const path = new URL(joinPath('settings', 'params'), root); const path = new URL(joinPath('api', 'settings', 'params'), root);
const res = await f(path); const res = await f(path);
return await res.json() as ConfigParams; return await res.json() as ConfigParams;
}, },
async schedulers(): Promise<Array<string>> { async schedulers(): Promise<Array<string>> {
const path = new URL(joinPath('settings', 'schedulers'), root); const path = new URL(joinPath('api', 'settings', 'schedulers'), root);
const res = await f(path); const res = await f(path);
return await res.json() as Array<string>; return await res.json() as Array<string>;
}, },
async platforms(): Promise<Array<string>> { async platforms(): Promise<Array<string>> {
const path = new URL(joinPath('settings', 'platforms'), root); const path = new URL(joinPath('api', 'settings', 'platforms'), root);
const res = await f(path); const res = await f(path);
return await res.json() as Array<string>; return await res.json() as Array<string>;
}, },
@ -240,7 +240,7 @@ export function makeClient(root: string, f = fetch): ApiClient {
throw new NotImplementedError(); throw new NotImplementedError();
}, },
async ready(params: ApiResponse): Promise<{ready: boolean}> { 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); path.searchParams.append('output', params.output.key);
const res = await f(path); const res = await f(path);