Spaces:
Paused
Paused
Update server.js
Browse files
server.js
CHANGED
|
@@ -3,15 +3,14 @@ import http from 'http';
|
|
| 3 |
|
| 4 |
// Configuration
|
| 5 |
const PORT = 7860;
|
| 6 |
-
|
|
|
|
| 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 |
-
model_file_name: 'decoder_model_merged' // ← Required for codegen models in Transformers.js v3
|
| 14 |
-
});
|
| 15 |
console.log("Model loaded successfully!");
|
| 16 |
}
|
| 17 |
|
|
@@ -50,16 +49,26 @@ const server = http.createServer(async (req, res) => {
|
|
| 50 |
res.writeHead(503);
|
| 51 |
return res.end(JSON.stringify({ error: "Model is still loading..." }));
|
| 52 |
}
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
});
|
|
|
|
|
|
|
|
|
|
| 58 |
res.writeHead(200);
|
| 59 |
-
res.end(JSON.stringify({ result
|
| 60 |
} catch (err) {
|
| 61 |
res.writeHead(400);
|
| 62 |
-
res.end(JSON.stringify({ error: "Invalid JSON or request" }));
|
| 63 |
}
|
| 64 |
});
|
| 65 |
return;
|
|
|
|
| 3 |
|
| 4 |
// Configuration
|
| 5 |
const PORT = 7860;
|
| 6 |
+
// Instruction-following model: understands "generate a login page" and responds correctly
|
| 7 |
+
const MODEL_NAME = 'onnx-community/Qwen2.5-0.5B-Instruct';
|
| 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, { dtype: 'q4' });
|
|
|
|
|
|
|
| 14 |
console.log("Model loaded successfully!");
|
| 15 |
}
|
| 16 |
|
|
|
|
| 49 |
res.writeHead(503);
|
| 50 |
return res.end(JSON.stringify({ error: "Model is still loading..." }));
|
| 51 |
}
|
| 52 |
+
|
| 53 |
+
// Use chat format so the model understands instructions properly
|
| 54 |
+
const messages = [
|
| 55 |
+
{ role: 'system', content: 'You are an expert JavaScript and web developer. When asked to generate code, respond with clean, complete, working code only. No explanations unless asked.' },
|
| 56 |
+
{ role: 'user', content: prompt }
|
| 57 |
+
];
|
| 58 |
+
|
| 59 |
+
const output = await generator(messages, {
|
| 60 |
+
max_new_tokens: 512, // More tokens for complete code output
|
| 61 |
+
temperature: 0.7,
|
| 62 |
+
do_sample: true
|
| 63 |
});
|
| 64 |
+
|
| 65 |
+
// Extract only the assistant's reply
|
| 66 |
+
const result = output[0].generated_text.at(-1).content;
|
| 67 |
res.writeHead(200);
|
| 68 |
+
res.end(JSON.stringify({ result }));
|
| 69 |
} catch (err) {
|
| 70 |
res.writeHead(400);
|
| 71 |
+
res.end(JSON.stringify({ error: "Invalid JSON or request", detail: err.message }));
|
| 72 |
}
|
| 73 |
});
|
| 74 |
return;
|