sathvikk commited on
Commit
b0e2e31
Β·
verified Β·
1 Parent(s): 6cb7262

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +22 -33
src/streamlit_app.py CHANGED
@@ -6,7 +6,7 @@ import streamlit as st
6
  import fitz # PyMuPDF
7
  from transformers import pipeline
8
 
9
- # Security headers and config
10
  st.set_page_config(
11
  page_title="PrepPal",
12
  page_icon="πŸ“˜",
@@ -14,8 +14,9 @@ st.set_page_config(
14
  menu_items={'About': "PrepPal - AI-powered PDF summarizer"}
15
  )
16
 
 
17
  st.markdown("""
18
- <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data:;">
19
  """, unsafe_allow_html=True)
20
 
21
  @st.cache_resource
@@ -23,31 +24,33 @@ def load_summarizer():
23
  try:
24
  return pipeline(
25
  "summarization",
26
- model="facebook/bart-large-cnn", # Reliable medium-size model
27
- device=-1 # Force CPU
28
  )
29
  except Exception as e:
30
  st.error(f"Model loading failed: {str(e)}")
31
  return None
32
 
33
- def extract_text(uploaded_file):
34
- text = ""
35
  try:
36
- # Save to temp file first (fixes 403 issues)
37
  with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
38
  tmp.write(uploaded_file.getbuffer())
39
  tmp_path = tmp.name
40
 
 
41
  with fitz.open(tmp_path) as doc:
42
  text = "\n".join([page.get_text() for page in doc])
43
 
 
44
  os.unlink(tmp_path)
45
  return text.strip()
46
  except Exception as e:
47
  st.error(f"PDF processing error: {str(e)}")
48
  return ""
49
 
50
- def summarize(text, model, max_chunk=1500):
51
  if not text or not model:
52
  return ""
53
 
@@ -65,11 +68,10 @@ def summarize(text, model, max_chunk=1500):
65
 
66
  return "\n".join(summary)
67
 
68
- # Main App with all 3 tabs
69
  def main():
70
  st.title("πŸ“˜ PrepPal - Study Assistant")
71
 
72
- # Create all three tabs
73
  tab1, tab2, tab3 = st.tabs(["πŸ“„ Summarize Notes", "❓ Ask a Doubt", "πŸ’¬ Feedback"])
74
 
75
  with tab1:
@@ -84,21 +86,21 @@ def main():
84
  )
85
 
86
  if uploaded_file:
87
- if uploaded_file.size > 10_000_000: # 10MB limit
88
  st.error("File too large (max 10MB)")
89
  else:
90
  with st.spinner("Extracting text..."):
91
- text = extract_text(uploaded_file)
92
 
93
  if text:
94
  with st.expander("View extracted text"):
95
  st.text(text[:1000] + "...")
96
 
97
- if st.button("Generate Summary", key="summarize_btn"):
98
  with st.spinner("Summarizing..."):
99
  model = load_summarizer()
100
  if model:
101
- summary = summarize(text, model)
102
 
103
  st.subheader("AI Summary")
104
  st.write(summary)
@@ -107,32 +109,19 @@ def main():
107
  "Download Summary",
108
  data=summary,
109
  file_name="summary.txt",
110
- mime="text/plain",
111
- key="download_btn"
112
  )
113
 
114
  with tab2:
115
  st.header("Ask a Question")
116
- st.write("Coming Soon: Ask questions about your uploaded documents")
117
- st.image("https://via.placeholder.com/600x200?text=Question+Answering+Feature+Coming+Soon",
118
- caption="AI question answering will be available in the next update")
119
-
120
- # Placeholder for future functionality
121
- question = st.text_input("What would you like to ask about your document?")
122
- if question:
123
- st.info("This feature is currently in development. Please check back soon!")
124
 
125
  with tab3:
126
  st.header("Your Feedback")
127
- st.write("Help us improve PrepPal")
128
-
129
- feedback = st.text_area("What do you think about PrepPal?")
130
- if st.button("Submit Feedback", key="feedback_btn"):
131
- if feedback:
132
- st.success("Thank you for your feedback! We'll use this to improve the app.")
133
- # In a real app, you would store this feedback somewhere
134
- else:
135
- st.warning("Please enter your feedback before submitting")
136
 
137
  if __name__ == "__main__":
138
  main()
 
6
  import fitz # PyMuPDF
7
  from transformers import pipeline
8
 
9
+ # Security configuration
10
  st.set_page_config(
11
  page_title="PrepPal",
12
  page_icon="πŸ“˜",
 
14
  menu_items={'About': "PrepPal - AI-powered PDF summarizer"}
15
  )
16
 
17
+ # Fix for 403 errors
18
  st.markdown("""
19
+ <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data:;">
20
  """, unsafe_allow_html=True)
21
 
22
  @st.cache_resource
 
24
  try:
25
  return pipeline(
26
  "summarization",
27
+ model="facebook/bart-large-cnn", # Reliable model
28
+ device=-1 # Force CPU for Hugging Face Spaces
29
  )
30
  except Exception as e:
31
  st.error(f"Model loading failed: {str(e)}")
32
  return None
33
 
34
+ def safe_extract_text(uploaded_file):
35
+ """Secure PDF text extraction with temp files"""
36
  try:
37
+ # First save to temporary file
38
  with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
39
  tmp.write(uploaded_file.getbuffer())
40
  tmp_path = tmp.name
41
 
42
+ # Process from filesystem
43
  with fitz.open(tmp_path) as doc:
44
  text = "\n".join([page.get_text() for page in doc])
45
 
46
+ # Clean up
47
  os.unlink(tmp_path)
48
  return text.strip()
49
  except Exception as e:
50
  st.error(f"PDF processing error: {str(e)}")
51
  return ""
52
 
53
+ def summarize_text(text, model, max_chunk=1500):
54
  if not text or not model:
55
  return ""
56
 
 
68
 
69
  return "\n".join(summary)
70
 
71
+ # Main App with all tabs
72
  def main():
73
  st.title("πŸ“˜ PrepPal - Study Assistant")
74
 
 
75
  tab1, tab2, tab3 = st.tabs(["πŸ“„ Summarize Notes", "❓ Ask a Doubt", "πŸ’¬ Feedback"])
76
 
77
  with tab1:
 
86
  )
87
 
88
  if uploaded_file:
89
+ if uploaded_file.size > 10_000_000:
90
  st.error("File too large (max 10MB)")
91
  else:
92
  with st.spinner("Extracting text..."):
93
+ text = safe_extract_text(uploaded_file)
94
 
95
  if text:
96
  with st.expander("View extracted text"):
97
  st.text(text[:1000] + "...")
98
 
99
+ if st.button("Generate Summary"):
100
  with st.spinner("Summarizing..."):
101
  model = load_summarizer()
102
  if model:
103
+ summary = summarize_text(text, model)
104
 
105
  st.subheader("AI Summary")
106
  st.write(summary)
 
109
  "Download Summary",
110
  data=summary,
111
  file_name="summary.txt",
112
+ mime="text/plain"
 
113
  )
114
 
115
  with tab2:
116
  st.header("Ask a Question")
117
+ st.info("This feature will allow you to ask questions about your documents")
118
+ st.write("Coming in the next update!")
 
 
 
 
 
 
119
 
120
  with tab3:
121
  st.header("Your Feedback")
122
+ feedback = st.text_area("How can we improve PrepPal?")
123
+ if st.button("Submit Feedback"):
124
+ st.success("Thank you! Your feedback has been recorded.")
 
 
 
 
 
 
125
 
126
  if __name__ == "__main__":
127
  main()