Update src/streamlit_app.py

#15
by Uzaiir - opened
Files changed (1) hide show
  1. src/streamlit_app.py +226 -129
src/streamlit_app.py CHANGED
@@ -1,181 +1,278 @@
1
- import streamlit as st
2
- import os
3
- from langchain_groq import ChatGroq
4
- from langchain.text_splitter import RecursiveCharacterTextSplitter
5
- from langchain.chains.combine_documents import create_stuff_documents_chain
6
- from langchain_core.prompts import ChatPromptTemplate
7
- from langchain.chains import create_retrieval_chain
8
- from langchain_community.vectorstores import FAISS
9
- from langchain_community.document_loaders import PyPDFDirectoryLoader
10
- from langchain_google_genai import GoogleGenerativeAIEmbeddings
11
- from dotenv import load_dotenv
12
- from PDFprocess_sample import process_pdf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
- # Loading GROQ and Google API
15
- load_dotenv()
 
 
 
16
 
17
- GROQ_API_KEY = os.getenv('GROQ_API_KEY')
18
- os.environ["GOOGLE_API_KEY"]= os.getenv('GOOGLE_API_KEY')
19
 
20
- #Loading CSS files
21
 
22
- # def load_css(file_name):
23
- # with open(file_name) as f:
24
- # css = f.read()
25
- # st.markdown(f"<style>{css}</style>", unsafe_allow_html=True)
26
 
27
- import os
 
 
 
 
28
 
29
- def load_css(file_name):
30
- current_dir = os.path.dirname(__file__)
31
- file_path = os.path.join(current_dir, file_name)
32
- with open(file_path, "r") as f:
33
- st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
34
 
35
- load_css("CSS/style.css")
 
 
 
 
 
 
 
 
 
36
 
 
37
 
38
 
39
- #setting up LLM
40
- llm = ChatGroq(
41
- api_key=GROQ_API_KEY,
42
- model_name="Llama3-8b-8192"
43
- )
44
 
 
45
 
46
- prompt = ChatPromptTemplate.from_template(
47
- """
48
- Answer the questions based on the provided context only.
49
- Please provide the most accurate response based on the question. Try to answer in detail in 1500 words
50
- <context>
51
- {context}
52
- <context>
53
- Questions: {input}
54
- """
55
- )
56
 
57
- input_method = st.sidebar.selectbox("Choose a method" , ["Choose input method...","Interact with Doc", "Get Ques from Doc"])
58
 
59
 
 
60
 
61
- st.sidebar.title("Upload your pdf")
62
 
63
- main_placeholder = st.empty()
64
- # #Document upload
65
- # uploaded_file = st.sidebar.file_uploader("_____________________________________", type="pdf", accept_multiple_files=True)
66
- # st.sidebar.write("Press Submit to process:")
67
 
 
 
68
 
 
 
 
 
 
 
 
 
 
 
 
69
 
70
- # process = st.sidebar.button("Submit")
71
 
72
- uploaded_files = st.sidebar.file_uploader("Upload your PDFs", type="pdf", accept_multiple_files=True)
73
 
74
 
75
- process = st.sidebar.button("Submit")
76
- # Document processing
 
 
 
 
 
77
 
78
- if process:
79
- if uploaded_files:
80
- for uploaded_file in uploaded_files:
81
- file_path = f"/tmp/{uploaded_file.name}"
82
- with open(file_path, "wb") as f:
83
- f.write(uploaded_file.getbuffer())
84
- st.write(f"Processing file: {file_path}")
85
- st.success(f"{uploaded_file.name} uploaded successfully!")
86
- process_pdf(file_path)
87
- else:
88
- st.warning("Please upload at least one PDF file.")
 
89
 
90
 
91
 
 
 
 
 
 
 
 
 
 
 
92
 
93
- #Document processing to convert it into vectors
94
- # if process:
95
- # if uploaded_file:
96
- # # Process the uploaded PDF file
97
- # process_pdf(uploaded_file)
98
- # else:
99
- # st.warning("Please upload a PDF file.")
100
 
101
- # Document processing
102
- # if process:
103
- # if uploaded_file:
104
- # # Save to /tmp/ before processing
105
- # file_path = f"/tmp/{uploaded_file.name}"
106
- # with open(file_path, "wb") as f:
107
- # f.write(uploaded_file.getbuffer())
108
-
109
- # # Call your existing logic with the saved path
110
- # process_pdf(file_path)
111
- # else:
112
- # st.warning("Please upload a PDF file.")
113
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
114
 
115
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
 
 
 
 
 
 
 
 
 
 
 
 
117
  if input_method == "Choose input method...":
