import { useContext, useState } from "react"; import { MessageDetail } from "@/chatgpt"; import { Tr } from "@/translate"; import { Drawer, DrawerContent, DrawerFooter, DrawerHeader, DrawerTitle, DrawerTrigger, } from "@/components/ui/drawer"; import { Button } from "@/components/ui/button"; import { Label } from "@/components/ui/label"; import { Textarea } from "@/components/ui/textarea"; import { AppContext } from "@/pages/App"; import { PaintBucketIcon } from "lucide-react"; interface Props { disableFactor: boolean[]; } interface ImageResponse { url?: string; b64_json?: string; revised_prompt: string; } export function ImageGenDrawer({ disableFactor }: Props) { const ctx = useContext(AppContext); if (ctx === null) return <>>; const [showGenImage, setShowGenImage] = useState(false); const [imageGenPrompt, setImageGenPrompt] = useState(""); const [imageGenModel, setImageGenModel] = useState("dall-e-3"); const [imageGenN, setImageGenN] = useState(1); const [imageGenQuality, setImageGEnQuality] = useState("standard"); const [imageGenResponseFormat, setImageGenResponseFormat] = useState("b64_json"); const [imageGenSize, setImageGenSize] = useState("1024x1024"); const [imageGenStyle, setImageGenStyle] = useState("vivid"); const [imageGenGenerating, setImageGenGenerating] = useState(false); useState("b64_json"); return ( <> {ctx.chatStore.image_gen_api && ctx.chatStore.image_gen_key ? ( factor)} > Generate Image Generate Image Prompt: { setImageGenPrompt(e.target.value); }} /> Model: { setImageGenModel(e.target.value); }} > DALL-E 3 DALL-E 2 n: setImageGenN(parseInt(e.target.value)) } /> Quality: setImageGEnQuality(e.target.value)} > HD Standard Response Format: setImageGenResponseFormat(e.target.value) } > b64_json url Size: setImageGenSize(e.target.value)} > 256x256 (dall-e-2) 512x512 (dall-e-2) 1024x1024 (dall-e-2/3) 1792x1024 (dall-e-3) 1024x1792 (dall-e-3) Style (only dall-e-3): setImageGenStyle(e.target.value)} > vivid natural { try { setImageGenGenerating(true); const body: any = { prompt: imageGenPrompt, model: imageGenModel, n: imageGenN, quality: imageGenQuality, response_format: imageGenResponseFormat, size: imageGenSize, }; if (imageGenModel === "dall-e-3") { body.style = imageGenStyle; } const resp: ImageResponse[] = ( await fetch(ctx.chatStore.image_gen_api, { method: "POST", headers: { "Content-Type": "application/json", Authorization: `Bearer ${ctx.chatStore.image_gen_key}`, }, body: JSON.stringify(body), }).then((resp) => resp.json()) ).data; console.log("image gen resp", resp); for (const image of resp) { let url = ""; if (image.url) url = image.url; if (image.b64_json) url = "data:image/png;base64," + image.b64_json; if (!url) continue; ctx.chatStore.history.push({ role: "assistant", content: [ { type: "image_url", image_url: { url, detail: "low", }, }, { type: "text", text: image.revised_prompt, }, ], hide: false, token: 65, example: false, audio: null, logprobs: null, response_model_name: imageGenModel, }); ctx.setChatStore({ ...ctx.chatStore }); } } catch (e) { console.error(e); alert("Failed to generate image: " + e); } finally { setImageGenGenerating(false); } }} > {Tr("Generate")} setShowGenImage(false)}>Done ) : ( Generate Image )} > ); }