pdf_summarize / app.py
devaldicaliesta's picture
new update banner
b627c16
raw
history blame
2.37 kB
import streamlit as st
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.document_loaders import PyPDFLoader
from transformers import T5Tokenizer, T5ForConditionalGeneration
from transformers import pipeline
import torch
import base64
import time
from PIL import Image
# Load Hugging Face banner image
banner_image = Image.open("https://www.bing.com/images/search?view=detailV2&ccid=NaYLVXvb&id=C3841515E49D840D6F948419B3994082933F3647&thid=OIP.NaYLVXvb0WxvKKpw2IgHBAHaEC&mediaurl=https%3a%2f%2fwww.reliablecounter.com%2fblog%2fwp-content%2fuploads%2f2021%2f09%2fwork.jpg&exph=496&expw=910&q=work&simid=607995094262433128&FORM=IRPRST&ck=1B85342DCD1262FCC2110F5E0A1D52F8&selectedIndex=17")
st.image(banner_image, caption="Hugging Face LaMDA Mini Summary")
# Model and tokenizer
model_checkpoint = "MBZUAI/LaMini-Flan-T5-783M"
model_tokenizer = T5Tokenizer.from_pretrained(model_checkpoint)
model = T5ForConditionalGeneration.from_pretrained(model_checkpoint)
# File loader and preprocessing
def preprocess_pdf(file):
loader = PyPDFLoader(file)
pages = loader.load_and_split()
text_splitter = RecursiveCharacterTextSplitter(chunk_size=170, chunk_overlap=70)
texts = text_splitter.split_documents(pages)
final_text = ""
for text in texts:
final_text = final_text + text.page_content
return final_text
@st.cache_data
def language_model_pipeline(filepath):
summarization_pipeline = pipeline(
'summarization',
model=model,
tokenizer=model_tokenizer,
max_length=500,
min_length=32
)
input_text = preprocess_pdf(filepath)
summary_result = summarization_pipeline(input_text)
summarized_text = summary_result[0]['summary_text']
return summarized_text
# User interface
title = st.title("PDF Summarization using LaMini")
uploaded_file = st.file_uploader('Upload your PDF file', type=['pdf'])
if uploaded_file is not None:
st.success("File uploaded")
if st.button("Summarize"):
with st.spinner("Summarizing..."):
time.sleep(10)
filepath = uploaded_file.name
with open(filepath, "wb") as temp_file:
temp_file.write(uploaded_file.read())
summarized_result = language_model_pipeline(filepath)
st.success("Summary:")
st.write(summarized_result)