Spaces:
Running
Running
Update server.js
Browse files
server.js
CHANGED
@@ -12,6 +12,85 @@ import fetch from 'node-fetch';
|
|
12 |
|
13 |
dotenv.config();
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
const { Pool } = pg;
|
16 |
|
17 |
const app = express();
|
@@ -110,7 +189,7 @@ async function checkPromptCensorship(prompt) {
|
|
110 |
return false;
|
111 |
} catch (e) {
|
112 |
console.error('Gemini censorship API error:', e.message);
|
113 |
-
// fallback: treat as not censored if
|
114 |
return false;
|
115 |
}
|
116 |
}
|
|
|
12 |
|
13 |
dotenv.config();
|
14 |
|
15 |
+
// --- Hugging Face Space Auto-Restarter ---
|
16 |
+
const HF_TOKEN = process.env.reset;
|
17 |
+
const SPACE_ID = process.env.HF_SPACE_ID || 'shashwatIDR/vision';
|
18 |
+
const HF_API_BASE = 'https://huggingface.co/api';
|
19 |
+
|
20 |
+
async function restartSpace() {
|
21 |
+
if (!HF_TOKEN) {
|
22 |
+
console.error('[β No HF_TOKEN] Hugging Face token not set. Skipping restart.');
|
23 |
+
return;
|
24 |
+
}
|
25 |
+
console.log(`[π Attempting restart] ${SPACE_ID}`);
|
26 |
+
try {
|
27 |
+
const response = await axios.post(`${HF_API_BASE}/spaces/${SPACE_ID}/restart`, {}, {
|
28 |
+
headers: {
|
29 |
+
Authorization: `Bearer ${HF_TOKEN}`,
|
30 |
+
'Content-Type': 'application/json'
|
31 |
+
},
|
32 |
+
timeout: 30000
|
33 |
+
});
|
34 |
+
console.log(`[β
Restarted] ${SPACE_ID} at ${new Date().toLocaleString()}`);
|
35 |
+
if (response.data) {
|
36 |
+
console.log('[π Response]', response.data);
|
37 |
+
}
|
38 |
+
} catch (err) {
|
39 |
+
if (err.code === 'ENOTFOUND') {
|
40 |
+
console.error('[β DNS Error] Cannot resolve huggingface.co - check your internet connection');
|
41 |
+
} else if (err.code === 'ETIMEDOUT') {
|
42 |
+
console.error('[β Timeout] Request timed out - try again later');
|
43 |
+
} else if (err.response) {
|
44 |
+
console.error(`[β HTTP ${err.response.status}]`, err.response.data || err.response.statusText);
|
45 |
+
if (err.response.status === 401) {
|
46 |
+
console.error('[π Auth Error] Check your Hugging Face token');
|
47 |
+
} else if (err.response.status === 404) {
|
48 |
+
console.error('[π Not Found] Space may not exist or token lacks permissions');
|
49 |
+
}
|
50 |
+
} else {
|
51 |
+
console.error('[β Failed to Restart]', err.message);
|
52 |
+
}
|
53 |
+
}
|
54 |
+
}
|
55 |
+
|
56 |
+
async function testHFAPI() {
|
57 |
+
if (!HF_TOKEN) {
|
58 |
+
console.error('[β No HF_TOKEN] Hugging Face token not set. Skipping API test.');
|
59 |
+
return false;
|
60 |
+
}
|
61 |
+
try {
|
62 |
+
console.log('[π Testing Hugging Face API endpoint...]');
|
63 |
+
const response = await axios.get(`${HF_API_BASE}/spaces`, {
|
64 |
+
headers: { Authorization: `Bearer ${HF_TOKEN}` },
|
65 |
+
timeout: 10000,
|
66 |
+
params: { limit: 1 }
|
67 |
+
});
|
68 |
+
console.log('[β
API Test] Endpoint is working');
|
69 |
+
return true;
|
70 |
+
} catch (err) {
|
71 |
+
console.error('[β API Test Failed]', err.response?.status || err.code || err.message);
|
72 |
+
return false;
|
73 |
+
}
|
74 |
+
}
|
75 |
+
|
76 |
+
// On server startup, test API and schedule restarts
|
77 |
+
(async () => {
|
78 |
+
if (!HF_TOKEN) {
|
79 |
+
console.error('[β No HF_TOKEN] Hugging Face token not set. Skipping auto-restart scheduling.');
|
80 |
+
return;
|
81 |
+
}
|
82 |
+
console.log('[π Starting] HuggingFace Space Auto-Restarter');
|
83 |
+
const apiWorking = await testHFAPI();
|
84 |
+
if (!apiWorking) {
|
85 |
+
console.log('[β οΈ Warning] API test failed, but proceeding anyway...');
|
86 |
+
}
|
87 |
+
// Immediately restart once on startup
|
88 |
+
restartSpace();
|
89 |
+
// Then restart every 5 minutes
|
90 |
+
setInterval(restartSpace, 5 * 60 * 1000);
|
91 |
+
console.log('[β° Scheduled] Space will restart every 5 minutes');
|
92 |
+
})();
|
93 |
+
|
94 |
const { Pool } = pg;
|
95 |
|
96 |
const app = express();
|
|
|
189 |
return false;
|
190 |
} catch (e) {
|
191 |
console.error('Gemini censorship API error:', e.message);
|
192 |
+
// fallback: treat as not censored if error
|
193 |
return false;
|
194 |
}
|
195 |
}
|