barghavani commited on
Commit
6fabaae
·
verified ·
1 Parent(s): 24555e8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +118 -60
app.py CHANGED
@@ -1,20 +1,15 @@
1
  import gradio as gr
2
  import os
3
 
4
- from langchain.document_loaders import PyPDFLoader
5
  from langchain.text_splitter import RecursiveCharacterTextSplitter
6
- from langchain.vectorstores import Chroma
7
- from langchain.chains import ChatVectorDBChain
8
- from langchain.embeddings import OpenAIEmbeddings
9
  from langchain.chains import ConversationalRetrievalChain
10
  from langchain_community.embeddings import HuggingFaceEmbeddings
11
  from langchain_community.llms import HuggingFacePipeline
12
  from langchain.chains import ConversationChain
13
  from langchain.memory import ConversationBufferMemory
14
  from langchain_community.llms import HuggingFaceEndpoint
15
- from langchain_community.chat_models.openai import ChatOpenAI
16
- from langchain.chains import ConversationalRetrievalChain
17
- from langchain.llms import OpenAI
18
 
19
  from pathlib import Path
20
  import chromadb
@@ -26,12 +21,27 @@ import torch
26
  import tqdm
27
  import accelerate
28
  import re
 
 
 
 
 
 
 
 
 
 
29
 
30
 
 
31
  TOKEN = os.getenv('HUGGING_FACE_HUB_TOKEN')
32
 
33
 
34
-
 
 
 
 
35
  # Load PDF document and create doc splits
36
  def load_doc(list_file_path, chunk_size, chunk_overlap):
37
  # Processing for one document only
@@ -49,17 +59,20 @@ def load_doc(list_file_path, chunk_size, chunk_overlap):
49
  return doc_splits
50
 
51
 
52
- # Initialize your vector database with OpenAIEmbeddings and persist it
53
- def create_db(documents, collection_name, persist_directory="."):
54
- embeddings = OpenAIEmbeddings()
 
55
  vectordb = Chroma.from_documents(
56
- documents=documents,
57
- embedding=embeddings,
 
 
58
  collection_name=collection_name,
59
- persist_directory=persist_directory
60
  )
61
- vectordb.persist()
62
- return vectordb
63
 
64
  # Load vector database
65
  def load_db():
@@ -70,34 +83,74 @@ def load_db():
70
  return vectordb
71
 
72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
 
74
-
75
- # Assuming vectordb is correctly initialized and persisted as shown above
76
- def initialize_llmchain(vectordb, model_name="gpt-3.5-turbo", temperature=0.9):
77
- chat_model = ChatOpenAI(temperature=temperature, model_name=model_name)
78
- chat_vector_db_chain = ChatVectorDBChain.from_llm(
79
- llm=chat_model,
80
- vectordb=vectordb,
81
- return_source_documents=True
 
 
 
 
 
 
 
82
  )
83
- return chat_vector_db_chain
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
 
85
- # Example usage
86
- if __name__ == "__main__":
87
- # Suppose `documents` is a list of your documents and `collection_name` is your desired collection name
88
- documents = [...] # your documents here
89
- collection_name = "your_collection_name"
90
- vectordb = create_db(documents, collection_name)
91
 
92
- # Initialize the ChatVectorDBChain with the vector database
93
- chat_vector_db_chain = initialize_chat_vector_db_chain(vectordb)
94
-
95
- # Use the chain to process a query
96
- query = "your query here"
97
- result = chat_vector_db_chain({"question": query, "chat_history": ""})
98
-
99
- print("Answer:")
100
- print(result["answer"])
101
  # Generate collection name for vector database
102
  # - Use filepath as input, ensuring unicode text
103
  def create_collection_name(filepath):
@@ -155,31 +208,36 @@ def format_chat_history(message, chat_history):
155
  return formatted_chat_history
156
 
157
 
158
- def conversation(qa_chain, message, history):
 
 
 
 
 
 
159
  formatted_chat_history = format_chat_history(message, history)
160
- #print("formatted_chat_history",formatted_chat_history)
161
-
162
  # Generate response using QA chain
163
- response = qa_chain({"question": message, "chat_history": formatted_chat_history})
164
- response_answer = response["answer"]
165
- if response_answer.find("Helpful Answer:") != -1:
166
  response_answer = response_answer.split("Helpful Answer:")[-1]
167
- response_sources = response["source_documents"]
168
- response_source1 = response_sources[0].page_content.strip()
169
- response_source2 = response_sources[1].page_content.strip()
170
- response_source3 = response_sources[2].page_content.strip()
171
- # Langchain sources are zero-based
172
- response_source1_page = response_sources[0].metadata["page"] + 1
173
- response_source2_page = response_sources[1].metadata["page"] + 1
174
- response_source3_page = response_sources[2].metadata["page"] + 1
175
- # print ('chat response: ', response_answer)
176
- # print('DB source', response_sources)
177
 
178
  # Append user message and response to chat history
179
  new_history = history + [(message, response_answer)]
