LJTSG commited on
Commit
fb5f1e5
·
verified ·
1 Parent(s): 1b3eff6

Add serve.js

Browse files
Files changed (1) hide show
  1. serve.js +69 -0
serve.js ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const http = require('http'), fs = require('fs'), path = require('path');
2
+ const PORT = 8170;
3
+ const ROOT = __dirname;
4
+ const MODEL_DIR = path.join(ROOT, 'model_splits');
5
+
6
+ const MIME = {
7
+ '.html': 'text/html', '.js': 'text/javascript', '.mjs': 'text/javascript',
8
+ '.json': 'application/json', '.gguf': 'application/octet-stream',
9
+ '.wasm': 'application/wasm', '.css': 'text/css',
10
+ };
11
+
12
+ http.createServer((req, res) => {
13
+ if (req.method === 'OPTIONS') {
14
+ res.writeHead(200, {
15
+ 'Access-Control-Allow-Origin': '*',
16
+ 'Access-Control-Allow-Methods': 'GET,HEAD',
17
+ 'Cross-Origin-Embedder-Policy': 'require-corp',
18
+ 'Cross-Origin-Opener-Policy': 'same-origin',
19
+ });
20
+ return res.end();
21
+ }
22
+
23
+ let p = decodeURIComponent(req.url.split('?')[0]);
24
+ if (p === '/') p = '/index.html';
25
+
26
+ let fp;
27
+ if (p.startsWith('/model/')) {
28
+ fp = path.join(MODEL_DIR, p.slice('/model/'.length));
29
+ } else {
30
+ fp = path.resolve(ROOT, '.' + p);
31
+ }
32
+
33
+ fs.stat(fp, (e, st) => {
34
+ if (e) { res.writeHead(404); return res.end('not found: ' + p); }
35
+ const total = st.size;
36
+ const range = req.headers.range;
37
+ const ct = MIME[path.extname(fp)] || 'application/octet-stream';
38
+
39
+ const headers = {
40
+ 'Content-Type': ct, 'Accept-Ranges': 'bytes',
41
+ 'Access-Control-Allow-Origin': '*',
42
+ 'Cross-Origin-Embedder-Policy': 'require-corp',
43
+ 'Cross-Origin-Opener-Policy': 'same-origin',
44
+ };
45
+
46
+ if (range) {
47
+ const m = range.match(/bytes=(\d+)-(\d*)/);
48
+ if (m) {
49
+ const start = parseInt(m[1]);
50
+ const end = m[2] ? parseInt(m[2]) : total - 1;
51
+ headers['Content-Range'] = `bytes ${start}-${end}/${total}`;
52
+ headers['Content-Length'] = end - start + 1;
53
+ res.writeHead(206, headers);
54
+ return fs.createReadStream(fp, { start, end }).pipe(res);
55
+ }
56
+ }
57
+
58
+ headers['Content-Length'] = total;
59
+ res.writeHead(200, headers);
60
+ fs.createReadStream(fp).pipe(res);
61
+ });
62
+ }).listen(PORT, () => {
63
+ console.log(`EXAONE-Deep WebGPU on http://localhost:${PORT}`);
64
+ console.log(`Model splits: ${MODEL_DIR}`);
65
+ try {
66
+ const files = fs.readdirSync(MODEL_DIR).filter(f => f.endsWith('.gguf'));
67
+ console.log(`Split files: ${files.length}`);
68
+ } catch(e) { console.log('No model_splits dir yet'); }
69
+ });