Rooni commited on
Commit
098cbfd
·
verified ·
1 Parent(s): 0f8eb30

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +34 -36
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
- next();
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
- try {
31
- // Динамический импорт node-fetch
32
- const fetch = (await import('node-fetch')).default;
 
 
 
 
33
 
34
- console.log('Sending request to Hugging Face API...');
35
 
36
- const response = await fetch('https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-3-medium', {
37
- method: 'POST',
38
- headers: {
39
- 'Authorization': `Bearer ${apiKey}`,
40
- 'Content-Type': 'application/json'
41
- },
42
- body: JSON.stringify({
43
- inputs: prompt
44
- }),
45
- // Добавляем тайм-аут в 60 секунд
46
- timeout: 60000
47
- });
48
 
49
- if (!response.ok) {
50
- throw new Error(`Error from Hugging Face API: ${response.statusText}`);
51
- }
52
 
53
- console.log('Received response from Hugging Face API, processing image...');
54
 
55
- const imageBuffer = await response.buffer();
56
- const base64Image = imageBuffer.toString('base64');
57
 
58
- res.json({ image: base64Image });
59
- } catch (error) {
60
- console.error('Error generating image:', error.message);
61
- res.status(500).send('Error generating image');
 
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}`);