Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,11 @@
|
|
| 1 |
|
| 2 |
-
|
| 3 |
import gradio as gr
|
| 4 |
-
import fitz
|
| 5 |
from sentence_transformers import SentenceTransformer, util
|
| 6 |
import matplotlib.pyplot as plt
|
|
|
|
| 7 |
import numpy as np
|
| 8 |
-
import os
|
| 9 |
|
| 10 |
-
# Load transformer model once
|
| 11 |
model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 12 |
|
| 13 |
def extract_text_pdf(file_obj):
|
|
@@ -17,7 +15,7 @@ def extract_text_pdf(file_obj):
|
|
| 17 |
for page in doc:
|
| 18 |
text += page.get_text()
|
| 19 |
return text if text.strip() else None
|
| 20 |
-
except
|
| 21 |
return None
|
| 22 |
|
| 23 |
def semantic_similarity(text1, text2):
|
|
@@ -25,40 +23,77 @@ def semantic_similarity(text1, text2):
|
|
| 25 |
emb2 = model.encode([text2], convert_to_tensor=True)
|
| 26 |
return float(util.pytorch_cos_sim(emb1, emb2)[0][0])
|
| 27 |
|
| 28 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
old_text = extract_text_pdf(old_pdf)
|
| 30 |
new_text = extract_text_pdf(new_pdf)
|
| 31 |
|
| 32 |
if not old_text or not new_text:
|
| 33 |
-
return "β Could not extract text from one or both PDFs.", None
|
| 34 |
|
|
|
|
| 35 |
sim_score = semantic_similarity(old_text, new_text)
|
| 36 |
change_percent = round((1 - sim_score) * 100, 2)
|
| 37 |
-
|
| 38 |
-
summary = f"π Estimated Content Change: {change_percent}%\n\n"
|
| 39 |
-
summary += "π§ Semantic Similarity Score: {:.2f}\n".format(sim_score)
|
| 40 |
|
| 41 |
if change_percent < 10:
|
| 42 |
-
summary += "β
Minor
|
| 43 |
elif change_percent < 40:
|
| 44 |
-
summary += "π Moderate
|
| 45 |
else:
|
| 46 |
-
summary += "π
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
return summary,
|
| 49 |
|
| 50 |
iface = gr.Interface(
|
| 51 |
-
fn=
|
| 52 |
inputs=[
|
| 53 |
-
gr.File(label="
|
| 54 |
-
gr.File(label="
|
|
|
|
| 55 |
],
|
| 56 |
outputs=[
|
| 57 |
-
gr.Textbox(label="
|
| 58 |
-
gr.Plot(label="
|
|
|
|
| 59 |
],
|
| 60 |
-
title="π
|
| 61 |
-
description="
|
| 62 |
)
|
| 63 |
|
| 64 |
iface.launch()
|
|
|
|
| 1 |
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
+
import fitz
|
| 4 |
from sentence_transformers import SentenceTransformer, util
|
| 5 |
import matplotlib.pyplot as plt
|
| 6 |
+
import pandas as pd
|
| 7 |
import numpy as np
|
|
|
|
| 8 |
|
|
|
|
| 9 |
model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 10 |
|
| 11 |
def extract_text_pdf(file_obj):
|
|
|
|
| 15 |
for page in doc:
|
| 16 |
text += page.get_text()
|
| 17 |
return text if text.strip() else None
|
| 18 |
+
except:
|
| 19 |
return None
|
| 20 |
|
| 21 |
def semantic_similarity(text1, text2):
|
|
|
|
| 23 |
emb2 = model.encode([text2], convert_to_tensor=True)
|
| 24 |
return float(util.pytorch_cos_sim(emb1, emb2)[0][0])
|
| 25 |
|
| 26 |
+
def compare_with_los(text, lo_list):
|
| 27 |
+
scores = []
|
| 28 |
+
for lo in lo_list:
|
| 29 |
+
score = util.cos_sim(model.encode(lo, convert_to_tensor=True),
|
| 30 |
+
model.encode(text, convert_to_tensor=True))[0][0].item()
|
| 31 |
+
scores.append(round(score * 100, 2))
|
| 32 |
+
return scores
|
| 33 |
+
|
| 34 |
+
def compare_all(old_pdf, new_pdf, lo_file):
|
| 35 |
old_text = extract_text_pdf(old_pdf)
|
| 36 |
new_text = extract_text_pdf(new_pdf)
|
| 37 |
|
| 38 |
if not old_text or not new_text:
|
| 39 |
+
return "β Could not extract text from one or both PDFs.", None, None
|
| 40 |
|
| 41 |
+
# Overall semantic similarity
|
| 42 |
sim_score = semantic_similarity(old_text, new_text)
|
| 43 |
change_percent = round((1 - sim_score) * 100, 2)
|
| 44 |
+
summary = f"π Content Change: {change_percent}%\nπ§ Similarity Score: {sim_score:.2f}\n\n"
|
|
|
|
|
|
|
| 45 |
|
| 46 |
if change_percent < 10:
|
| 47 |
+
summary += "β
Minor content update."
|
| 48 |
elif change_percent < 40:
|
| 49 |
+
summary += "π Moderate update."
|
| 50 |
else:
|
| 51 |
+
summary += "π Significant changes detected."
|
| 52 |
+
|
| 53 |
+
# LO comparison
|
| 54 |
+
los = lo_file.read().decode("utf-8").splitlines()
|
| 55 |
+
old_scores = compare_with_los(old_text, los)
|
| 56 |
+
new_scores = compare_with_los(new_text, los)
|
| 57 |
+
score_diff = [round(new - old, 2) for old, new in zip(old_scores, new_scores)]
|
| 58 |
+
|
| 59 |
+
df = pd.DataFrame({
|
| 60 |
+
"Learning Outcome": los,
|
| 61 |
+
"Old Match (%)": old_scores,
|
| 62 |
+
"New Match (%)": new_scores,
|
| 63 |
+
"Change (%)": score_diff
|
| 64 |
+
})
|
| 65 |
+
table_html = df.to_html(index=False)
|
| 66 |
+
|
| 67 |
+
# Bar chart
|
| 68 |
+
fig, ax = plt.subplots(figsize=(10, 4))
|
| 69 |
+
index = np.arange(len(los))
|
| 70 |
+
bar_width = 0.35
|
| 71 |
+
ax.bar(index, old_scores, bar_width, label='Old')
|
| 72 |
+
ax.bar(index + bar_width, new_scores, bar_width, label='New')
|
| 73 |
+
ax.set_xlabel('Learning Outcomes')
|
| 74 |
+
ax.set_ylabel('Match Score (%)')
|
| 75 |
+
ax.set_title('LO-wise Semantic Match')
|
| 76 |
+
ax.set_xticks(index + bar_width / 2)
|
| 77 |
+
ax.set_xticklabels([f"LO{i+1}" for i in range(len(los))], rotation=45)
|
| 78 |
+
ax.legend()
|
| 79 |
+
fig.tight_layout()
|
| 80 |
|
| 81 |
+
return summary, fig, table_html
|
| 82 |
|
| 83 |
iface = gr.Interface(
|
| 84 |
+
fn=compare_all,
|
| 85 |
inputs=[
|
| 86 |
+
gr.File(label="Old Handout (PDF)"),
|
| 87 |
+
gr.File(label="New Handout (PDF)"),
|
| 88 |
+
gr.File(label="Learning Outcomes (.txt)", file_types=[".txt"])
|
| 89 |
],
|
| 90 |
outputs=[
|
| 91 |
+
gr.Textbox(label="Summary"),
|
| 92 |
+
gr.Plot(label="LO-wise Bar Chart"),
|
| 93 |
+
gr.HTML(label="LO-wise Comparison Table")
|
| 94 |
],
|
| 95 |
+
title="π Semantic Handout Comparator with LO Alignment",
|
| 96 |
+
description="Compare course handouts for overall change and LO alignment using transformer models."
|
| 97 |
)
|
| 98 |
|
| 99 |
iface.launch()
|