File size: 3,562 Bytes
0a190ad
39720cf
0a190ad
c8f0869
90c325f
881b2b7
c8f0869
93313cc
39720cf
 
 
 
 
 
 
 
881b2b7
39720cf
93313cc
edcfa46
39720cf
 
 
 
 
 
 
 
 
93313cc
39720cf
881b2b7
 
 
39720cf
 
 
 
 
881b2b7
 
 
39720cf
881b2b7
39720cf
 
881b2b7
39720cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
881b2b7
93313cc
 
 
39720cf
 
 
 
 
881b2b7
 
39720cf
 
 
 
 
 
881b2b7
39720cf
 
 
881b2b7
39720cf
 
 
881b2b7
39720cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
881b2b7
 
39720cf
 
 
 
 
881b2b7
39720cf
 
 
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
import os
os.environ["TRANSFORMERS_CACHE"] = "/cache"  # Special directory in Spaces

import streamlit as st
import fitz  # PyMuPDF
from transformers import pipeline

# Set page config
st.set_page_config(
    page_title="PrepPal",
    page_icon="πŸ“˜",
    layout="wide",
    menu_items={
        'About': "PrepPal - AI-powered study assistant"
    }
)

# Load summarizer model (using a smaller, faster model)
@st.cache_resource
def load_summarizer():
    try:
        return pipeline(
            "summarization", 
            model="Falconsai/text_summarization",  # Smaller model
            device=-1  # Use CPU (more reliable in Spaces)
        )
    except Exception as e:
        st.error(f"❌ Failed to load model: {str(e)}")
        return None

# Extract text from PDF with size limit
def extract_text_from_pdf(uploaded_file):
    text = ""
    try:
        # Check file size (max 5MB)
        if uploaded_file.size > 5_000_000:
            st.error("File too large (max 5MB)")
            return ""
            
        with fitz.open(stream=uploaded_file.read(), filetype="pdf") as doc:
            for page in doc:
                text += page.get_text()
        return text.strip()
    except Exception as e:
        st.error(f"❌ Error extracting text: {str(e)}")
        return ""

# Summarize text in chunks
def summarize_text(text, summarizer, max_chunk=1024):
    if not text:
        return ""
        
    try:
        chunks = [text[i:i+max_chunk] for i in range(0, len(text), max_chunk)]
        summary = ""
        for chunk in chunks:
            result = summarizer(
                chunk,
                max_length=150,
                min_length=30,
                do_sample=False
            )
            summary += result[0]['summary_text'] + " "
        return summary.strip()
    except Exception as e:
        st.error(f"❌ Summarization failed: {str(e)}")
        return ""

# Load model
summarizer = load_summarizer()

# UI Layout
st.title("πŸ“˜ PrepPal - Study Assistant")
st.markdown("Upload your notes and get an AI-powered summary")

tab1, tab2 = st.tabs(["πŸ“„ Summarize Notes", "πŸ’¬ Feedback"])

with tab1:
    st.header("PDF Summarizer")
    uploaded_file = st.file_uploader(
        "Choose a PDF file (max 5MB)",
        type=["pdf"],
        accept_multiple_files=False
    )

    if uploaded_file and summarizer:
        with st.spinner("Extracting text..."):
            text = extract_text_from_pdf(uploaded_file)

        if text:
            st.subheader("Extracted Text Preview")
            st.text_area("", text[:500] + "...", height=150, disabled=True)

            if st.button("Generate Summary"):
                with st.spinner("Summarizing..."):
                    summary = summarize_text(text, summarizer)
                
                if summary:
                    st.subheader("AI Summary")
                    st.write(summary)
                    
                    st.download_button(
                        "Download Summary",
                        data=summary,
                        file_name="summary.txt",
                        mime="text/plain"
                    )
                else:
                    st.warning("No summary generated")

with tab2:
    st.header("Feedback")
    st.write("We'd love to hear your thoughts!")
    feedback = st.text_area("Your feedback")
    if st.button("Submit Feedback"):
        st.success("Thank you! Your feedback has been recorded.")

# Add footer
st.markdown("---")
st.caption("PrepPal v1.0 | AI-powered study assistant")