File size: 3,703 Bytes
9da1727
 
 
ae0881b
 
 
9da1727
9c8f84a
 
ae0881b
9c8f84a
 
06b22b6
9c8f84a
 
06b22b6
fb4632b
50362c6
 
 
 
 
fb4632b
9da1727
9c8f84a
 
fb4632b
9c8f84a
ae0881b
 
91c31b8
ae0881b
 
dd4a9f5
 
ae0881b
06b22b6
 
 
 
 
 
dd4a9f5
 
9da1727
06b22b6
f2f0fd9
9da1727
f2f0fd9
141ba65
9da1727
dd4a9f5
 
06b22b6
dd4a9f5
 
9c8f84a
 
 
 
 
dd4a9f5
 
9c8f84a
dd4a9f5
ae0881b
9c8f84a
9da1727
9c8f84a
06b22b6
9c8f84a
dd4a9f5
9c8f84a
 
dd4a9f5
9c8f84a
dd4a9f5
9c8f84a
 
dd4a9f5
50362c6
dd4a9f5
9c8f84a
 
dd4a9f5
9c8f84a
 
 
 
 
50362c6
dd4a9f5
9c8f84a
 
 
 
9da1727
 
dd4a9f5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import gradio as gr
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
import easyocr
from PIL import Image
import numpy as np

# Sarcasm Detection Model
SARCASM_MODEL_NAME = "j-hartmann/emotion-english-distilroberta-base"
sarcasm_labels = ["not sarcastic", "sarcastic"]
sarcasm_tokenizer = AutoTokenizer.from_pretrained(SARCASM_MODEL_NAME)
sarcasm_model = AutoModelForSequenceClassification.from_pretrained(SARCASM_MODEL_NAME)

# Hate Speech Model
HATE_MODEL_NAME = "cardiffnlp/twitter-roberta-base-hate-multiclass-latest"
hate_labels = [
    "sexism",
    "racism",
    "disability",
    "sexual_orientation",
    "religion",
    "other",
    "not_hate"
]
hate_tokenizer = AutoTokenizer.from_pretrained(HATE_MODEL_NAME)
hate_model = AutoModelForSequenceClassification.from_pretrained(HATE_MODEL_NAME)

# OCR Reader
reader = easyocr.Reader(['en'], gpu=False)

def extract_text(image):
    if isinstance(image, Image.Image):
        image = np.array(image)
    texts = reader.readtext(image, detail=0)
    return ' '.join(texts)

def detect_sarcasm(text):
    inputs = sarcasm_tokenizer(text, return_tensors="pt", truncation=True, padding=True)
    with torch.no_grad():
        outputs = sarcasm_model(**inputs)
        probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
        pred = torch.argmax(probs).item()
        conf = float(probs[0][pred])
        return sarcasm_labels[pred], conf

def classify_hate(text):
    inputs = hate_tokenizer(text, return_tensors="pt", truncation=True, padding=True)
    with torch.no_grad():
        outputs = hate_model(**inputs)
        probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
        pred = torch.argmax(probs).item()
        conf = float(probs[0][pred])
        return hate_labels[pred], conf

def respond(chat_history, user_text, user_image):
    if user_image is not None:
        extracted_text = extract_text(user_image)
        if extracted_text.strip():
            text_to_analyze = extracted_text
        elif user_text and user_text.strip():
            text_to_analyze = user_text.strip()
        else:
            chat_history.append(("User", ""))
            chat_history.append(("Bot", "Please provide text or an image with readable text."))
            return chat_history, None, None
    else:
        text_to_analyze = user_text.strip()

    sarcasm_label, sarcasm_conf = detect_sarcasm(text_to_analyze)
    if sarcasm_label == "sarcastic":
        bot_response = f"Sarcasm detected (Confidence: {sarcasm_conf:.2f}). Hate speech detection skipped."
    else:
        hate_label, hate_conf = classify_hate(text_to_analyze)
        bot_response = (
            f"Hate Speech Category: {hate_label} (Confidence: {hate_conf:.2f})\n"
            f"Message: \"{text_to_analyze}\""
        )
    chat_history.append(("User", text_to_analyze))
    chat_history.append(("Bot", bot_response))
    return chat_history, None, None

with gr.Blocks() as demo:
    gr.Markdown("# Cyber Bully Detection System")

    chat_history = gr.State([])

    chatbot = gr.Chatbot()
    txt = gr.Textbox(show_label=False, placeholder="Type your message here and press Enter")
    img = gr.Image(source="upload", type="pil", label="Upload Screenshot (optional)")
    clear_btn = gr.Button("Clear Chat")

    txt.submit(respond, [chatbot, txt, img], [chatbot, txt, img])
    # Use a button to submit the image instead of img.submit (Image doesn't support submit)
    submit_img_btn = gr.Button("Analyze Image")
    submit_img_btn.click(respond, [chatbot, txt, img], [chatbot, txt, img])
    clear_btn.click(lambda: ([], None, None), None, [chatbot, txt, img])

if __name__ == "__main__":
    demo.launch()