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