Geek7 commited on
Commit
5e738b1
·
verified ·
1 Parent(s): ddda887

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -2
app.py CHANGED
@@ -4,6 +4,7 @@ import os
4
  from huggingface_hub import InferenceClient
5
  from io import BytesIO
6
  from PIL import Image
 
7
 
8
  # Initialize the Flask app
9
  app = Flask(__name__)
@@ -19,6 +20,9 @@ mutated fingers, poorly drawn fingers, disfigured fingers,
19
  too many fingers, deformed hands, extra hands, malformed hands,
20
  blurry hands, disproportionate fingers"""
21
 
 
 
 
22
  @app.route('/')
23
  def home():
24
  return "Welcome to the Image Background Remover!"
@@ -31,6 +35,14 @@ def is_prompt_explicit(prompt):
31
  return True
32
  return False
33
 
 
 
 
 
 
 
 
 
34
  # Function to generate an image from a text prompt
35
  def generate_image(prompt, negative_prompt=None, height=512, width=512, model="stabilityai/stable-diffusion-2-1", num_inference_steps=50, guidance_scale=7.5, seed=None):
36
  try:
@@ -71,7 +83,6 @@ def generate_api():
71
  try:
72
  # Check for explicit content
73
  if is_prompt_explicit(prompt):
74
- # Return the pre-defined "thinkgood.png" image
75
  return send_file(
76
  "thinkgood.jpeg",
77
  mimetype='image/png',
@@ -79,10 +90,19 @@ def generate_api():
79
  download_name='thinkgood.png'
80
  )
81
 
82
- # Call the generate_image function with the provided parameters
83
  image = generate_image(prompt, negative_prompt, height, width, model_name, num_inference_steps, guidance_scale, seed)
84
 
85
  if image:
 
 
 
 
 
 
 
 
 
86
  # Save the image to a BytesIO object
87
  img_byte_arr = BytesIO()
88
  image.save(img_byte_arr, format='PNG') # Convert the image to PNG
 
4
  from huggingface_hub import InferenceClient
5
  from io import BytesIO
6
  from PIL import Image
7
+ from transformers import pipeline
8
 
9
  # Initialize the Flask app
10
  app = Flask(__name__)
 
20
  too many fingers, deformed hands, extra hands, malformed hands,
21
  blurry hands, disproportionate fingers"""
22
 
23
+ # Initialize the NSFW detection pipeline
24
+ nsfw_classifier = pipeline("image-classification", model="Falconsai/nsfw_image_detection")
25
+
26
  @app.route('/')
27
  def home():
28
  return "Welcome to the Image Background Remover!"
 
35
  return True
36
  return False
37
 
38
+ # NSFW detection function
39
+ def is_nsfw_image(image):
40
+ results = nsfw_classifier(image)
41
+ for result in results:
42
+ if result['label'] == 'nsfw' and result['score'] > 0.5:
43
+ return True
44
+ return False
45
+
46
  # Function to generate an image from a text prompt
47
  def generate_image(prompt, negative_prompt=None, height=512, width=512, model="stabilityai/stable-diffusion-2-1", num_inference_steps=50, guidance_scale=7.5, seed=None):
48
  try:
 
83
  try:
84
  # Check for explicit content
85
  if is_prompt_explicit(prompt):
 
86
  return send_file(
87
  "thinkgood.jpeg",
88
  mimetype='image/png',
 
90
  download_name='thinkgood.png'
91
  )
92
 
93
+ # Generate the image
94
  image = generate_image(prompt, negative_prompt, height, width, model_name, num_inference_steps, guidance_scale, seed)
95
 
96
  if image:
97
+ # Check for NSFW content
98
+ if is_nsfw_image(image):
99
+ return send_file(
100
+ "nsfw.jpg",
101
+ mimetype='image/jpeg',
102
+ as_attachment=False,
103
+ download_name='nsfw.jpg'
104
+ )
105
+
106
  # Save the image to a BytesIO object
107
  img_byte_arr = BytesIO()
108
  image.save(img_byte_arr, format='PNG') # Convert the image to PNG