File size: 2,865 Bytes
955df66
 
 
8a4bc78
955df66
 
 
 
 
 
 
 
 
4441659
 
955df66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25fc809
 
955df66
 
 
 
 
 
 
 
 
 
 
 
 
6868e46
955df66
 
 
 
 
 
 
c8974f6
 
 
 
 
955df66
 
 
 
 
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
import streamlit as st 
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.document_loaders import PyPDFLoader
import time
from transformers import T5Tokenizer, T5ForConditionalGeneration
from transformers import pipeline
import torch
import base64

# Model and tokenizer 
#model_checkpoint = "LaMini-Flan-T5-248M"
model_checkpoint = "MBZUAI/LaMini-Flan-T5-783M"
model_tokenizer = T5Tokenizer.from_pretrained(model_checkpoint)
#model = T5ForConditionalGeneration.from_pretrained(model_checkpoint, device_map='auto', torch_dtype=torch.float32)
model = T5ForConditionalGeneration.from_pretrained(model_checkpoint)

#REPO_ID = "MBZUAI/LaMini-Flan-T5-783M"
#model = pipeline(task='summarization', model=REPO_ID, token=access_token)

# 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

# Language Model pipeline
def language_model_pipeline(filepath):
    summarization_pipeline = pipeline(
        'summarization',
        model=model,
        tokenizer=model_tokenizer,
        max_length=500, 
        min_length=70)
    input_text = preprocess_pdf(filepath)
    summary_result = summarization_pipeline(input_text)
    summarized_text = summary_result[0]['summary_text']
    return summarized_text

@st.cache_data
# Function to display the PDF content
def display_pdf(file):
    with open(file, "rb") as f:
        base64_pdf = base64.b64encode(f.read()).decode('utf-8')
    
    pdf_display = f'<embed src="data:application/pdf;base64,{base64_pdf}" width="700" height="1000" type="application/pdf">' 
    st.markdown(pdf_display, unsafe_allow_html=True)

# Streamlit code 
st.set_page_config(layout="wide")

def main():
    st.title("Document Summarization App using Language Model")

    uploaded_file = st.file_uploader("Upload your PDF file", type=['pdf'])

    if uploaded_file is not None:
        if st.button("Summarize"):
            col1, col2 = st.columns(2)
            filepath = uploaded_file.name
            with open(filepath, "wb") as temp_file:
                temp_file.write(uploaded_file.read())
            with col1:
                st.info("Uploaded File")
                pdf_view = display_pdf(filepath)

            with col2:
                prg = st.progress(0) 
                for i in range(100): 
                    time.sleep(0.1) 
                    prg.progress(i+1) 
                summarized_result = language_model_pipeline(filepath)
                st.info("Summarization Complete")
                st.success(summarized_result)

if __name__ == "__main__":
    main()