hansaka1 commited on
Commit
545bf64
·
verified ·
1 Parent(s): 5ba64b0

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -0
app.py ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pytesseract
3
+ from pytesseract import Output
4
+ from PIL import Image
5
+
6
+ def validate_chat_origin(image):
7
+ if image is None:
8
+ return "No image uploaded.", "Error"
9
+
10
+
11
+ try:
12
+ data = pytesseract.image_to_data(image, output_type=Output.DICT)
13
+ except Exception as e:
14
+ return f"OCR Error: {e}", "Error"
15
+
16
+ width, height = image.size
17
+ midpoint = width / 2
18
+
19
+
20
+ valid_lines = []
21
+
22
+
23
+ ignore_words = [
24
+ "end-to-end", "encrypted", "business", "account", "block",
25
+ "add", "contacts", "today", "joined", "march", "report", "spam"
26
+ ]
27
+
28
+ n_boxes = len(data['text'])
29
+
30
+
31
+ for i in range(n_boxes):
32
+ text = data['text'][i].strip()
33
+
34
+
35
+ if not text or int(data['conf'][i]) < 50:
36
+ continue
37
+
38
+
39
+ if any(ignored in text.lower() for ignored in ignore_words):
40
+ continue
41
+
42
+
43
+ valid_lines.append({
44
+ "text": text,
45
+ "top": data['top'][i],
46
+ "left": data['left'][i],
47
+ "width": data['width'][i]
48
+ })
49
+
50
+
51
+ valid_lines.sort(key=lambda x: x['top'])
52
+
53
+ if not valid_lines:
54
+ return "No chat messages detected (only headers found).", "Unknown"
55
+
56
+
57
+ first_message = valid_lines[0]
58
+
59
+
60
+
61
+ is_left_aligned = first_message['left'] < midpoint
62
+
63
+ debug_info = (
64
+ f"Detected First Text: '{first_message['text']}'\n"
65
+ f"Position X: {first_message['left']} (Image Width: {width})\n"
66
+ f"Alignment: {'Left (Stranger)' if is_left_aligned else 'Right (Me)'}"
67
+ )
68
+
69
+ if is_left_aligned:
70
+ return f"✅ VALID: First message is from stranger.\n\n{debug_info}", "VALID"
71
+ else:
72
+ return f"❌ INVALID: First message is from you.\n\n{debug_info}", "INVALID"
73
+
74
+ # Gradio Interface
75
+ iface = gr.Interface(
76
+ fn=validate_chat_origin,
77
+ inputs=gr.Image(type="pil", label="Upload Chat Screenshot"),
78
+ outputs=[
79
+ gr.Textbox(label="Analysis Result"),
80
+ gr.Label(label="Validation Status")
81
+ ],
82
+ title="WhatsApp First Message Validator",
83
+ description="Validates if the conversation was initiated by the stranger (Left Aligned = Valid). If the first text is Right Aligned, it is Invalid.",
84
+ theme="soft"
85
+ )
86
+
87
+ if __name__ == "__main__":
88
+ iface.launch()