Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,126 +1,110 @@
|
|
| 1 |
|
| 2 |
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
import fitz # PyMuPDF
|
| 5 |
-
|
| 6 |
-
import io
|
| 7 |
-
import re
|
| 8 |
-
import difflib
|
| 9 |
import matplotlib.pyplot as plt
|
| 10 |
-
import
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
text +=
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
return
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
vectorizer = TfidfVectorizer().fit_transform([content] + los)
|
| 46 |
-
similarities = cosine_similarity(vectorizer[0:1], vectorizer[1:]).flatten()
|
| 47 |
-
matched = []
|
| 48 |
-
scores_old = [round(np.random.uniform(1, 3), 1) for _ in los]
|
| 49 |
-
scores_new = []
|
| 50 |
-
|
| 51 |
-
for i, score in enumerate(similarities):
|
| 52 |
-
if score > 0.2:
|
| 53 |
-
matched.append(f"β {los[i]} (Match: {score:.2f})")
|
| 54 |
-
scores_new.append(round(score * 5, 1)) # normalize to 5
|
| 55 |
-
|
| 56 |
-
return matched, len(matched), scores_old, scores_new
|
| 57 |
-
|
| 58 |
-
def summarize_added_lines(old_text, new_text):
|
| 59 |
-
old_lines = set(old_text.splitlines())
|
| 60 |
-
new_lines = set(new_text.splitlines())
|
| 61 |
-
added_lines = list(new_lines - old_lines)
|
| 62 |
-
summary = []
|
| 63 |
-
for line in added_lines:
|
| 64 |
-
line_clean = re.sub(r'[^a-zA-Z0-9., ]', '', line).strip()
|
| 65 |
-
if len(line_clean.split()) >= 5:
|
| 66 |
-
summary.append("- " + line_clean)
|
| 67 |
-
return summary, len(added_lines), len(new_lines)
|
| 68 |
-
|
| 69 |
-
def create_bar_chart(los, scores_old, scores_new):
|
| 70 |
-
index = np.arange(len(los))
|
| 71 |
-
bar_width = 0.35
|
| 72 |
fig, ax = plt.subplots(figsize=(10, 5))
|
| 73 |
-
|
| 74 |
-
ax.bar(
|
| 75 |
-
ax.
|
| 76 |
-
ax.
|
| 77 |
-
ax.
|
| 78 |
-
ax.
|
| 79 |
-
ax.
|
| 80 |
ax.legend()
|
| 81 |
-
ax.grid(True, linestyle="--", alpha=0.4)
|
| 82 |
-
fig.tight_layout()
|
| 83 |
-
return fig
|
| 84 |
-
|
| 85 |
-
def compare_handouts(old_pdf_bytes, new_pdf_bytes, lo_file_bytes, lo_filename):
|
| 86 |
-
old_text = extract_text_from_pdf(old_pdf_bytes)
|
| 87 |
-
new_text = extract_text_from_pdf(new_pdf_bytes)
|
| 88 |
-
los = extract_los(lo_file_bytes, lo_filename)
|
| 89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
if not old_text or not new_text:
|
| 91 |
-
return "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
quality = quality_check(new_text)
|
| 97 |
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
-
chart =
|
| 107 |
-
return
|
| 108 |
|
| 109 |
iface = gr.Interface(
|
| 110 |
-
fn=
|
| 111 |
inputs=[
|
| 112 |
-
gr.File(label="
|
| 113 |
-
gr.File(label="
|
| 114 |
-
gr.File(label="
|
| 115 |
],
|
| 116 |
outputs=[
|
| 117 |
-
gr.
|
| 118 |
-
gr.
|
| 119 |
-
gr.Textbox(label="π Stats & Quality", lines=5),
|
| 120 |
-
gr.Plot(label="π LO Match Score Chart")
|
| 121 |
],
|
| 122 |
-
title="π
|
| 123 |
-
description="
|
| 124 |
)
|
| 125 |
|
| 126 |
iface.launch()
|
|
|
|
| 1 |
|
| 2 |
|
| 3 |
import gradio as gr
|
| 4 |
+
from sentence_transformers import SentenceTransformer, util
|
| 5 |
import fitz # PyMuPDF
|
| 6 |
+
import docx
|
|
|
|
|
|
|
|
|
|
| 7 |
import matplotlib.pyplot as plt
|
| 8 |
+
import io
|
| 9 |
+
import base64
|
| 10 |
+
|
| 11 |
+
model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 12 |
+
|
| 13 |
+
def extract_text_from_pdf(pdf_file):
|
| 14 |
+
try:
|
| 15 |
+
doc = fitz.open(stream=pdf_file.read(), filetype="pdf")
|
| 16 |
+
text = ""
|
| 17 |
+
for page in doc:
|
| 18 |
+
text += page.get_text()
|
| 19 |
+
return text
|
| 20 |
+
except Exception as e:
|
| 21 |
+
return ""
|
| 22 |
+
|
| 23 |
+
def extract_text_from_docx(file_obj):
|
| 24 |
+
try:
|
| 25 |
+
doc = docx.Document(file_obj)
|
| 26 |
+
return "\n".join([para.text for para in doc.paragraphs if para.text.strip()])
|
| 27 |
+
except Exception as e:
|
| 28 |
+
return ""
|
| 29 |
+
|
| 30 |
+
def match_learning_outcomes(lo_list, text):
|
| 31 |
+
text_blocks = [para for para in text.split("\n") if len(para.strip()) > 20]
|
| 32 |
+
text_embs = model.encode(text_blocks, convert_to_tensor=True)
|
| 33 |
+
lo_embs = model.encode(lo_list, convert_to_tensor=True)
|
| 34 |
+
|
| 35 |
+
lo_scores = []
|
| 36 |
+
for i, lo in enumerate(lo_list):
|
| 37 |
+
sims = util.cos_sim(lo_embs[i], text_embs)
|
| 38 |
+
max_score = float(sims.max())
|
| 39 |
+
lo_scores.append(max_score)
|
| 40 |
+
return lo_scores
|
| 41 |
+
|
| 42 |
+
def generate_similarity_chart(lo_list, old_scores, new_scores):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
fig, ax = plt.subplots(figsize=(10, 5))
|
| 44 |
+
x = range(len(lo_list))
|
| 45 |
+
ax.bar(x, old_scores, width=0.4, label="Old", align="center")
|
| 46 |
+
ax.bar([i + 0.4 for i in x], new_scores, width=0.4, label="New", align="center")
|
| 47 |
+
ax.set_xticks([i + 0.2 for i in x])
|
| 48 |
+
ax.set_xticklabels([f"LO{i+1}" for i in x], rotation=45)
|
| 49 |
+
ax.set_ylabel("Match Score (0-1)")
|
| 50 |
+
ax.set_title("LO-wise Semantic Match Score: Old vs New")
|
| 51 |
ax.legend()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
+
buf = io.BytesIO()
|
| 54 |
+
plt.tight_layout()
|
| 55 |
+
plt.savefig(buf, format="png")
|
| 56 |
+
buf.seek(0)
|
| 57 |
+
encoded = base64.b64encode(buf.read()).decode("utf-8")
|
| 58 |
+
plt.close(fig)
|
| 59 |
+
return f"data:image/png;base64,{encoded}"
|
| 60 |
+
|
| 61 |
+
def compare_handouts(old_pdf, new_pdf, lo_file):
|
| 62 |
+
old_text = extract_text_from_pdf(old_pdf)
|
| 63 |
+
new_text = extract_text_from_pdf(new_pdf)
|
| 64 |
if not old_text or not new_text:
|
| 65 |
+
return "β Could not extract text from one or both PDFs.", None
|
| 66 |
+
|
| 67 |
+
lo_text = extract_text_from_docx(lo_file)
|
| 68 |
+
lo_list = [line.strip() for line in lo_text.split("\n") if line.strip()]
|
| 69 |
+
if not lo_list:
|
| 70 |
+
return "β No learning outcomes detected.", None
|
| 71 |
|
| 72 |
+
# LO Matching
|
| 73 |
+
old_scores = match_learning_outcomes(lo_list, old_text)
|
| 74 |
+
new_scores = match_learning_outcomes(lo_list, new_text)
|
|
|
|
| 75 |
|
| 76 |
+
matched_old = sum(score > 0.6 for score in old_scores)
|
| 77 |
+
matched_new = sum(score > 0.6 for score in new_scores)
|
| 78 |
+
|
| 79 |
+
# Change % based on character count
|
| 80 |
+
change_percent = abs(len(new_text) - len(old_text)) / max(len(old_text), 1) * 100
|
| 81 |
+
|
| 82 |
+
summary = f"π **Content Change:** {change_percent:.2f}%\n"
|
| 83 |
+
summary += f"π― **LOs Matched (Old vs New):** {matched_old} vs {matched_new} of {len(lo_list)}\n"
|
| 84 |
+
|
| 85 |
+
if matched_new > matched_old:
|
| 86 |
+
summary += "π’ New handout covers learning outcomes better.\n"
|
| 87 |
+
elif matched_new < matched_old:
|
| 88 |
+
summary += "π΄ New handout covers fewer outcomes.\n"
|
| 89 |
+
else:
|
| 90 |
+
summary += "π‘ No major change in LO coverage.\n"
|
| 91 |
|
| 92 |
+
chart = generate_similarity_chart(lo_list, old_scores, new_scores)
|
| 93 |
+
return summary, chart
|
| 94 |
|
| 95 |
iface = gr.Interface(
|
| 96 |
+
fn=compare_handouts,
|
| 97 |
inputs=[
|
| 98 |
+
gr.File(label="Old Version PDF"),
|
| 99 |
+
gr.File(label="New Version PDF"),
|
| 100 |
+
gr.File(label="Learning Outcomes (.docx)", file_types=[".docx"]),
|
| 101 |
],
|
| 102 |
outputs=[
|
| 103 |
+
gr.Markdown(label="Summary"),
|
| 104 |
+
gr.Image(label="LO Match Chart")
|
|
|
|
|
|
|
| 105 |
],
|
| 106 |
+
title="π Educational Content Comparator",
|
| 107 |
+
description="Compare two handouts and evaluate changes + Learning Outcome coverage using semantic similarity.",
|
| 108 |
)
|
| 109 |
|
| 110 |
iface.launch()
|