Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for generative background replace #55

Merged
merged 1 commit into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ pcakage-lock.json
__DOCS__/JSDocTemplate/examples
devTools/sanity/results.json
.DS_Store

# Prettier
.prettierignore
6 changes: 5 additions & 1 deletion __TESTS__/unit/actions/Effect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ describe('Tests for Transformation Action -- Effect', () => {
.effect(Effect.generativeRestore())
.effect(Effect.upscale())
.effect(Effect.enhance())
.effect(Effect.generativeBackgroundReplace().prompt("dog"))
.effect(Effect.generativeBackgroundReplace())
.toString();

const expectedToContain = [
Expand Down Expand Up @@ -92,7 +94,9 @@ describe('Tests for Transformation Action -- Effect', () => {
'e_deshake:16',
'e_gen_restore',
'e_upscale',
'e_enhance'
'e_enhance',
'e_gen_background_replace:prompt_dog',
'e_gen_background_replace'
].join('/');

expect(tx).toBe(`${expectedToContain}`);
Expand Down
8 changes: 6 additions & 2 deletions __TESTS__/unit/fromJson/effect.fromJson.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ describe('effect.fromJson', () => {
{ actionType: 'generativeRecolor', prompts: ['something', 'else'], toColor: 'blue', detectMultiple: false },
{ actionType: 'generativeRestore' },
{ actionType: 'upscale' },
{ actionType: 'enhance' }
{ actionType: 'enhance' },
{ actionType: 'generativeBackgroundReplace', prompt: 'dog' },
{ actionType: 'generativeBackgroundReplace'},
]});

expect(transformation.toString().split('/')).toStrictEqual([
Expand Down Expand Up @@ -97,7 +99,9 @@ describe('effect.fromJson', () => {
'e_gen_recolor:prompt_(something;else);to-color_blue',
'e_gen_restore',
'e_upscale',
'e_enhance'
'e_enhance',
'e_gen_background_replace:prompt_dog',
'e_gen_background_replace'
]);
});
});
18 changes: 18 additions & 0 deletions __TESTS__/unit/toJson/effect.toJson.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,24 @@ describe('Effect toJson()', () => {
});
});

it('effect.GenerativeBackgroundReplace', () => {
const transformation = new Transformation()
.addAction(Effect.generativeBackgroundReplace().prompt('dog'))
.addAction(Effect.generativeBackgroundReplace());

expect(transformation.toJson()).toStrictEqual({
actions: [
{
actionType: 'generativeBackgroundReplace',
prompt: 'dog',
},
{
actionType: 'generativeBackgroundReplace',
},
]
});
});

