Skip to content

Commit

Permalink
refactor: formatInfo (#563)
Browse files Browse the repository at this point in the history
* refactor: formatInfo

* style: eslint

* refactor: use @bluelovers/auto1111-pnginfo

https://github.com/bluelovers/auto1111-pnginfo
  • Loading branch information
bluelovers committed May 15, 2024
1 parent b9ab9dc commit 77102c0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 33 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@
]
},
"dependencies": {
"@bluelovers/auto1111-pnginfo": "^2.0.1",
"@lobehub/ui": "latest",
"@rollup/rollup-win32-x64-msvc": "^4.13.0",
"ahooks": "^3",
"antd": "^5",
"antd-style": "latest",
Expand Down
45 changes: 12 additions & 33 deletions src/modules/ImageInfo/features/formatInfo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Converter } from '@/scripts/formatPrompt';
import { parseFromRawInfo } from '@bluelovers/auto1111-pnginfo';

const formatPrompt = (prompt: string) => {
let newPrompt = prompt.replaceAll('&lt;', '<').replaceAll('&gt;', '>');
Expand All @@ -9,44 +10,22 @@ export const formatInfo = (info: string) => {
if (!info || info === 'undefined') return;
if (!info.includes('<br>')) return;

let position: any = '';
let negative: any = '';
let config: any = '';
let {
prompt: position,
negative_prompt: negative,
...config,
} = parseFromRawInfo(info, {
isIncludePrompts: true,
})

let data = info?.split('Negative prompt:').filter(Boolean);
if (data[1]) {
position = data[0];
data = data[1].split('Steps:');
negative = `Negative prompt: ${data[0]}`;
config = `Steps: ${data[1]}`;
} else {
data = data[0].split('Steps:');
position = data[0];
config = `Steps: ${data[1]}`;
}

position = position.trim().replaceAll('<br>', '');
negative = negative.trim().replaceAll('<br>', '');
config = config.trim().replaceAll('<br>', '');

if (!config.includes(',')) return;
const clearConfigs = config
.split(',')
.map((item: any) => item?.trim())
.filter(Boolean);

const configs: any = {};

for (const item of clearConfigs) {
const items = item.split(':');
configs[items[0]?.trim()] = items[1]?.trim();
}
position = position.trim().replaceAll('<br>', '\n').replace(/[\s\r\n]+$/g, '');
negative = negative.trim().replaceAll('<br>', '\n').replace(/[\s\r\n]+$/g, '');

position = position ? formatPrompt(position) : '';
negative = negative ? formatPrompt(negative.split('Negative prompt: ')[1]) : '';
negative = negative ? formatPrompt(negative) : '';

return {
config: configs,
config,
negative: negative,
positive: position,
};
Expand Down

0 comments on commit 77102c0

Please sign in to comment.