180
- # return gr.update(value=""), new_history, response_sources[0], response_sources[1]
181
- return qa_chain, gr.update(value=""), new_history, response_source1, response_source1_page, response_source2, response_source2_page, response_source3, response_source3_page
182
 
 
 
183
 
184
  def upload_file(file_obj):
185
  list_file_path = []
@@ -190,7 +248,7 @@ def upload_file(file_obj):
190
  # initialize_database(file_path, progress)
191
  return list_file_path
192
 
193
-
194
  def demo():
195
  with gr.Blocks(theme="base") as demo:
196
  vector_db = gr.State()
@@ -201,7 +259,6 @@ def demo():
201
  """PDF-based chatbot (by Dr. Aloke Upadhaya)</center></h2>
202
  <h3>Ask any questions about your PDF documents, along with follow-ups</h3>
203
  """)
204
-
205
  with gr.Tab("Step 1 - Document pre-processing"):
206
  with gr.Row():
207
  document = gr.Files(height=100, file_count="multiple", file_types=["pdf"], interactive=True, label="Upload your PDF documents (single or multiple)")
@@ -282,3 +339,4 @@ def demo():
282
 
283
  if __name__ == "__main__":
284
  demo()
 
 
1
  import gradio as gr
2
  import os
3
 
4
+ from langchain_community.document_loaders import PyPDFLoader
5
  from langchain.text_splitter import RecursiveCharacterTextSplitter
6
+ from langchain_community.vectorstores import Chroma
 
 
7
  from langchain.chains import ConversationalRetrievalChain
8
  from langchain_community.embeddings import HuggingFaceEmbeddings
9
  from langchain_community.llms import HuggingFacePipeline
10
  from langchain.chains import ConversationChain
11
  from langchain.memory import ConversationBufferMemory
12
  from langchain_community.llms import HuggingFaceEndpoint
 
 
 
13
 
14
  from pathlib import Path
15
  import chromadb
 
21
  import tqdm
22
  import accelerate
23
  import re
24
+ from openai import OpenAI
25
+ from huggingface_hub import HfApi
26
+ from huggingface_hub import hf_hub_download
27
+
28
+ from langchain.document_loaders import PyPDFLoader
29
+ from langchain.embeddings import OpenAIEmbeddings
30
+ from langchain.chains import ChatVectorDBChain
31
+ from langchain.vectorstores import Chroma
32
+ from langchain.llms import OpenAI
33
+ import os
34
 
35
 
36
+ os.environ["OPENAI_API_KEY"]
37
  TOKEN = os.getenv('HUGGING_FACE_HUB_TOKEN')
38
 
39
 
40
+
41
+ # default_persist_directory = './chroma_HF/'
42
+ list_llm = ["jondurbin/airoboros-gpt-3.5-turbo-100k-7b"]
43
+ list_llm_simple = [os.path.basename(llm) for llm in list_llm]
44
+
45
  # Load PDF document and create doc splits
46
  def load_doc(list_file_path, chunk_size, chunk_overlap):
47
  # Processing for one document only
 
59
  return doc_splits
60
 
61
 
62
+ # Create vector database
63
+ def create_db(splits, collection_name):
64
+ embedding = OpenAIEmbeddings()
65
+ new_client = chromadb.EphemeralClient()
66
  vectordb = Chroma.from_documents(
67
+ documents=splits,
68
+ embedding=embedding,
69
+ persist_directory=".",
70
+ client=new_client,
71
  collection_name=collection_name,
72
+ # persist_directory=default_persist_directory
73
  )
74
+ return vectordb.persist()
75
+
76
 
77
  # Load vector database
78
  def load_db():
 
83
  return vectordb
84
 
85
 
86
+ # Initialize langchain LLM chain
87
+ def initialize_llmchain(llm_model, temperature, max_tokens, top_k, vector_db, progress=gr.Progress()):
88
+ progress(0.1, desc="Initializing HF tokenizer...")
89
+ # HuggingFacePipeline uses local model
90
+ # Note: it will download model locally...
91
+ # tokenizer=AutoTokenizer.from_pretrained(llm_model)
92
+ # progress(0.5, desc="Initializing HF pipeline...")
93
+ # pipeline=transformers.pipeline(
94
+ # "text-generation",
95
+ # model=llm_model,
96
+ # tokenizer=tokenizer,
97
+ # torch_dtype=torch.bfloat16,
98
+ # trust_remote_code=True,
99
+ # device_map="auto",
100
+ # # max_length=1024,
101
+ # max_new_tokens=max_tokens,
102
+ # do_sample=True,
103
+ # top_k=top_k,
104
+ # num_return_sequences=1,
105
+ # eos_token_id=tokenizer.eos_token_id
106
+ # )
107
+ # llm = HuggingFacePipeline(pipeline=pipeline, model_kwargs={'temperature': temperature})
108
+
109
+ # HuggingFaceHub uses HF inference endpoints
110
+ progress(0.5, desc="Initializing HF Hub...")
111
+ if llm_model == "jondurbin/airoboros-gpt-3.5-turbo-100k-7b":
112
+ llm = HuggingFaceEndpoint(
113
+ repo_id=llm_model,
114
+ # model_kwargs={"temperature": temperature, "max_new_tokens": max_tokens, "top_k": top_k, "load_in_8bit": True}
115
+ temperature = temperature,
116
+ max_new_tokens = max_tokens,
117
+ top_k = top_k,
118
+ load_in_8bit = True,
119
+ )
120
 
121
+ else:
122
+ llm = HuggingFaceEndpoint(
123
+ repo_id=llm_model,
124
+ # model_kwargs={"temperature": temperature, "max_new_tokens": max_tokens, "top_k": top_k, "trust_remote_code": True, "torch_dtype": "auto"}
125
+ # model_kwargs={"temperature": temperature, "max_new_tokens": max_tokens, "top_k": top_k}
126
+ temperature = temperature,
127
+ max_new_tokens = max_tokens,
128
+ top_k = top_k,
129
+ )
130
+
131
+ progress(0.75, desc="Defining buffer memory...")
132
+ memory = ConversationBufferMemory(
133
+ memory_key="chat_history",
134
+ output_key='answer',
135
+ return_messages=True
136
  )
137
+ # retriever=vector_db.as_retriever(search_type="similarity", search_kwargs={'k': 3})
138
+ retriever=vector_db.as_retriever()
139
+ progress(0.8, desc="Defining retrieval chain...")
140
+ qa_chain = ConversationalRetrievalChain.from_llm(
141
+ llm,
142
+ retriever=retriever,
143
+ chain_type="stuff",
144
+ memory=memory,
145
+ # combine_docs_chain_kwargs={"prompt": your_prompt})
146
+ return_source_documents=True,
147
+ #return_generated_question=False,
148
+ verbose=False,
149
+ )
150
+ progress(0.9, desc="Done!")
151
+ return qa_chain
152
 
 
 
 
 
 
 
153
 
 
 
 
 
 
 
 
 
 
154
  # Generate collection name for vector database
155
  # - Use filepath as input, ensuring unicode text
156
  def create_collection_name(filepath):
 
208
  return formatted_chat_history
209
 
210
 
211
+ def conversation(vector_db, message, history):
212
+ # Initialize ChatOpenAI with your desired settings
213
+ chat_open_ai = ChatOpenAI(temperature=0.9, model_name="gpt-3.5-turbo")
214
+ # Assuming vector_db is already initialized correctly
215
+ pdf_ga = ChatVectorDBChain.from_llm(chat_open_ai, vector_db, return_source_documents=True)
216
+
217
+ # Format the chat history for input to the model
218
  formatted_chat_history = format_chat_history(message, history)
219
+
 
220
  # Generate response using QA chain
221
+ result = pdf_ga({"question": message, "chat_history": formatted_chat_history})
222
+ response_answer = result["answer"]
223
+ if "Helpful Answer:" in response_answer:
224
  response_answer = response_answer.split("Helpful Answer:")[-1]
225
+
226
+ response_sources = result["source_documents"]
227
+ response_source1 = response_sources[0].page_content.strip() if len(response_sources) > 0 else "No source available"
228
+ response_source2 = response_sources[1].page_content.strip() if len(response_sources) > 1 else "No source available"
229
+ response_source3 = response_sources[2].page_content.strip() if len(response_sources) > 2 else "No source available"
230
+
231
+ # Langchain sources are zero-based; adjust pages if sources are available
232
+ response_source1_page = response_sources[0].metadata["page"] + 1 if len(response_sources) > 0 else "N/A"
233
+ response_source2_page = response_sources[1].metadata["page"] + 1 if len(response_sources) > 1 else "N/A"
234
+ response_source3_page = response_sources[2].metadata["page"] + 1 if len(response_sources) > 2 else "N/A"
235
 
236
  # Append user message and response to chat history
237
  new_history = history + [(message, response_answer)]
 
 
238
 
239
+ return gr.update(value=""), new_history, response_source1, response_source1_page, response_source2, response_source2_page, response_source3, response_source3_page
240
+
241
 
242
  def upload_file(file_obj):
243
  list_file_path = []
 
248
  # initialize_database(file_path, progress)
249
  return list_file_path
250
 
251
+
252
  def demo():
253
  with gr.Blocks(theme="base") as demo:
254
  vector_db = gr.State()
 
259
  """PDF-based chatbot (by Dr. Aloke Upadhaya)</center></h2>
260
  <h3>Ask any questions about your PDF documents, along with follow-ups</h3>
261
  """)
 
262
  with gr.Tab("Step 1 - Document pre-processing"):
263
  with gr.Row():
264
  document = gr.Files(height=100, file_count="multiple", file_types=["pdf"], interactive=True, label="Upload your PDF documents (single or multiple)")
 
339
 
340
  if __name__ == "__main__":
341
  demo()
342
+