Spaces:
Sleeping
Sleeping
Update index.js
Browse files
index.js
CHANGED
@@ -1,85 +1,70 @@
|
|
1 |
const express = require('express');
|
2 |
const bodyParser = require('body-parser');
|
|
|
3 |
const axios = require('axios');
|
4 |
const { DateTime, Duration } = require('luxon');
|
5 |
-
const { format } = require("util")
|
6 |
-
const puppeteer = require("puppeteer");
|
7 |
-
const { GoogleGenerativeAI } = require("@google/generative-ai");
|
8 |
-
const GPT4js = require('gpt4js');
|
9 |
-
|
10 |
-
const app = express();
|
11 |
const port = 7860;
|
12 |
-
|
13 |
app.use(bodyParser.json());
|
14 |
|
15 |
-
function
|
16 |
return Buffer.from(text).toString('base64');
|
17 |
}
|
18 |
|
19 |
-
|
|
|
20 |
return Buffer.from(base64, 'base64').toString('utf-8');
|
21 |
}
|
22 |
|
23 |
-
const
|
|
|
24 |
|
25 |
-
const
|
26 |
|
27 |
function generateUID(length) {
|
28 |
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
|
|
29 |
let uid = '';
|
30 |
for (let i = 0; i < length; i++) {
|
31 |
-
uid += characters.charAt(Math.floor(Math.random() *
|
32 |
}
|
33 |
return uid;
|
34 |
}
|
35 |
|
36 |
-
async function
|
37 |
try {
|
38 |
-
const model = genAI.getGenerativeModel({ model: "gemini-
|
39 |
-
|
|
|
40 |
const result = await chat.sendMessage(msg);
|
41 |
const response = await result.response;
|
42 |
-
|
|
|
43 |
return text;
|
44 |
} catch (error) {
|
45 |
console.error(error);
|
46 |
-
throw error;
|
47 |
}
|
48 |
}
|
49 |
|
50 |
-
async function
|
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 |
-
|
|
|
62 |
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
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 |
-
|
76 |
-
|
77 |
-
setInterval(async () => {
|
78 |
-
await pingWebsite();
|
79 |
-
}, 5 * 60 * 60 * 1000);
|
80 |
}
|
81 |
|
82 |
-
|
|
|
83 |
|
84 |
app.get('/v2/send', async (req, res) => {
|
85 |
try {
|
@@ -91,41 +76,45 @@ app.get('/v2/send', async (req, res) => {
|
|
91 |
});
|
92 |
}
|
93 |
|
94 |
-
let chatData =
|
95 |
|
96 |
if (!chatData) {
|
|
|
97 |
chatData = {
|
98 |
lastChat: DateTime.local(),
|
99 |
data: {
|
100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
|
|
|
|
|
|
125 |
chatData.lastChat = DateTime.local();
|
126 |
-
|
127 |
-
db_chatSessions[id] = chatData;
|
128 |
-
|
129 |
res.status(200).json({
|
130 |
success: true,
|
131 |
response: botResponse
|
@@ -138,21 +127,46 @@ app.get('/v2/send', async (req, res) => {
|
|
138 |
}
|
139 |
});
|
140 |
|
|
|
141 |
app.get('/v2/listuser', (req, res) => {
|
142 |
-
const userList = Object.keys(
|
143 |
res.status(200).json({ userList });
|
144 |
});
|
145 |
|
146 |
app.get('/v2/get/message', (req, res) => {
|
147 |
const id = req.query.id;
|
148 |
if (!id) return res.status(400).json({ success: false, response: "input id" });
|
149 |
-
if (!
|
150 |
return res.status(404).json({ error: 'User not found' });
|
151 |
}
|
152 |
-
const userMessages =
|
153 |
res.status(200).json({ messages: userMessages });
|
154 |
});
|
155 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
156 |
app.listen(port, () => {
|
157 |
-
|
158 |
-
});
|
|
|
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 |
app.use(bodyParser.json());
|
10 |
|
11 |
+
function textToBase64(text) {
|
12 |
return Buffer.from(text).toString('base64');
|
13 |
}
|
14 |
|
15 |
+
// Fungsi untuk mengembalikan teks dari Base64
|
16 |
+
function base64ToText(base64) {
|
17 |
return Buffer.from(base64, 'base64').toString('utf-8');
|
18 |
}
|
19 |
|
20 |
+
const { GoogleGenerativeAI } = require("@google/generative-ai");
|
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() * charactersLength));
|
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 |
+
const text = response.text();
|
43 |
+
|
44 |
return text;
|
45 |
} catch (error) {
|
46 |
console.error(error);
|
|
|
47 |
}
|
48 |
}
|
49 |
|
50 |
+
async function cleanInactiveUsersV2() {
|
51 |
const currentTime = DateTime.local();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
+
Object.keys(db_chatHistory).forEach((userId) => {
|
54 |
+
const user = db_chatHistory[userId];
|
55 |
|
56 |
+
if (user && user.lastChat && user.lastChat.plus(Duration.fromObject({ minutes: 20 })) < currentTime) {
|
57 |
+
// User's last message was more than 10 minutes ago, remove the user from db_chatHistory
|
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) => {
|
70 |
try {
|
|
|
76 |
});
|
77 |
}
|
78 |
|
79 |
+
let chatData = db_chatHistory[id];
|
80 |
|
81 |
if (!chatData) {
|
82 |
+
// Jika data riwayat obrolan tidak ada, buat data baru
|
83 |
chatData = {
|
84 |
lastChat: DateTime.local(),
|
85 |
data: {
|
86 |
+
history: [
|
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 |
+
// Simpan pesan user ke riwayat obrolan
|
106 |
+
chatData.data.history.push({
|
107 |
+
role: 'user',
|
108 |
+
parts: [{text: message}]
|
109 |
+
});
|
110 |
+
// Simpan bot response ke riwayat obrolan
|
111 |
+
chatData.data.history.push({
|
112 |
+
role: 'model',
|
113 |
+
parts: [{text: botResponse}]
|
114 |
+
});
|
115 |
+
// Update waktu terakhir obrolan
|
116 |
chatData.lastChat = DateTime.local();
|
117 |
+
db_chatHistory[id] = chatData;
|
|
|
|
|
118 |
res.status(200).json({
|
119 |
success: true,
|
120 |
response: botResponse
|
|
|
127 |
}
|
128 |
});
|
129 |
|
130 |
+
|
131 |
app.get('/v2/listuser', (req, res) => {
|
132 |
+
const userList = Object.keys(db_chatHistory);
|
133 |
res.status(200).json({ userList });
|
134 |
});
|
135 |
|
136 |
app.get('/v2/get/message', (req, res) => {
|
137 |
const id = req.query.id;
|
138 |
if (!id) return res.status(400).json({ success: false, response: "input id" });
|
139 |
+
if (!db_chatHistory[id]) {
|
140 |
return res.status(404).json({ error: 'User not found' });
|
141 |
}
|
142 |
+
const userMessages = db_chatHistory[id].data.history;
|
143 |
res.status(200).json({ messages: userMessages });
|
144 |
});
|
145 |
|
146 |
+
// Fungsi untuk ping website
|
147 |
+
async function pingWebsite() {
|
148 |
+
const browser = await puppeteer.launch({
|
149 |
+
headless: true,
|
150 |
+
args: ['--no-sandbox', '--disable-setuid-sandbox']
|
151 |
+
});
|
152 |
+
const page = await browser.newPage();
|
153 |
+
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;]");
|
154 |
+
await page.goto('https://huggingface.co/spaces/Nexchan/gptdemo');
|
155 |
+
console.log("Ping");
|
156 |
+
await browser.close();
|
157 |
+
}
|
158 |
+
|
159 |
+
// Ping website setiap 5 jam
|
160 |
+
async function pingEvery5Hours() {
|
161 |
+
await pingWebsite();
|
162 |
+
setInterval(async () => {
|
163 |
+
await pingWebsite();
|
164 |
+
}, 5 * 60 * 60 * 1000); // 5 jam dalam milidetik
|
165 |
+
}
|
166 |
+
|
167 |
+
// Mulai ping
|
168 |
+
pingEvery5Hours();
|
169 |
+
|
170 |
app.listen(port, () => {
|
171 |
+
console.log(`Server is running on port ${port}`);
|
172 |
+
});
|