Spaces:
Sleeping
Sleeping
Download src/streamlit_app.py from MSivasandeep/Prep-pal: direct link, hf CLI and curl.
- Browser
- Download file 2.89 kB
-
https://huggingface.co/spaces/MSivasandeep/Prep-pal/resolve/main/src/streamlit_app.py
- Command line
-
hf download hf://spaces/MSivasandeep/Prep-pal/src/streamlit_app.py
-
curl -L -o streamlit_app.py https://huggingface.co/spaces/MSivasandeep/Prep-pal/resolve/main/src/streamlit_app.py
2.89 kB
| import os | |
| # β Step 1: Set HuggingFace and Torch cache to a writable location | |
| os.environ["TRANSFORMERS_CACHE"] = "/tmp/hf_cache" | |
| os.environ["HF_HOME"] = "/tmp/hf_cache" | |
| os.environ["HF_DATASETS_CACHE"] = "/tmp/hf_cache" | |
| os.environ["HF_METRICS_CACHE"] = "/tmp/hf_cache" | |
| 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) | |
| if result and 'summary_text' in result[0]: | |
| summary += result[0]['summary_text'] + "\n" | |
| return summary.strip() | |
| # Load model | |
| summarizer = load_summarizer() | |
| # UI Tabs | |
| tab1, tab2, tab3 = st.tabs(["π Summarize Notes", "β Ask a Doubt", "π¬ Feedback"]) | |
| # Tab 1: Summarize Notes | |
| 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 | |
| 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 | |
| with tab3: | |
| st.header("π¬ User Feedback") | |
| st.info("π¬ A feedback form will be added here to collect your thoughts and improve PrepPal.") | |