File size: 5,235 Bytes
f74bc21
4b06e1e
 
f74bc21
4b06e1e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import streamlit as st
import fitz  # PyMuPDF for PDF extraction
from transformers import pipeline

# Set page config
st.set_page_config(page_title="PrepPal", page_icon="πŸ“˜", layout="wide")

# Load summarizer model (using Hugging Face pipeline)
@st.cache_resource
import streamlit as st
import fitz  # PyMuPDF for PDF extraction
from transformers import pipeline

# Set page config
st.set_page_config(page_title="PrepPal", page_icon="πŸ“˜", layout="wide")

# Load summarizer model (using Hugging Face pipeline)
@st.cache_resource
def load_summarizer():
    return pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")


# PDF text extraction
def extract_text_from_pdf(uploaded_file):
    text = ""
    try:
        with fitz.open(stream=uploaded_file.read(), filetype="pdf") as doc:
            for page in doc:
                text += page.get_text()
    except Exception as e:
        st.error(f"Error extracting text from PDF: {e}")
    return text

# Summarize text in chunks
def summarize_text(text, summarizer, max_chunk_length=2000):
    chunks = [text[i:i + max_chunk_length] for i in range(0, len(text), max_chunk_length)]
    summary = ""
    for chunk in chunks:
        result = summarizer(chunk, max_length=130, min_length=30, do_sample=False)  # Corrected 'false' to 'False'
        summary += result[0]['summary_text'] + "\n"
    return summary.strip()

# Load summarizer model
summarizer = load_summarizer()

# Tabs
tab1, tab2, tab3 = st.tabs(["πŸ“„ Summarize Notes", "❓ Ask a Doubt", "πŸ’¬ Feedback"])

# Tab 1: Summarizer
with tab1:
    st.header("πŸ“„ Upload Notes & Get Summary")
    st.write("Upload your class notes in PDF format to receive a summarized version.")
    uploaded_pdf = st.file_uploader("Upload your PDF notes (PDF only)", type=["pdf"])

    if uploaded_pdf:
        with st.spinner("Extracting text from PDF..."):
            pdf_text = extract_text_from_pdf(uploaded_pdf)

        if pdf_text.strip():
            st.subheader("πŸ“˜ Extracted Text (Preview)")
            st.text_area("Raw Text", pdf_text[:1000] + "...", height=200)

            if st.button("βœ‚οΈ Summarize"):
                with st.spinner("Summarizing... Please wait."):
                    summary = summarize_text(pdf_text, summarizer)
                st.subheader("βœ… Summary")
                st.text_area("Summary Output", summary, height=300)

                st.download_button("⬇️ Download Summary", summary, file_name="summary.txt")
        else:
            st.warning("⚠️ No text found in the uploaded PDF.")

# Tab 2: Ask a Doubt (coming soon)
with tab2:
    st.header("❓ Ask a Doubt")
    st.info("πŸ”§ This feature is under development. You’ll soon be able to chat with your notes using AI!")

# Tab 3: Feedback (coming soon)
with tab3:
    st.header("πŸ’¬ User Feedback")
    st.info("πŸ“¬ A feedback form will be added here to collect your thoughts and improve PrepPal.")

# PDF text extraction
def extract_text_from_pdf(uploaded_file):
    text = ""
    try:
        with fitz.open(stream=uploaded_file.read(), filetype="pdf") as doc:
            for page in doc:
                text += page.get_text()
    except Exception as e:
        st.error(f"Error extracting text from PDF: {e}")
    return text

# Summarize text in chunks
def summarize_text(text, summarizer, max_chunk_length=2000):
    chunks = [text[i:i + max_chunk_length] for i in range(0, len(text), max_chunk_length)]
    summary = ""
    for chunk in chunks:
        result = summarizer(chunk, max_length=130, min_length=30, do_sample=False)  # Corrected 'false' to 'False'
        summary += result[0]['summary_text'] + "\n"
    return summary.strip()

# Load summarizer model
summarizer = load_summarizer()

# Tabs
tab1, tab2, tab3 = st.tabs(["πŸ“„ Summarize Notes", "❓ Ask a Doubt", "πŸ’¬ Feedback"])

# Tab 1: Summarizer
with tab1:
    st.header("πŸ“„ Upload Notes & Get Summary")
    st.write("Upload your class notes in PDF format to receive a summarized version.")
    uploaded_pdf = st.file_uploader("Upload your PDF notes (PDF only)", type=["pdf"])

    if uploaded_pdf:
        with st.spinner("Extracting text from PDF..."):
            pdf_text = extract_text_from_pdf(uploaded_pdf)
            
        if pdf_text.strip():
            st.subheader("πŸ“˜ Extracted Text (Preview)")
            st.text_area("Raw Text", pdf_text[:1000] + "...", height=200)

            if st.button("βœ‚οΈ Summarize"):
                with st.spinner("Summarizing... Please wait."):
                    summary = summarize_text(pdf_text, summarizer)
                st.subheader("βœ… Summary")
                st.text_area("Summary Output", summary, height=300)

                st.download_button("⬇️ Download Summary", summary, file_name="summary.txt")
        else:
            st.warning("⚠️ No text found in the uploaded PDF.")

# Tab 2: Ask a Doubt (coming soon)
with tab2:
    st.header("❓ Ask a Doubt")
    st.info("πŸ”§ This feature is under development. You’ll soon be able to chat with your notes using AI!")

# Tab 3: Feedback (coming soon)
with tab3:
    st.header("πŸ’¬ User Feedback")
    st.info("πŸ“¬ A feedback form will be added here to collect your thoughts and improve PrepPal.")