Spaces:
Sleeping
Sleeping
import os | |
os.environ["TRANSFORMERS_CACHE"] = "/tmp/huggingface" | |
import streamlit as st | |
import fitz # PyMuPDF | |
from transformers import pipeline | |
# Set page config | |
st.set_page_config(page_title="PrepPal", page_icon="π", layout="wide") | |
# Load summarizer model | |
def load_summarizer(): | |
return pipeline("summarization", model="sshleifer/distilbart-cnn-12-6") | |
# Extract text from uploaded PDF | |
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 large text | |
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) | |
summary += result[0]['summary_text'] + "\n" | |
return summary.strip() | |
# Load model | |
summarizer = load_summarizer() | |
# UI | |
tab1, tab2, tab3 = st.tabs(["π Summarize Notes", "β Ask a Doubt", "π¬ Feedback"]) | |
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.") | |
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!") | |
with tab3: | |
st.header("π¬ User Feedback") | |
st.info("π¬ A feedback form will be added here to collect your thoughts and improve PrepPal.") |