-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
459 lines (422 loc) · 18.5 KB
/
Copy pathindex.js
File metadata and controls
459 lines (422 loc) · 18.5 KB
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
import express from 'express';
import * as dotenv from 'dotenv';
import PaLM from 'palm-api';
import admin from 'firebase-admin';
import puppeteer from 'puppeteer';
import fs from 'fs'
import path from 'path'
import axios from 'axios'
dotenv.config();
import { GoogleGenerativeAI, HarmCategory,
HarmBlockThreshold, } from "@google/generative-ai";
const config = {
"type": "service_account",
"project_id": "plam-97af6",
"private_key_id": "4ea37e741821142659d569932e7807aaa50c71a1",
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCmjHBK+Avwd+Zh\nmk8M3RDWb0O+eUeS2RiP7Y6kKGg4sFafu5WUyLlDRCWi2IwnNxiskp7wsUYTif2r\n7srqTgosGYnQmy0fXmfwrhyBJvkr37dMwjSSDKF7+D9jWx2i/XKb/KhuF72rrI8p\nHkBeUoakvTdhBDIonYct53owNJzYnv8y7EGVca2tjMG4EbGBp5kktGRfQ752f/3P\nNLwQjVzTmYu+4Y3W2xRz3mKEbw4dW0DptHVYD0MufK0S19ce4KuoZDS+MZpkdfz0\nXpreNIADKfY7IA3wyJ0CF0xx/ilw6hNGp1UwUs6xSnGUA8WzdWHychsC/nDVJMyJ\nIxiQ+L6RAgMBAAECggEACpESds8yArm4tiySI2DLNq+sfqM3NUvs0QTjberaYe5e\nMJg4L05k4q2qLaDLk5ldSWWsC91Tct3oaEYoPaFh59Ze9gtCW6EjuxNw34jXej8h\nZA9WQoXmoPTpIKahRLwLz3XCQenjsMar9pHix5k4za7rmo4nmIgWEYWELRbJdLzX\nT4KLcw4GPz3x1NWWr7zMdQx/w56XMgInMhBJDYqg02ntKuZRIuULkglO9KBJXsMh\nDnm1U5ej0/2JPYvT4WoAhJ7pAqmEuzY6AATT4T3seXizdkFOVnh6f74VWtlFaOkx\nH3RLcedP3x/QQ9WW53FaImEh1sDKDYp+LoLiGrlDIQKBgQDqJVakyk/Bxyb3+Szi\nFVTEJYbWhaE7uPgTgv70evCSUYSoryrPbXi5VoqE808zqZSvjwRc2w9Qj6G7AaCs\nUiuK9pXkYnULMtp2P+OQS8vbFpJwmi+3ctbOKZ2YbOuYlG0IOU50HqmJMFJTSSVr\n4sjsmvjVgbbzdRf0/jLFScQonQKBgQC2F+/P620zXTZQiCJI4Hj+ArCk3onVU+uU\n0/Ajk+h/RPWynjbNmx65vVm3B4FBFUysl3hTwoMfloVpz+M5QNUGLpP4V6P4LxX4\n1krHNlCmcGX2u12lb5xKtPnxh832j33HqCpHQn/pXwDGhKDcRp9PEBmUz80qe/ft\nB5wveECphQKBgDeLkglXwSHsF6992grsGobJjDo5yyz6zUb7rXXb8FQVqI3HuDUq\n+BenkwbFHYXY9d/L4rIQeYqX+Dr/wNkASToXEynxvWhhsPeaYmapxHt92FGEEkvH\nn3Oa5bryI9W3aEIqLhVX7MoRv4XodUspqpaOkPc9ZWRN49gHsY2h2Tn1AoGAdT2s\nNh7KzItzcR51lJ6SyFlLCpwN+sBxqf1bXEzbITZxS3P6rR73BXgYz3ORkSn5mBZe\nbu+KJUhw1dP35OENR1GuC/PBqwYGC5VNyD38Z21u+RFiFiZqZt9nHk/mGJ4Cyg5I\nSmFSoht9EFRMjgcag3lGPz4cRwJSetavhRDT8VECgYATTXdUW9/LUI7+TIR13rk2\nd9Kr3JFzTJlRdBaQGL4c2UFrIIu4J6Hx6H7GWc6JkkDhNRrGBvhfxrGBIrHte774\nzcASLY3YBMYjE6xZXwgAklea9439CP/8d4PbJRtZ1nbaQ85o0j6GemV2TwotH26p\nq7X8f5aUv6zZBJBApfRQHQ==\n-----END PRIVATE KEY-----\n",
"client_email": "firebase-adminsdk-o0xv5@plam-97af6.iam.gserviceaccount.com",
"client_id": "104078289980871485358",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-o0xv5%40plam-97af6.iam.gserviceaccount.com",
"universe_domain": "googleapis.com"
}
async function scrape(text,n){
const browser = await puppeteer.launch({
headless:true,
args: [
"--disable-setuid-sandbox",
"--no-sandbox",
"--single-process",
"--no-zygote",
],
executablePath:
process.env.NODE_ENV === "production"
? process.env.PUPPETEER_EXECUTABLE_PATH
: puppeteer.executablePath(),
});
const page = await browser.newPage();
// Navigate to the page that will perform the tests.
await page.goto('https://dalle-mini-dalle-mini.static.hf.space/index.html');
//type
await page.waitForSelector('input[type="text"]',{timeout: 600000});
await page.type('input[type="text"]', text);
await page.click('.gr-button')
//get all images from a div named "images"
await page.waitForSelector('div.grid-cols-3 button img',{timeout: 600000});
const images = await page.$$eval('div.grid-cols-3 button img', imgs => {
for(let i = 0; i <n || 9;i++){
imgs.map(img => img.src)
}
},{timeout: 600000});
await browser.close();
return images;
}
console.log('2')
const app = express();
const port = process.env.PORT || 2000 ; // Define the port you want to use
console.log('3')
admin.initializeApp({
credential: admin.credential.cert(config),
databaseURL: 'https://plam-97af6-default-rtdb.firebaseio.com', // Replace with your Firestore database URL
});
const db = admin.firestore();
const collectionRef = db.collection('users');
// Establish the database connection using async/await
app.use(express.json()); // Middleware for parsing JSON request bodies
app.get('/', async (req, res) => {
const headers = req.headers;
if (!headers['userid']) return res.status(400).send('Userid is required.');
if (!headers['key']) return res.status(400).send('Please enter your makersuite key');
if (!headers['message']) return res.status(400).send('Please enter your message');
if (headers['psid']) return res.status(400).send('Looks like you are using old version of the API. Please update to the latest version. Please read our docuentation for more information.');
const documentRef = collectionRef.doc(headers['userid']);
documentRef.get()
.then(async (docSnapshot) => {
if (docSnapshot.exists) {
const data = docSnapshot.data();
let arrayData;
if(data.premium == true && headers['roomid']){
console.log("We are looking for your roomid")
console.log(data.room)
let yes;
for(let i=0;i<data.room.length;i++){
console.log(i)
console.log(data.room[i].roomid)
if(data.room[i].roomid == headers['roomid'].toString()){
yes = true
arrayData = data.room[i].chats
console.log("We found your roomid")
}
}
if(!yes){
console.log("We did not find your roomid")
const newroom = req.headers.roomid;
const documentRef = collectionRef.doc(userid);
documentRef.get().then((docSnapshot) => {
if(docSnapshot.exists){
const data = docSnapshot.data();
if(data.premium == true){
for(let i=0;i<data.room.length;i++){
if(data.room[i].roomid == newroom){
}
}
let array = [data.room] || [];
const newd = {
roomid:newroom,
chats:[]
}
console.log(array)
array.push(newd)
collectionRef.doc(userid).update({
room: array,
}).then(() => {
console.log('Created')
}
).catch((error) => {
console.error('Error updating document: ', error);
});
}else{
let data = {response:'Looks like you are not a premium user. Please considure upgrading.', code:200}
res.json(data)
}
}
});
}
}else{
arrayData = data.chats
}
// Replace with your actual field name
const bot = new PaLM(headers['key']);
let chat;
if (data.premium == true) {
chat = bot.createChat({ messages: arrayData })
} else {
chat = bot.createChat()
}
const toask = await chat.ask(headers['message']);
let datsa = { response: toask, code: 200 }
res.json(datsa)
if(data.premium == true){
let doc1 = [{ content: headers['message'] }, { content: toask }]
for (let i = 0; i < doc1.length; i++) {
arrayData.push(doc1[i])
}
if(data.premium == true){
if(headers['roomid']){
collectionRef.doc(headers['userid']).update({
['room.' + headers['roomid'].toString() + '.chats']: arrayData,
}).then(() => {
console.log('Document successfully updated!');
}
).catch((error) => {
console.error('Error updating document: ', error);
});
}
}else{
collectionRef.doc(headers['userid']).update({
chats: arrayData,
}).then(() => {
console.log('Document successfully updated!');
}
).catch((error) => {
console.error('Error updating document: ', error);
});
}
}
} else {
let data = { response: 'You are not registered', code: 503 }
res.json(data)
console.log('Document does not exist');
}
})
.catch((error) => {
let data = { response: 'Error getting document:'+error, code: 400 }
res.json(data)
console.error('Error getting document:', error);
});
});
app.get('/register', async (req, res) => {
//get all data length
const headers = req.headers;
const snapshot = await collectionRef.count().get();
let newData ={}
if(snapshot.data().count<=120){
newData = {
chats: [],
premium:true,
room:[
{
roomid:12345,
chats:[]
},
{
roomid:123456,
chats:[]
}
]
};
}else{
const currentDate = new Date();
// Set the date to the first day of the next month
currentDate.setMonth(currentDate.getMonth() + 1, 1);
newData = {
chats: [],
premium:false,
images:900,
resetDate:currentDate.toDateString(),
room:[
{
roomid:12345,
chats:[]
},
{
roomid:123456,
chats:[]
}
]
};
}
collectionRef.add(newData)
.then((docRef) => {
let data = { response: 'User registered', code: 200 , newid: docRef.id}
res.json(data)
})
.catch((error) => {
console.error('Error adding document: ', error);
});
})
app.get('/ping', async (req, res) => {
//get actual ping
let data = { response: 'pong', code: 200 }
res.json(data)
})
app.get('/images', async (req, res) => {
const text = req.headers.prompt;
if(!text) return res.status(400).send('Text is required.');
//check if the user is premium
const documentRef = collectionRef.doc(req.headers['userid']);
documentRef.get().then(async (docSnapshot) => {
if(docSnapshot.exists){
const data = docSnapshot.data();
if(data.premium == true){
const images = await scrape(text, req.headers['number'] | 9);
let data = {response:images, code:200}
res.json(data)
}else{
if(data.resetDate == new Date().getMonth().toString()){
documentRef.get().then((docSnapshot) => {
if(docSnapshot.exists){
const data = docSnapshot.data();
collectionRef.doc(req.headers['userid']).update({
images: 600,
resetDate: new Date().getMonth().toDateString()
}).then(() => {
console.log('Document successfully updated!');
}
).catch((error) => {
console.error('Error updating document: ', error);
});
}
});
}
if(data.images>0){
const images = await scrape(text);
let data = {response:images, code:200}
//update user
const documentRef = collectionRef.doc(req.headers['userid']);
documentRef.get().then((docSnapshot) => {
if(docSnapshot.exists){
const data = docSnapshot.data();
collectionRef.doc(req.headers['userid']).update({
images: data.images - headers['number'] | 9,
}).then(() => {
res.json(data);
console.log('Document successfully updated!');
}
).catch((error) => {
res.json({response:'Error updating document: '+error, code:400});
console.error('Error updating document: ', error);
});
}
});
}else{
res.json({response:'You have reached your limit. Please upgrade to premium to get unlimited images.', code:200});
}
}
}
});
});
app.get('/update', async (req,res) =>{
const userid = req.headers.userid;
//get every next moth date
// Get the current date
const currentDate = new Date();
// Set the date to the first day of the next month
currentDate.setMonth(currentDate.getMonth() + 1, 1);
// Display the result
const newData = {
images:900,
resetDate:currentDate.toDateString()
}
});
app.get("/v1/gemini/:model", async (req,res) =>{
const model = req.params.model;
if(!model) return res.json({response:'model required'})
if(model != "gemini-pro" && model != "gemini-pro-vision") return res.json({response:'Not a valid model'});
const headers = req.headers;
if(model == "gemini-pro"){
try{
if(!headers['api_key']) return res.json({response:'Please enter a api key'})
if(!headers['text']) return res.json({response:'Pleae enter your question'})
const genAI = new GoogleGenerativeAI(headers['api_key']);
const model = genAI.getGenerativeModel({ model: 'gemini-pro'});
const generationConfig = {
temperature: headers['temperature'] || 1,
topK: headers['top_k'] || 1,
topP: headers['top_p'] || 1,
maxOutputTokens: headers['maxOutputTokens']|| 2048,
};
const safetySettings = [
{
category: HarmCategory.HARM_CATEGORY_HARASSMENT,
threshold: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
},
{
category: HarmCategory.HARM_CATEGORY_HATE_SPEECH,
threshold: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
},
{
category: HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT,
threshold: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
},
{
category: HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
threshold: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
},
];
const parts = [
{text: headers['text']},
];
const result = await model.generateContent({
contents: [{ role: "user", parts }],
generationConfig,
safetySettings,
});
const response = result.response;
res.json({response:response.text(), code:200});
}catch(e){
return res.json({response:'Error: '+e, code:400});
}
}
if(model == "gemini-pro-vision"){
try{
const headers = req.headers;
if(!headers['api_key']) return res.json({response:'Please enter a api key'})
if(!headers['userid']) return res.json({response:'Please enter a userid'})
if(!headers['text']) return res.json({response:'Pleae enter your question'})
if(!headers['image']) return res.json({response:"Please enter a image url"})
const genAI = new GoogleGenerativeAI(headers['api_key']);
const model = genAI.getGenerativeModel({ model: 'gemini-pro-vision'});
const documentRef = collectionRef.doc(headers['userid']);
if(!documentRef) return res.json({response:'User not found', code:400}).status(400);
documentRef.get().then(async (docSnapshot) => {
if(docSnapshot.exists){
const data = docSnapshot.data();
if(data.experimental == true){
const generationConfig = {
temperature: headers['temperature'] || 1,
topK: headers['top_k'] || 1,
topP: headers['top-p'] || 1,
maxOutputTokens: headers['maxOutputTokens']|| 2048,
};
const safetySettings = [
{
category: HarmCategory.HARM_CATEGORY_HARASSMENT,
threshold: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
},
{
category: HarmCategory.HARM_CATEGORY_HATE_SPEECH,
threshold: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
},
{
category: HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT,
threshold: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
},
{
category: HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
threshold: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
},
];
const response = await axios.get(headers['image'], { responseType: 'arraybuffer' });
const base64Data = Buffer.from(response.data, 'binary').toString('base64');
const parts = [
{text: headers['text']+"\n"},
{
inlineData: {
mimeType: "image/jpeg",
data: base64Data
}
},
];
const result = await model.generateContent({
contents: [{ role: "user", parts }],
generationConfig,
safetySettings,
});
const responses = result.response;
res.json({response:responses.text(), code:200});
}else{
return res.status(400).send('You are not a experimental user.');
}
}
});
}catch(e){
return res.json({response:'Error: '+e, code:400});
}
}
})
app.listen(port, () => {
console.log(`Server is running on port ${port} . you can now send requests.`);
});