BhuvanRShetty kaisex commited on
Commit
f7c002b
·
verified ·
1 Parent(s): 04d5b62

newVersion app.py (#5)

Browse files

- newVersion app.py (e37fb3546d643238fd797bd4df6bab6621859c40)


Co-authored-by: kaisexX <kaisex@users.noreply.huggingface.co>

Files changed (1) hide show
  1. app.py +20 -13
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 Classifier Function
25
  def textclassifier(text):
26
  if not text.strip():
27
- return "Empty input", 0.0
28
 
29
- # Check for swear words
30
- if any(word.lower() in swear_words for word in text.split()):
31
- return "swear-word", 1.0
32
 
33
- # Use model
 
 
 
 
34
  try:
35
  result = text_classifier(text)
36
  label = result[0]["label"]
37
  score = result[0]["score"]
38
 
39
- # Threshold logic
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.Label(label="Prediction"),
 
55
  gr.Number(label="Confidence Score")
56
  ],
57
- title="Text Classifier with Swear Word Filter",
58
- # description="First checks for swear words, then uses NSFW text classifier if no swear word is found."
59
  )
60
 
61
- interface.launch()
 
 
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()