Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,7 @@ import subprocess
|
|
| 5 |
from huggingface_hub import InferenceClient
|
| 6 |
from io import BytesIO
|
| 7 |
from PIL import Image
|
|
|
|
| 8 |
|
| 9 |
# Initialize the Flask app
|
| 10 |
app = Flask(__name__)
|
|
@@ -20,23 +21,35 @@ mutated fingers, poorly drawn fingers, disfigured fingers,
|
|
| 20 |
too many fingers, deformed hands, extra hands, malformed hands,
|
| 21 |
blurry hands, disproportionate fingers"""
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
@app.route('/')
|
| 24 |
def home():
|
| 25 |
return "Welcome to the Image Background Remover!"
|
| 26 |
|
| 27 |
-
# Simple content moderation function
|
| 28 |
-
def is_prompt_explicit(prompt):
|
| 29 |
-
# Streamlined keyword list to avoid unnecessary restrictions
|
| 30 |
-
explicit_keywords = [
|
| 31 |
-
"sexual","boobs","boob","penis","porn", "hentai", "fetish", "nude", "provocative", "obscene", "vulgar", "intimate", "kinky", "hardcore",
|
| 32 |
-
"threesome", "orgy", "masturbation", "genital", "suicide",
|
| 33 |
-
"self-harm", "depression", "kill myself", "worthless","vagina","intercourse","fuck","fucking","fuckers","fucker"
|
| 34 |
-
]
|
| 35 |
-
for keyword in explicit_keywords:
|
| 36 |
-
if keyword.lower() in prompt.lower():
|
| 37 |
-
return True
|
| 38 |
-
return False
|
| 39 |
-
|
| 40 |
# Function to generate an image from a text prompt
|
| 41 |
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):
|
| 42 |
try:
|
|
@@ -75,14 +88,16 @@ def generate_api():
|
|
| 75 |
return jsonify({"error": "Prompt is required"}), 400
|
| 76 |
|
| 77 |
try:
|
| 78 |
-
# Check for explicit content
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
| 81 |
return send_file(
|
| 82 |
"nsfw.jpg",
|
| 83 |
mimetype='image/png',
|
| 84 |
as_attachment=False,
|
| 85 |
-
download_name='
|
| 86 |
)
|
| 87 |
|
| 88 |
# Call the generate_image function with the provided parameters
|
|
|
|
| 5 |
from huggingface_hub import InferenceClient
|
| 6 |
from io import BytesIO
|
| 7 |
from PIL import Image
|
| 8 |
+
import re
|
| 9 |
|
| 10 |
# Initialize the Flask app
|
| 11 |
app = Flask(__name__)
|
|
|
|
| 21 |
too many fingers, deformed hands, extra hands, malformed hands,
|
| 22 |
blurry hands, disproportionate fingers"""
|
| 23 |
|
| 24 |
+
# Define the list of explicit keywords
|
| 25 |
+
EXPLICIT_KEYWORDS = [
|
| 26 |
+
"sexual", "sex", "boobs", "boob", "breasts", "cleavage", "penis", "phallus", "porn", "pornography", "hentai",
|
| 27 |
+
"fetish", "nude", "nudity", "provocative", "obscene", "vulgar", "intimate", "kinky", "hardcore",
|
| 28 |
+
"threesome", "orgy", "masturbation", "masturbate", "genital", "genitals", "vagina", "vaginal",
|
| 29 |
+
"anus", "anal", "butt", "buttocks", "butthole", "ass", "prostate", "erection", "cum", "ejaculation",
|
| 30 |
+
"sperm", "semen", "naked", "bare", "lingerie", "thong", "striptease", "stripper",
|
| 31 |
+
"seductive", "sensual", "explicit", "lewd", "taboo", "NSFW", "bdsm", "dominatrix", "submission",
|
| 32 |
+
"intercourse", "penetration", "orgasm", "fuck", "fucking", "fuckers", "fucker", "slut", "whore",
|
| 33 |
+
"prostitute", "hooker", "escort", "camgirl", "camwhore", "sugar daddy", "sugar baby", "adult content",
|
| 34 |
+
"sexually explicit", "arousal", "lust", "depraved", "hardcore porn", "softcore", "erotic", "erotica",
|
| 35 |
+
"roleplay", "incest", "taboo", "voyeur", "exhibitionist", "peeping", "dildo", "sex toy", "vibrator",
|
| 36 |
+
"suicide", "self-harm", "depression", "kill myself", "worthless", "abuse", "violence", "rape",
|
| 37 |
+
"sexual violence", "molestation", "pedophilia", "child porn", "underage", "illegal content"
|
| 38 |
+
]
|
| 39 |
+
|
| 40 |
+
# Function to scan the entire prompt for explicit keywords
|
| 41 |
+
def scan_prompt(prompt, keywords):
|
| 42 |
+
# Create a regex pattern to match any keyword (case insensitive)
|
| 43 |
+
pattern = r'\b(?:' + '|'.join(re.escape(keyword) for keyword in keywords) + r')\b'
|
| 44 |
+
# Find all matches in the prompt
|
| 45 |
+
matches = re.findall(pattern, prompt, flags=re.IGNORECASE)
|
| 46 |
+
# Return True if matches are found, and the list of matched keywords
|
| 47 |
+
return bool(matches), matches
|
| 48 |
+
|
| 49 |
@app.route('/')
|
| 50 |
def home():
|
| 51 |
return "Welcome to the Image Background Remover!"
|
| 52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
# Function to generate an image from a text prompt
|
| 54 |
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):
|
| 55 |
try:
|
|
|
|
| 88 |
return jsonify({"error": "Prompt is required"}), 400
|
| 89 |
|
| 90 |
try:
|
| 91 |
+
# Check for explicit content using scan_prompt
|
| 92 |
+
is_nsfw, found_keywords = scan_prompt(prompt, EXPLICIT_KEYWORDS)
|
| 93 |
+
if is_nsfw:
|
| 94 |
+
print(f"Explicit keywords found: {found_keywords}")
|
| 95 |
+
# Return the pre-defined "nsfw.jpg" image
|
| 96 |
return send_file(
|
| 97 |
"nsfw.jpg",
|
| 98 |
mimetype='image/png',
|
| 99 |
as_attachment=False,
|
| 100 |
+
download_name='nsfw_detected.png'
|
| 101 |
)
|
| 102 |
|
| 103 |
# Call the generate_image function with the provided parameters
|