Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,22 +1,31 @@
|
|
| 1 |
-
# app.py
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
import difflib
|
| 4 |
-
from
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
else:
|
| 15 |
-
return
|
| 16 |
|
| 17 |
-
def
|
| 18 |
-
|
| 19 |
-
|
|
|
|
| 20 |
|
| 21 |
old_lines = old_text.splitlines()
|
| 22 |
new_lines = new_text.splitlines()
|
|
@@ -24,24 +33,20 @@ def compare_documents(old_file, new_file):
|
|
| 24 |
diff = list(difflib.unified_diff(old_lines, new_lines))
|
| 25 |
added = [line for line in diff if line.startswith('+') and not line.startswith('+++')]
|
| 26 |
removed = [line for line in diff if line.startswith('-') and not line.startswith('---')]
|
| 27 |
-
|
| 28 |
percent_change = (len(added) + len(removed)) / max(len(old_lines), 1) * 100
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
)
|
| 46 |
-
|
| 47 |
-
iface.launch()
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import fitz # PyMuPDF
|
| 3 |
import difflib
|
| 4 |
+
from sentence_transformers import SentenceTransformer, util
|
| 5 |
+
|
| 6 |
+
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 7 |
+
|
| 8 |
+
def extract_text_from_pdf(pdf_file):
|
| 9 |
+
doc = fitz.open(stream=pdf_file.read(), filetype="pdf")
|
| 10 |
+
full_text = ""
|
| 11 |
+
for page in doc:
|
| 12 |
+
full_text += page.get_text()
|
| 13 |
+
return full_text
|
| 14 |
+
|
| 15 |
+
def extract_los(lo_file):
|
| 16 |
+
if lo_file.name.endswith('.txt'):
|
| 17 |
+
return lo_file.read().decode('utf-8').splitlines()
|
| 18 |
+
elif lo_file.name.endswith('.docx'):
|
| 19 |
+
from docx import Document
|
| 20 |
+
doc = Document(lo_file)
|
| 21 |
+
return [para.text for para in doc.paragraphs if para.text.strip()]
|
| 22 |
else:
|
| 23 |
+
return []
|
| 24 |
|
| 25 |
+
def compare_and_assess(old_pdf, new_pdf, lo_file):
|
| 26 |
+
# Compare PDFs
|
| 27 |
+
old_text = extract_text_from_pdf(old_pdf)
|
| 28 |
+
new_text = extract_text_from_pdf(new_pdf)
|
| 29 |
|
| 30 |
old_lines = old_text.splitlines()
|
| 31 |
new_lines = new_text.splitlines()
|
|
|
|
| 33 |
diff = list(difflib.unified_diff(old_lines, new_lines))
|
| 34 |
added = [line for line in diff if line.startswith('+') and not line.startswith('+++')]
|
| 35 |
removed = [line for line in diff if line.startswith('-') and not line.startswith('---')]
|
|
|
|
| 36 |
percent_change = (len(added) + len(removed)) / max(len(old_lines), 1) * 100
|
| 37 |
+
|
| 38 |
+
# LO analysis
|
| 39 |
+
los = extract_los(lo_file)
|
| 40 |
+
new_emb = model.encode(new_text, convert_to_tensor=True)
|
| 41 |
+
lo_scores = []
|
| 42 |
+
for lo in los:
|
| 43 |
+
lo_emb = model.encode(lo, convert_to_tensor=True)
|
| 44 |
+
score = util.cos_sim(new_emb, lo_emb).max().item()
|
| 45 |
+
lo_scores.append(f"β’ {lo[:80]}: {score*100:.1f}% relevance")
|
| 46 |
+
|
| 47 |
+
# Format Output
|
| 48 |
+
summary = f"π Content Updated: {percent_change:.2f}%\n"
|
| 49 |
+
summary += f"πΌ Added Lines: {len(added)} | π½ Removed Lines: {len(removed)}\n\n"
|
| 50 |
+
summary += "π― Learning Outcome Coverage:\n" + "\n".join(lo_scores[:10])
|
| 51 |
+
return summary
|
| 52 |
+
|
|
|
|
|
|
|
|
|