apexify.js 3.3.10 → 3.3.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/ai/ApexAI.ts CHANGED
@@ -10,16 +10,16 @@ import {
10
10
  typeWriter,
11
11
  readImage,
12
12
  } from "./utils";
13
+ import axios from "axios";
13
14
  import {
14
15
  ModalBuilder,
15
16
  TextInputBuilder,
16
17
  TextInputStyle,
17
- ActionRowBuilder
18
+ ActionRowBuilder
18
19
  } from "discord.js";
19
20
  import { filters } from "./buttons/tools";
20
21
  import { imageTools } from "./buttons/drawMenu";
21
22
 
22
-
23
23
  export interface Options {
24
24
  voice?: {
25
25
  textVoice?:{
@@ -52,7 +52,7 @@ export interface Options {
52
52
  };
53
53
  };
54
54
  others?: {
55
- messageType: {
55
+ messageType?: {
56
56
  type: string;
57
57
  intialContent: string;
58
58
  };
@@ -405,6 +405,7 @@ export async function ApexAI (message: any, aiOptions: Options) {
405
405
  await typeWriter(message.channel, part, speed, delay);
406
406
  }
407
407
  }
408
+
408
409
  } else {
409
410
  if (response.length <= 2000) {
410
411
  await message.reply({
@@ -477,139 +478,106 @@ export async function ApexAI (message: any, aiOptions: Options) {
477
478
  }
478
479
 
479
480
  export async function gemmaAi_4(prompt: string) {
480
- try {
481
- const response = await fetch('https://api-inference.huggingface.co/models/google/gemma-7b-it', {
482
- method: 'POST',
483
- headers: {
484
- 'Content-Type': 'application/json',
485
- 'Authorization': 'Bearer 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'
486
- },
487
- body: JSON.stringify({ inputs: prompt })
488
- });
489
-
490
- if (!response.ok) {
491
- throw new Error('Network response was not ok');
481
+ try {
482
+ const response = await axios.post('https://api-inference.huggingface.co/models/google/gemma-7b-it', { inputs: prompt }, {
483
+ headers: { 'Authorization': `Bearer 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` }
484
+ });
485
+ return response.data[0].generated_text;
486
+ } catch (error: any) {
487
+ console.error('Error fetching response:', error.message);
488
+ return null;
492
489
  }
493
-
494
- const responseData = await response.json();
495
- return responseData[0].generated_text;
496
- } catch (error: any) {
497
- console.error('Error fetching response:', error.message);
498
- return null;
499
- }
500
490
  }
501
491
 
502
492
  export async function gemmaAi_3(prompt: string) {
503
- try {
504
- const response = await fetch('https://api-inference.huggingface.co/models/google/gemma-2b-it', {
505
- method: 'POST',
506
- headers: {
507
- 'Content-Type': 'application/json',
508
- 'Authorization': 'Bearer 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'
509
- },
510
- body: JSON.stringify({ inputs: prompt })
511
- });
512
-
513
- if (!response.ok) {
514
- throw new Error('Network response was not ok');
515
- }
516
-
517
- const responseData = await response.json();
518
- return responseData[0].generated_text;
519
- } catch (error: any) {
520
- console.error('Error fetching response:', error.message);
521
- return null;
522
- }
493
+ try {
494
+ const response = await axios.post('https://api-inference.huggingface.co/models/google/gemma-2b-it', { inputs: prompt }, {
495
+ headers: { 'Authorization': `Bearer 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` }
496
+ });
497
+ return response.data[0].generated_text;
498
+ } catch (error: any) {
499
+ console.error('Error fetching response:', error.message);
500
+ return null;
501
+ }
523
502
  }
503
+
524
504
  export async function apexai(prompt: string) {
525
- try {
505
+ try {
526
506
  const messages = [
527
- {"role": "user", "content": `${prompt}`}
507
+ {"role": "user", "content": `${prompt}`}
528
508
  ];
529
- const formattedMessages = messages.map(message => `[${message.role}] ${message.content}`).join('\n');
530
-
531
- const response = await fetch('https://api-inference.huggingface.co/models/mistralai/Mixtral-8x7B-Instruct-v0.1', {
532
- method: 'POST',
533
- headers: {
534
- 'Authorization': 'Bearer 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',
535
- 'Content-Type': 'application/json'
536
- },
537
- body: JSON.stringify({ inputs: formattedMessages })
538
- });
509
+ const formattedMessages = messages.map(message => `[${message.role}] ${message.content}`).join('\n');
510
+
511
+ const response = await axios.post(`https://api-inference.huggingface.co/models/mistralai/Mixtral-8x7B-Instruct-v0.1`, {
512
+ inputs: formattedMessages
513
+ }, {
514
+ headers: {
515
+ 'Authorization': `Bearer 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`,
516
+ 'Content-Type': 'application/json'
517
+ }
518
+ });
539
519
 
540
- if (!response.ok) {
541
- throw new Error('Network response was not ok');
542
- }
520
+ const generatedText = response.data[0].generated_text;
543
521
 
544
- const responseData = await response.json();
545
- const generatedText = responseData[0].generated_text;
546
- const lines = generatedText.split('\n').slice(1);
547
- const output = lines.join('\n');
522
+ const lines = generatedText.split('\n').slice(1);
548
523
 
549
- return output;
550
- } catch (error: any) {
551
- console.error('Error:', error.message);
552
- return 'Please wait i am on cooldown.';
553
- }
524
+ const output = lines.join('\n');
525
+
526
+ return output
527
+ } catch (error: any) {
528
+ console.error('Error:', error.response.data);
529
+ return 'Please wait i am on cooldown.'
530
+ }
554
531
  }
555
532
 
556
533
  export async function starChat(prompt: string) {
557
- const messages = [{"role":"user","content": `${prompt}`}];
558
-
559
- try {
560
- const response = await fetch('https://api-inference.huggingface.co/models/HuggingFaceH4/starchat2-15b-v0.1', {
561
- method: 'POST',
562
- headers: {
563
- 'Authorization': 'Bearer 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',
564
- 'Content-Type': 'application/json'
565
- },
566
- body: JSON.stringify({ inputs: messages })
567
- });
568
-
569
- if (!response.ok) {
570
- throw new Error('Network response was not ok');
571
- }
534
+ const messages = [{"role":"user","content": `${prompt}`}]
535
+
536
+ try {
537
+ const response = await axios.post('https://api-inference.huggingface.co/models/HuggingFaceH4/starchat2-15b-v0.1', {
538
+ inputs: JSON.stringify(messages),
539
+ }, {
540
+ headers: {
541
+ 'Authorization': `Bearer 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`,
542
+ },
543
+ });
572
544
 
573
- const responseData = await response.json();
574
- const chatbotReply = responseData[0];
575
- const chatbotResponseText = chatbotReply.generated_text.replace(/^.*?\n.*?\n/, '');
576
- const chatbotResponseArray = JSON.parse(chatbotResponseText);
577
- const chatbotResponseString = chatbotResponseArray.join(' ');
545
+ const chatbotReply = response.data[0];
546
+ const chatbotResponseText = chatbotReply.generated_text.replace(/^.*?\n.*?\n/, '');
547
+ const chatbotResponseArray = JSON.parse(chatbotResponseText);
548
+ const chatbotResponseString = chatbotResponseArray.join(' ');
578
549
 
579
- return chatbotResponseString;
580
- } catch (error: any) {
581
- console.error('Error:', error.message);
582
- return null;
583
- }
550
+ return chatbotResponseString;
551
+ } catch (error: any) {
552
+ console.error('Error fetching response:', error.message);
553
+ return null;
554
+ }
584
555
  }
585
556
 
586
557
  export async function zephyr_beta(prompt: string) {
587
- const messages = [{"role":"user","content": `${prompt}` }];
588
-
589
- try {
590
- const response = await fetch('https://api-inference.huggingface.co/models/HuggingFaceH4/zephyr-7b-beta', {
591
- method: 'POST',
592
- headers: {
593
- 'Authorization': 'Bearer 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',
594
- 'Content-Type': 'application/json'
595
- },
596
- body: JSON.stringify({ inputs: messages })
597
- });
598
558
 
599
- if (!response.ok) {
600
- throw new Error('Network response was not ok');
601
- }
559
+ const messages = [{"role":"user","content": `${prompt}` }]
560
+ try {
561
+ const response = await axios.post('https://api-inference.huggingface.co/models/HuggingFaceH4/zephyr-7b-beta', {
562
+ inputs: JSON.stringify(messages),
563
+ }, {
564
+ headers: {
565
+ 'Authorization': `Bearer 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`,
566
+ },
567
+ });
602
568
 
603
- const responseData = await response.json();
604
- const chatbotReply = responseData[0];
605
- const textParts = chatbotReply.generated_text.split('\n');
606
- const secondArrayString = textParts[2];
607
- const chatbotResponseArray = JSON.parse(secondArrayString);
608
- const chatbotResponseString = chatbotResponseArray.map((obj: any) => obj.content).join(' ');
609
-
610
- return chatbotResponseString;
611
- } catch (error: any) {
612
- console.error('Error:', error.message);
613
- return null;
614
- }
569
+ const chatbotReply = response.data[0];
570
+
571
+ const textParts = chatbotReply.generated_text.split('\n');
572
+
573
+ const secondArrayString = textParts[2];
574
+ const chatbotResponseArray = JSON.parse(secondArrayString);
575
+
576
+ const chatbotResponseString = chatbotResponseArray.map((obj: any) => obj.content).join(' ');
577
+
578
+ return chatbotResponseString;
579
+ } catch (error: any) {
580
+ console.error('Error fetching response:', error.message);
581
+ return null;
582
+ }
615
583
  }
@@ -1,6 +1,7 @@
1
1
  import translate from "@iamtraction/google-translate";
2
2
  import sharp from "sharp";
3
3
  import { ButtonBuilder, StringSelectMenuBuilder, StringSelectMenuOptionBuilder, ButtonStyle, ActionRowBuilder, AttachmentBuilder, AttachmentData } from "discord.js";
4
+ import axios from "axios";
4
5
  import api from "api";
5
6
 
6
7
  const sdk: any = api("@prodia/v1.3.0#be019b2kls0gqss3");
@@ -87,8 +88,8 @@ async function aiImagine(
87
88
  nsfw: boolean,
88
89
  nsfwKeyWords: string[],
89
90
  ) {
90
- const maxRetryAttempts = 3;
91
- const retryInterval = 10000;
91
+ const maxRetryAttempts = 4;
92
+ const retryInterval = 5000;
92
93
  let response: any;
93
94
  async function retry(fn: any, retriesLeft = maxRetryAttempts) {
94
95
  try {
@@ -97,6 +98,7 @@ async function aiImagine(
97
98
  if (retriesLeft === 0) {
98
99
  throw error;
99
100
  }
101
+
100
102
  await new Promise((resolve) => setTimeout(resolve, retryInterval));
101
103
  return retry(fn, retriesLeft - 1);
102
104
  }
@@ -159,45 +161,38 @@ async function aiImagine(
159
161
  let buffferedImage: any;
160
162
 
161
163
  if (imageModel === "v3") {
162
- const res = await retry(async () => {
163
- const resp = await fetch(response.url, {
164
- method: 'GET',
165
- headers: {
166
- 'Content-Type': 'application/octet-stream'
167
- }
168
- });
169
- if (!resp.ok) {
170
- throw new Error("Failed to fetch image data.");
171
- }
172
- return await resp.arrayBuffer();
173
- });
174
-
164
+ const res = await retry(() =>
165
+ axios.get(response.url, {
166
+ responseType: "arraybuffer",
167
+ }),
168
+ );
169
+
175
170
  await message.channel?.sendTyping();
176
-
177
- buffferedImage = Buffer.from(res, "binary");
178
-
171
+
172
+ buffferedImage = Buffer.from(res.data, "binary");
173
+
179
174
  const resizedImage = await sharp(buffferedImage)
180
- .resize({
181
- width: 0x320,
182
- })
183
- .toBuffer();
184
-
175
+ .resize({
176
+ width: 0x320,
177
+ })
178
+ .toBuffer();
179
+
185
180
  const attachment = {
186
- file: resizedImage,
187
- name: `image_${_0x4d7fb6 + 1}.png`,
181
+ file: resizedImage,
182
+ name: `image_${_0x4d7fb6 + 1}.png`,
188
183
  } as AttachmentData;
189
-
184
+
190
185
  attachData.push(attachment);
191
-
186
+
192
187
  const urlButton = new ButtonBuilder()
193
- .setStyle(ButtonStyle.Link)
194
- .setLabel(`Image ${_0x4d7fb6 + 1}`)
195
- .setURL(response.url);
196
-
188
+ .setStyle(ButtonStyle.Link)
189
+ .setLabel(`Image ${_0x4d7fb6 + 1}`)
190
+ .setURL(response.url);
191
+
197
192
  buttonsRow1.push(urlButton);
198
-
193
+
199
194
  imageUrls.push(response.url);
200
- } else {
195
+ } else {
201
196
  const imageUrl = response.url || response;
202
197
 
203
198
  await message.channel?.sendTyping();
@@ -392,45 +387,52 @@ async function checkJobStatus(jobId: number | string | any) {
392
387
 
393
388
  async function attemptImageCaptioning(imageUrl: string) {
394
389
  try {
395
- let retryCount = 0;
396
- const maxRetries = 3;
397
-
398
- const fetchData = async () => {
399
- try {
400
- const response = await fetch(`https://api-inference.huggingface.co/models/Salesforce/blip-image-captioning-base`, {
401
- method: 'POST',
402
- headers: {
403
- "Content-Type": "application/json",
404
- Authorization: `Bearer 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`,
405
- },
406
- body: JSON.stringify({ image: imageUrl })
407
- });
408
-
409
- if (response.ok) {
410
- return await response.json();
411
- } else {
412
- console.error(`Failed to fetch image captioning API: ${response.statusText}`);
413
- return null;
414
- }
415
- } catch (error: any) {
416
- console.error(`Error fetching data: ${error.message}`);
417
- throw error;
418
- }
419
- };
390
+ let retryCount = 0;
391
+ const maxRetries = 3;
420
392
 
421
- while (retryCount < maxRetries) {
422
- try {
423
- return await fetchData();
424
- } catch (error: any) {
425
- retryCount++;
426
- }
393
+ const fetchData = async () => {
394
+ try {
395
+ const response = await axios.post(
396
+ `https://api-inference.huggingface.co/models/Salesforce/blip-image-captioning-base`,
397
+ { image: imageUrl },
398
+ {
399
+ headers: {
400
+ "Content-Type": "application/json",
401
+ Authorization: `Bearer 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`,
402
+ },
403
+ },
404
+ );
405
+
406
+ if (response.status === 200) {
407
+ return response.data[0].generated_text;
408
+ } else {
409
+ console.error(
410
+ `Failed to fetch image captioning API: ${response.statusText}`,
411
+ );
412
+ return null;
413
+ }
414
+ } catch (error: any) {
415
+ console.error(`Error fetching data: ${error.message}`);
416
+ throw error;
427
417
  }
418
+ };
428
419
 
429
- return null;
420
+ while (retryCount < maxRetries) {
421
+ try {
422
+ return await fetchData();
423
+ } catch (error: any) {
424
+ console.error(
425
+ `Error fetching data (Retry ${retryCount + 1}): ${error.message}`,
426
+ );
427
+ retryCount++;
428
+ }
429
+ }
430
+
431
+ return null;
430
432
  } catch (error: any) {
431
- console.error(`Error in attemptImageCaptioning: ${error.message}`);
432
- return null;
433
+ console.error(`Error in attemptImageCaptioning: ${error.message}`);
434
+ return null;
433
435
  }
434
436
  }
435
437
 
436
- export { aiImagine };
438
+ export { aiImagine };
@@ -1,5 +1,6 @@
1
1
  import { Hercai } from "hercai";
2
- import translate from "@iamtraction/google-translate";
2
+ import translate from "@iamtraction/google-translate";
3
+ import axios from "axios";
3
4
  import { createWorker, recognize } from "tesseract.js";
4
5
  import { chunkString } from "./chunkString";
5
6
  import { aiImagine } from "./draw";
@@ -7,7 +8,7 @@ import fs from "fs";
7
8
 
8
9
  const herc = new Hercai();
9
10
  let isProcessing = false;
10
- type ChatModelOption = "v3" | "v3-32k" | "turbo" | "turbo-16k" | "gemini";
11
+ type ChatModelOption = any | "v3" | "v3-32k" | "turbo" | "turbo-16k" | "gemini" ;
11
12
 
12
13
  async function aiVoice(
13
14
  message: any,
@@ -67,16 +68,20 @@ async function aiVoice(
67
68
  setTimeout(async () => {
68
69
  const encodedChunk = encodeURIComponent(chunk);
69
70
  const url = `https://translate.google.com/translate_tts?ie=UTF-8&tl=en&client=tw-ob&q=${encodedChunk}`;
70
- const response = await fetch(url, {
71
+ const mp3Stream = await axios.get(url, {
72
+ responseType: "stream",
71
73
  headers: {
72
74
  Referer: "http://translate.google.com/",
73
75
  "User-Agent": "stagefright/1.2 (Linux;Android 5.0)",
74
76
  },
75
77
  });
76
- const blob = await response.blob();
77
- const file = new File([blob], "respond.mp3");
78
78
  await message.reply({
79
- files: [file],
79
+ files: [
80
+ {
81
+ attachment: mp3Stream.data,
82
+ name: "respond.mp3",
83
+ },
84
+ ],
80
85
  allowedMentions: { repliedUser: false },
81
86
  });
82
87
  }, delay);
@@ -89,12 +94,13 @@ async function aiVoice(
89
94
 
90
95
  const options = {
91
96
  method: "POST",
97
+ url: "https://text-to-speech-neural-google.p.rapidapi.com/generateAudioFiles",
92
98
  headers: {
93
99
  "content-type": "application/json",
94
100
  "X-RapidAPI-Key": apiKey,
95
101
  "X-RapidAPI-Host": "text-to-speech-neural-google.p.rapidapi.com",
96
102
  },
97
- body: JSON.stringify({
103
+ data: {
98
104
  audioFormat: "ogg",
99
105
  paragraphChunks: [msg],
100
106
  voiceParams: {
@@ -102,13 +108,13 @@ async function aiVoice(
102
108
  engine: "google",
103
109
  languageCode: "en-US",
104
110
  },
105
- }),
111
+ },
106
112
  };
107
113
 
108
114
  try {
109
- const response = await fetch("https://text-to-speech-neural-google.p.rapidapi.com/generateAudioFiles", options);
110
- const audioData = await response.json();
111
- fs.writeFileSync("output.ogg", Buffer.from(audioData.audioStream, "base64"));
115
+ const response = await axios.request(options);
116
+ const audioData = response.data.audioStream;
117
+ fs.writeFileSync("output.ogg", Buffer.from(audioData, "base64"));
112
118
  await message.reply({
113
119
  files: [
114
120
  {
@@ -136,18 +142,19 @@ async function aiVoice(
136
142
 
137
143
  const options = {
138
144
  method: "POST",
145
+ url: "https://cloudlabs-text-to-speech.p.rapidapi.com/synthesize",
139
146
  headers: {
140
147
  "content-type": "application/x-www-form-urlencoded",
141
148
  "X-RapidAPI-Key": apiKey,
142
149
  "X-RapidAPI-Host": "cloudlabs-text-to-speech.p.rapidapi.com",
143
150
  },
144
- body: encodedParams.toString(),
151
+ data: encodedParams,
145
152
  };
146
153
 
147
154
  try {
148
- const response = await fetch("https://cloudlabs-text-to-speech.p.rapidapi.com/synthesize", options);
149
- const { result: { audio_url } } = await response.json();
150
- await message.reply(audio_url, {
155
+ const response = await axios.request(options);
156
+ const audioUrl = response.data.result.audio_url;
157
+ await message.reply(audioUrl, {
151
158
  allowedMentions: { repliedUser: false },
152
159
  });
153
160
  } catch (error) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "apexify.js",
3
- "version": "3.3.10",
3
+ "version": "3.3.11",
4
4
  "description": "Ai and Canvas library. Supports typescript and javascript",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
File without changes