DHEIVER commited on
Commit
3bf61c2
·
verified ·
1 Parent(s): 2be297c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -69
app.py CHANGED
@@ -2,7 +2,6 @@ import gradio as gr
2
  import os
3
  api_token = os.getenv("HF_TOKEN")
4
 
5
-
6
  from langchain_community.vectorstores import FAISS
7
  from langchain_community.document_loaders import PyPDFLoader
8
  from langchain.text_splitter import RecursiveCharacterTextSplitter
@@ -15,14 +14,16 @@ from langchain.memory import ConversationBufferMemory
15
  from langchain_community.llms import HuggingFaceEndpoint
16
  import torch
17
 
18
- list_llm = ["meta-llama/Meta-Llama-3-8B-Instruct", "mistralai/Mistral-7B-Instruct-v0.2"]
 
 
 
 
 
19
  list_llm_simple = [os.path.basename(llm) for llm in list_llm]
20
 
21
- # Load and split PDF document
22
  def load_doc(list_file_path):
23
- # Processing for one document only
24
- # loader = PyPDFLoader(file_path)
25
- # pages = loader.load()
26
  loaders = [PyPDFLoader(x) for x in list_file_path]
27
  pages = []
28
  for loader in loaders:
@@ -34,36 +35,20 @@ def load_doc(list_file_path):
34
  doc_splits = text_splitter.split_documents(pages)
35
  return doc_splits
36
 
37
- # Create vector database
38
  def create_db(splits):
39
  embeddings = HuggingFaceEmbeddings()
40
  vectordb = FAISS.from_documents(splits, embeddings)
41
  return vectordb
42
 
43
-
44
- # Initialize langchain LLM chain
45
- from langchain_community.llms import HuggingFaceEndpoint
46
-
47
- # Initialize langchain LLM chain
48
  def initialize_llmchain(llm_model, temperature, max_tokens, top_k, vector_db, progress=gr.Progress()):
49
- if llm_model == "meta-llama/Meta-Llama-3-8B-Instruct":
50
- llm = HuggingFaceEndpoint(
51
- repo_id=llm_model,
52
- huggingfacehub_api_token=api_token,
53
- temperature=temperature,
54
- max_new_tokens=max_tokens,
55
- top_k=top_k,
56
- task="text-generation" # Explicitly specify the task type
57
- )
58
- else:
59
- llm = HuggingFaceEndpoint(
60
- huggingfacehub_api_token=api_token,
61
- repo_id=llm_model,
62
- temperature=temperature,
63
- max_new_tokens=max_tokens,
64
- top_k=top_k,
65
- task="text-generation" # Explicitly specify the task type
66
- )
67
 
