Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
| 2 |
+
import string
|
| 3 |
+
import easyocr
|
| 4 |
+
|
| 5 |
+
# Initialize OCR Reader once
|
| 6 |
+
ocr_reader = easyocr.Reader(['en'])
|
| 7 |
+
|
| 8 |
+
def extract_text_from_image(image_path):
|
| 9 |
+
result = ocr_reader.readtext(image_path, detail=0)
|
| 10 |
+
# Join OCR lines into one string
|
| 11 |
+
ocr_text = " ".join(result)
|
| 12 |
+
return ocr_text
|
| 13 |
+
|
| 14 |
+
def preprocess(text):
|
| 15 |
+
return text.lower().translate(str.maketrans('', '', string.punctuation)).strip()
|
| 16 |
+
|
| 17 |
+
def detect_sarcasm(combined_text):
|
| 18 |
+
MODEL_NAME = "helinivan/english-sarcasm-detector"
|
| 19 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 20 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
|
| 21 |
+
|
| 22 |
+
inputs = tokenizer([preprocess(combined_text)], padding=True, truncation=True, max_length=256, return_tensors="pt")
|
| 23 |
+
outputs = model(**inputs)
|
| 24 |
+
probs = outputs.logits.softmax(dim=-1).tolist()[0]
|
| 25 |
+
sarcasm_pred = probs.index(max(probs))
|
| 26 |
+
confidence = max(probs)
|
| 27 |
+
|
| 28 |
+
return {"sarcasm": bool(sarcasm_pred), "confidence": confidence}
|
| 29 |
+
|
| 30 |
+
# Example usage:
|
| 31 |
+
ocr_text = extract_text_from_image('path_to_image.jpg')
|
| 32 |
+
typed_text = "Your favorite sarcastic phrase here"
|
| 33 |
+
|
| 34 |
+
combined = ocr_text + " " + typed_text
|
| 35 |
+
result = detect_sarcasm(combined)
|
| 36 |
+
print(result)
|