Update app.py
Browse files
app.py
CHANGED
|
@@ -5,27 +5,27 @@ import easyocr
|
|
| 5 |
from PIL import Image
|
| 6 |
import numpy as np
|
| 7 |
|
| 8 |
-
# Sarcasm
|
| 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
|
| 15 |
-
HATE_MODEL_NAME = "
|
| 16 |
hate_labels = [
|
| 17 |
-
"abusive_language",
|
| 18 |
-
"harassment",
|
| 19 |
-
"threat",
|
| 20 |
-
"racism",
|
| 21 |
"sexism",
|
| 22 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
|
|
|
| 56 |
if image is not None:
|
| 57 |
-
|
| 58 |
-
if not
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
| 62 |
else:
|
| 63 |
-
|
|
|
|
| 64 |
|
| 65 |
-
|
|
|
|
| 66 |
if sarcasm_label == "sarcastic":
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
hate_label
|
|
|
|
| 73 |
)
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 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="
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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__":
|