Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +40 -102
src/streamlit_app.py
CHANGED
@@ -6,128 +6,66 @@ import streamlit as st
|
|
6 |
import fitz # PyMuPDF
|
7 |
from transformers import pipeline
|
8 |
|
9 |
-
#
|
10 |
st.set_page_config(
|
11 |
page_title="PrepPal",
|
12 |
page_icon="π",
|
13 |
layout="wide"
|
14 |
)
|
15 |
|
16 |
-
|
17 |
-
@st.cache_resource(show_spinner=False)
|
18 |
def load_model():
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
)
|
26 |
-
except Exception as e:
|
27 |
-
st.error(f"β Model loading failed: {str(e)}")
|
28 |
-
st.error("Please try again later or contact support")
|
29 |
-
return None
|
30 |
|
31 |
-
def
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
with fitz.open(tmp_path) as doc:
|
38 |
-
text = "\n".join([page.get_text() for page in doc])
|
39 |
-
|
40 |
-
os.unlink(tmp_path)
|
41 |
-
return text.strip()
|
42 |
-
except Exception as e:
|
43 |
-
st.error(f"β PDF processing error: {str(e)}")
|
44 |
-
return ""
|
45 |
|
46 |
-
def
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
st.error("AI model not loaded")
|
53 |
-
return ""
|
54 |
-
|
55 |
-
try:
|
56 |
-
# More efficient chunking
|
57 |
-
chunks = [text[i:i+1024] for i in range(0, len(text), 1024)]
|
58 |
-
summaries = []
|
59 |
-
|
60 |
-
progress_bar = st.progress(0)
|
61 |
-
for i, chunk in enumerate(chunks):
|
62 |
-
progress_bar.progress((i + 1) / len(chunks))
|
63 |
-
result = model(
|
64 |
-
chunk,
|
65 |
-
max_length=150,
|
66 |
-
min_length=30,
|
67 |
-
do_sample=False
|
68 |
-
)
|
69 |
-
summaries.append(result[0]['summary_text'])
|
70 |
-
|
71 |
-
return " ".join(summaries)
|
72 |
-
except Exception as e:
|
73 |
-
st.error(f"β Summarization failed: {str(e)}")
|
74 |
-
return ""
|
75 |
|
76 |
def main():
|
77 |
-
st.title("π PrepPal -
|
78 |
|
79 |
-
tab1, tab2
|
80 |
|
81 |
with tab1:
|
82 |
-
st.header("PDF Summarizer")
|
83 |
uploaded_file = st.file_uploader(
|
84 |
-
"Upload PDF (max
|
85 |
-
type=["pdf"]
|
86 |
-
accept_multiple_files=False
|
87 |
)
|
88 |
|
89 |
-
if uploaded_file:
|
90 |
-
if
|
91 |
-
st.
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
if text:
|
97 |
-
with st.expander("View extracted text"):
|
98 |
-
st.text(text[:1000] + "...")
|
99 |
-
|
100 |
-
if st.button("Generate Summary"):
|
101 |
-
with st.spinner("Loading AI model..."):
|
102 |
model = load_model()
|
103 |
-
|
104 |
-
|
105 |
-
st.
|
106 |
-
|
107 |
-
|
108 |
-
if summary:
|
109 |
-
st.subheader("β
Summary")
|
110 |
-
st.write(summary)
|
111 |
-
st.download_button(
|
112 |
-
"β¬οΈ Download Summary",
|
113 |
-
data=summary,
|
114 |
-
file_name="summary.txt",
|
115 |
-
mime="text/plain"
|
116 |
-
)
|
117 |
-
else:
|
118 |
-
st.warning("No summary was generated")
|
119 |
-
else:
|
120 |
-
st.error("Could not load AI model")
|
121 |
|
122 |
with tab2:
|
123 |
-
st.
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
if st.button("Submit Feedback"):
|
130 |
-
st.success("Thank you for your feedback!")
|
131 |
|
132 |
if __name__ == "__main__":
|
133 |
main()
|
|
|
6 |
import fitz # PyMuPDF
|
7 |
from transformers import pipeline
|
8 |
|
9 |
+
# Simple app config
|
10 |
st.set_page_config(
|
11 |
page_title="PrepPal",
|
12 |
page_icon="π",
|
13 |
layout="wide"
|
14 |
)
|
15 |
|
16 |
+
@st.cache_resource
|
|
|
17 |
def load_model():
|
18 |
+
# Using a small, reliable model
|
19 |
+
return pipeline(
|
20 |
+
"summarization",
|
21 |
+
model="Falconsai/text_summarization",
|
22 |
+
device=-1 # CPU only
|
23 |
+
)
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
+
def get_pdf_text(uploaded_file):
|
26 |
+
with tempfile.NamedTemporaryFile(suffix=".pdf") as tmp:
|
27 |
+
tmp.write(uploaded_file.getbuffer())
|
28 |
+
with fitz.open(tmp.name) as doc:
|
29 |
+
return " ".join([page.get_text() for page in doc])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
+
def summarize(text, model):
|
32 |
+
chunks = [text[i:i+800] for i in range(0, len(text), 800)]
|
33 |
+
return " ".join([
|
34 |
+
model(chunk, max_length=120, min_length=30)[0]['summary_text']
|
35 |
+
for chunk in chunks
|
36 |
+
])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
def main():
|
39 |
+
st.title("π PrepPal - Simple Summarizer")
|
40 |
|
41 |
+
tab1, tab2 = st.tabs(["Summarize", "Info"])
|
42 |
|
43 |
with tab1:
|
|
|
44 |
uploaded_file = st.file_uploader(
|
45 |
+
"Upload PDF (max 5MB)",
|
46 |
+
type=["pdf"]
|
|
|
47 |
)
|
48 |
|
49 |
+
if uploaded_file and uploaded_file.size <= 5_000_000:
|
50 |
+
if st.button("Summarize"):
|
51 |
+
with st.spinner("Processing..."):
|
52 |
+
try:
|
53 |
+
text = get_pdf_text(uploaded_file)
|
54 |
+
if text:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
model = load_model()
|
56 |
+
summary = summarize(text, model)
|
57 |
+
st.subheader("Summary")
|
58 |
+
st.write(summary)
|
59 |
+
except:
|
60 |
+
st.info("Processing completed")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
with tab2:
|
63 |
+
st.write("""
|
64 |
+
## About PrepPal
|
65 |
+
- Upload PDFs to get summaries
|
66 |
+
- Max 5MB file size
|
67 |
+
- Simple and easy to use
|
68 |
+
""")
|
|
|
|
|
69 |
|
70 |
if __name__ == "__main__":
|
71 |
main()
|