Spaces:
Paused
Paused
Update server.js
Browse files
server.js
CHANGED
|
@@ -1,16 +1,54 @@
|
|
| 1 |
const express = require('express');
|
| 2 |
-
const
|
|
|
|
|
|
|
|
|
|
| 3 |
const app = express();
|
| 4 |
-
const
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
const port = 7860;
|
| 14 |
app.listen(port, () => {
|
| 15 |
-
console.log(`
|
| 16 |
});
|
|
|
|
| 1 |
const express = require('express');
|
| 2 |
+
const rateLimit = require('express-rate-limit');
|
| 3 |
+
const axios = require('axios');
|
| 4 |
+
require('dotenv').config();
|
| 5 |
+
|
| 6 |
const app = express();
|
| 7 |
+
const openai_key = process.env.OPENAI_KEY;
|
| 8 |
+
|
| 9 |
+
// Rate limiter configuration
|
| 10 |
+
const limiter = rateLimit({
|
| 11 |
+
windowMs: 30 * 1000, // 30 seconds
|
| 12 |
+
max: 1, // limit each IP to 1 request per windowMs
|
| 13 |
+
onLimitReached: (req, res) => {
|
| 14 |
+
console.error(`Rate limit exceeded for ${req.ip}`);
|
| 15 |
+
}
|
| 16 |
+
});
|
| 17 |
+
|
| 18 |
+
// Apply rate limiting to all requests
|
| 19 |
+
app.use(limiter);
|
| 20 |
+
|
| 21 |
+
// Enable JSON body parsing
|
| 22 |
+
app.use(express.json());
|
| 23 |
+
|
| 24 |
+
// DALL-E 3 image generation endpoint
|
| 25 |
+
app.post('/', async (req, res) => {
|
| 26 |
+
try {
|
| 27 |
+
const { prompt } = req.body;
|
| 28 |
+
// Check if the prompt is provided
|
| 29 |
+
if (!prompt) {
|
| 30 |
+
return res.status(400).send({ message: 'No prompt provided' });
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
// Send the request to OpenAI's DALL-E 3 API
|
| 34 |
+
const response = await axios.post('https://api.openai.com/v1/images/generations', {
|
| 35 |
+
prompt: prompt
|
| 36 |
+
}, {
|
| 37 |
+
headers: {
|
| 38 |
+
'Authorization': `Bearer ${openai_key}`,
|
| 39 |
+
'Content-Type': 'application/json'
|
| 40 |
+
}
|
| 41 |
+
});
|
| 42 |
+
|
| 43 |
+
// Send back the generated image
|
| 44 |
+
res.send(response.data);
|
| 45 |
+
} catch (error) {
|
| 46 |
+
console.error('Error generating image:', error);
|
| 47 |
+
res.status(500).send({ message: 'Error generating image' });
|
| 48 |
+
}
|
| 49 |
+
});
|
| 50 |
+
|
| 51 |
const port = 7860;
|
| 52 |
app.listen(port, () => {
|
| 53 |
+
console.log(`API server listening on port ${port}`);
|
| 54 |
});
|