Spaces:
Paused
Paused
Create server.js
Browse files
server.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { pipeline } from '@huggingface/transformers';
|
| 2 |
+
import http from 'http';
|
| 3 |
+
|
| 4 |
+
// Configuration
|
| 5 |
+
const PORT = 7860;
|
| 6 |
+
const MODEL_NAME = 'Xenova/codegen-350M-mono'; // Lightweight, powerful coding model
|
| 7 |
+
|
| 8 |
+
let generator;
|
| 9 |
+
|
| 10 |
+
// Initialize the model on startup
|
| 11 |
+
async function loadModel() {
|
| 12 |
+
console.log("Loading coding model...");
|
| 13 |
+
generator = await pipeline('text-generation', MODEL_NAME);
|
| 14 |
+
console.log("Model loaded successfully!");
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
const server = http.createServer(async (req, res) => {
|
| 18 |
+
// Set headers for JSON response
|
| 19 |
+
res.setHeader('Content-Type', 'application/json');
|
| 20 |
+
|
| 21 |
+
// 1. Your requested Status Check
|
| 22 |
+
if (req.url === '/' && req.method === 'GET') {
|
| 23 |
+
res.writeHead(200);
|
| 24 |
+
res.end(JSON.stringify({ "status": " Backend is running" }));
|
| 25 |
+
return;
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
// 2. Code Generation Endpoint
|
| 29 |
+
if (req.url === '/generate' && req.method === 'POST') {
|
| 30 |
+
let body = '';
|
| 31 |
+
req.on('data', chunk => { body += chunk.toString(); });
|
| 32 |
+
req.on('end', async () => {
|
| 33 |
+
try {
|
| 34 |
+
const { prompt } = JSON.parse(body);
|
| 35 |
+
if (!generator) {
|
| 36 |
+
res.writeHead(503);
|
| 37 |
+
return res.end(JSON.stringify({ error: "Model is still loading..." }));
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
// Generate code based on the prompt
|
| 41 |
+
const output = await generator(prompt, {
|
| 42 |
+
max_new_tokens: 100,
|
| 43 |
+
temperature: 0.7
|
| 44 |
+
});
|
| 45 |
+
|
| 46 |
+
res.writeHead(200);
|
| 47 |
+
res.end(JSON.stringify({ result: output[0].generated_text }));
|
| 48 |
+
} catch (err) {
|
| 49 |
+
res.writeHead(400);
|
| 50 |
+
res.end(JSON.stringify({ error: "Invalid JSON or request" }));
|
| 51 |
+
}
|
| 52 |
+
});
|
| 53 |
+
return;
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
// Default 404
|
| 57 |
+
res.writeHead(404);
|
| 58 |
+
res.end(JSON.stringify({ error: "Not Found" }));
|
| 59 |
+
});
|
| 60 |
+
|
| 61 |
+
// Start everything
|
| 62 |
+
loadModel().then(() => {
|
| 63 |
+
server.listen(PORT, '0.0.0.0', () => {
|
| 64 |
+
console.log(`Server running at http://0.0.0.0:${PORT}`);
|
| 65 |
+
});
|
| 66 |
+
});
|