Spaces:
Paused
Paused
Update server.js
Browse files
server.js
CHANGED
|
@@ -3,7 +3,6 @@ import http from 'http';
|
|
| 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 |
|
|
@@ -15,12 +14,10 @@ async function loadModel() {
|
|
| 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();
|
|
@@ -28,17 +25,14 @@ const server = http.createServer(async (req, res) => {
|
|
| 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": " Backend is running" }));
|
| 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,19 +44,50 @@ const server = http.createServer(async (req, res) => {
|
|
| 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 |
-
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
{ role: 'user', content: prompt }
|
| 57 |
];
|
| 58 |
|
| 59 |
const output = await generator(messages, {
|
| 60 |
-
max_new_tokens:
|
| 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 }));
|
|
@@ -74,7 +99,6 @@ const server = http.createServer(async (req, res) => {
|
|
| 74 |
return;
|
| 75 |
}
|
| 76 |
|
| 77 |
-
// Default 404
|
| 78 |
res.writeHead(404);
|
| 79 |
res.end(JSON.stringify({ error: "Not Found", requested_path: pathname }));
|
| 80 |
});
|
|
|
|
| 3 |
|
| 4 |
// Configuration
|
| 5 |
const PORT = 7860;
|
|
|
|
| 6 |
const MODEL_NAME = 'onnx-community/Qwen2.5-0.5B-Instruct';
|
| 7 |
let generator;
|
| 8 |
|
|
|
|
| 14 |
}
|
| 15 |
|
| 16 |
const server = http.createServer(async (req, res) => {
|
|
|
|
| 17 |
res.setHeader('Access-Control-Allow-Origin', '*');
|
| 18 |
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
|
| 19 |
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
|
| 20 |
|
|
|
|
| 21 |
if (req.method === 'OPTIONS') {
|
| 22 |
res.writeHead(200);
|
| 23 |
return res.end();
|
|
|
|
| 25 |
|
| 26 |
res.setHeader('Content-Type', 'application/json');
|
| 27 |
|
|
|
|
| 28 |
const pathname = req.url.split('?')[0];
|
| 29 |
|
|
|
|
| 30 |
if (pathname === '/' && req.method === 'GET') {
|
| 31 |
res.writeHead(200);
|
| 32 |
res.end(JSON.stringify({ "status": " Backend is running" }));
|
| 33 |
return;
|
| 34 |
}
|
| 35 |
|
|
|
|
| 36 |
if (pathname === '/generate' && req.method === 'POST') {
|
| 37 |
let body = '';
|
| 38 |
req.on('data', chunk => { body += chunk.toString(); });
|
|
|
|
| 44 |
return res.end(JSON.stringify({ error: "Model is still loading..." }));
|
| 45 |
}
|
| 46 |
|
|
|
|
| 47 |
const messages = [
|
| 48 |
+
{
|
| 49 |
+
role: 'system',
|
| 50 |
+
content: `You are Gini AI, an expert full-stack web development assistant created by Emalawi19.
|
| 51 |
+
|
| 52 |
+
IDENTITY:
|
| 53 |
+
- Your name is Gini AI.
|
| 54 |
+
- You were created by the company Emalawi19.
|
| 55 |
+
- The founder of Emalawi19 is Professor Roosevelt Chinkwende.
|
| 56 |
+
- You are an AI model, not a human.
|
| 57 |
+
- If asked who made you, who your founder is, or about your company, answer using the above facts.
|
| 58 |
+
|
| 59 |
+
YOUR SPECIALIZATION:
|
| 60 |
+
- You build complete websites — both frontend (HTML, CSS, JavaScript) and backend (Node.js, Express.js, REST APIs, databases).
|
| 61 |
+
- You guide users step by step through building a full website, one step at a time.
|
| 62 |
+
- You never dump all the code at once. You always go one step at a time, waiting for the user to confirm before moving to the next step.
|
| 63 |
+
|
| 64 |
+
HOW YOU GUIDE USERS (CRITICAL — always follow this):
|
| 65 |
+
1. When a user asks you to build or create a website or web app, first give a brief overview of all the steps you will walk them through (e.g. Step 1: Project Setup, Step 2: Backend, Step 3: Frontend, etc.).
|
| 66 |
+
2. Then say: "Let's start with Step 1. Ready? Just say 'next' or 'continue' when you're ready to proceed."
|
| 67 |
+
3. For each step, provide:
|
| 68 |
+
- A clear title (e.g. "Step 2 of 6: Setting up the Express backend")
|
| 69 |
+
- A short explanation of what this step does and why
|
| 70 |
+
- The complete, working code for that step only
|
| 71 |
+
- Clear instructions on where to save the file and how to run it
|
| 72 |
+
- End every step with: "When you're done with this step, say 'next' to continue to Step X."
|
| 73 |
+
4. Never skip steps. Never combine steps. One step at a time.
|
| 74 |
+
5. Code must always be complete and copy-paste ready — no placeholders like "add your code here".
|
| 75 |
+
6. Default stack: HTML/CSS/JS for frontend, Node.js + Express for backend, unless the user requests otherwise.
|
| 76 |
+
|
| 77 |
+
GENERAL BEHAVIOR:
|
| 78 |
+
- For non-coding questions, give concise helpful answers but always steer back to building something.
|
| 79 |
+
- Always be encouraging and clear — assume the user may be a beginner.
|
| 80 |
+
- Never refer to yourself as a human or a developer. You are Gini AI, an AI assistant.`
|
| 81 |
+
},
|
| 82 |
{ role: 'user', content: prompt }
|
| 83 |
];
|
| 84 |
|
| 85 |
const output = await generator(messages, {
|
| 86 |
+
max_new_tokens: 512,
|
| 87 |
temperature: 0.7,
|
| 88 |
do_sample: true
|
| 89 |
});
|
| 90 |
|
|
|
|
| 91 |
const result = output[0].generated_text.at(-1).content;
|
| 92 |
res.writeHead(200);
|
| 93 |
res.end(JSON.stringify({ result }));
|
|
|
|
| 99 |
return;
|
| 100 |
}
|
| 101 |
|
|
|
|
| 102 |
res.writeHead(404);
|
| 103 |
res.end(JSON.stringify({ error: "Not Found", requested_path: pathname }));
|
| 104 |
});
|