himel06 commited on
Commit
2a7d287
·
verified ·
1 Parent(s): 5036a24

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +391 -59
app.py CHANGED
@@ -1,63 +1,395 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
-
9
-
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
-
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
-
26
- messages.append({"role": "user", "content": message})
27
-
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
- """
43
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
44
- """
45
- demo = gr.ChatInterface(
46
- respond,
47
- additional_inputs=[
48
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
49
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
50
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
51
- gr.Slider(
52
- minimum=0.1,
53
- maximum=1.0,
54
- value=0.95,
55
- step=0.05,
56
- label="Top-p (nucleus sampling)",
57
- ),
58
- ],
59
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
 
61
 
62
  if __name__ == "__main__":
63
- demo.launch()
 
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_huggingface import HuggingFaceEmbeddings
10
+ from langchain_community.llms import HuggingFacePipeline
11
+ from langchain.chains import ConversationChain
12
+ from langchain.memory import ConversationBufferMemory
13
+ #from langchain_community.llms import HuggingFaceEndpoint
14
+ from langchain_huggingface import HuggingFaceEndpoint
15
+
16
+ from pathlib import Path
17
+ import chromadb
18
+ from unidecode import unidecode
19
+
20
+ from transformers import AutoTokenizer
21
+ import transformers
22
+ import torch
23
+ import tqdm
24
+ import accelerate
25
+ import re
26
+
27
+ # default_persist_directory = './chroma_HF/'
28
+ list_llm = ["mistralai/Mistral-7B-Instruct-v0.2", "mistralai/Mixtral-8x7B-Instruct-v0.1",
29
+ "mistralai/Mistral-7B-Instruct-v0.1", \
30
+ "google/gemma-7b-it", "google/gemma-2b-it", \
31
+ "HuggingFaceH4/zephyr-7b-beta", "HuggingFaceH4/zephyr-7b-gemma-v0.1", \
32
+ "meta-llama/Llama-2-7b-chat-hf", "microsoft/phi-2", \
33
+ "TinyLlama/TinyLlama-1.1B-Chat-v1.0", "mosaicml/mpt-7b-instruct", "tiiuae/falcon-7b-instruct", \
34
+ "google/flan-t5-xxl"
35
+ ]
36
+ list_llm_simple = [os.path.basename(llm) for llm in list_llm]
37
+
38
+
39
+ # Load PDF document and create doc splits
40
+ def load_doc(list_file_path, chunk_size, chunk_overlap):
41
+ # Processing for one document only
42
+ # loader = PyPDFLoader(file_path)
43
+ # pages = loader.load()
44
+ loaders = [PyPDFLoader(x) for x in list_file_path]
45
+ pages = []
46
+ for loader in loaders:
47
+ pages.extend(loader.load())
48
+ # text_splitter = RecursiveCharacterTextSplitter(chunk_size = 600, chunk_overlap = 50)
49
+ text_splitter = RecursiveCharacterTextSplitter(
50
+ chunk_size=chunk_size,
51
+ chunk_overlap=chunk_overlap)
52
+ doc_splits = text_splitter.split_documents(pages)
53
+ return doc_splits
54
+
55
+
56
+ # Create vector database
57
+ def create_db(splits, collection_name):
58
+ embedding = HuggingFaceEmbeddings()
59
+ #new_client = chromadb.EphemeralClient()
60
+ new_client = chromadb.PersistentClient()
61
+ vectordb = Chroma.from_documents(
62
+ documents=splits,
63
+ embedding=embedding,
64
+ client=new_client,
65
+ collection_name=collection_name,
66
+ # persist_directory=default_persist_directory
67
+ )
68
+ return vectordb
69
+
70
+
71
+ # Load vector database
72
+ def load_db():
73
+ embedding = HuggingFaceEmbeddings()
74
+ vectordb = Chroma(
75
+ # persist_directory=default_persist_directory,
76
+ embedding_function=embedding)
77
+ return vectordb
78
+
79
+
80
+ # Initialize langchain LLM chain
81
+ def initialize_llmchain(llm_model, temperature, max_tokens, top_k, vector_db, progress=gr.Progress()):
82
+ progress(0.1, desc="Initializing HF tokenizer...")
83
+ # HuggingFacePipeline uses local model
84
+ # Note: it will download model locally...
85
+ # tokenizer=AutoTokenizer.from_pretrained(llm_model)
86
+ # progress(0.5, desc="Initializing HF pipeline...")
87
+ # pipeline=transformers.pipeline(
88
+ # "text-generation",
89
+ # model=llm_model,
90
+ # tokenizer=tokenizer,
91
+ # torch_dtype=torch.bfloat16,
92
+ # trust_remote_code=True,
93
+ # device_map="auto",
94
+ # # max_length=1024,
95
+ # max_new_tokens=max_tokens,
96
+ # do_sample=True,
97
+ # top_k=top_k,
98
+ # num_return_sequences=1,
99
+ # eos_token_id=tokenizer.eos_token_id
100
+ # )
101
+ # llm = HuggingFacePipeline(pipeline=pipeline, model_kwargs={'temperature': temperature})
102
+
103
+ # HuggingFaceHub uses HF inference endpoints
104
+ progress(0.5, desc="Initializing HF Hub...")
105
+ # Use of trust_remote_code as model_kwargs
106
+ # Warning: langchain issue
107
+ # URL: https://github.com/langchain-ai/langchain/issues/6080
108
+ if llm_model == "mistralai/Mixtral-8x7B-Instruct-v0.1":
109
+ llm = HuggingFaceEndpoint(
110
+ repo_id=llm_model,
111
+ # model_kwargs={"temperature": temperature, "max_new_tokens": max_tokens, "top_k": top_k, "load_in_8bit": True}
112
+ temperature=temperature,
113
+ max_new_tokens=max_tokens,
114
+ top_k=top_k,
115
+ load_in_8bit=True,
116
+ )
117
+ elif llm_model in ["HuggingFaceH4/zephyr-7b-gemma-v0.1", "mosaicml/mpt-7b-instruct"]:
118
+ raise gr.Error("LLM model is too large to be loaded automatically on free inference endpoint")
119
+ llm = HuggingFaceEndpoint(
120
+ repo_id=llm_model,
121
+ temperature=temperature,
122
+ max_new_tokens=max_tokens,
123
+ top_k=top_k,
124
+ )
125
+ elif llm_model == "microsoft/phi-2":
126
+ # raise gr.Error("phi-2 model requires 'trust_remote_code=True', currently not supported by langchain HuggingFaceHub...")
127
+ llm = HuggingFaceEndpoint(
128
+ repo_id=llm_model,
129
+ # model_kwargs={"temperature": temperature, "max_new_tokens": max_tokens, "top_k": top_k, "trust_remote_code": True, "torch_dtype": "auto"}
130
+ temperature=temperature,
131
+ max_new_tokens=max_tokens,
132
+ top_k=top_k,
133
+ trust_remote_code=True,
134
+ torch_dtype="auto",
135
+ )
136
+ elif llm_model == "TinyLlama/TinyLlama-1.1B-Chat-v1.0":
137
+ llm = HuggingFaceEndpoint(
138
+ repo_id=llm_model,
139
+ # model_kwargs={"temperature": temperature, "max_new_tokens": 250, "top_k": top_k}
140
+ temperature=temperature,
141
+ max_new_tokens=250,
142
+ top_k=top_k,
143
+ )
144
+ elif llm_model == "meta-llama/Llama-2-7b-chat-hf":
145
+ raise gr.Error("Llama-2-7b-chat-hf model requires a Pro subscription...")
146
+ llm = HuggingFaceEndpoint(
147
+ repo_id=llm_model,
148
+ # model_kwargs={"temperature": temperature, "max_new_tokens": max_tokens, "top_k": top_k}
149
+ temperature=temperature,
150
+ max_new_tokens=max_tokens,
151
+ top_k=top_k,
152
+ )
153
+ else:
154
+ llm = HuggingFaceEndpoint(
155
+ repo_id=llm_model,
156
+ # model_kwargs={"temperature": temperature, "max_new_tokens": max_tokens, "top_k": top_k, "trust_remote_code": True, "torch_dtype": "auto"}
157
+ # model_kwargs={"temperature": temperature, "max_new_tokens": max_tokens, "top_k": top_k}
158
+ temperature=temperature,
159
+ max_new_tokens=max_tokens,
160
+ top_k=top_k,
161
+ )
162
+
163
+ progress(0.75, desc="Defining buffer memory...")
164
+ memory = ConversationBufferMemory(
165
+ memory_key="chat_history",
166
+ output_key='answer',
167
+ return_messages=True
168
+ )
169
+ # retriever=vector_db.as_retriever(search_type="similarity", search_kwargs={'k': 3})
170
+ retriever = vector_db.as_retriever()
171
+ progress(0.8, desc="Defining retrieval chain...")
172
+ qa_chain = ConversationalRetrievalChain.from_llm(
173
+ llm,
174
+ retriever=retriever,
175
+ chain_type="stuff",
176
+ memory=memory,
177
+ # combine_docs_chain_kwargs={"prompt": your_prompt})
178
+ return_source_documents=True,
179
+ # return_generated_question=False,
180
+ verbose=False,
181
+ )
182
+ progress(0.9, desc="Done!")
183
+ return qa_chain
184
+
185
+
186
+ # Generate collection name for vector database
187
+ # - Use filepath as input, ensuring unicode text
188
+ def create_collection_name(filepath):
189
+ # Extract filename without extension
190
+ collection_name = Path(filepath).stem
191
+ # Fix potential issues from naming convention
192
+ ## Remove space
193
+ collection_name = collection_name.replace(" ", "-")
194
+ ## ASCII transliterations of Unicode text
195
+ collection_name = unidecode(collection_name)
196
+ ## Remove special characters
197
+ # collection_name = re.findall("[\dA-Za-z]*", collection_name)[0]
198
+ collection_name = re.sub('[^A-Za-z0-9]+', '-', collection_name)
199
+ ## Limit length to 50 characters
200
+ collection_name = collection_name[:50]
201
+ ## Minimum length of 3 characters
202
+ if len(collection_name) < 3:
203
+ collection_name = collection_name + 'xyz'
204
+ ## Enforce start and end as alphanumeric character
205
+ if not collection_name[0].isalnum():
206
+ collection_name = 'A' + collection_name[1:]
207
+ if not collection_name[-1].isalnum():
208
+ collection_name = collection_name[:-1] + 'Z'
209
+ print('Filepath: ', filepath)
210
+ print('Collection name: ', collection_name)
211
+ return collection_name
212
+
213
+
214
+ # Initialize database
215
+ def initialize_database(list_file_obj, chunk_size, chunk_overlap, progress=gr.Progress()):
216
+ # Create list of documents (when valid)
217
+ list_file_path = [x.name for x in list_file_obj if x is not None]
218
+ # Create collection_name for vector database
219
+ progress(0.1, desc="Creating collection name...")
220
+ collection_name = create_collection_name(list_file_path[0])
221
+ progress(0.25, desc="Loading document...")
222
+ # Load document and create splits
223
+ doc_splits = load_doc(list_file_path, chunk_size, chunk_overlap)
224
+ # Create or load vector database
225
+ progress(0.5, desc="Generating vector database...")
226
+ # global vector_db
227
+ vector_db = create_db(doc_splits, collection_name)
228
+ progress(0.9, desc="Done!")
229
+ return vector_db, collection_name, "Complete!"
230
+
231
+
232
+ def initialize_LLM(llm_option, llm_temperature, max_tokens, top_k, vector_db, progress=gr.Progress()):
233
+ # print("llm_option",llm_option)
234
+ llm_name = list_llm[llm_option]
235
+ print("llm_name: ", llm_name)
236
+ qa_chain = initialize_llmchain(llm_name, llm_temperature, max_tokens, top_k, vector_db, progress)
237
+ return qa_chain, "Complete!"
238
+
239
+
240
+ def format_chat_history(message, chat_history):
241
+ formatted_chat_history = []
242
+ for user_message, bot_message in chat_history:
243
+ formatted_chat_history.append(f"User: {user_message}")
244
+ formatted_chat_history.append(f"Assistant: {bot_message}")
245
+ return formatted_chat_history
246
+
247
+
248
+ def conversation(qa_chain, message, history):
249
+ formatted_chat_history = format_chat_history(message, history)
250
+ # print("formatted_chat_history",formatted_chat_history)
251
+
252
+ # Generate response using QA chain
253
+ response = qa_chain({"question": message, "chat_history": formatted_chat_history})
254
+ response_answer = response["answer"]
255
+ if response_answer.find("Helpful Answer:") != -1:
256
+ response_answer = response_answer.split("Helpful Answer:")[-1]
257
+ response_sources = response["source_documents"]
258
+ response_source1 = response_sources[0].page_content.strip()
259
+ response_source2 = response_sources[1].page_content.strip()
260
+ response_source3 = response_sources[2].page_content.strip()
261
+ # Langchain sources are zero-based
262
+ response_source1_page = response_sources[0].metadata["page"] + 1
263
+ response_source2_page = response_sources[1].metadata["page"] + 1
264
+ response_source3_page = response_sources[2].metadata["page"] + 1
265
+ # print ('chat response: ', response_answer)
266
+ # print('DB source', response_sources)
267
+
268
+ # Append user message and response to chat history
269
+ new_history = history + [(message, response_answer)]
270
+ # return gr.update(value=""), new_history, response_sources[0], response_sources[1]
271
+ return qa_chain, gr.update(
272
+ value=""), new_history, response_source1, response_source1_page, response_source2, response_source2_page, response_source3, response_source3_page
273
+
274
+
275
+ def upload_file(file_obj):
276
+ list_file_path = []
277
+ for idx, file in enumerate(file_obj):
278
+ file_path = file_obj.name
279
+ list_file_path.append(file_path)
280
+ # print(file_path)
281
+ # initialize_database(file_path, progress)
282
+ return list_file_path
283
+
284
+
285
+ def demo():
286
+ with gr.Blocks(theme="base") as demo:
287
+ vector_db = gr.State()
288
+ qa_chain = gr.State()
289
+ collection_name = gr.State()
290
+
291
+ gr.Markdown(
292
+ """<center><h2>PDF-based chatbot</center></h2>
293
+ <h3>Ask any questions about your PDF documents</h3>""")
294
+ gr.Markdown(
295
+ """<b>Note:</b> This AI assistant, using Langchain and open-source LLMs, performs retrieval-augmented generation (RAG) from your PDF documents. \
296
+ The user interface explicitely shows multiple steps to help understand the RAG workflow.
297
+ This chatbot takes past questions into account when generating answers (via conversational memory), and includes document references for clarity purposes.<br>
298
+ <br><b>Warning:</b> This space uses the free CPU Basic hardware from Hugging Face. Some steps and LLM models used below (free inference endpoints) can take some time to generate a reply.
299
+ """)
300
+
301
+ with gr.Tab("Step 1 - Upload PDF"):
302
+ with gr.Row():
303
+ document = gr.Files(height=100, file_count="multiple", file_types=["pdf"], interactive=True,
304
+ label="Upload your PDF documents (single or multiple)")
305
+ # upload_btn = gr.UploadButton("Loading document...", height=100, file_count="multiple", file_types=["pdf"], scale=1)
306
+
307
+ with gr.Tab("Step 2 - Process document"):
308
+ with gr.Row():
309
+ db_btn = gr.Radio(["ChromaDB"], label="Vector database type", value="ChromaDB", type="index",
310
+ info="Choose your vector database")
311
+ with gr.Accordion("Advanced options - Document text splitter", open=False):
312
+ with gr.Row():
313
+ slider_chunk_size = gr.Slider(minimum=100, maximum=1000, value=600, step=20, label="Chunk size",
314
+ info="Chunk size", interactive=True)
315
+ with gr.Row():
316
+ slider_chunk_overlap = gr.Slider(minimum=10, maximum=200, value=40, step=10, label="Chunk overlap",
317
+ info="Chunk overlap", interactive=True)
318
+ with gr.Row():
319
+ db_progress = gr.Textbox(label="Vector database initialization", value="None")
320
+ with gr.Row():
321
+ db_btn = gr.Button("Generate vector database")
322
+
323
+ with gr.Tab("Step 3 - Initialize QA chain"):
324
+ with gr.Row():
325
+ llm_btn = gr.Radio(list_llm_simple, \
326
+ label="LLM models", value=list_llm_simple[0], type="index",
327
+ info="Choose your LLM model")
328
+ with gr.Accordion("Advanced options - LLM model", open=False):
329
+ with gr.Row():
330
+ slider_temperature = gr.Slider(minimum=0.01, maximum=1.0, value=0.7, step=0.1, label="Temperature",
331
+ info="Model temperature", interactive=True)
332
+ with gr.Row():
333
+ slider_maxtokens = gr.Slider(minimum=224, maximum=4096, value=1024, step=32, label="Max Tokens",
334
+ info="Model max tokens", interactive=True)
335
+ with gr.Row():
336
+ slider_topk = gr.Slider(minimum=1, maximum=10, value=3, step=1, label="top-k samples",
337
+ info="Model top-k samples", interactive=True)
338
+ with gr.Row():
339
+ llm_progress = gr.Textbox(value="None", label="QA chain initialization")
340
+ with gr.Row():
341
+ qachain_btn = gr.Button("Initialize Question Answering chain")
342
+
343
+ with gr.Tab("Step 4 - Chatbot"):
344
+ chatbot = gr.Chatbot(height=300)
345
+ with gr.Accordion("Advanced - Document references", open=False):
346
+ with gr.Row():
347
+ doc_source1 = gr.Textbox(label="Reference 1", lines=2, container=True, scale=20)
348
+ source1_page = gr.Number(label="Page", scale=1)
349
+ with gr.Row():
350
+ doc_source2 = gr.Textbox(label="Reference 2", lines=2, container=True, scale=20)
351
+ source2_page = gr.Number(label="Page", scale=1)
352
+ with gr.Row():
353
+ doc_source3 = gr.Textbox(label="Reference 3", lines=2, container=True, scale=20)
354
+ source3_page = gr.Number(label="Page", scale=1)
355
+ with gr.Row():
356
+ msg = gr.Textbox(placeholder="Type message (e.g. 'What is this document about?')", container=True)
357
+ with gr.Row():
358
+ submit_btn = gr.Button("Submit message")
359
+ clear_btn = gr.ClearButton([msg, chatbot], value="Clear conversation")
360
+
361
+ # Preprocessing events
362
+ # upload_btn.upload(upload_file, inputs=[upload_btn], outputs=[document])
363
+ db_btn.click(initialize_database, \
364
+ inputs=[document, slider_chunk_size, slider_chunk_overlap], \
365
+ outputs=[vector_db, collection_name, db_progress])
366
+ qachain_btn.click(initialize_LLM, \
367
+ inputs=[llm_btn, slider_temperature, slider_maxtokens, slider_topk, vector_db], \
368
+ outputs=[qa_chain, llm_progress]).then(lambda: [None, "", 0, "", 0, "", 0], \
369
+ inputs=None, \
370
+ outputs=[chatbot, doc_source1, source1_page,
371
+ doc_source2, source2_page, doc_source3,
372
+ source3_page], \
373
+ queue=False)
374
+
375
+ # Chatbot events
376
+ msg.submit(conversation, \
377
+ inputs=[qa_chain, msg, chatbot], \
378
+ outputs=[qa_chain, msg, chatbot, doc_source1, source1_page, doc_source2, source2_page, doc_source3,
379
+ source3_page], \
380
+ queue=False)
381
+ submit_btn.click(conversation, \
382
+ inputs=[qa_chain, msg, chatbot], \
383
+ outputs=[qa_chain, msg, chatbot, doc_source1, source1_page, doc_source2, source2_page,
384
+ doc_source3, source3_page], \
385
+ queue=False)
386
+ clear_btn.click(lambda: [None, "", 0, "", 0, "", 0], \
387
+ inputs=None, \
388
+ outputs=[chatbot, doc_source1, source1_page, doc_source2, source2_page, doc_source3,
389
+ source3_page], \
390
+ queue=False)
391
+ demo.queue().launch(debug=True)
392
 
393
 
394
  if __name__ == "__main__":
395
+ demo()