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

Plugin resolution support #112

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions packages/stablestudio-plugin/src/Plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,11 @@ export type Plugin<P extends PluginTypeHelper = PluginTypeHelperDefault> = {
StableDiffusionStyle[] | undefined
>;

/** If you want to provide a list of resolutions in pixels to choose from, you can return them via this function and they will be presented as a slider in the UI */
getStableDiffusionAllowedResolutions?: (
model?: ID
) => MaybePromise<{ width: number; height: number }[] | undefined>;

/** Determines the default count passed to `createStableDiffusionImages` */
getStableDiffusionDefaultCount?: () => number | undefined;

Expand Down
41 changes: 38 additions & 3 deletions packages/stablestudio-ui/src/Generation/Image/Size/Ratio/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import * as ReactQuery from "@tanstack/react-query";

import { Generation } from "~/Generation";
import { Size } from "~/Geometry";
import { Plugin } from "~/Plugin";
import { Theme } from "~/Theme";

export type Ratio = Size & { label?: string };
Expand Down Expand Up @@ -151,8 +154,30 @@ export namespace Ratios {
{ width: 4, height: 1 },
] as const;

const gcd = (a: number, b: number): number => (b ? gcd(b, a % b) : a);
const simplifyAspectRatio = (width: number, height: number) => {
const divisor = gcd(width, height);
return { width: width / divisor, height: height / divisor } as Ratio;
};

export const usePluginResolutions = (model?: ID) => {
const getStableDiffusionAllowedResolutions = Plugin.use(
({ getStableDiffusionAllowedResolutions }) =>
getStableDiffusionAllowedResolutions
);

return ReactQuery.useQuery({
enabled: !!getStableDiffusionAllowedResolutions,

queryKey: ["Generation.Image.Ratio.PluginResolutions.use"],
queryFn: async () =>
(await getStableDiffusionAllowedResolutions?.(model)) ?? [],
});
};

export const use = (id?: ID, fullControl = false) => {
const { input } = Generation.Image.Input.use(id);
const { data: pluginResolutions } = usePluginResolutions(input?.model);
const bounds = Generation.Image.Size.Bounds.use(id);
const ratios = useMemo(() => {
if (!input?.width || !input?.height || !bounds) return [];
Expand All @@ -168,9 +193,19 @@ export namespace Ratios {
};
};

const ratios = (fullControl ? presets : presets.slice(0, -2))
.map(sizing)
.filter(({ input }) => input.width * input.height <= bounds.area.max);
const ratios = pluginResolutions
? pluginResolutions.map(({ width, height }) => ({
...simplifyAspectRatio(width, height),
input: {
width,
height,
},
}))
: (fullControl ? presets : presets.slice(0, -2))
.map(sizing)
.filter(
({ input }) => input.width * input.height <= bounds.area.max
);

const flipped = ratios.map(({ width, height, input }) => ({
width: height,
Expand Down