Syed Junaid Iqbal commited on
Commit
cfb7816
β€’
1 Parent(s): 2076fcc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -19
app.py CHANGED
@@ -1,24 +1,34 @@
1
  import subprocess
2
 
3
-
4
  import streamlit as st
5
  from dotenv import load_dotenv
6
  from langchain.text_splitter import RecursiveCharacterTextSplitter
7
  from langchain.vectorstores import Chroma
8
  from langchain.embeddings import FastEmbedEmbeddings # General embeddings from HuggingFace models.
9
  from langchain.memory import ConversationBufferMemory
10
- from langchain.chains import ConversationalRetrievalChain
 
11
  from htmlTemplates import css, bot_template, user_template
12
  from langchain.llms import LlamaCpp # For loading transformer models.
13
  from langchain.document_loaders import PyPDFLoader, TextLoader, JSONLoader, CSVLoader
14
  import tempfile
15
  from langchain.chains import RetrievalQA
16
  from langchain.prompts import PromptTemplate
 
17
  import os
18
  import glob
19
 
20
 
 
21
  def get_pdf_text(pdf_docs):
 
 
 
 
 
 
 
 
22
  temp_dir = tempfile.TemporaryDirectory()
23
  temp_filepath = os.path.join(temp_dir.name, pdf_docs.name)
24
 
@@ -31,6 +41,8 @@ def get_pdf_text(pdf_docs):
31
 
32
 
33
  def get_text_file(text_docs):
 
 
34
  temp_dir = tempfile.TemporaryDirectory()
35
  temp_filepath = os.path.join(temp_dir.name, text_docs.name)
36
 
@@ -70,8 +82,8 @@ def get_json_file(json_docs):
70
 
71
  def get_text_chunks(documents):
72
  text_splitter = RecursiveCharacterTextSplitter(
73
- chunk_size=1000,
74
- chunk_overlap=200,
75
  length_function=len
76
  )
77
 
@@ -81,25 +93,24 @@ def get_text_chunks(documents):
81
 
82
 
83
  def get_vectorstore(text_chunks, embeddings):
84
- # embeddings = FastEmbedEmbeddings( model_name= "BAAI/bge-small-en-v1.5",
85
- # cache_dir="./embedding_model/")
86
-
87
  vectorstore = Chroma.from_documents(documents= text_chunks,
88
- embedding= embeddings,
89
  persist_directory= "./vectordb/")
 
90
  return vectorstore
91
 
92
  def get_conversation_chain(vectorstore):
93
- # model_name_or_path = 'TheBloke/Llama-2-7B-chat-GGUF'
94
- # model_basename = 'llama-2-7b-chat.Q2_K.gguf'
95
- model_path = "./models/llama-2-13b-chat.Q4_K_S.gguf"
96
 
97
- llm = LlamaCpp(model_path="./models/llama-2-13b-chat.Q4_K_S.gguf",
98
- template = 0.4,
 
 
99
  n_ctx=4000,
100
- max_tokens=4000,
101
  n_gpu_layers = 50,
102
  n_batch = 512,
 
103
  verbose=True)
104
 