118
- st.title(f"Welcome You all!")
119
- st.title("Choose an option in the sidebar")
120
- st.title("Now, let's get started!")
121
 
122
-
123
- #If User wants to interact with the document
124
  elif input_method == "Interact with Doc":
125
- st.title(f"let's Interact with pdf's")
126
 
127
- prompt1 = st.text_input("______", placeholder="Enter your Question")
128
 
129
-
130
- # Generate response if question is entered
131
  if prompt1 and "vectors" in st.session_state:
132
- document_chain = create_stuff_documents_chain(llm, prompt)
133
  retriever = st.session_state.vectors.as_retriever()
134
  retrieval_chain = create_retrieval_chain(retriever, document_chain)
135
 
136
-
137
  response = retrieval_chain.invoke({'input': prompt1})
138
 
139
- # st.write(response['answer'])
140
-
141
- #Get the respose in the card
142
  st.markdown(
143
- f"""
144
- <div class="card">
145
- <div class="response">{response['answer']}</div>
146
- </div>
147
- """,
148
- unsafe_allow_html=True,
149
- )
150
-
151
-
152
-
153
- #When User wants to get questions from the doc based on certain topic
154
  elif input_method == "Get Ques from Doc":
155
- st.title(f"Let's Get Ques from Document")
156
-
157
- prompt2 = """Based on the topic of {topic},
158
- kindly provide a comprehensive list of all possible questions that could arise.
159
- For each question, provide detailed and explanatory answers in atleast 1000 words detail based on the context,
160
- ensuring that the responses are as informative as possible.
161
- make sure you strictly follow the {topic}"""
162
- topic = st.text_input("Enter a topic", placeholder="What is your topic")
163
-
164
- # Generate response if question is entered
165
  if topic and "vectors" in st.session_state:
166
- document_chain = create_stuff_documents_chain(llm, prompt)
 
 
 
 
 
 
 
 
167
  retriever = st.session_state.vectors.as_retriever()
168
  retrieval_chain = create_retrieval_chain(retriever, document_chain)
169
 
170
-
171
  response = retrieval_chain.invoke({'input': prompt2})
172
-
173
- #Get the respose in the card
174
  st.markdown(
175
- f"""
176
- <div class="card">
177
- <div class="response">{response['answer']}</div>
178
- </div>
179
- """,
180
- unsafe_allow_html=True,
181
- )
 
 
1
+ # import streamlit as st
2
+ # import os
3
+ # from langchain_groq import ChatGroq
4
+ # from langchain.text_splitter import RecursiveCharacterTextSplitter
5
+ # from langchain.chains.combine_documents import create_stuff_documents_chain
6
+ # from langchain_core.prompts import ChatPromptTemplate
7
+ # from langchain.chains import create_retrieval_chain
8
+ # from langchain_community.vectorstores import FAISS
9
+ # from langchain_community.document_loaders import PyPDFDirectoryLoader
10
+ # from langchain_google_genai import GoogleGenerativeAIEmbeddings
11
+ # from dotenv import load_dotenv
12
+ # from PDFprocess_sample import process_pdf
13
+
14
+ # # Loading GROQ and Google API
15
+ # load_dotenv()
16
+
17
+ # GROQ_API_KEY = os.getenv('GROQ_API_KEY')
18
+ # os.environ["GOOGLE_API_KEY"]= os.getenv('GOOGLE_API_KEY')
19
+
20
+ # #Loading CSS files
21
+
22
+ # # def load_css(file_name):
23
+ # # with open(file_name) as f:
24
+ # # css = f.read()
25
+ # # st.markdown(f"<style>{css}</style>", unsafe_allow_html=True)
26
+
27
+ # import os
28
 
29
+ # def load_css(file_name):
30
+ # current_dir = os.path.dirname(__file__)
31
+ # file_path = os.path.join(current_dir, file_name)
32
+ # with open(file_path, "r") as f:
33
+ # st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
34
 
35
+ # load_css("CSS/style.css")
 
36
 
 
37
 
 
 
 
 
38
 
39
+ # #setting up LLM
40
+ # llm = ChatGroq(
41
+ # api_key=GROQ_API_KEY,
42
+ # model_name="Llama3-8b-8192"
43
+ # )
44
 
 
 
 
 
 
45
 
46
+ # prompt = ChatPromptTemplate.from_template(
47
+ # """
48
+ # Answer the questions based on the provided context only.
49
+ # Please provide the most accurate response based on the question. Try to answer in detail in 1500 words
50
+ # <context>
51
+ # {context}
52
+ # <context>
53
+ # Questions: {input}
54
+ # """
55
+ # )
56
 
