Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import fitz # PyMuPDF
|
| 3 |
+
import docx
|
| 4 |
+
import difflib
|
| 5 |
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 6 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 7 |
+
from sentence_transformers import SentenceTransformer
|
| 8 |
+
import matplotlib.pyplot as plt
|
| 9 |
+
import io
|
| 10 |
+
import textstat
|
| 11 |
+
|
| 12 |
+
# Model for semantic similarity
|
| 13 |
+
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 14 |
+
|
| 15 |
+
def extract_text_from_pdf(file):
|
| 16 |
+
try:
|
| 17 |
+
text = ""
|
| 18 |
+
with fitz.open(stream=file, filetype="pdf") as doc:
|
| 19 |
+
for page in doc:
|
| 20 |
+
text += page.get_text()
|
| 21 |
+
return text.strip()
|
| 22 |
+
except Exception as e:
|
| 23 |
+
return ""
|
| 24 |
+
|
| 25 |
+
def extract_text_from_docx(file):
|
| 26 |
+
doc = docx.Document(file)
|
| 27 |
+
return "\n".join([para.text for para in doc.paragraphs if para.text.strip()])
|
| 28 |
+
|
| 29 |
+
def semantic_similarity(text1, text2):
|
| 30 |
+
embeddings = model.encode([text1, text2])
|
| 31 |
+
return float(cosine_similarity([embeddings[0]], [embeddings[1]])[0][0])
|
| 32 |
+
|
| 33 |
+
def compute_readability_score(text):
|
| 34 |
+
try:
|
| 35 |
+
return textstat.flesch_reading_ease(text)
|
| 36 |
+
except:
|
| 37 |
+
return 0.0
|
| 38 |
+
|
| 39 |
+
def compare_handouts(old_file, new_file, lo_file):
|
| 40 |
+
# Step 1: Extract content
|
| 41 |
+
old_text = extract_text_from_pdf(old_file)
|
| 42 |
+
new_text = extract_text_from_pdf(new_file)
|
| 43 |
+
if not old_text or not new_text:
|
| 44 |
+
return "β Could not extract text from one or both PDFs.", None, None, None, None
|
| 45 |
+
|
| 46 |
+
lo_text = extract_text_from_docx(lo_file)
|
| 47 |
+
lo_list = [lo.strip() for lo in lo_text.split("\n") if lo.strip()]
|
| 48 |
+
if not lo_list:
|
| 49 |
+
return "β οΈ No learning outcomes detected.", None, None, None, None
|
| 50 |
+
|
| 51 |
+
# Step 2: Semantic LO Matching
|
| 52 |
+
lo_matches = 0
|
| 53 |
+
for lo in lo_list:
|
| 54 |
+
score_old = semantic_similarity(lo, old_text)
|
| 55 |
+
score_new = semantic_similarity(lo, new_text)
|
| 56 |
+
if score_new > score_old and score_new > 0.6:
|
| 57 |
+
lo_matches += 1
|
| 58 |
+
|
| 59 |
+
# Step 3: Cosine similarity for content diff
|
| 60 |
+
tfidf = TfidfVectorizer().fit_transform([old_text, new_text])
|
| 61 |
+
content_sim = cosine_similarity(tfidf[0:1], tfidf[1:2])[0][0]
|
| 62 |
+
content_change = round((1 - content_sim) * 100, 2)
|
| 63 |
+
|
| 64 |
+
# Step 4: Readability
|
| 65 |
+
read_old = compute_readability_score(old_text)
|
| 66 |
+
read_new = compute_readability_score(new_text)
|
| 67 |
+
read_change = round(read_new - read_old, 2)
|
| 68 |
+
|
| 69 |
+
# Step 5: Composite score
|
| 70 |
+
lo_score = (lo_matches / len(lo_list)) * 100
|
| 71 |
+
composite_score = round((0.6 * lo_score + 0.3 * content_change + 0.1 * max(read_change, 0)), 2)
|
| 72 |
+
|
| 73 |
+
# Step 6: Summary
|
| 74 |
+
summary = f'''
|
| 75 |
+
π Content Change (TF-IDF): {content_change}%
|
| 76 |
+
π§ LO Alignment: {lo_matches} of {len(lo_list)} matched
|
| 77 |
+
π Readability Change: {read_change}
|
| 78 |
+
β Composite Improvement Score: {composite_score}%
|
| 79 |
+
|
| 80 |
+
π’ Summary: Based on semantic LO coverage, textual change, and readability,
|
| 81 |
+
the new handout shows {"significant" if composite_score > 50 else "moderate" if composite_score > 25 else "minimal"} improvement.
|
| 82 |
+
'''
|
| 83 |
+
|
| 84 |
+
# Step 7: Plot LO Match Bar
|
| 85 |
+
plt.figure(figsize=(6, 3))
|
| 86 |
+
plt.bar(["Matched LOs", "Unmatched LOs"], [lo_matches, len(lo_list)-lo_matches], color=["green", "red"])
|
| 87 |
+
plt.title("Learning Outcome Coverage")
|
| 88 |
+
plt.ylabel("Number")
|
| 89 |
+
buf = io.BytesIO()
|
| 90 |
+
plt.tight_layout()
|
| 91 |
+
plt.savefig(buf, format='png')
|
| 92 |
+
plt.close()
|
| 93 |
+
buf.seek(0)
|
| 94 |
+
|
| 95 |
+
return summary, gr.Image(value=buf, format="png"), content_change, lo_score, composite_score
|
| 96 |
+
|
| 97 |
+
# Gradio interface
|
| 98 |
+
iface = gr.Interface(
|
| 99 |
+
fn=compare_handouts,
|
| 100 |
+
inputs=[
|
| 101 |
+
gr.File(label="Upload OLD Handout (PDF)", type="binary"),
|
| 102 |
+
gr.File(label="Upload NEW Handout (PDF)", type="binary"),
|
| 103 |
+
gr.File(label="Upload Learning Outcomes (DOCX)", type="binary")
|
| 104 |
+
],
|
| 105 |
+
outputs=[
|
| 106 |
+
gr.Textbox(label="π Summary"),
|
| 107 |
+
gr.Image(label="π LO Alignment Chart"),
|
| 108 |
+
gr.Number(label="TF-IDF Content Change (%)"),
|
| 109 |
+
gr.Number(label="LO Coverage (%)"),
|
| 110 |
+
gr.Number(label="Composite Score")
|
| 111 |
+
],
|
| 112 |
+
title="π Handout Comparison & LO Analysis",
|
| 113 |
+
description="Upload two versions of a handout and learning outcomes to analyze improvements in content, LO coverage, and readability.",
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
iface.launch()
|