105
  memory = ConversationBufferMemory(
@@ -110,7 +121,8 @@ def get_conversation_chain(vectorstore):
110
  You are a Experience human Resource Manager. When the employee asks you a question, you will have to refer the company policy and respond in a professional way. Make sure to sound Empethetic while being professional and sound like a Human!
111
  Try to summarise the content and keep the answer to the point.
112
  If you don't know the answer, just say that you don't know, don't try to make up an answer.
113
- When generating answer for the given question make sure to follow the example template!
 
114
  Example:
115
  Question : how many paid leaves do i have ?
116
  Answer : The number of paid leaves varies depending on the type of leave, like privilege leave you're entitled to a maximum of 21 days in a calendar year. Other leaves might have different entitlements. thanks for asking!
@@ -120,16 +132,22 @@ def get_conversation_chain(vectorstore):
120
 
121
  Question: {question}
122
  Answer:
 
 
123
  """
124
 
125
  rag_prompt_custom = PromptTemplate.from_template(template)
 
 
126
 
127
  conversation_chain = RetrievalQA.from_chain_type(
128
  llm,
129
  retriever=vectorstore.as_retriever(),
130
  chain_type_kwargs={"prompt": rag_prompt_custom},
131
- memory = memory
132
  )
 
 
 
133
  return conversation_chain
134
 
135
 
@@ -154,6 +172,7 @@ def handle_userinput():
154
  if clear:
155
  st.session_state.conversation.clean()
156
  msg = st.session_state.conversation.run(prompt)
 
157
  st.session_state.messages.append({"role": "assistant", "content": msg})
158
  st.chat_message("assistant").write(msg)
159
 
@@ -182,7 +201,8 @@ def main():
182
  st.subheader("πŸš€ A HR powered by Generative AI")
183
  # user_question = st.text_input("Ask a question about your documents:")
184
 
185
- st.session_state.embeddings = embeddings = FastEmbedEmbeddings( model_name= "BAAI/bge-small-en-v1.5", cache_dir="./embedding_model/")
 
186
 
187
  if len(glob.glob("./vectordb/*.sqlite3")) > 0:
188
 
@@ -227,7 +247,6 @@ def main():
227
 
228
 
229
  if __name__ == '__main__':
230
- # Define the command
231
  command = 'CMAKE_ARGS="-DLLAMA_CUBLAS=on" FORCE_CMAKE=1 pip install llama-cpp-python --no-cache-dir'
232
 
233
  # Run the command using subprocess
@@ -237,4 +256,5 @@ if __name__ == '__main__':
237
  except subprocess.CalledProcessError as e:
238
  print(f"Error: {e}")
239
 
240
- main()
 
 
1
  import subprocess
2
 
 
3
  import streamlit as st
4
  from dotenv import load_dotenv
5
  from langchain.text_splitter import RecursiveCharacterTextSplitter
6
  from langchain.vectorstores import Chroma
7
  from langchain.embeddings import FastEmbedEmbeddings # General embeddings from HuggingFace models.
8
  from langchain.memory import ConversationBufferMemory
9
+ from langchain.callbacks.manager import CallbackManager
10
+ from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
11
  from htmlTemplates import css, bot_template, user_template
12
  from langchain.llms import LlamaCpp # For loading transformer models.
13
  from langchain.document_loaders import PyPDFLoader, TextLoader, JSONLoader, CSVLoader
14
  import tempfile
15
  from langchain.chains import RetrievalQA
16
  from langchain.prompts import PromptTemplate
17
+ from langchain import hub
18
  import os
19
  import glob
20
 
21
 
22
+ # TEXT LOADERS
23
  def get_pdf_text(pdf_docs):
24
+ """
25
+ Purpose: A hypothetical loader for PDF files in Python.
26
+ Usage: Used to extract text or other information from PDF documents.
27
+ Load Function: A load_pdf function might be used to read and extract data from a PDF file.
28
+
29
+ input : pdf document path
30
+ returns : extracted text
31
+ """
32
  temp_dir = tempfile.TemporaryDirectory()
33
  temp_filepath = os.path.join(temp_dir.name, pdf_docs.name)
34
 
 
41
 
42
 
43
  def get_text_file(text_docs):
44
+ """
45
+ """
46
  temp_dir = tempfile.TemporaryDirectory()
47
  temp_filepath = os.path.join(temp_dir.name, text_docs.name)
48
 
 
82
 
83
  def get_text_chunks(documents):
84
  text_splitter = RecursiveCharacterTextSplitter(
85
+ chunk_size=512,
86
+ chunk_overlap=50,
87
  length_function=len
88
  )
89
 
 
93
 
94
 
95
  def get_vectorstore(text_chunks, embeddings):
96
+
 
 
97
  vectorstore = Chroma.from_documents(documents= text_chunks,
98
+ embedding= st.session_state.embeddings,
99
  persist_directory= "./vectordb/")
100
+ # Document stored
101
  return vectorstore
102
 
103
  def get_conversation_chain(vectorstore):
 
 
 
104
 
105
+ model_path = "models/llama-2-13b-chat.Q4_K_S.gguf"
106
+ callback_manager = CallbackManager([StreamingStdOutCallbackHandler()])
107
+
108
+ llm = LlamaCpp(model_path= model_path,
109
  n_ctx=4000,
110
+ max_tokens= 500,
111
  n_gpu_layers = 50,
112
  n_batch = 512,
113
+ callback_manager = callback_manager
114
  verbose=True)
115
 
116
  memory = ConversationBufferMemory(
 
121
  You are a Experience human Resource Manager. When the employee asks you a question, you will have to refer the company policy and respond in a professional way. Make sure to sound Empethetic while being professional and sound like a Human!
122
  Try to summarise the content and keep the answer to the point.
123
  If you don't know the answer, just say that you don't know, don't try to make up an answer.
124
+
125
+ Followe the template below
126
  Example:
127
  Question : how many paid leaves do i have ?
128
  Answer : The number of paid leaves varies depending on the type of leave, like privilege leave you're entitled to a maximum of 21 days in a calendar year. Other leaves might have different entitlements. thanks for asking!
 
132
 
133
  Question: {question}
134
  Answer:
135
+
136
+ Just answer to the point!
137
  """
138
 
139
  rag_prompt_custom = PromptTemplate.from_template(template)
140
+
141
+ # prompt = hub.pull("rlm/rag-prompt")
142
 
143
  conversation_chain = RetrievalQA.from_chain_type(
144
  llm,
145
  retriever=vectorstore.as_retriever(),
146
  chain_type_kwargs={"prompt": rag_prompt_custom},
 
147
  )
148
+ conversation_chain.callback_manager = callback_manager
149
+ conversation_chain.memory = ConversationBufferMemory()
150
+
151
  return conversation_chain
152
 
153
 
 
172
  if clear:
173
  st.session_state.conversation.clean()
174
  msg = st.session_state.conversation.run(prompt)
175
+ print(msg)
176
  st.session_state.messages.append({"role": "assistant", "content": msg})
177
  st.chat_message("assistant").write(msg)
178
 
 
201
  st.subheader("πŸš€ A HR powered by Generative AI")
202
  # user_question = st.text_input("Ask a question about your documents:")
203
 
204
+ st.session_state.embeddings = FastEmbedEmbeddings( model_name= "BAAI/bge-small-en-v1.5",
205
+ cache_dir="./embedding_model/")
206
 
207
  if len(glob.glob("./vectordb/*.sqlite3")) > 0:
208
 
 
247
 
248
 
249
  if __name__ == '__main__':
 
250
  command = 'CMAKE_ARGS="-DLLAMA_CUBLAS=on" FORCE_CMAKE=1 pip install llama-cpp-python --no-cache-dir'
251
 
252
  # Run the command using subprocess
 
256
  except subprocess.CalledProcessError as e:
257
  print(f"Error: {e}")
258
 
259
+ main()
260
+