File size: 2,215 Bytes
ae14a0c
a99d0d7
f7c002b
a99d0d7
ae14a0c
a99d0d7
 
 
 
 
 
 
 
ae14a0c
a99d0d7
 
 
 
 
 
 
 
 
 
f7c002b
a99d0d7
 
f7c002b
a99d0d7
f7c002b
 
 
a99d0d7
f7c002b
 
 
 
 
a99d0d7
 
 
 
 
f7c002b
a99d0d7
 
 
 
f7c002b
a99d0d7
 
f7c002b
a99d0d7
 
 
 
 
 
f7c002b
 
a99d0d7
 
f7c002b
 
a99d0d7
 
f7c002b
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import gradio as gr
import json
import re
from transformers import AutoTokenizer, AutoModelForSequenceClassification, TextClassificationPipeline

# Load Swear Words
try:
    with open("swearWord.json", "r") as f:
        swear_words = set(json.load(f))
    print("Swear words loaded successfully.")
except Exception as e:
    print(f"Failed to load swearWord.json: {e}")
    swear_words = set()

# Load Model and Tokenizer
try:
    tokenizer = AutoTokenizer.from_pretrained("eliasalbouzidi/distilbert-nsfw-text-classifier")
    model = AutoModelForSequenceClassification.from_pretrained("eliasalbouzidi/distilbert-nsfw-text-classifier")
    text_classifier = TextClassificationPipeline(model=model, tokenizer=tokenizer)
    print("Model loaded successfully.")
except Exception as e:
    print(f"Error loading model: {e}")
    exit(1)

# Text Classification and Censorship Function
def textclassifier(text):
    if not text.strip():
        return "Empty input", "unknown", 0.0

    # Censor known swear words
    def censor_word(word):
        return "***" if word.lower() in swear_words else word

    words = re.findall(r"\w+|[^\w\s]", text, re.UNICODE)
    censored_words = [censor_word(word) if re.match(r"\w+", word) else word for word in words]
    censored_text = " ".join(censored_words)

    # Run model on original input
    try:
        result = text_classifier(text)
        label = result[0]["label"]
        score = result[0]["score"]

        # Apply threshold for uncertainty
        threshold = 0.994
        if label == "nsfw" and score < threshold:
            label = "uncertain"

        return censored_text, label, round(score, 4)

    except Exception as e:
        return censored_text, f"Error: {str(e)}", 0.0

# Gradio Interface
interface = gr.Interface(
    fn=textclassifier,
    inputs=gr.Textbox(label="Enter text"),
    outputs=[
        gr.Textbox(label="Censored Text"),
        gr.Label(label="NSFW Prediction"),
        gr.Number(label="Confidence Score")
    ],
    title="Text Censorship + NSFW Classifier",
    description="Censors known swear words using *** and classifies the original text as NSFW, Safe, or Uncertain."
)

if __name__ == "__main__":
    interface.launch()