Update app.py
Browse files
app.py
CHANGED
|
@@ -5,14 +5,13 @@ import easyocr
|
|
| 5 |
from PIL import Image
|
| 6 |
import numpy as np
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
sarcasm_labels = ["not sarcastic", "sarcastic"]
|
| 11 |
-
sarcasm_tokenizer = AutoTokenizer.from_pretrained(
|
| 12 |
-
sarcasm_model = AutoModelForSequenceClassification.from_pretrained(
|
| 13 |
|
| 14 |
-
|
| 15 |
-
HATE_MODEL_NAME = "cardiffnlp/twitter-roberta-base-hate-multiclass-latest"
|
| 16 |
hate_labels = [
|
| 17 |
"sexism",
|
| 18 |
"racism",
|
|
@@ -22,17 +21,18 @@ hate_labels = [
|
|
| 22 |
"other",
|
| 23 |
"not_hate"
|
| 24 |
]
|
| 25 |
-
hate_tokenizer = AutoTokenizer.from_pretrained(
|
| 26 |
-
hate_model = AutoModelForSequenceClassification.from_pretrained(
|
| 27 |
|
| 28 |
-
# OCR Reader
|
| 29 |
reader = easyocr.Reader(['en'], gpu=False)
|
| 30 |
|
| 31 |
def extract_text(image):
|
|
|
|
|
|
|
| 32 |
if isinstance(image, Image.Image):
|
| 33 |
image = np.array(image)
|
| 34 |
-
|
| 35 |
-
return ' '.join(
|
| 36 |
|
| 37 |
def detect_sarcasm(text):
|
| 38 |
inputs = sarcasm_tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
|
@@ -40,8 +40,8 @@ def detect_sarcasm(text):
|
|
| 40 |
outputs = sarcasm_model(**inputs)
|
| 41 |
probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
| 42 |
pred = torch.argmax(probs).item()
|
| 43 |
-
|
| 44 |
-
return sarcasm_labels[pred],
|
| 45 |
|
| 46 |
def classify_hate(text):
|
| 47 |
inputs = hate_tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
|
@@ -49,56 +49,54 @@ def classify_hate(text):
|
|
| 49 |
outputs = hate_model(**inputs)
|
| 50 |
probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
| 51 |
pred = torch.argmax(probs).item()
|
| 52 |
-
|
| 53 |
-
return hate_labels[pred],
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
else:
|
| 68 |
-
|
| 69 |
-
display_input = text
|
| 70 |
|
| 71 |
-
|
| 72 |
-
sarcasm_label, sarcasm_conf = detect_sarcasm(text)
|
| 73 |
if sarcasm_label == "sarcastic":
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 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 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
insert_before=iface.input_components[0]
|
| 101 |
-
)
|
| 102 |
|
| 103 |
if __name__ == "__main__":
|
| 104 |
-
|
|
|
|
| 5 |
from PIL import Image
|
| 6 |
import numpy as np
|
| 7 |
|
| 8 |
+
# Models and labels (same as before)
|
| 9 |
+
SARCASM_MODEL = "j-hartmann/emotion-english-distilroberta-base"
|
| 10 |
sarcasm_labels = ["not sarcastic", "sarcastic"]
|
| 11 |
+
sarcasm_tokenizer = AutoTokenizer.from_pretrained(SARCASM_MODEL)
|
| 12 |
+
sarcasm_model = AutoModelForSequenceClassification.from_pretrained(SARCASM_MODEL)
|
| 13 |
|
| 14 |
+
HATE_MODEL = "cardiffnlp/twitter-roberta-base-hate-multiclass-latest"
|
|
|
|
| 15 |
hate_labels = [
|
| 16 |
"sexism",
|
| 17 |
"racism",
|
|
|
|
| 21 |
"other",
|
| 22 |
"not_hate"
|
| 23 |
]
|
| 24 |
+
hate_tokenizer = AutoTokenizer.from_pretrained(HATE_MODEL)
|
| 25 |
+
hate_model = AutoModelForSequenceClassification.from_pretrained(HATE_MODEL)
|
| 26 |
|
|
|
|
| 27 |
reader = easyocr.Reader(['en'], gpu=False)
|
| 28 |
|
| 29 |
def extract_text(image):
|
| 30 |
+
if image is None:
|
| 31 |
+
return ""
|
| 32 |
if isinstance(image, Image.Image):
|
| 33 |
image = np.array(image)
|
| 34 |
+
texts = reader.readtext(image, detail=0)
|
| 35 |
+
return ' '.join(texts)
|
| 36 |
|
| 37 |
def detect_sarcasm(text):
|
| 38 |
inputs = sarcasm_tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
|
|
|
| 40 |
outputs = sarcasm_model(**inputs)
|
| 41 |
probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
| 42 |
pred = torch.argmax(probs).item()
|
| 43 |
+
conf = float(probs[0][pred])
|
| 44 |
+
return sarcasm_labels[pred], conf
|
| 45 |
|
| 46 |
def classify_hate(text):
|
| 47 |
inputs = hate_tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
|
|
|
| 49 |
outputs = hate_model(**inputs)
|
| 50 |
probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
| 51 |
pred = torch.argmax(probs).item()
|
| 52 |
+
conf = float(probs[0][pred])
|
| 53 |
+
return hate_labels[pred], conf
|
| 54 |
|
| 55 |
+
def respond(chat_history, user_text, user_image):
|
| 56 |
+
# Combine OCR and text input
|
| 57 |
+
if user_image is not None:
|
| 58 |
+
extracted = extract_text(user_image)
|
| 59 |
+
if extracted.strip():
|
| 60 |
+
input_text = extracted
|
| 61 |
+
elif user_text.strip():
|
| 62 |
+
input_text = user_text.strip()
|
| 63 |
+
else:
|
| 64 |
+
chat_history.append(("User", ""))
|
| 65 |
+
chat_history.append(("Bot", "Please provide text or an image with text."))
|
| 66 |
+
return chat_history, None, None
|
| 67 |
else:
|
| 68 |
+
input_text = user_text.strip()
|
|
|
|
| 69 |
|
| 70 |
+
sarcasm_label, sarcasm_conf = detect_sarcasm(input_text)
|
|
|
|
| 71 |
if sarcasm_label == "sarcastic":
|
| 72 |
+
response_text = f"Sarcasm detected (Confidence: {sarcasm_conf:.2f}). Hate speech detection skipped."
|
| 73 |
+
hate_label = None
|
| 74 |
+
else:
|
| 75 |
+
hate_label, hate_conf = classify_hate(input_text)
|
| 76 |
+
response_text = (
|
| 77 |
+
f"Hate Speech Category: {hate_label} (Confidence: {hate_conf:.2f})\n"
|
| 78 |
+
f"Text analyzed: \"{input_text}\""
|
| 79 |
+
)
|
| 80 |
+
chat_history.append(("User", input_text))
|
| 81 |
+
chat_history.append(("Bot", response_text))
|
| 82 |
+
return chat_history, None, None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
+
with gr.Blocks() as demo:
|
| 85 |
+
gr.Markdown("# Cyber Bully Detection System (Chat Interface)")
|
| 86 |
+
|
| 87 |
+
chat_history = gr.State([])
|
| 88 |
+
|
| 89 |
+
with gr.Row():
|
| 90 |
+
chatbot = gr.Chatbot()
|
| 91 |
+
with gr.Row():
|
| 92 |
+
txt = gr.Textbox(show_label=False, placeholder="Type your message here and press enter")
|
| 93 |
+
img = gr.Image(label="Upload screenshot (optional)", type="pil")
|
| 94 |
+
with gr.Row():
|
| 95 |
+
clear = gr.Button("Clear Chat")
|
| 96 |
|
| 97 |
+
txt.submit(respond, [chatbot, txt, img], [chatbot, txt, img])
|
| 98 |
+
img.submit(respond, [chatbot, txt, img], [chatbot, txt, img])
|
| 99 |
+
clear.click(lambda: ([], None, None), None, [chatbot, txt, img])
|
|
|
|
|
|
|
| 100 |
|
| 101 |
if __name__ == "__main__":
|
| 102 |
+
demo.launch()
|