Spaces:
Sleeping
Sleeping
Update index.js
Browse files
index.js
CHANGED
@@ -1,49 +1,180 @@
|
|
1 |
const express = require('express');
|
2 |
-
const bodyParser = require('body-parser');
|
3 |
-
const app = express();
|
4 |
-
const axios = require('axios');
|
5 |
const { DateTime, Duration } = require('luxon');
|
6 |
-
const { format } = require("util")
|
7 |
-
const port = 7860;
|
8 |
const puppeteer = require("puppeteer");
|
9 |
-
|
|
|
|
|
|
|
|
|
10 |
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
}
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
}
|
19 |
|
20 |
-
|
21 |
-
const genAI = new GoogleGenerativeAI(base64ToText("QUl6YVN5QXYyeHlBblQ5dXZqVTcwdlN1YVBXNGRFa25vWHpnMDVN"));
|
22 |
|
23 |
const db_chatHistory = {};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
function generateUID(length) {
|
26 |
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
27 |
-
const charactersLength = characters.length;
|
28 |
let uid = '';
|
29 |
for (let i = 0; i < length; i++) {
|
30 |
-
uid += characters.charAt(Math.floor(Math.random() *
|
31 |
}
|
32 |
return uid;
|
33 |
}
|
34 |
|
35 |
async function sendMessageV2(data_chat, msg) {
|
36 |
try {
|
37 |
-
const model = genAI.getGenerativeModel({ model: "gemini-pro"});
|
38 |
-
|
39 |
-
const chat = model.startChat(data_chat)
|
40 |
const result = await chat.sendMessage(msg);
|
41 |
const response = await result.response;
|
42 |
-
|
43 |
-
|
44 |
return text;
|
45 |
} catch (error) {
|
46 |
-
console.error(error);
|
|
|
47 |
}
|
48 |
}
|
49 |
|
@@ -54,16 +185,13 @@ async function cleanInactiveUsersV2() {
|
|
54 |
const user = db_chatHistory[userId];
|
55 |
|
56 |
if (user && user.lastChat && user.lastChat.plus(Duration.fromObject({ minutes: 20 })) < currentTime) {
|
57 |
-
|
58 |
-
delete db_chatHistory[userId];
|
59 |
}
|
60 |
});
|
61 |
|
62 |
-
// Schedule the next execution after 1 minute
|
63 |
setTimeout(cleanInactiveUsersV2, 180000);
|
64 |
}
|
65 |
|
66 |
-
// Start the continuous execution of cleanInactiveUsers
|
67 |
cleanInactiveUsersV2();
|
68 |
|
69 |
app.get('/v2/send', async (req, res) => {
|
@@ -76,74 +204,36 @@ app.get('/v2/send', async (req, res) => {
|
|
76 |
});
|
77 |
}
|
78 |
|
79 |
-
let chatData = db_chatHistory[id]
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
{
|
88 |
-
role: "user",
|
89 |
-
parts: [{text: "hello, can you answer my question with a cute kaomoji like this 。◕‿◕。"}]
|
90 |
-
},
|
91 |
-
{
|
92 |
-
role: "model",
|
93 |
-
parts: [{text: "sure (◠‿◕) , so.... ( /^ω^)/♪♪ how can I help dear user ( ╹▽╹ )?"}],
|
94 |
-
},
|
95 |
-
],
|
96 |
-
generationConfig: {
|
97 |
-
maxOutputTokens: 500,
|
98 |
-
},
|
99 |
-
}
|
100 |
}
|
101 |
-
}
|
102 |
-
|
103 |
const botResponse = await sendMessageV2(chatData.data, message);
|
104 |
|
105 |
-
|
106 |
-
|
107 |
-
return item.role === 'user' && item.parts[0].text === message;
|
108 |
-
});
|
109 |
-
|
110 |
-
// Cek apakah bot response sudah ada di riwayat
|
111 |
-
const botResponseExists = chatData.data.history.some(item => {
|
112 |
-
return item.role === 'model' && item.parts[0].text === botResponse;
|
113 |
-
});
|
114 |
-
|
115 |
-
// Jika tidak ada, tambahkan ke riwayat
|
116 |
-
if (!userMessageExists) {
|
117 |
-
chatData.data.history.push({
|
118 |
-
role: 'user',
|
119 |
-
parts: [{text: message}]
|
120 |
-
});
|
121 |
}
|
122 |
|
123 |
-
if (!
|
124 |
-
chatData.data.history.push({
|
125 |
-
role: 'model',
|
126 |
-
parts: [{text: botResponse}]
|
127 |
-
});
|
128 |
}
|
129 |
|
130 |
-
// Update waktu terakhir obrolan
|
131 |
chatData.lastChat = DateTime.local();
|
132 |
db_chatHistory[id] = chatData;
|
133 |
-
|
134 |
-
|
135 |
-
response: botResponse
|
136 |
-
});
|
137 |
} catch (e) {
|
138 |
-
res.status(400).json({
|
139 |
-
success: false,
|
140 |
-
response: format(e)
|
141 |
-
});
|
142 |
}
|
143 |
});
|
144 |
|
145 |
-
|
146 |
-
|
147 |
app.get('/v2/listuser', (req, res) => {
|
148 |
const userList = Object.keys(db_chatHistory);
|
149 |
res.status(200).json({ userList });
|
@@ -151,20 +241,15 @@ app.get('/v2/listuser', (req, res) => {
|
|
151 |
|
152 |
app.get('/v2/get/message', (req, res) => {
|
153 |
const id = req.query.id;
|
154 |
-
if (!id
|
155 |
-
|
156 |
-
return res.status(404).json({ error: 'User not found' });
|
157 |
}
|
158 |
const userMessages = db_chatHistory[id].data.history;
|
159 |
res.status(200).json({ messages: userMessages });
|
160 |
});
|
161 |
|
162 |
-
// Fungsi untuk ping website
|
163 |
async function pingWebsite() {
|
164 |
-
const browser = await puppeteer.launch({
|
165 |
-
headless: true,
|
166 |
-
args: ['--no-sandbox', '--disable-setuid-sandbox']
|
167 |
-
});
|
168 |
const page = await browser.newPage();
|
169 |
await page.setUserAgent("Mozilla/5.0 (Linux; Android 10; SM-G965U Build/QP1A.190711.020; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/114.0.5735.141 Mobile Safari/537.36 [FB_IAB/FB4A;FBAV/420.0.0.32.61;]");
|
170 |
await page.goto('https://huggingface.co/spaces/Nexchan/gptdemo');
|
@@ -172,17 +257,13 @@ async function pingWebsite() {
|
|
172 |
await browser.close();
|
173 |
}
|
174 |
|
175 |
-
// Ping website setiap 5 jam
|
176 |
async function pingEvery5Hours() {
|
177 |
await pingWebsite();
|
178 |
-
setInterval(
|
179 |
-
await pingWebsite();
|
180 |
-
}, 5 * 60 * 60 * 1000); // 5 jam dalam milidetik
|
181 |
}
|
182 |
|
183 |
-
// Mulai ping
|
184 |
pingEvery5Hours();
|
185 |
|
186 |
app.listen(port, () => {
|
187 |
-
|
188 |
});
|
|
|
1 |
const express = require('express');
|
|
|
|
|
|
|
2 |
const { DateTime, Duration } = require('luxon');
|
|
|
|
|
3 |
const puppeteer = require("puppeteer");
|
4 |
+
const { format } = require("util");
|
5 |
+
const { GoogleGenerativeAI } = require("@google/generative-ai");
|
6 |
+
const GPT4js = require("gpt4js");
|
7 |
+
const app = express();
|
8 |
+
const port = 7860;
|
9 |
|
10 |
+
app.use(express.json());
|
11 |
+
|
12 |
+
const db_chatHitamAi = {};
|
13 |
+
const db_chatGPTAi = {};
|
14 |
+
const G4 = {};
|
15 |
+
|
16 |
+
G4.sendMessage = async function(msg, providerName) {
|
17 |
+
try {
|
18 |
+
const options = {
|
19 |
+
provider: providerName || "BlackBox",
|
20 |
+
model: "gpt-4o-free",
|
21 |
+
};
|
22 |
+
const provider = GPT4js.createProvider(options.provider);
|
23 |
+
const text = await provider.chatCompletion(msg, options);
|
24 |
+
return text;
|
25 |
+
} catch (error) {
|
26 |
+
console.error("Error in G4.sendMessage: ", error);
|
27 |
+
throw error;
|
28 |
+
}
|
29 |
+
};
|
30 |
+
|
31 |
+
app.get('/blackbox/send', async (req, res) => {
|
32 |
+
try {
|
33 |
+
const { id, message } = req.query;
|
34 |
+
if (!id || !message) {
|
35 |
+
return res.status(400).json({
|
36 |
+
success: false,
|
37 |
+
response: "Input parameter id and message are required."
|
38 |
+
});
|
39 |
+
}
|
40 |
+
let chatData = db_chatHitamAi[id] || {
|
41 |
+
lastChat: DateTime.local(),
|
42 |
+
data: {
|
43 |
+
message: [
|
44 |
+
{ role: "system", content: "You're an expert bot in programming." }
|
45 |
+
]
|
46 |
+
}
|
47 |
+
};
|
48 |
+
chatData.data.message.push({ role: 'user', content: message });
|
49 |
+
const botResponse = await G4.sendMessage(chatData, "BlackBox");
|
50 |
+
chatData.data.message.push({ role: 'assistant', content: botResponse });
|
51 |
+
chatData.lastChat = DateTime.local();
|
52 |
+
db_chatHitamAi[id] = chatData;
|
53 |
+
res.status(200).json({ success: true, response: botResponse });
|
54 |
+
} catch (e) {
|
55 |
+
res.status(400).json({ success: false, response: format(e) });
|
56 |
+
}
|
57 |
+
});
|
58 |
+
|
59 |
+
app.get('/blackbox/listuser', (req, res) => {
|
60 |
+
const userList = Object.keys(db_chatHitamAi);
|
61 |
+
res.status(200).json({ userList });
|
62 |
+
});
|
63 |
+
|
64 |
+
app.get('/blackbox/get/message', (req, res) => {
|
65 |
+
const id = req.query.id;
|
66 |
+
if (!id || !db_chatHitamAi[id]) {
|
67 |
+
return res.status(404).json({ success: false, response: "User not found" });
|
68 |
+
}
|
69 |
+
const userMessages = db_chatHitamAi[id].data.history;
|
70 |
+
res.status(200).json({ messages: userMessages });
|
71 |
+
});
|
72 |
+
|
73 |
+
async function cleanInactiveUsersV4B() {
|
74 |
+
const currentTime = DateTime.local();
|
75 |
+
|
76 |
+
Object.keys(db_chatHitamAi).forEach((userId) => {
|
77 |
+
const user = db_chatHitamAi[userId];
|
78 |
+
|
79 |
+
if (user && user.lastChat && user.lastChat.plus(Duration.fromObject({ minutes: 20 })) < currentTime) {
|
80 |
+
delete db_chatHitamAi[userId];
|
81 |
+
}
|
82 |
+
});
|
83 |
+
|
84 |
+
setTimeout(cleanInactiveUsersV4B, 180000);
|
85 |
}
|
86 |
|
87 |
+
cleanInactiveUsersV4B();
|
88 |
+
|
89 |
+
app.get('/gpt4/send', async (req, res) => {
|
90 |
+
try {
|
91 |
+
const { id, message } = req.query;
|
92 |
+
if (!id || !message) {
|
93 |
+
return res.status(400).json({
|
94 |
+
success: false,
|
95 |
+
response: "Input parameter id and message are required."
|
96 |
+
});
|
97 |
+
}
|
98 |
+
let chatData = db_chatGPTAi[id] || {
|
99 |
+
lastChat: DateTime.local(),
|
100 |
+
data: {
|
101 |
+
message: [
|
102 |
+
{ role: "system", content: "You're an expert bot in programming." }
|
103 |
+
]
|
104 |
+
}
|
105 |
+
};
|
106 |
+
chatData.data.message.push({ role: 'user', content: message });
|
107 |
+
const botResponse = await G4.sendMessage(chatData, "Nextway");
|
108 |
+
chatData.data.message.push({ role: 'assistant', content: botResponse });
|
109 |
+
chatData.lastChat = DateTime.local();
|
110 |
+
db_chatGPTAi[id] = chatData;
|
111 |
+
res.status(200).json({ success: true, response: botResponse });
|
112 |
+
} catch (e) {
|
113 |
+
res.status(400).json({ success: false, response: format(e) });
|
114 |
+
}
|
115 |
+
});
|
116 |
+
|
117 |
+
app.get('/gpt4/listuser', (req, res) => {
|
118 |
+
const userList = Object.keys(db_chatGPTAi);
|
119 |
+
res.status(200).json({ userList });
|
120 |
+
});
|
121 |
+
|
122 |
+
app.get('/gpt4/get/message', (req, res) => {
|
123 |
+
const id = req.query.id;
|
124 |
+
if (!id || !db_chatGPTAi[id]) {
|
125 |
+
return res.status(404).json({ success: false, response: "User not found" });
|
126 |
+
}
|
127 |
+
const userMessages = db_chatGPTAi[id].data.history;
|
128 |
+
res.status(200).json({ messages: userMessages });
|
129 |
+
});
|
130 |
+
|
131 |
+
async function cleanInactiveUsersV5G() {
|
132 |
+
const currentTime = DateTime.local();
|
133 |
+
|
134 |
+
Object.keys(db_chatGPTAi).forEach((userId) => {
|
135 |
+
const user = db_chatGPTAi[userId];
|
136 |
+
|
137 |
+
if (user && user.lastChat && user.lastChat.plus(Duration.fromObject({ minutes: 20 })) < currentTime) {
|
138 |
+
delete db_chatGPTAi[userId];
|
139 |
+
}
|
140 |
+
});
|
141 |
+
|
142 |
+
setTimeout(cleanInactiveUsersV5G, 180000);
|
143 |
}
|
144 |
|
145 |
+
cleanInactiveUsersV5G();
|
|
|
146 |
|
147 |
const db_chatHistory = {};
|
148 |
+
const genAI = new GoogleGenerativeAI(base64ToText("QUl6YVN5QXYyeHlBblQ5dXZqVTcwdlN1YVBXNGRFa25vWHpnMDVN"));
|
149 |
+
|
150 |
+
function textToBase64(text) {
|
151 |
+
return Buffer.from(text).toString('base64');
|
152 |
+
}
|
153 |
+
|
154 |
+
function base64ToText(base64) {
|
155 |
+
return Buffer.from(base64, 'base64').toString();
|
156 |
+
}
|
157 |
|
158 |
function generateUID(length) {
|
159 |
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
|
|
160 |
let uid = '';
|
161 |
for (let i = 0; i < length; i++) {
|
162 |
+
uid += characters.charAt(Math.floor(Math.random() * characters.length));
|
163 |
}
|
164 |
return uid;
|
165 |
}
|
166 |
|
167 |
async function sendMessageV2(data_chat, msg) {
|
168 |
try {
|
169 |
+
const model = genAI.getGenerativeModel({ model: "gemini-pro" });
|
170 |
+
const chat = model.startChat(data_chat);
|
|
|
171 |
const result = await chat.sendMessage(msg);
|
172 |
const response = await result.response;
|
173 |
+
const text = response.text();
|
|
|
174 |
return text;
|
175 |
} catch (error) {
|
176 |
+
console.error("Error in sendMessageV2: ", error);
|
177 |
+
throw error;
|
178 |
}
|
179 |
}
|
180 |
|
|
|
185 |
const user = db_chatHistory[userId];
|
186 |
|
187 |
if (user && user.lastChat && user.lastChat.plus(Duration.fromObject({ minutes: 20 })) < currentTime) {
|
188 |
+
delete db_chatHistory[userId];
|
|
|
189 |
}
|
190 |
});
|
191 |
|
|
|
192 |
setTimeout(cleanInactiveUsersV2, 180000);
|
193 |
}
|
194 |
|
|
|
195 |
cleanInactiveUsersV2();
|
196 |
|
197 |
app.get('/v2/send', async (req, res) => {
|
|
|
204 |
});
|
205 |
}
|
206 |
|
207 |
+
let chatData = db_chatHistory[id] || {
|
208 |
+
lastChat: DateTime.local(),
|
209 |
+
data: {
|
210 |
+
history: [
|
211 |
+
{ role: "user", parts: [{ text: "hello, can you answer my question with a cute kaomoji like this 。◕‿◕。" }] },
|
212 |
+
{ role: "model", parts: [{ text: "sure (◠‿◕) , so.... ( /^ω^)/♪♪ how can I help dear user ( ╹▽╹ )?" }] }
|
213 |
+
],
|
214 |
+
generationConfig: { maxOutputTokens: 500 }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
215 |
}
|
216 |
+
};
|
217 |
+
|
218 |
const botResponse = await sendMessageV2(chatData.data, message);
|
219 |
|
220 |
+
if (!chatData.data.history.some(item => item.role === 'user' && item.parts[0].text === message)) {
|
221 |
+
chatData.data.history.push({ role: 'user', parts: [{ text: message }] });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
222 |
}
|
223 |
|
224 |
+
if (!chatData.data.history.some(item => item.role === 'model' && item.parts[0].text === botResponse)) {
|
225 |
+
chatData.data.history.push({ role: 'model', parts: [{ text: botResponse }] });
|
|
|
|
|
|
|
226 |
}
|
227 |
|
|
|
228 |
chatData.lastChat = DateTime.local();
|
229 |
db_chatHistory[id] = chatData;
|
230 |
+
|
231 |
+
res.status(200).json({ success: true, response: botResponse });
|
|
|
|
|
232 |
} catch (e) {
|
233 |
+
res.status(400).json({ success: false, response: format(e) });
|
|
|
|
|
|
|
234 |
}
|
235 |
});
|
236 |
|
|
|
|
|
237 |
app.get('/v2/listuser', (req, res) => {
|
238 |
const userList = Object.keys(db_chatHistory);
|
239 |
res.status(200).json({ userList });
|
|
|
241 |
|
242 |
app.get('/v2/get/message', (req, res) => {
|
243 |
const id = req.query.id;
|
244 |
+
if (!id || !db_chatHistory[id]) {
|
245 |
+
return res.status(404).json({ success: false, response: "User not found" });
|
|
|
246 |
}
|
247 |
const userMessages = db_chatHistory[id].data.history;
|
248 |
res.status(200).json({ messages: userMessages });
|
249 |
});
|
250 |
|
|
|
251 |
async function pingWebsite() {
|
252 |
+
const browser = await puppeteer.launch({ headless: true, args: ['--no-sandbox', '--disable-setuid-sandbox'] });
|
|
|
|
|
|
|
253 |
const page = await browser.newPage();
|
254 |
await page.setUserAgent("Mozilla/5.0 (Linux; Android 10; SM-G965U Build/QP1A.190711.020; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/114.0.5735.141 Mobile Safari/537.36 [FB_IAB/FB4A;FBAV/420.0.0.32.61;]");
|
255 |
await page.goto('https://huggingface.co/spaces/Nexchan/gptdemo');
|
|
|
257 |
await browser.close();
|
258 |
}
|
259 |
|
|
|
260 |
async function pingEvery5Hours() {
|
261 |
await pingWebsite();
|
262 |
+
setInterval(pingWebsite, 5 * 60 * 60 * 1000);
|
|
|
|
|
263 |
}
|
264 |
|
|
|
265 |
pingEvery5Hours();
|
266 |
|
267 |
app.listen(port, () => {
|
268 |
+
console.log(`Server is running on port ${port}`);
|
269 |
});
|