File size: 2,010 Bytes
86bd386
 
 
3ea6928
86bd386
3ea6928
5669f71
abed4cc
289e552
3ea6928
5d38af1
289e552
587da90
ffc35f2
0f8eb30
ffc35f2
 
 
 
 
 
 
 
289e552
a3fc701
289e552
 
 
2b60bab
289e552
86bd386
 
 
94f215f
 
1722338
289e552
 
3ea6928
289e552
 
 
 
94f215f
 
 
289e552
 
 
 
 
 
94f215f
 
289e552
 
 
 
 
94f215f
289e552
2b60bab
587da90
 
abed4cc
289e552
 
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
const express = require('express');
const bodyParser = require('body-parser');
const dotenv = require('dotenv');

// Загрузка переменных окружения из .env файла
dotenv.config();

const app = express();
const port = 3000;
const apiKey = process.env.KEY;

app.use(bodyParser.json());

// Обработка только POST запросов
app.all('/', (req, res, next) => {
    if (req.method !== 'POST') {
        res.status(405).send('Только POST');
    } else {
        next();
    }
});

app.post('/generate-image', async (req, res) => {
    const { prompt } = req.body;

    if (!prompt) {
        return res.status(400).send('Missing "prompt" in request body');
    }

    try {
        // Динамический импорт node-fetch
        const fetch = (await import('node-fetch')).default;

        console.log('Sending request to Hugging Face API...');

        const response = await fetch('https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-3-medium', {
            method: 'POST',
            headers: {
                'Authorization': `Bearer ${apiKey}`,
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                inputs: prompt
            }),
            // Добавляем тайм-аут в 60 секунд
            timeout: 60000
        });

        if (!response.ok) {
            throw new Error(`Error from Hugging Face API: ${response.statusText}`);
        }

        console.log('Received response from Hugging Face API, processing image...');

        const imageBuffer = await response.buffer();
        const base64Image = imageBuffer.toString('base64');

        res.json({ image: base64Image });
    } catch (error) {
        console.error('Error generating image:', error.message);
        res.status(500).send('Error generating image');
    }
});

app.listen(port, () => {
    console.log(`Server is running on http://localhost:${port}`);
});