add try catch alert to imageGen

This commit is contained in:
2023-11-10 22:14:47 +08:00
parent bd6edb4ca8
commit a72e98ad25

View File

@@ -195,61 +195,66 @@ export function AddImage({
className="bg-sky-400 m-1 p-1 rounded disabled:bg-slate-500" className="bg-sky-400 m-1 p-1 rounded disabled:bg-slate-500"
disabled={imageGenGenerating} disabled={imageGenGenerating}
onClick={async () => { onClick={async () => {
setImageGenGenerating(true); try {
const body: any = { setImageGenGenerating(true);
prompt: imageGenPrompt, const body: any = {
model: imageGenModel, prompt: imageGenPrompt,
n: imageGenN, model: imageGenModel,
quality: imageGenQuality, n: imageGenN,
response_format: imageGenResponseFormat, quality: imageGenQuality,
size: imageGenSize, response_format: imageGenResponseFormat,
}; size: imageGenSize,
if (imageGenModel === "dall-e-3") { };
body.style = imageGenStyle; if (imageGenModel === "dall-e-3") {
} body.style = imageGenStyle;
const resp: ImageResponse[] = ( }
await fetch(chatStore.image_gen_api, { const resp: ImageResponse[] = (
method: "POST", await fetch(chatStore.image_gen_api, {
headers: { method: "POST",
"Content-Type": "application/json", headers: {
Authorization: `Bearer ${chatStore.image_gen_key}`, "Content-Type": "application/json",
}, Authorization: `Bearer ${chatStore.image_gen_key}`,
body: JSON.stringify(body), },
}).then((resp) => resp.json()) body: JSON.stringify(body),
).data; }).then((resp) => resp.json())
).data;
console.log("image gen resp", resp);
for (const image of resp) { for (const image of resp) {
let url = ""; let url = "";
if (image.url) url = image.url; if (image.url) url = image.url;
if (image.b64_json) if (image.b64_json)
url = "data:image/png;base64," + image.b64_json; url = "data:image/png;base64," + image.b64_json;
if (!url) continue; if (!url) continue;
chatStore.history.push({ chatStore.history.push({
role: "user", role: "user",
content: [ content: [
{ {
type: "image_url", type: "image_url",
image_url: { image_url: {
url, url,
detail: "low", detail: "low",
},
}, },
}, {
{ type: "text",
type: "text", text: image.revised_prompt,
text: image.revised_prompt, },
}, ],
], hide: false,
hide: false, token: 65,
token: 65, example: false,
example: false, });
});
setChatStore({ ...chatStore }); setChatStore({ ...chatStore });
}
} catch (e) {
console.error(e);
alert("Failed to generate image: " + e);
} finally {
setImageGenGenerating(false);
} }
setImageGenGenerating(false);
console.log("image resp", resp);
}} }}
> >
{Tr("Generate")} {Tr("Generate")}