Emalawi19 commited on
Commit
d5edfd7
·
verified ·
1 Parent(s): fd2801b

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +16 -14
server.js CHANGED
@@ -3,38 +3,41 @@ import http from 'http';
3
 
4
  // Configuration
5
  const PORT = 7860;
6
- // UPGRADED: Starcoder2-1b is a much more capable coding model
7
- const MODEL_NAME = 'onnx-community/starcoder2-3b';
8
-
9
  let generator;
10
 
11
  // Initialize the model on startup
12
  async function loadModel() {
13
- console.log("Loading upgraded coding model...");
14
- // We specify 'auto' for the device to let the server manage memory efficiently
15
- generator = await pipeline('text-generation', MODEL_NAME, { device: 'auto' });
16
  console.log("Model loaded successfully!");
17
  }
18
 
19
  const server = http.createServer(async (req, res) => {
 
20
  res.setHeader('Access-Control-Allow-Origin', '*');
21
  res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
22
  res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
23
 
 
24
  if (req.method === 'OPTIONS') {
25
  res.writeHead(200);
26
  return res.end();
27
  }
28
 
29
  res.setHeader('Content-Type', 'application/json');
 
 
30
  const pathname = req.url.split('?')[0];
31
 
 
32
  if (pathname === '/' && req.method === 'GET') {
33
  res.writeHead(200);
34
- res.end(JSON.stringify({ "status": "Backend running with Starcoder2-1b" }));
35
  return;
36
  }
37
 
 
38
  if (pathname === '/generate' && req.method === 'POST') {
39
  let body = '';
40
  req.on('data', chunk => { body += chunk.toString(); });
@@ -45,14 +48,11 @@ const server = http.createServer(async (req, res) => {
45
  res.writeHead(503);
46
  return res.end(JSON.stringify({ error: "Model is still loading..." }));
47
  }
48
-
49
- // UPGRADED: Increased tokens and tuned temperature for better code quality
50
  const output = await generator(prompt, {
51
- max_new_tokens: 250,
52
- temperature: 0.2,
53
- do_sample: true
54
  });
55
-
56
  res.writeHead(200);
57
  res.end(JSON.stringify({ result: output[0].generated_text }));
58
  } catch (err) {
@@ -63,10 +63,12 @@ const server = http.createServer(async (req, res) => {
63
  return;
64
  }
65
 
 
66
  res.writeHead(404);
67
- res.end(JSON.stringify({ error: "Not Found" }));
68
  });
69
 
 
70
  loadModel().then(() => {
71
  server.listen(PORT, '0.0.0.0', () => {
72
  console.log(`Server running at http://0.0.0.0:${PORT}`);
 
3
 
4
  // Configuration
5
  const PORT = 7860;
6
+ const MODEL_NAME = 'Xenova/codegen-2B-mono'; // ← Upgraded: 2B param model, much stronger code generation
 
 
7
  let generator;
8
 
9
  // Initialize the model on startup
10
  async function loadModel() {
11
+ console.log("Loading coding model...");
12
+ generator = await pipeline('text-generation', MODEL_NAME);
 
13
  console.log("Model loaded successfully!");
14
  }
15
 
16
  const server = http.createServer(async (req, res) => {
17
+ // 1. Add CORS headers so external websites can talk to this API
18
  res.setHeader('Access-Control-Allow-Origin', '*');
19
  res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
20
  res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
21
 
22
+ // Handle preflight requests for the API
23
  if (req.method === 'OPTIONS') {
24
  res.writeHead(200);
25
  return res.end();
26
  }
27
 
28
  res.setHeader('Content-Type', 'application/json');
29
+
30
+ // Clean the URL (Removes Hugging Face's ?__theme=light additions)
31
  const pathname = req.url.split('?')[0];
32
 
33
+ // 2. Your requested Status Check
34
  if (pathname === '/' && req.method === 'GET') {
35
  res.writeHead(200);
36
+ res.end(JSON.stringify({ "status": " Backend is running" }));
37
  return;
38
  }
39
 
40
+ // 3. Code Generation Endpoint
41
  if (pathname === '/generate' && req.method === 'POST') {
42
  let body = '';
43
  req.on('data', chunk => { body += chunk.toString(); });
 
48
  res.writeHead(503);
49
  return res.end(JSON.stringify({ error: "Model is still loading..." }));
50
  }
51
+ // Generate code based on the prompt
 
52
  const output = await generator(prompt, {
53
+ max_new_tokens: 100,
54
+ temperature: 0.7
 
55
  });
 
56
  res.writeHead(200);
57
  res.end(JSON.stringify({ result: output[0].generated_text }));
58
  } catch (err) {
 
63
  return;
64
  }
65
 
66
+ // Default 404
67
  res.writeHead(404);
68
+ res.end(JSON.stringify({ error: "Not Found", requested_path: pathname }));
69
  });
70
 
71
+ // Start everything
72
  loadModel().then(() => {
73
  server.listen(PORT, '0.0.0.0', () => {
74
  console.log(`Server running at http://0.0.0.0:${PORT}`);