Spaces:
Sleeping
Sleeping
Update index.js
Browse files
index.js
CHANGED
@@ -12,26 +12,18 @@ const port = 7860;
|
|
12 |
|
13 |
app.use(bodyParser.json());
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
// Fungsi untuk mengonversi teks menjadi Base64
|
19 |
function convertTextToBase64(text) {
|
20 |
return Buffer.from(text).toString('base64');
|
21 |
}
|
22 |
|
23 |
-
// Fungsi untuk mengembalikan teks dari Base64
|
24 |
function convertBase64ToText(base64) {
|
25 |
return Buffer.from(base64, 'base64').toString('utf-8');
|
26 |
}
|
27 |
|
28 |
-
// Inisialisasi GoogleGenerativeAI
|
29 |
const genAI = new GoogleGenerativeAI(convertBase64ToText("QUl6YVN5QXYyeHlBblQ5dXZqVTcwdlN1YVBXNGRFa25vWHpnMDVN"));
|
30 |
|
31 |
-
// Data sesi chat untuk versi 2
|
32 |
const db_chatSessions = {};
|
33 |
|
34 |
-
// Fungsi untuk menghasilkan UID
|
35 |
function generateUID(length) {
|
36 |
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
37 |
let uid = '';
|
@@ -41,43 +33,54 @@ function generateUID(length) {
|
|
41 |
return uid;
|
42 |
}
|
43 |
|
44 |
-
// Fungsi untuk mengirim pesan versi 2
|
45 |
async function sendChatMessageV2(data_chat, msg) {
|
46 |
try {
|
47 |
const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash" });
|
48 |
-
|
49 |
const chat = model.startChat(data_chat);
|
50 |
const result = await chat.sendMessage(msg);
|
51 |
const response = await result.response;
|
52 |
const text = response.text();
|
53 |
-
|
54 |
return text;
|
55 |
} catch (error) {
|
56 |
console.error(error);
|
|
|
57 |
}
|
58 |
}
|
59 |
|
60 |
-
// Fungsi untuk membersihkan pengguna tidak aktif versi 2
|
61 |
async function clearInactiveUsersV2() {
|
62 |
const currentTime = DateTime.local();
|
63 |
-
|
64 |
Object.keys(db_chatSessions).forEach((userId) => {
|
65 |
const user = db_chatSessions[userId];
|
66 |
-
|
67 |
if (user && user.lastChat && user.lastChat.plus(Duration.fromObject({ minutes: 20 })) < currentTime) {
|
68 |
-
// Hapus pengguna yang tidak aktif lebih dari 20 menit dari db_chatSessions
|
69 |
delete db_chatSessions[userId];
|
70 |
}
|
71 |
});
|
72 |
-
|
73 |
-
// Jadwalkan eksekusi berikutnya setelah 3 menit
|
74 |
setTimeout(clearInactiveUsersV2, 180000);
|
75 |
}
|
76 |
|
77 |
-
// Mulai eksekusi terus-menerus untuk membersihkan pengguna tidak aktif
|
78 |
clearInactiveUsersV2();
|
79 |
|
80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
app.get('/v2/send', async (req, res) => {
|
82 |
try {
|
83 |
const { id, message } = req.query;
|
@@ -91,42 +94,35 @@ app.get('/v2/send', async (req, res) => {
|
|
91 |
let chatData = db_chatSessions[id];
|
92 |
|
93 |
if (!chatData) {
|
94 |
-
// Jika data riwayat obrolan tidak ada, buat data baru
|
95 |
chatData = {
|
96 |
lastChat: DateTime.local(),
|
97 |
data: {
|
98 |
-
history: [
|
99 |
-
{
|
100 |
-
role: "user",
|
101 |
-
parts: [{text: "hello, can you answer my question with a cute kaomoji like this 。◕‿◕。"}]
|
102 |
-
},
|
103 |
-
{
|
104 |
-
role: "model",
|
105 |
-
parts: [{text: "sure (◠‿◕) , so.... ( /^ω^)/♪♪ how can I help dear user ( ╹▽╹ )?"}],
|
106 |
-
},
|
107 |
-
],
|
108 |
-
generationConfig: {
|
109 |
-
maxOutputTokens: 500,
|
110 |
-
},
|
111 |
}
|
112 |
};
|
113 |
}
|
114 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
const botResponse = await sendChatMessageV2(chatData.data, message);
|
116 |
|
117 |
-
|
118 |
-
|
119 |
-
role: 'user',
|
120 |
-
parts: [{text: message}]
|
121 |
-
});
|
122 |
-
// Simpan bot response ke riwayat obrolan
|
123 |
-
chatData.data.history.push({
|
124 |
-
role: 'model',
|
125 |
-
parts: [{text: botResponse}]
|
126 |
-
});
|
127 |
-
// Update waktu terakhir obrolan
|
128 |
chatData.lastChat = DateTime.local();
|
129 |
db_chatSessions[id] = chatData;
|
|
|
130 |
res.status(200).json({
|
131 |
success: true,
|
132 |
response: botResponse
|
@@ -139,13 +135,11 @@ app.get('/v2/send', async (req, res) => {
|
|
139 |
}
|
140 |
});
|
141 |
|
142 |
-
// Endpoint untuk daftar pengguna versi 2
|
143 |
app.get('/v2/listuser', (req, res) => {
|
144 |
const userList = Object.keys(db_chatSessions);
|
145 |
res.status(200).json({ userList });
|
146 |
});
|
147 |
|
148 |
-
// Endpoint untuk mendapatkan pesan versi 2
|
149 |
app.get('/v2/get/message', (req, res) => {
|
150 |
const id = req.query.id;
|
151 |
if (!id) return res.status(400).json({ success: false, response: "input id" });
|
@@ -156,30 +150,6 @@ app.get('/v2/get/message', (req, res) => {
|
|
156 |
res.status(200).json({ messages: userMessages });
|
157 |
});
|
158 |
|
159 |
-
// Fungsi untuk ping website
|
160 |
-
async function pingWebsite() {
|
161 |
-
const browser = await puppeteer.launch({
|
162 |
-
headless: true,
|
163 |
-
args: ['--no-sandbox', '--disable-setuid-sandbox']
|
164 |
-
});
|
165 |
-
const page = await browser.newPage();
|
166 |
-
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;]");
|
167 |
-
await page.goto('https://huggingface.co/spaces/Nexchan/gptdemo');
|
168 |
-
console.log("Ping");
|
169 |
-
await browser.close();
|
170 |
-
}
|
171 |
-
|
172 |
-
// Ping website setiap 5 jam
|
173 |
-
async function pingEvery5Hours() {
|
174 |
-
await pingWebsite();
|
175 |
-
setInterval(async () => {
|
176 |
-
await pingWebsite();
|
177 |
-
}, 5 * 60 * 60 * 1000); // 5 jam dalam milidetik
|
178 |
-
}
|
179 |
-
|
180 |
-
// Mulai ping
|
181 |
-
pingEvery5Hours();
|
182 |
-
|
183 |
app.listen(port, () => {
|
184 |
console.log(`Server is running on port ${port}`);
|
185 |
-
});
|
|
|
12 |
|
13 |
app.use(bodyParser.json());
|
14 |
|
|
|
|
|
|
|
|
|
15 |
function convertTextToBase64(text) {
|
16 |
return Buffer.from(text).toString('base64');
|
17 |
}
|
18 |
|
|
|
19 |
function convertBase64ToText(base64) {
|
20 |
return Buffer.from(base64, 'base64').toString('utf-8');
|
21 |
}
|
22 |
|
|
|
23 |
const genAI = new GoogleGenerativeAI(convertBase64ToText("QUl6YVN5QXYyeHlBblQ5dXZqVTcwdlN1YVBXNGRFa25vWHpnMDVN"));
|
24 |
|
|
|
25 |
const db_chatSessions = {};
|
26 |
|
|
|
27 |
function generateUID(length) {
|
28 |
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
29 |
let uid = '';
|
|
|
33 |
return uid;
|
34 |
}
|
35 |
|
|
|
36 |
async function sendChatMessageV2(data_chat, msg) {
|
37 |
try {
|
38 |
const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash" });
|
|
|
39 |
const chat = model.startChat(data_chat);
|
40 |
const result = await chat.sendMessage(msg);
|
41 |
const response = await result.response;
|
42 |
const text = response.text();
|
|
|
43 |
return text;
|
44 |
} catch (error) {
|
45 |
console.error(error);
|
46 |
+
throw error;
|
47 |
}
|
48 |
}
|
49 |
|
|
|
50 |
async function clearInactiveUsersV2() {
|
51 |
const currentTime = DateTime.local();
|
|
|
52 |
Object.keys(db_chatSessions).forEach((userId) => {
|
53 |
const user = db_chatSessions[userId];
|
|
|
54 |
if (user && user.lastChat && user.lastChat.plus(Duration.fromObject({ minutes: 20 })) < currentTime) {
|
|
|
55 |
delete db_chatSessions[userId];
|
56 |
}
|
57 |
});
|
|
|
|
|
58 |
setTimeout(clearInactiveUsersV2, 180000);
|
59 |
}
|
60 |
|
|
|
61 |
clearInactiveUsersV2();
|
62 |
|
63 |
+
async function pingWebsite() {
|
64 |
+
const browser = await puppeteer.launch({
|
65 |
+
headless: true,
|
66 |
+
args: ['--no-sandbox', '--disable-setuid-sandbox']
|
67 |
+
});
|
68 |
+
const page = await browser.newPage();
|
69 |
+
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;]");
|
70 |
+
await page.goto('https://huggingface.co/spaces/Nexchan/gptdemo');
|
71 |
+
console.log("Ping");
|
72 |
+
await browser.close();
|
73 |
+
}
|
74 |
+
|
75 |
+
async function pingEvery5Hours() {
|
76 |
+
await pingWebsite();
|
77 |
+
setInterval(async () => {
|
78 |
+
await pingWebsite();
|
79 |
+
}, 5 * 60 * 60 * 1000);
|
80 |
+
}
|
81 |
+
|
82 |
+
pingEvery5Hours();
|
83 |
+
|
84 |
app.get('/v2/send', async (req, res) => {
|
85 |
try {
|
86 |
const { id, message } = req.query;
|
|
|
94 |
let chatData = db_chatSessions[id];
|
95 |
|
96 |
if (!chatData) {
|
|
|
97 |
chatData = {
|
98 |
lastChat: DateTime.local(),
|
99 |
data: {
|
100 |
+
history: []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
}
|
102 |
};
|
103 |
}
|
104 |
|
105 |
+
function addToChatHistory(chatData, role, text) {
|
106 |
+
const lastEntry = chatData.data.history[chatData.data.history.length - 1];
|
107 |
+
if (lastEntry && lastEntry.role === role) {
|
108 |
+
lastEntry.parts.push({ text });
|
109 |
+
} else {
|
110 |
+
chatData.data.history.push({
|
111 |
+
role,
|
112 |
+
parts: [{ text }]
|
113 |
+
});
|
114 |
+
}
|
115 |
+
}
|
116 |
+
|
117 |
+
addToChatHistory(chatData, 'user', message);
|
118 |
+
|
119 |
const botResponse = await sendChatMessageV2(chatData.data, message);
|
120 |
|
121 |
+
addToChatHistory(chatData, 'model', botResponse);
|
122 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
123 |
chatData.lastChat = DateTime.local();
|
124 |
db_chatSessions[id] = chatData;
|
125 |
+
|
126 |
res.status(200).json({
|
127 |
success: true,
|
128 |
response: botResponse
|
|
|
135 |
}
|
136 |
});
|
137 |
|
|
|
138 |
app.get('/v2/listuser', (req, res) => {
|
139 |
const userList = Object.keys(db_chatSessions);
|
140 |
res.status(200).json({ userList });
|
141 |
});
|
142 |
|
|
|
143 |
app.get('/v2/get/message', (req, res) => {
|
144 |
const id = req.query.id;
|
145 |
if (!id) return res.status(400).json({ success: false, response: "input id" });
|
|
|
150 |
res.status(200).json({ messages: userMessages });
|
151 |
});
|
152 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
153 |
app.listen(port, () => {
|
154 |
console.log(`Server is running on port ${port}`);
|
155 |
+
});
|