nit454 commited on
Commit
50362c6
Β·
verified Β·
1 Parent(s): 91c31b8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -33
app.py CHANGED
@@ -5,27 +5,27 @@ import easyocr
5
  from PIL import Image
6
  import numpy as np
7
 
8
- # Sarcasm Detection Model (public)
9
  SARCASM_MODEL_NAME = "j-hartmann/emotion-english-distilroberta-base"
10
  sarcasm_labels = ["not sarcastic", "sarcastic"]
11
  sarcasm_tokenizer = AutoTokenizer.from_pretrained(SARCASM_MODEL_NAME)
12
  sarcasm_model = AutoModelForSequenceClassification.from_pretrained(SARCASM_MODEL_NAME)
13
 
14
- # Hate Speech Model - DeBERTa fine-tuned (replace with your own model)
15
- HATE_MODEL_NAME = "your-username/deberta-hate-speech-custom"
16
  hate_labels = [
17
- "abusive_language",
18
- "harassment",
19
- "threat",
20
- "racism",
21
  "sexism",
22
- "religious_hate",
 
 
 
 
23
  "not_hate"
24
  ]
25
  hate_tokenizer = AutoTokenizer.from_pretrained(HATE_MODEL_NAME)
26
  hate_model = AutoModelForSequenceClassification.from_pretrained(HATE_MODEL_NAME)
27
 
28
- # OCR
29
  reader = easyocr.Reader(['en'], gpu=False)
30
 
31
  def extract_text(image):
@@ -52,38 +52,52 @@ def classify_hate(text):
52
  confidence = float(probs[0][pred])
53
  return hate_labels[pred], confidence
54
 
55
- def pipeline(image=None, text=None):
 
 
56
  if image is not None:
57
- input_text = extract_text(image)
58
- if not input_text.strip():
59
- return "No text found in image.", None
60
- elif text and text.strip():
61
- input_text = text
 
 
 
62
  else:
63
- return "Please provide text or upload an image.", None
 
64
 
65
- sarcasm_label, sarcasm_conf = detect_sarcasm(input_text)
 
66
  if sarcasm_label == "sarcastic":
67
- return f"Text detected as SARCASTIC (Confidence: {sarcasm_conf:.2f}). Hate speech classification skipped.", sarcasm_label
 
 
 
68
 
69
- hate_label, hate_conf = classify_hate(input_text)
70
- return (
71
- f"Input Text: {input_text}\nHate Speech Category: {hate_label} (Confidence: {hate_conf:.2f})\nSarcasm: {sarcasm_label} (Confidence: {sarcasm_conf:.2f})",
72
- hate_label
 
73
  )
 
 
 
74
 
75
- iface = gr.Interface(
76
- fn=pipeline,
77
- inputs=[
78
- gr.Image(type="pil", label="Upload Screenshot (optional)"),
79
- gr.Textbox(lines=3, placeholder="Or, type/paste text here")
80
- ],
81
- outputs=[
82
- gr.Textbox(label="Result"),
83
- gr.Label(num_top_classes=len(hate_labels), label="Hate Speech Class")
84
- ],
85
  title="Cyber Bully Detection System",
86
- description="Detects sarcasm first; if no sarcasm detected, classifies hate speech into abusive language, harassment, threat, racism, sexism, religious hate, or no hate."
 
 
 
 
 
 
87
  )
88
 
89
  if __name__ == "__main__":
 
5
  from PIL import Image
6
  import numpy as np
7
 
8
+ # Sarcasm Model
9
  SARCASM_MODEL_NAME = "j-hartmann/emotion-english-distilroberta-base"
10
  sarcasm_labels = ["not sarcastic", "sarcastic"]
11
  sarcasm_tokenizer = AutoTokenizer.from_pretrained(SARCASM_MODEL_NAME)
12
  sarcasm_model = AutoModelForSequenceClassification.from_pretrained(SARCASM_MODEL_NAME)
13
 
14
+ # Hate Speech Model (RoBERTa multiclass)
15
+ HATE_MODEL_NAME = "cardiffnlp/twitter-roberta-base-hate-multiclass-latest"
16
  hate_labels = [
 
 
 
 
17
  "sexism",
18
+ "racism",
19
+ "disability",
20
+ "sexual_orientation",
21
+ "religion",
22
+ "other",
23
  "not_hate"
24
  ]
25
  hate_tokenizer = AutoTokenizer.from_pretrained(HATE_MODEL_NAME)
26
  hate_model = AutoModelForSequenceClassification.from_pretrained(HATE_MODEL_NAME)
27
 
28
+ # OCR Reader
29
  reader = easyocr.Reader(['en'], gpu=False)
30
 
31
  def extract_text(image):
 
52
  confidence = float(probs[0][pred])
53
  return hate_labels[pred], confidence
54
 
55
+
56
+ def chatbot(conversation, user_message, image=None):
57
+ # Process image first if available
58
  if image is not None:
59
+ extracted_text = extract_text(image)
60
+ if not extracted_text.strip():
61
+ response = "No text found in the uploaded image."
62
+ conversation.append(("User", user_message))
63
+ conversation.append(("Cyber Bully Bot", response))
64
+ return conversation, None, None
65
+ text = extracted_text
66
+ display_input = f"[Extracted from image] {text}"
67
  else:
68
+ text = user_message.strip()
69
+ display_input = text
70
 
71
+ # Sarcasm detection
72
+ sarcasm_label, sarcasm_conf = detect_sarcasm(text)
73
  if sarcasm_label == "sarcastic":
74
+ response = f"Text detected as SARCASTIC (Confidence: {sarcasm_conf:.2f}). Hate speech classification skipped."
75
+ conversation.append(("User", display_input))
76
+ conversation.append(("Cyber Bully Bot", response))
77
+ return conversation, None, None
78
 
79
+ # Hate speech classification
80
+ hate_label, hate_conf = classify_hate(text)
81
+ response = (
82
+ f"Hate Speech Category: {hate_label} (Confidence: {hate_conf:.2f})\n"
83
+ f"Sarcasm: {sarcasm_label} (Confidence: {sarcasm_conf:.2f})"
84
  )
85
+ conversation.append(("User", display_input))
86
+ conversation.append(("Cyber Bully Bot", response))
87
+ return conversation, None, None
88
 
89
+ default_conversation = []
90
+
91
+ iface = gr.ChatInterface(
92
+ fn=chatbot,
 
 
 
 
 
 
93
  title="Cyber Bully Detection System",
94
+ description="Upload images or enter text. Bot detects sarcasm first, then classifies hate speech categories.",
95
+ )
96
+
97
+ # Add an image upload component beside text input
98
+ iface.add_component(
99
+ gr.Image(source="upload", label="Upload Screenshot (optional)", interactive=True),
100
+ insert_before=iface.input_components[0]
101
  )
102
 
103
  if __name__ == "__main__":