randydev commited on
Commit
8dff905
·
verified ·
1 Parent(s): 55ae3b6

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +57 -0
index.js CHANGED
@@ -31,6 +31,8 @@ import { ApiKey } from './models.js';
31
  import { Readable } from "stream";
32
  import { randomBytes } from "crypto";
33
  import { AkenoaiJs } from "akenoaijs";
 
 
34
 
35
  import {
36
  CheckMilWare,
@@ -131,11 +133,66 @@ const AllJsonReques = {
131
  },
132
  }
133
 
 
 
 
 
 
 
 
 
 
 
134
 
135
  app.get("/api/v1/json/all", async (req, res) => {
136
  res.json(AllJsonReques);
137
  });
138
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
139
  app.get('/broadcast-user', async (req, res) => {
140
  const dbClient = new Database("AkenoXJs");
141
  const collection = dbClient.collection("api_keys");
 
31
  import { Readable } from "stream";
32
  import { randomBytes } from "crypto";
33
  import { AkenoaiJs } from "akenoaijs";
34
+ import tf from '@tensorflow/tfjs-node';
35
+ import nsfwjs from 'nsfwjs';
36
 
37
  import {
38
  CheckMilWare,
 
133
  },
134
  }
135
 
136
+ let model;
137
+
138
+ nsfwjs.load("https://raw.githubusercontent.com/infinitered/nsfwjs/master/models/inception_v3/model.json", { size: 299 })
139
+ .then((loadedModel) => {
140
+ model = loadedModel;
141
+ console.log('Model loaded');
142
+ })
143
+ .catch((error) => {
144
+ console.error('Error loading model:', error);
145
+ });
146
 
147
  app.get("/api/v1/json/all", async (req, res) => {
148
  res.json(AllJsonReques);
149
  });
150
 
151
+ app.get('api/v1/nsfw', async (req, res) => {
152
+ try {
153
+ const { url } = req.query;
154
+ if (!url) {
155
+ return res.status(400).json({ message: 'Invalid URL.' });
156
+ }
157
+
158
+ const response = await axios.get(url, { responseType: 'arraybuffer' });
159
+ const imageBuffer = Buffer.from(response.data);
160
+
161
+ let imageTensor;
162
+
163
+ if (url.endsWith('.gif')) {
164
+ const jpgBuffer = await sharp(imageBuffer)
165
+ .resize({ width: 299, height: 299 })
166
+ .toFormat('jpeg')
167
+ .toBuffer();
168
+
169
+ imageTensor = tf.node.decodeImage(jpgBuffer, 3);
170
+ } else {
171
+ imageTensor = tf.node.decodeImage(imageBuffer, 3);
172
+ }
173
+
174
+ const predictions = await model.classify(imageTensor);
175
+ imageTensor.dispose();
176
+
177
+ const formattedPredictions = predictions.reduce((acc, { className, probability }) => {
178
+ acc[className] = probability;
179
+ return acc;
180
+ }, {});
181
+
182
+ res.json(formattedPredictions);
183
+ } catch (error) {
184
+ console.error('Error processing image:', error);
185
+
186
+ if (error.response) {
187
+ return res.status(error.response.status).json({ message: 'Error fetching image from URL.', details: error.message });
188
+ } else if (error.code === 'ERR_INVALID_URL') {
189
+ return res.status(400).json({ message: 'Invalid image URL.', details: error.message });
190
+ } else {
191
+ return res.status(500).json({ message: 'Internal server error.', details: error.message });
192
+ }
193
+ }
194
+ });
195
+
196
  app.get('/broadcast-user', async (req, res) => {
197
  const dbClient = new Database("AkenoXJs");
198
  const collection = dbClient.collection("api_keys");