Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from transformers import
|
| 2 |
import string
|
| 3 |
import easyocr
|
| 4 |
|
|
@@ -6,30 +6,29 @@ import easyocr
|
|
| 6 |
ocr_reader = easyocr.Reader(['en'])
|
| 7 |
|
| 8 |
def extract_text_from_image(image_path):
|
| 9 |
-
# Extract text lines from image
|
| 10 |
result = ocr_reader.readtext(image_path, detail=0)
|
| 11 |
-
ocr_text = " ".join(result)
|
| 12 |
return ocr_text
|
| 13 |
|
| 14 |
def preprocess(text):
|
| 15 |
-
# Lowercase, strip punctuation, and whitespace
|
| 16 |
return text.lower().translate(str.maketrans('', '', string.punctuation)).strip()
|
| 17 |
|
| 18 |
def detect_sarcasm(combined_text):
|
| 19 |
-
MODEL_NAME = "
|
| 20 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 21 |
-
model =
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
confidence = max(probs)
|
| 28 |
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
if __name__ == "__main__":
|
| 32 |
-
# Example usage:
|
| 33 |
image_path = "path_to_image.jpg" # Replace with your image file path
|
| 34 |
typed_text = "Your favorite sarcastic phrase here"
|
| 35 |
|
|
|
|
| 1 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
| 2 |
import string
|
| 3 |
import easyocr
|
| 4 |
|
|
|
|
| 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 |
+
ocr_text = " ".join(result)
|
| 11 |
return ocr_text
|
| 12 |
|
| 13 |
def preprocess(text):
|
|
|
|
| 14 |
return text.lower().translate(str.maketrans('', '', string.punctuation)).strip()
|
| 15 |
|
| 16 |
def detect_sarcasm(combined_text):
|
| 17 |
+
MODEL_NAME = "mrm8488/t5-base-finetuned-sarcasm-twitter"
|
| 18 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 19 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME)
|
| 20 |
|
| 21 |
+
input_text = preprocess(combined_text)
|
| 22 |
+
inputs = tokenizer.encode(input_text, return_tensors="pt")
|
| 23 |
+
outputs = model.generate(inputs, max_length=2)
|
| 24 |
+
prediction = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
|
| 25 |
|
| 26 |
+
sarcasm = prediction == "true"
|
| 27 |
+
confidence = None # This model doesn’t output confidence scores directly
|
| 28 |
+
|
| 29 |
+
return {"sarcasm": sarcasm, "confidence": confidence}
|
| 30 |
|
| 31 |
if __name__ == "__main__":
|
|
|
|
| 32 |
image_path = "path_to_image.jpg" # Replace with your image file path
|
| 33 |
typed_text = "Your favorite sarcastic phrase here"
|
| 34 |
|