Spaces:
Running
Running
Download script.js from Sphken23/ai-artisan-studio: direct link, hf CLI and curl.
- Browser
- Download file 6.76 kB
-
https://huggingface.co/spaces/Sphken23/ai-artisan-studio/resolve/main/script.js
- Command line
-
hf download hf://spaces/Sphken23/ai-artisan-studio/script.js
-
curl -L -o script.js https://huggingface.co/spaces/Sphken23/ai-artisan-studio/resolve/main/script.js
6.76 kB
| document.addEventListener('DOMContentLoaded', function() { | |
| const generateBtn = document.querySelector('button'); | |
| const imagePlaceholder = document.querySelector('.bg-gray-200'); | |
| const textarea = document.querySelector('textarea'); | |
| const styleSelect = document.querySelector('select'); | |
| const imageDetails = document.getElementById('image-details'); | |
| const detailText = document.getElementById('detail-text'); | |
| // Example prompts for different styles | |
| const styleExamples = { | |
| portrait: "A professional headshot of a software developer with glasses, modern office background, soft lighting", | |
| landscape: "Sunset over tropical beach with palm trees, vibrant colors, 4K resolution", | |
| abstract: "Geometric shapes in neon colors floating in dark space, digital art style", | |
| product: "Minimalist product photo of a smartphone on marble surface, studio lighting", | |
| fantasy: "Ancient dragon perched on castle tower, moonlit night, highly detailed", | |
| realistic: "Close-up of a chef preparing sushi in a restaurant kitchen, ultra-realistic", | |
| anime: "Anime-style character with pink hair wearing school uniform, cherry blossoms background", | |
| watercolor: "Watercolor painting of Paris streets in spring, loose brush strokes" | |
| }; | |
| styleSelect.addEventListener('change', function() { | |
| textarea.placeholder = `Example: "${styleExamples[this.value]}"`; | |
| }); | |
| generateBtn.addEventListener('click', async function() { | |
| const description = textarea.value.trim(); | |
| if (!description) { | |
| alert("Please enter an image description"); | |
| return; | |
| } | |
| // Show loading state | |
| imagePlaceholder.innerHTML = '<div class="animate-pulse w-full h-full flex items-center justify-center"><i data-feather="loader" class="w-16 h-16 text-gray-400 animate-spin"></i></div>'; | |
| feather.replace(); | |
| try { | |
| // First translate non-English text to English for better results | |
| let processedDescription = description; | |
| // Detect if text is non-English (simple check for non-ASCII characters) | |
| if (/[^\x00-\x7F]/.test(description)) { | |
| const translationResponse = await fetch(`https://api.mymemory.translated.net/get?q=${encodeURIComponent(description)}&langpair=auto|en`); | |
| const translationData = await translationResponse.json(); | |
| if (translationData.responseData && translationData.responseData.translatedText) { | |
| processedDescription = translationData.responseData.translatedText; | |
| // Show translation feedback | |
| imagePlaceholder.innerHTML = ` | |
| <div class="w-full h-full flex flex-col items-center justify-center p-4"> | |
| <div class="bg-blue-50 p-4 rounded-lg mb-4"> | |
| <p class="text-sm text-blue-800"><span class="font-semibold">Original:</span> ${description}</p> | |
| <p class="text-sm text-blue-800 mt-1"><span class="font-semibold">Translated:</span> ${processedDescription}</p> | |
| </div> | |
| <i data-feather="loader" class="w-16 h-16 text-gray-400 animate-spin"></i> | |
| </div> | |
| `; | |
| feather.replace(); | |
| } | |
| } | |
| // Generate random seed based on processed description for consistency | |
| const seed = hashCode(processedDescription); | |
| const style = styleSelect.value; | |
| const category = getCategoryForStyle(style); | |
| // Enhance prompt with style keywords | |
| const enhancedPrompt = `${processedDescription}, ${style} style, high quality, detailed`; | |
| const generatedImage = `http://static.photos/${category}/1024x576/${seed}`; | |
| imagePlaceholder.innerHTML = ` | |
| <img src="${generatedImage}" alt="Generated image" class="w-full h-full object-cover rounded-lg" | |
| onmouseenter="showDetails('${enhancedPrompt}', '${style}')" | |
| onmouseleave="hideDetails()"> | |
| `; | |
| // Store image data for download | |
| imagePlaceholder.dataset.imageUrl = generatedImage; | |
| imagePlaceholder.dataset.description = enhancedPrompt; | |
| // Add download button | |
| const downloadBtn = document.createElement('button'); | |
| downloadBtn.className = 'mt-4 bg-green-600 hover:bg-green-700 text-white font-bold py-2 px-4 rounded-lg transition duration-200'; | |
| downloadBtn.textContent = 'Download Image'; | |
| downloadBtn.onclick = downloadImage; | |
| imagePlaceholder.parentNode.insertBefore(downloadBtn, imagePlaceholder.nextSibling); | |
| } catch (error) { | |
| console.error("Error generating image:", error); | |
| imagePlaceholder.innerHTML = '<div class="w-full h-full flex items-center justify-center text-red-500"><i data-feather="alert-circle" class="w-16 h-16"></i><p class="ml-2">Error generating image</p></div>'; | |
| feather.replace(); | |
| } | |
| }); | |
| // Helper function to generate hash code | |
| function hashCode(str) { | |
| let hash = 0; | |
| for (let i = 0; i < str.length; i++) { | |
| hash = str.charCodeAt(i) + ((hash << 5) - hash); | |
| } | |
| return Math.abs(hash % 1000); | |
| } | |
| // Map styles to photo categories | |
| function getCategoryForStyle(style) { | |
| const styleMap = { | |
| portrait: 'people', | |
| landscape: 'nature', | |
| abstract: 'abstract', | |
| product: 'workspace', | |
| fantasy: 'vintage', | |
| realistic: 'office', | |
| anime: 'education', | |
| watercolor: 'textures' | |
| }; | |
| return styleMap[style] || 'abstract'; | |
| } | |
| }); | |
| // Global functions for image hover details | |
| function showDetails(description, style) { | |
| const details = document.getElementById('image-details'); | |
| const text = document.getElementById('detail-text'); | |
| text.textContent = `${style} style: ${description.substring(0, 100)}${description.length > 100 ? '...' : ''}`; | |
| details.classList.remove('hidden'); | |
| } | |
| function hideDetails() { | |
| document.getElementById('image-details').classList.add('hidden'); | |
| } | |
| function downloadImage() { | |
| const placeholder = document.querySelector('.bg-gray-200'); | |
| const link = document.createElement('a'); | |
| link.href = placeholder.dataset.imageUrl; | |
| link.download = `ai-art-${Date.now()}.jpg`; | |
| document.body.appendChild(link); | |
| link.click(); | |
| document.body.removeChild(link); | |
| } | |