themanas021 commited on
Commit
2d63e89
Β·
1 Parent(s): f7c63f3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -54
app.py CHANGED
@@ -1,11 +1,8 @@
1
  import streamlit as st
2
  import os
3
- import base64
4
- import time
5
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
6
  from transformers import pipeline
7
  import torch
8
- import textwrap
9
  from langchain.embeddings import SentenceTransformerEmbeddings
10
  from langchain.vectorstores import Chroma
11
  from langchain.llms import HuggingFacePipeline
@@ -27,10 +24,7 @@ base_model = AutoModelForSeq2SeqLM.from_pretrained(
27
  persist_directory = "db"
28
 
29
  @st.cache_resource
30
- def data_ingestion(uploaded_file):
31
- with open(uploaded_file.name, "rb") as pdf_file:
32
- pdf_content = pdf_file.read()
33
-
34
  # Process the PDF content here and generate a brief summary.
35
  # You can use libraries like PyPDF2, pdfminer, or other PDF processing tools.
36
 
@@ -76,58 +70,29 @@ def process_answer(instruction):
76
  answer = generated_text['result']
77
  return answer
78
 
79
- def get_file_size(file):
80
- file.seek(0, os.SEEK_END)
81
- file_size = file.tell()
82
- file.seek(0)
83
- return file_size
84
-
85
- @st.cache_data
86
- # Function to display the PDF of a given file
87
- def displayPDF(file):
88
- # Opening file from file path
89
- with open(file, "rb") as f:
90
- base64_pdf = base64.b64encode(f.read()).decode('utf-8')
91
 
92
- # Embedding PDF in HTML
93
- pdf_display = F'<iframe src="data:application/pdf;base64,{base64_pdf}" width="100%" height="600" type="application/pdf"></iframe>'
94
 
95
- # Displaying File
96
- st.markdown(pdf_display, unsafe_allow_html=True)
97
 
98
- # Display conversation history using Streamlit messages
99
- def display_conversation(history):
100
- for i in range(len(history["generated"])):
101
- message(history["past"][i], is_user=True, key=str(i) + "_user")
102
- message(history["generated"][i], key=str(i))
103
 
104
- def main():
105
- st.markdown("<h1 style='text-align: center; color: blue;'>Chat with your PDF πŸ¦œπŸ“„ </h1>", unsafe_allow_html=True)
106
- st.markdown("<h3 style='text-align: center; color: grey;'>Built by <a href='https://github.com/manas95826'>The Manas with ❀️ </a></h3>", unsafe_allow_html=True)
 
107
 
108
- st.markdown("<h2 style='text-align: center; color:red;'>Upload your PDF πŸ‘‡</h2>", unsafe_allow_html=True)
109
-
110
- uploaded_file = st.file_uploader("", type=["pdf"])
111
-
112
- if uploaded_file is not None:
113
- file_details = {
114
- "Filename": uploaded_file.name,
115
- "File size": get_file_size(uploaded_file)
116
- }
117
-
118
- col1, col2 = st.columns([1, 2])
119
- with col1:
120
- st.markdown("<h4 style color:black;'>File details</h4>", unsafe_allow_html=True)
121
- st.json(file_details)
122
- st.markdown("<h4 style color:black;'>File preview</h4>", unsafe_allow_html=True)
123
- pdf_view = displayPDF(uploaded_file)
124
-
125
- with col2:
126
- with st.spinner('Processing the uploaded PDF...'):
127
- pdf_summary = data_ingestion(uploaded_file)
128
- st.success('PDF processing is complete!')
129
- st.markdown("<h4 style color:black;'>PDF Summary</h4>", unsafe_allow_html=True)
130
- st.write(pdf_summary)
131
 
132
  if __name__ == "__main__":
133
  main()
 
1
  import streamlit as st
2
  import os
 
 
3
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
4
  from transformers import pipeline
5
  import torch
 
6
  from langchain.embeddings import SentenceTransformerEmbeddings
7
  from langchain.vectorstores import Chroma
8
  from langchain.llms import HuggingFacePipeline
 
24
  persist_directory = "db"
25
 
26
  @st.cache_resource
27
+ def data_ingestion(pdf_content):
 
 
 
28
  # Process the PDF content here and generate a brief summary.
29
  # You can use libraries like PyPDF2, pdfminer, or other PDF processing tools.
30
 
 
70
  answer = generated_text['result']
71
  return answer
72
 
73
+ def main():
74
+ st.markdown("<h1 style='text-align: center; color: blue;'>Chat with your PDF πŸ¦œπŸ“„ </h1>", unsafe_allow_html=True)
75
+ st.markdown("<h3 style='text-align: center; color: grey;'>Built by <a href='https://github.com/manas95826'>The Manas with ❀️ </a></h3>", unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
76
 
77
+ st.markdown("<h2 style='text-align: center; color:red;'>Ask Questions about your PDF πŸ‘‡</h2>", unsafe_allow_html=True)
 
78
 
79
+ user_input = st.text_input("Ask a question:", key="input")
 
80
 
81
+ # Initialize session state for generated responses
82
+ if "generated" not in st.session_state:
83
+ st.session_state["generated"] = []
 
 
84
 
85
+ # Search the database for a response based on user input and update session state
86
+ if user_input:
87
+ answer = process_answer({'query': user_input})
88
+ st.session_state["generated"].append(answer)
89
 
90
+ # Display conversation history using Streamlit messages
91
+ if st.session_state["generated"]:
92
+ st.markdown("<h4>Conversation History:</h4>", unsafe_allow_html=True)
93
+ for i, response in enumerate(st.session_state["generated"]):
94
+ st.text(f"Q{i+1}: {user_input}")
95
+ st.text(f"A{i+1}: {response}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
 
97
  if __name__ == "__main__":
98
  main()