LJTSG's picture
Add serve.js
fb5f1e5 verified
const http = require('http'), fs = require('fs'), path = require('path');
const PORT = 8170;
const ROOT = __dirname;
const MODEL_DIR = path.join(ROOT, 'model_splits');
const MIME = {
'.html': 'text/html', '.js': 'text/javascript', '.mjs': 'text/javascript',
'.json': 'application/json', '.gguf': 'application/octet-stream',
'.wasm': 'application/wasm', '.css': 'text/css',
};
http.createServer((req, res) => {
if (req.method === 'OPTIONS') {
res.writeHead(200, {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET,HEAD',
'Cross-Origin-Embedder-Policy': 'require-corp',
'Cross-Origin-Opener-Policy': 'same-origin',
});
return res.end();
}
let p = decodeURIComponent(req.url.split('?')[0]);
if (p === '/') p = '/index.html';
let fp;
if (p.startsWith('/model/')) {
fp = path.join(MODEL_DIR, p.slice('/model/'.length));
} else {
fp = path.resolve(ROOT, '.' + p);
}
fs.stat(fp, (e, st) => {
if (e) { res.writeHead(404); return res.end('not found: ' + p); }
const total = st.size;
const range = req.headers.range;
const ct = MIME[path.extname(fp)] || 'application/octet-stream';
const headers = {
'Content-Type': ct, 'Accept-Ranges': 'bytes',
'Access-Control-Allow-Origin': '*',
'Cross-Origin-Embedder-Policy': 'require-corp',
'Cross-Origin-Opener-Policy': 'same-origin',
};
if (range) {
const m = range.match(/bytes=(\d+)-(\d*)/);
if (m) {
const start = parseInt(m[1]);
const end = m[2] ? parseInt(m[2]) : total - 1;
headers['Content-Range'] = `bytes ${start}-${end}/${total}`;
headers['Content-Length'] = end - start + 1;
res.writeHead(206, headers);
return fs.createReadStream(fp, { start, end }).pipe(res);
}
}
headers['Content-Length'] = total;
res.writeHead(200, headers);
fs.createReadStream(fp).pipe(res);
});
}).listen(PORT, () => {
console.log(`EXAONE-Deep WebGPU on http://localhost:${PORT}`);
console.log(`Model splits: ${MODEL_DIR}`);
try {
const files = fs.readdirSync(MODEL_DIR).filter(f => f.endsWith('.gguf'));
console.log(`Split files: ${files.length}`);
} catch(e) { console.log('No model_splits dir yet'); }
});