Spaces:
Running
Running
Update index.html
Browse files- index.html +76 -64
index.html
CHANGED
@@ -318,7 +318,7 @@
|
|
318 |
const historyGrid = document.getElementById('history-grid');
|
319 |
|
320 |
// API Endpoint
|
321 |
-
const API_URL = 'https://multimodalart-cosxl.hf.space/
|
322 |
|
323 |
// Current state
|
324 |
let uploadedImage = null;
|
@@ -405,52 +405,65 @@
|
|
405 |
generateBtn.disabled = true;
|
406 |
|
407 |
try {
|
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 |
|
@@ -466,24 +479,23 @@
|
|
466 |
});
|
467 |
|
468 |
function addToHistory(originalUrl, resultUrl) {
|
469 |
-
|
470 |
-
|
471 |
-
|
472 |
-
|
473 |
-
|
474 |
-
|
475 |
-
|
476 |
-
|
477 |
-
|
478 |
-
|
479 |
-
|
480 |
-
|
481 |
-
|
482 |
-
|
483 |
-
|
484 |
-
historyGrid.removeChild(historyGrid.lastChild);
|
485 |
-
}
|
486 |
}
|
|
|
487 |
});
|
488 |
</script>
|
489 |
</body>
|
|
|
318 |
const historyGrid = document.getElementById('history-grid');
|
319 |
|
320 |
// API Endpoint
|
321 |
+
const API_URL = 'https://multimodalart-cosxl.hf.space/api/predict';
|
322 |
|
323 |
// Current state
|
324 |
let uploadedImage = null;
|
|
|
405 |
generateBtn.disabled = true;
|
406 |
|
407 |
try {
|
408 |
+
// Convert image to base64
|
409 |
+
const imageBase64 = await fileToBase64(uploadedFile);
|
410 |
+
|
411 |
+
// Construct API payload
|
412 |
+
const payload = {
|
413 |
+
data: [
|
414 |
+
imageBase64, // Image data
|
415 |
+
colorPrompt.value, // Positive prompt
|
416 |
+
negativePrompt.value, // Negative prompt
|
417 |
+
Number(guidanceScale.value), // Guidance scale
|
418 |
+
Number(steps.value) // Inference steps
|
419 |
+
]
|
420 |
+
};
|
421 |
+
|
422 |
+
// API request
|
423 |
+
const response = await fetch(API_URL, {
|
424 |
+
method: 'POST',
|
425 |
+
headers: {
|
426 |
+
'Content-Type': 'application/json',
|
427 |
+
},
|
428 |
+
body: JSON.stringify(payload)
|
429 |
+
});
|
430 |
+
|
431 |
+
// Handle response errors
|
432 |
+
if (!response.ok) {
|
433 |
+
const errorText = await response.text();
|
434 |
+
throw new Error(`API Error: ${response.status} - ${errorText}`);
|
435 |
+
}
|
436 |
+
|
437 |
+
const result = await response.json();
|
438 |
+
|
439 |
+
// Handle different response formats
|
440 |
+
let resultImageData;
|
441 |
+
if (result.data && result.data[0]) {
|
442 |
+
resultImageData = result.data[0];
|
443 |
+
} else if (result.output) {
|
444 |
+
resultImageData = result.output;
|
445 |
+
} else {
|
446 |
+
throw new Error('Unexpected API response format');
|
447 |
+
}
|
448 |
+
|
449 |
+
// Display results
|
450 |
+
originalImage.src = URL.createObjectURL(uploadedFile);
|
451 |
+
resultImage.src = `data:image/png;base64,${resultImageData}`;
|
452 |
+
emptyState.classList.add('hidden');
|
453 |
+
resultContainer.classList.remove('hidden');
|
454 |
+
|
455 |
+
// Add to history
|
456 |
+
addToHistory(URL.createObjectURL(uploadedFile), `data:image/png;base64,${resultImageData}`);
|
457 |
+
historySection.classList.remove('hidden');
|
458 |
+
|
459 |
+
} catch (error) {
|
460 |
+
console.error('API Error:', error);
|
461 |
+
alert(`Error processing image: ${error.message}`);
|
462 |
+
} finally {
|
463 |
+
generateIcon.classList.remove('hidden');
|
464 |
+
loadingIcon.classList.add('hidden');
|
465 |
+
generateBtn.disabled = false;
|
466 |
+
|
467 |
}
|
468 |
});
|
469 |
|
|
|
479 |
});
|
480 |
|
481 |
function addToHistory(originalUrl, resultUrl) {
|
482 |
+
const historyItem = document.createElement('div');
|
483 |
+
historyItem.className = 'history-item bg-white p-2 rounded-lg shadow-sm cursor-pointer';
|
484 |
+
historyItem.innerHTML = `
|
485 |
+
<img src="${resultUrl}" class="w-full h-auto rounded" alt="History item">
|
486 |
+
`;
|
487 |
+
historyItem.addEventListener('click', () => {
|
488 |
+
originalImage.src = originalUrl;
|
489 |
+
resultImage.src = resultUrl;
|
490 |
+
emptyState.classList.add('hidden');
|
491 |
+
resultContainer.classList.remove('hidden');
|
492 |
+
});
|
493 |
+
historyGrid.insertBefore(historyItem, historyGrid.firstChild);
|
494 |
+
|
495 |
+
if (historyGrid.children.length > 6) {
|
496 |
+
historyGrid.removeChild(historyGrid.lastChild);
|
|
|
|
|
497 |
}
|
498 |
+
}
|
499 |
});
|
500 |
</script>
|
501 |
</body>
|