PdfSummarizer / main.py
Rauhan's picture
UPLOAD: files upload
9aa78e6
raw
history blame
3.92 kB
from src.pipelines.pipeline import Pipeline
from datetime import datetime
import streamlit as st
from io import BytesIO
from fpdf import FPDF
import time
# Configure the page
st.set_page_config(
page_title="PDF Summarizer",
page_icon=None,
layout="wide"
)
# Initialize the pipeline
pipeline = Pipeline()
# Custom styling
st.markdown("""
<style>
.main-header {
font-size: 2.5rem;
color: #1E88E5;
margin-bottom: 2rem;
}
.summary-header {
font-size: 1.8rem;
color: #2E7D32;
margin: 1.5rem 0;
}
.status-container {
padding: 1rem;
border-radius: 0.5rem;
background-color: #f0f2f6;
margin: 1rem 0;
}
</style>
""", unsafe_allow_html=True)
# Sidebar
with st.sidebar:
st.markdown("### Upload PDF")
uploadedFile = st.file_uploader("Choose your PDF file", type=['pdf'])
if uploadedFile:
pdfDetails = {
'File Name': uploadedFile.name,
'File Size': f"{round(len(uploadedFile.getvalue()) / 1024, 2)} KB",
'Upload Time': datetime.now().strftime("%Y-%m-%d %H:%M:%S")
}
st.markdown("### PDF Details")
for key, value in pdfDetails.items():
st.write(f"**{key}:** {value}")
# Main content
st.markdown("<h1 class='main-header'>PDF Summarizer</h1>", unsafe_allow_html=True)
st.write("""
This application generates a concise summary from your uploaded PDF document.
Upload a file to get started.
""")
if uploadedFile:
statusContainer = st.empty()
summaryContainer = st.empty()
with statusContainer.container():
st.markdown("### Processing Status")
statusBox = st.empty()
try:
# Read PDF
startTime = time.time()
statusBox.info("Reading PDF file...")
pdfBytes = uploadedFile.getvalue()
readDuration = time.time() - startTime
statusBox.success(f"PDF file read successfully ({readDuration:.2f}s)")
# Generate summary
statusBox.info("Generating summary...")
summaryStartTime = time.time()
summary = pipeline.run(pdfBytes)
totalTime = time.time() - startTime
if summary:
statusBox.success(f"Summary generated successfully (Total time: {totalTime:.2f}s)")
with summaryContainer.container():
st.markdown("<h2 class='summary-header'>Generated Summary</h2>", unsafe_allow_html=True)
st.markdown(summary)
if st.button("Download Summary as PDF"):
try:
pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", 'B', 16)
pdf.cell(200, 10, txt="PDF Summary", ln=True, align='C')
pdf.ln(10)
pdf.set_font("Arial", size=12)
pdf.multi_cell(0, 10, summary)
pdfOutput = BytesIO()
pdf.output(pdfOutput)
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
st.download_button(
label="Click to Download",
data=pdfOutput.getvalue(),
file_name=f"summary_{timestamp}.pdf",
mime="application/pdf"
)
except Exception as e:
st.error(f"Error creating PDF: {str(e)}")
else:
statusBox.error("Failed to generate summary. Please try again.")
except Exception as e:
statusBox.error(f"Error processing PDF: {str(e)}")
else:
st.info("Please upload a PDF file using the sidebar to get started.")