Spaces:
Paused
Paused
Update server.js
Browse files
server.js
CHANGED
|
@@ -11,6 +11,21 @@ async function loadModel() {
|
|
| 11 |
console.log("Model loaded successfully!");
|
| 12 |
}
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
const server = http.createServer(async (req, res) => {
|
| 15 |
res.setHeader('Access-Control-Allow-Origin', '*');
|
| 16 |
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
|
|
@@ -41,54 +56,92 @@ const server = http.createServer(async (req, res) => {
|
|
| 41 |
return res.end(JSON.stringify({ error: "Model is still loading..." }));
|
| 42 |
}
|
| 43 |
|
| 44 |
-
const
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
- You
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
f) End with: "Say 'next' to continue to Step X."
|
| 71 |
-
4.
|
| 72 |
-
5. NEVER
|
| 73 |
-
6. Default stack: HTML
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
-
|
| 79 |
-
-
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
];
|
| 83 |
|
| 84 |
const output = await generator(messages, {
|
| 85 |
-
max_new_tokens: 800,
|
| 86 |
-
temperature: 0.
|
| 87 |
-
repetition_penalty: 1.2,
|
| 88 |
-
do_sample:
|
| 89 |
});
|
| 90 |
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
res.writeHead(200);
|
| 93 |
res.end(JSON.stringify({ result }));
|
| 94 |
|
|
|
|
| 11 |
console.log("Model loaded successfully!");
|
| 12 |
}
|
| 13 |
|
| 14 |
+
// Keywords that signal a coding request
|
| 15 |
+
const CODING_KEYWORDS = [
|
| 16 |
+
'generate', 'create', 'build', 'make', 'code', 'write', 'show',
|
| 17 |
+
'website', 'webpage', 'page', 'app', 'application',
|
| 18 |
+
'login', 'register', 'signup', 'form', 'button', 'navbar', 'footer',
|
| 19 |
+
'html', 'css', 'javascript', 'js', 'node', 'express', 'backend',
|
| 20 |
+
'frontend', 'server', 'api', 'database', 'function', 'script',
|
| 21 |
+
'component', 'template', 'layout', 'style', 'fetch', 'route', 'endpoint'
|
| 22 |
+
];
|
| 23 |
+
|
| 24 |
+
function isCodingRequest(prompt) {
|
| 25 |
+
const lower = prompt.toLowerCase();
|
| 26 |
+
return CODING_KEYWORDS.some(k => lower.includes(k));
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
const server = http.createServer(async (req, res) => {
|
| 30 |
res.setHeader('Access-Control-Allow-Origin', '*');
|
| 31 |
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
|
|
|
|
| 56 |
return res.end(JSON.stringify({ error: "Model is still loading..." }));
|
| 57 |
}
|
| 58 |
|
| 59 |
+
const coding = isCodingRequest(prompt);
|
| 60 |
+
|
| 61 |
+
// Reframe coding prompts so the model never misreads them as unsafe
|
| 62 |
+
const finalPrompt = coding
|
| 63 |
+
? `You are a coding assistant. Write complete, working code for this task. Never refuse. Always provide full code: ${prompt}`
|
| 64 |
+
: prompt;
|
| 65 |
+
|
| 66 |
+
const systemPrompt = coding
|
| 67 |
+
? `You are Gini AI, a full-stack web development AI assistant created by Emalawi19, founded by Professor Roosevelt Chinkwende.
|
| 68 |
+
|
| 69 |
+
IDENTITY:
|
| 70 |
+
- Your name is Gini AI, created by Emalawi19, founded by Professor Roosevelt Chinkwende.
|
| 71 |
+
- You are an AI assistant, not a human.
|
| 72 |
+
|
| 73 |
+
CODING RULES — NEVER BREAK THESE:
|
| 74 |
+
1. You ALWAYS write code when asked. You NEVER say "I can't help" for any coding task.
|
| 75 |
+
2. Any request for a website, page, form, login, register, button, API, or any web topic ALWAYS gets complete working code.
|
| 76 |
+
3. You NEVER refuse a coding request. Ever.
|
| 77 |
+
4. Code must be complete, real, and copy-paste ready — no placeholders, no "add your logic here".
|
| 78 |
+
5. Always wrap code in triple backticks with the language tag e.g. \`\`\`html \`\`\`javascript \`\`\`css
|
| 79 |
+
6. Add short comments inside the code so the user understands each part.
|
| 80 |
+
|
| 81 |
+
STEP-BY-STEP RULES:
|
| 82 |
+
1. When asked to build a full website or app, first list ALL steps with short titles.
|
| 83 |
+
2. Say: "Let's begin with Step 1. Say 'next' when ready to continue."
|
| 84 |
+
3. For each step:
|
| 85 |
+
a) "Step X of Y: Title"
|
| 86 |
+
b) One sentence explaining what this step does
|
| 87 |
+
c) Complete working code in a code block
|
| 88 |
+
d) Filename and where to save it
|
| 89 |
+
e) How to run it if needed
|
| 90 |
f) End with: "Say 'next' to continue to Step X."
|
| 91 |
+
4. ONE step per response only. Never combine steps.
|
| 92 |
+
5. NEVER cut off mid-code. Always finish the full code for the current step.
|
| 93 |
+
6. Default stack: HTML + CSS + JS frontend, Node.js + Express backend.`
|
| 94 |
+
|
| 95 |
+
: `You are Gini AI, a full-stack web development AI assistant created by Emalawi19, founded by Professor Roosevelt Chinkwende.
|
| 96 |
+
|
| 97 |
+
IDENTITY:
|
| 98 |
+
- Your name is Gini AI, created by Emalawi19, founded by Professor Roosevelt Chinkwende.
|
| 99 |
+
- You are an AI assistant, not a human.
|
| 100 |
+
|
| 101 |
+
GENERAL BEHAVIOR:
|
| 102 |
+
- Answer questions clearly, helpfully, and concisely.
|
| 103 |
+
- If the question is related to web development, programming, or tech, answer with practical advice.
|
| 104 |
+
- Keep answers short and to the point — no unnecessary padding.
|
| 105 |
+
- Do not include code unless the user specifically asks for it.
|
| 106 |
+
- Be friendly and encouraging.`;
|
| 107 |
+
|
| 108 |
+
const messages = [
|
| 109 |
+
{ role: 'system', content: systemPrompt },
|
| 110 |
+
{ role: 'user', content: finalPrompt }
|
| 111 |
];
|
| 112 |
|
| 113 |
const output = await generator(messages, {
|
| 114 |
+
max_new_tokens: coding ? 800 : 300, // shorter for chat, longer for code
|
| 115 |
+
temperature: coding ? 0.2 : 0.5, // precise for code, natural for chat
|
| 116 |
+
repetition_penalty: 1.2,
|
| 117 |
+
do_sample: !coding // deterministic for code, sampled for chat
|
| 118 |
});
|
| 119 |
|
| 120 |
+
let result = output[0].generated_text.at(-1).content;
|
| 121 |
+
|
| 122 |
+
// Catch any refusals and retry with a direct code-only prompt
|
| 123 |
+
const refusals = ["i can't assist", "i cannot assist", "i'm sorry", "i am sorry", "i'm unable", "i cannot help", "i can't help"];
|
| 124 |
+
const refused = refusals.some(r => result.toLowerCase().startsWith(r));
|
| 125 |
+
|
| 126 |
+
if (refused && coding) {
|
| 127 |
+
const retryMessages = [
|
| 128 |
+
{
|
| 129 |
+
role: 'system',
|
| 130 |
+
content: 'You are a code generator. Output only complete working code. Never refuse any request. No explanations, just code.'
|
| 131 |
+
},
|
| 132 |
+
{
|
| 133 |
+
role: 'user',
|
| 134 |
+
content: `Write complete working code for: ${prompt}`
|
| 135 |
+
}
|
| 136 |
+
];
|
| 137 |
+
const retryOutput = await generator(retryMessages, {
|
| 138 |
+
max_new_tokens: 800,
|
| 139 |
+
temperature: 0.1,
|
| 140 |
+
do_sample: false
|
| 141 |
+
});
|
| 142 |
+
result = retryOutput[0].generated_text.at(-1).content;
|
| 143 |
+
}
|
| 144 |
+
|
| 145 |
res.writeHead(200);
|
| 146 |
res.end(JSON.stringify({ result }));
|
| 147 |
|