Spaces:
Paused
Paused
Update server.js
Browse files
server.js
CHANGED
|
@@ -3,42 +3,38 @@ import http from 'http';
|
|
| 3 |
|
| 4 |
// Configuration
|
| 5 |
const PORT = 7860;
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
let generator;
|
| 9 |
|
| 10 |
// Initialize the model on startup
|
| 11 |
async function loadModel() {
|
| 12 |
-
console.log("Loading coding model...");
|
| 13 |
-
|
|
|
|
| 14 |
console.log("Model loaded successfully!");
|
| 15 |
}
|
| 16 |
|
| 17 |
const server = http.createServer(async (req, res) => {
|
| 18 |
-
// 1. Add CORS headers so external websites can talk to this API
|
| 19 |
res.setHeader('Access-Control-Allow-Origin', '*');
|
| 20 |
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
|
| 21 |
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
|
| 22 |
|
| 23 |
-
// Handle preflight requests for the API
|
| 24 |
if (req.method === 'OPTIONS') {
|
| 25 |
res.writeHead(200);
|
| 26 |
return res.end();
|
| 27 |
}
|
| 28 |
|
| 29 |
res.setHeader('Content-Type', 'application/json');
|
| 30 |
-
|
| 31 |
-
// Clean the URL (Removes Hugging Face's ?__theme=light additions)
|
| 32 |
const pathname = req.url.split('?')[0];
|
| 33 |
|
| 34 |
-
// 2. Your requested Status Check
|
| 35 |
if (pathname === '/' && req.method === 'GET') {
|
| 36 |
res.writeHead(200);
|
| 37 |
-
res.end(JSON.stringify({ "status": "
|
| 38 |
return;
|
| 39 |
}
|
| 40 |
|
| 41 |
-
// 3. Code Generation Endpoint
|
| 42 |
if (pathname === '/generate' && req.method === 'POST') {
|
| 43 |
let body = '';
|
| 44 |
req.on('data', chunk => { body += chunk.toString(); });
|
|
@@ -50,10 +46,11 @@ const server = http.createServer(async (req, res) => {
|
|
| 50 |
return res.end(JSON.stringify({ error: "Model is still loading..." }));
|
| 51 |
}
|
| 52 |
|
| 53 |
-
//
|
| 54 |
const output = await generator(prompt, {
|
| 55 |
-
max_new_tokens:
|
| 56 |
-
temperature: 0.
|
|
|
|
| 57 |
});
|
| 58 |
|
| 59 |
res.writeHead(200);
|
|
@@ -66,12 +63,10 @@ const server = http.createServer(async (req, res) => {
|
|
| 66 |
return;
|
| 67 |
}
|
| 68 |
|
| 69 |
-
// Default 404
|
| 70 |
res.writeHead(404);
|
| 71 |
-
res.end(JSON.stringify({ error: "Not Found"
|
| 72 |
});
|
| 73 |
|
| 74 |
-
// Start everything
|
| 75 |
loadModel().then(() => {
|
| 76 |
server.listen(PORT, '0.0.0.0', () => {
|
| 77 |
console.log(`Server running at http://0.0.0.0:${PORT}`);
|
|
|
|
| 3 |
|
| 4 |
// Configuration
|
| 5 |
const PORT = 7860;
|
| 6 |
+
// UPGRADED: Starcoder2-1b is a much more capable coding model
|
| 7 |
+
const MODEL_NAME = 'Xenova/starcoder2-1b';
|
| 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(); });
|
|
|
|
| 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);
|
|
|
|
| 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}`);
|