amirkhanbloch's picture
Update app.py
c967891 verified
import gradio as gr
import requests
import io
import os
from PIL import Image
# Get the API URL and headers from environment variables
API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0"
#API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"
headers = {"Authorization": f"Bearer {os.getenv('HF_TOKEN')}"}
# Define a list of negative words related to pornography
# Expanded list of negative words related to pornography
negative_prompts = [
"porn", "pornography", "nude", "naked", "sex", "sexual", "erotic",
"explicit", "adult content", "xxx", "hentai", "fetish", "blowjob",
"intercourse", "masturbation", "orgasm", "stripper", "sexy", "lust",
"hardcore", "dirty", "inappropriate", "vulgar", "obscene", "raunchy",
"tits", "boobs", "penis", "vagina", "anal", "NSFW", "threesome",
"gangbang", "adult entertainment", "adult film", "camgirl", "camming",
"escort", "prostitution", "hardcore porn", "softcore", "smut", "BDSM",
"bondage", "domination", "submission", "submissive", "dominatrix",
"sadomasochism", "kinks", "nudity", "voyeur", "exhibitionism", "shemale",
"slut", "whore", "hooker", "escort service", "erotic massage", "sensual",
"foreplay", "handjob", "nude model", "porn star", "sexual fantasy",
"fetishism", "double penetration", "cumshot", "facial", "deepthroat",
"cock", "pussy", "dildo", "vibrator", "orgy", "swinger", "explicit content",
"provocative", "risqué", "titillating", "wet dream", "fuck","fucked","fucking","suck","sucking"
]
# Define the query function
def query(payload):
response = requests.post(API_URL, headers=headers, json=payload)
response.raise_for_status() # Raise an error for bad responses
return response.content
# Define the function to generate the image
def generate_image(input_text):
if any(neg_word in input_text.lower() for neg_word in negative_prompts):
return None, "The input contains a negative prompt. Image generation is blocked."
image_bytes = query({"inputs": input_text})
image = Image.open(io.BytesIO(image_bytes))
return image,""
# Create Gradio interface
iface = gr.Interface(
fn=generate_image,
inputs=gr.Textbox(label="Input Text", placeholder="Enter your description here..."),
outputs=[gr.Image(label="Generated Image"), gr.Markdown(label="Message")],
title=" LodhranGPT Image Generator",
description="Enter a description to generate an image."
)
# Launch the Gradio app
iface.launch()