Skip to content

Commit

Permalink
feat: move API routes under prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
ssube committed Jan 13, 2023
1 parent 600ebae commit b477a99
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
36 changes: 26 additions & 10 deletions api/onnx_web/serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
# 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
Expand Down Expand Up @@ -48,6 +48,7 @@
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')
Expand Down Expand Up @@ -314,9 +315,24 @@ def load_params():

# 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('/<path:filename>')
def index_path(filename):
return serve_file(filename)


@app.route('/api')
def introspect():
return {
'name': 'onnx-web',
'routes': [{
Expand All @@ -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')
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -447,7 +463,7 @@ def inpaint():
})


@app.route('/ready')
@app.route('/api/ready')
def ready():
output_file = request.args.get('output', None)

Expand All @@ -456,6 +472,6 @@ def ready():
})


@app.route('/output/<path:filename>')
@app.route('/api/output/<path:filename>')
def output(filename: str):
return send_from_directory(path.join('..', output_path), filename, as_attachment=False)
14 changes: 7 additions & 7 deletions gui/src/api/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export async function imageFromResponse(root: string, res: Response): Promise<Ap

if (res.status === STATUS_SUCCESS) {
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 {
output: {
key: data.output,
Expand All @@ -117,7 +117,7 @@ export async function imageFromResponse(root: string, res: Response): Promise<Ap
}

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('steps', params.steps.toFixed(0));

Expand Down Expand Up @@ -158,22 +158,22 @@ export function makeClient(root: string, f = fetch): ApiClient {

return {
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);
return await res.json() as Array<string>;
},
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);
return await res.json() as ConfigParams;
},
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);
return await res.json() as 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);
return await res.json() as Array<string>;
},
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit b477a99

Please sign in to comment.