Spaces:
Running
Running
File size: 6,758 Bytes
cfa8e01 dc526d2 cfa8e01 dc526d2 cfa8e01 dc526d2 cfa8e01 dc526d2 cfa8e01 dc526d2 cfa8e01 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
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);
}
|