Spaces:
Runtime error
Runtime error
newVersion app.py (#5)
Browse files- newVersion app.py (e37fb3546d643238fd797bd4df6bab6621859c40)
Co-authored-by: kaisexX <kaisex@users.noreply.huggingface.co>
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import json
|
|
|
|
| 3 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification, TextClassificationPipeline
|
| 4 |
|
| 5 |
# Load Swear Words
|
|
@@ -21,41 +22,47 @@ except Exception as e:
|
|
| 21 |
print(f"Error loading model: {e}")
|
| 22 |
exit(1)
|
| 23 |
|
| 24 |
-
# Text
|
| 25 |
def textclassifier(text):
|
| 26 |
if not text.strip():
|
| 27 |
-
return "Empty input", 0.0
|
| 28 |
|
| 29 |
-
#
|
| 30 |
-
|
| 31 |
-
return "
|
| 32 |
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
try:
|
| 35 |
result = text_classifier(text)
|
| 36 |
label = result[0]["label"]
|
| 37 |
score = result[0]["score"]
|
| 38 |
|
| 39 |
-
#
|
| 40 |
threshold = 0.994
|
| 41 |
if label == "nsfw" and score < threshold:
|
| 42 |
label = "uncertain"
|
| 43 |
|
| 44 |
-
return label, round(score, 4)
|
| 45 |
|
| 46 |
except Exception as e:
|
| 47 |
-
return f"Error: {str(e)}", 0.0
|
| 48 |
|
| 49 |
# Gradio Interface
|
| 50 |
interface = gr.Interface(
|
| 51 |
fn=textclassifier,
|
| 52 |
inputs=gr.Textbox(label="Enter text"),
|
| 53 |
outputs=[
|
| 54 |
-
gr.
|
|
|
|
| 55 |
gr.Number(label="Confidence Score")
|
| 56 |
],
|
| 57 |
-
title="Text
|
| 58 |
-
|
| 59 |
)
|
| 60 |
|
| 61 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import json
|
| 3 |
+
import re
|
| 4 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification, TextClassificationPipeline
|
| 5 |
|
| 6 |
# Load Swear Words
|
|
|
|
| 22 |
print(f"Error loading model: {e}")
|
| 23 |
exit(1)
|
| 24 |
|
| 25 |
+
# Text Classification and Censorship Function
|
| 26 |
def textclassifier(text):
|
| 27 |
if not text.strip():
|
| 28 |
+
return "Empty input", "unknown", 0.0
|
| 29 |
|
| 30 |
+
# Censor known swear words
|
| 31 |
+
def censor_word(word):
|
| 32 |
+
return "***" if word.lower() in swear_words else word
|
| 33 |
|
| 34 |
+
words = re.findall(r"\w+|[^\w\s]", text, re.UNICODE)
|
| 35 |
+
censored_words = [censor_word(word) if re.match(r"\w+", word) else word for word in words]
|
| 36 |
+
censored_text = " ".join(censored_words)
|
| 37 |
+
|
| 38 |
+
# Run model on original input
|
| 39 |
try:
|
| 40 |
result = text_classifier(text)
|
| 41 |
label = result[0]["label"]
|
| 42 |
score = result[0]["score"]
|
| 43 |
|
| 44 |
+
# Apply threshold for uncertainty
|
| 45 |
threshold = 0.994
|
| 46 |
if label == "nsfw" and score < threshold:
|
| 47 |
label = "uncertain"
|
| 48 |
|
| 49 |
+
return censored_text, label, round(score, 4)
|
| 50 |
|
| 51 |
except Exception as e:
|
| 52 |
+
return censored_text, f"Error: {str(e)}", 0.0
|
| 53 |
|
| 54 |
# Gradio Interface
|
| 55 |
interface = gr.Interface(
|
| 56 |
fn=textclassifier,
|
| 57 |
inputs=gr.Textbox(label="Enter text"),
|
| 58 |
outputs=[
|
| 59 |
+
gr.Textbox(label="Censored Text"),
|
| 60 |
+
gr.Label(label="NSFW Prediction"),
|
| 61 |
gr.Number(label="Confidence Score")
|
| 62 |
],
|
| 63 |
+
title="Text Censorship + NSFW Classifier",
|
| 64 |
+
description="Censors known swear words using *** and classifies the original text as NSFW, Safe, or Uncertain."
|
| 65 |
)
|
| 66 |
|
| 67 |
+
if __name__ == "__main__":
|
| 68 |
+
interface.launch()
|