Spaces:
Paused
Paused
Savestate: Implement Advanced Canvas Rendering Engine with Kalam handwriting font, multiply blend modes, word wrapping, and random rotation for ultra-realistic offline inpainting
Browse files- index.html +1 -1
- src/ai/flux-client.js +61 -41
- src/views/chat/chat-view.js +92 -36
index.html
CHANGED
|
@@ -74,7 +74,7 @@
|
|
| 74 |
</head>
|
| 75 |
<body class="dark-theme in-chat-view">
|
| 76 |
|
| 77 |
-
<div id="live-version-badge" style="position: fixed; top: 50px; left: 50%; transform: translateX(-50%); font-size: 10px; color: var(--text-secondary); opacity: 0.6; z-index: 99999; pointer-events: none; font-weight: 500; font-family: monospace; letter-spacing: 1px; background: rgba(0,0,0,0.2); padding: 2px 8px; border-radius: 10px; border: 1px solid rgba(255,255,255,0.05);">BUILD 1.8.
|
| 78 |
|
| 79 |
<!-- SVG Filters for Liquid Glass Refraction -->
|
| 80 |
<svg width="0" height="0" style="position: absolute; pointer-events: none;">
|
|
|
|
| 74 |
</head>
|
| 75 |
<body class="dark-theme in-chat-view">
|
| 76 |
|
| 77 |
+
<div id="live-version-badge" style="position: fixed; top: 50px; left: 50%; transform: translateX(-50%); font-size: 10px; color: var(--text-secondary); opacity: 0.6; z-index: 99999; pointer-events: none; font-weight: 500; font-family: monospace; letter-spacing: 1px; background: rgba(0,0,0,0.2); padding: 2px 8px; border-radius: 10px; border: 1px solid rgba(255,255,255,0.05);">BUILD 1.8.222</div>
|
| 78 |
|
| 79 |
<!-- SVG Filters for Liquid Glass Refraction -->
|
| 80 |
<svg width="0" height="0" style="position: absolute; pointer-events: none;">
|
src/ai/flux-client.js
CHANGED
|
@@ -1,58 +1,78 @@
|
|
| 1 |
-
import { Client } from "@gradio/client";
|
| 2 |
-
|
| 3 |
export class FluxClient {
|
| 4 |
constructor() {
|
| 5 |
-
this.
|
| 6 |
}
|
| 7 |
|
| 8 |
async fillImage(prompt, originalBase64, maskBase64, apiKey) {
|
| 9 |
if (!apiKey) throw new Error("Hugging Face API Token fehlt.");
|
| 10 |
|
|
|
|
| 11 |
const originalBlob = this.base64ToBlob(originalBase64, 'image/png');
|
| 12 |
const maskBlob = this.base64ToBlob(maskBase64, 'image/png');
|
| 13 |
-
|
| 14 |
-
// Für optimale Ergebnisse maskBlob invertieren, da mask meist weiß für inpaint ist,
|
| 15 |
-
// aber das hängt vom Tool ab. Wir lassen es so, wie es von chat-view kommt.
|
| 16 |
-
|
| 17 |
-
try {
|
| 18 |
-
const app = await Client.connect(this.spaceId, { hf_token: apiKey });
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
composite: null
|
| 25 |
-
};
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
prompt, // prompt
|
| 30 |
-
0, // seed
|
| 31 |
-
true, // randomize_seed
|
| 32 |
-
1024, // width
|
| 33 |
-
1024, // height
|
| 34 |
-
30, // guidance_scale
|
| 35 |
-
28 // num_inference_steps
|
| 36 |
-
]);
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
reader.onloadend = () => resolve(reader.result);
|
| 48 |
-
reader.onerror = reject;
|
| 49 |
-
reader.readAsDataURL(blob);
|
| 50 |
});
|
| 51 |
-
|
| 52 |
} catch (e) {
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
}
|
| 57 |
|
| 58 |
base64ToBlob(base64, mimeType) {
|
|
@@ -67,7 +87,7 @@ export class FluxClient {
|
|
| 67 |
const byteArray = new Uint8Array(byteNumbers);
|
| 68 |
byteArrays.push(byteArray);
|
| 69 |
}
|
| 70 |
-
return new Blob(byteArrays, {
|
| 71 |
}
|
| 72 |
}
|
| 73 |
|
|
|
|
|
|
|
|
|
|
| 1 |
export class FluxClient {
|
| 2 |
constructor() {
|
| 3 |
+
this.huggingFaceUrl = "https://router.huggingface.co/hf-inference/models/diffusers/stable-diffusion-xl-1.0-inpainting-0.1";
|
| 4 |
}
|
| 5 |
|
| 6 |
async fillImage(prompt, originalBase64, maskBase64, apiKey) {
|
| 7 |
if (!apiKey) throw new Error("Hugging Face API Token fehlt.");
|
| 8 |
|
| 9 |
+
// Convert base64 to blobs for multipart/form-data
|
| 10 |
const originalBlob = this.base64ToBlob(originalBase64, 'image/png');
|
| 11 |
const maskBlob = this.base64ToBlob(maskBase64, 'image/png');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
const formData = new FormData();
|
| 14 |
+
formData.append("inputs", prompt);
|
| 15 |
+
formData.append("image", originalBlob, "image.png");
|
| 16 |
+
formData.append("mask_image", maskBlob, "mask.png");
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
const controller = new AbortController();
|
| 19 |
+
const timeoutId = setTimeout(() => controller.abort(), 180000); // 3 Minuten Timeout
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
let response;
|
| 22 |
+
try {
|
| 23 |
+
response = await fetch(this.huggingFaceUrl, {
|
| 24 |
+
method: 'POST',
|
| 25 |
+
headers: {
|
| 26 |
+
'Authorization': `Bearer ${apiKey}`
|
| 27 |
+
},
|
| 28 |
+
body: formData,
|
| 29 |
+
signal: controller.signal
|
|
|
|
|
|
|
|
|
|
| 30 |
});
|
|
|
|
| 31 |
} catch (e) {
|
| 32 |
+
if (e.name === 'AbortError') {
|
| 33 |
+
throw new Error("Timeout: Der Hugging Face Server hat nach 3 Minuten nicht geantwortet.");
|
| 34 |
+
}
|
| 35 |
+
if (e.message.includes('Failed to fetch') || e.message.includes('NetworkError')) {
|
| 36 |
+
console.warn("Direct fetch failed (CORS/Network). Retrying via proxy...");
|
| 37 |
+
// Retry with a CORS proxy
|
| 38 |
+
try {
|
| 39 |
+
const proxyUrl = "https://corsproxy.io/?" + encodeURIComponent(this.huggingFaceUrl);
|
| 40 |
+
response = await fetch(proxyUrl, {
|
| 41 |
+
method: 'POST',
|
| 42 |
+
headers: {
|
| 43 |
+
'Authorization': `Bearer ${apiKey}`
|
| 44 |
+
},
|
| 45 |
+
body: formData,
|
| 46 |
+
signal: controller.signal
|
| 47 |
+
});
|
| 48 |
+
} catch(proxyErr) {
|
| 49 |
+
throw new Error("Netzwerkfehler (Failed to fetch). Hugging Face blockiert die Anfrage (CORS) oder das Modell ist überlastet. " + proxyErr.message);
|
| 50 |
+
}
|
| 51 |
+
} else {
|
| 52 |
+
throw e;
|
| 53 |
+
}
|
| 54 |
+
} finally {
|
| 55 |
+
clearTimeout(timeoutId);
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
if (!response.ok) {
|
| 59 |
+
const errText = await response.text();
|
| 60 |
+
let msg = errText;
|
| 61 |
+
try {
|
| 62 |
+
const json = JSON.parse(errText);
|
| 63 |
+
if (json.error) msg = json.error;
|
| 64 |
+
if (json.estimated_time) msg += ` (Geschätzte Wartezeit: ${Math.round(json.estimated_time)}s)`;
|
| 65 |
+
} catch(e) {}
|
| 66 |
+
throw new Error(`FLUX.1-Fill API Error ${response.status}: ${msg}`);
|
| 67 |
}
|
| 68 |
+
|
| 69 |
+
const blob = await response.blob();
|
| 70 |
+
return new Promise((resolve, reject) => {
|
| 71 |
+
const reader = new FileReader();
|
| 72 |
+
reader.onloadend = () => resolve(reader.result); // Returns data:image/jpeg;base64,...
|
| 73 |
+
reader.onerror = reject;
|
| 74 |
+
reader.readAsDataURL(blob);
|
| 75 |
+
});
|
| 76 |
}
|
| 77 |
|
| 78 |
base64ToBlob(base64, mimeType) {
|
|
|
|
| 87 |
const byteArray = new Uint8Array(byteNumbers);
|
| 88 |
byteArrays.push(byteArray);
|
| 89 |
}
|
| 90 |
+
return new Blob(byteArrays, {type: mimeType});
|
| 91 |
}
|
| 92 |
}
|
| 93 |
|
src/views/chat/chat-view.js
CHANGED
|
@@ -4525,49 +4525,105 @@ body { margin: 0; padding: 0; background: #000; overflow: hidden; display: flex;
|
|
| 4525 |
const width = sourceImage.width;
|
| 4526 |
const height = sourceImage.height;
|
| 4527 |
|
| 4528 |
-
//
|
| 4529 |
-
|
| 4530 |
-
|
| 4531 |
-
|
| 4532 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4533 |
|
| 4534 |
-
|
| 4535 |
-
|
| 4536 |
-
|
|
|
|
| 4537 |
|
| 4538 |
-
//
|
| 4539 |
-
|
| 4540 |
-
|
|
|
|
|
|
|
| 4541 |
|
| 4542 |
jsonData.forEach(item => {
|
| 4543 |
-
|
| 4544 |
-
|
| 4545 |
-
|
| 4546 |
-
|
| 4547 |
-
|
| 4548 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4549 |
});
|
| 4550 |
|
| 4551 |
-
|
| 4552 |
-
|
| 4553 |
|
| 4554 |
-
|
| 4555 |
-
|
| 4556 |
-
|
| 4557 |
-
|
| 4558 |
-
|
| 4559 |
-
|
| 4560 |
-
},
|
| 4561 |
-
|
| 4562 |
-
const resultBase64 = await fluxClient.fillImage(promptText, lastUserBase64, maskBase64, hfKey);
|
| 4563 |
-
clearTimeout(timerId);
|
| 4564 |
-
img.src = resultBase64;
|
| 4565 |
-
} catch(apiErr) {
|
| 4566 |
-
console.error("FLUX API Error:", apiErr);
|
| 4567 |
-
throw new Error(apiErr.message);
|
| 4568 |
-
}
|
| 4569 |
-
} else {
|
| 4570 |
-
throw new Error("Hugging Face API Key fehlt in den Einstellungen!");
|
| 4571 |
}
|
| 4572 |
|
| 4573 |
} catch (e) {
|
|
|
|
| 4525 |
const width = sourceImage.width;
|
| 4526 |
const height = sourceImage.height;
|
| 4527 |
|
| 4528 |
+
// --- ADVANCED CANVAS RENDERING ENGINE ---
|
| 4529 |
+
// Dynamically load a beautiful handwriting font
|
| 4530 |
+
if (!document.getElementById('font-kalam')) {
|
| 4531 |
+
const link = document.createElement('link');
|
| 4532 |
+
link.id = 'font-kalam';
|
| 4533 |
+
link.href = 'https://fonts.googleapis.com/css2?family=Kalam:wght@400;700&display=swap';
|
| 4534 |
+
link.rel = 'stylesheet';
|
| 4535 |
+
document.head.appendChild(link);
|
| 4536 |
+
}
|
| 4537 |
+
|
| 4538 |
+
try {
|
| 4539 |
+
await document.fonts.load('16px "Kalam"');
|
| 4540 |
+
} catch(e) {
|
| 4541 |
+
console.warn("Font loading took too long, fallback may be used.");
|
| 4542 |
+
}
|
| 4543 |
|
| 4544 |
+
const finalCanvas = document.createElement('canvas');
|
| 4545 |
+
finalCanvas.width = width;
|
| 4546 |
+
finalCanvas.height = height;
|
| 4547 |
+
const fCtx = finalCanvas.getContext('2d');
|
| 4548 |
|
| 4549 |
+
// Draw original image
|
| 4550 |
+
fCtx.drawImage(sourceImage, 0, 0);
|
| 4551 |
+
|
| 4552 |
+
// Enable multiply blend mode to make ink look realistic on paper
|
| 4553 |
+
fCtx.globalCompositeOperation = 'multiply';
|
| 4554 |
|
| 4555 |
jsonData.forEach(item => {
|
| 4556 |
+
let x = (item.x / 1000) * width;
|
| 4557 |
+
let y = (item.y / 1000) * height;
|
| 4558 |
+
let w = (item.width / 1000) * width || 100;
|
| 4559 |
+
let h = (item.height / 1000) * height || 30;
|
| 4560 |
+
|
| 4561 |
+
// Slight padding
|
| 4562 |
+
x += w * 0.02;
|
| 4563 |
+
y += h * 0.05;
|
| 4564 |
+
w *= 0.96;
|
| 4565 |
+
h *= 0.90;
|
| 4566 |
+
|
| 4567 |
+
// Realistic ink color (dark slate blue)
|
| 4568 |
+
fCtx.fillStyle = '#1e293b';
|
| 4569 |
+
fCtx.textAlign = 'center';
|
| 4570 |
+
fCtx.textBaseline = 'middle';
|
| 4571 |
+
|
| 4572 |
+
// Calculate font size
|
| 4573 |
+
const isLongText = item.text.split(' ').length > 4;
|
| 4574 |
+
let fontSize = isLongText ? Math.max(14, Math.min(24, w / 15)) : Math.max(16, Math.min(28, h * 0.6));
|
| 4575 |
+
fCtx.font = `400 ${fontSize}px "Kalam", "Comic Sans MS", cursive`;
|
| 4576 |
+
|
| 4577 |
+
fCtx.save();
|
| 4578 |
+
// Slight random rotation for human feel (-0.8 to +0.8 degrees)
|
| 4579 |
+
const angle = (Math.random() - 0.5) * 1.6 * Math.PI / 180;
|
| 4580 |
+
fCtx.translate(x + w/2, y + h/2);
|
| 4581 |
+
fCtx.rotate(angle);
|
| 4582 |
+
fCtx.translate(-(x + w/2), -(y + h/2));
|
| 4583 |
+
|
| 4584 |
+
if (!isLongText) {
|
| 4585 |
+
// Short text: just center it
|
| 4586 |
+
fCtx.fillText(item.text, x + w/2, y + h/2);
|
| 4587 |
+
} else {
|
| 4588 |
+
// Long text: Word Wrapping
|
| 4589 |
+
const words = item.text.split(' ');
|
| 4590 |
+
let line = '';
|
| 4591 |
+
const lines = [];
|
| 4592 |
+
|
| 4593 |
+
for(let n = 0; n < words.length; n++) {
|
| 4594 |
+
const testLine = line + words[n] + ' ';
|
| 4595 |
+
const metrics = fCtx.measureText(testLine);
|
| 4596 |
+
if (metrics.width > w && n > 0) {
|
| 4597 |
+
lines.push(line);
|
| 4598 |
+
line = words[n] + ' ';
|
| 4599 |
+
} else {
|
| 4600 |
+
line = testLine;
|
| 4601 |
+
}
|
| 4602 |
+
}
|
| 4603 |
+
lines.push(line);
|
| 4604 |
+
|
| 4605 |
+
const lineHeight = fontSize * 1.4;
|
| 4606 |
+
const totalHeight = lines.length * lineHeight;
|
| 4607 |
+
let startY = y + (h - totalHeight) / 2 + (lineHeight / 2);
|
| 4608 |
+
|
| 4609 |
+
lines.forEach(l => {
|
| 4610 |
+
fCtx.fillText(l.trim(), x + w/2, startY);
|
| 4611 |
+
startY += lineHeight;
|
| 4612 |
+
});
|
| 4613 |
+
}
|
| 4614 |
+
fCtx.restore();
|
| 4615 |
});
|
| 4616 |
|
| 4617 |
+
// Reset blend mode just in case
|
| 4618 |
+
fCtx.globalCompositeOperation = 'source-over';
|
| 4619 |
|
| 4620 |
+
const resultBase64 = finalCanvas.toDataURL('image/jpeg', 0.92);
|
| 4621 |
+
img.src = resultBase64;
|
| 4622 |
+
|
| 4623 |
+
const loadingText = img.previousElementSibling;
|
| 4624 |
+
if (loadingText) {
|
| 4625 |
+
loadingText.innerHTML = "Handschrift generiert!";
|
| 4626 |
+
setTimeout(() => { loadingText.style.display = 'none'; }, 2000);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4627 |
}
|
| 4628 |
|
| 4629 |
} catch (e) {
|