preppal / src /streamlit_app.py
sathvikk's picture
Update src/streamlit_app.py
4b06e1e verified
import streamlit as st
import fitz # PyMuPDF for PDF extraction
from transformers import pipeline
# Set page config
st.set_page_config(page_title="PrepPal", page_icon="πŸ“˜", layout="wide")
# Load summarizer model (using Hugging Face pipeline)
@st.cache_resource
import streamlit as st
import fitz # PyMuPDF for PDF extraction
from transformers import pipeline
# Set page config
st.set_page_config(page_title="PrepPal", page_icon="πŸ“˜", layout="wide")
# Load summarizer model (using Hugging Face pipeline)
@st.cache_resource
def load_summarizer():
return pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
# PDF text extraction
def extract_text_from_pdf(uploaded_file):
text = ""
try:
with fitz.open(stream=uploaded_file.read(), filetype="pdf") as doc:
for page in doc:
text += page.get_text()
except Exception as e:
st.error(f"Error extracting text from PDF: {e}")
return text
# Summarize text in chunks
def summarize_text(text, summarizer, max_chunk_length=2000):
chunks = [text[i:i + max_chunk_length] for i in range(0, len(text), max_chunk_length)]
summary = ""
for chunk in chunks:
result = summarizer(chunk, max_length=130, min_length=30, do_sample=False) # Corrected 'false' to 'False'
summary += result[0]['summary_text'] + "\n"
return summary.strip()
# Load summarizer model
summarizer = load_summarizer()
# Tabs
tab1, tab2, tab3 = st.tabs(["πŸ“„ Summarize Notes", "❓ Ask a Doubt", "πŸ’¬ Feedback"])
# Tab 1: Summarizer
with tab1:
st.header("πŸ“„ Upload Notes & Get Summary")
st.write("Upload your class notes in PDF format to receive a summarized version.")
uploaded_pdf = st.file_uploader("Upload your PDF notes (PDF only)", type=["pdf"])
if uploaded_pdf:
with st.spinner("Extracting text from PDF..."):
pdf_text = extract_text_from_pdf(uploaded_pdf)
if pdf_text.strip():
st.subheader("πŸ“˜ Extracted Text (Preview)")
st.text_area("Raw Text", pdf_text[:1000] + "...", height=200)
if st.button("βœ‚οΈ Summarize"):
with st.spinner("Summarizing... Please wait."):
summary = summarize_text(pdf_text, summarizer)
st.subheader("βœ… Summary")
st.text_area("Summary Output", summary, height=300)
st.download_button("⬇️ Download Summary", summary, file_name="summary.txt")
else:
st.warning("⚠️ No text found in the uploaded PDF.")
# Tab 2: Ask a Doubt (coming soon)
with tab2:
st.header("❓ Ask a Doubt")
st.info("πŸ”§ This feature is under development. You’ll soon be able to chat with your notes using AI!")
# Tab 3: Feedback (coming soon)
with tab3:
st.header("πŸ’¬ User Feedback")
st.info("πŸ“¬ A feedback form will be added here to collect your thoughts and improve PrepPal.")
# PDF text extraction
def extract_text_from_pdf(uploaded_file):
text = ""
try:
with fitz.open(stream=uploaded_file.read(), filetype="pdf") as doc:
for page in doc:
text += page.get_text()
except Exception as e:
st.error(f"Error extracting text from PDF: {e}")
return text
# Summarize text in chunks
def summarize_text(text, summarizer, max_chunk_length=2000):
chunks = [text[i:i + max_chunk_length] for i in range(0, len(text), max_chunk_length)]
summary = ""
for chunk in chunks:
result = summarizer(chunk, max_length=130, min_length=30, do_sample=False) # Corrected 'false' to 'False'
summary += result[0]['summary_text'] + "\n"
return summary.strip()
# Load summarizer model
summarizer = load_summarizer()
# Tabs
tab1, tab2, tab3 = st.tabs(["πŸ“„ Summarize Notes", "❓ Ask a Doubt", "πŸ’¬ Feedback"])
# Tab 1: Summarizer
with tab1:
st.header("πŸ“„ Upload Notes & Get Summary")
st.write("Upload your class notes in PDF format to receive a summarized version.")
uploaded_pdf = st.file_uploader("Upload your PDF notes (PDF only)", type=["pdf"])
if uploaded_pdf:
with st.spinner("Extracting text from PDF..."):
pdf_text = extract_text_from_pdf(uploaded_pdf)
if pdf_text.strip():
st.subheader("πŸ“˜ Extracted Text (Preview)")
st.text_area("Raw Text", pdf_text[:1000] + "...", height=200)
if st.button("βœ‚οΈ Summarize"):
with st.spinner("Summarizing... Please wait."):
summary = summarize_text(pdf_text, summarizer)
st.subheader("βœ… Summary")
st.text_area("Summary Output", summary, height=300)
st.download_button("⬇️ Download Summary", summary, file_name="summary.txt")
else:
st.warning("⚠️ No text found in the uploaded PDF.")
# Tab 2: Ask a Doubt (coming soon)
with tab2:
st.header("❓ Ask a Doubt")
st.info("πŸ”§ This feature is under development. You’ll soon be able to chat with your notes using AI!")
# Tab 3: Feedback (coming soon)
with tab3:
st.header("πŸ’¬ User Feedback")
st.info("πŸ“¬ A feedback form will be added here to collect your thoughts and improve PrepPal.")