harshithasudhakar commited on
Commit
877c158
Β·
verified Β·
1 Parent(s): d5e24d9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -32
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
- # Load pretrained model for simplification
7
- simplifier = pipeline("summarization", model="facebook/bart-large-cnn")
8
-
9
- def simplify_text(text):
10
- """Simplifies a given academic text using a pretrained model."""
11
- simplified = simplifier(text, max_length=96, min_length=30, do_sample=False)
12
- return simplified[0]['summary_text']
13
-
14
- def extract_text_from_pdf(pdf_file):
15
- """Extracts text from an uploaded PDF file stream."""
16
- text = ""
17
- with fitz.open(stream=pdf_file.read(), filetype="pdf") as doc:
18
- for page in doc:
19
- text += page.get_text()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  return text
21
 
22
- # Streamlit UI
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 your text:")
28
- if st.button("Simplify") and user_text:
29
- simplified_text = simplify_text(user_text)
30
- st.subheader("Simplified Text:")
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
- extracted_text = extract_text_from_pdf(uploaded_file)
37
- st.subheader("Extracted Text from PDF:")
38
- st.text_area("Extracted Text", extracted_text, height=200)
39
-
40
- if st.button("Simplify Extracted Text"):
41
- simplified_text = simplify_text(extracted_text[:1000]) # Limit length for model input
42
- st.subheader("Simplified Text:")
43
- st.text_area("Simplified Output", simplified_text, height=150)
44
-
45
- st.write("\nMade by Harshitha")
 
 
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.")