rahgadda commited on
Commit
526daeb
1 Parent(s): 2b71ac0

Initial Draft

Browse files
Files changed (1) hide show
  1. app.py +300 -153
app.py CHANGED
@@ -1,36 +1,25 @@
 
1
  import os
 
2
  import re
3
- import streamlit as st
4
- import google.generativeai as genai
5
- from dotenv import load_dotenv
6
 
7
- from langchain_community.document_loaders import TextLoader
8
  from langchain_community.document_loaders import PyPDFLoader
9
  from langchain.docstore.document import Document
10
- from langchain import PromptTemplate
11
- from langchain_google_genai import ChatGoogleGenerativeAI
 
12
 
13
- # Loading Google Gemini API Key from Environment Variables
 
 
 
 
 
 
 
14
  load_dotenv()
15
  genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
16
 
17
- # Display user Error, Warning or Success Message
18
- def fn_display_user_messages(lv_text, lv_type, mv_processing_message):
19
- """Display user Info, Error, Warning or Success Message"""
20
-
21
- if lv_type == "Success":
22
- with mv_processing_message.container():
23
- st.success(lv_text)
24
- elif lv_type == "Error":
25
- with mv_processing_message.container():
26
- st.error(lv_text)
27
- elif lv_type == "Warning":
28
- with mv_processing_message.container():
29
- st.warning(lv_text)
30
- else:
31
- with mv_processing_message.container():
32
- st.info(lv_text)
33
-
34
  # Upload pdf file into 'pdf-data' folder if it does not exist
35
  def fn_upload_pdf(mv_pdf_input_file, mv_processing_message):
36
  """Upload pdf file into 'pdf-data' folder if it does not exist"""
@@ -43,8 +32,8 @@ def fn_upload_pdf(mv_pdf_input_file, mv_processing_message):
43
  lv_temp_file_path = os.path.join("pdf-data",lv_file_name)
44
 
45
  if os.path.exists(lv_temp_file_path):
46
- print("Step1: File already available")
47
- fn_display_user_messages("Step1: File already available","Warning", mv_processing_message)
48
  else:
49
  with open(lv_temp_file_path,"wb") as lv_file:
50
  lv_file.write(mv_pdf_input_file.getbuffer())
@@ -52,77 +41,137 @@ def fn_upload_pdf(mv_pdf_input_file, mv_processing_message):
52
  print("Step1: PDF uploaded successfully at -> " + lv_temp_file_path)
53
  fn_display_user_messages("Step1: PDF uploaded successfully at -> " + lv_temp_file_path, "Info", mv_processing_message)
54
 
55
- # Extract uploaded pdf data
56
- def fn_extract_pdf_data(mv_pdf_input_file, mv_processing_message):
57
- """Extract uploaded pdf data"""
58
-
59
- lv_temp_pdf_file_path = os.path.join("pdf-data",mv_pdf_input_file.name)
60
 
61
- # -- Loading PDF Data
62
- lv_pdf_loader = PyPDFLoader(lv_temp_pdf_file_path)
63
- lv_pdf_content = lv_pdf_loader.load()
64
 
65
- # -- Define patterns with flexibility
66
- pattern1 = r"(\w+)-\n(\w+)" # Match hyphenated words separated by a line break
67
- pattern2 = r"(?<!\n\s)\n(?!\s\n)" # Match line breaks not surrounded by whitespace
68
- pattern3 = r"\n\s*\n" # Match multiple line breaks with optional whitespace
69
-
70
- lv_pdf_formatted_content = []
71
 
72
- for lv_page in lv_pdf_content:
73
- # -- Apply substitutions with flexibility
74
- lv_pdf_page_content = re.sub(pattern1, r"\1\2", lv_page.page_content)
75
- lv_pdf_page_content = re.sub(pattern2, " ", lv_pdf_page_content.strip())
76
- lv_pdf_page_content = re.sub(pattern3, " ", lv_pdf_page_content)
77
- lv_pdf_page_content = re.sub("\n", " ", lv_pdf_page_content)
78
-
79
- lv_pdf_formatted_content.append(
80
- Document( page_content= lv_pdf_page_content,
81
- metadata= lv_page.metadata
82
- )
83
- )
84
 