68
  memory = ConversationBufferMemory(
69
  memory_key="chat_history",
@@ -81,37 +66,28 @@ def initialize_llmchain(llm_model, temperature, max_tokens, top_k, vector_db, pr
81
  verbose=False,
82
  )
83
  return qa_chain
84
-
85
- # Initialize database
86
  def initialize_database(list_file_obj, progress=gr.Progress()):
87
- # Create a list of documents (when valid)
88
  list_file_path = [x.name for x in list_file_obj if x is not None]
89
- # Load document and create splits
90
  doc_splits = load_doc(list_file_path)
91
- # Create or load vector database
92
  vector_db = create_db(doc_splits)
93
  return vector_db, "Database created!"
94
 
95
- # Initialize LLM
96
  def initialize_LLM(llm_option, llm_temperature, max_tokens, top_k, vector_db, progress=gr.Progress()):
97
- # print("llm_option",llm_option)
98
  llm_name = list_llm[llm_option]
99
  print("llm_name: ",llm_name)
100
  qa_chain = initialize_llmchain(llm_name, llm_temperature, max_tokens, top_k, vector_db, progress)
101
  return qa_chain, "QA chain initialized. Chatbot is ready!"
102
 
103
-
104
  def format_chat_history(message, chat_history):
105
  formatted_chat_history = []
106
  for user_message, bot_message in chat_history:
107
  formatted_chat_history.append(f"User: {user_message}")
108
  formatted_chat_history.append(f"Assistant: {bot_message}")
109
  return formatted_chat_history
110
-
111
 
112
  def conversation(qa_chain, message, history):
113
  formatted_chat_history = format_chat_history(message, history)
114
- # Generate response using QA chain
115
  response = qa_chain.invoke({"question": message, "chat_history": formatted_chat_history})
116
  response_answer = response["answer"]
117
  if response_answer.find("Helpful Answer:") != -1:
@@ -120,14 +96,11 @@ def conversation(qa_chain, message, history):
120
  response_source1 = response_sources[0].page_content.strip()
121
  response_source2 = response_sources[1].page_content.strip()
122
  response_source3 = response_sources[2].page_content.strip()
123
- # Langchain sources are zero-based
124
  response_source1_page = response_sources[0].metadata["page"] + 1
125
  response_source2_page = response_sources[1].metadata["page"] + 1
126
  response_source3_page = response_sources[2].metadata["page"] + 1
127
- # Append user message and response to chat history
128
  new_history = history + [(message, response_answer)]
129
  return qa_chain, gr.update(value=""), new_history, response_source1, response_source1_page, response_source2, response_source2_page, response_source3, response_source3_page
130
-
131
 
132
  def upload_file(file_obj):
133
  list_file_path = []
@@ -136,10 +109,15 @@ def upload_file(file_obj):
136
  list_file_path.append(file_path)
137
  return list_file_path
138
 
139
-
140
  def demo():
141
- # with gr.Blocks(theme=gr.themes.Default(primary_hue="sky")) as demo:
142
- with gr.Blocks(theme=gr.themes.Default(primary_hue="red", secondary_hue="pink", neutral_hue = "sky")) as demo:
 
 
 
 
 
 
143
  vector_db = gr.State()
144
  qa_chain = gr.State()
145
  gr.HTML("<center><h1>RAG PDF chatbot</h1><center>")
@@ -154,10 +132,10 @@ def demo():
154
  with gr.Row():
155
  db_btn = gr.Button("Create vector database")
156
  with gr.Row():
157
- db_progress = gr.Textbox(value="Not initialized", show_label=False) # label="Vector database status",
158
  gr.Markdown("<style>body { font-size: 16px; }</style><b>Select Large Language Model (LLM) and input parameters</b>")
159
  with gr.Row():
160
- llm_btn = gr.Radio(list_llm_simple, label="Available LLMs", value = list_llm_simple[0], type="index") # info="Select LLM", show_label=False
161
  with gr.Row():
162
  with gr.Accordion("LLM input parameters", open=False):
163
  with gr.Row():
@@ -165,11 +143,11 @@ def demo():
165
  with gr.Row():
166
  slider_maxtokens = gr.Slider(minimum = 128, maximum = 9192, value=4096, step=128, label="Max New Tokens", info="Maximum number of tokens to be generated",interactive=True)
167
  with gr.Row():
168
- slider_topk = gr.Slider(minimum = 1, maximum = 10, value=3, step=1, label="top-k", info="Number of tokens to select the next token from", interactive=True)
169
  with gr.Row():
170
  qachain_btn = gr.Button("Initialize Question Answering Chatbot")
171
  with gr.Row():
172
- llm_progress = gr.Textbox(value="Not initialized", show_label=False) # label="Chatbot status",
173
 
174
  with gr.Column(scale = 200):
175
  gr.Markdown("<b>Step 2 - Chat with your Document</b>")
@@ -190,32 +168,31 @@ def demo():
190
  submit_btn = gr.Button("Submit")
191
  clear_btn = gr.ClearButton([msg, chatbot], value="Clear")
192
 
193
- # Preprocessing events
194
- db_btn.click(initialize_database, \
195
- inputs=[document], \
196
  outputs=[vector_db, db_progress])
197
- qachain_btn.click(initialize_LLM, \
198
- inputs=[llm_btn, slider_temperature, slider_maxtokens, slider_topk, vector_db], \
199
- outputs=[qa_chain, llm_progress]).then(lambda:[None,"",0,"",0,"",0], \
200
- inputs=None, \
201
- outputs=[chatbot, doc_source1, source1_page, doc_source2, source2_page, doc_source3, source3_page], \
202
  queue=False)
203
 
204
- # Chatbot events
205
- msg.submit(conversation, \
206
- inputs=[qa_chain, msg, chatbot], \
207
- outputs=[qa_chain, msg, chatbot, doc_source1, source1_page, doc_source2, source2_page, doc_source3, source3_page], \
208
  queue=False)
209
- submit_btn.click(conversation, \
210
- inputs=[qa_chain, msg, chatbot], \
211
- outputs=[qa_chain, msg, chatbot, doc_source1, source1_page, doc_source2, source2_page, doc_source3, source3_page], \
212
  queue=False)
213
- clear_btn.click(lambda:[None,"",0,"",0,"",0], \
214
- inputs=None, \
215
- outputs=[chatbot, doc_source1, source1_page, doc_source2, source2_page, doc_source3, source3_page], \
216
  queue=False)
217
- demo.queue().launch(debug=True)
218
 
 
219
 
220
  if __name__ == "__main__":
221
  demo()
 
2
  import os
3
  api_token = os.getenv("HF_TOKEN")
4
 
 
5
  from langchain_community.vectorstores import FAISS
6
  from langchain_community.document_loaders import PyPDFLoader
7
  from langchain.text_splitter import RecursiveCharacterTextSplitter
 
14
  from langchain_community.llms import HuggingFaceEndpoint
15
  import torch
16
 
17
+ # Added Deepseek model to the list
18
+ list_llm = [
19
+ "meta-llama/Meta-Llama-3-8B-Instruct",
20
+ "mistralai/Mistral-7B-Instruct-v0.2",
21
+ "deepseek-ai/deepseek-llm-7b-chat"
22
+ ]
23
  list_llm_simple = [os.path.basename(llm) for llm in list_llm]
24
 
25
+ # Rest of the functions remain the same until demo()
26
  def load_doc(list_file_path):
 
 
 
27
  loaders = [PyPDFLoader(x) for x in list_file_path]
28
  pages = []
29
  for loader in loaders:
 
35
  doc_splits = text_splitter.split_documents(pages)
36
  return doc_splits
37
 
 
38
  def create_db(splits):
39
  embeddings = HuggingFaceEmbeddings()
40
  vectordb = FAISS.from_documents(splits, embeddings)
41
  return vectordb
42
 
 
 
 
 
 
43
  def initialize_llmchain(llm_model, temperature, max_tokens, top_k, vector_db, progress=gr.Progress()):
44
+ llm = HuggingFaceEndpoint(
45
+ repo_id=llm_model,
46
+ huggingfacehub_api_token=api_token,
47
+ temperature=temperature,
48
+ max_new_tokens=max_tokens,
49
+ top_k=top_k,
50
+ task="text-generation"
51
+ )
 
 
 
 
 
 
 
 
 
 
52
 
53
  memory = ConversationBufferMemory(
54
  memory_key="chat_history",
 
66
  verbose=False,
67
  )
68
  return qa_chain
69
+
 
70
  def initialize_database(list_file_obj, progress=gr.Progress()):
 
71
  list_file_path = [x.name for x in list_file_obj if x is not None]
 
72
  doc_splits = load_doc(list_file_path)
 
73
  vector_db = create_db(doc_splits)
74
  return vector_db, "Database created!"
75
 
 
76
  def initialize_LLM(llm_option, llm_temperature, max_tokens, top_k, vector_db, progress=gr.Progress()):
 
77
  llm_name = list_llm[llm_option]
78
  print("llm_name: ",llm_name)
79
  qa_chain = initialize_llmchain(llm_name, llm_temperature, max_tokens, top_k, vector_db, progress)
80
  return qa_chain, "QA chain initialized. Chatbot is ready!"
81
 
 
82
  def format_chat_history(message, chat_history):
83
  formatted_chat_history = []
84
  for user_message, bot_message in chat_history:
85
  formatted_chat_history.append(f"User: {user_message}")
86
  formatted_chat_history.append(f"Assistant: {bot_message}")
87
  return formatted_chat_history
 
88
 
89
  def conversation(qa_chain, message, history):
90
  formatted_chat_history = format_chat_history(message, history)
 
91
  response = qa_chain.invoke({"question": message, "chat_history": formatted_chat_history})
92
  response_answer = response["answer"]
93
  if response_answer.find("Helpful Answer:") != -1:
 
96
  response_source1 = response_sources[0].page_content.strip()
97
  response_source2 = response_sources[1].page_content.strip()
98
  response_source3 = response_sources[2].page_content.strip()
 
99
  response_source1_page = response_sources[0].metadata["page"] + 1
100
  response_source2_page = response_sources[1].metadata["page"] + 1
101
  response_source3_page = response_sources[2].metadata["page"] + 1
 
102
  new_history = history + [(message, response_answer)]
103
  return qa_chain, gr.update(value=""), new_history, response_source1, response_source1_page, response_source2, response_source2_page, response_source3, response_source3_page
 
104
 
105
  def upload_file(file_obj):
106
  list_file_path = []
 
109
  list_file_path.append(file_path)
110
  return list_file_path
111
 
 
112
  def demo():
113
+ # Modified theme to use dark blue colors
114
+ theme = gr.themes.Default(
115
+ primary_hue="indigo",
116
+ secondary_hue="blue",
117
+ neutral_hue="slate"
118
+ )
119
+
120
+ with gr.Blocks(theme=theme) as demo:
121
  vector_db = gr.State()
122
  qa_chain = gr.State()
123
  gr.HTML("<center><h1>RAG PDF chatbot</h1><center>")
 
132
  with gr.Row():
133
  db_btn = gr.Button("Create vector database")
134
  with gr.Row():
135
+ db_progress = gr.Textbox(value="Not initialized", show_label=False)
136
  gr.Markdown("<style>body { font-size: 16px; }</style><b>Select Large Language Model (LLM) and input parameters</b>")
137
  with gr.Row():
138
+ llm_btn = gr.Radio(list_llm_simple, label="Available LLMs", value = list_llm_simple[0], type="index")
139
  with gr.Row():
140
  with gr.Accordion("LLM input parameters", open=False):
141
  with gr.Row():
 
143
  with gr.Row():
144
  slider_maxtokens = gr.Slider(minimum = 128, maximum = 9192, value=4096, step=128, label="Max New Tokens", info="Maximum number of tokens to be generated",interactive=True)
145
  with gr.Row():
146
+ slider_topk = gr.Slider(minimum = 1, maximum = 10, value=3, step=1, label="top-k", info="Number of tokens to select the next token from", interactive=True)
147
  with gr.Row():
148
  qachain_btn = gr.Button("Initialize Question Answering Chatbot")
149
  with gr.Row():
150
+ llm_progress = gr.Textbox(value="Not initialized", show_label=False)
151
 
152
  with gr.Column(scale = 200):
153
  gr.Markdown("<b>Step 2 - Chat with your Document</b>")
 
168
  submit_btn = gr.Button("Submit")
169
  clear_btn = gr.ClearButton([msg, chatbot], value="Clear")
170
 
171
+ # Event handlers
172
+ db_btn.click(initialize_database,
173
+ inputs=[document],
174
  outputs=[vector_db, db_progress])
175
+ qachain_btn.click(initialize_LLM,
176
+ inputs=[llm_btn, slider_temperature, slider_maxtokens, slider_topk, vector_db],
177
+ outputs=[qa_chain, llm_progress]).then(lambda:[None,"",0,"",0,"",0],
178
+ inputs=None,
179
+ outputs=[chatbot, doc_source1, source1_page, doc_source2, source2_page, doc_source3, source3_page],
180
  queue=False)
181
 
182
+ msg.submit(conversation,
183
+ inputs=[qa_chain, msg, chatbot],
184
+ outputs=[qa_chain, msg, chatbot, doc_source1, source1_page, doc_source2, source2_page, doc_source3, source3_page],
 
185
  queue=False)
186
+ submit_btn.click(conversation,
187
+ inputs=[qa_chain, msg, chatbot],
188
+ outputs=[qa_chain, msg, chatbot, doc_source1, source1_page, doc_source2, source2_page, doc_source3, source3_page],
189
  queue=False)
190
+ clear_btn.click(lambda:[None,"",0,"",0,"",0],
191
+ inputs=None,
192
+ outputs=[chatbot, doc_source1, source1_page, doc_source2, source2_page, doc_source3, source3_page],
193
  queue=False)
 
194
 
195
+ demo.queue().launch(debug=True)
196
 
197
  if __name__ == "__main__":
198
  demo()