Update src/streamlit_app.py
Browse files- src/streamlit_app.py +139 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,141 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import fitz # PyMuPDF for PDF extraction
|
| 3 |
+
from transformers import pipeline
|
| 4 |
|
| 5 |
+
# Set page config
|
| 6 |
+
st.set_page_config(page_title="PrepPal", page_icon="π", layout="wide")
|
| 7 |
+
|
| 8 |
+
# Load summarizer model (using Hugging Face pipeline)
|
| 9 |
+
@st.cache_resource
|
| 10 |
+
import streamlit as st
|
| 11 |
+
import fitz # PyMuPDF for PDF extraction
|
| 12 |
+
from transformers import pipeline
|
| 13 |
+
|
| 14 |
+
# Set page config
|
| 15 |
+
st.set_page_config(page_title="PrepPal", page_icon="π", layout="wide")
|
| 16 |
+
|
| 17 |
+
# Load summarizer model (using Hugging Face pipeline)
|
| 18 |
+
@st.cache_resource
|
| 19 |
+
def load_summarizer():
|
| 20 |
+
return pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
# PDF text extraction
|
| 24 |
+
def extract_text_from_pdf(uploaded_file):
|
| 25 |
+
text = ""
|
| 26 |
+
try:
|
| 27 |
+
with fitz.open(stream=uploaded_file.read(), filetype="pdf") as doc:
|
| 28 |
+
for page in doc:
|
| 29 |
+
text += page.get_text()
|
| 30 |
+
except Exception as e:
|
| 31 |
+
st.error(f"Error extracting text from PDF: {e}")
|
| 32 |
+
return text
|
| 33 |
+
|
| 34 |
+
# Summarize text in chunks
|
| 35 |
+
def summarize_text(text, summarizer, max_chunk_length=2000):
|
| 36 |
+
chunks = [text[i:i + max_chunk_length] for i in range(0, len(text), max_chunk_length)]
|
| 37 |
+
summary = ""
|
| 38 |
+
for chunk in chunks:
|
| 39 |
+
result = summarizer(chunk, max_length=130, min_length=30, do_sample=False) # Corrected 'false' to 'False'
|
| 40 |
+
summary += result[0]['summary_text'] + "\n"
|
| 41 |
+
return summary.strip()
|
| 42 |
+
|
| 43 |
+
# Load summarizer model
|
| 44 |
+
summarizer = load_summarizer()
|
| 45 |
+
|
| 46 |
+
# Tabs
|
| 47 |
+
tab1, tab2, tab3 = st.tabs(["π Summarize Notes", "β Ask a Doubt", "π¬ Feedback"])
|
| 48 |
+
|
| 49 |
+
# Tab 1: Summarizer
|
| 50 |
+
with tab1:
|
| 51 |
+
st.header("π Upload Notes & Get Summary")
|
| 52 |
+
st.write("Upload your class notes in PDF format to receive a summarized version.")
|
| 53 |
+
uploaded_pdf = st.file_uploader("Upload your PDF notes (PDF only)", type=["pdf"])
|
| 54 |
+
|
| 55 |
+
if uploaded_pdf:
|
| 56 |
+
with st.spinner("Extracting text from PDF..."):
|
| 57 |
+
pdf_text = extract_text_from_pdf(uploaded_pdf)
|
| 58 |
+
|
| 59 |
+
if pdf_text.strip():
|
| 60 |
+
st.subheader("π Extracted Text (Preview)")
|
| 61 |
+
st.text_area("Raw Text", pdf_text[:1000] + "...", height=200)
|
| 62 |
+
|
| 63 |
+
if st.button("βοΈ Summarize"):
|
| 64 |
+
with st.spinner("Summarizing... Please wait."):
|
| 65 |
+
summary = summarize_text(pdf_text, summarizer)
|
| 66 |
+
st.subheader("β
Summary")
|
| 67 |
+
st.text_area("Summary Output", summary, height=300)
|
| 68 |
+
|
| 69 |
+
st.download_button("β¬οΈ Download Summary", summary, file_name="summary.txt")
|
| 70 |
+
else:
|
| 71 |
+
st.warning("β οΈ No text found in the uploaded PDF.")
|
| 72 |
+
|
| 73 |
+
# Tab 2: Ask a Doubt (coming soon)
|
| 74 |
+
with tab2:
|
| 75 |
+
st.header("β Ask a Doubt")
|
| 76 |
+
st.info("π§ This feature is under development. Youβll soon be able to chat with your notes using AI!")
|
| 77 |
+
|
| 78 |
+
# Tab 3: Feedback (coming soon)
|
| 79 |
+
with tab3:
|
| 80 |
+
st.header("π¬ User Feedback")
|
| 81 |
+
st.info("π¬ A feedback form will be added here to collect your thoughts and improve PrepPal.")
|
| 82 |
+
|
| 83 |
+
# PDF text extraction
|
| 84 |
+
def extract_text_from_pdf(uploaded_file):
|
| 85 |
+
text = ""
|
| 86 |
+
try:
|
| 87 |
+
with fitz.open(stream=uploaded_file.read(), filetype="pdf") as doc:
|
| 88 |
+
for page in doc:
|
| 89 |
+
text += page.get_text()
|
| 90 |
+
except Exception as e:
|
| 91 |
+
st.error(f"Error extracting text from PDF: {e}")
|
| 92 |
+
return text
|
| 93 |
+
|
| 94 |
+
# Summarize text in chunks
|
| 95 |
+
def summarize_text(text, summarizer, max_chunk_length=2000):
|
| 96 |
+
chunks = [text[i:i + max_chunk_length] for i in range(0, len(text), max_chunk_length)]
|
| 97 |
+
summary = ""
|
| 98 |
+
for chunk in chunks:
|
| 99 |
+
result = summarizer(chunk, max_length=130, min_length=30, do_sample=False) # Corrected 'false' to 'False'
|
| 100 |
+
summary += result[0]['summary_text'] + "\n"
|
| 101 |
+
return summary.strip()
|
| 102 |
+
|
| 103 |
+
# Load summarizer model
|
| 104 |
+
summarizer = load_summarizer()
|
| 105 |
+
|
| 106 |
+
# Tabs
|
| 107 |
+
tab1, tab2, tab3 = st.tabs(["π Summarize Notes", "β Ask a Doubt", "π¬ Feedback"])
|
| 108 |
+
|
| 109 |
+
# Tab 1: Summarizer
|
| 110 |
+
with tab1:
|
| 111 |
+
st.header("π Upload Notes & Get Summary")
|
| 112 |
+
st.write("Upload your class notes in PDF format to receive a summarized version.")
|
| 113 |
+
uploaded_pdf = st.file_uploader("Upload your PDF notes (PDF only)", type=["pdf"])
|
| 114 |
+
|
| 115 |
+
if uploaded_pdf:
|
| 116 |
+
with st.spinner("Extracting text from PDF..."):
|
| 117 |
+
pdf_text = extract_text_from_pdf(uploaded_pdf)
|
| 118 |
+
|
| 119 |
+
if pdf_text.strip():
|
| 120 |
+
st.subheader("π Extracted Text (Preview)")
|
| 121 |
+
st.text_area("Raw Text", pdf_text[:1000] + "...", height=200)
|
| 122 |
+
|
| 123 |
+
if st.button("βοΈ Summarize"):
|
| 124 |
+
with st.spinner("Summarizing... Please wait."):
|
| 125 |
+
summary = summarize_text(pdf_text, summarizer)
|
| 126 |
+
st.subheader("β
Summary")
|
| 127 |
+
st.text_area("Summary Output", summary, height=300)
|
| 128 |
+
|
| 129 |
+
st.download_button("β¬οΈ Download Summary", summary, file_name="summary.txt")
|
| 130 |
+
else:
|
| 131 |
+
st.warning("β οΈ No text found in the uploaded PDF.")
|
| 132 |
+
|
| 133 |
+
# Tab 2: Ask a Doubt (coming soon)
|
| 134 |
+
with tab2:
|
| 135 |
+
st.header("β Ask a Doubt")
|
| 136 |
+
st.info("π§ This feature is under development. Youβll soon be able to chat with your notes using AI!")
|
| 137 |
+
|
| 138 |
+
# Tab 3: Feedback (coming soon)
|
| 139 |
+
with tab3:
|
| 140 |
+
st.header("π¬ User Feedback")
|
| 141 |
+
st.info("π¬ A feedback form will be added here to collect your thoughts and improve PrepPal.")
|