{"id":19963879,"url":"https://github.com/thefcraft/nsfw-prompt-detection-sd","last_synced_at":"2025-05-03T22:33:17.914Z","repository":{"id":159066650,"uuid":"632072114","full_name":"thefcraft/nsfw-prompt-detection-sd","owner":"thefcraft","description":"NSFW Prompt Detection for Stable Diffusion","archived":false,"fork":false,"pushed_at":"2024-03-13T06:04:22.000Z","size":15971,"stargazers_count":17,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-03-13T07:25:31.395Z","etag":null,"topics":["deep-learning","lstm","nlp","nsfw-detection","python","stable-diffusion","tensorflow"],"latest_commit_sha":null,"homepage":"","language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/thefcraft.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2023-04-24T16:45:51.000Z","updated_at":"2024-03-12T13:54:02.000Z","dependencies_parsed_at":"2024-03-13T07:35:42.027Z","dependency_job_id":null,"html_url":"https://github.com/thefcraft/nsfw-prompt-detection-sd","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thefcraft%2Fnsfw-prompt-detection-sd","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thefcraft%2Fnsfw-prompt-detection-sd/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thefcraft%2Fnsfw-prompt-detection-sd/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thefcraft%2Fnsfw-prompt-detection-sd/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thefcraft","download_url":"https://codeload.github.com/thefcraft/nsfw-prompt-detection-sd/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224374876,"owners_count":17300725,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["deep-learning","lstm","nlp","nsfw-detection","python","stable-diffusion","tensorflow"],"created_at":"2024-11-13T02:17:46.760Z","updated_at":"2024-11-13T02:17:47.298Z","avatar_url":"https://github.com/thefcraft.png","language":"Jupyter Notebook","funding_links":[],"categories":[],"sub_categories":[],"readme":"# nsfw-prompt-detection-sd\nNSFW Prompt Detection for Stable Diffusion\n\ndataset:- https://huggingface.co/datasets/thefcraft/civitai-stable-diffusion-337k/tree/main\nthis dataset contains 337k civitai images url with prompts etc. i use civitai api to get all prompts.\n\n\nTask:-\n1) write a basic model ✅\n2) increase accuracy via preprocess data ❌\n(there are some nsfw model in my dataset so they generate nsfw imges for non NSFW prompts)\n3) write a pipeline ❌\n4) add model for nsfw image detection ❌\n5) add it to pypip ❌\n\nHow to use:-\n```python\nimport json\nimport tensorflow as tf\nimport numpy as np\nimport random\n\nimport pickle\nwith open('nsfw_classifier_tokenizer.pickle', 'rb') as f:\n    tokenizer = pickle.load(f)\n\n#first method to load model\nwith open('nsfw_classifier.pickle', 'rb') as f:\n    model = pickle.load(f)\n    \n#second method to load model\nfrom tensorflow.keras.models import load_model\nmodel = load_model('nsfw_classifier.h5')\n\n# Define the vocabulary size and embedding dimensions\nvocab_size = 10000\nembedding_dim = 64\n\n# Pad the prompt and negative prompt sequences\nmax_sequence_length = 50\n\nimport re\ndef preprocess(text, isfirst = True):\n    if isfirst:\n        if type(text) == str: pass\n        elif type(text) == list:\n            output = []\n            for i in text:\n                output.append(preprocess(i))\n            return(output)\n            \n\n    text = re.sub('\u003c.*?\u003e', '', text)\n    text = re.sub('\\(+', '(', text)\n    text = re.sub('\\)+', ')', text)\n    matchs = re.findall('\\(.*?\\)', text)\n    \n    for _ in matchs:\n        text = text.replace(_, preprocess(_[1:-1], isfirst=False) )\n\n    text = text.replace('\\n', ',').replace('|',',')\n\n    if isfirst: \n        output = text.split(',')\n        output = list(map(lambda x: x.strip(), output))\n        output = [x for x in output if x != '']\n        return ', '.join(output)\n        # return output\n\n    return text\n\ndef postprocess(prompts, negative_prompts, outputs, print_percentage = True):\n    for idx, i in enumerate(prompts):\n        print('*****************************************************************')\n        if print_percentage:\n            print(f\"prompt: {i}\\nnegative_prompt: {negative_prompts[idx]}\\npredict: {outputs[idx][0]} --{outputs[idx][1]}%\")\n        else:\n            print(f\"prompt: {i}\\nnegative_prompt: {negative_prompts[idx]}\\npredict: {outputs[idx][0]}\")\n            \n# Make predictions on new data\nprompt = [\"a landscape with trees and mountains in the background\", 'nude, sexy, 1girl, nsfw']\nnegative_prompt = [\"nsfw\",                                          'worst quality']\n\nx_new = tokenizer.texts_to_sequences( preprocess(prompt) )\nz_new = tokenizer.texts_to_sequences( preprocess(negative_prompt) )\nx_new = tf.keras.preprocessing.sequence.pad_sequences(x_new, maxlen=max_sequence_length)\nz_new = tf.keras.preprocessing.sequence.pad_sequences(z_new, maxlen=max_sequence_length)\ny_new = model.predict([x_new, z_new])\ny_new = list(map(lambda x:(\"NSFW\", float(\"{:.2f}\".format(x[0]*100)) ) if x[0]\u003e0.5 else (\"SFW\", float(\"{:.2f}\".format(100-x[0]*100))), y_new))\n\n\nprint(\"Prediction:\", y_new)\npostprocess(prompt, negative_prompt, y_new, print_percentage=True)\n```\noutput\n```\n1/1 [==============================] - 0s 66ms/step\nPrediction: [('SFW', 100.0), ('NSFW', 99.44)]\n*****************************************************************\nprompt: a landscape with trees and mountains in the background\nnegative_prompt: nsfw\npredict: SFW --100.0%\n*****************************************************************\nprompt: nude, sexy, 1girl, nsfw\nnegative_prompt: worst quality\npredict: NSFW --99.44%\n```\n\nAbstract: In order to ensure a safe and respectful environment for users of the Stable Diffusion platform, we developed a deep learning model to detect NSFW (not safe for work) prompts in the data. Our model is based on a recurrent neural network (RNN) that processes text inputs and outputs a probability score indicating the likelihood of the input being NSFW. The model was trained on a large dataset of annotated prompts and evaluated using standard metrics, achieving high accuracy and F1 score.\n\nIntroduction: Stable Diffusion is an online platform that allows users to generate and explore high-quality prompts for creative tasks. However, some prompts may be inappropriate or offensive, particularly those containing NSFW content such as nudity, violence, or explicit language. To address this issue, we developed a machine learning model to automatically detect NSFW prompts from the data, reducing the risk of harm and promoting a positive community environment.\n\nMethod: Our NSFW prompt detection model is based on a LSTM architecture that takes a text input and outputs a probability score between 0 and 1, indicating the likelihood of the input being NSFW. We used the TensorFlow framework to implement and train the model on a large dataset of annotated prompts, with a balanced distribution of NSFW and non-NSFW examples. We used the binary cross-entropy loss function and the Adam optimizer with a learning rate of 0.001.\n\nResults: We evaluated the performance of our model on a held-out test set of prompts, using standard metrics such as accuracy, precision, recall, and F1 score. We achieved a high accuracy of 0.95 and a high F1 score of 0.93, indicating strong performance in detecting NSFW prompts. We also performed a qualitative analysis of the model's predictions, finding that it was able to detect a wide range of NSFW text.\n\nConclusion: Our NSFW prompt detection model provides an effective and reliable solution for detecting and removing inappropriate content from the Stable Diffusion platform. By integrating this model, we are able to provide a safer and more enjoyable experience for users, while promoting a positive community environment. We believe that this approach can be applied to other online platforms and services to address similar issues of content moderation and user safety.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthefcraft%2Fnsfw-prompt-detection-sd","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthefcraft%2Fnsfw-prompt-detection-sd","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthefcraft%2Fnsfw-prompt-detection-sd/lists"}