Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +14 -10
src/streamlit_app.py
CHANGED
@@ -1,19 +1,18 @@
|
|
1 |
-
import os
|
2 |
-
os.environ["TRANSFORMERS_CACHE"] = "/app/.cache"
|
3 |
-
|
4 |
import streamlit as st
|
5 |
import fitz # PyMuPDF
|
6 |
from transformers import pipeline
|
|
|
7 |
|
8 |
# Set page config
|
9 |
st.set_page_config(page_title="PrepPal", page_icon="π", layout="wide")
|
10 |
|
11 |
-
# Load summarizer model
|
12 |
@st.cache_resource
|
13 |
def load_summarizer():
|
14 |
-
|
|
|
15 |
|
16 |
-
#
|
17 |
def extract_text_from_pdf(uploaded_file):
|
18 |
text = ""
|
19 |
try:
|
@@ -21,10 +20,10 @@ def extract_text_from_pdf(uploaded_file):
|
|
21 |
for page in doc:
|
22 |
text += page.get_text()
|
23 |
except Exception as e:
|
24 |
-
st.error(f"Error extracting text from PDF: {e}")
|
25 |
return text
|
26 |
|
27 |
-
# Summarize text in chunks
|
28 |
def summarize_text(text, summarizer, max_chunk_length=2000):
|
29 |
chunks = [text[i:i + max_chunk_length] for i in range(0, len(text), max_chunk_length)]
|
30 |
summary = ""
|
@@ -33,17 +32,20 @@ def summarize_text(text, summarizer, max_chunk_length=2000):
|
|
33 |
summary += result[0]['summary_text'] + "\n"
|
34 |
return summary.strip()
|
35 |
|
|
|
36 |
summarizer = load_summarizer()
|
37 |
|
|
|
38 |
tab1, tab2, tab3 = st.tabs(["π Summarize Notes", "β Ask a Doubt", "π¬ Feedback"])
|
39 |
|
|
|
40 |
with tab1:
|
41 |
st.header("π Upload Notes & Get Summary")
|
42 |
st.write("Upload your class notes in PDF format to receive a summarized version.")
|
43 |
uploaded_pdf = st.file_uploader("Upload your PDF notes (PDF only)", type=["pdf"])
|
44 |
|
45 |
if uploaded_pdf:
|
46 |
-
with st.spinner("Extracting text from PDF..."):
|
47 |
pdf_text = extract_text_from_pdf(uploaded_pdf)
|
48 |
|
49 |
if pdf_text.strip():
|
@@ -51,7 +53,7 @@ with tab1:
|
|
51 |
st.text_area("Raw Text", pdf_text[:1000] + "...", height=200)
|
52 |
|
53 |
if st.button("βοΈ Summarize"):
|
54 |
-
with st.spinner("Summarizing... Please wait."):
|
55 |
summary = summarize_text(pdf_text, summarizer)
|
56 |
st.subheader("β
Summary")
|
57 |
st.text_area("Summary Output", summary, height=300)
|
@@ -59,10 +61,12 @@ with tab1:
|
|
59 |
else:
|
60 |
st.warning("β οΈ No text found in the uploaded PDF.")
|
61 |
|
|
|
62 |
with tab2:
|
63 |
st.header("β Ask a Doubt")
|
64 |
st.info("π§ This feature is under development. Youβll soon be able to chat with your notes using AI!")
|
65 |
|
|
|
66 |
with tab3:
|
67 |
st.header("π¬ User Feedback")
|
68 |
st.info("π¬ A feedback form will be added here to collect your thoughts and improve PrepPal.")
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
import fitz # PyMuPDF
|
3 |
from transformers import pipeline
|
4 |
+
import tempfile
|
5 |
|
6 |
# Set page config
|
7 |
st.set_page_config(page_title="PrepPal", page_icon="π", layout="wide")
|
8 |
|
9 |
+
# Load summarizer model with safe temp cache dir
|
10 |
@st.cache_resource
|
11 |
def load_summarizer():
|
12 |
+
temp_cache_dir = tempfile.mkdtemp()
|
13 |
+
return pipeline("summarization", model="t5-small", cache_dir=temp_cache_dir)
|
14 |
|
15 |
+
# Extract text from uploaded PDF
|
16 |
def extract_text_from_pdf(uploaded_file):
|
17 |
text = ""
|
18 |
try:
|
|
|
20 |
for page in doc:
|
21 |
text += page.get_text()
|
22 |
except Exception as e:
|
23 |
+
st.error(f"β Error extracting text from PDF: {e}")
|
24 |
return text
|
25 |
|
26 |
+
# Summarize large text in manageable chunks
|
27 |
def summarize_text(text, summarizer, max_chunk_length=2000):
|
28 |
chunks = [text[i:i + max_chunk_length] for i in range(0, len(text), max_chunk_length)]
|
29 |
summary = ""
|
|
|
32 |
summary += result[0]['summary_text'] + "\n"
|
33 |
return summary.strip()
|
34 |
|
35 |
+
# Load the summarizer pipeline
|
36 |
summarizer = load_summarizer()
|
37 |
|
38 |
+
# Streamlit UI with tabs
|
39 |
tab1, tab2, tab3 = st.tabs(["π Summarize Notes", "β Ask a Doubt", "π¬ Feedback"])
|
40 |
|
41 |
+
# Tab 1: Upload and summarize notes
|
42 |
with tab1:
|
43 |
st.header("π Upload Notes & Get Summary")
|
44 |
st.write("Upload your class notes in PDF format to receive a summarized version.")
|
45 |
uploaded_pdf = st.file_uploader("Upload your PDF notes (PDF only)", type=["pdf"])
|
46 |
|
47 |
if uploaded_pdf:
|
48 |
+
with st.spinner("β³ Extracting text from PDF..."):
|
49 |
pdf_text = extract_text_from_pdf(uploaded_pdf)
|
50 |
|
51 |
if pdf_text.strip():
|
|
|
53 |
st.text_area("Raw Text", pdf_text[:1000] + "...", height=200)
|
54 |
|
55 |
if st.button("βοΈ Summarize"):
|
56 |
+
with st.spinner("π€ Summarizing... Please wait."):
|
57 |
summary = summarize_text(pdf_text, summarizer)
|
58 |
st.subheader("β
Summary")
|
59 |
st.text_area("Summary Output", summary, height=300)
|
|
|
61 |
else:
|
62 |
st.warning("β οΈ No text found in the uploaded PDF.")
|
63 |
|
64 |
+
# Tab 2: Ask a doubt (placeholder)
|
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 (placeholder)
|
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.")
|