2023-04-28 02:28:39 +00:00
|
|
|
import * as InvokeAI from 'app/types/invokeai';
|
2022-09-16 17:18:15 +00:00
|
|
|
|
2022-09-20 13:17:31 +00:00
|
|
|
export const stringToSeedWeights = (
|
|
|
|
string: string
|
|
|
|
): InvokeAI.SeedWeights | boolean => {
|
|
|
|
const stringPairs = string.split(',');
|
|
|
|
const arrPairs = stringPairs.map((p) => p.split(':'));
|
|
|
|
const pairs = arrPairs.map((p: Array<string>): InvokeAI.SeedWeightPair => {
|
2022-10-03 16:15:26 +00:00
|
|
|
return { seed: Number(p[0]), weight: Number(p[1]) };
|
2022-09-20 13:17:31 +00:00
|
|
|
});
|
2022-09-16 17:18:15 +00:00
|
|
|
|
2022-09-20 13:17:31 +00:00
|
|
|
if (!validateSeedWeights(pairs)) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-09-16 17:18:15 +00:00
|
|
|
|
2022-09-20 13:17:31 +00:00
|
|
|
return pairs;
|
2022-09-16 17:18:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const validateSeedWeights = (
|
2022-09-20 13:17:31 +00:00
|
|
|
seedWeights: InvokeAI.SeedWeights | string
|
2022-09-16 17:18:15 +00:00
|
|
|
): boolean => {
|
2022-09-20 13:17:31 +00:00
|
|
|
return typeof seedWeights === 'string'
|
|
|
|
? Boolean(stringToSeedWeights(seedWeights))
|
|
|
|
: Boolean(
|
|
|
|
seedWeights.length &&
|
|
|
|
!seedWeights.some((pair: InvokeAI.SeedWeightPair) => {
|
|
|
|
const { seed, weight } = pair;
|
|
|
|
const isSeedValid = !isNaN(parseInt(seed.toString(), 10));
|
|
|
|
const isWeightValid =
|
|
|
|
!isNaN(parseInt(weight.toString(), 10)) &&
|
|
|
|
weight >= 0 &&
|
|
|
|
weight <= 1;
|
|
|
|
return !(isSeedValid && isWeightValid);
|
|
|
|
})
|
|
|
|
);
|
2022-09-16 17:18:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const seedWeightsToString = (
|
2022-09-20 13:17:31 +00:00
|
|
|
seedWeights: InvokeAI.SeedWeights
|
|
|
|
): string => {
|
|
|
|
return seedWeights.reduce((acc, pair, i, arr) => {
|
|
|
|
const { seed, weight } = pair;
|
|
|
|
acc += `${seed}:${weight}`;
|
|
|
|
if (i !== arr.length - 1) {
|
|
|
|
acc += ',';
|
2022-09-16 17:18:15 +00:00
|
|
|
}
|
2022-09-20 13:17:31 +00:00
|
|
|
return acc;
|
|
|
|
}, '');
|
|
|
|
};
|
|
|
|
|
|
|
|
export const seedWeightsToArray = (
|
|
|
|
seedWeights: InvokeAI.SeedWeights
|
|
|
|
): Array<Array<number>> => {
|
|
|
|
return seedWeights.map((pair: InvokeAI.SeedWeightPair) => [
|
|
|
|
pair.seed,
|
|
|
|
pair.weight,
|
|
|
|
]);
|
|
|
|
};
|
2022-09-16 17:18:15 +00:00
|
|
|
|
2022-09-20 13:17:31 +00:00
|
|
|
export const stringToSeedWeightsArray = (
|
|
|
|
string: string
|
|
|
|
): Array<Array<number>> => {
|
|
|
|
const stringPairs = string.split(',');
|
|
|
|
const arrPairs = stringPairs.map((p) => p.split(':'));
|
|
|
|
return arrPairs.map(
|
2023-02-04 00:36:31 +00:00
|
|
|
(p: Array<string>): Array<number> => [parseInt(p[0], 10), parseFloat(p[1])]
|
2022-09-20 13:17:31 +00:00
|
|
|
);
|
2022-09-16 17:18:15 +00:00
|
|
|
};
|