Esteves Enzo commited on
Commit
2d49c67
·
1 Parent(s): ef3b17f

manage error + img ratio

Browse files
app/api/collections/route.ts CHANGED
@@ -15,5 +15,5 @@ export async function POST(request: Request) {
15
  }
16
  })
17
 
18
- return Response.json({ images, status: 200, statusText: "OK" })
19
  }
 
15
  }
16
  })
17
 
18
+ return Response.json({ images, status: 200, ok: true })
19
  }
app/api/pipeline.ts DELETED
@@ -1,21 +0,0 @@
1
- import { pipeline, env } from "@xenova/transformers";
2
-
3
- // Use the Singleton pattern to enable lazy construction of the pipeline.
4
- // NOTE: We wrap the class in a function to prevent code duplication (see below).
5
- process.env.HF_ACCESS_TOKEN = process.env.NEXT_PUBLIC_APP_HF_TOKEN
6
-
7
- const P = () => class PipelineSingleton {
8
- static task = 'image-classification';
9
- static model = 'enzostvs/nsfw-detector-transformersjs';
10
- static instance = null;
11
-
12
- static async getInstance(progress_callback: any = null) {
13
- if (this.instance === null) {
14
- this.instance = pipeline(this.task, this.model, { progress_callback }) as any;
15
- }
16
- return this.instance;
17
- }
18
- }
19
-
20
- const PipelineSingleton = P();
21
- export default PipelineSingleton;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/api/route.ts CHANGED
@@ -1,28 +1,26 @@
1
  import { PrismaClient } from '@prisma/client'
2
 
3
- import PipelineSingleton from './pipeline';
4
- import { RawImage } from '@xenova/transformers';
5
-
6
  const prisma = new PrismaClient()
7
 
