CoolFace
Apppublic

admin08077/Githubgemini

sourceHugging Faceotherupdated 10mo agoView on Hugging Face
0likes
AiImageGenerator-CoAidnKa.js.map1 linesDownload Raw Back to assets
1{"version":3,"file":"AiImageGenerator-CoAidnKa.js","sources":["../../components/features/AiImageGenerator.tsx"],"sourcesContent":["import React, { useState, useCallback } from 'react';\nimport { generateImage } from '../../services/index.ts';\nimport { ImageGeneratorIcon, SparklesIcon } from '../icons.tsx';\nimport { LoadingSpinner } from '../shared/index.tsx';\nimport { ArrowDownTrayIcon } from '../icons.tsx';\n\nconst surprisePrompts = [\n    'A majestic lion wearing a crown, painted in the style of Van Gogh.',\n    'A futuristic cityscape on another planet with two moons in the sky.',\n    'A cozy, magical library inside a giant tree.',\n    'A surreal image of a ship sailing on a sea of clouds.',\n    'An astronaut riding a space-themed bicycle on the moon.',\n];\n\nexport const AiImageGenerator: React.FC = () => {\n    const [prompt, setPrompt] = useState<string>('A photorealistic image of a futuristic city at sunset, with flying cars.');\n    const [imageUrl, setImageUrl] = useState<string | null>(null);\n    const [isLoading, setIsLoading] = useState<boolean>(false);\n    const [error, setError] = useState<string>('');\n\n    const handleGenerate = useCallback(async () => {\n        if (!prompt.trim()) {\n            setError('Please enter a prompt to generate an image.');\n            return;\n        }\n        setIsLoading(true);\n        setError('');\n        setImageUrl(null);\n        try {\n            const resultUrl = await generateImage(prompt);\n            setImageUrl(resultUrl);\n        } catch (err) {\n            const errorMessage = err instanceof Error ? err.message : 'An unknown error occurred.';\n            setError(`Failed to generate image: ${errorMessage}`);\n        } finally {\n            setIsLoading(false);\n        }\n    }, [prompt]);\n\n    const handleSurpriseMe = () => {\n        const randomPrompt = surprisePrompts[Math.floor(Math.random() * surprisePrompts.length)];\n        setPrompt(randomPrompt);\n    };\n    \n    const handleDownload = () => {\n        if (!imageUrl) return;\n        const link = document.createElement('a');\n        link.href = imageUrl;\n        link.download = `${prompt.slice(0, 30).replace(/\\s/g, '_')}.png`;\n        document.body.appendChild(link);\n        link.click();\n        document.body.removeChild(link);\n    }\n\n    return (\n        <div className=\"h-full flex flex-col p-4 sm:p-6 lg:p-8 text-text-primary\">\n            <header className=\"mb-6\">\n                <h1 className=\"text-3xl font-bold flex items-center\">\n                    <ImageGeneratorIcon />\n                    <span className=\"ml-3\">AI Image Generator</span>\n                </h1>\n                <p className=\"text-text-secondary mt-1\">Generate stunning, high-quality images from text prompts using Imagen 3.</p>\n            </header>\n            \n            <div className=\"flex flex-col gap-4 mb-4\">\n                <label htmlFor=\"prompt-input\" className=\"text-sm font-medium text-text-secondary\">Your Prompt</label>\n                <textarea\n                    id=\"prompt-input\"\n                    value={prompt}\n                    onChange={(e) => setPrompt(e.target.value)}\n                    placeholder=\"e.g., A cute cat wearing a wizard hat\"\n                    className=\"w-full p-3 rounded-md bg-surface border border-border focus:ring-2 focus:ring-primary focus:outline-none resize-y\"\n                    rows={3}\n                />\n                <div className=\"flex gap-2\">\n                    <button\n                        onClick={handleGenerate}\n                        disabled={isLoading}\n                        className=\"btn-primary w-full flex items-center justify-center px-6 py-3\"\n                    >\n                        {isLoading ? <LoadingSpinner /> : 'Generate Image'}\n                    </button>\n                    <button\n                        onClick={handleSurpriseMe}\n                        disabled={isLoading}\n                        className=\"px-4 py-3 bg-surface border border-border rounded-md hover:bg-gray-100 transition-colors\"\n                        title=\"Surprise Me!\"\n                    >\n                        <SparklesIcon />\n                    </button>\n                </div>\n            </div>\n\n            <div className=\"flex-grow flex items-center justify-center bg-background border-2 border-dashed border-border rounded-lg p-4 relative overflow-auto\">\n                {isLoading && <LoadingSpinner />}\n                {error && <p className=\"text-red-500 text-center\">{error}</p>}\n                {imageUrl && !isLoading && (\n                    <>\n                        <img src={imageUrl} alt={prompt || \"Generated by AI\"} className=\"max-w-full max-h-full object-contain rounded-md shadow-lg\" />\n                        <button \n                          onClick={handleDownload}\n                          className=\"absolute top-4 right-4 p-2 bg-black/30 text-white rounded-full hover:bg-black/50 backdrop-blur-sm\"\n                          title=\"Download Image\"\n                        >\n                            <ArrowDownTrayIcon />\n                        </button>\n                    </>\n                )}\n                {!isLoading && !imageUrl && !error && (\n                    <div className=\"text-center text-text-secondary\">\n                        <p>Your generated image will appear here.</p>\n                    </div>\n                )}\n            </div>\n        </div>\n    );\n};"],"names":["surprisePrompts","AiImageGenerator","prompt","setPrompt","useState","imageUrl","setImageUrl","isLoading","setIsLoading","error","setError","handleGenerate","useCallback","resultUrl","generateImage","err","errorMessage","handleSurpriseMe","randomPrompt","handleDownload","link","jsxs","jsx","ImageGeneratorIcon","e","LoadingSpinner","SparklesIcon","Fragment","ArrowDownTrayIcon"],"mappings":"qUAMA,MAAMA,EAAkB,CACpB,qEACA,sEACA,+CACA,wDACA,yDACJ,EAEaC,EAA6B,IAAM,CAC5C,KAAM,CAACC,EAAQC,CAAS,EAAIC,EAAAA,SAAiB,0EAA0E,EACjH,CAACC,EAAUC,CAAW,EAAIF,EAAAA,SAAwB,IAAI,EACtD,CAACG,EAAWC,CAAY,EAAIJ,EAAAA,SAAkB,EAAK,EACnD,CAACK,EAAOC,CAAQ,EAAIN,EAAAA,SAAiB,EAAE,EAEvCO,EAAiBC,EAAAA,YAAY,SAAY,CAC3C,GAAI,CAACV,EAAO,OAAQ,CAChBQ,EAAS,6CAA6C,EACtD,MACJ,CACAF,EAAa,EAAI,EACjBE,EAAS,EAAE,EACXJ,EAAY,IAAI,EAChB,GAAI,CACA,MAAMO,EAAY,MAAMC,EAAcZ,CAAM,EAC5CI,EAAYO,CAAS,CACzB,OAASE,EAAK,CACV,MAAMC,EAAeD,aAAe,MAAQA,EAAI,QAAU,6BAC1DL,EAAS,6BAA6BM,CAAY,EAAE,CACxD,QAAA,CACIR,EAAa,EAAK,CACtB,CACJ,EAAG,CAACN,CAAM,CAAC,EAELe,EAAmB,IAAM,CAC3B,MAAMC,EAAelB,EAAgB,KAAK,MAAM,KAAK,OAAA,EAAWA,EAAgB,MAAM,CAAC,EACvFG,EAAUe,CAAY,CAC1B,EAEMC,EAAiB,IAAM,CACzB,GAAI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