Skip to content

Commit 753eb57

Browse files
committed
generation ETA
1 parent 44aac30 commit 753eb57

File tree

4 files changed

+68
-83
lines changed

4 files changed

+68
-83
lines changed

packages/stablestudio-ui/src/Comfy/index.tsx

Lines changed: 7 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,13 @@ export namespace Comfy {
226226
id: inputID,
227227
};
228228

229-
const cropped = await cropImage(image, newInput);
230-
if (!cropped) continue;
231-
232-
responses.push(cropped);
229+
responses.push({
230+
id: image.id,
231+
inputID: newInput.id,
232+
created: new Date(),
233+
src: URL.createObjectURL(image.blob),
234+
finishReason: 0,
235+
});
233236
newInputs[inputID] = newInput;
234237
}
235238

@@ -292,51 +295,3 @@ export namespace Comfy {
292295
);
293296
};
294297
}
295-
296-
function cropImage(
297-
image: StableStudio.StableDiffusionImage,
298-
input: Generation.Image.Input
299-
) {
300-
return new Promise<Generation.Image | void>((resolve) => {
301-
const id = image.id;
302-
const blob = image.blob;
303-
if (!blob || !id) return resolve();
304-
305-
// crop image to box size
306-
const croppedCanvas = document.createElement("canvas");
307-
croppedCanvas.width = input.width;
308-
croppedCanvas.height = input.height;
309-
310-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
311-
const croppedCtx = croppedCanvas.getContext("2d")!;
312-
313-
const img = new window.Image();
314-
img.src = URL.createObjectURL(blob);
315-
img.onload = () => {
316-
croppedCtx.drawImage(
317-
img,
318-
0,
319-
0,
320-
input.width,
321-
input.height,
322-
0,
323-
0,
324-
input.width,
325-
input.height
326-
);
327-
328-
croppedCanvas.toBlob((blob) => {
329-
if (blob) {
330-
const objectURL = URL.createObjectURL(blob);
331-
resolve({
332-
id,
333-
inputID: input.id,
334-
created: new Date(),
335-
src: objectURL,
336-
finishReason: 0,
337-
});
338-
}
339-
});
340-
};
341-
});
342-
}

packages/stablestudio-ui/src/Generation/Image/Output/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export function Output({ outputID, placeholder, divider }: Props) {
6363
key={keys("image", images.length, images.length - index)}
6464
placeholder={placeholder}
6565
image={image}
66+
outputID={output?.id}
6667
progress={output?.progress}
6768
scale={1}
6869
example={

packages/stablestudio-ui/src/Generation/Image/SpecialEffects/index.tsx

Lines changed: 57 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export function SpecialEffects({
1212
example,
1313
onClick,
1414
input,
15+
output,
1516
progress,
1617
}: {
1718
showing?: boolean;
@@ -23,10 +24,26 @@ export function SpecialEffects({
2324
};
2425
onClick?: () => void;
2526
input?: ID;
27+
output?: ID;
2628
border?: boolean;
2729
progress?: number;
2830
}) {
2931
if (!showing) return null;
32+
const [starting] = useState(
33+
output ? Generation.Image.Output.get(output).requestedAt : undefined
34+
);
35+
const [eta, setETA] = useState<number | undefined>();
36+
37+
useEffect(() => {
38+
if (!loading) return;
39+
if (typeof progress !== "number") return;
40+
if (!starting) return;
41+
42+
const seconds = (Date.now() - starting.getTime()) / 1000;
43+
const eta = seconds / progress - seconds;
44+
setETA(eta);
45+
}, [loading, progress, starting]);
46+
3047
return (
3148
<div
3249
className={classes(
@@ -80,37 +97,46 @@ export function SpecialEffects({
8097
<div className="absolute flex h-full w-full items-center justify-center">
8198
{loading &&
8299
(typeof progress === "number" ? (
83-
<div
84-
className={classes(
85-
variant === "small" ? "h-1/2 w-1/2" : "h-10 w-10"
86-
)}
87-
>
88-
<CircularProgressbar
89-
value={progress}
90-
maxValue={1}
91-
styles={{
92-
// Customize the path, i.e. the "completed progress"
93-
path: {
94-
// Path color
95-
stroke: "#ffffff",
96-
// Whether to use rounded or flat corners on the ends - can use 'butt' or 'round'
97-
strokeLinecap: "butt",
98-
// Customize transition animation
99-
transition: "stroke-dashoffset 0.5s ease 0s",
100-
transformOrigin: "center center",
101-
strokeWidth: 8,
102-
},
103-
// Customize the circle behind the path, i.e. the "total progress"
104-
trail: {
105-
// Trail color
106-
stroke: "#4c4c4d",
107-
// Whether to use rounded or flat corners on the ends - can use 'butt' or 'round'
108-
strokeLinecap: "butt",
109-
transformOrigin: "center center",
110-
strokeWidth: 8,
111-
},
112-
}}
113-
/>
100+
<div className="relative flex flex-col items-center justify-center gap-1">
101+
<div
102+
className={classes(
103+
variant === "small" ? "h-1/2 w-1/2" : "h-10 w-10"
104+
)}
105+
>
106+
<CircularProgressbar
107+
value={progress}
108+
maxValue={1}
109+
styles={{
110+
// Customize the path, i.e. the "completed progress"
111+
path: {
112+
// Path color
113+
stroke: "#ffffff",
114+
// Whether to use rounded or flat corners on the ends - can use 'butt' or 'round'
115+
strokeLinecap: "butt",
116+
// Customize transition animation
117+
transition: "stroke-dashoffset 0.5s ease 0s",
118+
transformOrigin: "center center",
119+
strokeWidth: 8,
120+
},
121+
// Customize the circle behind the path, i.e. the "total progress"
122+
trail: {
123+
// Trail color
124+
stroke: "#4c4c4d",
125+
// Whether to use rounded or flat corners on the ends - can use 'butt' or 'round'
126+
strokeLinecap: "butt",
127+
transformOrigin: "center center",
128+
strokeWidth: 8,
129+
},
130+
}}
131+
/>
132+
</div>
133+
<p className="absolute top-full mt-1">
134+
{(eta ?? 0) > 0 && (
135+
<span className="text-sm text-white">
136+
{Math.round(eta ?? 0)}s
137+
</span>
138+
)}
139+
</p>
114140
</div>
115141
) : (
116142
<Theme.Loading.Spinner

packages/stablestudio-ui/src/Generation/Image/index.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ type Props = Styleable &
4242
hideControls?: boolean;
4343
placeholder?: boolean;
4444
progress?: number;
45+
outputID?: ID;
4546

4647
onClick?: () => void;
4748
onDelete?: () => void;
@@ -53,6 +54,7 @@ export function Image({
5354
hideControls,
5455
placeholder,
5556
progress,
57+
outputID,
5658

5759
scale,
5860
preserveAspectRatio,
@@ -158,6 +160,7 @@ export function Image({
158160
showing={shouldShowSpecialEffects}
159161
loading={!placeholder && shouldShowSpecialEffects}
160162
progress={progress}
163+
output={outputID}
161164
variant={(style.height ?? 512) < 48 ? "small" : undefined}
162165
// example={example}
163166
// onClick={example ? onTryTemplate : undefined}

0 commit comments

Comments
 (0)