Spaces:
Running
Running
File size: 19,182 Bytes
4a02a00 |
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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 |
<!DOCTYPE html>
<html lang="en" class="dark">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image Style Transfer</title>
<script src="https://cdn.tailwindcss.com"></script>
<script>
tailwind.config = {
darkMode: 'class',
theme: {
extend: {
colors: {
dark: {
50: '#f9fafb',
100: '#1a1b1e',
200: '#2d2e32',
300: '#3b3c41',
400: '#4a4b50'
}
}
}
}
}
</script>
<style>
.drop-zone {
transition: all 0.3s ease;
}
.drop-zone.dragover {
background-color: rgba(59, 130, 246, 0.1);
border-color: #3b82f6;
}
.dark .drop-zone.dragover {
background-color: rgba(59, 130, 246, 0.2);
}
@keyframes pulse {
0% {
transform: scale(1);
}
50% {
transform: scale(1.05);
}
100% {
transform: scale(1);
}
}
.pulse {
animation: pulse 2s infinite;
}
.modal {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.9);
z-index: 1000;
backdrop-filter: blur(5px);
}
.modal-content {
max-width: 90%;
max-height: 90vh;
margin: auto;
position: relative;
top: 50%;
transform: translateY(-50%);
}
.image-container {
position: relative;
overflow: hidden;
border-radius: 0.5rem;
background: linear-gradient(to bottom right, #1a1b1e, #2d2e32);
}
.download-btn {
position: absolute;
top: 0.75rem;
right: 0.75rem;
opacity: 0;
transition: opacity 0.2s;
}
.use-input-btn {
position: absolute;
top: 0.75rem;
left: 0.75rem;
opacity: 0;
transition: opacity 0.2s;
}
.image-container:hover .download-btn,
.image-container:hover .use-input-btn {
opacity: 1;
}
.hidden {
display: none !important;
}
.history-divider {
border-top: 1px solid rgba(75, 85, 99, 0.3);
margin: 1rem 0;
text-align: center;
position: relative;
}
.history-divider span {
background: #1a1b1e;
padding: 0 1rem;
position: relative;
top: -0.75rem;
color: #6b7280;
font-size: 0.875rem;
}
.noscroll::-webkit-scrollbar {
display: none;
}
.skeleton {
background: linear-gradient(110deg, #2d2e32 30%, #3b3c41 50%, #2d2e32 70%);
background-size: 200% 100%;
animation: loading 1.5s ease-in-out infinite;
}
@keyframes loading {
to {
background-position-x: -200%;
}
}
</style>
<script>
function setupDragDrop() {
const dropZone = document.getElementById('drop-zone');
['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
dropZone.addEventListener(eventName, preventDefaults, false);
});
function preventDefaults(e) {
e.preventDefault();
e.stopPropagation();
}
['dragenter', 'dragover'].forEach(eventName => {
dropZone.addEventListener(eventName, highlight, false);
});
['dragleave', 'drop'].forEach(eventName => {
dropZone.addEventListener(eventName, unhighlight, false);
});
function highlight(e) {
dropZone.classList.add('dragover');
}
function unhighlight(e) {
dropZone.classList.remove('dragover');
}
dropZone.addEventListener('drop', handleDrop, false);
function handleDrop(e) {
const dt = e.dataTransfer;
const file = dt.files[0];
handleImageFile(file);
}
}
function handleImageFile(file) {
if (file && file.type.startsWith('image/')) {
const reader = new FileReader();
reader.onload = function (e) {
document.getElementById('preview').src = e.target.result;
document.getElementById('preview').classList.remove('hidden');
document.getElementById('drop-text').classList.add('hidden');
};
reader.readAsDataURL(file);
}
}
function handleImageUpload(event) {
const file = event.target.files[0];
if (file) handleImageFile(file);
}
function setRandomSeed() {
document.getElementById('seed').value = Math.floor(Math.random() * 1000000);
}
function downloadImage(imageUrl) {
const link = document.createElement('a');
link.href = imageUrl;
link.download = 'styled-image-' + Date.now() + '.png';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
function useAsInput(imageUrl) {
// Convert base64/URL to blob
fetch(imageUrl)
.then(res => res.blob())
.then(blob => {
// Create a File object
const file = new File([blob], "image.png", { type: "image/png" });
// Update the file input
const dataTransfer = new DataTransfer();
dataTransfer.items.add(file);
document.getElementById('file-input').files = dataTransfer.files;
// Update preview
document.getElementById('preview').src = imageUrl;
document.getElementById('preview').classList.remove('hidden');
document.getElementById('drop-text').classList.add('hidden');
window.scrollTo({ top: 0, behavior: 'smooth' });
});
}
async function generateImages() {
const submitButton = document.getElementById('submit-btn');
const gallery = document.getElementById('gallery');
const errorDiv = document.getElementById('error');
submitButton.disabled = true;
submitButton.textContent = 'Generating...';
errorDiv.textContent = '';
errorDiv.classList.add('hidden');
// Add timestamp divider with formatted time
const now = new Date();
const timestamp = now.toLocaleTimeString('en-US', {
hour: 'numeric',
minute: '2-digit',
second: '2-digit',
hour12: true
});
const divider = document.createElement('div');
divider.className = 'history-divider';
divider.innerHTML = `<span>${timestamp}</span>`;
gallery.insertBefore(divider, gallery.firstChild);
// Create new container
const generationContainer = document.createElement('div');
generationContainer.className = 'grid grid-cols-1 sm:grid-cols-2 gap-4 mb-6';
gallery.insertBefore(generationContainer, gallery.firstChild);
// Add skeletons based on num_images
const numImages = document.getElementById('num-images').value;
for (let i = 0; i < numImages; i++) {
const skeleton = document.createElement('div');
skeleton.className = 'p-2';
skeleton.innerHTML = `
<div class="aspect-square rounded-lg skeleton animate-pulse"></div>
`;
generationContainer.appendChild(skeleton);
}
try {
const response = await fetch('/generate', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
prompt: document.getElementById('prompt').value,
image: document.getElementById('preview').src,
num_images: document.getElementById('num-images').value,
guidance_scale: document.getElementById('guidance-scale').value,
seed: document.getElementById('seed').value,
})
});
const data = await response.json();
if (response.ok) {
// Clear skeletons
generationContainer.innerHTML = '';
data.images.forEach(imageUrl => {
const container = document.createElement('div');
container.className = 'image-container shadow-xl hover:shadow-2xl transition-all duration-300';
const img = document.createElement('img');
img.src = imageUrl;
img.className = 'w-full h-auto rounded-lg cursor-pointer hover:opacity-95 transition-opacity';
img.onclick = () => showModal(imageUrl);
const downloadBtn = document.createElement('button');
downloadBtn.className = 'download-btn p-2 bg-gray-900/80 hover:bg-gray-900 rounded-full shadow-lg';
downloadBtn.innerHTML = `<svg class="w-5 h-5 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-4l-4 4m0 0l-4-4m4 4V4"/>
</svg>`;
downloadBtn.onclick = (e) => {
e.stopPropagation();
downloadImage(imageUrl);
};
const useInputBtn = document.createElement('button');
useInputBtn.className = 'use-input-btn p-2 bg-gray-900/80 hover:bg-gray-900 rounded-full shadow-lg';
useInputBtn.innerHTML = `<svg class="w-5 h-5 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z"/>
</svg>`;
useInputBtn.onclick = (e) => {
e.stopPropagation();
useAsInput(imageUrl);
};
container.appendChild(img);
container.appendChild(downloadBtn);
container.appendChild(useInputBtn);
const wrapper = document.createElement('div');
wrapper.className = 'p-2';
wrapper.appendChild(container);
generationContainer.appendChild(wrapper);
});
} else {
throw new Error(data.error || 'Failed to generate images');
}
} catch (error) {
// Clear skeletons on error
generationContainer.innerHTML = '';
errorDiv.textContent = error.message;
errorDiv.classList.remove('hidden');
} finally {
submitButton.disabled = false;
submitButton.textContent = 'Generate Images';
}
}
function initializeClickUpload() {
const dropZone = document.getElementById('drop-zone');
const fileInput = document.getElementById('file-input');
dropZone.addEventListener('click', () => {
fileInput.click();
});
}
function showModal(imageUrl) {
const modal = document.getElementById('imageModal');
const modalImg = document.getElementById('modalImage');
modalImg.src = imageUrl;
modal.style.display = 'block';
document.body.style.overflow = 'hidden';
}
function closeModal() {
const modal = document.getElementById('imageModal');
modal.style.display = 'none';
document.body.style.overflow = 'auto';
}
document.addEventListener('DOMContentLoaded', () => {
setupDragDrop();
initializeClickUpload();
});
</script>
</head>
<body class="bg-dark-200 min-h-screen py-8 text-gray-100">
<div class="container mx-auto px-4 max-w-6xl">
<h1 class="text-4xl font-bold text-left mb-1 text-gray-100">FLUX.1 Kontext [max]</h1>
<p class=" mb-8 ">Image editing and manipulation model guidance-distilled from FLUX.1 Kontext [max]</p>
<div class="grid grid-cols-1 md:grid-cols-2 gap-8">
<div class="bg-dark-100 p-8 rounded-2xl shadow-lg border border-dark-300">
<div class="mb-8">
<div id="drop-zone"
class="drop-zone border-2 border-dashed border-dark-300 rounded-lg p-8 text-center cursor-pointer hover:border-blue-500 transition-colors">
<input type="file" accept="image/*" onChange="handleImageUpload(event)" class="hidden"
id="file-input">
<img id="preview" class="hidden w-full h-auto rounded-lg" alt="Preview">
<div id="drop-text">
<svg class="mx-auto h-12 w-12 text-gray-500 mb-4" stroke="currentColor" fill="none"
viewBox="0 0 48 48">
<path
d="M28 8H12a4 4 0 00-4 4v20m32-12v8m0 0v8a4 4 0 01-4 4H12a4 4 0 01-4-4v-4m32-4l-3.172-3.172a4 4 0 00-5.656 0L28 28M8 32l9.172-9.172a4 4 0 015.656 0L28 28m0 0l4 4m4-24h8m-4-4v8m-12 4h.02"
stroke-width="2" stroke-linecap="round" stroke-linejoin="round" />
</svg>
<p class="text-gray-400">Drag and drop an image here, or click to select</p>
</div>
</div>
</div>
<div class="mb-6">
<label class="block text-gray-300 font-medium mb-2" for="prompt">
Prompt
</label>
<textarea id="prompt"
class="w-full p-3 bg-dark-300 border border-dark-400 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-all text-gray-100"
rows="3">Change the style to a vibrant anime style, detailed, high quality.</textarea>
</div>
<div class="grid grid-cols-2 gap-6 mb-6">
<div>
<label class="block text-gray-300 font-medium mb-2" for="num-images">
Number of Images
</label>
<input type="number" id="num-images" value="1" min="1" max="4"
class="w-full p-3 bg-dark-300 border border-dark-400 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-all text-gray-100">
</div>
<div>
<label class="block text-gray-300 font-medium mb-2" for="guidance-scale">
Guidance Scale
</label>
<input type="number" id="guidance-scale" value="3.5" min="1.0" max="20.0" step="0.1"
class="w-full p-3 bg-dark-300 border border-dark-400 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-all text-gray-100">
</div>
</div>
<div class="mb-8">
<label class="block text-gray-300 font-medium mb-2" for="seed">
Seed
</label>
<div class="flex gap-2">
<input type="number" id="seed" value="24"
class="flex-1 p-3 bg-dark-300 border border-dark-400 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-all text-gray-100">
<button onclick="setRandomSeed()"
class="px-4 py-2 bg-dark-300 hover:bg-dark-400 text-gray-300 rounded-lg transition-colors">
🎲 Random
</button>
</div>
</div>
<button id="submit-btn" onclick="generateImages()"
class="w-full bg-blue-600 hover:bg-blue-700 text-white font-medium py-3 px-4 rounded-lg transition-colors transform hover:scale-[1.02] active:scale-[0.98]">
Generate Images
</button>
<div id="error" class="hidden mt-4 p-4 bg-red-900/50 text-red-400 text-sm rounded-lg"></div>
</div>
<div class="bg-dark-100 p-8 rounded-2xl shadow-lg border border-dark-300 h-fit">
<h2 class="text-2xl font-bold mb-6 text-gray-100">Generated Images History</h2>
<div id="gallery" class="space-y-6 max-h-[80vh] overflow-y-auto noscroll" >
<!-- Generated images will appear here -->
</div>
</div>
</div>
</div>
<!-- Add modal at the end of body -->
<div id="imageModal" class="modal" onclick="closeModal()">
<img id="modalImage" class="modal-content cursor-pointer" alt="Full size image">
</div>
</body>
</html> |