up
Browse files- custom_ui.py +19 -5
custom_ui.py
CHANGED
|
@@ -32,12 +32,21 @@ _intro_img = _img_data_uri(f"assets/{INTRO_EMOTION}.png")
|
|
| 32 |
|
| 33 |
BLOCKS_JS = """
|
| 34 |
() => {
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
if (!p) return;
|
| 38 |
const bubble = p.getAttribute('data-bubble');
|
| 39 |
const img = p.getAttribute('data-img');
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
const overlay = document.getElementById('ari-overlay');
|
| 42 |
if (!overlay) return;
|
| 43 |
const bubbleEl = overlay.querySelector('#ari-bubble');
|
|
@@ -46,17 +55,22 @@ BLOCKS_JS = """
|
|
| 46 |
if (imgEl && img) imgEl.src = img;
|
| 47 |
}
|
| 48 |
|
| 49 |
-
// MutationObserver watches the hidden gr.HTML host for Gradio innerHTML rewrites
|
| 50 |
function installObserver() {
|
| 51 |
const host = document.getElementById('ari-payload-host');
|
| 52 |
if (!host) {
|
| 53 |
-
setTimeout(installObserver,
|
| 54 |
return;
|
| 55 |
}
|
|
|
|
|
|
|
| 56 |
const obs = new MutationObserver(() => applyPayload(host));
|
| 57 |
obs.observe(host, { childList: true, subtree: true, characterData: true });
|
| 58 |
|
| 59 |
-
//
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
applyPayload(host);
|
| 61 |
}
|
| 62 |
|
|
|
|
| 32 |
|
| 33 |
BLOCKS_JS = """
|
| 34 |
() => {
|
| 35 |
+
let _lastPayload = null;
|
| 36 |
+
|
| 37 |
+
function applyPayload(host) {
|
| 38 |
+
// Gradio wraps gr.HTML in a div[id=ari-payload-host]; the actual HTML
|
| 39 |
+
// content is inside it. We search the whole subtree for our data div.
|
| 40 |
+
const p = host.querySelector('[data-ari]');
|
| 41 |
if (!p) return;
|
| 42 |
const bubble = p.getAttribute('data-bubble');
|
| 43 |
const img = p.getAttribute('data-img');
|
| 44 |
|
| 45 |
+
// Skip if nothing changed (poller dedup)
|
| 46 |
+
const key = bubble + '||' + img;
|
| 47 |
+
if (key === _lastPayload) return;
|
| 48 |
+
_lastPayload = key;
|
| 49 |
+
|
| 50 |
const overlay = document.getElementById('ari-overlay');
|
| 51 |
if (!overlay) return;
|
| 52 |
const bubbleEl = overlay.querySelector('#ari-bubble');
|
|
|
|
| 55 |
if (imgEl && img) imgEl.src = img;
|
| 56 |
}
|
| 57 |
|
|
|
|
| 58 |
function installObserver() {
|
| 59 |
const host = document.getElementById('ari-payload-host');
|
| 60 |
if (!host) {
|
| 61 |
+
setTimeout(installObserver, 300);
|
| 62 |
return;
|
| 63 |
}
|
| 64 |
+
|
| 65 |
+
// MutationObserver: fires when Gradio rewrites innerHTML
|
| 66 |
const obs = new MutationObserver(() => applyPayload(host));
|
| 67 |
obs.observe(host, { childList: true, subtree: true, characterData: true });
|
| 68 |
|
| 69 |
+
// setInterval fallback: catches cases where MutationObserver
|
| 70 |
+
// fires before Gradio finishes writing all attributes
|
| 71 |
+
setInterval(() => applyPayload(host), 150);
|
| 72 |
+
|
| 73 |
+
// Fire once immediately in case content is already there
|
| 74 |
applyPayload(host);
|
| 75 |
}
|
| 76 |
|