nit454 commited on
Commit
fcc603b
·
verified ·
1 Parent(s): 5003fa2

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +106 -0
app.py ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
4
+ import easyocr
5
+ from PIL import Image
6
+ import numpy as np
7
+
8
+ # -------------------- MODEL SETUP --------------------
9
+ MODEL_NAME = "microsoft/deberta-v3-base" # Context-rich NLP model
10
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
11
+ model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
12
+
13
+ # Classes (Your categories)
14
+ LABELS = [
15
+ "abusive_language",
16
+ "harassment",
17
+ "threat",
18
+ "racism",
19
+ "sexism",
20
+ "religious_hate",
21
+ "not_hate"
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
+ extracted = reader.readtext(image, detail=0)
35
+ return " ".join(extracted)
36
+
37
+
38
+ def classify_text_with_deberta(text):
39
+ """Runs text through DeBERTa model for classification."""
40
+ if not text.strip():
41
+ return "No text found for analysis.", None
42
+ inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
43
+ with torch.no_grad():
44
+ outputs = model(**inputs)
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
+ return f"Prediction: {LABELS[pred]} (Confidence: {confidence:.2f})", LABELS[pred]
50
+
51
+
52
+ def respond(chat_history, user_text, user_image):
53
+ """Chatbot pipeline: OCR → DeBERTa classification → Chat output"""
54
+ # OCR extraction if image uploaded
55
+ if user_image is not None:
56
+ extracted_text = extract_text_from_image(user_image)
57
+ if extracted_text.strip():
58
+ text = extracted_text
59
+ display_text = f"[Extracted from OCR] {extracted_text}"
60
+ elif user_text and user_text.strip():
61
+ text = user_text
62
+ display_text = text
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 = user_text or ""
69
+ display_text = text.strip()
70
+
71
+ if not display_text:
72
+ chat_history.append(("User", ""))
73
+ chat_history.append(("Bot", "Empty input provided."))
74
+ return chat_history, "", None
75
+
76
+ # Run DeBERTa classification
77
+ result, label = classify_text_with_deberta(text)
78
+ chat_history.append(("User", display_text))
79
+ chat_history.append(("Cyber Bully Bot", result))
80
+ return chat_history, "", None
81
+
82
+
83
+ # -------------------- GRADIO INTERFACE --------------------
84
+ with gr.Blocks() as demo:
85
+ gr.Markdown("## 💬 Cyber Bully Detection System (OCR + DeBERTa Context Analysis)")
86
+ chat_history = gr.State([])
87
+
88
+ chatbot = gr.Chatbot(label="Chat History")
89
+ with gr.Row():
90
+ text_input = gr.Textbox(show_label=False, placeholder="Type a message or paste text here")
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(respond, [chatbot, text_input, image_input], [chatbot, text_input, image_input])
97
+ clear_btn.click(lambda: ([], "", None), None, [chatbot, text_input, image_input])
98
+
99
+ gr.Markdown(
100
+ "🔍 **How it works:** Upload a screenshot or type text. "
101
+ "The system extracts text via OCR and uses DeBERTa to understand contextual meaning and classify it."
102
+ )
103
+
104
+ # -------------------- LAUNCH --------------------
105
+ if __name__ == "__main__":
106
+ demo.launch()