Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +30 -28
src/streamlit_app.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
import os
|
2 |
-
os.environ["TRANSFORMERS_CACHE"] = "/cache" #
|
3 |
|
4 |
import streamlit as st
|
5 |
import fitz # PyMuPDF
|
@@ -15,13 +15,13 @@ st.set_page_config(
|
|
15 |
}
|
16 |
)
|
17 |
|
18 |
-
# Load summarizer model
|
19 |
@st.cache_resource
|
20 |
def load_summarizer():
|
21 |
try:
|
22 |
return pipeline(
|
23 |
"summarization",
|
24 |
-
model="
|
25 |
device=-1 # Use CPU (more reliable in Spaces)
|
26 |
)
|
27 |
except Exception as e:
|
@@ -46,21 +46,21 @@ def extract_text_from_pdf(uploaded_file):
|
|
46 |
return ""
|
47 |
|
48 |
# Summarize text in chunks
|
49 |
-
def summarize_text(text, summarizer,
|
50 |
-
if not text:
|
51 |
return ""
|
52 |
|
53 |
try:
|
54 |
-
chunks = [text[i:i+
|
55 |
summary = ""
|
56 |
for chunk in chunks:
|
57 |
result = summarizer(
|
58 |
chunk,
|
59 |
-
max_length=
|
60 |
min_length=30,
|
61 |
do_sample=False
|
62 |
)
|
63 |
-
summary += result[0]['summary_text'] + "
|
64 |
return summary.strip()
|
65 |
except Exception as e:
|
66 |
st.error(f"β Summarization failed: {str(e)}")
|
@@ -71,36 +71,35 @@ summarizer = load_summarizer()
|
|
71 |
|
72 |
# UI Layout
|
73 |
st.title("π PrepPal - Study Assistant")
|
74 |
-
st.
|
75 |
-
|
76 |
-
tab1, tab2 = st.tabs(["π Summarize Notes", "π¬ Feedback"])
|
77 |
|
78 |
with tab1:
|
79 |
st.header("PDF Summarizer")
|
80 |
-
|
|
|
|
|
81 |
"Choose a PDF file (max 5MB)",
|
82 |
type=["pdf"],
|
83 |
accept_multiple_files=False
|
84 |
)
|
85 |
|
86 |
-
if
|
87 |
with st.spinner("Extracting text..."):
|
88 |
-
|
89 |
|
90 |
-
if
|
91 |
st.subheader("Extracted Text Preview")
|
92 |
-
st.text_area("",
|
93 |
|
94 |
-
if st.button("
|
95 |
-
with st.spinner("Summarizing..."):
|
96 |
-
summary = summarize_text(
|
97 |
|
98 |
if summary:
|
99 |
-
st.subheader("
|
100 |
-
st.
|
101 |
-
|
102 |
st.download_button(
|
103 |
-
"Download Summary",
|
104 |
data=summary,
|
105 |
file_name="summary.txt",
|
106 |
mime="text/plain"
|
@@ -109,12 +108,15 @@ with tab1:
|
|
109 |
st.warning("No summary generated")
|
110 |
|
111 |
with tab2:
|
112 |
-
st.header("
|
113 |
-
st.
|
114 |
-
|
|
|
|
|
|
|
115 |
if st.button("Submit Feedback"):
|
116 |
-
st.success("Thank you
|
117 |
|
118 |
-
#
|
119 |
st.markdown("---")
|
120 |
st.caption("PrepPal v1.0 | AI-powered study assistant")
|
|
|
1 |
import os
|
2 |
+
os.environ["TRANSFORMERS_CACHE"] = "/cache" # Hugging Face Spaces cache directory
|
3 |
|
4 |
import streamlit as st
|
5 |
import fitz # PyMuPDF
|
|
|
15 |
}
|
16 |
)
|
17 |
|
18 |
+
# Load summarizer model with error handling
|
19 |
@st.cache_resource
|
20 |
def load_summarizer():
|
21 |
try:
|
22 |
return pipeline(
|
23 |
"summarization",
|
24 |
+
model="sshleifer/distilbart-cnn-12-6",
|
25 |
device=-1 # Use CPU (more reliable in Spaces)
|
26 |
)
|
27 |
except Exception as e:
|
|
|
46 |
return ""
|
47 |
|
48 |
# Summarize text in chunks
|
49 |
+
def summarize_text(text, summarizer, max_chunk_length=2000):
|
50 |
+
if not text or not summarizer:
|
51 |
return ""
|
52 |
|
53 |
try:
|
54 |
+
chunks = [text[i:i+max_chunk_length] for i in range(0, len(text), max_chunk_length)]
|
55 |
summary = ""
|
56 |
for chunk in chunks:
|
57 |
result = summarizer(
|
58 |
chunk,
|
59 |
+
max_length=130,
|
60 |
min_length=30,
|
61 |
do_sample=False
|
62 |
)
|
63 |
+
summary += result[0]['summary_text'] + "\n"
|
64 |
return summary.strip()
|
65 |
except Exception as e:
|
66 |
st.error(f"β Summarization failed: {str(e)}")
|
|
|
71 |
|
72 |
# UI Layout
|
73 |
st.title("π PrepPal - Study Assistant")
|
74 |
+
tab1, tab2, tab3 = st.tabs(["π Summarize Notes", "β Ask a Doubt", "π¬ Feedback"])
|
|
|
|
|
75 |
|
76 |
with tab1:
|
77 |
st.header("PDF Summarizer")
|
78 |
+
st.write("Upload your class notes in PDF format to receive a summarized version.")
|
79 |
+
|
80 |
+
uploaded_pdf = st.file_uploader(
|
81 |
"Choose a PDF file (max 5MB)",
|
82 |
type=["pdf"],
|
83 |
accept_multiple_files=False
|
84 |
)
|
85 |
|
86 |
+
if uploaded_pdf and summarizer:
|
87 |
with st.spinner("Extracting text..."):
|
88 |
+
pdf_text = extract_text_from_pdf(uploaded_pdf)
|
89 |
|
90 |
+
if pdf_text:
|
91 |
st.subheader("Extracted Text Preview")
|
92 |
+
st.text_area("", pdf_text[:1000] + "...", height=200, disabled=True)
|
93 |
|
94 |
+
if st.button("βοΈ Summarize"):
|
95 |
+
with st.spinner("Summarizing... Please wait."):
|
96 |
+
summary = summarize_text(pdf_text, summarizer)
|
97 |
|
98 |
if summary:
|
99 |
+
st.subheader("β
Summary")
|
100 |
+
st.text_area("Summary Output", summary, height=300)
|
|
|
101 |
st.download_button(
|
102 |
+
"β¬οΈ Download Summary",
|
103 |
data=summary,
|
104 |
file_name="summary.txt",
|
105 |
mime="text/plain"
|
|
|
108 |
st.warning("No summary generated")
|
109 |
|
110 |
with tab2:
|
111 |
+
st.header("Ask Questions About Your Notes")
|
112 |
+
st.info("π§ This feature is coming soon! You'll be able to ask questions about your uploaded notes.")
|
113 |
+
|
114 |
+
with tab3:
|
115 |
+
st.header("Help Improve PrepPal")
|
116 |
+
feedback = st.text_area("Your feedback or suggestions")
|
117 |
if st.button("Submit Feedback"):
|
118 |
+
st.success("Thank you for your feedback! We'll use it to improve PrepPal.")
|
119 |
|
120 |
+
# Footer
|
121 |
st.markdown("---")
|
122 |
st.caption("PrepPal v1.0 | AI-powered study assistant")
|