Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,6 @@ import os
|
|
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,9 +19,6 @@ mutated fingers, poorly drawn fingers, disfigured fingers,
|
|
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,13 +31,23 @@ def is_prompt_explicit(prompt):
|
|
35 |
return True
|
36 |
return False
|
37 |
|
38 |
-
# NSFW detection function
|
39 |
def is_nsfw_image(image):
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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):
|
|
|
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 |
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 |
return True
|
32 |
return False
|
33 |
|
34 |
+
# NSFW detection function using InferenceClient
|
35 |
def is_nsfw_image(image):
|
36 |
+
# Convert the image to bytes
|
37 |
+
img_byte_arr = BytesIO()
|
38 |
+
image.save(img_byte_arr, format='PNG')
|
39 |
+
img_byte_arr.seek(0)
|
40 |
+
|
41 |
+
# Send the image to the Hugging Face NSFW model
|
42 |
+
try:
|
43 |
+
result = client.image_classification("Falconsai/nsfw_image_detection", img_byte_arr.getvalue())
|
44 |
+
for item in result:
|
45 |
+
if item['label'] == 'nsfw' and item['score'] > 0.5:
|
46 |
+
return True
|
47 |
+
return False
|
48 |
+
except Exception as e:
|
49 |
+
print(f"Error in NSFW detection: {e}")
|
50 |
+
return False
|
51 |
|
52 |
# Function to generate an image from a text prompt
|
53 |
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):
|