Spaces:
Sleeping
Sleeping
Update server.js
Browse files
server.js
CHANGED
|
@@ -1,19 +1,13 @@
|
|
| 1 |
const express = require('express');
|
| 2 |
const rateLimit = require('express-rate-limit');
|
| 3 |
const axios = require('axios');
|
| 4 |
-
const multer = require('multer'); //
|
| 5 |
-
const ImageKit = require('imagekit-javascript'); // Импорт ImageKit
|
| 6 |
|
| 7 |
const app = express();
|
| 8 |
app.use(express.json({ limit: '10mb' })); // Увеличение лимита для base64 изображений
|
| 9 |
|
| 10 |
const openai_key = process.env.OPENAI_KEY;
|
| 11 |
const base_url = process.env.BASE_URL;
|
| 12 |
-
const imagekit = new ImageKit({
|
| 13 |
-
publicKey: process.env.IMAGEKIT_PUBLIC_KEY,
|
| 14 |
-
privateKey: process.env.IMAGEKIT_PRIVATE_KEY,
|
| 15 |
-
urlEndpoint: process.env.IMAGEKIT_URL_ENDPOINT,
|
| 16 |
-
});
|
| 17 |
|
| 18 |
const limiter = rateLimit({
|
| 19 |
windowMs: 40 * 1000, // 40 секунд
|
|
@@ -35,9 +29,18 @@ app.post('/gener', async (req, res) => {
|
|
| 35 |
return res.json({ content: '+ошибка+❗ Проект закрыт и больше не функционирует из-за отсутствия финансирования!-ошибка-' });
|
| 36 |
});
|
| 37 |
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
const prompt = req.body.prompt;
|
| 40 |
-
const
|
| 41 |
const apiKey = req.body.api || openai_key;
|
| 42 |
let payload;
|
| 43 |
|
|
@@ -50,8 +53,8 @@ app.post('/gen', async (req, res) => {
|
|
| 50 |
}
|
| 51 |
|
| 52 |
// Создание тела запроса в зависимости от наличия изображения
|
| 53 |
-
if (
|
| 54 |
-
// Если изображение
|
| 55 |
payload = {
|
| 56 |
"model": "gpt-4-vision-preview",
|
| 57 |
"messages": [
|
|
@@ -73,7 +76,7 @@ app.post('/gen', async (req, res) => {
|
|
| 73 |
{
|
| 74 |
"type": "image_url",
|
| 75 |
"image_url": {
|
| 76 |
-
"url":
|
| 77 |
},
|
| 78 |
},
|
| 79 |
],
|
|
@@ -118,51 +121,6 @@ app.post('/gen', async (req, res) => {
|
|
| 118 |
}
|
| 119 |
});
|
| 120 |
|
| 121 |
-
app.post('/translate', async (req, res) => {
|
| 122 |
-
const prompt = req.body.prompt;
|
| 123 |
-
const lang = req.body.lang;
|
| 124 |
-
const apiKey = req.body.api || openai_key;
|
| 125 |
-
let payload;
|
| 126 |
-
|
| 127 |
-
if (!prompt) {
|
| 128 |
-
return res.json({ content: '+ошибка+❌ Ошибка данных, повторите попытку.-ошибка-' });
|
| 129 |
-
}
|
| 130 |
-
payload = {
|
| 131 |
-
"model": "gpt-3.5-turbo-16k",
|
| 132 |
-
"messages": [
|
| 133 |
-
{
|
| 134 |
-
"role": "system",
|
| 135 |
-
"content": starttransl,
|
| 136 |
-
},
|
| 137 |
-
{
|
| 138 |
-
"role": "user",
|
| 139 |
-
"content": `На ${lang}: ${prompt}`,
|
| 140 |
-
}
|
| 141 |
-
],
|
| 142 |
-
"max_tokens": 9000,
|
| 143 |
-
};
|
| 144 |
-
|
| 145 |
-
try {
|
| 146 |
-
const response = await axios.post(base_url, payload, {
|
| 147 |
-
headers: {
|
| 148 |
-
'Authorization': `Bearer ${apiKey}`,
|
| 149 |
-
'Content-Type': 'application/json',
|
| 150 |
-
},
|
| 151 |
-
});
|
| 152 |
-
|
| 153 |
-
if (response.data.choices && response.data.choices.length > 0 && response.data.choices[0].message) {
|
| 154 |
-
const content = response.data.choices[0].message.content.trim();
|
| 155 |
-
console.log(content);
|
| 156 |
-
res.json({ content });
|
| 157 |
-
} else {
|
| 158 |
-
res.json({ content: '+ошибка+❌ Не удалось прочитать перевод.-ошибка-' });
|
| 159 |
-
}
|
| 160 |
-
} catch (error) {
|
| 161 |
-
console.error(error);
|
| 162 |
-
res.json({ content: '+ошибка+❌ Произошла ошибка сервера при генерации.-ошибка-' });
|
| 163 |
-
};
|
| 164 |
-
});
|
| 165 |
-
|
| 166 |
const port = 7860;
|
| 167 |
app.listen(port, () => {
|
| 168 |
console.log(`API сервер запущен на порту ${port}`);
|
|
|
|
| 1 |
const express = require('express');
|
| 2 |
const rateLimit = require('express-rate-limit');
|
| 3 |
const axios = require('axios');
|
| 4 |
+
const multer = require('multer'); // импорт multer
|
|
|
|
| 5 |
|
| 6 |
const app = express();
|
| 7 |
app.use(express.json({ limit: '10mb' })); // Увеличение лимита для base64 изображений
|
| 8 |
|
| 9 |
const openai_key = process.env.OPENAI_KEY;
|
| 10 |
const base_url = process.env.BASE_URL;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
const limiter = rateLimit({
|
| 13 |
windowMs: 40 * 1000, // 40 секунд
|
|
|
|
| 29 |
return res.json({ content: '+ошибка+❗ Проект закрыт и больше не функционирует из-за отсутствия финансирования!-ошибка-' });
|
| 30 |
});
|
| 31 |
|
| 32 |
+
const storage = multer.diskStorage({
|
| 33 |
+
destination: './uploads',
|
| 34 |
+
filename: (req, file, cb) => {
|
| 35 |
+
cb(null, Date.now() + '-' + file.originalname);
|
| 36 |
+
},
|
| 37 |
+
});
|
| 38 |
+
|
| 39 |
+
const upload = multer({ storage });
|
| 40 |
+
|
| 41 |
+
app.post('/gen', upload.single('image'), async (req, res) => {
|
| 42 |
const prompt = req.body.prompt;
|
| 43 |
+
const img = req.file ? req.file.path : undefined; // Проверка на существование файла перед доступом к его пути
|
| 44 |
const apiKey = req.body.api || openai_key;
|
| 45 |
let payload;
|
| 46 |
|
|
|
|
| 53 |
}
|
| 54 |
|
| 55 |
// Создание тела запроса в зависимости от наличия изображения
|
| 56 |
+
if (img) {
|
| 57 |
+
// Если изображение предоставлено
|
| 58 |
payload = {
|
| 59 |
"model": "gpt-4-vision-preview",
|
| 60 |
"messages": [
|
|
|
|
| 76 |
{
|
| 77 |
"type": "image_url",
|
| 78 |
"image_url": {
|
| 79 |
+
"url": `http://${req.headers.host}/uploads/${img}`,
|
| 80 |
},
|
| 81 |
},
|
| 82 |
],
|
|
|
|
| 121 |
}
|
| 122 |
});
|
| 123 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
const port = 7860;
|
| 125 |
app.listen(port, () => {
|
| 126 |
console.log(`API сервер запущен на порту ${port}`);
|