85
- # print("Page Details of "+str(lv_page.metadata)+" is - "+lv_pdf_page_content)
 
 
86
 
87
- print("Step2: PDF content extracted")
88
- fn_display_user_messages("Step2: PDF content extracted", "Info", mv_processing_message)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
 
90
- return lv_pdf_formatted_content
 
 
 
 
91
 
92
- # Load PDF data as Text File
93
- def fn_process_pf_data(mv_pdf_input_file, mv_processing_message):
94
- """Load PDF data as Text File"""
95
 
96
- # -- Create txt folder inside vectordb folder if it does not exist
97
- if not os.path.exists(os.path.join("vectordb","txt")):
98
- os.makedirs(os.path.join("vectordb","txt"))
 
 
 
 
 
 
 
 
 
 
 
 
 
99
 
100
- lv_file_name = mv_pdf_input_file.name[:-4] + ".txt"
101
- lv_temp_file_path = os.path.join(os.path.join("vectordb","txt"),lv_file_name)
 
102
 
103
- if os.path.isfile(lv_temp_file_path):
104
- print("Step2: Processed file details exists")
105
- fn_display_user_messages("Step2: Processed file details exists", "Warning", mv_processing_message)
106
- else:
107
- lv_pdf_formatted_content = fn_extract_pdf_data(mv_pdf_input_file, mv_processing_message)
108
- lv_text_data = ""
109
-
110
- for lv_page in lv_pdf_formatted_content:
111
- # print(lv_page.page_content)
112
- lv_text_data = lv_text_data + lv_page.page_content
113
-
114
- # print(lv_text_data)
115
- f = open(lv_temp_file_path, "w")
116
- f.write(lv_text_data)
117
- f.close()
118
 
119
- # Return QA Response
120
- def fn_generate_QnA_response(mv_user_question, mv_pdf_input_file, mv_processing_message):
121
- """Returns QA Response"""
122
 
123
- print("Step4: Generating LLM response")
124
- fn_display_user_messages("Step4: Generating LLM response","Info", mv_processing_message)
 
 
 
 
 
 
 
 
 
 
125
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
  lv_template = """Instruction:
127
  You are an AI assistant for answering questions about the provided context.
128
  You are given the following extracted parts of a long document and a question. Provide a detailed answer.
@@ -132,96 +181,194 @@ def fn_generate_QnA_response(mv_user_question, mv_pdf_input_file, mv_processing_
132
  =======
133
  Question: {question}
134
  Output:\n"""
135
-
136
  lv_qa_prompt = PromptTemplate(
137
- template=lv_template,
138
- input_variables=["question", "context"]
139
- )
140
- # lv_model = ChatGoogleGenerativeAI(model="gemini-pro",
141
- # temperature=0.7, top_p=0.85)
142
-
143
- lv_model = genai.GenerativeModel('gemini-pro')
144
-
145
- lv_file_name = mv_pdf_input_file.name[:-4] + ".txt"
146
- lv_temp_file_path = os.path.join(os.path.join("vectordb","txt"),lv_file_name)
147
- lv_text_loader = TextLoader(lv_temp_file_path)
148
- lv_pdf_formatted_content = lv_text_loader.load()
149
- lv_text_data = ""
150
- for lv_page in lv_pdf_formatted_content:
151
- lv_text_data = lv_text_data + lv_page.page_content
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
152
 
153
  lv_qa_formatted_prompt = lv_qa_prompt.format(
154
- question=mv_user_question,
155
- context=lv_text_data
156
- )
157
-
158
- # lv_llm_response = lv_model.invoke(lv_qa_formatted_prompt).content
159
- lv_llm_response = lv_model.generate_content(lv_qa_formatted_prompt).text
 
160
 
161
  print("Step5: LLM response generated")