it('effect.GenerativeRecolor', () => {
const transformation = new Transformation()
.addAction(Effect.generativeRecolor('something', 'red'))
Expand Down
16 changes: 16 additions & 0 deletions src/actions/effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { DropShadow } from "./effect/DropShadow.js";
import { GenerativeRemove } from "./effect/GenerativeRemove.js";
import { GenerativeReplace } from "./effect/GenerativeReplace.js";
import { GenerativeRecolor } from "./effect/GenerativeRecolor.js";
import { GenerativeBackgroundReplace } from "./effect/GenerativeBackgroundReplace.js";

/**
* @summary action
Expand Down Expand Up @@ -460,6 +461,18 @@ function generativeReplace(): GenerativeReplace {
return new GenerativeReplace();
}

/**
* @summary action
* @description Uses generative AI to replace background of your image with something else.
* {@link https://cloudinary.com/documentation/transformation_reference#e_gen_background_replace|Generative Background Replace}
*
* @memberOf Actions.Effect
* @return {Actions.Effect.GenerativeBackgroundReplace}
*/
function generativeBackgroundReplace(): GenerativeBackgroundReplace {
return new GenerativeBackgroundReplace();
}

/**
* @summary action
* @description Uses generative AI to recolor objects from your image.
Expand Down Expand Up @@ -610,6 +623,7 @@ const Effect = {
dropShadow,
generativeRemove,
generativeReplace,
generativeBackgroundReplace,
generativeRecolor,
generativeRestore,
upscale,
Expand Down Expand Up @@ -637,6 +651,7 @@ export declare type EffectActions =
| DropShadow
| GenerativeRemove
| GenerativeReplace
| GenerativeBackgroundReplace
| GenerativeRecolor;

export {
Expand Down Expand Up @@ -677,6 +692,7 @@ export {
dropShadow,
generativeRemove,
generativeReplace,
generativeBackgroundReplace,
generativeRecolor,
generativeRestore,
upscale,
Expand Down
46 changes: 46 additions & 0 deletions src/actions/effect/GenerativeBackgroundReplace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Action } from "../../internal/Action.js";
import { Qualifier } from "../../internal/qualifier/Qualifier.js";
import { IGenerativeBackgroundReplaceModel } from "../../internal/models/IEffectActionModel.js";

/**
* @description Uses generative AI to replace background of your image with something else.
* @extends SDK.Action
* @memberOf Actions.Effect
* @see Visit {@link Actions.Effect|Effect} for an example
*/
class GenerativeBackgroundReplace extends Action {
private _prompt: string;

constructor() {
super();
this._actionModel.actionType = "generativeBackgroundReplace";
}

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

return this;
}

protected prepareQualifiers(): void {
if (!this._prompt) {
this.addQualifier(new Qualifier("e", "gen_background_replace"));
} else {
this.addQualifier(
new Qualifier("e", `gen_background_replace:prompt_${this._prompt}`)
);
}
}

static fromJson(
actionModel: IGenerativeBackgroundReplaceModel
): GenerativeBackgroundReplace {
const { prompt } = actionModel;
const result = new this();

return result.prompt(prompt);
}
}

export { GenerativeBackgroundReplace };
8 changes: 5 additions & 3 deletions src/internal/fromJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,10 @@ import { BorderAction } from "../actions/border.js";
import { GenerativeRemove } from "../actions/effect/GenerativeRemove.js";
import { GenerativeReplace } from "../actions/effect/GenerativeReplace.js";
import { GenerativeRecolor } from "../actions/effect/GenerativeRecolor.js";
import {ResizeAdvancedAction} from "../actions/resize/ResizeAdvancedAction.js";
import {BackgroundColor} from "../actions/background/actions/BackgroundColor.js";
import {ResizeAutoPadAction} from "../actions/resize/ResizeAutoPadAction.js";
import { ResizeAdvancedAction } from "../actions/resize/ResizeAdvancedAction.js";
import { BackgroundColor } from "../actions/background/actions/BackgroundColor.js";
import { ResizeAutoPadAction } from "../actions/resize/ResizeAutoPadAction.js";
import { GenerativeBackgroundReplace } from "actions/effect/GenerativeBackgroundReplace.js";

const ActionModelMap: Record<string, IHasFromJson> = {
scale: ResizeScaleAction,
Expand Down Expand Up @@ -149,6 +150,7 @@ const ActionModelMap: Record<string, IHasFromJson> = {
border: BorderAction,
generativeRemove: GenerativeRemove,
generativeReplace: GenerativeReplace,
generativeBackgroundReplace: GenerativeBackgroundReplace,
generativeRecolor: GenerativeRecolor,
generativeRestore: SimpleEffectAction,
upscale: SimpleEffectAction,
Expand Down
5 changes: 5 additions & 0 deletions src/internal/models/IEffectActionModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ interface IGenerativeReplaceModel extends IActionModel {
detectMultiple?: boolean;
}

interface IGenerativeBackgroundReplaceModel extends IActionModel {
prompt?: string;
}

interface IBackgroundColorModel extends IActionModel {
color?: SystemColors | string;
}
Expand Down Expand Up @@ -163,5 +167,6 @@ export {
IGenerativeRemoveModel,
IGenerativeReplaceModel,
IGenerativeRecolorModel,
IGenerativeBackgroundReplaceModel,
IBackgroundColorModel,
};
Loading