Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +70 -38
src/streamlit_app.py
CHANGED
@@ -1,40 +1,72 @@
|
|
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 |
+
def load_summarizer():
|
11 |
+
return pipeline("summarization", model="t5-small")
|
12 |
+
|
13 |
+
|
14 |
+
# PDF text extraction
|
15 |
+
def extract_text_from_pdf(uploaded_file):
|
16 |
+
text = ""
|
17 |
+
try:
|
18 |
+
with fitz.open(stream=uploaded_file.read(), filetype="pdf") as doc:
|
19 |
+
for page in doc:
|
20 |
+
text += page.get_text()
|
21 |
+
except Exception as e:
|
22 |
+
st.error(f"Error extracting text from PDF: {e}")
|
23 |
+
return text
|
24 |
+
|
25 |
+
# Summarize text in chunks
|
26 |
+
def summarize_text(text, summarizer, max_chunk_length=2000):
|
27 |
+
chunks = [text[i:i + max_chunk_length] for i in range(0, len(text), max_chunk_length)]
|
28 |
+
summary = ""
|
29 |
+
for chunk in chunks:
|
30 |
+
result = summarizer(chunk, max_length=130, min_length=30, do_sample=False) # Corrected 'false' to 'False'
|
31 |
+
summary += result[0]['summary_text'] + "\n"
|
32 |
+
return summary.strip()
|
33 |
+
|
34 |
+
# Load summarizer model
|
35 |
+
summarizer = load_summarizer()
|
36 |
+
|
37 |
+
# Tabs
|
38 |
+
tab1, tab2, tab3 = st.tabs(["π Summarize Notes", "β Ask a Doubt", "π¬ Feedback"])
|
39 |
+
|
40 |
+
# Tab 1: Summarizer
|
41 |
+
with tab1:
|
42 |
+
st.header("π Upload Notes & Get Summary")
|
43 |
+
st.write("Upload your class notes in PDF format to receive a summarized version.")
|
44 |
+
uploaded_pdf = st.file_uploader("Upload your PDF notes (PDF only)", type=["pdf"])
|
45 |
+
|
46 |
+
if uploaded_pdf:
|
47 |
+
with st.spinner("Extracting text from PDF..."):
|
48 |
+
pdf_text = extract_text_from_pdf(uploaded_pdf)
|
49 |
+
|
50 |
+
if pdf_text.strip():
|
51 |
+
st.subheader("π Extracted Text (Preview)")
|
52 |
+
st.text_area("Raw Text", pdf_text[:1000] + "...", height=200)
|
53 |
+
|
54 |
+
if st.button("βοΈ Summarize"):
|
55 |
+
with st.spinner("Summarizing... Please wait."):
|
56 |
+
summary = summarize_text(pdf_text, summarizer)
|
57 |
+
st.subheader("β
Summary")
|
58 |
+
st.text_area("Summary Output", summary, height=300)
|
59 |
+
|
60 |
+
st.download_button("β¬οΈ Download Summary", summary, file_name="summary.txt")
|
61 |
+
else:
|
62 |
+
st.warning("β οΈ No text found in the uploaded PDF.")
|
63 |
+
|
64 |
+
# Tab 2: Ask a Doubt (coming soon)
|
65 |
+
with tab2:
|
66 |
+
st.header("β Ask a Doubt")
|
67 |
+
st.info("π§ This feature is under development. Youβll soon be able to chat with your notes using AI!")
|
68 |
+
|
69 |
+
# Tab 3: Feedback (coming soon)
|
70 |
+
with tab3:
|
71 |
+
st.header("π¬ User Feedback")
|
72 |
+
st.info("π¬ A feedback form will be added here to collect your thoughts and improve PrepPal.")
|