162
  fn_display_user_messages("Step5: LLM response generated","Info", mv_processing_message)
163
 
164
  return lv_llm_response
165
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
166
 
167
- # Main Program
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
168
  def main():
 
169
  # -- Streamlit Settings
170
- st.set_page_config("Chat With Your Product User Manual")
171
- st.header("Chat With Your Product User Manual💁")
172
- st.text("")
173
  st.text("")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
174
  st.text("")
175
 
 
 
 
 
176
  # -- Display Processing Details
177
- mv_processing_message = st.empty()
178
  st.text("")
 
 
179
  st.text("")
180
 
181
- # -- Setting Chat History
182
- if "messages" not in st.session_state:
183
- st.session_state["messages"] = []
 
 
 
 
 
 
 
 
 
 
 
184
 
185
- # -- Read User Manuals for Q&A
186
- with st.sidebar:
187
- mv_pdf_input_file = st.file_uploader("Choose a UM PDF file:", type=["pdf"])
188
  st.text("")
 
189
  st.text("")
 
 
 
 
 
 
 
 
 
 
 
 
 
190
 
191
- # -- Process Uploaded User Manual PDF
192
- col1, col2, col3 = st.columns(3)
193
- if col1.button("Submit"):
194
- if mv_pdf_input_file is not None:
195
- fn_upload_pdf(mv_pdf_input_file, mv_processing_message)
196
- lv_pdf_page_content = fn_process_pf_data(mv_pdf_input_file, mv_processing_message)
197
- else:
198
- fn_display_user_messages("Upload PDF file before clicking on Submit", "Error", mv_processing_message)
199
-
200
- # -- Clear Chat History
201
- if col2.button("Reset"):
202
- st.session_state["messages"] = []
203
-
204
- # -- Creating Chat Details
205
- mv_user_question = st.chat_input("Pass your input here")
206
-
207
- # -- Recording Chat Input and Generating Response
208
- if mv_user_question:
209
- # -- Saving User Input
210
- st.session_state.messages.append({"role": "user", "content": mv_user_question})
211
-
212
- # -- Generating LLM Response
213
- lv_response = fn_generate_QnA_response(mv_user_question, mv_pdf_input_file, mv_processing_message)
214
-
215
- # -- Saving LLM Response
216
- st.session_state.messages.append(
217
- {"role": "agent", "content": lv_response}
218
- )
219
-
220
- # -- Display chat messages from history
221
- for message in st.session_state.messages:
222
- with st.chat_message(message["role"]):
223
- st.markdown(message["content"])
224
-
225
- # Loading Main
226
- if __name__ == "__main__":
227
  main()
 
1
+ import streamlit as st
2
  import os
3
+ import requests
4
  import re
 
 
 
5
 
 
6
  from langchain_community.document_loaders import PyPDFLoader
7
  from langchain.docstore.document import Document
8
+ from langchain.text_splitter import CharacterTextSplitter
9
+ from langchain_community.embeddings import HuggingFaceEmbeddings
10
+ from langchain.vectorstores.faiss import FAISS
11
 
12
+ from langchain.prompts.prompt import PromptTemplate
13
+ from langchain_community.llms import LlamaCpp
14
+ from langchain.chains import RetrievalQA
15
+
16
+ from dotenv import load_dotenv
17
+ import google.generativeai as genai
18
+
19
+ # Loading Google Gemini
20
  load_dotenv()
21
  genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  # Upload pdf file into 'pdf-data' folder if it does not exist
24
  def fn_upload_pdf(mv_pdf_input_file, mv_processing_message):
25
  """Upload pdf file into 'pdf-data' folder if it does not exist"""
 
32
  lv_temp_file_path = os.path.join("pdf-data",lv_file_name)
33
 
34
  if os.path.exists(lv_temp_file_path):
35
+ print("File already available")
36
+ fn_display_user_messages("File already available","Warning", mv_processing_message)
37
  else:
38
  with open(lv_temp_file_path,"wb") as lv_file:
