Update app.py
Browse files
app.py
CHANGED
|
@@ -14,38 +14,47 @@ CORS(app) # Enable CORS for all routes
|
|
| 14 |
HF_TOKEN = os.environ.get("HF_TOKEN") # Ensure to set your Hugging Face token in the environment
|
| 15 |
client = InferenceClient(token=HF_TOKEN)
|
| 16 |
|
| 17 |
-
# Initialize NSFW model
|
| 18 |
-
NSFW_MODEL = "MichalMlodawski/nsfw-text-detection-large"
|
| 19 |
-
nsfw_client = InferenceClient(model=NSFW_MODEL, token=HF_TOKEN)
|
| 20 |
-
|
| 21 |
# Hardcoded negative prompt
|
| 22 |
-
NEGATIVE_PROMPT_FINGERS = """2D,missing fingers, extra fingers, elongated fingers, fused fingers,
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
|
| 25 |
-
def
|
| 26 |
-
|
| 27 |
-
response = nsfw_client(prompt, task="text-classification")
|
| 28 |
-
if "error" in response:
|
| 29 |
-
print(f"Error in NSFW detection: {response['error']}")
|
| 30 |
-
return False
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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:
|
| 43 |
# Generate the image using Hugging Face's inference API with additional parameters
|
| 44 |
image = client.text_to_image(
|
| 45 |
-
prompt=prompt,
|
| 46 |
negative_prompt=NEGATIVE_PROMPT_FINGERS,
|
| 47 |
-
height=height,
|
| 48 |
-
width=width,
|
| 49 |
model=model,
|
| 50 |
num_inference_steps=num_inference_steps, # Control the number of inference steps
|
| 51 |
guidance_scale=guidance_scale, # Control the guidance scale
|
|
@@ -77,12 +86,12 @@ def generate_api():
|
|
| 77 |
try:
|
| 78 |
# Check for explicit content
|
| 79 |
if is_prompt_explicit(prompt):
|
| 80 |
-
# Return the pre-defined "
|
| 81 |
return send_file(
|
| 82 |
-
"nsfw.jpg",
|
| 83 |
-
mimetype='image/
|
| 84 |
as_attachment=False,
|
| 85 |
-
download_name='
|
| 86 |
)
|
| 87 |
|
| 88 |
# Call the generate_image function with the provided parameters
|
|
@@ -96,8 +105,8 @@ def generate_api():
|
|
| 96 |
|
| 97 |
# Send the generated image as a response
|
| 98 |
return send_file(
|
| 99 |
-
img_byte_arr,
|
| 100 |
-
mimetype='image/png',
|
| 101 |
as_attachment=False, # Send the file as an attachment
|
| 102 |
download_name='generated_image.png' # The file name for download
|
| 103 |
)
|
|
@@ -110,4 +119,5 @@ def generate_api():
|
|
| 110 |
# Add this block to make sure your app runs when called
|
| 111 |
if __name__ == "__main__":
|
| 112 |
subprocess.Popen(["python", "wk.py"]) # Start awake.py
|
|
|
|
| 113 |
app.run(host='0.0.0.0', port=7860) # Run directly if needed for testing
|
|
|
|
| 14 |
HF_TOKEN = os.environ.get("HF_TOKEN") # Ensure to set your Hugging Face token in the environment
|
| 15 |
client = InferenceClient(token=HF_TOKEN)
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
# Hardcoded negative prompt
|
| 18 |
+
NEGATIVE_PROMPT_FINGERS = """2D,missing fingers, extra fingers, elongated fingers, fused fingers,
|
| 19 |
+
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", "sex", "boobs", "boob", "breasts", "cleavage", "penis", "phallus", "porn", "pornography", "hentai",
|
| 32 |
+
"fetish", "nude", "nudity", "provocative", "obscene", "vulgar", "intimate", "kinky", "hardcore",
|
| 33 |
+
"threesome", "orgy", "masturbation", "masturbate", "genital", "genitals", "vagina", "vaginal",
|
| 34 |
+
"anus", "anal", "butt", "buttocks", "butthole", "ass", "prostate", "erection", "cum", "ejaculation",
|
| 35 |
+
"sperm", "semen", "naked", "bare", "lingerie", "thong", "striptease", "stripper",
|
| 36 |
+
"seductive", "sensual", "explicit", "lewd", "taboo", "NSFW", "bdsm", "dominatrix", "submission",
|
| 37 |
+
"intercourse", "penetration", "orgasm", "fuck", "fucking", "fuckers", "fucker", "slut", "whore",
|
| 38 |
+
"prostitute", "hooker", "escort", "camgirl", "camwhore", "sugar daddy", "sugar baby", "adult content",
|
| 39 |
+
"sexually explicit", "arousal", "lust", "depraved", "hardcore porn", "softcore", "erotic", "erotica",
|
| 40 |
+
"roleplay", "incest", "taboo", "voyeur", "exhibitionist", "peeping", "dildo", "sex toy", "vibrator",
|
| 41 |
+
"suicide", "self-harm", "depression", "kill myself", "worthless", "abuse", "violence", "rape",
|
| 42 |
+
"sexual violence", "molestation", "pedophilia", "child porn", "underage", "illegal content"
|
| 43 |
+
]
|
| 44 |
+
for keyword in explicit_keywords:
|
| 45 |
+
if keyword.lower() in prompt.lower():
|
| 46 |
+
return True
|
| 47 |
+
return False
|
| 48 |
|
| 49 |
# Function to generate an image from a text prompt
|
| 50 |
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):
|
| 51 |
try:
|
| 52 |
# Generate the image using Hugging Face's inference API with additional parameters
|
| 53 |
image = client.text_to_image(
|
| 54 |
+
prompt=prompt,
|
| 55 |
negative_prompt=NEGATIVE_PROMPT_FINGERS,
|
| 56 |
+
height=height,
|
| 57 |
+
width=width,
|
| 58 |
model=model,
|
| 59 |
num_inference_steps=num_inference_steps, # Control the number of inference steps
|
| 60 |
guidance_scale=guidance_scale, # Control the guidance scale
|
|
|
|
| 86 |
try:
|
| 87 |
# Check for explicit content
|
| 88 |
if is_prompt_explicit(prompt):
|
| 89 |
+
# Return the pre-defined "thinkgood.png" image
|
| 90 |
return send_file(
|
| 91 |
+
"nsfw.jpg",
|
| 92 |
+
mimetype='image/png',
|
| 93 |
as_attachment=False,
|
| 94 |
+
download_name='thinkgood.png'
|
| 95 |
)
|
| 96 |
|
| 97 |
# Call the generate_image function with the provided parameters
|
|
|
|
| 105 |
|
| 106 |
# Send the generated image as a response
|
| 107 |
return send_file(
|
| 108 |
+
img_byte_arr,
|
| 109 |
+
mimetype='image/png',
|
| 110 |
as_attachment=False, # Send the file as an attachment
|
| 111 |
download_name='generated_image.png' # The file name for download
|
| 112 |
)
|
|
|
|
| 119 |
# Add this block to make sure your app runs when called
|
| 120 |
if __name__ == "__main__":
|
| 121 |
subprocess.Popen(["python", "wk.py"]) # Start awake.py
|
| 122 |
+
|
| 123 |
app.run(host='0.0.0.0', port=7860) # Run directly if needed for testing
|