Skip to content

Commit

Permalink
gen bg replace, gen fill - handle special chars
Browse files Browse the repository at this point in the history
  • Loading branch information
lisa-kuzmina committed Nov 19, 2024
1 parent 39965d6 commit ab4e832
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 8 deletions.
8 changes: 4 additions & 4 deletions __TESTS_BUNDLE_SIZE__/bundleSizeTestCases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,28 @@ const bundleSizeTestCases:ITestCase[] = [
},
{
name: 'Import all of the SDK',
sizeLimitInKB: 129,
sizeLimitInKB: 130,
importsArray: [
importFromPackage('* as CloudinaryURLGEN')
]
},
{
name: 'Import a Transformation Object',
sizeLimitInKB: 7,
sizeLimitInKB: 20,
importsArray: [
importFromPackage('Transformation')
]
},
{
name: 'Import All Actions',
sizeLimitInKB: 65,
sizeLimitInKB: 78,
importsArray: [
importFromPackage('Actions')
]
},
{
name: 'Import All Qualifiers',
sizeLimitInKB: 40,
sizeLimitInKB: 53,
importsArray: [
importFromPackage('Qualifiers')
]
Expand Down
5 changes: 4 additions & 1 deletion __TESTS__/unit/fromJson/effect.fromJson.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@ describe('effect.fromJson', () => {
{ actionType: 'upscale' },
{ actionType: 'enhance' },
{ actionType: 'generativeBackgroundReplace', prompt: 'dog' },
{ actionType: 'generativeBackgroundReplace', prompt: 'dog, dog (dog)' },
{ actionType: 'generativeBackgroundReplace'},
{ actionType: 'extract', 'prompts': 'blue sky' },
{ actionType: 'extract', 'prompts': ['blue sky', 'yellow sun'], detectMultiple: true },
{ actionType: 'extract', 'prompts': ['green grass'], mode: 'mask', invert: true },
{ actionType: 'extract', 'prompts': ['yellow sun', 'green grass'], mode: 'content' },

{ actionType: 'pad', 'dimensions': { width: 100, height: 200 }, 'background': { backgroundType: 'generativeFill', prompt: 'some; test (123!)'} },
]});

expect(transformation.toString().split('/')).toStrictEqual([
Expand Down Expand Up @@ -106,11 +107,13 @@ describe('effect.fromJson', () => {
'e_upscale',
'e_enhance',
'e_gen_background_replace:prompt_dog',
'e_gen_background_replace:prompt_dog%2C dog %28dog%29',
'e_gen_background_replace',
'e_extract:prompt_blue sky',
'e_extract:prompt_(blue sky;yellow sun);multiple_true',
'e_extract:prompt_green grass;mode_mask;invert_true',
'e_extract:prompt_(yellow sun;green grass);mode_content',
'b_gen_fill:prompt_some%3B test %28123%21%29,c_pad,h_200,w_100',
]);
});
});
8 changes: 6 additions & 2 deletions src/actions/effect/GenerativeBackgroundReplace.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Action } from "../../internal/Action.js";
import { Qualifier } from "../../internal/qualifier/Qualifier.js";
import { IGenerativeBackgroundReplaceModel } from "../../internal/models/IEffectActionModel.js";
import { encodePromptComponent } from "../../internal/utils/encodePromptComponents.js";

/**
* @description Uses generative AI to replace background of your image with something else.
Expand All @@ -17,7 +18,7 @@ class GenerativeBackgroundReplace extends Action {
}

prompt(value: string): GenerativeBackgroundReplace {
this._prompt = value;
this._prompt = value ? encodePromptComponent(value) : value;
this._actionModel.prompt = value;

return this;
Expand All @@ -28,7 +29,10 @@ class GenerativeBackgroundReplace extends Action {
this.addQualifier(new Qualifier("e", "gen_background_replace"));
} else {
this.addQualifier(
new Qualifier("e", `gen_background_replace:prompt_${this._prompt}`)
new Qualifier(
"e",
`gen_background_replace:prompt_${this._prompt}`
)
);
}
}
Expand Down
22 changes: 22 additions & 0 deletions src/internal/utils/encodePromptComponents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export const encodePromptComponent = (prompt: string): string => composeStringOperations(encodeURIComponent, replaceMissingChars, decodeSpaces)(prompt);

const charsMissingFromUrlEncode = /[!'()*~_.-]/g;

const missingCharsMap: { [char: string]: string } = {
'!': '%21',
"'": '%27',
'(': '%28',
')': '%29',
'*': '%2A',
'~': '%7E',
_: '%5F',
'.': '%2E',
'-': '%2D',
};

const replaceMissingChars = (str: string): string => {
return str.replace(charsMissingFromUrlEncode, (match) => missingCharsMap[match] ?? match);
};

const decodeSpaces = (str: string): string => str.replace(/%20/g, ' ');
const composeStringOperations = (...fns: Array<(str: string) => string>) => (arg: string) => fns.reduce((acc, fn) => fn(acc), arg);
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {encodePromptComponent} from "../../../internal/utils/encodePromptComponents.js";
import {BackgroundQualifier} from "./base/BackgroundQualifier.js";

/**
Expand All @@ -15,7 +16,7 @@ class BackgroundGenerativeFillQualifier extends BackgroundQualifier {
}

prompt(prompt: string): BackgroundGenerativeFillQualifier {
this._prompt = prompt;
this._prompt = prompt ? encodePromptComponent(prompt) : prompt;
return this;
}

Expand Down

0 comments on commit ab4e832

Please sign in to comment.