nit454 commited on
Commit
ae0881b
Β·
verified Β·
1 Parent(s): 06b22b6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -17
app.py CHANGED
@@ -1,16 +1,19 @@
1
  import gradio as gr
2
  from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
  import torch
 
 
 
4
 
5
- # Sarcasm detection model (public and reliable)
6
  SARCASM_MODEL_NAME = "j-hartmann/emotion-english-distilroberta-base"
7
- sarcasm_labels = ["not sarcastic", "sarcastic"] # simplified mapping
8
 
9
  sarcasm_tokenizer = AutoTokenizer.from_pretrained(SARCASM_MODEL_NAME)
10
  sarcasm_model = AutoModelForSequenceClassification.from_pretrained(SARCASM_MODEL_NAME)
11
 
12
- # Hate speech classification DeBERTa model fine-tuned for your labels (hypothetical model)
13
- HATE_MODEL_NAME = "your-username/deberta-hate-speech-custom" # replace with your actual fine-tuned model
14
  hate_labels = [
15
  "abusive_words",
16
  "harassment",
@@ -23,6 +26,15 @@ hate_labels = [
23
  hate_tokenizer = AutoTokenizer.from_pretrained(HATE_MODEL_NAME)
24
  hate_model = AutoModelForSequenceClassification.from_pretrained(HATE_MODEL_NAME)
25
 
 
 
 
 
 
 
 
 
 
26
  def detect_sarcasm(text):
27
  inputs = sarcasm_tokenizer(text, return_tensors="pt", truncation=True, padding=True)
28
  with torch.no_grad():
@@ -41,27 +53,41 @@ def classify_hate(text):
41
  confidence = float(probs[0][pred])
42
  return hate_labels[pred], confidence
43
 
44
- def chatbot(text):
45
- if not text or not text.strip():
46
- return "Please enter text to analyze."
 
 
 
 
 
 
 
47
 
48
- sarcasm_label, sarcasm_conf = detect_sarcasm(text)
49
  if sarcasm_label == "sarcastic":
50
- return f"Text is detected as SARCASTIC (Confidence: {sarcasm_conf:.2f}). Hate speech classification is skipped."
51
 
52
- hate_label, hate_conf = classify_hate(text)
53
  return (
54
- f"Text is NOT sarcastic.\n"
55
- f"Hate Speech Classification: {hate_label} (Confidence: {hate_conf:.2f})"
 
56
  )
57
 
58
  iface = gr.Interface(
59
  fn=chatbot,
60
- inputs=gr.Textbox(lines=3, placeholder="Enter text"),
61
- outputs="text",
62
- title="Sarcasm-aware Hate Speech Classifier",
63
- description="""First detects sarcasm, and if no sarcasm, classifies hate speech into
64
- detailed categories: abusive words, harassment (e.g., body shaming), religious hate, sexism, etc."""
 
 
 
 
 
 
65
  )
66
 
67
  if __name__ == "__main__":
 
1
  import gradio as gr
2
  from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
  import torch
4
+ import easyocr
5
+ from PIL import Image
6
+ import numpy as np
7
 
8
+ # Sarcasm detection model (public, replace if needed)
9
  SARCASM_MODEL_NAME = "j-hartmann/emotion-english-distilroberta-base"
10
+ sarcasm_labels = ["not sarcastic", "sarcastic"]
11
 
12
  sarcasm_tokenizer = AutoTokenizer.from_pretrained(SARCASM_MODEL_NAME)
13
  sarcasm_model = AutoModelForSequenceClassification.from_pretrained(SARCASM_MODEL_NAME)
14
 
15
+ # Hate speech classification (fine-tuned DeBERTa for your labels, replace with your actual model)
16
+ HATE_MODEL_NAME = "your-username/deberta-hate-speech-custom"
17
  hate_labels = [
18
  "abusive_words",
19
  "harassment",
 
26
  hate_tokenizer = AutoTokenizer.from_pretrained(HATE_MODEL_NAME)
27
  hate_model = AutoModelForSequenceClassification.from_pretrained(HATE_MODEL_NAME)
28
 
29
+ # OCR Reader
30
+ reader = easyocr.Reader(['en'], gpu=False)
31
+
32
+ def ocr_extract(image):
33
+ if isinstance(image, Image.Image):
34
+ image = np.array(image)
35
+ result = reader.readtext(image, detail=0)
36
+ return ' '.join(result)
37
+
38
  def detect_sarcasm(text):
39
  inputs = sarcasm_tokenizer(text, return_tensors="pt", truncation=True, padding=True)
40
  with torch.no_grad():
 
53
  confidence = float(probs[0][pred])
54
  return hate_labels[pred], confidence
55
 
56
+ def chatbot(image=None, text=None):
57
+ input_text = ""
58
+ if image is not None:
59
+ input_text = ocr_extract(image)
60
+ if not input_text.strip():
61
+ return "No text found in image.", None, None
62
+ elif text and text.strip():
63
+ input_text = text
64
+ else:
65
+ return "Please provide an image or some text.", None, None
66
 
67
+ sarcasm_label, sarcasm_conf = detect_sarcasm(input_text)
68
  if sarcasm_label == "sarcastic":
69
+ return f"Text is detected as SARCASTIC (Confidence: {sarcasm_conf:.2f}). Hate speech classification skipped.", None, sarcasm_label
70
 
71
+ hate_label, hate_conf = classify_hate(input_text)
72
  return (
73
+ f"Input text: {input_text}\nHate Speech: {hate_label} (Confidence: {hate_conf:.2f})\nSarcasm: {sarcasm_label} (Confidence: {sarcasm_conf:.2f})",
74
+ hate_label,
75
+ sarcasm_label
76
  )
77
 
78
  iface = gr.Interface(
79
  fn=chatbot,
80
+ inputs=[
81
+ gr.Image(type="pil", label="Upload Screenshot (optional)"),
82
+ gr.Textbox(lines=3, placeholder="Or, type/paste text here")
83
+ ],
84
+ outputs=[
85
+ gr.Textbox(label="Output"),
86
+ gr.Label(num_top_classes=len(hate_labels), label="Hate Speech Class"),
87
+ gr.Label(num_top_classes=2, label="Sarcasm")
88
+ ],
89
+ title="Sarcasm-aware Hate Speech Classifier with OCR",
90
+ description="Detect sarcasm first; if no sarcasm, classify hate speech from text or image screenshot."
91
  )
92
 
93
  if __name__ == "__main__":