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

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. 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
- # App configuration
10
  st.set_page_config(
11
  page_title="PrepPal",
12
  page_icon="πŸ“˜",
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):
32
- try:
33
- with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
34
- tmp.write(uploaded_file.getbuffer())
35
- tmp_path = tmp.name
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 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")
78
 
79
- tab1, tab2, tab3 = st.tabs(["πŸ“„ Summarize Notes", "❓ Ask a Doubt", "πŸ’¬ Feedback"])
80
 
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")
124
- st.info("This feature is coming soon!")
125
-
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__":
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()