39
  lv_file.write(mv_pdf_input_file.getbuffer())
 
41
  print("Step1: PDF uploaded successfully at -> " + lv_temp_file_path)
42
  fn_display_user_messages("Step1: PDF uploaded successfully at -> " + lv_temp_file_path, "Info", mv_processing_message)
43
 
44
+ # Create Vector DB of uploaded PDF
45
+ def fn_create_vector_db(mv_pdf_input_file, mv_processing_message):
46
+ """Create Vector DB of uploaded PDF"""
 
 
47
 
48
+ lv_file_name = mv_pdf_input_file.name[:-4] + ".vectorstore"
 
 
49
 
50
+ if not os.path.exists(os.path.join("vectordb","fiaas")):
51
+ os.makedirs(os.path.join("vectordb","fiaas"))
 
 
 
 
52
 
53
+ lv_temp_file_path = os.path.join(os.path.join("vectordb","fiaas"),lv_file_name)
54
+ lv_embeddings = HuggingFaceEmbeddings(
55
+ model_name="sentence-transformers/all-mpnet-base-v2",
56
+ model_kwargs={'device': 'cpu'}
57
+ )
 
 
 
 
 
 
 
58
 
59
+ if os.path.exists(lv_temp_file_path):
60
+ print("VectorDB already available for uploaded file")
61
+ fn_display_user_messages("VectorDB already available for uploaded file","Warning", mv_processing_message)
62
 
63
+ lv_vector_store = FAISS.load_local(lv_temp_file_path, lv_embeddings,allow_dangerous_deserialization=True)
64
+ return lv_vector_store
65
+ else:
66
+ lv_temp_pdf_file_path = os.path.join("pdf-data",mv_pdf_input_file.name)
67
+
68
+ # -- Loading PDF Data
69
+ lv_pdf_loader = PyPDFLoader(lv_temp_pdf_file_path)
70
+ lv_pdf_content = lv_pdf_loader.load()
71
+
72
+ # -- Define patterns with flexibility
73
+ pattern1 = r"(\w+)-\n(\w+)" # Match hyphenated words separated by a line break
74
+ pattern2 = r"(?<!\n\s)\n(?!\s\n)" # Match line breaks not surrounded by whitespace
75
+ pattern3 = r"\n\s*\n" # Match multiple line breaks with optional whitespace
76
+
77
+ lv_pdf_formatted_content = []
78
+ for lv_page in lv_pdf_content:
79
+ # -- Apply substitutions with flexibility
80
+ lv_pdf_page_content = re.sub(pattern1, r"\1\2", lv_page.page_content)
81
+ lv_pdf_page_content = re.sub(pattern2, " ", lv_pdf_page_content.strip())
82
+ lv_pdf_page_content = re.sub(pattern3, " ", lv_pdf_page_content)
83
+ lv_pdf_page_content = re.sub("\n", " ", lv_pdf_page_content)
84
+
85
+ lv_pdf_formatted_content.append(Document( page_content= lv_pdf_page_content,
86
+ metadata= lv_page.metadata)
87
+ )
88
+
89
+ # print("Page Details of "+str(lv_page.metadata)+" is - "+lv_pdf_page_content)
90
+
91
+ print("Step2: PDF content extracted")
92
+ fn_display_user_messages("Step2: PDF content extracted", "Info", mv_processing_message)
93
+
94
+ # -- Chunking PDF Data
95
+ lv_text_splitter = CharacterTextSplitter(
96
+ separator="\n",
97
+ chunk_size=300,
98
+ chunk_overlap=30,
99
+ length_function=len
100
+ )
101
+ lv_pdf_chunk_documents = lv_text_splitter.split_documents(lv_pdf_formatted_content)
102
+ print("Step3: PDF content chucked and document object created")
103
+ fn_display_user_messages("Step3: PDF content chucked and document object created", "Info", mv_processing_message)
104
 
