Spaces:
Sleeping
Sleeping
File size: 4,241 Bytes
d84f60a 7ec8f89 d84f60a 7ec8f89 d84f60a 7ec8f89 d84f60a 7ec8f89 d84f60a 7ec8f89 d84f60a 7ec8f89 d84f60a 7ec8f89 d84f60a 7ec8f89 d84f60a |
1 2 3 4 5 6 7 8 9 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 57 58 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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
import streamlit as st
import fitz # PyMuPDF
import docx
from difflib import HtmlDiff, SequenceMatcher
import os
# Directory to save uploaded files
UPLOAD_DIR = "uploaded_files"
if not os.path.exists(UPLOAD_DIR):
os.makedirs(UPLOAD_DIR)
# Functions to save, extract text, and metadata
def save_uploaded_file(uploaded_file):
file_path = os.path.join(UPLOAD_DIR, uploaded_file.name)
with open(file_path, "wb") as f:
f.write(uploaded_file.getbuffer())
return file_path
def extract_text_pdf(file_path):
doc = fitz.open(file_path)
text = ""
for page in doc:
text += page.get_text()
return text
def extract_text_word(file_path):
doc = docx.Document(file_path)
text = "\n".join([para.text for para in doc.paragraphs])
return text
def extract_metadata_pdf(file_path):
doc = fitz.open(file_path)
metadata = doc.metadata
return metadata
def extract_metadata_word(file_path):
doc = docx.Document(file_path)
core_props = doc.core_properties
metadata = {
"author": core_props.author,
"created": core_props.created,
"modified": core_props.modified
}
return metadata
# Function to compare text and return highlighted HTML differences
def compare_texts(text1, text2):
differ = HtmlDiff()
return differ.make_file(text1.splitlines(), text2.splitlines(), context=True, numlines=2)
# Function to calculate similarity score
def calculate_similarity(text1, text2):
matcher = SequenceMatcher(None, text1, text2)
return matcher.ratio()
# Streamlit App Interface
st.title("Document Edit Detection POC")
st.write("Upload both the original and edited documents below:")
# File upload
original_file = st.file_uploader("Upload Original Document", type=["pdf", "docx"])
edited_file = st.file_uploader("Upload Edited Document", type=["pdf", "docx"])
# Process if both files are uploaded
if original_file and edited_file:
# Save uploaded files
original_file_path = save_uploaded_file(original_file)
edited_file_path = save_uploaded_file(edited_file)
# Identify file types
original_ext = os.path.splitext(original_file.name)[1]
edited_ext = os.path.splitext(edited_file.name)[1]
# Check if both files are of the same type
if original_ext != edited_ext:
st.error("Both documents must be of the same type (PDF or DOCX).")
else:
# Extract text and metadata
if original_ext == ".pdf":
original_text = extract_text_pdf(original_file_path)
edited_text = extract_text_pdf(edited_file_path)
original_metadata = extract_metadata_pdf(original_file_path)
edited_metadata = extract_metadata_pdf(edited_file_path)
else:
original_text = extract_text_word(original_file_path)
edited_text = extract_text_word(edited_file_path)
original_metadata = extract_metadata_word(original_file_path)
edited_metadata = extract_metadata_word(edited_file_path)
# Display Metadata
st.subheader("Metadata Comparison")
metadata_match = original_metadata == edited_metadata
st.write("Metadata Match:", metadata_match)
st.write("Original Document Metadata:")
st.write(original_metadata)
st.write("Edited Document Metadata:")
st.write(edited_metadata)
# Compare text
st.subheader("Text Comparison")
text_diff_html = compare_texts(original_text, edited_text)
similarity_score = calculate_similarity(original_text, edited_text)
st.write("Similarity Score:", round(similarity_score * 100, 2), "%")
text_match = similarity_score == 1.0
st.write("Text Match:", text_match)
# Display highlighted text differences
st.write("Differences:")
st.components.v1.html(text_diff_html, height=400, scrolling=True)
# Report Generation
st.subheader("Report Summary")
st.write("Metadata Match:", metadata_match)
st.write("Text Match:", text_match)
st.write("Similarity Score:", round(similarity_score * 100, 2), "%")
else:
st.info("Please upload both the original and edited documents to proceed.")
|