Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,117 +1,64 @@
|
|
| 1 |
|
| 2 |
|
| 3 |
import gradio as gr
|
| 4 |
-
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 5 |
-
from sklearn.metrics.pairwise import cosine_similarity
|
| 6 |
-
from sentence_transformers import SentenceTransformer, util
|
| 7 |
import fitz # PyMuPDF
|
| 8 |
-
import
|
| 9 |
import matplotlib.pyplot as plt
|
| 10 |
-
import
|
|
|
|
| 11 |
|
| 12 |
-
# Load transformer model
|
| 13 |
model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 14 |
|
| 15 |
-
def
|
| 16 |
-
text = ""
|
| 17 |
try:
|
| 18 |
-
with fitz.open(stream=
|
|
|
|
| 19 |
for page in doc:
|
| 20 |
text += page.get_text()
|
|
|
|
| 21 |
except Exception as e:
|
| 22 |
-
|
| 23 |
-
return text
|
| 24 |
-
|
| 25 |
-
def extract_text_from_docx(file):
|
| 26 |
-
doc = docx.Document(file)
|
| 27 |
-
return "\n".join([para.text for para in doc.paragraphs])
|
| 28 |
|
| 29 |
-
def
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
similarities = util.pytorch_cos_sim(content_embedding, lo_embeddings)[0]
|
| 34 |
-
return similarities.tolist()
|
| 35 |
|
| 36 |
-
def
|
| 37 |
-
old_text =
|
| 38 |
-
new_text =
|
| 39 |
-
if not old_text.strip() or not new_text.strip():
|
| 40 |
-
return "β Could not extract text from one or both PDFs.", None, None
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
if not lo_list:
|
| 45 |
-
return "β οΈ No learning outcomes detected in uploaded DOCX file.", None, None
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
change = round(((avg_new - avg_old) / avg_old) * 100, 2) if avg_old != 0 else 100.0
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
summary += "
|
| 58 |
-
elif change < -10:
|
| 59 |
-
summary += "\nπ΄ Content may have been reduced or simplified."
|
| 60 |
else:
|
| 61 |
-
summary += "
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
"
|
| 69 |
-
"
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
table_path = "/mnt/data/lo_comparison_table.png"
|
| 81 |
-
plt.savefig(table_path, bbox_inches='tight', dpi=300)
|
| 82 |
-
plt.close()
|
| 83 |
-
|
| 84 |
-
# Bar chart
|
| 85 |
-
fig, ax = plt.subplots(figsize=(10, 4))
|
| 86 |
-
bar_width = 0.35
|
| 87 |
-
index = range(len(los))
|
| 88 |
-
ax.bar(index, old_scores, bar_width, label='Old', alpha=0.7)
|
| 89 |
-
ax.bar([i + bar_width for i in index], new_scores, bar_width, label='New', alpha=0.7)
|
| 90 |
-
ax.set_xticks([i + bar_width / 2 for i in index])
|
| 91 |
-
ax.set_xticklabels(los)
|
| 92 |
-
ax.set_ylabel('Semantic Match (0-1)')
|
| 93 |
-
ax.set_title('Learning Outcome Comparison')
|
| 94 |
-
ax.legend()
|
| 95 |
-
chart_path = "/mnt/data/lo_score_chart.png"
|
| 96 |
-
plt.tight_layout()
|
| 97 |
-
plt.savefig(chart_path)
|
| 98 |
-
plt.close()
|
| 99 |
-
|
| 100 |
-
return summary, table_path, chart_path
|
| 101 |
-
|
| 102 |
-
# Gradio interface
|
| 103 |
-
with gr.Blocks() as demo:
|
| 104 |
-
gr.Markdown("## π§ Transformer-Based Course Content Comparator")
|
| 105 |
-
with gr.Row():
|
| 106 |
-
old_pdf_input = gr.File(label="π Old Handout (PDF)", file_types=[".pdf"])
|
| 107 |
-
new_pdf_input = gr.File(label="π New Handout (PDF)", file_types=[".pdf"])
|
| 108 |
-
lo_input = gr.File(label="π Learning Outcomes (DOCX)", file_types=[".docx"])
|
| 109 |
-
submit_btn = gr.Button("π Analyze")
|
| 110 |
-
summary_output = gr.Textbox(label="Summary")
|
| 111 |
-
lo_table_output = gr.Image(label="π LO Change Table")
|
| 112 |
-
lo_chart_output = gr.Image(label="π LO Match Chart")
|
| 113 |
-
|
| 114 |
-
submit_btn.click(fn=compare_handouts, inputs=[old_pdf_input, new_pdf_input, lo_input],
|
| 115 |
-
outputs=[summary_output, lo_table_output, lo_chart_output])
|
| 116 |
-
|
| 117 |
-
demo.launch()
|
|
|
|
| 1 |
|
| 2 |
|
| 3 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 4 |
import fitz # PyMuPDF
|
| 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):
|
|
|
|
| 14 |
try:
|
| 15 |
+
with fitz.open(stream=file_obj, filetype="pdf") as doc:
|
| 16 |
+
text = ""
|
| 17 |
for page in doc:
|
| 18 |
text += page.get_text()
|
| 19 |
+
return text if text.strip() else None
|
| 20 |
except Exception as e:
|
| 21 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
def semantic_similarity(text1, text2):
|
| 24 |
+
emb1 = model.encode([text1], convert_to_tensor=True)
|
| 25 |
+
emb2 = model.encode([text2], convert_to_tensor=True)
|
| 26 |
+
return float(util.pytorch_cos_sim(emb1, emb2)[0][0])
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
def compare_docs(old_pdf, new_pdf):
|
| 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 updates detected, mostly similar content."
|
| 43 |
+
elif change_percent < 40:
|
| 44 |
+
summary += "π Moderate content updates detected."
|
|
|
|
|
|
|
| 45 |
else:
|
| 46 |
+
summary += "π Major revisions and new content identified."
|
| 47 |
+
|
| 48 |
+
return summary, None
|
| 49 |
+
|
| 50 |
+
iface = gr.Interface(
|
| 51 |
+
fn=compare_docs,
|
| 52 |
+
inputs=[
|
| 53 |
+
gr.File(label="Upload Old Handout (PDF)", file_types=[".pdf"]),
|
| 54 |
+
gr.File(label="Upload New Handout (PDF)", file_types=[".pdf"])
|
| 55 |
+
],
|
| 56 |
+
outputs=[
|
| 57 |
+
gr.Textbox(label="Comparison Summary"),
|
| 58 |
+
gr.Plot(label="(Coming Soon) Visual Summary")
|
| 59 |
+
],
|
| 60 |
+
title="π Course Handout Comparator with Semantic AI",
|
| 61 |
+
description="Upload old and new PDFs to see how much content has changed. Uses transformer model for expert-like judgment.",
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|