105
+ # -- Creating FIASS Vector Store
106
+ lv_vector_store = FAISS.from_documents(lv_pdf_chunk_documents, lv_embeddings)
107
+ print("Step4: Vector store created")
108
+ fn_display_user_messages("Step4: Vector store created", "Info", mv_processing_message)
109
+ lv_vector_store.save_local(lv_temp_file_path)
110
 
111
+ return lv_vector_store
 
 
112
 
113
+ # Display user Error, Warning or Success Message
114
+ def fn_display_user_messages(lv_text, lv_type, mv_processing_message):
115
+ """Display user Info, Error, Warning or Success Message"""
116
+
117
+ if lv_type == "Success":
118
+ with mv_processing_message.container():
119
+ st.success(lv_text)
120
+ elif lv_type == "Error":
121
+ with mv_processing_message.container():
122
+ st.error(lv_text)
123
+ elif lv_type == "Warning":
124
+ with mv_processing_message.container():
125
+ st.warning(lv_text)
126
+ else:
127
+ with mv_processing_message.container():
128
+ st.info(lv_text)
129
 
130
+ # Download TheBloke Models
131
+ def fn_download_llm_models(mv_selected_model, mv_processing_message):
132
+ """Download TheBloke Models"""
133
 
134
+ lv_download_url = ""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135
 
136
+ print("Downloading TheBloke of "+mv_selected_model)
137
+ fn_display_user_messages("Downloading TheBloke of "+mv_selected_model, "Info", mv_processing_message)
 
138
 
139
+ if mv_selected_model == 'microsoft/phi-2':
140
+ lv_download_url = "https://huggingface.co/TheBloke/phi-2-GGUF/resolve/main/phi-2.Q2_K.gguf"
141
+ elif mv_selected_model == 'google/gemma-2b':
142
+ lv_download_url = "https://huggingface.co/MaziyarPanahi/gemma-2b-it-GGUF/resolve/main/gemma-2b-it.Q2_K.gguf"
143
+ elif mv_selected_model == 'mistralai/Mistral-7B-Instruct-v0.2':
144
+ lv_download_url = "https://huggingface.co/TheBloke/Mistral-7B-Instruct-v0.2-GGUF/resolve/main/mistral-7b-instruct-v0.2.Q2_K.gguf"
145
+
146
+ if not os.path.exists("model"):
147
+ os.makedirs("model")
148
+
149
+ lv_filename = os.path.basename(lv_download_url)
150
+ lv_temp_file_path = os.path.join("model",lv_filename)
151
 
152
+ if os.path.exists(lv_temp_file_path):
153
+ print("Model already available")
154
+ fn_display_user_messages("Model already available","Warning", mv_processing_message)
155
+ else:
156
+ lv_response = requests.get(lv_download_url, stream=True)
157
+ if lv_response.status_code == 200:
158
+ with open(lv_temp_file_path, 'wb') as f:
159
+ for chunk in lv_response.iter_content(chunk_size=1024):
160
+ if chunk:
161
+ f.write(chunk)
162
+
163
+ print("Download completed")
164
+ fn_display_user_messages("Model download completed","Info", mv_processing_message)
165
+ else:
166
+ print(f"Model download completed {response.status_code}")
167
+ fn_display_user_messages(f"Model download completed {response.status_code}","Error", mv_processing_message)
168
+
169
+ # Function return QA Response using Vector Store
170
+ def fn_generate_QnA_response(mv_selected_model, mv_user_question, lv_vector_store, mv_processing_message):
171
+ """Returns QA Response using Vector Store"""
172
+
173
+ lv_model_path = ""
174
+ lv_model_type = ""
175
  lv_template = """Instruction:
176
  You are an AI assistant for answering questions about the provided context.
177
  You are given the following extracted parts of a long document and a question. Provide a detailed answer.
 
181
  =======
182
  Question: {question}
183
  Output:\n"""
 