8
  export async function POST(
9
  request: Request,
10
  ) {
 
 
 
 
 
 
11
  const { inputs, negative_prompt } = await request.json()
12
- const response = await fetch('https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0', {
13
  method: 'POST',
14
  body: JSON.stringify({
15
  inputs,
16
  negative_prompt
17
  }),
18
- headers: {
19
- Authorization: `Bearer ${process.env.NEXT_PUBLIC_APP_HF_TOKEN}`,
20
- 'Content-Type': 'application/json',
21
- ['x-use-cache']: "0"
22
- },
23
  })
24
 
25
- // avoid json parse error
26
  const res = await response.clone().json().catch(() => ({}));
27
  if (res?.error) return Response.json({ status: response.status, ok: false, message: res.error });
28
 
@@ -30,12 +28,19 @@ export async function POST(
30
  const headers = new Headers();
31
  headers.set("Content-Type", "image/*");
32
 
33
- // check if image is not safe for work
34
- const file_url = await RawImage.fromBlob(blob)
35
- const classifier: any = await PipelineSingleton.getInstance();
36
- if (classifier) {
37
- const results = await classifier(file_url);
38
- if (results?.length && results[0]?.label === "nsfw") {
 
 
 
 
 
 
 
39
  return Response.json({ status: 401, ok: false, message: "Image is not safe for work." });
40
  }
41
  }
 
1
  import { PrismaClient } from '@prisma/client'
2
 
 
 
 
3
  const prisma = new PrismaClient()
4
 
5
  export async function POST(
6
  request: Request,
7
  ) {
8
+ const global_headers = {
9
+ Authorization: `Bearer ${process.env.NEXT_PUBLIC_APP_HF_TOKEN}`,
10
+ 'Content-Type': 'application/json',
11
+ ['x-use-cache']: "0"
12
+ }
13
+
14
  const { inputs, negative_prompt } = await request.json()
15
+ const response = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/models/stabilityai/stable-diffusion-xl-base-1.0`, {
16
  method: 'POST',
17
  body: JSON.stringify({
18
  inputs,
19
  negative_prompt
20
  }),
21
+ headers: global_headers,
 
 
 
 
22
  })
23
 
 
24
  const res = await response.clone().json().catch(() => ({}));
25
  if (res?.error) return Response.json({ status: response.status, ok: false, message: res.error });
26
 
 
28
  const headers = new Headers();
29
  headers.set("Content-Type", "image/*");
30
 
31
+ const response_isnsfw = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/models/DamarJati/NSFW-Filterization-DecentScan`, {
32
+ method: 'POST',
33
+ headers: {
34
+ ...global_headers,
35
+ ...headers,
36
+ },
37
+ body: blob,
38
+ })
39
+ const isNSFW = await response_isnsfw.clone().json().catch(() => ({}));
40
+ // if (isNSFW?.error) return Response.json({ status: response.status, ok: false, message: isNSFW.error });
41
+ if (isNSFW?.length) {
42
+ const scoreNotSafe = isNSFW?.find((n: { label: string }) => n.label === "no_safe");
43
+ if (scoreNotSafe?.score > 0.7) {
44
  return Response.json({ status: 401, ok: false, message: "Image is not safe for work." });
45
  }
46
  }
components/modal/modal.tsx CHANGED
@@ -58,14 +58,20 @@ export const Modal: React.FC<Props> = ({ id, onClose }) => {
58
  >
59
  <motion.div
60
  onClick={(e) => e.stopPropagation()}
61
- className="max-w-2xl h-2/3 w-full z-[1] rounded-3xl overflow-hidden flex items-center justify-center flex-col gap-4 bg-white/30 backdrop-blur-sm p-2"
62
  variants={dropIn}
63
  initial="hidden"
64
  animate="visible"
65
  exit="exit"
66
  >
67
  <div
68
- className="bg-cover bg-center w-full h-full rounded-2xl"
 
 
 
 
 
 
69
  style={{
70
  backgroundImage: `url(${bufferToBase64})`,
71
  }}
 
58
  >
59
  <motion.div
60
  onClick={(e) => e.stopPropagation()}
61
+ className="max-w-2xl h-2/3 w-full z-[1] rounded-3xl overflow-hidden flex items-center justify-center flex-col gap-4 bg-white/30 backdrop-blur-sm px-2 pb-2 pt-6"
62
  variants={dropIn}
63
  initial="hidden"
64
  animate="visible"
65
  exit="exit"
66
  >
67
  <div
68
+ className="bg-contain bg-center w-full h-full rounded-2xl bg-no-repeat"
69
+ style={{
70
+ backgroundImage: `url(${bufferToBase64})`,
71
+ }}
72
+ />
73
+ <div
74
+ className="bg-cover bg-center w-full h-full rounded-2xl bg-no-repeat z-[-1] absolute top-0 left-0 opacity-90 blur-xl"
75
  style={{
76
  backgroundImage: `url(${bufferToBase64})`,
77
  }}
package.json CHANGED
@@ -11,7 +11,6 @@
11
  "dependencies": {
12
  "@prisma/client": "^5.5.2",
13
  "@tanstack/react-query": "^4.32.6",
14
- "@xenova/transformers": "^2.7.0",
15
  "classnames": "^2.3.2",
16
  "filereader": "^0.10.3",
17
  "framer-motion": "^10.16.4",
 
11
  "dependencies": {
12
  "@prisma/client": "^5.5.2",
13
  "@tanstack/react-query": "^4.32.6",
 
14
  "classnames": "^2.3.2",
15
  "filereader": "^0.10.3",
16
  "framer-motion": "^10.16.4",
prisma/schema.prisma CHANGED
@@ -4,7 +4,7 @@ generator client {
4
 
5
  datasource db {
6
  provider = "sqlite"
7
- url = "file://data/dev.db"
8
  }
9
 
10
  model Image {
 
4
 
5
  datasource db {
6
  provider = "sqlite"
7
+ url = "file:../data/dev.db"
8
  }
9
 
10
  model Image {