Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,102 +5,97 @@ import easyocr
|
|
| 5 |
from PIL import Image
|
| 6 |
import numpy as np
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
|
|
|
|
|
|
| 10 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 11 |
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
"
|
| 22 |
-
]
|
| 23 |
-
|
| 24 |
-
# Initialize OCR reader
|
| 25 |
-
reader = easyocr.Reader(['en'], gpu=False)
|
| 26 |
-
|
| 27 |
-
# -------------------- FUNCTIONS --------------------
|
| 28 |
-
def extract_text_from_image(image):
|
| 29 |
-
"""Extracts text from uploaded image using EasyOCR."""
|
| 30 |
if image is None:
|
| 31 |
return ""
|
| 32 |
if isinstance(image, Image.Image):
|
| 33 |
image = np.array(image)
|
| 34 |
-
|
| 35 |
-
return " ".join(
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
| 40 |
if not text.strip():
|
| 41 |
return "No text found for analysis.", None
|
| 42 |
-
inputs = tokenizer(text, return_tensors="pt", truncation=True
|
| 43 |
with torch.no_grad():
|
| 44 |
-
|
| 45 |
-
logits = outputs.logits
|
| 46 |
probs = torch.nn.functional.softmax(logits, dim=-1)
|
| 47 |
pred = torch.argmax(probs).item()
|
| 48 |
confidence = float(probs[0][pred])
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
else
|
| 64 |
-
chat_history.append(("User", ""))
|
| 65 |
-
chat_history.append(("Bot", "Please enter text or upload a readable image."))
|
| 66 |
-
return chat_history, "", None
|
| 67 |
else:
|
| 68 |
-
text =
|
| 69 |
-
|
| 70 |
|
| 71 |
-
if not
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
return chat_history, "", None
|
| 75 |
|
| 76 |
-
#
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
| 81 |
|
| 82 |
|
| 83 |
-
#
|
|
|
|
|
|
|
| 84 |
with gr.Blocks() as demo:
|
| 85 |
-
gr.Markdown("
|
| 86 |
-
|
| 87 |
|
| 88 |
-
chatbot = gr.Chatbot(label="Chat
|
| 89 |
with gr.Row():
|
| 90 |
-
text_input = gr.Textbox(show_label=False, placeholder="Type a message
|
| 91 |
image_input = gr.Image(source="upload", type="pil", label="Upload Screenshot (optional)")
|
| 92 |
with gr.Row():
|
| 93 |
submit_btn = gr.Button("Analyze")
|
| 94 |
clear_btn = gr.Button("Clear Chat")
|
| 95 |
|
| 96 |
-
submit_btn.click(
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
"The system extracts text via OCR and uses DeBERTa to understand contextual meaning and classify it."
|
| 102 |
)
|
| 103 |
|
| 104 |
-
|
|
|
|
| 105 |
if __name__ == "__main__":
|
| 106 |
demo.launch()
|
|
|
|
| 5 |
from PIL import Image
|
| 6 |
import numpy as np
|
| 7 |
|
| 8 |
+
# -------------------------------
|
| 9 |
+
# MODEL: CardiffNLP RoBERTa Hate Classifier
|
| 10 |
+
# -------------------------------
|
| 11 |
+
MODEL_NAME = "cardiffnlp/twitter-roberta-base-hate-multiclass-latest"
|
| 12 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 13 |
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
|
| 14 |
|
| 15 |
+
LABELS = ["sexism", "racism", "disability", "sexual_orientation", "religion", "other", "not_hate"]
|
| 16 |
+
|
| 17 |
+
# -------------------------------
|
| 18 |
+
# OCR Setup
|
| 19 |
+
# -------------------------------
|
| 20 |
+
reader = easyocr.Reader(["en"], gpu=False)
|
| 21 |
+
|
| 22 |
+
def extract_text(image):
|
| 23 |
+
"""Extract text from uploaded image using EasyOCR"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
if image is None:
|
| 25 |
return ""
|
| 26 |
if isinstance(image, Image.Image):
|
| 27 |
image = np.array(image)
|
| 28 |
+
result = reader.readtext(image, detail=0)
|
| 29 |
+
return " ".join(result)
|
| 30 |
+
|
| 31 |
+
# -------------------------------
|
| 32 |
+
# CLASSIFICATION LOGIC
|
| 33 |
+
# -------------------------------
|
| 34 |
+
def classify_text(text):
|
| 35 |
+
"""Classify input text using RoBERTa model"""
|
| 36 |
if not text.strip():
|
| 37 |
return "No text found for analysis.", None
|
| 38 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True)
|
| 39 |
with torch.no_grad():
|
| 40 |
+
logits = model(**inputs).logits
|
|
|
|
| 41 |
probs = torch.nn.functional.softmax(logits, dim=-1)
|
| 42 |
pred = torch.argmax(probs).item()
|
| 43 |
confidence = float(probs[0][pred])
|
| 44 |
+
label = LABELS[pred]
|
| 45 |
+
return f"Category: {label} (Confidence: {confidence:.2f})", label
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
# -------------------------------
|
| 49 |
+
# CHATBOT FUNCTION
|
| 50 |
+
# -------------------------------
|
| 51 |
+
def cyberbully_chat(messages, user_message, image=None):
|
| 52 |
+
"""Chat-like conversational function"""
|
| 53 |
+
history = messages or []
|
| 54 |
+
|
| 55 |
+
# Extract text from image (if any)
|
| 56 |
+
if image:
|
| 57 |
+
text = extract_text(image)
|
| 58 |
+
content = f"[Extracted from image] {text}" if text else "[No readable text found]"
|
|
|
|
|
|
|
|
|
|
| 59 |
else:
|
| 60 |
+
text = user_message
|
| 61 |
+
content = text.strip()
|
| 62 |
|
| 63 |
+
if not content or content == "[No readable text found]":
|
| 64 |
+
history.append({"role": "assistant", "content": "Please provide valid text or an image with text."})
|
| 65 |
+
return history
|
|
|
|
| 66 |
|
| 67 |
+
# Classify with model
|
| 68 |
+
classification, label = classify_text(text)
|
| 69 |
+
|
| 70 |
+
# Append to chat
|
| 71 |
+
history.append({"role": "user", "content": content})
|
| 72 |
+
history.append({"role": "assistant", "content": classification})
|
| 73 |
+
return history
|
| 74 |
|
| 75 |
|
| 76 |
+
# -------------------------------
|
| 77 |
+
# GRADIO ChatGPT-like UI
|
| 78 |
+
# -------------------------------
|
| 79 |
with gr.Blocks() as demo:
|
| 80 |
+
gr.Markdown("# π€ Cyber Bully Detection System")
|
| 81 |
+
gr.Markdown("Upload an image or type text. The system will analyze hate-speech categories using a RoBERTa model specialized for social media context.")
|
| 82 |
|
| 83 |
+
chatbot = gr.Chatbot(type="messages", label="CyberBully Chat")
|
| 84 |
with gr.Row():
|
| 85 |
+
text_input = gr.Textbox(show_label=False, placeholder="Type a message here...")
|
| 86 |
image_input = gr.Image(source="upload", type="pil", label="Upload Screenshot (optional)")
|
| 87 |
with gr.Row():
|
| 88 |
submit_btn = gr.Button("Analyze")
|
| 89 |
clear_btn = gr.Button("Clear Chat")
|
| 90 |
|
| 91 |
+
submit_btn.click(
|
| 92 |
+
cyberbully_chat,
|
| 93 |
+
[chatbot, text_input, image_input],
|
| 94 |
+
[chatbot],
|
| 95 |
+
queue=True
|
|
|
|
| 96 |
)
|
| 97 |
|
| 98 |
+
clear_btn.click(lambda: [], None, chatbot, queue=False)
|
| 99 |
+
|
| 100 |
if __name__ == "__main__":
|
| 101 |
demo.launch()
|