Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
+
from sentence_transformers import SentenceTransformer, util
|
4 |
+
import PyPDF2
|
5 |
+
from docx import Document
|
6 |
+
|
7 |
+
# Load the tokenizer and model for sentence embeddings
|
8 |
+
@st.cache_resource
|
9 |
+
def load_model():
|
10 |
+
tokenizer = AutoTokenizer.from_pretrained("rakeshkiriyath/gpt2Medium_text_to_sql")
|
11 |
+
model = AutoModelForCausalLM.from_pretrained("rakeshkiriyath/gpt2Medium_text_to_sql")
|
12 |
+
sentence_model = SentenceTransformer('all-MiniLM-L6-v2') # Smaller, faster sentence embeddings model
|
13 |
+
return tokenizer, model, sentence_model
|
14 |
+
|
15 |
+
# Extract text from a PDF file
|
16 |
+
def extract_text_from_pdf(pdf_file):
|
17 |
+
pdf_reader = PyPDF2.PdfReader(pdf_file)
|
18 |
+
text = ""
|
19 |
+
for page in pdf_reader.pages:
|
20 |
+
text += page.extract_text()
|
21 |
+
return text
|
22 |
+
|
23 |
+
# Extract text from a Word document
|
24 |
+
def extract_text_from_word(docx_file):
|
25 |
+
doc = Document(docx_file)
|
26 |
+
text = ""
|
27 |
+
for paragraph in doc.paragraphs:
|
28 |
+
text += paragraph.text + "\n"
|
29 |
+
return text
|
30 |
+
|
31 |
+
# Compare sentences for similarity
|
32 |
+
def compare_sentences(doc1_sentences, doc2_sentences, sentence_model):
|
33 |
+
similar_sentences = []
|
34 |
+
for i, sent1 in enumerate(doc1_sentences):
|
35 |
+
best_match = None
|
36 |
+
best_score = 0
|
37 |
+
for j, sent2 in enumerate(doc2_sentences):
|
38 |
+
score = util.pytorch_cos_sim(sentence_model.encode(sent1), sentence_model.encode(sent2)).item()
|
39 |
+
if score > best_score: # Higher similarity score
|
40 |
+
best_score = score
|
41 |
+
best_match = (i, j, score, sent1, sent2)
|
42 |
+
if best_match and best_score > 0.6: # Threshold for similarity
|
43 |
+
similar_sentences.append(best_match)
|
44 |
+
return similar_sentences
|
45 |
+
|
46 |
+
# Streamlit UI
|
47 |
+
def main():
|
48 |
+
st.title("Comparative Analysis of Two Documents")
|
49 |
+
st.sidebar.header("Upload Files")
|
50 |
+
|
51 |
+
# Upload files
|
52 |
+
uploaded_file1 = st.sidebar.file_uploader("Upload the First Document (PDF/Word)", type=["pdf", "docx"])
|
53 |
+
uploaded_file2 = st.sidebar.file_uploader("Upload the Second Document (PDF/Word)", type=["pdf", "docx"])
|
54 |
+
|
55 |
+
if uploaded_file1 and uploaded_file2:
|
56 |
+
# Extract text from the uploaded documents
|
57 |
+
text1 = extract_text_from_pdf(uploaded_file1) if uploaded_file1.name.endswith(".pdf") else extract_text_from_word(uploaded_file1)
|
58 |
+
text2 = extract_text_from_pdf(uploaded_file2) if uploaded_file2.name.endswith(".pdf") else extract_text_from_word(uploaded_file2)
|
59 |
+
|
60 |
+
# Split text into sentences
|
61 |
+
doc1_sentences = text1.split('. ')
|
62 |
+
doc2_sentences = text2.split('. ')
|
63 |
+
|
64 |
+
# Load model
|
65 |
+
tokenizer, model, sentence_model = load_model()
|
66 |
+
|
67 |
+
# Perform sentence comparison
|
68 |
+
similar_sentences = compare_sentences(doc1_sentences, doc2_sentences, sentence_model)
|
69 |
+
|
70 |
+
# Display results
|
71 |
+
st.header("Comparative Analysis Results")
|
72 |
+
if similar_sentences:
|
73 |
+
for match in similar_sentences:
|
74 |
+
doc1_index, doc2_index, score, sent1, sent2 = match
|
75 |
+
st.markdown(f"**Document 1 Sentence {doc1_index + 1}:** {sent1}")
|
76 |
+
st.markdown(f"**Document 2 Sentence {doc2_index + 1}:** {sent2}")
|
77 |
+
st.markdown(f"**Similarity Score:** {score:.2f}")
|
78 |
+
st.markdown("---")
|
79 |
+
else:
|
80 |
+
st.info("No significantly similar sentences found.")
|
81 |
+
|
82 |
+
else:
|
83 |
+
st.warning("Please upload two documents to compare.")
|
84 |
+
|
85 |
+
if __name__ == "__main__":
|
86 |
+
main()
|