Update app.py
Browse files
app.py
CHANGED
|
@@ -3,59 +3,95 @@ import pytesseract
|
|
| 3 |
from pytesseract import Output
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
-
# Ensure Tesseract is found (if needed for specific environments, otherwise leave default)
|
| 7 |
-
# pytesseract.pytesseract.tesseract_cmd = r'/usr/bin/tesseract'
|
| 8 |
-
|
| 9 |
def validate_chat_origin(image):
|
| 10 |
if image is None:
|
| 11 |
return {"valid": False, "error": "No image uploaded"}
|
| 12 |
|
| 13 |
try:
|
|
|
|
| 14 |
data = pytesseract.image_to_data(image, output_type=Output.DICT)
|
| 15 |
except Exception as e:
|
| 16 |
return {"valid": False, "error": str(e)}
|
| 17 |
|
| 18 |
width, height = image.size
|
| 19 |
-
midpoint = width / 2
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
n_boxes = len(data['text'])
|
|
|
|
| 25 |
for i in range(n_boxes):
|
| 26 |
text = data['text'][i].strip()
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
continue
|
|
|
|
|
|
|
| 29 |
if any(ignored in text.lower() for ignored in ignore_words):
|
| 30 |
continue
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
valid_lines.append({
|
| 33 |
"text": text,
|
| 34 |
"top": data['top'][i],
|
| 35 |
-
"left": data['left'][i]
|
|
|
|
| 36 |
})
|
| 37 |
|
|
|
|
| 38 |
valid_lines.sort(key=lambda x: x['top'])
|
| 39 |
|
| 40 |
if not valid_lines:
|
| 41 |
-
return {"valid": False, "reason": "No text
|
| 42 |
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
-
|
| 46 |
-
is_left_aligned = first_message['left'] < midpoint
|
| 47 |
|
| 48 |
-
if
|
| 49 |
-
return {"valid": True, "reason": "First message is from stranger
|
| 50 |
else:
|
| 51 |
-
|
|
|
|
| 52 |
|
| 53 |
-
#
|
| 54 |
iface = gr.Interface(
|
| 55 |
fn=validate_chat_origin,
|
| 56 |
inputs=gr.Image(type="pil"),
|
| 57 |
-
outputs=gr.JSON(),
|
| 58 |
-
title="WhatsApp Validator
|
| 59 |
)
|
| 60 |
|
| 61 |
if __name__ == "__main__":
|
|
|
|
| 3 |
from pytesseract import Output
|
| 4 |
from PIL import Image
|
| 5 |
|
|
|
|
|
|
|
|
|
|
| 6 |
def validate_chat_origin(image):
|
| 7 |
if image is None:
|
| 8 |
return {"valid": False, "error": "No image uploaded"}
|
| 9 |
|
| 10 |
try:
|
| 11 |
+
# Get data with bounding boxes
|
| 12 |
data = pytesseract.image_to_data(image, output_type=Output.DICT)
|
| 13 |
except Exception as e:
|
| 14 |
return {"valid": False, "error": str(e)}
|
| 15 |
|
| 16 |
width, height = image.size
|
|
|
|
| 17 |
|
| 18 |
+
# --- CONFIGURATION ---
|
| 19 |
+
# 1. Ignore the top 12% of the screen (Status Bar + Header Name)
|
| 20 |
+
header_margin = height * 0.12
|
| 21 |
+
|
| 22 |
+
# 2. Strict Stranger Zone: Incoming messages MUST start in the first 15% of width
|
| 23 |
+
# (e.g., if screen is 1000px wide, message must start within 0-150px)
|
| 24 |
+
stranger_margin_limit = width * 0.15
|
| 25 |
|
| 26 |
+
# 3. Ignore words (Case insensitive)
|
| 27 |
+
ignore_words = [
|
| 28 |
+
"end-to-end", "encrypted", "business", "account", "block",
|
| 29 |
+
"add", "contacts", "today", "joined", "march", "april", "may", "june",
|
| 30 |
+
"report", "spam", "copied", "messages", "calls", "group"
|
| 31 |
+
]
|
| 32 |
+
|
| 33 |
+
valid_lines = []
|
| 34 |
n_boxes = len(data['text'])
|
| 35 |
+
|
| 36 |
for i in range(n_boxes):
|
| 37 |
text = data['text'][i].strip()
|
| 38 |
+
conf = int(data['conf'][i])
|
| 39 |
+
|
| 40 |
+
# Basic filter: Empty text or low confidence
|
| 41 |
+
if not text or conf < 40:
|
| 42 |
+
continue
|
| 43 |
+
|
| 44 |
+
# Filter A: Ignore System Header (Top of screen)
|
| 45 |
+
# This prevents "13:25" or "68%" in status bar from triggering "Right Aligned"
|
| 46 |
+
if data['top'][i] < header_margin:
|
| 47 |
continue
|
| 48 |
+
|
| 49 |
+
# Filter B: Ignore specific system words
|
| 50 |
if any(ignored in text.lower() for ignored in ignore_words):
|
| 51 |
continue
|
| 52 |
|
| 53 |
+
# Filter C: purely numeric small chunks often misread (like timestamps isolated)
|
| 54 |
+
# If it's just digits and very short, skip (unless it's a long phone number)
|
| 55 |
+
if text.isdigit() and len(text) < 6:
|
| 56 |
+
continue
|
| 57 |
+
|
| 58 |
valid_lines.append({
|
| 59 |
"text": text,
|
| 60 |
"top": data['top'][i],
|
| 61 |
+
"left": data['left'][i],
|
| 62 |
+
"width": data['width'][i]
|
| 63 |
})
|
| 64 |
|
| 65 |
+
# Sort by vertical position (Top to Bottom)
|
| 66 |
valid_lines.sort(key=lambda x: x['top'])
|
| 67 |
|
| 68 |
if not valid_lines:
|
| 69 |
+
return {"valid": False, "reason": "No readable chat text found."}
|
| 70 |
|
| 71 |
+
# --- DECISION LOGIC ---
|
| 72 |
+
# Pick the very first detected text block
|
| 73 |
+
first_msg = valid_lines[0]
|
| 74 |
+
|
| 75 |
+
# Check: Does it start on the far left?
|
| 76 |
+
# Incoming messages align Left. Outgoing align Right. Centered text aligns Middle.
|
| 77 |
+
# Only "Left" is valid.
|
| 78 |
+
|
| 79 |
+
is_stranger = first_msg['left'] < stranger_margin_limit
|
| 80 |
|
| 81 |
+
debug_msg = f"Detected: '{first_msg['text']}' at X={first_msg['left']} (Limit: {stranger_margin_limit})"
|
|
|
|
| 82 |
|
| 83 |
+
if is_stranger:
|
| 84 |
+
return {"valid": True, "reason": "First message is from stranger.", "debug": debug_msg}
|
| 85 |
else:
|
| 86 |
+
# If it's not on the left, it's either ME (Right) or SYSTEM (Center)
|
| 87 |
+
return {"valid": False, "reason": "First message is NOT from stranger (Right/Center aligned).", "debug": debug_msg}
|
| 88 |
|
| 89 |
+
# Interface
|
| 90 |
iface = gr.Interface(
|
| 91 |
fn=validate_chat_origin,
|
| 92 |
inputs=gr.Image(type="pil"),
|
| 93 |
+
outputs=gr.JSON(),
|
| 94 |
+
title="WhatsApp Validator V2"
|
| 95 |
)
|
| 96 |
|
| 97 |
if __name__ == "__main__":
|