184
  lv_qa_prompt = PromptTemplate(
185
+ template=lv_template,
186
+ input_variables=["question", "context"]
187
+ )
188
+
189
+ if mv_selected_model == 'microsoft/phi-2':
190
+ lv_model_path = "model/phi-2.Q2_K.gguf"
191
+ lv_model_type = "pi"
192
+ elif mv_selected_model == 'google/gemma-2b':
193
+ lv_model_path = "model/gemma-2b-it.Q2_K.gguf"
194
+ lv_model_type = "gemma"
195
+ elif mv_selected_model == 'mistralai/Mistral-7B-Instruct-v0.2':
196
+ lv_model_path = "model/mistral-7b-instruct-v0.2.Q2_K.gguf"
197
+ lv_model_type = "mistral"
198
+
199
+ print("Step4: Generating LLM response")
200
+ fn_display_user_messages("Step4: Generating LLM response","Info", mv_processing_message)
201
+
202
+ lv_model = LlamaCpp(
203
+ model_path=lv_model_path,
204
+ temperature=0.00,
205
+ max_tokens=2048,
206
+ top_p=1,
207
+ n_ctx=2048,
208
+ verbose=False
209
+ )
210
+ lv_vector_search_result = lv_vector_store.similarity_search(mv_user_question, k=2)
211
+ # print("Vector Search Result - ")
212
+ # print(lv_vector_search_result)
213
+
214
+ # -- Creating formatted document result
215
+ lv_document_context = ""
216
+ lv_count = 0
217
+ for lv_result in lv_vector_search_result:
218
+ print("Concatenating Result of page - " + str(lv_count) + " with content of document page no - "+str(lv_result.metadata["page"]))
219
+ lv_document_context += lv_result.page_content
220
+ lv_count += 1
221
+
222
+ # print("Formatted Document Search Result - ")
223
+ # print(lv_document_context)
224
 
225
  lv_qa_formatted_prompt = lv_qa_prompt.format(
226
+ question=mv_user_question,
227
+ context=lv_document_context
228
+ )
229
+ print("Formatted Prompt - " + lv_qa_formatted_prompt)
230
+
231
+ lv_llm_response = lv_model(lv_qa_formatted_prompt)
232
+ # print("LLM Response" +lv_llm_response)
233
 
234
  print("Step5: LLM response generated")
235
  fn_display_user_messages("Step5: LLM response generated","Info", mv_processing_message)
236
 
237
  return lv_llm_response
238
 
239
+ # Function return API based QA Response using Vector Store
240
+ def fn_generate_API_QnA_response(mv_selected_model, mv_user_question, lv_vector_store, mv_processing_message):
241
+ """Returns QA Response using Vector Store"""
242
+
243
+ lv_template = """Instruction:
244
+ You are an AI assistant for answering questions about the provided context.
245
+ You are given the following extracted parts of a long document and a question. Provide a detailed answer.
246
+ If you don't know the answer, just say "Hmm, I'm not sure." Don't try to make up an answer.
247
+ =======
248
+ {context}
249
+ =======
250
+ Question: {question}
251
+ Output:\n"""
252
+ lv_qa_prompt = PromptTemplate(
253
+ template=lv_template,
254
+ input_variables=["question", "context"]
255
+ )
256
+
257
+ lv_vector_search_result = lv_vector_store.similarity_search(mv_user_question, k=2)
258
+ # print("Vector Search Result - ")
259
+ # print(lv_vector_search_result)
260
+
261
+ # -- Creating formatted document result
262
+ lv_document_context = ""
263
+ lv_count = 0
264
+ for lv_result in lv_vector_search_result:
265
+ # print("Concatenating Result of page - " + str(lv_count) + " with content of document page no - "+str(lv_result.metadata["page"]))
266
+ lv_document_context += lv_result.page_content
267
+ lv_count += 1
268
 
