parse size from json params
This commit is contained in:
parent
570b56ab72
commit
d80ab00446
|
@ -21,6 +21,7 @@ import { useStore } from 'zustand';
|
||||||
|
|
||||||
import { BaseImgParams, Txt2ImgParams } from '../client/types.js';
|
import { BaseImgParams, Txt2ImgParams } from '../client/types.js';
|
||||||
import { StateContext } from '../state.js';
|
import { StateContext } from '../state.js';
|
||||||
|
import { defaultTo } from 'lodash';
|
||||||
|
|
||||||
export interface ProfilesProps {
|
export interface ProfilesProps {
|
||||||
params: BaseImgParams;
|
params: BaseImgParams;
|
||||||
|
@ -157,12 +158,10 @@ export async function loadParamsFromFile(file: File): Promise<Partial<Txt2ImgPar
|
||||||
case 'jpg':
|
case 'jpg':
|
||||||
case 'jpeg':
|
case 'jpeg':
|
||||||
case 'png':
|
case 'png':
|
||||||
|
case 'webp':
|
||||||
return parseImageParams(file);
|
return parseImageParams(file);
|
||||||
case 'json':
|
case 'json':
|
||||||
{
|
return parseJSONParams(await file.text());
|
||||||
const params = JSON.parse(await file.text());
|
|
||||||
return params.params as Txt2ImgParams;
|
|
||||||
}
|
|
||||||
case 'txt':
|
case 'txt':
|
||||||
default:
|
default:
|
||||||
return parseAutoComment(await file.text());
|
return parseAutoComment(await file.text());
|
||||||
|
@ -178,8 +177,7 @@ export async function parseImageParams(file: File): Promise<Partial<Txt2ImgParam
|
||||||
const userComment = tags.UserComment || tags['Parameters'] || tags['parameters'];
|
const userComment = tags.UserComment || tags['Parameters'] || tags['parameters'];
|
||||||
|
|
||||||
if (doesExist(makerNote) && isProbablyJSON(makerNote.value)) {
|
if (doesExist(makerNote) && isProbablyJSON(makerNote.value)) {
|
||||||
const params = JSON.parse(makerNote.value);
|
return parseJSONParams(makerNote.value);
|
||||||
return params.params as Txt2ImgParams; // TODO: enforce schema and do some error handling
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (doesExist(userComment) && typeof userComment.value === 'string') {
|
if (doesExist(userComment) && typeof userComment.value === 'string') {
|
||||||
|
@ -189,6 +187,21 @@ export async function parseImageParams(file: File): Promise<Partial<Txt2ImgParam
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function parseJSONParams(json: string): Promise<Partial<Txt2ImgParams>> {
|
||||||
|
const data = JSON.parse(json);
|
||||||
|
const params: Partial<Txt2ImgParams> = {
|
||||||
|
...data.params,
|
||||||
|
};
|
||||||
|
|
||||||
|
const size = defaultTo(data.input_size, data.size);
|
||||||
|
if (doesExist(size)) {
|
||||||
|
params.height = size.height;
|
||||||
|
params.width = size.width;
|
||||||
|
}
|
||||||
|
|
||||||
|
return params;
|
||||||
|
}
|
||||||
|
|
||||||
export function isProbablyJSON(maybeJSON: unknown): maybeJSON is string {
|
export function isProbablyJSON(maybeJSON: unknown): maybeJSON is string {
|
||||||
return typeof maybeJSON === 'string' && maybeJSON[0] === '{' && maybeJSON[maybeJSON.length - 1] === '}';
|
return typeof maybeJSON === 'string' && maybeJSON[0] === '{' && maybeJSON[maybeJSON.length - 1] === '}';
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue