Update server.js
Browse files
server.js
CHANGED
|
@@ -16,51 +16,49 @@ app.all('/', (req, res, next) => {
|
|
| 16 |
if (req.method !== 'POST') {
|
| 17 |
res.status(405).send('Только POST');
|
| 18 |
} else {
|
| 19 |
-
|
| 20 |
-
}
|
| 21 |
-
});
|
| 22 |
-
|
| 23 |
-
app.post('/generate-image', async (req, res) => {
|
| 24 |
-
const { prompt } = req.body;
|
| 25 |
-
|
| 26 |
-
if (!prompt) {
|
| 27 |
-
return res.status(400).send('Missing "prompt" in request body');
|
| 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 |
app.listen(port, () => {
|
| 66 |
console.log(`Server is running on http://localhost:${port}`);
|
|
|
|
| 16 |
if (req.method !== 'POST') {
|
| 17 |
res.status(405).send('Только POST');
|
| 18 |
} else {
|
| 19 |
+
const { prompt } = req.body;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
if (!prompt) {
|
| 22 |
+
return res.status(400).send('Missing "prompt" in request body');
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
try {
|
| 26 |
+
// Динамический импорт node-fetch
|
| 27 |
+
const fetch = (await import('node-fetch')).default;
|
| 28 |
|
| 29 |
+
console.log('Sending request to Hugging Face API...');
|
| 30 |
|
| 31 |
+
const response = await fetch('https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-3-medium', {
|
| 32 |
+
method: 'POST',
|
| 33 |
+
headers: {
|
| 34 |
+
'Authorization': `Bearer ${apiKey}`,
|
| 35 |
+
'Content-Type': 'application/json'
|
| 36 |
+
},
|
| 37 |
+
body: JSON.stringify({
|
| 38 |
+
inputs: prompt
|
| 39 |
+
}),
|
| 40 |
+
// Добавляем тайм-аут в 60 секунд
|
| 41 |
+
timeout: 60000
|
| 42 |
+
});
|
| 43 |
|
| 44 |
+
if (!response.ok) {
|
| 45 |
+
throw new Error(`Error from Hugging Face API: ${response.statusText}`);
|
| 46 |
+
}
|
| 47 |
|
| 48 |
+
console.log('Received response from Hugging Face API, processing image...');
|
| 49 |
|
| 50 |
+
const imageBuffer = await response.buffer();
|
| 51 |
+
const base64Image = imageBuffer.toString('base64');
|
| 52 |
|
| 53 |
+
res.json({ image: base64Image });
|
| 54 |
+
} catch (error) {
|
| 55 |
+
console.error('Error generating image:', error.message);
|
| 56 |
+
res.status(500).send('Error generating image');
|
| 57 |
+
}
|
| 58 |
}
|
| 59 |
});
|
| 60 |
+
|
| 61 |
+
});
|
| 62 |
|
| 63 |
app.listen(port, () => {
|
| 64 |
console.log(`Server is running on http://localhost:${port}`);
|