269
+ print("Formatted Document Search Result - ")
270
+ print(lv_document_context)
271
+
272
+ lv_qa_formatted_prompt = lv_qa_prompt.format(
273
+ question=mv_user_question,
274
+ context=lv_document_context
275
+ )
276
+
277
+ if mv_selected_model == 'Google Gemini-pro':
278
+ lv_model = genai.GenerativeModel('gemini-pro')
279
+
280
+ print("Step4: Generating LLM response")
281
+ fn_display_user_messages("Step4: Generating LLM response","Info", mv_processing_message)
282
+
283
+ lv_llm_response = lv_model.generate_content(lv_qa_formatted_prompt).text
284
+
285
+ print("Step5: LLM response generated")
286
+ fn_display_user_messages("Step5: LLM response generated","Info", mv_processing_message)
287
+
288
+ return lv_llm_response
289
+
290
+ # Main Function
291
  def main():
292
+
293
  # -- Streamlit Settings
294
+ st.set_page_config(layout='wide')
295
+ col1, col2, col3 = st.columns(3)
296
+ col2.title("Chat with PDF")
297
  st.text("")
298
+
299
+ # -- Initialize chat history
300
+ if "messages" not in st.session_state:
301
+ st.session_state.messages = []
302
+
303
+ # -- Display Supported Models
304
+ col1, col2, col3 = st.columns(3)
305
+ mv_selected_model = col3.selectbox('Select Model',
306
+ [
307
+ 'microsoft/phi-2',
308
+ 'google/gemma-2b',
309
+ 'mistralai/Mistral-7B-Instruct-v0.2',
310
+ 'Google Gemini-pro'
311
+ ]
312
+ )
313
+
314
+ # -- Display Supported Vector Stores
315
+ col1, col2, col3 = st.columns(3)
316
+ mv_selected_vector_db = col3.selectbox('Select Vector DB', ['FAISS'])
317
  st.text("")
318
 
319
+ # -- Reading PDF File
320
+ col1, col2, col3 = st.columns(3)
321
+ mv_pdf_input_file = col2.file_uploader("Choose a PDF file:", type=["pdf"])
322
+
323
  # -- Display Processing Details
 
324
  st.text("")
325
+ col1, col2, col3 = st.columns(3)
326
+ mv_processing_message = col2.empty()
327
  st.text("")
328
 
329
+ # -- Downloading Model Files
330
+ if mv_selected_model != "Google Gemini-pro":
331
+ fn_download_llm_models(mv_selected_model, mv_processing_message)
332
+ else:
333
+ print("Call Google API")
334
+
335
+ # -- Processing PDF
336
+ if (mv_pdf_input_file is not None):
337
+
338
+ # -- Upload PDF
339
+ fn_upload_pdf(mv_pdf_input_file, mv_processing_message)
340
+
341
+ # -- Create Vector Index
342
+ lv_vector_store = fn_create_vector_db(mv_pdf_input_file, mv_processing_message)
343
 
344
+ # -- Perform RAG
345
+ col1, col2, col3 = st.columns(3)
 
346
  st.text("")
347
+ lv_chat_history = col2.chat_message
348
  st.text("")
349
+
350
+ if mv_user_question := col2.chat_input("Chat on PDF Data"):
351
+ # -- Add user message to chat history
352
+ st.session_state.messages.append({"role": "user", "content": mv_user_question})
353
+
354
+ # -- Generating LLM response
355
+ if mv_selected_model != "Google Gemini-pro":
356
+ lv_response = fn_generate_QnA_response(mv_selected_model, mv_user_question, lv_vector_store, mv_processing_message)
357
+ else:
358
+ lv_response = fn_generate_API_QnA_response(mv_selected_model, mv_user_question, lv_vector_store, mv_processing_message)
359
+
360
+ # -- Adding assistant response to chat history
361
+ st.session_state.messages.append({"role": "assistant", "content": lv_response})
362
 
363
+ # -- Display chat messages from history on app rerun
364
+ for message in st.session_state.messages:
365
+ with lv_chat_history(message["role"]):
366
+ st.markdown(message["content"])
367
+
368
+ # -- Validate Data
369
+
370
+ # -- Get Web Response
371
+
372
+ # Calling Main Function
373
+ if __name__ == '__main__':
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
374
  main()