Update app.py
Browse files
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
|
| 6 |
SARCASM_MODEL_NAME = "j-hartmann/emotion-english-distilroberta-base"
|
| 7 |
-
sarcasm_labels = ["not sarcastic", "sarcastic"]
|
| 8 |
|
| 9 |
sarcasm_tokenizer = AutoTokenizer.from_pretrained(SARCASM_MODEL_NAME)
|
| 10 |
sarcasm_model = AutoModelForSequenceClassification.from_pretrained(SARCASM_MODEL_NAME)
|
| 11 |
|
| 12 |
-
# Hate speech classification
|
| 13 |
-
HATE_MODEL_NAME = "your-username/deberta-hate-speech-custom"
|
| 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 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
sarcasm_label, sarcasm_conf = detect_sarcasm(
|
| 49 |
if sarcasm_label == "sarcastic":
|
| 50 |
-
return f"Text is detected as SARCASTIC (Confidence: {sarcasm_conf:.2f}). Hate speech classification
|
| 51 |
|
| 52 |
-
hate_label, hate_conf = classify_hate(
|
| 53 |
return (
|
| 54 |
-
f"
|
| 55 |
-
|
|
|
|
| 56 |
)
|
| 57 |
|
| 58 |
iface = gr.Interface(
|
| 59 |
fn=chatbot,
|
| 60 |
-
inputs=
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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__":
|