BhuvanRShetty kaisex commited on
Commit
a99d0d7
·
verified ·
1 Parent(s): f9b14e7

update app.py (#2)

Browse files

- update app.py (fe1f1706896b2ee43521612aceaa14f3c7a19183)


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

Files changed (1) hide show
  1. app.py +58 -4
app.py CHANGED
@@ -1,7 +1,61 @@
1
  import gradio as gr
 
 
2
 
3
- def textclassifer(text):
4
- return "hello"
 
 
 
 
 
 
5
 
6
- interface=gr.Interface(fn=textclassifer,inputs='text',outputs='text')
7
- interface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import json
3
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification, TextClassificationPipeline
4
 
5
+ # Load Swear Words
6
+ try:
7
+ with open("swearWord.json", "r") as f:
8
+ swear_words = set(json.load(f))
9
+ print("Swear words loaded successfully.")
10
+ except Exception as e:
11
+ print(f"Failed to load swearWord.json: {e}")
12
+ swear_words = set()
13
 
14
+ # Load Model and Tokenizer
15
+ try:
16
+ tokenizer = AutoTokenizer.from_pretrained("eliasalbouzidi/distilbert-nsfw-text-classifier")
17
+ model = AutoModelForSequenceClassification.from_pretrained("eliasalbouzidi/distilbert-nsfw-text-classifier")
18
+ text_classifier = TextClassificationPipeline(model=model, tokenizer=tokenizer)
19
+ print("Model loaded successfully.")
20
+ 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()