sathvikk commited on
Commit
39720cf
Β·
verified Β·
1 Parent(s): 55e2230

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +89 -40
src/streamlit_app.py CHANGED
@@ -1,71 +1,120 @@
1
  import os
2
- os.environ["TRANSFORMERS_CACHE"] = "/tmp/huggingface"
3
 
4
  import streamlit as st
5
  import fitz # PyMuPDF
6
  from transformers import pipeline
7
 
8
  # Set page config
9
- st.set_page_config(page_title="PrepPal", page_icon="πŸ“˜", layout="wide")
 
 
 
 
 
 
 
10
 
11
- # Load summarizer model
12
  @st.cache_resource
13
  def load_summarizer():
14
- return pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
15
-
 
 
 
 
 
 
 
16
 
17
- # Extract text from uploaded PDF
18
  def extract_text_from_pdf(uploaded_file):
19
  text = ""
20
  try:
 
 
 
 
 
21
  with fitz.open(stream=uploaded_file.read(), filetype="pdf") as doc:
22
  for page in doc:
23
  text += page.get_text()
 
24
  except Exception as e:
25
- st.error(f"❌ Error extracting text from PDF: {e}")
26
- return text
27
 
28
- # Summarize large text
29
- def summarize_text(text, summarizer, max_chunk_length=2000):
30
- chunks = [text[i:i + max_chunk_length] for i in range(0, len(text), max_chunk_length)]
31
- summary = ""
32
- for chunk in chunks:
33
- result = summarizer(chunk, max_length=130, min_length=30, do_sample=False)
34
- summary += result[0]['summary_text'] + "\n"
35
- return summary.strip()
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
  # Load model
38
  summarizer = load_summarizer()
39
 
40
- # UI
41
- tab1, tab2, tab3 = st.tabs(["πŸ“„ Summarize Notes", "❓ Ask a Doubt", "πŸ’¬ Feedback"])
 
 
 
42
 
43
  with tab1:
44
- st.header("πŸ“„ Upload Notes & Get Summary")
45
- st.write("Upload your class notes in PDF format to receive a summarized version.")
46
- uploaded_pdf = st.file_uploader("Upload your PDF notes (PDF only)", type=["pdf"])
 
 
 
47
 
48
- if uploaded_pdf:
49
- with st.spinner("Extracting text from PDF..."):
50
- pdf_text = extract_text_from_pdf(uploaded_pdf)
51
 
52
- if pdf_text.strip():
53
- st.subheader("πŸ“˜ Extracted Text (Preview)")
54
- st.text_area("Raw Text", pdf_text[:1000] + "...", height=200)
55
 
56
- if st.button("βœ‚οΈ Summarize"):
57
- with st.spinner("Summarizing... Please wait."):
58
- summary = summarize_text(pdf_text, summarizer)
59
- st.subheader("βœ… Summary")
60
- st.text_area("Summary Output", summary, height=300)
61
- st.download_button("⬇️ Download Summary", summary, file_name="summary.txt")
62
- else:
63
- st.warning("⚠️ No text found in the uploaded PDF.")
 
 
 
 
 
 
 
 
64
 
65
  with tab2:
66
- st.header("❓ Ask a Doubt")
67
- st.info("πŸ”§ This feature is under development. You’ll soon be able to chat with your notes using AI!")
 
 
 
68
 
69
- with tab3:
70
- st.header("πŸ’¬ User Feedback")
71
- st.info("πŸ“¬ A feedback form will be added here to collect your thoughts and improve PrepPal.")
 
1
  import os
2
+ os.environ["TRANSFORMERS_CACHE"] = "/cache" # Special directory in Spaces
3
 
4
  import streamlit as st
5
  import fitz # PyMuPDF
6
  from transformers import pipeline
7
 
8
  # Set page config
9
+ st.set_page_config(
10
+ page_title="PrepPal",
11
+ page_icon="πŸ“˜",
12
+ layout="wide",
13
+ menu_items={
14
+ 'About': "PrepPal - AI-powered study assistant"
15
+ }
16
+ )
17
 
18
+ # Load summarizer model (using a smaller, faster model)
19
  @st.cache_resource
20
  def load_summarizer():
21
+ try:
22
+ return pipeline(
23
+ "summarization",
24
+ model="Falconsai/text_summarization", # Smaller model
25
+ device=-1 # Use CPU (more reliable in Spaces)
26
+ )
27
+ except Exception as e:
28
+ st.error(f"❌ Failed to load model: {str(e)}")
29
+ return None
30
 
31
+ # Extract text from PDF with size limit
32
  def extract_text_from_pdf(uploaded_file):
33
  text = ""
34
  try:
35
+ # Check file size (max 5MB)
36
+ if uploaded_file.size > 5_000_000:
37
+ st.error("File too large (max 5MB)")
38
+ return ""
39
+
40
  with fitz.open(stream=uploaded_file.read(), filetype="pdf") as doc:
41
  for page in doc:
42
  text += page.get_text()
43
+ return text.strip()
44
  except Exception as e:
45
+ st.error(f"❌ Error extracting text: {str(e)}")
46
+ return ""
47
 
48
+ # Summarize text in chunks
49
+ def summarize_text(text, summarizer, max_chunk=1024):
50
+ if not text:
51
+ return ""
52
+
53
+ try:
54
+ chunks = [text[i:i+max_chunk] for i in range(0, len(text), max_chunk)]
55
+ summary = ""
56
+ for chunk in chunks:
57
+ result = summarizer(
58
+ chunk,
59
+ max_length=150,
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)}")
67
+ return ""
68
 
69
  # Load model
70
  summarizer = load_summarizer()
71
 
72
+ # UI Layout
73
+ st.title("πŸ“˜ PrepPal - Study Assistant")
74
+ st.markdown("Upload your notes and get an AI-powered summary")
75
+
76
+ tab1, tab2 = st.tabs(["πŸ“„ Summarize Notes", "πŸ’¬ Feedback"])
77
 
78
  with tab1:
79
+ st.header("PDF Summarizer")
80
+ uploaded_file = st.file_uploader(
81
+ "Choose a PDF file (max 5MB)",
82
+ type=["pdf"],
83
+ accept_multiple_files=False
84
+ )
85
 
86
+ if uploaded_file and summarizer:
87
+ with st.spinner("Extracting text..."):
88
+ text = extract_text_from_pdf(uploaded_file)
89
 
90
+ if text:
91
+ st.subheader("Extracted Text Preview")
92
+ st.text_area("", text[:500] + "...", height=150, disabled=True)
93
 
94
+ if st.button("Generate Summary"):
95
+ with st.spinner("Summarizing..."):
96
+ summary = summarize_text(text, summarizer)
97
+
98
+ if summary:
99
+ st.subheader("AI Summary")
100
+ st.write(summary)
101
+
102
+ st.download_button(
103
+ "Download Summary",
104
+ data=summary,
105
+ file_name="summary.txt",
106
+ mime="text/plain"
107
+ )
108
+ else:
109
+ st.warning("No summary generated")
110
 
111
  with tab2:
112
+ st.header("Feedback")
113
+ st.write("We'd love to hear your thoughts!")
114
+ feedback = st.text_area("Your feedback")
115
+ if st.button("Submit Feedback"):
116
+ st.success("Thank you! Your feedback has been recorded.")
117
 
118
+ # Add footer
119
+ st.markdown("---")
120
+ st.caption("PrepPal v1.0 | AI-powered study assistant")