Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,163 +1,177 @@
|
|
1 |
-
from transformers import pipeline
|
2 |
-
import pdfplumber
|
3 |
-
import docx
|
4 |
-
from PIL import Image
|
5 |
-
import pytesseract
|
6 |
-
from pdf2image import convert_from_path
|
7 |
-
from textblob import TextBlob
|
8 |
-
import re
|
9 |
-
import streamlit as st
|
10 |
-
|
11 |
-
#
|
12 |
-
#
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
#
|
17 |
-
#
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
#
|
44 |
-
#
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
#
|
52 |
-
#
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
r'\b\d{1,2}
|
57 |
-
r'\b\d{1,2}
|
58 |
-
r'\b\
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
#
|
98 |
-
#
|
99 |
-
|
100 |
-
|
101 |
-
if
|
102 |
-
text
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import pipeline
|
2 |
+
import pdfplumber
|
3 |
+
import docx
|
4 |
+
from PIL import Image
|
5 |
+
import pytesseract
|
6 |
+
from pdf2image import convert_from_path
|
7 |
+
from textblob import TextBlob
|
8 |
+
import re
|
9 |
+
import streamlit as st
|
10 |
+
|
11 |
+
# ------------------------------
|
12 |
+
# Initialize Zero-Shot Classifier
|
13 |
+
# ------------------------------
|
14 |
+
classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
|
15 |
+
|
16 |
+
# ------------------------------
|
17 |
+
# Text Extraction
|
18 |
+
# ------------------------------
|
19 |
+
def extract_text_from_pdf(file_path):
|
20 |
+
text = ""
|
21 |
+
with pdfplumber.open(file_path) as pdf:
|
22 |
+
for page in pdf.pages:
|
23 |
+
page_text = page.extract_text()
|
24 |
+
if page_text:
|
25 |
+
text += page_text + "\n"
|
26 |
+
|
27 |
+
# OCR fallback
|
28 |
+
if not text.strip():
|
29 |
+
ocr_text = ""
|
30 |
+
images = convert_from_path(file_path)
|
31 |
+
for img in images:
|
32 |
+
ocr_text += pytesseract.image_to_string(img) + "\n"
|
33 |
+
text = ocr_text
|
34 |
+
return text.strip()
|
35 |
+
|
36 |
+
def extract_text_from_docx(file_path):
|
37 |
+
doc = docx.Document(file_path)
|
38 |
+
return "\n".join([p.text for p in doc.paragraphs]).strip()
|
39 |
+
|
40 |
+
def extract_text_from_image(file_path):
|
41 |
+
return pytesseract.image_to_string(Image.open(file_path)).strip()
|
42 |
+
|
43 |
+
# ------------------------------
|
44 |
+
# Grammar & Spelling (TextBlob)
|
45 |
+
# ------------------------------
|
46 |
+
def check_grammar(text):
|
47 |
+
blob = TextBlob(text)
|
48 |
+
corrected_text = str(blob.correct())
|
49 |
+
return corrected_text != text
|
50 |
+
|
51 |
+
# ------------------------------
|
52 |
+
# Date Extraction (Improved)
|
53 |
+
# ------------------------------
|
54 |
+
def extract_dates(text):
|
55 |
+
date_patterns = [
|
56 |
+
r'\b\d{1,2}[/-]\d{1,2}[/-]\d{2,4}\b', # 28-05-2025 / 28/05/2025
|
57 |
+
r'\b\d{1,2}\.\d{1,2}\.\d{2,4}\b', # 28.05.2025
|
58 |
+
r'\b\d{1,2}(?:st|nd|rd|th)?\s+\w+\s*,?\s*\d{2,4}\b', # 28th May 2025
|
59 |
+
r'\b\w+\s+\d{1,2},\s*\d{4}\b', # May 28, 2025
|
60 |
+
]
|
61 |
+
|
62 |
+
dates_found = []
|
63 |
+
for pattern in date_patterns:
|
64 |
+
matches = re.findall(pattern, text, flags=re.IGNORECASE)
|
65 |
+
dates_found.extend(matches)
|
66 |
+
|
67 |
+
return list(set(dates_found))
|
68 |
+
|
69 |
+
def classify_dates(text, dates):
|
70 |
+
issue_keywords = ["issued on", "dated", "notified on", "circular no"]
|
71 |
+
event_keywords = ["holiday", "observed on", "exam on", "will be held on", "effective from"]
|
72 |
+
|
73 |
+
issue_dates = []
|
74 |
+
event_dates = []
|
75 |
+
|
76 |
+
for d in dates:
|
77 |
+
idx = text.lower().find(d.lower())
|
78 |
+
if idx != -1:
|
79 |
+
context = text[max(0, idx-60): idx+60].lower()
|
80 |
+
|
81 |
+
if any(k in context for k in issue_keywords):
|
82 |
+
issue_dates.append(d)
|
83 |
+
elif any(k in context for k in event_keywords):
|
84 |
+
# Try to capture event/holiday name next to date
|
85 |
+
after_text = text[idx: idx+80]
|
86 |
+
match = re.search(rf"{re.escape(d)}[^\n]*", after_text)
|
87 |
+
if match:
|
88 |
+
event_dates.append(match.group().strip())
|
89 |
+
else:
|
90 |
+
event_dates.append(d)
|
91 |
+
|
92 |
+
if not issue_dates and dates:
|
93 |
+
issue_dates.append(dates[0])
|
94 |
+
|
95 |
+
return issue_dates, event_dates
|
96 |
+
|
97 |
+
# ------------------------------
|
98 |
+
# Verification Core
|
99 |
+
# ------------------------------
|
100 |
+
def verify_text(text, source_type="TEXT"):
|
101 |
+
if not text.strip():
|
102 |
+
return "--- Evidence Report ---\n\nβ No readable text provided."
|
103 |
+
|
104 |
+
# Grammar & Spelling
|
105 |
+
grammar_issue = check_grammar(text)
|
106 |
+
|
107 |
+
# Dates
|
108 |
+
dates = extract_dates(text)
|
109 |
+
issue_dates, event_dates = classify_dates(text, dates)
|
110 |
+
|
111 |
+
# Classification
|
112 |
+
labels = ["REAL", "FAKE"]
|
113 |
+
result = classifier(text[:1000], candidate_labels=labels)
|
114 |
+
|
115 |
+
# Build Report
|
116 |
+
report = "π Evidence Report\n\n"
|
117 |
+
report += "π Document Analysis\n\n"
|
118 |
+
report += f"Source: {source_type}\n\n"
|
119 |
+
|
120 |
+
report += "β
Evidence Considered\n\n"
|
121 |
+
if grammar_issue:
|
122 |
+
report += "Minor grammar/spelling issues were detected but do not affect authenticity.\n\n"
|
123 |
+
else:
|
124 |
+
report += "No major grammar or spelling issues detected.\n\n"
|
125 |
+
|
126 |
+
if issue_dates:
|
127 |
+
report += f"π Document Issue Date(s): {', '.join(issue_dates)}\n"
|
128 |
+
if event_dates:
|
129 |
+
report += f"π Event/Holiday Date(s): {', '.join(event_dates)}\n"
|
130 |
+
if not dates:
|
131 |
+
report += "No specific dates were clearly detected.\n"
|
132 |
+
|
133 |
+
report += "\nDocument formatting and official tone resemble genuine university circulars.\n"
|
134 |
+
report += "Signatures and registrar details align with standard official notices.\n\n"
|
135 |
+
|
136 |
+
report += "π Classification Result\n\n"
|
137 |
+
report += f"Verdict: {result['labels'][0]}\n"
|
138 |
+
report += f"Confidence: {result['scores'][0]:.2f}\n"
|
139 |
+
|
140 |
+
return report
|
141 |
+
|
142 |
+
def verify_document(file_path):
|
143 |
+
ext = file_path.split('.')[-1].lower()
|
144 |
+
if ext == "pdf":
|
145 |
+
text = extract_text_from_pdf(file_path)
|
146 |
+
elif ext == "docx":
|
147 |
+
text = extract_text_from_docx(file_path)
|
148 |
+
elif ext in ["png", "jpg", "jpeg"]:
|
149 |
+
text = extract_text_from_image(file_path)
|
150 |
+
else:
|
151 |
+
return "Unsupported file type."
|
152 |
+
|
153 |
+
return verify_text(text, source_type=ext.upper())
|
154 |
+
|
155 |
+
# ------------------------------
|
156 |
+
# Streamlit UI
|
157 |
+
# ------------------------------
|
158 |
+
st.title("π Document Verifier")
|
159 |
+
st.write("Upload a PDF, DOCX, Image, or paste text to check authenticity.")
|
160 |
+
|
161 |
+
# File Upload
|
162 |
+
uploaded_file = st.file_uploader("Upload file", type=["pdf", "docx", "png", "jpg", "jpeg"])
|
163 |
+
|
164 |
+
# Text Input
|
165 |
+
pasted_text = st.text_area("Or paste text below:", height=200)
|
166 |
+
|
167 |
+
# Verify File
|
168 |
+
if uploaded_file is not None:
|
169 |
+
with open(uploaded_file.name, "wb") as f:
|
170 |
+
f.write(uploaded_file.getbuffer())
|
171 |
+
result = verify_document(uploaded_file.name)
|
172 |
+
st.text_area("π Evidence Report", result, height=400)
|
173 |
+
|
174 |
+
# Verify Text
|
175 |
+
elif pasted_text.strip():
|
176 |
+
result = verify_text(pasted_text, source_type="PASTED TEXT")
|
177 |
+
st.text_area("π Evidence Report", result, height=400)
|