Spaces:
Sleeping
Sleeping
Update static/script.js
Browse files- static/script.js +252 -68
static/script.js
CHANGED
|
@@ -1,68 +1,252 @@
|
|
| 1 |
-
//
|
| 2 |
-
const glowingEffect = document.createElement('div');
|
| 3 |
-
glowingEffect.classList.add('glowing-effect');
|
| 4 |
-
document.body.appendChild(glowingEffect);
|
| 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 |
-
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// ---------- Glowing cursor effect ----------
|
| 2 |
+
const glowingEffect = document.createElement('div');
|
| 3 |
+
glowingEffect.classList.add('glowing-effect', 'js-generated');
|
| 4 |
+
document.body.appendChild(glowingEffect);
|
| 5 |
+
|
| 6 |
+
// Update CSS custom properties for the glow position (mouse + touch)
|
| 7 |
+
function updateGlow(x, y) {
|
| 8 |
+
glowingEffect.style.setProperty('--x', `${x}px`);
|
| 9 |
+
glowingEffect.style.setProperty('--y', `${y}px`);
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
document.addEventListener('mousemove', (e) => updateGlow(e.clientX, e.clientY));
|
| 13 |
+
document.addEventListener('touchmove', (e) => {
|
| 14 |
+
if (e.touches && e.touches[0]) {
|
| 15 |
+
updateGlow(e.touches[0].clientX, e.touches[0].clientY);
|
| 16 |
+
}
|
| 17 |
+
}, { passive: true });
|
| 18 |
+
|
| 19 |
+
// ---------- Helper utilities ----------
|
| 20 |
+
const qs = (id) => document.getElementById(id);
|
| 21 |
+
const showOutput = (html) => {
|
| 22 |
+
const outputDiv = qs('output') || (() => {
|
| 23 |
+
const d = document.createElement('div');
|
| 24 |
+
d.id = 'output';
|
| 25 |
+
document.body.appendChild(d);
|
| 26 |
+
return d;
|
| 27 |
+
})();
|
| 28 |
+
outputDiv.innerHTML = html;
|
| 29 |
+
};
|
| 30 |
+
const createDownloadLink = (url, label = 'Download') => {
|
| 31 |
+
const a = document.createElement('a');
|
| 32 |
+
a.href = url;
|
| 33 |
+
a.textContent = label;
|
| 34 |
+
a.download = '';
|
| 35 |
+
a.className = 'download-link';
|
| 36 |
+
return a;
|
| 37 |
+
};
|
| 38 |
+
const createImgPreview = (src, alt = 'preview') => {
|
| 39 |
+
const img = document.createElement('img');
|
| 40 |
+
img.src = src;
|
| 41 |
+
img.alt = alt;
|
| 42 |
+
img.style.maxWidth = '320px';
|
| 43 |
+
img.style.height = 'auto';
|
| 44 |
+
img.style.display = 'block';
|
| 45 |
+
img.style.marginTop = '12px';
|
| 46 |
+
img.style.borderRadius = '8px';
|
| 47 |
+
return img;
|
| 48 |
+
};
|
| 49 |
+
|
| 50 |
+
// ---------- Elements (gracefully handle missing elements) ----------
|
| 51 |
+
const imageInput = qs('image') || qs('image-input'); // caption image
|
| 52 |
+
const foregroundInput = qs('foreground'); // foreground for change bg / remove bg
|
| 53 |
+
const backgroundInput = qs('background'); // background for change bg (optional)
|
| 54 |
+
const generateCaptionBtn = qs('generate-caption-btn');
|
| 55 |
+
const removeBackgroundBtn = qs('remove-background-btn');
|
| 56 |
+
const changeBackgroundBtn = qs('change-background-btn');
|
| 57 |
+
|
| 58 |
+
// ---------- Core request functions using FormData ----------
|
| 59 |
+
async function postFormData(url, formData) {
|
| 60 |
+
const res = await fetch(url, {
|
| 61 |
+
method: 'POST',
|
| 62 |
+
body: formData, // do NOT set Content-Type; browser will add the correct boundary
|
| 63 |
+
});
|
| 64 |
+
if (!res.ok) {
|
| 65 |
+
const text = await res.text().catch(() => '');
|
| 66 |
+
throw new Error(`Server responded ${res.status}: ${text}`);
|
| 67 |
+
}
|
| 68 |
+
return res.json().catch(() => { throw new Error('Invalid JSON response'); });
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
// ---------- Generate Caption ----------
|
| 72 |
+
if (generateCaptionBtn) {
|
| 73 |
+
generateCaptionBtn.addEventListener('click', async (e) => {
|
| 74 |
+
e.preventDefault();
|
| 75 |
+
try {
|
| 76 |
+
const fileInput = imageInput;
|
| 77 |
+
if (!fileInput || !fileInput.files || fileInput.files.length === 0) {
|
| 78 |
+
showOutput('<span style="color:#b00">Please choose an image to generate caption.</span>');
|
| 79 |
+
return;
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
showOutput('Generating caption…');
|
| 83 |
+
|
| 84 |
+
const fd = new FormData();
|
| 85 |
+
fd.append('image', fileInput.files[0]);
|
| 86 |
+
|
| 87 |
+
const data = await postFormData('/generate_caption', fd);
|
| 88 |
+
// Expecting JSON like: { caption: "...", image_url: "..." } but handle flexibly
|
| 89 |
+
const caption = data.caption || data.text || '';
|
| 90 |
+
const imageUrl = data.image_url || data.result_url || data.output_url || null;
|
| 91 |
+
|
| 92 |
+
let html = `<strong>Generated Caption:</strong><div style="margin-top:8px">${caption || '<em>(no caption returned)</em>'}</div>`;
|
| 93 |
+
if (imageUrl) {
|
| 94 |
+
const img = createImgPreview(imageUrl, 'Generated caption preview');
|
| 95 |
+
html += '<div></div>'; // separator
|
| 96 |
+
// we will set outputDiv innerHTML, then append image node and download link
|
| 97 |
+
}
|
| 98 |
+
showOutput(html);
|
| 99 |
+
|
| 100 |
+
if (imageUrl) {
|
| 101 |
+
const outDiv = qs('output');
|
| 102 |
+
outDiv.appendChild(createImgPreview(imageUrl, 'Generated caption preview'));
|
| 103 |
+
outDiv.appendChild(createDownloadLink(imageUrl, 'Download Result Image'));
|
| 104 |
+
// Optional: add "Copy caption" button
|
| 105 |
+
if (caption) {
|
| 106 |
+
const copyBtn = document.createElement('button');
|
| 107 |
+
copyBtn.textContent = 'Copy Caption';
|
| 108 |
+
copyBtn.style.marginLeft = '8px';
|
| 109 |
+
copyBtn.addEventListener('click', () => {
|
| 110 |
+
navigator.clipboard?.writeText(caption).then(() => {
|
| 111 |
+
copyBtn.textContent = 'Copied!';
|
| 112 |
+
setTimeout(() => copyBtn.textContent = 'Copy Caption', 1500);
|
| 113 |
+
}).catch(() => {
|
| 114 |
+
copyBtn.textContent = 'Copy failed';
|
| 115 |
+
});
|
| 116 |
+
});
|
| 117 |
+
outDiv.appendChild(copyBtn);
|
| 118 |
+
}
|
| 119 |
+
}
|
| 120 |
+
} catch (err) {
|
| 121 |
+
console.error(err);
|
| 122 |
+
showOutput(`<span style="color:#b00">Error generating caption: ${err.message}</span>`);
|
| 123 |
+
}
|
| 124 |
+
});
|
| 125 |
+
}
|
| 126 |
+
|
| 127 |
+
// ---------- Remove Background ----------
|
| 128 |
+
if (removeBackgroundBtn) {
|
| 129 |
+
removeBackgroundBtn.addEventListener('click', async (e) => {
|
| 130 |
+
e.preventDefault();
|
| 131 |
+
try {
|
| 132 |
+
// Prefer explicit foreground input; fallback to generic image input
|
| 133 |
+
const fileInput = foregroundInput || imageInput;
|
| 134 |
+
if (!fileInput || !fileInput.files || fileInput.files.length === 0) {
|
| 135 |
+
showOutput('<span style="color:#b00">Please choose an image to remove background.</span>');
|
| 136 |
+
return;
|
| 137 |
+
}
|
| 138 |
+
|
| 139 |
+
showOutput('Removing background…');
|
| 140 |
+
|
| 141 |
+
const fd = new FormData();
|
| 142 |
+
fd.append('image', fileInput.files[0]);
|
| 143 |
+
|
| 144 |
+
const data = await postFormData('/remove_background', fd);
|
| 145 |
+
// Expect JSON like: { output_url: "..." } or { output_path: "..." }
|
| 146 |
+
const outUrl = data.output_url || data.output_path || data.result_url || null;
|
| 147 |
+
|
| 148 |
+
if (outUrl) {
|
| 149 |
+
const outHtml = '<strong>Removed Background:</strong>';
|
| 150 |
+
showOutput(outHtml);
|
| 151 |
+
const outDiv = qs('output');
|
| 152 |
+
outDiv.appendChild(createImgPreview(outUrl, 'Result - background removed'));
|
| 153 |
+
outDiv.appendChild(createDownloadLink(outUrl, 'Download Result Image'));
|
| 154 |
+
} else {
|
| 155 |
+
showOutput('<span style="color:#b00">Background removed but server did not return a URL.</span>');
|
| 156 |
+
}
|
| 157 |
+
} catch (err) {
|
| 158 |
+
console.error(err);
|
| 159 |
+
showOutput(`<span style="color:#b00">Error removing background: ${err.message}</span>`);
|
| 160 |
+
}
|
| 161 |
+
});
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
// ---------- Change Background ----------
|
| 165 |
+
if (changeBackgroundBtn) {
|
| 166 |
+
changeBackgroundBtn.addEventListener('click', async (e) => {
|
| 167 |
+
e.preventDefault();
|
| 168 |
+
try {
|
| 169 |
+
const fgInput = foregroundInput || imageInput;
|
| 170 |
+
if (!fgInput || !fgInput.files || fgInput.files.length === 0) {
|
| 171 |
+
showOutput('<span style="color:#b00">Please choose a foreground image.</span>');
|
| 172 |
+
return;
|
| 173 |
+
}
|
| 174 |
+
|
| 175 |
+
// Option A: use a second file input for background if available
|
| 176 |
+
// Option B: fallback to a hard-coded URL (if your server supports that)
|
| 177 |
+
const bgFile = (backgroundInput && backgroundInput.files && backgroundInput.files[0]) || null;
|
| 178 |
+
const FALLBACK_BACKGROUND_URL = null; // set a URL string here only if your server supports background URLs
|
| 179 |
+
|
| 180 |
+
if (!bgFile && !FALLBACK_BACKGROUND_URL) {
|
| 181 |
+
showOutput('<span style="color:#b00">Please choose a background image (or configure FALLBACK_BACKGROUND_URL in the script).</span>');
|
| 182 |
+
return;
|
| 183 |
+
}
|
| 184 |
+
|
| 185 |
+
showOutput('Changing background…');
|
| 186 |
+
|
| 187 |
+
const fd = new FormData();
|
| 188 |
+
fd.append('foreground', fgInput.files[0]);
|
| 189 |
+
if (bgFile) {
|
| 190 |
+
fd.append('background', bgFile);
|
| 191 |
+
} else {
|
| 192 |
+
fd.append('background_url', FALLBACK_BACKGROUND_URL);
|
| 193 |
+
}
|
| 194 |
+
|
| 195 |
+
const data = await postFormData('/change_background', fd);
|
| 196 |
+
const outUrl = data.output_url || data.output_path || data.result_url || null;
|
| 197 |
+
|
| 198 |
+
if (outUrl) {
|
| 199 |
+
showOutput('<strong>Changed Background:</strong>');
|
| 200 |
+
const outDiv = qs('output');
|
| 201 |
+
outDiv.appendChild(createImgPreview(outUrl, 'Result - background changed'));
|
| 202 |
+
outDiv.appendChild(createDownloadLink(outUrl, 'Download Result Image'));
|
| 203 |
+
} else {
|
| 204 |
+
showOutput('<span style="color:#b00">Background changed but server did not return a URL.</span>');
|
| 205 |
+
}
|
| 206 |
+
} catch (err) {
|
| 207 |
+
console.error(err);
|
| 208 |
+
showOutput(`<span style="color:#b00">Error changing background: ${err.message}</span>`);
|
| 209 |
+
}
|
| 210 |
+
});
|
| 211 |
+
}
|
| 212 |
+
|
| 213 |
+
// ---------- Accessibility: enable Enter key on focused file inputs when needed ----------
|
| 214 |
+
document.addEventListener('keydown', (e) => {
|
| 215 |
+
if (e.key === 'Enter') {
|
| 216 |
+
// try to trigger caption generation if that button exists and is visible
|
| 217 |
+
const active = document.activeElement;
|
| 218 |
+
if (active && (active.tagName === 'INPUT' && active.type === 'file')) {
|
| 219 |
+
// don't auto-submit; just do nothing (files require user action)
|
| 220 |
+
return;
|
| 221 |
+
}
|
| 222 |
+
}
|
| 223 |
+
});
|
| 224 |
+
|
| 225 |
+
// ---------- Optional: add minimal CSS for the glowing effect if not already in CSS ----------
|
| 226 |
+
(function ensureGlowCss() {
|
| 227 |
+
const id = 'glow-css';
|
| 228 |
+
if (document.getElementById(id)) return;
|
| 229 |
+
const style = document.createElement('style');
|
| 230 |
+
style.id = id;
|
| 231 |
+
style.textContent = `
|
| 232 |
+
.glowing-effect {
|
| 233 |
+
position: fixed;
|
| 234 |
+
left: 0;
|
| 235 |
+
top: 0;
|
| 236 |
+
width: 240px;
|
| 237 |
+
height: 240px;
|
| 238 |
+
pointer-events: none;
|
| 239 |
+
transform: translate(calc(var(--x, 0) - 50%), calc(var(--y, 0) - 50%));
|
| 240 |
+
mix-blend-mode: screen;
|
| 241 |
+
filter: blur(40px);
|
| 242 |
+
z-index: 9999;
|
| 243 |
+
transition: transform 0.04s linear;
|
| 244 |
+
background: radial-gradient(circle at center, rgba(0,150,255,0.18) 0%, rgba(0,150,255,0.06) 40%, transparent 70%);
|
| 245 |
+
}
|
| 246 |
+
.glowing-effect.js-generated { opacity: 0.95; }
|
| 247 |
+
#output { margin: 14px auto; max-width: 720px; text-align: left; }
|
| 248 |
+
.download-link { display:inline-block; margin-top:8px; padding:8px 12px; background:#007bff; color:#fff; border-radius:6px; text-decoration:none; }
|
| 249 |
+
.download-link:hover { background:#0056b3; }
|
| 250 |
+
`;
|
| 251 |
+
document.head.appendChild(style);
|
| 252 |
+
})();
|