nit454 commited on
Commit
e532328
Β·
verified Β·
1 Parent(s): fcc603b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -69
app.py CHANGED
@@ -5,102 +5,97 @@ 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()
 
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()