sathvikk commited on
Commit
354355a
Β·
verified Β·
1 Parent(s): f4b5840

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +55 -29
src/streamlit_app.py CHANGED
@@ -13,21 +13,19 @@ st.set_page_config(
13
  layout="wide"
14
  )
15
 
16
- # Security headers
17
- st.markdown("""
18
- <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self' 'unsafe-inline'">
19
- """, unsafe_allow_html=True)
20
-
21
- @st.cache_resource
22
  def load_model():
23
  try:
 
24
  return pipeline(
25
  "summarization",
26
- model="Falconsai/text_summarization", # Smaller model for Spaces
27
- device=-1 # Use CPU
28
  )
29
  except Exception as e:
30
- st.error(f"Failed to load model: {str(e)}")
 
31
  return None
32
 
33
  def process_pdf(uploaded_file):
@@ -42,19 +40,38 @@ def process_pdf(uploaded_file):
42
  os.unlink(tmp_path)
43
  return text.strip()
44
  except Exception as e:
45
- st.error(f"Error processing PDF: {str(e)}")
46
  return ""
47
 
48
  def generate_summary(text, model):
49
- if not text or not model:
 
 
 
 
 
50
  return ""
51
 
52
- chunks = [text[i:i+1000] for i in range(0, len(text), 1000)]
53
- summary = []
54
- for chunk in chunks:
55
- result = model(chunk, max_length=150, min_length=30, do_sample=False)
56
- summary.append(result[0]['summary_text'])
57
- return " ".join(summary)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
 
59
  def main():
60
  st.title("πŸ“˜ PrepPal - Study Assistant")
@@ -64,34 +81,43 @@ def main():
64
  with tab1:
65
  st.header("PDF Summarizer")
66
  uploaded_file = st.file_uploader(
67
- "Upload PDF (max 5MB)",
68
  type=["pdf"],
69
  accept_multiple_files=False
70
  )
71
 
72
  if uploaded_file:
73
- if uploaded_file.size > 5_000_000:
74
- st.error("File too large (max 5MB)")
75
  else:
76
- with st.spinner("Processing PDF..."):
77
  text = process_pdf(uploaded_file)
78
 
79
  if text:
80
  with st.expander("View extracted text"):
81
- st.text(text[:500] + "...")
82
 
83
  if st.button("Generate Summary"):
84
- with st.spinner("Creating summary..."):
85
  model = load_model()
86
- if model:
87
- summary = generate_summary(text, model)
88
- st.subheader("Summary")
 
 
 
 
89
  st.write(summary)
90
  st.download_button(
91
- "Download Summary",
92
  data=summary,
93
- file_name="summary.txt"
 
94
  )
 
 
 
 
95
 
96
  with tab2:
97
  st.header("Ask a Question")
@@ -100,7 +126,7 @@ def main():
100
  with tab3:
101
  st.header("Feedback")
102
  feedback = st.text_area("Your suggestions")
103
- if st.button("Submit"):
104
  st.success("Thank you for your feedback!")
105
 
106
  if __name__ == "__main__":
 
13
  layout="wide"
14
  )
15
 
16
+ # Load model with better error handling
17
+ @st.cache_resource(show_spinner=False)
 
 
 
 
18
  def load_model():
19
  try:
20
+ # Using a smaller, more reliable model
21
  return pipeline(
22
  "summarization",
23
+ model="facebook/bart-large-cnn",
24
+ device=-1 # Force CPU
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 process_pdf(uploaded_file):
 
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 generate_summary(text, model):
47
+ if not text:
48
+ st.error("No text extracted from PDF")
49
+ return ""
50
+
51
+ if not model:
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 - Study Assistant")
 
81
  with tab1:
82
  st.header("PDF Summarizer")
83
  uploaded_file = st.file_uploader(
84
+ "Upload PDF (max 10MB)",
85
  type=["pdf"],
86
  accept_multiple_files=False
87
  )
88
 
89
  if uploaded_file:
90
+ if uploaded_file.size > 10_000_000:
91
+ st.error("File too large (max 10MB)")
92
  else:
93
+ with st.spinner("Extracting text from PDF..."):
94
  text = process_pdf(uploaded_file)
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
+ if model:
105
+ st.info("Generating summary... This may take a minute")
106
+ summary = generate_summary(text, model)
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.header("Ask a Question")
 
126
  with tab3:
127
  st.header("Feedback")
128
  feedback = st.text_area("Your suggestions")
129
+ if st.button("Submit Feedback"):
130
  st.success("Thank you for your feedback!")
131
 
132
  if __name__ == "__main__":