Update app.py
Browse files
app.py
CHANGED
|
@@ -3,43 +3,61 @@ from transformers import pipeline
|
|
| 3 |
import streamlit as st
|
| 4 |
import fitz # PyMuPDF for PDF text extraction
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
"
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
return text
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
st.title("Text Simplification with Pretrained Model")
|
| 24 |
option = st.radio("Choose input type:", ("Text Input", "Upload PDF"))
|
| 25 |
|
| 26 |
if option == "Text Input":
|
| 27 |
-
user_text = st.text_area("Enter
|
| 28 |
-
if st.button("Simplify") and user_text:
|
| 29 |
-
simplified_text = simplify_text(user_text)
|
| 30 |
-
st.
|
| 31 |
-
st.text_area("Simplified Output", simplified_text, height=150)
|
| 32 |
|
| 33 |
elif option == "Upload PDF":
|
| 34 |
-
uploaded_file = st.file_uploader("Upload a PDF", type=["pdf"])
|
| 35 |
if uploaded_file:
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
st.
|
| 44 |
-
|
| 45 |
-
st.
|
|
|
|
|
|
| 3 |
import streamlit as st
|
| 4 |
import fitz # PyMuPDF for PDF text extraction
|
| 5 |
|
| 6 |
+
st.set_page_config(page_title="Text Simplifier", layout="centered")
|
| 7 |
+
|
| 8 |
+
st.title("📚 Jargon Simplifier")
|
| 9 |
+
st.write("This tool simplifies complex or academic text into easier, plain language.")
|
| 10 |
+
|
| 11 |
+
# ---------------------------- Available Models ----------------------------
|
| 12 |
+
MODEL_OPTIONS = {
|
| 13 |
+
"PEGASUS (Simplification - pszemraj)": "pszemraj/pegasus-xsum-simplify",
|
| 14 |
+
"T5 Small (Prompted Simplify)": "t5-small",
|
| 15 |
+
"T5 Base (Prompted Simplify)": "t5-base"
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
# ---------------------------- Model Selection ----------------------------
|
| 19 |
+
selected_model = st.selectbox("Choose a model:", list(MODEL_OPTIONS.keys()))
|
| 20 |
+
model_name = MODEL_OPTIONS[selected_model]
|
| 21 |
+
|
| 22 |
+
@st.cache_resource(show_spinner=True)
|
| 23 |
+
def load_model(name):
|
| 24 |
+
return pipeline("text2text-generation", model=name)
|
| 25 |
+
|
| 26 |
+
simplifier = load_model(model_name)
|
| 27 |
+
|
| 28 |
+
# ---------------------------- Simplification Function ----------------------------
|
| 29 |
+
def simplify_text(text, model_name):
|
| 30 |
+
if "t5" in model_name:
|
| 31 |
+
text = "simplify: " + text # T5 needs task prefix
|
| 32 |
+
output = simplifier(text, max_length=256, min_length=30, do_sample=False)
|
| 33 |
+
return output[0]['generated_text']
|
| 34 |
+
|
| 35 |
+
# ---------------------------- PDF Extraction ----------------------------
|
| 36 |
+
def extract_text_from_pdf(uploaded_file):
|
| 37 |
+
with fitz.open(stream=uploaded_file.read(), filetype="pdf") as doc:
|
| 38 |
+
text = "\n".join(page.get_text("text") for page in doc)
|
| 39 |
return text
|
| 40 |
|
| 41 |
+
# ---------------------------- UI ----------------------------
|
|
|
|
| 42 |
option = st.radio("Choose input type:", ("Text Input", "Upload PDF"))
|
| 43 |
|
| 44 |
if option == "Text Input":
|
| 45 |
+
user_text = st.text_area("✍️ Enter complex text here:")
|
| 46 |
+
if st.button("Simplify") and user_text.strip():
|
| 47 |
+
simplified_text = simplify_text(user_text.strip(), model_name)
|
| 48 |
+
st.text_area("✅ Simplified Output:", value=simplified_text, height=200)
|
|
|
|
| 49 |
|
| 50 |
elif option == "Upload PDF":
|
| 51 |
+
uploaded_file = st.file_uploader("📄 Upload a PDF file", type=["pdf"])
|
| 52 |
if uploaded_file:
|
| 53 |
+
try:
|
| 54 |
+
extracted_text = extract_text_from_pdf(uploaded_file)
|
| 55 |
+
preview = st.text_area("📄 Extracted Text Preview (first 1000 chars):", value=extracted_text[:1000], height=200)
|
| 56 |
+
if st.button("Simplify Extracted Text"):
|
| 57 |
+
simplified_text = simplify_text(extracted_text[:1000], model_name)
|
| 58 |
+
st.text_area("✅ Simplified Output:", value=simplified_text, height=200)
|
| 59 |
+
except Exception as e:
|
| 60 |
+
st.error(f"❌ Error reading PDF: {e}")
|
| 61 |
+
|
| 62 |
+
st.markdown("---")
|
| 63 |
+
st.caption("Made with ❤️ using HuggingFace Transformers and Streamlit.")
|