import os import base64 import tempfile import streamlit as st from transformers import pipeline from PyPDF2 import PdfReader from transformers import AutoTokenizer, AutoModelForSeq2SeqLM # Load the summarization model tokenizer = AutoTokenizer.from_pretrained("MBZUAI/LaMini-Flan-T5-248M") base_model = AutoModelForSeq2SeqLM.from_pretrained("MBZUAI/LaMini-Flan-T5-248M") # Function to extract text from a PDF using PyPDF2 def extract_text_from_pdf(pdf_path): reader = PdfReader(pdf_path) text = "" for page in reader.pages: text += page.extract_text() # Extract text from each page if not text.strip(): raise ValueError("The PDF file contains no extractable text.") return text # LLM pipeline for summarization def llm_pipeline(input_text): pipe_sum = pipeline( 'summarization', model=base_model, tokenizer=tokenizer, max_length=500, min_length=50, ) result = pipe_sum(input_text) return result[0]['summary_text'] @st.cache_data # Function to display the PDF def displayPDF(file_path): with open(file_path, "rb") as f: base64_pdf = base64.b64encode(f.read()).decode('utf-8') pdf_display = f'' st.markdown(pdf_display, unsafe_allow_html=True) # Streamlit App def main(): st.title('Content Summarizer') # PDF Upload Section uploaded_file = st.file_uploader("Upload your PDF file", type=['pdf']) if uploaded_file is not None: if st.button("Summarize PDF"): col1, col2 = st.columns(2) # Save the uploaded file to a temporary location with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf", dir="/tmp/") as temp_file: temp_file.write(uploaded_file.read()) temp_filepath = temp_file.name with col1: st.info("Uploaded PDF File") displayPDF(temp_filepath) with col2: st.info("Summarization") input_text = extract_text_from_pdf(temp_filepath) if input_text: # Proceed only if text extraction was successful summary = llm_pipeline(input_text) st.success(summary) # Text Input Section st.header("Summarize Your Text") user_input = st.text_area("Enter your content here:", height=200) if st.button("Summarize Text"): if user_input.strip(): col1, col2 = st.columns(2) with col1: st.info("Original Content") st.write(user_input) with col2: st.info("Summarization") summary = llm_pipeline(user_input) st.success(summary) else: st.warning("Please enter some content to summarize.") if __name__ == '__main__': main()