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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
indices = np.linspace(0, 1, num_points)
|
| 20 |
-
theta = 2 * np.pi * num_turns * indices
|
| 21 |
-
radius = indices
|
| 22 |
-
|
| 23 |
-
x = radius * np.cos(theta)
|
| 24 |
-
y = radius * np.sin(theta)
|
| 25 |
-
|
| 26 |
-
df = pd.DataFrame({
|
| 27 |
-
"x": x,
|
| 28 |
-
"y": y,
|
| 29 |
-
"idx": indices,
|
| 30 |
-
"rand": np.random.randn(num_points),
|
| 31 |
-
})
|
| 32 |
-
|
| 33 |
-
st.altair_chart(alt.Chart(df, height=700, width=700)
|
| 34 |
-
.mark_point(filled=True)
|
| 35 |
-
.encode(
|
| 36 |
-
x=alt.X("x", axis=None),
|
| 37 |
-
y=alt.Y("y", axis=None),
|
| 38 |
-
color=alt.Color("idx", legend=None, scale=alt.Scale()),
|
| 39 |
-
size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
|
| 40 |
-
))
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
AI Notes Summarizer - Main Application
|
| 3 |
+
A Streamlit web application for summarizing PDF files and text content using AI.
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
import streamlit as st
|
| 7 |
+
import os
|
| 8 |
+
from pathlib import Path
|
| 9 |
|
| 10 |
+
# Import custom modules
|
| 11 |
+
from modules.pdf_processor import PDFProcessor
|
| 12 |
+
from modules.text_summarizer import TextSummarizer
|
| 13 |
+
from modules.utils import setup_logging, validate_input, display_summary_stats, format_file_size
|
| 14 |
|
| 15 |
+
# Initialize components
|
| 16 |
+
@st.cache_resource
|
| 17 |
+
def initialize_components():
|
| 18 |
+
"""Initialize PDF processor and text summarizer"""
|
| 19 |
+
pdf_processor = PDFProcessor()
|
| 20 |
+
text_summarizer = TextSummarizer()
|
| 21 |
+
return pdf_processor, text_summarizer
|
| 22 |
|
| 23 |
+
def main():
|
| 24 |
+
"""Main application function"""
|
| 25 |
+
st.set_page_config(
|
| 26 |
+
page_title="AI Notes Summarizer",
|
| 27 |
+
page_icon="π",
|
| 28 |
+
layout="wide",
|
| 29 |
+
initial_sidebar_state="expanded"
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
# Initialize components
|
| 33 |
+
pdf_processor, text_summarizer = initialize_components()
|
| 34 |
+
|
| 35 |
+
# App header
|
| 36 |
+
st.title("π AI Notes Summarizer")
|
| 37 |
+
st.markdown("Transform your lengthy documents and notes into concise, bullet-point summaries using AI.")
|
| 38 |
+
|
| 39 |
+
# Sidebar for options
|
| 40 |
+
st.sidebar.header("βοΈ Settings")
|
| 41 |
+
|
| 42 |
+
# Model selection
|
| 43 |
+
model_options = {
|
| 44 |
+
"BART (Recommended)": "facebook/bart-large-cnn",
|
| 45 |
+
"T5 Small": "t5-small",
|
| 46 |
+
"DistilBART": "sshleifer/distilbart-cnn-12-6"
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
selected_model = st.sidebar.selectbox(
|
| 50 |
+
"Choose AI Model:",
|
| 51 |
+
options=list(model_options.keys()),
|
| 52 |
+
index=0,
|
| 53 |
+
help="BART is recommended for best quality summaries"
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
# Update text summarizer model if changed
|
| 57 |
+
if text_summarizer.model_name != model_options[selected_model]:
|
| 58 |
+
text_summarizer.model_name = model_options[selected_model]
|
| 59 |
+
text_summarizer.summarizer = None # Reset to reload model
|
| 60 |
+
|
| 61 |
+
# Summary length options
|
| 62 |
+
summary_length = st.sidebar.select_slider(
|
| 63 |
+
"Summary Length:",
|
| 64 |
+
options=["Short", "Medium", "Long"],
|
| 65 |
+
value="Medium",
|
| 66 |
+
help="Choose the desired length of the summary"
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
# Update summary length settings
|
| 70 |
+
length_settings = {
|
| 71 |
+
"Short": (30, 150),
|
| 72 |
+
"Medium": (50, 300),
|
| 73 |
+
"Long": (100, 500)
|
| 74 |
+
}
|
| 75 |
+
text_summarizer.min_summary_length, text_summarizer.max_summary_length = length_settings[summary_length]
|
| 76 |
+
|
| 77 |
+
# Main content area
|
| 78 |
+
tab1, tab2 = st.tabs(["π PDF Upload", "π Text Input"])
|
| 79 |
+
|
| 80 |
+
with tab1:
|
| 81 |
+
st.header("Upload PDF File")
|
| 82 |
+
st.markdown("Upload a PDF file to extract and summarize its content.")
|
| 83 |
+
|
| 84 |
+
uploaded_file = st.file_uploader(
|
| 85 |
+
"Choose a PDF file",
|
| 86 |
+
type=['pdf'],
|
| 87 |
+
help="Upload a PDF file (max 10MB)"
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
if uploaded_file is not None:
|
| 91 |
+
# Display file info
|
| 92 |
+
file_size = format_file_size(uploaded_file.size)
|
| 93 |
+
st.info(f"π **File:** {uploaded_file.name} ({file_size})")
|
| 94 |
+
|
| 95 |
+
# Process PDF button
|
| 96 |
+
if st.button("π Extract & Summarize PDF", type="primary"):
|
| 97 |
+
with st.spinner("Processing PDF file..."):
|
| 98 |
+
# Extract text from PDF
|
| 99 |
+
extracted_text = pdf_processor.process_pdf(uploaded_file)
|
| 100 |
+
|
| 101 |
+
if extracted_text:
|
| 102 |
+
st.success("β
Text extracted successfully!")
|
| 103 |
+
|
| 104 |
+
# Show extracted text preview
|
| 105 |
+
with st.expander("π View Extracted Text (Preview)"):
|
| 106 |
+
st.text_area(
|
| 107 |
+
"Extracted Content:",
|
| 108 |
+
value=extracted_text[:1000] + "..." if len(extracted_text) > 1000 else extracted_text,
|
| 109 |
+
height=200,
|
| 110 |
+
disabled=True
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
# Generate summary
|
| 114 |
+
summary = text_summarizer.summarize_text(extracted_text)
|
| 115 |
+
|
| 116 |
+
if summary:
|
| 117 |
+
st.success("β
Summary generated successfully!")
|
| 118 |
+
|
| 119 |
+
# Display summary
|
| 120 |
+
st.subheader("π Summary")
|
| 121 |
+
st.markdown(summary)
|
| 122 |
+
|
| 123 |
+
# Display statistics
|
| 124 |
+
st.subheader("π Statistics")
|
| 125 |
+
display_summary_stats(extracted_text, summary)
|
| 126 |
+
|
| 127 |
+
# Download option
|
| 128 |
+
st.download_button(
|
| 129 |
+
label="πΎ Download Summary",
|
| 130 |
+
data=summary,
|
| 131 |
+
file_name=f"{uploaded_file.name}_summary.txt",
|
| 132 |
+
mime="text/plain"
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
with tab2:
|
| 136 |
+
st.header("Direct Text Input")
|
| 137 |
+
st.markdown("Paste your text content directly for summarization.")
|
| 138 |
+
|
| 139 |
+
text_input = st.text_area(
|
| 140 |
+
"Enter your text here:",
|
| 141 |
+
height=300,
|
| 142 |
+
placeholder="Paste your text content here...",
|
| 143 |
+
help="Minimum 100 characters required for effective summarization"
|
| 144 |
+
)
|
| 145 |
+
|
| 146 |
+
# Character count
|
| 147 |
+
char_count = len(text_input)
|
| 148 |
+
st.caption(f"Characters: {char_count:,}")
|
| 149 |
+
|
| 150 |
+
if st.button("π Summarize Text", type="primary"):
|
| 151 |
+
if validate_input(text_input, min_length=100):
|
| 152 |
+
# Generate summary
|
| 153 |
+
summary = text_summarizer.summarize_text(text_input)
|
| 154 |
+
|
| 155 |
+
if summary:
|
| 156 |
+
st.success("β
Summary generated successfully!")
|
| 157 |
+
|
| 158 |
+
# Display summary
|
| 159 |
+
st.subheader("π Summary")
|
| 160 |
+
st.markdown(summary)
|
| 161 |
+
|
| 162 |
+
# Display statistics
|
| 163 |
+
st.subheader("π Statistics")
|
| 164 |
+
display_summary_stats(text_input, summary)
|
| 165 |
+
|
| 166 |
+
# Download option
|
| 167 |
+
st.download_button(
|
| 168 |
+
label="πΎ Download Summary",
|
| 169 |
+
data=summary,
|
| 170 |
+
file_name="text_summary.txt",
|
| 171 |
+
mime="text/plain"
|
| 172 |
+
)
|
| 173 |
+
|
| 174 |
+
# Footer
|
| 175 |
+
st.markdown("---")
|
| 176 |
+
st.markdown(
|
| 177 |
+
"""
|
| 178 |
+
<div style='text-align: center; color: #666;'>
|
| 179 |
+
<p>AI Notes Summarizer | Powered by Hugging Face Transformers</p>
|
| 180 |
+
</div>
|
| 181 |
+
""",
|
| 182 |
+
unsafe_allow_html=True
|
| 183 |
+
)
|
| 184 |
|
| 185 |
+
if __name__ == "__main__":
|
| 186 |
+
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|