Update app.py
Browse files
app.py
CHANGED
|
@@ -3,44 +3,56 @@ from transformers import LayoutLMv2Processor, LayoutLMv2ForTokenClassification
|
|
| 3 |
import torch
|
| 4 |
from difflib import unified_diff
|
| 5 |
|
| 6 |
-
def extract_text_from_pdf(
|
| 7 |
"""Extract text from a PDF using pdfplumber."""
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
def compare_texts(source_text, target_text):
|
| 17 |
"""Compare two texts and highlight differences with source as truth."""
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
def process_pdfs(pdf1, pdf2):
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
if __name__ == "__main__":
|
| 41 |
-
#
|
| 42 |
-
pdf1_path = "path/to/
|
| 43 |
-
pdf2_path = "path/to/
|
| 44 |
|
| 45 |
# Process and print differences
|
| 46 |
result = process_pdfs(pdf1_path, pdf2_path)
|
|
|
|
| 3 |
import torch
|
| 4 |
from difflib import unified_diff
|
| 5 |
|
| 6 |
+
def extract_text_from_pdf(file):
|
| 7 |
"""Extract text from a PDF using pdfplumber."""
|
| 8 |
+
try:
|
| 9 |
+
text = ""
|
| 10 |
+
with pdfplumber.open(file) as pdf:
|
| 11 |
+
for page in pdf.pages:
|
| 12 |
+
page_text = page.extract_text()
|
| 13 |
+
if page_text:
|
| 14 |
+
text += page_text + "\n"
|
| 15 |
+
return text
|
| 16 |
+
except Exception as e:
|
| 17 |
+
return f"Error extracting text from PDF: {e}"
|
| 18 |
|
| 19 |
def compare_texts(source_text, target_text):
|
| 20 |
"""Compare two texts and highlight differences with source as truth."""
|
| 21 |
+
try:
|
| 22 |
+
diff = unified_diff(
|
| 23 |
+
source_text.splitlines(),
|
| 24 |
+
target_text.splitlines(),
|
| 25 |
+
lineterm='',
|
| 26 |
+
fromfile='Source PDF',
|
| 27 |
+
tofile='Target PDF'
|
| 28 |
+
)
|
| 29 |
+
return '\n'.join(diff)
|
| 30 |
+
except Exception as e:
|
| 31 |
+
return f"Error comparing texts: {e}"
|
| 32 |
|
| 33 |
def process_pdfs(pdf1, pdf2):
|
| 34 |
+
try:
|
| 35 |
+
# Extract text from the uploaded PDFs
|
| 36 |
+
text1 = extract_text_from_pdf(pdf1)
|
| 37 |
+
text2 = extract_text_from_pdf(pdf2)
|
| 38 |
|
| 39 |
+
if "Error" in text1 or "Error" in text2:
|
| 40 |
+
return f"Extraction issues detected: {text1 if 'Error' in text1 else ''} {text2 if 'Error' in text2 else ''}"
|
| 41 |
|
| 42 |
+
if not text1 or not text2:
|
| 43 |
+
return "One or both PDFs have no extractable text. Please check the files."
|
| 44 |
|
| 45 |
+
# Compare texts and find differences
|
| 46 |
+
differences = compare_texts(text1, text2)
|
| 47 |
+
|
| 48 |
+
return f"Differences found between the PDFs:\n\n{differences}"
|
| 49 |
+
except Exception as e:
|
| 50 |
+
return f"Error processing PDFs: {e}"
|
| 51 |
|
| 52 |
if __name__ == "__main__":
|
| 53 |
+
# Replace this block with code to upload and pass files if running in a web app environment
|
| 54 |
+
pdf1_path = "path/to/source.pdf" # Placeholder path
|
| 55 |
+
pdf2_path = "path/to/target.pdf" # Placeholder path
|
| 56 |
|
| 57 |
# Process and print differences
|
| 58 |
result = process_pdfs(pdf1_path, pdf2_path)
|