File size: 2,474 Bytes
123428d
 
 
 
 
1b87cfa
 
123428d
 
 
 
 
1b87cfa
 
 
123428d
 
 
 
0b19b33
 
 
 
 
 
 
 
 
123428d
0b19b33
 
 
123428d
1b87cfa
123428d
 
 
0b19b33
123428d
 
 
 
 
 
 
 
 
 
1b87cfa
123428d
1b87cfa
 
 
123428d
 
 
 
 
 
 
 
 
 
 
 
 
1b87cfa
123428d
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { pipeline } from '@huggingface/transformers';
import http from 'http';

// Configuration
const PORT = 7860;
// UPGRADED: Starcoder2-1b is a much more capable coding model
const MODEL_NAME = 'Xenova/starcoder2-1b'; 

let generator;

// Initialize the model on startup
async function loadModel() {
    console.log("Loading upgraded coding model...");
    // We specify 'auto' for the device to let the server manage memory efficiently
    generator = await pipeline('text-generation', MODEL_NAME, { device: 'auto' });
    console.log("Model loaded successfully!");
}

const server = http.createServer(async (req, res) => {
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
    res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
    
    if (req.method === 'OPTIONS') {
        res.writeHead(200);
        return res.end();
    }

    res.setHeader('Content-Type', 'application/json');
    const pathname = req.url.split('?')[0];

    if (pathname === '/' && req.method === 'GET') {
        res.writeHead(200);
        res.end(JSON.stringify({ "status": "Backend running with Starcoder2-1b" }));
        return;
    }

    if (pathname === '/generate' && req.method === 'POST') {
        let body = '';
        req.on('data', chunk => { body += chunk.toString(); });
        req.on('end', async () => {
            try {
                const { prompt } = JSON.parse(body);
                if (!generator) {
                    res.writeHead(503);
                    return res.end(JSON.stringify({ error: "Model is still loading..." }));
                }

                // UPGRADED: Increased tokens and tuned temperature for better code quality
                const output = await generator(prompt, { 
                    max_new_tokens: 250, 
                    temperature: 0.2,
                    do_sample: true
                });

                res.writeHead(200);
                res.end(JSON.stringify({ result: output[0].generated_text }));
            } catch (err) {
                res.writeHead(400);
                res.end(JSON.stringify({ error: "Invalid JSON or request" }));
            }
        });
        return;
    }

    res.writeHead(404);
    res.end(JSON.stringify({ error: "Not Found" }));
});

loadModel().then(() => {
    server.listen(PORT, '0.0.0.0', () => {
        console.log(`Server running at http://0.0.0.0:${PORT}`);
    });
});