Upload script.js with huggingface_hub
Browse files
script.js
ADDED
@@ -0,0 +1,163 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
function gradioApp() {
|
2 |
+
const elems = document.getElementsByTagName('gradio-app');
|
3 |
+
const elem = elems.length == 0 ? document : elems[0];
|
4 |
+
|
5 |
+
if (elem !== document) {
|
6 |
+
elem.getElementById = function(id) {
|
7 |
+
return document.getElementById(id);
|
8 |
+
};
|
9 |
+
}
|
10 |
+
return elem.shadowRoot ? elem.shadowRoot : elem;
|
11 |
+
}
|
12 |
+
|
13 |
+
/**
|
14 |
+
* Get the currently selected top-level UI tab button (e.g. the button that says "Extras").
|
15 |
+
*/
|
16 |
+
function get_uiCurrentTab() {
|
17 |
+
return gradioApp().querySelector('#tabs > .tab-nav > button.selected');
|
18 |
+
}
|
19 |
+
|
20 |
+
/**
|
21 |
+
* Get the first currently visible top-level UI tab content (e.g. the div hosting the "txt2img" UI).
|
22 |
+
*/
|
23 |
+
function get_uiCurrentTabContent() {
|
24 |
+
return gradioApp().querySelector('#tabs > .tabitem[id^=tab_]:not([style*="display: none"])');
|
25 |
+
}
|
26 |
+
|
27 |
+
var uiUpdateCallbacks = [];
|
28 |
+
var uiAfterUpdateCallbacks = [];
|
29 |
+
var uiLoadedCallbacks = [];
|
30 |
+
var uiTabChangeCallbacks = [];
|
31 |
+
var optionsChangedCallbacks = [];
|
32 |
+
var uiAfterUpdateTimeout = null;
|
33 |
+
var uiCurrentTab = null;
|
34 |
+
|
35 |
+
/**
|
36 |
+
* Register callback to be called at each UI update.
|
37 |
+
* The callback receives an array of MutationRecords as an argument.
|
38 |
+
*/
|
39 |
+
function onUiUpdate(callback) {
|
40 |
+
uiUpdateCallbacks.push(callback);
|
41 |
+
}
|
42 |
+
|
43 |
+
/**
|
44 |
+
* Register callback to be called soon after UI updates.
|
45 |
+
* The callback receives no arguments.
|
46 |
+
*
|
47 |
+
* This is preferred over `onUiUpdate` if you don't need
|
48 |
+
* access to the MutationRecords, as your function will
|
49 |
+
* not be called quite as often.
|
50 |
+
*/
|
51 |
+
function onAfterUiUpdate(callback) {
|
52 |
+
uiAfterUpdateCallbacks.push(callback);
|
53 |
+
}
|
54 |
+
|
55 |
+
/**
|
56 |
+
* Register callback to be called when the UI is loaded.
|
57 |
+
* The callback receives no arguments.
|
58 |
+
*/
|
59 |
+
function onUiLoaded(callback) {
|
60 |
+
uiLoadedCallbacks.push(callback);
|
61 |
+
}
|
62 |
+
|
63 |
+
/**
|
64 |
+
* Register callback to be called when the UI tab is changed.
|
65 |
+
* The callback receives no arguments.
|
66 |
+
*/
|
67 |
+
function onUiTabChange(callback) {
|
68 |
+
uiTabChangeCallbacks.push(callback);
|
69 |
+
}
|
70 |
+
|
71 |
+
/**
|
72 |
+
* Register callback to be called when the options are changed.
|
73 |
+
* The callback receives no arguments.
|
74 |
+
* @param callback
|
75 |
+
*/
|
76 |
+
function onOptionsChanged(callback) {
|
77 |
+
optionsChangedCallbacks.push(callback);
|
78 |
+
}
|
79 |
+
|
80 |
+
function executeCallbacks(queue, arg) {
|
81 |
+
for (const callback of queue) {
|
82 |
+
try {
|
83 |
+
callback(arg);
|
84 |
+
} catch (e) {
|
85 |
+
console.error("error running callback", callback, ":", e);
|
86 |
+
}
|
87 |
+
}
|
88 |
+
}
|
89 |
+
|
90 |
+
/**
|
91 |
+
* Schedule the execution of the callbacks registered with onAfterUiUpdate.
|
92 |
+
* The callbacks are executed after a short while, unless another call to this function
|
93 |
+
* is made before that time. IOW, the callbacks are executed only once, even
|
94 |
+
* when there are multiple mutations observed.
|
95 |
+
*/
|
96 |
+
function scheduleAfterUiUpdateCallbacks() {
|
97 |
+
clearTimeout(uiAfterUpdateTimeout);
|
98 |
+
uiAfterUpdateTimeout = setTimeout(function() {
|
99 |
+
executeCallbacks(uiAfterUpdateCallbacks);
|
100 |
+
}, 200);
|
101 |
+
}
|
102 |
+
|
103 |
+
var executedOnLoaded = false;
|
104 |
+
|
105 |
+
document.addEventListener("DOMContentLoaded", function() {
|
106 |
+
var mutationObserver = new MutationObserver(function(m) {
|
107 |
+
if (!executedOnLoaded && gradioApp().querySelector('#txt2img_prompt')) {
|
108 |
+
executedOnLoaded = true;
|
109 |
+
executeCallbacks(uiLoadedCallbacks);
|
110 |
+
}
|
111 |
+
|
112 |
+
executeCallbacks(uiUpdateCallbacks, m);
|
113 |
+
scheduleAfterUiUpdateCallbacks();
|
114 |
+
const newTab = get_uiCurrentTab();
|
115 |
+
if (newTab && (newTab !== uiCurrentTab)) {
|
116 |
+
uiCurrentTab = newTab;
|
117 |
+
executeCallbacks(uiTabChangeCallbacks);
|
118 |
+
}
|
119 |
+
});
|
120 |
+
mutationObserver.observe(gradioApp(), {childList: true, subtree: true});
|
121 |
+
});
|
122 |
+
|
123 |
+
/**
|
124 |
+
* Add a ctrl+enter as a shortcut to start a generation
|
125 |
+
*/
|
126 |
+
document.addEventListener('keydown', function(e) {
|
127 |
+
var handled = false;
|
128 |
+
if (e.key !== undefined) {
|
129 |
+
if ((e.key == "Enter" && (e.metaKey || e.ctrlKey || e.altKey))) handled = true;
|
130 |
+
} else if (e.keyCode !== undefined) {
|
131 |
+
if ((e.keyCode == 13 && (e.metaKey || e.ctrlKey || e.altKey))) handled = true;
|
132 |
+
}
|
133 |
+
if (handled) {
|
134 |
+
var button = get_uiCurrentTabContent().querySelector('button[id$=_generate]');
|
135 |
+
if (button) {
|
136 |
+
button.click();
|
137 |
+
}
|
138 |
+
e.preventDefault();
|
139 |
+
}
|
140 |
+
});
|
141 |
+
|
142 |
+
/**
|
143 |
+
* checks that a UI element is not in another hidden element or tab content
|
144 |
+
*/
|
145 |
+
function uiElementIsVisible(el) {
|
146 |
+
if (el === document) {
|
147 |
+
return true;
|
148 |
+
}
|
149 |
+
|
150 |
+
const computedStyle = getComputedStyle(el);
|
151 |
+
const isVisible = computedStyle.display !== 'none';
|
152 |
+
|
153 |
+
if (!isVisible) return false;
|
154 |
+
return uiElementIsVisible(el.parentNode);
|
155 |
+
}
|
156 |
+
|
157 |
+
function uiElementInSight(el) {
|
158 |
+
const clRect = el.getBoundingClientRect();
|
159 |
+
const windowHeight = window.innerHeight;
|
160 |
+
const isOnScreen = clRect.bottom > 0 && clRect.top < windowHeight;
|
161 |
+
|
162 |
+
return isOnScreen;
|
163 |
+
}
|