Skip to content

Commit

Permalink
handle json in comments field
Browse files Browse the repository at this point in the history
  • Loading branch information
ssube committed Jul 20, 2023
1 parent 9db5463 commit 063acc6
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions gui/src/components/Profiles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
TextField,
} from '@mui/material';
import * as ExifReader from 'exifreader';
import { defaultTo } from 'lodash';
import { defaultTo, isString } from 'lodash';
import * as React from 'react';
import { useContext } from 'react';
import { useTranslation } from 'react-i18next';
Expand Down Expand Up @@ -176,11 +176,11 @@ export async function parseImageParams(file: File): Promise<Partial<Txt2ImgParam
// eslint-disable-next-line dot-notation, @typescript-eslint/strict-boolean-expressions
const userComment = tags.UserComment || tags['Parameters'] || tags['parameters'];

if (doesExist(makerNote) && isProbablyJSON(makerNote.value)) {
if (doesExist(makerNote) && isString(makerNote.value) && isProbablyJSON(makerNote.value)) {
return parseJSONParams(makerNote.value);
}

if (doesExist(userComment) && typeof userComment.value === 'string') {
if (doesExist(userComment) && isString(userComment.value)) {
return parseAutoComment(userComment.value);
}

Expand All @@ -202,13 +202,17 @@ export async function parseJSONParams(json: string): Promise<Partial<Txt2ImgPara
return params;
}

export function isProbablyJSON(maybeJSON: unknown): maybeJSON is string {
export function isProbablyJSON(maybeJSON: unknown): boolean {
return typeof maybeJSON === 'string' && maybeJSON[0] === '{' && maybeJSON[maybeJSON.length - 1] === '}';
}

export const NEGATIVE_PROMPT_TAG = 'Negative prompt:';

export function parseAutoComment(comment: string): Partial<Txt2ImgParams> {
export async function parseAutoComment(comment: string): Promise<Partial<Txt2ImgParams>> {
if (isProbablyJSON(comment)) {
return parseJSONParams(comment);
}

const lines = comment.split('\n');
const [prompt, maybeNegative, ...otherLines] = lines;

Expand Down

0 comments on commit 063acc6

Please sign in to comment.