57
+ # input_method = st.sidebar.selectbox("Choose a method" , ["Choose input method...","Interact with Doc", "Get Ques from Doc"])
58
 
59
 
 
 
 
 
 
60
 
61
+ # st.sidebar.title("Upload your pdf")
62
 
63
+ # main_placeholder = st.empty()
64
+ # # #Document upload
65
+ # # uploaded_file = st.sidebar.file_uploader("_____________________________________", type="pdf", accept_multiple_files=True)
66
+ # # st.sidebar.write("Press Submit to process:")
 
 
 
 
 
 
67
 
 
68
 
69
 
70
+ # # process = st.sidebar.button("Submit")
71
 
72
+ # uploaded_files = st.sidebar.file_uploader("Upload your PDFs", type="pdf", accept_multiple_files=True)
73
 
 
 
 
 
74
 
75
+ # process = st.sidebar.button("Submit")
76
+ # # Document processing
77
 
78
+ # if process:
79
+ # if uploaded_files:
80
+ # for uploaded_file in uploaded_files:
81
+ # file_path = f"/tmp/{uploaded_file.name}"
82
+ # with open(file_path, "wb") as f:
83
+ # f.write(uploaded_file.getbuffer())
84
+ # st.write(f"Processing file: {file_path}")
85
+ # st.success(f"{uploaded_file.name} uploaded successfully!")
86
+ # process_pdf(file_path)
87
+ # else:
88
+ # st.warning("Please upload at least one PDF file.")
89
 
 
90
 
 
91
 
92
 
93
+ # #Document processing to convert it into vectors
94
+ # # if process:
95
+ # # if uploaded_file:
96
+ # # # Process the uploaded PDF file
97
+ # # process_pdf(uploaded_file)
98
+ # # else:
99
+ # # st.warning("Please upload a PDF file.")
100
 
101
+ # # Document processing
102
+ # # if process:
103
+ # # if uploaded_file:
104
+ # # # Save to /tmp/ before processing
105
+ # # file_path = f"/tmp/{uploaded_file.name}"
106
+ # # with open(file_path, "wb") as f:
107
+ # # f.write(uploaded_file.getbuffer())
108
+
109
+ # # # Call your existing logic with the saved path
110
+ # # process_pdf(file_path)
111
+ # # else:
112
+ # # st.warning("Please upload a PDF file.")
113
 
114
 
115
 
116
+
117
+ # if input_method == "Choose input method...":
118
+ # st.title(f"Welcome You all!")
119
+ # st.title("Choose an option in the sidebar")
120
+ # st.title("Now, let's get started!")
121
+
122
+
123
+ # #If User wants to interact with the document
124
+ # elif input_method == "Interact with Doc":
125
+ # st.title(f"let's Interact with pdf's")
126
 
127
+ # prompt1 = st.text_input("______", placeholder="Enter your Question")
 
 
 
 
 
 
128
 
 
 
 
 
 
 
 
 
 
 
 
 
129
 
130
+ # # Generate response if question is entered
131
+ # if prompt1 and "vectors" in st.session_state:
132
+ # document_chain = create_stuff_documents_chain(llm, prompt)
133
+ # retriever = st.session_state.vectors.as_retriever()
134
+ # retrieval_chain = create_retrieval_chain(retriever, document_chain)
135
+
136
+
137
+ # response = retrieval_chain.invoke({'input': prompt1})
138
+
139
+ # # st.write(response['answer'])
140
+
141
+ # #Get the respose in the card
142
+ # st.markdown(
143
+ # f"""
144
+ # <div class="card">
145
+ # <div class="response">{response['answer']}</div>
146
+ # </div>
147
+ # """,
148
+ # unsafe_allow_html=True,
149
+ # )
150
+
151
+
152
+
153
+ # #When User wants to get questions from the doc based on certain topic
154
+ # elif input_method == "Get Ques from Doc":
155
+ # st.title(f"Let's Get Ques from Document")
156
+
157
+ # prompt2 = """Based on the topic of {topic},
158
+ # kindly provide a comprehensive list of all possible questions that could arise.
159
+ # For each question, provide detailed and explanatory answers in atleast 1000 words detail based on the context,
160
+ # ensuring that the responses are as informative as possible.
161
+ # make sure you strictly follow the {topic}"""
162
+ # topic = st.text_input("Enter a topic", placeholder="What is your topic")
163
+
164
+ # # Generate response if question is entered
165
+ # if topic and "vectors" in st.session_state:
166
+ # document_chain = create_stuff_documents_chain(llm, prompt)
167
+ # retriever = st.session_state.vectors.as_retriever()
168
+ # retrieval_chain = create_retrieval_chain(retriever, document_chain)
169
+
170
+
171
+ # response = retrieval_chain.invoke({'input': prompt2})
172
+
173
+ # #Get the respose in the card
174
+ # st.markdown(
175
+ # f"""
176
+ # <div class="card">
177
+ # <div class="response">{response['answer']}</div>
178
+ # </div>
179
+ # """,
180
+ # unsafe_allow_html=True,
181
+ # )
182
 
