Update app.py
Browse files
app.py
CHANGED
|
@@ -5,13 +5,14 @@ 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_labels = [
|
| 16 |
"sexism",
|
| 17 |
"racism",
|
|
@@ -21,14 +22,13 @@ hate_labels = [
|
|
| 21 |
"other",
|
| 22 |
"not_hate"
|
| 23 |
]
|
| 24 |
-
hate_tokenizer = AutoTokenizer.from_pretrained(
|
| 25 |
-
hate_model = AutoModelForSequenceClassification.from_pretrained(
|
| 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)
|
|
@@ -53,50 +53,47 @@ def classify_hate(text):
|
|
| 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 |
-
|
| 59 |
-
if
|
| 60 |
-
|
| 61 |
-
elif user_text.strip():
|
| 62 |
-
|
| 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 |
-
|
| 69 |
|
| 70 |
-
sarcasm_label, sarcasm_conf = detect_sarcasm(
|
| 71 |
if sarcasm_label == "sarcastic":
|
| 72 |
-
|
| 73 |
-
hate_label = None
|
| 74 |
else:
|
| 75 |
-
hate_label, hate_conf = classify_hate(
|
| 76 |
-
|
| 77 |
f"Hate Speech Category: {hate_label} (Confidence: {hate_conf:.2f})\n"
|
| 78 |
-
f"
|
| 79 |
)
|
| 80 |
-
chat_history.append(("User",
|
| 81 |
-
chat_history.append(("Bot",
|
| 82 |
return chat_history, None, None
|
| 83 |
|
| 84 |
with gr.Blocks() as demo:
|
| 85 |
-
gr.Markdown("# Cyber Bully Detection System
|
| 86 |
-
|
| 87 |
chat_history = gr.State([])
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 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 |
-
|
| 99 |
-
|
|
|
|
|
|
|
| 100 |
|
| 101 |
if __name__ == "__main__":
|
| 102 |
demo.launch()
|
|
|
|
| 5 |
from PIL import Image
|
| 6 |
import numpy as np
|
| 7 |
|
| 8 |
+
# Sarcasm Detection 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
|
| 15 |
+
HATE_MODEL_NAME = "cardiffnlp/twitter-roberta-base-hate-multiclass-latest"
|
| 16 |
hate_labels = [
|
| 17 |
"sexism",
|
| 18 |
"racism",
|
|
|
|
| 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):
|
|
|
|
|
|
|
| 32 |
if isinstance(image, Image.Image):
|
| 33 |
image = np.array(image)
|
| 34 |
texts = reader.readtext(image, detail=0)
|
|
|
|
| 53 |
return hate_labels[pred], conf
|
| 54 |
|
| 55 |
def respond(chat_history, user_text, user_image):
|
|
|
|
| 56 |
if user_image is not None:
|
| 57 |
+
extracted_text = extract_text(user_image)
|
| 58 |
+
if extracted_text.strip():
|
| 59 |
+
text_to_analyze = extracted_text
|
| 60 |
+
elif user_text and user_text.strip():
|
| 61 |
+
text_to_analyze = user_text.strip()
|
| 62 |
else:
|
| 63 |
chat_history.append(("User", ""))
|
| 64 |
+
chat_history.append(("Bot", "Please provide text or an image with readable text."))
|
| 65 |
return chat_history, None, None
|
| 66 |
else:
|
| 67 |
+
text_to_analyze = user_text.strip()
|
| 68 |
|
| 69 |
+
sarcasm_label, sarcasm_conf = detect_sarcasm(text_to_analyze)
|
| 70 |
if sarcasm_label == "sarcastic":
|
| 71 |
+
bot_response = f"Sarcasm detected (Confidence: {sarcasm_conf:.2f}). Hate speech detection skipped."
|
|
|
|
| 72 |
else:
|
| 73 |
+
hate_label, hate_conf = classify_hate(text_to_analyze)
|
| 74 |
+
bot_response = (
|
| 75 |
f"Hate Speech Category: {hate_label} (Confidence: {hate_conf:.2f})\n"
|
| 76 |
+
f"Message: \"{text_to_analyze}\""
|
| 77 |
)
|
| 78 |
+
chat_history.append(("User", text_to_analyze))
|
| 79 |
+
chat_history.append(("Bot", bot_response))
|
| 80 |
return chat_history, None, None
|
| 81 |
|
| 82 |
with gr.Blocks() as demo:
|
| 83 |
+
gr.Markdown("# Cyber Bully Detection System")
|
| 84 |
+
|
| 85 |
chat_history = gr.State([])
|
| 86 |
+
|
| 87 |
+
chatbot = gr.Chatbot()
|
| 88 |
+
txt = gr.Textbox(show_label=False, placeholder="Type your message here and press Enter")
|
| 89 |
+
img = gr.Image(source="upload", type="pil", label="Upload Screenshot (optional)")
|
| 90 |
+
clear_btn = gr.Button("Clear Chat")
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
txt.submit(respond, [chatbot, txt, img], [chatbot, txt, img])
|
| 93 |
+
# Use a button to submit the image instead of img.submit (Image doesn't support submit)
|
| 94 |
+
submit_img_btn = gr.Button("Analyze Image")
|
| 95 |
+
submit_img_btn.click(respond, [chatbot, txt, img], [chatbot, txt, img])
|
| 96 |
+
clear_btn.click(lambda: ([], None, None), None, [chatbot, txt, img])
|
| 97 |
|
| 98 |
if __name__ == "__main__":
|
| 99 |
demo.launch()
|