Khrisna commited on
Commit
87c3c2a
·
1 Parent(s): f7f0a4c

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +51 -7
index.js CHANGED
@@ -6,6 +6,8 @@ const express = require('express')
6
  const PDFDocument = require('pdfkit')
7
  const axios = require("axios")
8
  const FormData = require("form-data")
 
 
9
  //const { BingChat } = (await import("bing-chat")).default
10
  const apikey = "@SadTeam77"
11
 
@@ -150,6 +152,36 @@ app.post('/api/waifu2x', async (req, res) => {
150
  res.json({ error: true, message: e === '[object Object]' ? 'Internal Server Error' : e })
151
  }
152
  })
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
153
  /*app.post('/api/bingchat', async (req, res) => {
154
  try {
155
  console.log(req.body)
@@ -179,7 +211,13 @@ app.listen(PORT, () => console.log('App running on port', PORT))
179
  function formatSize(num) {
180
  return bytes(+num || 0, { unitSeparator: ' ' })
181
  }
182
-
 
 
 
 
 
 
183
  function toPDF(urls) {
184
  return new Promise(async (resolve, reject) => {
185
  try {
@@ -291,10 +329,16 @@ async function waifu2x(image, formats) {
291
  })
292
  return images.data
293
  }
294
- function isBase64(str) {
295
- try {
296
- return btoa(atob(str)) === str
297
- } catch {
298
- return false
299
- }
 
 
 
 
 
 
300
  }
 
6
  const PDFDocument = require('pdfkit')
7
  const axios = require("axios")
8
  const FormData = require("form-data")
9
+ const tfjs = require('@tensorflow/tfjs-node')
10
+ const nsfwjs = require('nsfwjs')
11
  //const { BingChat } = (await import("bing-chat")).default
12
  const apikey = "@SadTeam77"
13
 
 
152
  res.json({ error: true, message: e === '[object Object]' ? 'Internal Server Error' : e })
153
  }
154
  })
155
+ app.post('/api/nsfw-check', async (req, res) => {
156
+ try {
157
+ console.log(req.body)
158
+ const { images, status } = req.body
159
+ if (!images) return res.json({ success: false, message: 'Required an images!' })
160
+ if (!status) return res.json({ success: false, message: 'Required an status text!' })
161
+
162
+ if(status !== apikey) return res.json({ success: false, message: 'Invalid status!' })
163
+ if (/^(https?|http):\/\//i.test(images)) {
164
+ const data_img = await axios.request({
165
+ method: "GET",
166
+ url: images,
167
+ responseType: "arraybuffer"
168
+ })
169
+ const response = await check_nsfw(data_img.data)
170
+ res.json(response)
171
+ } else if (images && typeof images == 'string' && isBase64(images)) {
172
+ const response = await check_nsfw(Buffer.from(images, "base64"))
173
+ res.json(response)
174
+ } else {
175
+ res.json({
176
+ success: false, message: 'No url or base64 detected!!'
177
+ })
178
+ }
179
+ } catch (e) {
180
+ console.log(e)
181
+ e = String(e)
182
+ res.json({ error: true, message: e === '[object Object]' ? 'Internal Server Error' : e })
183
+ }
184
+ })
185
  /*app.post('/api/bingchat', async (req, res) => {
186
  try {
187
  console.log(req.body)
 
211
  function formatSize(num) {
212
  return bytes(+num || 0, { unitSeparator: ' ' })
213
  }
214
+ function isBase64(str) {
215
+ try {
216
+ return btoa(atob(str)) === str
217
+ } catch {
218
+ return false
219
+ }
220
+ }
221
  function toPDF(urls) {
222
  return new Promise(async (resolve, reject) => {
223
  try {
 
329
  })
330
  return images.data
331
  }
332
+ async function check_nsfw(buffer) {
333
+ const model = await nsfwjs.load() // To load a local model, nsfw.load('file://./path/to/model/')
334
+ // Image must be in tf.tensor3d format
335
+ // you can convert image to tf.tensor3d with tf.node.decodeImage(Uint8Array,channels)
336
+ const image = await tfjs.node.decodeImage(buffer,3)
337
+ const predictions = await model.classify(image)
338
+ image.dispose() // Tensor memory must be managed explicitly (it is not sufficient to let a tf.Tensor go out of scope for its memory to be released).
339
+ return prediction.map(v => {
340
+ class_name: v.className,
341
+ probability: v.probability,
342
+ probability_percent: (v.probability * 100).toFixed(2)
343
+ })
344
  }