isurulkh commited on
Commit
739e8a6
·
1 Parent(s): 8483504

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +86 -0
  2. requirements.txt +10 -0
app.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
3
+ from langchain.document_loaders import PyPDFLoader,DirectoryLoader
4
+ from langchain.chains.summarize import load_summarize_chain
5
+ from transformers import T5Tokenizer, T5ForConditionalGeneration
6
+ from transformers import pipeline
7
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
8
+ import torch
9
+ import base64
10
+
11
+ #model and tokenizer
12
+ # load the model & tokenizer
13
+ #checkpoint = "LaMini-Flan-T5-248M"
14
+ #tokenizer = T5Tokenizer.from_pretrained(checkpoint)
15
+ #base_model = T5ForConditionalGeneration.from_pretrained(checkpoint, device_map='auto', torch_dtype=torch.float32)
16
+
17
+ # Load model directly
18
+ tokenizer = AutoTokenizer.from_pretrained("MBZUAI/LaMini-Flan-T5-248M")
19
+ base_model = AutoModelForSeq2SeqLM.from_pretrained("MBZUAI/LaMini-Flan-T5-248M")
20
+
21
+ #file loader and preprocessing
22
+ def file_preprocessing(file):
23
+ loader = PyPDFLoader(file)
24
+ pages = loader.load_and_split()
25
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=200, chunk_overlap=50)
26
+ texts = text_splitter.split_documents(pages)
27
+ final_texts = ""
28
+ for text in texts:
29
+ final_texts = final_texts + text.page_content
30
+ return final_texts, len(final_texts)
31
+
32
+ # LLM pipeline- using summarization pipleine
33
+ def llm_pipeline(filepath):
34
+ input_text, input_length = file_preprocessing(filepath)
35
+ pipe_sum = pipeline(
36
+ 'summarization',
37
+ model = base_model,
38
+ tokenizer = tokenizer,
39
+ max_length = input_length//5,
40
+ min_length = 25)
41
+ result = pipe_sum(input_text)
42
+ result = result[0]['summary_text']
43
+ return result
44
+
45
+ @st.cache_data #to improve performance by caching
46
+ def displayPDF(file):
47
+ # Opening file from file path as read binary
48
+ with open(file, "rb") as f:
49
+ base64_pdf = base64.b64encode(f.read()).decode('utf-8')
50
+
51
+ # Embedding PDF file in the web browser
52
+ pdf_display = F'<iframe src="data:application/pdf;base64,{base64_pdf}" width="100%" height="600" type="application/pdf"></iframe>'
53
+
54
+ # Displaying File
55
+ st.markdown(pdf_display, unsafe_allow_html=True)
56
+
57
+ #streamlit code
58
+ st.set_page_config(page_title='pdf insight',layout="wide",page_icon="📃",initial_sidebar_state="expanded")
59
+ def main():
60
+ st.title("PDF Insight")
61
+
62
+ uploaded_file = st.file_uploader("Upload the PDF", type=['pdf'])
63
+
64
+ if uploaded_file is not None:
65
+ if st.button("Summarize"):
66
+ col1, col2 = st.columns([0.4,0.6])
67
+ filepath = "uploaded_pdfs/"+uploaded_file.name
68
+
69
+ with open(filepath, "wb") as temp_file:
70
+ temp_file.write(uploaded_file.read())
71
+
72
+ with col1:
73
+ st.info("Uploaded PDF")
74
+ pdf_view = displayPDF(filepath)
75
+
76
+ with col2:
77
+ summary = llm_pipeline(filepath)
78
+ st.info("Summarization")
79
+ st.success(summary)
80
+
81
+
82
+ #initializing the app
83
+ if __name__ == "__main__":
84
+ main()
85
+
86
+
requirements.txt ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ langchain
2
+ sentence_transformers
3
+ torch
4
+ sentencepiece
5
+ transformers
6
+ accelerate
7
+ chromadb
8
+ pypdf
9
+ tiktoken
10
+ streamlit