Spaces:
Paused
Paused
Update server.js
Browse files
server.js
CHANGED
|
@@ -11,6 +11,47 @@ async function loadModel() {
|
|
| 11 |
console.log("Model loaded successfully!");
|
| 12 |
}
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
const CODING_PHRASES = [
|
| 15 |
'generate code', 'write code', 'create code',
|
| 16 |
'generate a website', 'build a website', 'create a website', 'make a website',
|
|
@@ -47,7 +88,6 @@ function isCodingRequest(prompt) {
|
|
| 47 |
return CODING_PHRASES.some(phrase => lower.includes(phrase));
|
| 48 |
}
|
| 49 |
|
| 50 |
-
// Detect if the prompt is asking for a full HTML website/page
|
| 51 |
function isWebsiteRequest(prompt) {
|
| 52 |
const lower = prompt.toLowerCase();
|
| 53 |
return ['website', 'webpage', 'web page', 'html page', 'login page',
|
|
@@ -55,34 +95,7 @@ function isWebsiteRequest(prompt) {
|
|
| 55 |
'homepage', 'home page', 'portfolio', 'dashboard'].some(w => lower.includes(w));
|
| 56 |
}
|
| 57 |
|
| 58 |
-
// Ensure HTML output starts with <!DOCTYPE html> or <html> and ends with </html>
|
| 59 |
-
function enforceHTMLStructure(text) {
|
| 60 |
-
// Extract code from inside ```html ... ``` block if present
|
| 61 |
-
const codeBlockMatch = text.match(/```html\s*([\s\S]*?)```/i);
|
| 62 |
-
if (codeBlockMatch) {
|
| 63 |
-
let html = codeBlockMatch[1].trim();
|
| 64 |
-
html = cleanHTML(html);
|
| 65 |
-
return '```html\n' + html + '\n```';
|
| 66 |
-
}
|
| 67 |
-
|
| 68 |
-
// If raw HTML is present (starts with < somewhere), extract and clean it
|
| 69 |
-
const htmlStart = text.search(/<(!DOCTYPE|html)/i);
|
| 70 |
-
if (htmlStart !== -1) {
|
| 71 |
-
let html = text.slice(htmlStart);
|
| 72 |
-
// Cut off anything after </html>
|
| 73 |
-
const htmlEnd = html.search(/<\/html>/i);
|
| 74 |
-
if (htmlEnd !== -1) {
|
| 75 |
-
html = html.slice(0, htmlEnd + 7); // include </html>
|
| 76 |
-
}
|
| 77 |
-
html = cleanHTML(html);
|
| 78 |
-
return '```html\n' + html + '\n```';
|
| 79 |
-
}
|
| 80 |
-
|
| 81 |
-
return text;
|
| 82 |
-
}
|
| 83 |
-
|
| 84 |
function cleanHTML(html) {
|
| 85 |
-
// Ensure it starts with <!DOCTYPE html>
|
| 86 |
if (!html.trimStart().toLowerCase().startsWith('<!doctype')) {
|
| 87 |
if (html.trimStart().toLowerCase().startsWith('<html')) {
|
| 88 |
html = '<!DOCTYPE html>\n' + html.trimStart();
|
|
@@ -90,20 +103,32 @@ function cleanHTML(html) {
|
|
| 90 |
html = '<!DOCTYPE html>\n<html lang="en">\n' + html.trimStart();
|
| 91 |
}
|
| 92 |
}
|
| 93 |
-
|
| 94 |
-
// Ensure it ends with </html>
|
| 95 |
if (!html.trimEnd().toLowerCase().endsWith('</html>')) {
|
| 96 |
-
// Check if </body> is present, add </html> after it
|
| 97 |
if (html.toLowerCase().includes('</body>')) {
|
| 98 |
html = html.replace(/<\/body>\s*$/i, '</body>\n</html>');
|
| 99 |
} else {
|
| 100 |
html = html.trimEnd() + '\n</body>\n</html>';
|
| 101 |
}
|
| 102 |
}
|
| 103 |
-
|
| 104 |
return html.trim();
|
| 105 |
}
|
| 106 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
const server = http.createServer(async (req, res) => {
|
| 108 |
res.setHeader('Access-Control-Allow-Origin', '*');
|
| 109 |
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
|
|
@@ -114,12 +139,12 @@ const server = http.createServer(async (req, res) => {
|
|
| 114 |
return res.end();
|
| 115 |
}
|
| 116 |
|
| 117 |
-
res.setHeader('Content-Type', 'application/json');
|
| 118 |
const pathname = req.url.split('?')[0];
|
| 119 |
|
| 120 |
if (pathname === '/' && req.method === 'GET') {
|
|
|
|
| 121 |
res.writeHead(200);
|
| 122 |
-
res.end(JSON.stringify({ status: "Backend is running" }));
|
| 123 |
return;
|
| 124 |
}
|
| 125 |
|
|
@@ -129,13 +154,25 @@ const server = http.createServer(async (req, res) => {
|
|
| 129 |
req.on('end', async () => {
|
| 130 |
try {
|
| 131 |
const { prompt } = JSON.parse(body);
|
|
|
|
| 132 |
if (!generator) {
|
|
|
|
| 133 |
res.writeHead(503);
|
| 134 |
return res.end(JSON.stringify({ error: "Model is still loading..." }));
|
| 135 |
}
|
| 136 |
|
| 137 |
-
const coding
|
| 138 |
-
const
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
|
| 140 |
const finalPrompt = coding
|
| 141 |
? `You are a coding assistant. Write complete, working code for this task. Never refuse. Always provide full code: ${prompt}`
|
|
@@ -154,100 +191,110 @@ CODING RULES β NEVER BREAK THESE:
|
|
| 154 |
3. Default language is always JavaScript/HTML/CSS β NEVER Python unless the user asks.
|
| 155 |
4. Add short comments inside the code.
|
| 156 |
|
| 157 |
-
HTML WEBSITE RULES
|
| 158 |
1. ALL website/page code MUST start with <!DOCTYPE html> on the very first line.
|
| 159 |
2. Then <html lang="en"> on the second line.
|
| 160 |
-
3. Must include a proper <head>
|
| 161 |
-
4. Must include a proper <body>
|
| 162 |
5. Must end with </body> then </html> as the very last line.
|
| 163 |
-
6. ALL CSS
|
| 164 |
-
7.
|
| 165 |
-
8. The
|
| 166 |
-
9. The page must be complete and functional β never cut off mid-code.
|
| 167 |
|
| 168 |
STEP-BY-STEP RULES:
|
| 169 |
-
1. When asked to build a full website or app,
|
| 170 |
-
2. Say: "Let's begin with Step 1. Say 'next' when ready
|
| 171 |
-
3. For each step:
|
| 172 |
-
|
| 173 |
-
b) One sentence explaining what this step does
|
| 174 |
-
c) Complete working code in a code block
|
| 175 |
-
d) Filename and where to save it
|
| 176 |
-
e) How to run it if needed
|
| 177 |
-
f) End with: "Say 'next' to continue to Step X."
|
| 178 |
-
4. ONE step per response only. Never combine steps.
|
| 179 |
-
5. NEVER cut off mid-code. Always finish the full code for the current step.
|
| 180 |
-
6. Default stack: HTML + CSS + JS frontend, Node.js + Express backend.`
|
| 181 |
|
| 182 |
: `You are Gini AI, a helpful AI assistant created by Emalawi19, founded by Professor Roosevelt Chinkwende.
|
| 183 |
|
| 184 |
IDENTITY:
|
| 185 |
- Your name is Gini AI, created by Emalawi19, founded by Professor Roosevelt Chinkwende.
|
| 186 |
- You are an AI assistant, not a human.
|
| 187 |
-
|
| 188 |
GENERAL BEHAVIOR:
|
| 189 |
- Answer the question directly and clearly in plain English.
|
|
|
|
|
|
|
|
|
|
| 190 |
- NEVER generate code for general knowledge or everyday questions.
|
| 191 |
-
-
|
| 192 |
-
- If someone asks "what is the function of X" or "explain X" β answer in plain sentences, not code.
|
| 193 |
-
- Keep answers concise, friendly, and easy to understand.
|
| 194 |
-
- Only mention web development if the question is specifically about it.`;
|
| 195 |
|
| 196 |
const messages = [
|
| 197 |
{ role: 'system', content: systemPrompt },
|
| 198 |
-
{ role: 'user',
|
| 199 |
];
|
| 200 |
|
| 201 |
-
|
| 202 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
temperature: coding ? 0.2 : 0.5,
|
| 204 |
repetition_penalty: 1.2,
|
| 205 |
-
do_sample: !coding
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
});
|
| 207 |
|
| 208 |
-
let result = output[0].generated_text.at(-1).content;
|
| 209 |
-
|
| 210 |
// Catch refusals and retry
|
| 211 |
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"];
|
| 212 |
-
const refused
|
| 213 |
|
| 214 |
if (refused && coding) {
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
{
|
| 221 |
-
role: 'user',
|
| 222 |
-
content: `Write complete working HTML code starting with <!DOCTYPE html> and ending with </html> for: ${prompt}`
|
| 223 |
-
}
|
| 224 |
-
];
|
| 225 |
-
const retryOutput = await generator(retryMessages, {
|
| 226 |
max_new_tokens: 800,
|
| 227 |
temperature: 0.1,
|
| 228 |
-
do_sample: false
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
});
|
| 230 |
-
result = retryOutput[0].generated_text.at(-1).content;
|
| 231 |
}
|
| 232 |
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
result = enforceHTMLStructure(result);
|
| 236 |
}
|
| 237 |
|
| 238 |
-
|
| 239 |
-
|
|
|
|
|
|
|
| 240 |
|
| 241 |
} catch (err) {
|
| 242 |
-
res.
|
| 243 |
-
res.end(
|
| 244 |
}
|
| 245 |
});
|
| 246 |
return;
|
| 247 |
}
|
| 248 |
|
|
|
|
| 249 |
res.writeHead(404);
|
| 250 |
-
res.end(JSON.stringify({ error: "Not Found"
|
| 251 |
});
|
| 252 |
|
| 253 |
loadModel().then(() => {
|
|
|
|
| 11 |
console.log("Model loaded successfully!");
|
| 12 |
}
|
| 13 |
|
| 14 |
+
// ββ WIKIPEDIA SEARCH βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 15 |
+
// Step 1: Search Wikipedia for the best matching article title
|
| 16 |
+
async function searchWikipedia(query) {
|
| 17 |
+
try {
|
| 18 |
+
const url = `https://en.wikipedia.org/w/api.php?action=search&list=search&srsearch=${encodeURIComponent(query)}&srlimit=1&format=json&origin=*`;
|
| 19 |
+
const res = await fetch(url);
|
| 20 |
+
const data = await res.json();
|
| 21 |
+
const results = data?.query?.search;
|
| 22 |
+
if (!results || results.length === 0) return null;
|
| 23 |
+
return results[0].title; // Return best matching article title
|
| 24 |
+
} catch {
|
| 25 |
+
return null;
|
| 26 |
+
}
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
// Step 2: Fetch the article summary (first 600 chars) using the title
|
| 30 |
+
async function fetchWikipediaSummary(title) {
|
| 31 |
+
try {
|
| 32 |
+
const url = `https://en.wikipedia.org/api/rest_v1/page/summary/${encodeURIComponent(title)}`;
|
| 33 |
+
const res = await fetch(url);
|
| 34 |
+
const data = await res.json();
|
| 35 |
+
if (data.extract) {
|
| 36 |
+
// Trim to 600 chars so it fits the model's context without overloading it
|
| 37 |
+
return data.extract.slice(0, 600);
|
| 38 |
+
}
|
| 39 |
+
return null;
|
| 40 |
+
} catch {
|
| 41 |
+
return null;
|
| 42 |
+
}
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
// Step 3: Combine search + fetch into one call
|
| 46 |
+
async function getWikipediaContext(prompt) {
|
| 47 |
+
const title = await searchWikipedia(prompt);
|
| 48 |
+
if (!title) return null;
|
| 49 |
+
const summary = await fetchWikipediaSummary(title);
|
| 50 |
+
if (!summary) return null;
|
| 51 |
+
return { title, summary };
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
// ββ CODING KEYWORD DETECTION βββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 55 |
const CODING_PHRASES = [
|
| 56 |
'generate code', 'write code', 'create code',
|
| 57 |
'generate a website', 'build a website', 'create a website', 'make a website',
|
|
|
|
| 88 |
return CODING_PHRASES.some(phrase => lower.includes(phrase));
|
| 89 |
}
|
| 90 |
|
|
|
|
| 91 |
function isWebsiteRequest(prompt) {
|
| 92 |
const lower = prompt.toLowerCase();
|
| 93 |
return ['website', 'webpage', 'web page', 'html page', 'login page',
|
|
|
|
| 95 |
'homepage', 'home page', 'portfolio', 'dashboard'].some(w => lower.includes(w));
|
| 96 |
}
|
| 97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
function cleanHTML(html) {
|
|
|
|
| 99 |
if (!html.trimStart().toLowerCase().startsWith('<!doctype')) {
|
| 100 |
if (html.trimStart().toLowerCase().startsWith('<html')) {
|
| 101 |
html = '<!DOCTYPE html>\n' + html.trimStart();
|
|
|
|
| 103 |
html = '<!DOCTYPE html>\n<html lang="en">\n' + html.trimStart();
|
| 104 |
}
|
| 105 |
}
|
|
|
|
|
|
|
| 106 |
if (!html.trimEnd().toLowerCase().endsWith('</html>')) {
|
|
|
|
| 107 |
if (html.toLowerCase().includes('</body>')) {
|
| 108 |
html = html.replace(/<\/body>\s*$/i, '</body>\n</html>');
|
| 109 |
} else {
|
| 110 |
html = html.trimEnd() + '\n</body>\n</html>';
|
| 111 |
}
|
| 112 |
}
|
|
|
|
| 113 |
return html.trim();
|
| 114 |
}
|
| 115 |
|
| 116 |
+
function enforceHTMLStructure(text) {
|
| 117 |
+
const codeBlockMatch = text.match(/```html\s*([\s\S]*?)```/i);
|
| 118 |
+
if (codeBlockMatch) {
|
| 119 |
+
return '```html\n' + cleanHTML(codeBlockMatch[1].trim()) + '\n```';
|
| 120 |
+
}
|
| 121 |
+
const htmlStart = text.search(/<(!DOCTYPE|html)/i);
|
| 122 |
+
if (htmlStart !== -1) {
|
| 123 |
+
let html = text.slice(htmlStart);
|
| 124 |
+
const htmlEnd = html.search(/<\/html>/i);
|
| 125 |
+
if (htmlEnd !== -1) html = html.slice(0, htmlEnd + 7);
|
| 126 |
+
return '```html\n' + cleanHTML(html) + '\n```';
|
| 127 |
+
}
|
| 128 |
+
return text;
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
+
// ββ HTTP SERVER βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 132 |
const server = http.createServer(async (req, res) => {
|
| 133 |
res.setHeader('Access-Control-Allow-Origin', '*');
|
| 134 |
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
|
|
|
|
| 139 |
return res.end();
|
| 140 |
}
|
| 141 |
|
|
|
|
| 142 |
const pathname = req.url.split('?')[0];
|
| 143 |
|
| 144 |
if (pathname === '/' && req.method === 'GET') {
|
| 145 |
+
res.setHeader('Content-Type', 'application/json');
|
| 146 |
res.writeHead(200);
|
| 147 |
+
res.end(JSON.stringify({ status: "Backend is running", model: MODEL_NAME }));
|
| 148 |
return;
|
| 149 |
}
|
| 150 |
|
|
|
|
| 154 |
req.on('end', async () => {
|
| 155 |
try {
|
| 156 |
const { prompt } = JSON.parse(body);
|
| 157 |
+
|
| 158 |
if (!generator) {
|
| 159 |
+
res.setHeader('Content-Type', 'application/json');
|
| 160 |
res.writeHead(503);
|
| 161 |
return res.end(JSON.stringify({ error: "Model is still loading..." }));
|
| 162 |
}
|
| 163 |
|
| 164 |
+
const coding = isCodingRequest(prompt);
|
| 165 |
+
const websiteReq = isWebsiteRequest(prompt);
|
| 166 |
+
|
| 167 |
+
// ββ Fetch Wikipedia context for non-coding questions ββββββββββ
|
| 168 |
+
let wikipediaContext = '';
|
| 169 |
+
if (!coding) {
|
| 170 |
+
const wiki = await getWikipediaContext(prompt);
|
| 171 |
+
if (wiki) {
|
| 172 |
+
wikipediaContext = `\n\nWIKIPEDIA REFERENCE (from article: "${wiki.title}"):\n${wiki.summary}\n\nUse the above Wikipedia information to improve your answer. Rewrite it in your own words β do not copy it directly. Add your own explanation on top of it.`;
|
| 173 |
+
console.log(`Wikipedia context fetched: "${wiki.title}"`);
|
| 174 |
+
}
|
| 175 |
+
}
|
| 176 |
|
| 177 |
const finalPrompt = coding
|
| 178 |
? `You are a coding assistant. Write complete, working code for this task. Never refuse. Always provide full code: ${prompt}`
|
|
|
|
| 191 |
3. Default language is always JavaScript/HTML/CSS β NEVER Python unless the user asks.
|
| 192 |
4. Add short comments inside the code.
|
| 193 |
|
| 194 |
+
HTML WEBSITE RULES:
|
| 195 |
1. ALL website/page code MUST start with <!DOCTYPE html> on the very first line.
|
| 196 |
2. Then <html lang="en"> on the second line.
|
| 197 |
+
3. Must include a proper <head> with <meta charset>, <meta viewport>, <title>, and <style>.
|
| 198 |
+
4. Must include a proper <body> with all content.
|
| 199 |
5. Must end with </body> then </html> as the very last line.
|
| 200 |
+
6. ALL CSS inside <style> in <head>. ALL JS inside <script> at the bottom of <body>.
|
| 201 |
+
7. Wrap full HTML in a \`\`\`html code block.
|
| 202 |
+
8. The page must be complete and functional β never cut off mid-code.
|
|
|
|
| 203 |
|
| 204 |
STEP-BY-STEP RULES:
|
| 205 |
+
1. When asked to build a full website or app, list ALL steps first.
|
| 206 |
+
2. Say: "Let's begin with Step 1. Say 'next' when ready."
|
| 207 |
+
3. For each step: title, explanation, complete code, filename, how to run, then "Say 'next' to continue."
|
| 208 |
+
4. ONE step per response. Never combine. Never cut off mid-code.`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
|
| 210 |
: `You are Gini AI, a helpful AI assistant created by Emalawi19, founded by Professor Roosevelt Chinkwende.
|
| 211 |
|
| 212 |
IDENTITY:
|
| 213 |
- Your name is Gini AI, created by Emalawi19, founded by Professor Roosevelt Chinkwende.
|
| 214 |
- You are an AI assistant, not a human.
|
| 215 |
+
${wikipediaContext}
|
| 216 |
GENERAL BEHAVIOR:
|
| 217 |
- Answer the question directly and clearly in plain English.
|
| 218 |
+
- If Wikipedia reference data is provided above, use it to make your answer accurate and informative.
|
| 219 |
+
- Rewrite the Wikipedia information in your own words β never copy it word for word.
|
| 220 |
+
- Add your own helpful explanation beyond just what Wikipedia says.
|
| 221 |
- NEVER generate code for general knowledge or everyday questions.
|
| 222 |
+
- Keep answers concise, friendly, and easy to understand.`;
|
|
|
|
|
|
|
|
|
|
| 223 |
|
| 224 |
const messages = [
|
| 225 |
{ role: 'system', content: systemPrompt },
|
| 226 |
+
{ role: 'user', content: finalPrompt }
|
| 227 |
];
|
| 228 |
|
| 229 |
+
// ββ SSE streaming response ββββββββββββββββββββββββββββββββββββ
|
| 230 |
+
res.writeHead(200, {
|
| 231 |
+
'Content-Type': 'text/event-stream',
|
| 232 |
+
'Cache-Control': 'no-cache',
|
| 233 |
+
'Connection': 'keep-alive',
|
| 234 |
+
'X-Accel-Buffering': 'no'
|
| 235 |
+
});
|
| 236 |
+
|
| 237 |
+
let fullResult = '';
|
| 238 |
+
|
| 239 |
+
await generator(messages, {
|
| 240 |
+
max_new_tokens: coding ? 800 : 300,
|
| 241 |
temperature: coding ? 0.2 : 0.5,
|
| 242 |
repetition_penalty: 1.2,
|
| 243 |
+
do_sample: !coding,
|
| 244 |
+
callback_function: (beams) => {
|
| 245 |
+
const newText = beams[0]?.generated_text ?? '';
|
| 246 |
+
if (newText.length > fullResult.length) {
|
| 247 |
+
const chunk = newText.slice(fullResult.length);
|
| 248 |
+
fullResult = newText;
|
| 249 |
+
res.write(`data: ${JSON.stringify({ chunk })}\n\n`);
|
| 250 |
+
}
|
| 251 |
+
}
|
| 252 |
});
|
| 253 |
|
|
|
|
|
|
|
| 254 |
// Catch refusals and retry
|
| 255 |
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"];
|
| 256 |
+
const refused = refusals.some(r => fullResult.toLowerCase().startsWith(r));
|
| 257 |
|
| 258 |
if (refused && coding) {
|
| 259 |
+
fullResult = '';
|
| 260 |
+
await generator([
|
| 261 |
+
{ role: 'system', content: 'You are a code generator. Output only complete working code. Never refuse.' },
|
| 262 |
+
{ role: 'user', content: `Write complete working HTML code starting with <!DOCTYPE html> and ending with </html> for: ${prompt}` }
|
| 263 |
+
], {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 264 |
max_new_tokens: 800,
|
| 265 |
temperature: 0.1,
|
| 266 |
+
do_sample: false,
|
| 267 |
+
callback_function: (beams) => {
|
| 268 |
+
const newText = beams[0]?.generated_text ?? '';
|
| 269 |
+
if (newText.length > fullResult.length) {
|
| 270 |
+
const chunk = newText.slice(fullResult.length);
|
| 271 |
+
fullResult = newText;
|
| 272 |
+
res.write(`data: ${JSON.stringify({ chunk })}\n\n`);
|
| 273 |
+
}
|
| 274 |
+
}
|
| 275 |
});
|
|
|
|
| 276 |
}
|
| 277 |
|
| 278 |
+
if (websiteReq && coding) {
|
| 279 |
+
fullResult = enforceHTMLStructure(fullResult);
|
|
|
|
| 280 |
}
|
| 281 |
|
| 282 |
+
// Send final done event with source info
|
| 283 |
+
const wikiSource = (!coding && wikipediaContext) ? 'π Wikipedia' : null;
|
| 284 |
+
res.write(`data: ${JSON.stringify({ done: true, result: fullResult, source: wikiSource })}\n\n`);
|
| 285 |
+
res.end();
|
| 286 |
|
| 287 |
} catch (err) {
|
| 288 |
+
res.write(`data: ${JSON.stringify({ error: err.message })}\n\n`);
|
| 289 |
+
res.end();
|
| 290 |
}
|
| 291 |
});
|
| 292 |
return;
|
| 293 |
}
|
| 294 |
|
| 295 |
+
res.setHeader('Content-Type', 'application/json');
|
| 296 |
res.writeHead(404);
|
| 297 |
+
res.end(JSON.stringify({ error: "Not Found" }));
|
| 298 |
});
|
| 299 |
|
| 300 |
loadModel().then(() => {
|