183
 
184
+ import streamlit as st
185
+ import os
186
+ from PDFprocess_sample import process_pdf_from_path
187
+ from langchain_core.runnables import Runnable
188
+ from langchain.chains import RetrievalQA
189
+ from langchain.chains.combine_documents import create_stuff_documents_chain
190
+ from langchain_community.chat_models import ChatGoogleGenerativeAI
191
+ from langchain.chains.retrieval_qa.base import create_retrieval_chain
192
+
193
+ # Set up the page
194
+ st.set_page_config(page_title="Chat with your PDF", layout="wide")
195
+
196
+ # CSS Styling
197
+ with open("src/CSS/style.css") as f:
198
+ st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
199
+
200
+ st.markdown("<h1 class='main-heading'>Chat with your PDF using Gemini AI</h1>", unsafe_allow_html=True)
201
+
202
+ # Sidebar Upload
203
+ st.sidebar.markdown("<h2 style='color: white;'>Upload PDFs</h2>", unsafe_allow_html=True)
204
+ uploaded_files = st.sidebar.file_uploader("Upload your PDFs", type="pdf", accept_multiple_files=True)
205
+
206
+ if uploaded_files:
207
+ for uploaded_file in uploaded_files:
208
+ file_path = f"/tmp/{uploaded_file.name}"
209
+
210
+ with open(file_path, "wb") as f:
211
+ f.write(uploaded_file.getbuffer())
212
 
213
+ st.sidebar.success(f"{uploaded_file.name} uploaded successfully!")
214
+ st.write(f"Processing file: {uploaded_file.name}")
215
+
216
+ process_pdf_from_path(file_path)
217
+
218
+ # Select input method
219
+ input_method = st.sidebar.selectbox("Choose input method...", ["Choose input method...", "Interact with Doc", "Get Ques from Doc"])
220
+
221
+ # Initialize LLM
222
+ llm = ChatGoogleGenerativeAI(model="gemini-pro")
223
+
224
  if input_method == "Choose input method...":
225
+ st.title("Welcome You all!")
226
+ st.subheader("Choose an option in the sidebar")
227
+ st.write("Now, let's get started!")
228
 
 
 
229
  elif input_method == "Interact with Doc":
230
+ st.title("Let's Interact with the PDF")
231
 
232
+ prompt1 = st.text_input("Ask a question", placeholder="Enter your Question")
233
 
 
 
234
  if prompt1 and "vectors" in st.session_state:
235
+ document_chain = create_stuff_documents_chain(llm, prompt="Answer the question based on the document.")
236
  retriever = st.session_state.vectors.as_retriever()
237
  retrieval_chain = create_retrieval_chain(retriever, document_chain)
238
 
 
239
  response = retrieval_chain.invoke({'input': prompt1})
240
 
 
 
 
241
  st.markdown(
242
+ f"""
243
+ <div class="card">
244
+ <div class="response">{response['answer']}</div>
245
+ </div>
246
+ """,
247
+ unsafe_allow_html=True,
248
+ )
249
+
 
 
 
250
  elif input_method == "Get Ques from Doc":
251
+ st.title("Let's Generate Questions from the Document")
252
+
253
+ topic = st.text_input("Enter a topic", placeholder="What is your topic?")
254
+
 
 
 
 
 
 
255
  if topic and "vectors" in st.session_state:
256
+ prompt2 = f"""
257
+ Based on the topic of {topic},
258
+ kindly provide a comprehensive list of all possible questions that could arise.
259
+ For each question, provide detailed and explanatory answers in at least 1000 words,
260
+ ensuring that the responses are as informative as possible.
261
+ Make sure you strictly follow the topic of {topic}.
262
+ """
263
+
264
+ document_chain = create_stuff_documents_chain(llm, prompt="Generate questions and answers based on the topic and document.")
265
  retriever = st.session_state.vectors.as_retriever()
266
  retrieval_chain = create_retrieval_chain(retriever, document_chain)
267
 
 
268
  response = retrieval_chain.invoke({'input': prompt2})
269
+
 
270
  st.markdown(
271
+ f"""
272
+ <div class="card">
273
+ <div class="response">{response['answer']}</div>
274
+ </div>
275
+ """,
276
+ unsafe_allow_html=True,
277
+ )
278
+