wiwaaw commited on
Commit
f6818d0
·
1 Parent(s): 2a299f9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -11
app.py CHANGED
@@ -1,16 +1,20 @@
1
  import streamlit as st
2
  from langchain.text_splitter import RecursiveCharacterTextSplitter
3
  from langchain.document_loaders import PyPDFLoader
4
- import time
5
  from transformers import T5Tokenizer, T5ForConditionalGeneration
6
  from transformers import pipeline
7
  import torch
8
  import base64
9
 
10
  # Model and tokenizer
 
11
  model_checkpoint = "MBZUAI/LaMini-Flan-T5-783M"
12
  model_tokenizer = T5Tokenizer.from_pretrained(model_checkpoint)
13
- model = T5ForConditionalGeneration.from_pretrained(model_checkpoint)
 
 
 
14
 
15
  # File loader and preprocessing
16
  def preprocess_pdf(file):
@@ -36,13 +40,37 @@ def language_model_pipeline(filepath):
36
  summarized_text = summary_result[0]['summary_text']
37
  return summarized_text
38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
- st.title("Document Summarization App using Language Model")
41
- uploaded_file = st.file_uploader("Upload your PDF file", type=['pdf'])
42
- if uploaded_file is not None:
43
- if st.button("Summarize"):
44
- filepath = uploaded_file.name
45
- with open(filepath, "wb") as temp_file:
46
- temp_file.write(uploaded_file.read())
47
- summarized_result = language_model_pipeline(filepath)
48
- st.success("Summarization Complete", summarized_result)
 
1
  import streamlit as st
2
  from langchain.text_splitter import RecursiveCharacterTextSplitter
3
  from langchain.document_loaders import PyPDFLoader
4
+
5
  from transformers import T5Tokenizer, T5ForConditionalGeneration
6
  from transformers import pipeline
7
  import torch
8
  import base64
9
 
10
  # Model and tokenizer
11
+ #model_checkpoint = "LaMini-Flan-T5-248M"
12
  model_checkpoint = "MBZUAI/LaMini-Flan-T5-783M"
13
  model_tokenizer = T5Tokenizer.from_pretrained(model_checkpoint)
14
+ model = T5ForConditionalGeneration.from_pretrained(model_checkpoint, device_map='auto', torch_dtype=torch.float32)
15
+
16
+ #REPO_ID = "MBZUAI/LaMini-Flan-T5-783M"
17
+ #model = pipeline(task='summarization', model=REPO_ID, token=access_token)
18
 
19
  # File loader and preprocessing
20
  def preprocess_pdf(file):
 
40
  summarized_text = summary_result[0]['summary_text']
41
  return summarized_text
42
 
43
+ @st.cache_data
44
+ # Function to display the PDF content
45
+ def display_pdf(file):
46
+ with open(file, "rb") as f:
47
+ base64_pdf = base64.b64encode(f.read()).decode('utf-8')
48
+
49
+ pdf_display = f'<iframe src="data:application/pdf;base64,{base64_pdf}" width="100%" height="600" type="application/pdf"></iframe>'
50
+ st.markdown(pdf_display, unsafe_allow_html=True)
51
+
52
+ # Streamlit code
53
+ #st.set_page_config(layout="wide")
54
+
55
+ def main():
56
+ st.title("Document Summarization App using Language Model")
57
+
58
+ uploaded_file = st.file_uploader("Upload your PDF file", type=['pdf'])
59
+
60
+ if uploaded_file is not None:
61
+ if st.button("Summarize"):
62
+ col1, col2 = st.columns(2)
63
+ filepath = "pdf/" + uploaded_file.name
64
+ with open(filepath, "wb") as temp_file:
65
+ temp_file.write(uploaded_file.read())
66
+ with col1:
67
+ st.info("Uploaded File")
68
+ pdf_view = display_pdf(filepath)
69
+
70
+ with col2:
71
+ summarized_result = language_model_pipeline(filepath)
72
+ st.info("Summarization Complete")
73
+ st.success(summarized_result)
74
 
75
+ if __name__ == "__main__":
76
+ main()