sathvikk commited on
Commit
5309b0e
Β·
verified Β·
1 Parent(s): a4c06db

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +30 -28
src/streamlit_app.py CHANGED
@@ -1,5 +1,5 @@
1
  import os
2
- os.environ["TRANSFORMERS_CACHE"] = "/cache" # Special directory in Spaces
3
 
4
  import streamlit as st
5
  import fitz # PyMuPDF
@@ -15,13 +15,13 @@ st.set_page_config(
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:
@@ -46,21 +46,21 @@ def extract_text_from_pdf(uploaded_file):
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)}")
@@ -71,36 +71,35 @@ 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"
@@ -109,12 +108,15 @@ with tab1:
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")
 
1
  import os
2
+ os.environ["TRANSFORMERS_CACHE"] = "/cache" # Hugging Face Spaces cache directory
3
 
4
  import streamlit as st
5
  import fitz # PyMuPDF
 
15
  }
16
  )
17
 
18
+ # Load summarizer model with error handling
19
  @st.cache_resource
20
  def load_summarizer():
21
  try:
22
  return pipeline(
23
  "summarization",
24
+ model="sshleifer/distilbart-cnn-12-6",
25
  device=-1 # Use CPU (more reliable in Spaces)
26
  )
27
  except Exception as e:
 
46
  return ""
47
 
48
  # Summarize text in chunks
49
+ def summarize_text(text, summarizer, max_chunk_length=2000):
50
+ if not text or not summarizer:
51
  return ""
52
 
53
  try:
54
+ chunks = [text[i:i+max_chunk_length] for i in range(0, len(text), max_chunk_length)]
55
  summary = ""
56
  for chunk in chunks:
57
  result = summarizer(
58
  chunk,
59
+ max_length=130,
60
  min_length=30,
61
  do_sample=False
62
  )
63
+ summary += result[0]['summary_text'] + "\n"
64
  return summary.strip()
65
  except Exception as e:
66
  st.error(f"❌ Summarization failed: {str(e)}")
 
71
 
72
  # UI Layout
73
  st.title("πŸ“˜ PrepPal - Study Assistant")
74
+ tab1, tab2, tab3 = st.tabs(["πŸ“„ Summarize Notes", "❓ Ask a Doubt", "πŸ’¬ Feedback"])
 
 
75
 
76
  with tab1:
77
  st.header("PDF Summarizer")
78
+ st.write("Upload your class notes in PDF format to receive a summarized version.")
79
+
80
+ uploaded_pdf = st.file_uploader(
81
  "Choose a PDF file (max 5MB)",
82
  type=["pdf"],
83
  accept_multiple_files=False
84
  )
85
 
86
+ if uploaded_pdf and summarizer:
87
  with st.spinner("Extracting text..."):
88
+ pdf_text = extract_text_from_pdf(uploaded_pdf)
89
 
90
+ if pdf_text:
91
  st.subheader("Extracted Text Preview")
92
+ st.text_area("", pdf_text[:1000] + "...", height=200, disabled=True)
93
 
94
+ if st.button("βœ‚οΈ Summarize"):
95
+ with st.spinner("Summarizing... Please wait."):
96
+ summary = summarize_text(pdf_text, summarizer)
97
 
98
  if summary:
99
+ st.subheader("βœ… Summary")
100
+ st.text_area("Summary Output", summary, height=300)
 
101
  st.download_button(
102
+ "⬇️ Download Summary",
103
  data=summary,
104
  file_name="summary.txt",
105
  mime="text/plain"
 
108
  st.warning("No summary generated")
109
 
110
  with tab2:
111
+ st.header("Ask Questions About Your Notes")
112
+ st.info("πŸ”§ This feature is coming soon! You'll be able to ask questions about your uploaded notes.")
113
+
114
+ with tab3:
115
+ st.header("Help Improve PrepPal")
116
+ feedback = st.text_area("Your feedback or suggestions")
117
  if st.button("Submit Feedback"):
118
+ st.success("Thank you for your feedback! We'll use it to improve PrepPal.")
119
 
120
+ # Footer
121
  st.markdown("---")
122
  st.caption("PrepPal v1.0 | AI-powered study assistant")