Spaces:
Sleeping
Sleeping
Update static/script.js
Browse files- static/script.js +50 -0
static/script.js
CHANGED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// Remove the hidden class
|
2 |
+
function revealContent() {
|
3 |
+
const loader = document.getElementById('loader');
|
4 |
+
const textGenContainer = document.getElementById('text-gen-container');
|
5 |
+
loader.classList.remove('hidden');
|
6 |
+
textGenContainer.classList.remove('hidden');
|
7 |
+
}
|
8 |
+
|
9 |
+
// Call the function once the DOM is loaded
|
10 |
+
document.addEventListener('DOMContentLoaded', () => {
|
11 |
+
// Connect to the endpoint and parse JSON response
|
12 |
+
const initConnection = async () => {
|
13 |
+
try {
|
14 |
+
// Fetch the endpoint
|
15 |
+
const connectionResult = await fetch('/infer_t5');
|
16 |
+
|
17 |
+
// Parse the JSON response
|
18 |
+
if (!connectionResult.ok) {
|
19 |
+
throw Error(await connectionResult.text());
|
20 |
+
}
|
21 |
+
|
22 |
+
const json = await connectionResult.json();
|
23 |
+
console.log(json);
|
24 |
+
} catch (err) {
|
25 |
+
// Display the err object and wait for 2 sec before retrying
|
26 |
+
console.error(err);
|
27 |
+
setTimeout(() => initConnection(), 2000);
|
28 |
+
return;
|
29 |
+
} finally {
|
30 |
+
// Clear timeouts and intervals
|
31 |
+
clearTimeout(timeoutId);
|
32 |
+
clearInterval(intervalId);
|
33 |
+
}
|
34 |
+
|
35 |
+
// Connection established, remove loader animation
|
36 |
+
revealContent();
|
37 |
+
};
|
38 |
+
|
39 |
+
// Timeout logic for waiting for endpoint availability
|
40 |
+
let timeoutId, intervalId;
|
41 |
+
timeoutId = setTimeout(() => {
|
42 |
+
intervalId = setInterval(() => {
|
43 |
+
console.warn('Endpoint timed out, retrying...');
|
44 |
+
initConnection();
|
45 |
+
}, 2000);
|
46 |
+
}, 5000);
|
47 |
+
|
48 |
+
// Attempt to connect to the endpoint
|
49 |
+
initConnection();
|
50 |
+
});
|