Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,52 +2,51 @@ import gradio as gr
|
|
| 2 |
import os
|
| 3 |
import re
|
| 4 |
from pathlib import Path
|
| 5 |
-
|
| 6 |
from langchain_community.vectorstores import Chroma
|
| 7 |
from langchain.chains import ConversationalRetrievalChain
|
| 8 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
from langchain_community.llms import HuggingFaceEndpoint
|
| 13 |
|
| 14 |
from pathlib import Path
|
| 15 |
import chromadb
|
| 16 |
from unidecode import unidecode
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
-
# default_persist_directory = './chroma_HF/'
|
| 28 |
-
list_llm = ["mistralai/Mistral-7B-Instruct-v0.2", "mistralai/Mixtral-8x7B-Instruct-v0.1", "mistralai/Mistral-7B-Instruct-v0.1", \
|
| 29 |
-
"google/gemma-7b-it","google/gemma-2b-it", \
|
| 30 |
-
"HuggingFaceH4/zephyr-7b-beta", "HuggingFaceH4/zephyr-7b-gemma-v0.1", \
|
| 31 |
-
"meta-llama/Llama-2-7b-chat-hf", "microsoft/phi-2", \
|
| 32 |
-
"TinyLlama/TinyLlama-1.1B-Chat-v1.0", "mosaicml/mpt-7b-instruct", "tiiuae/falcon-7b-instruct", \
|
| 33 |
-
"google/flan-t5-xxl"
|
| 34 |
]
|
| 35 |
-
list_llm_simple = [os.path.basename(llm) for llm in
|
| 36 |
|
| 37 |
# Load PDF document and create doc splits
|
| 38 |
def load_doc(list_file_path, chunk_size, chunk_overlap):
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
loaders = [PyPDFLoader(x) for x in list_file_path]
|
| 43 |
pages = []
|
| 44 |
for loader in loaders:
|
| 45 |
pages.extend(loader.load())
|
| 46 |
-
# text_splitter = RecursiveCharacterTextSplitter(chunk_size = 600, chunk_overlap = 50)
|
| 47 |
-
text_splitter = RecursiveCharacterTextSplitter(
|
| 48 |
-
chunk_size = chunk_size,
|
| 49 |
-
chunk_overlap = chunk_overlap)
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
doc_splits = text_splitter.split_documents(pages)
|
| 52 |
return doc_splits
|
| 53 |
|
|
@@ -58,322 +57,322 @@ def create_db(splits, collection_name):
|
|
| 58 |
embedding=embedding,
|
| 59 |
client=new_client,
|
| 60 |
collection_name=collection_name,
|
| 61 |
-
|
| 62 |
)
|
| 63 |
return vectordb
|
| 64 |
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
|
| 74 |
|
| 75 |
# Initialize langchain LLM chain
|
| 76 |
def initialize_llmchain(llm_model, temperature, max_tokens, top_k, vector_db, progress=gr.Progress()):
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
|
| 158 |
-
|
| 159 |
memory = ConversationBufferMemory(
|
| 160 |
memory_key="chat_history",
|
| 161 |
output_key='answer',
|
| 162 |
return_messages=True
|
| 163 |
)
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
qa_chain = ConversationalRetrievalChain.from_llm(
|
| 168 |
llm,
|
| 169 |
retriever=retriever,
|
| 170 |
chain_type="stuff",
|
| 171 |
memory=memory,
|
| 172 |
-
|
| 173 |
return_source_documents=True,
|
| 174 |
-
|
| 175 |
verbose=False,
|
| 176 |
)
|
| 177 |
-
|
| 178 |
return qa_chain
|
| 179 |
|
| 180 |
|
| 181 |
# Generate collection name for vector database
|
| 182 |
-
|
| 183 |
def create_collection_name(filepath):
|
| 184 |
-
|
| 185 |
collection_name = Path(filepath).stem
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
if len(collection_name) < 3:
|
| 198 |
collection_name = collection_name + 'xyz'
|
| 199 |
-
|
| 200 |
if not collection_name[0].isalnum():
|
| 201 |
collection_name = 'A' + collection_name[1:]
|
| 202 |
if not collection_name[-1].isalnum():
|
| 203 |
collection_name = collection_name[:-1] + 'Z'
|
| 204 |
-
|
| 205 |
-
|
| 206 |
return collection_name
|
| 207 |
|
| 208 |
|
| 209 |
# Initialize database
|
| 210 |
def initialize_database(list_file_obj, chunk_size, chunk_overlap, progress=gr.Progress()):
|
| 211 |
-
|
| 212 |
list_file_path = [x.name for x in list_file_obj if x is not None]
|
| 213 |
-
|
| 214 |
-
|
| 215 |
collection_name = create_collection_name(list_file_path[0])
|
| 216 |
-
|
| 217 |
-
|
| 218 |
doc_splits = load_doc(list_file_path, chunk_size, chunk_overlap)
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
vector_db = create_db(doc_splits, collection_name)
|
| 223 |
-
progress(0.9, desc="Done!")
|
| 224 |
-
return vector_db, collection_name, "Complete!"
|
| 225 |
|
|
|
|
| 226 |
|
|
|
|
| 227 |
def initialize_LLM(llm_option, llm_temperature, max_tokens, top_k, vector_db, progress=gr.Progress()):
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
qa_chain = initialize_llmchain(llm_name, llm_temperature, max_tokens, top_k, vector_db, progress)
|
| 232 |
return qa_chain, "Complete!"
|
| 233 |
|
| 234 |
-
|
| 235 |
def format_chat_history(message, chat_history):
|
| 236 |
formatted_chat_history = []
|
| 237 |
for user_message, bot_message in chat_history:
|
| 238 |
formatted_chat_history.append(f"User: {user_message}")
|
| 239 |
formatted_chat_history.append(f"Assistant: {bot_message}")
|
| 240 |
return formatted_chat_history
|
| 241 |
-
|
| 242 |
|
| 243 |
|
|
|
|
| 244 |
def conversation(qa_chain, message, history):
|
| 245 |
formatted_chat_history = format_chat_history(message, history)
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
response = qa_chain({"question": message, "chat_history": formatted_chat_history})
|
| 250 |
-
response_answer = response["answer"]
|
| 251 |
-
|
| 252 |
-
|
| 253 |
response_sources = response["source_documents"]
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
new_history = history + [(message, response_answer)]
|
| 266 |
-
|
| 267 |
-
return qa_chain, gr.update(value=""), new_history,
|
| 268 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 269 |
|
| 270 |
-
def upload_file(file_obj):
|
| 271 |
-
list_file_path = []
|
| 272 |
-
for idx, file in enumerate(file_obj):
|
| 273 |
-
file_path = file_obj.name
|
| 274 |
-
list_file_path.append(file_path)
|
| 275 |
-
# print(file_path)
|
| 276 |
-
# initialize_database(file_path, progress)
|
| 277 |
-
return list_file_path
|
| 278 |
|
| 279 |
|
| 280 |
|
|
|
|
| 281 |
def demo():
|
| 282 |
-
with gr.Blocks(theme="
|
| 283 |
vector_db = gr.State()
|
| 284 |
qa_chain = gr.State()
|
| 285 |
collection_name = gr.State()
|
| 286 |
|
| 287 |
gr.Markdown(
|
| 288 |
-
"""<center><h2>PDF-based
|
| 289 |
<h3>Ask any questions about your PDF documents</h3>""")
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
|
| 297 |
-
with gr.Tab("
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
|
| 302 |
-
with gr.Tab("
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
|
| 315 |
-
with gr.Tab("
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
chatbot = gr.Chatbot(height=300)
|
| 333 |
-
with gr.Accordion("
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
|
| 349 |
-
#
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
demo.queue().launch(debug=True)
|
| 375 |
|
| 376 |
|
|
|
|
| 377 |
|
| 378 |
if __name__ == "__main__":
|
| 379 |
demo()
|
|
|
|
| 2 |
import os
|
| 3 |
import re
|
| 4 |
from pathlib import Path
|
|
|
|
| 5 |
from langchain_community.vectorstores import Chroma
|
| 6 |
from langchain.chains import ConversationalRetrievalChain
|
| 7 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
|
| 11 |
from langchain_community.llms import HuggingFaceEndpoint
|
| 12 |
|
| 13 |
from pathlib import Path
|
| 14 |
import chromadb
|
| 15 |
from unidecode import unidecode
|
| 16 |
|
| 17 |
+
# List of allowed models
|
| 18 |
+
allowed_llms = [
|
| 19 |
+
"mistralai/Mistral-7B-Instruct-v0.2",
|
| 20 |
+
"mistralai/Mixtral-8x7B-Instruct-v0.1",
|
| 21 |
+
"mistralai/Mistral-7B-Instruct-v0.1",
|
| 22 |
+
"google/gemma-7b-it",
|
| 23 |
+
"google/gemma-2b-it",
|
| 24 |
+
"HuggingFaceH4/zephyr-7b-beta",
|
| 25 |
+
"HuggingFaceH4/zephyr-7b-gemma-v0.1",
|
| 26 |
+
"meta-llama/Llama-2-7b-chat-hf"
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
|
| 30 |
|
| 31 |
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
]
|
| 34 |
+
list_llm_simple = [os.path.basename(llm) for llm in allowed_llms]
|
| 35 |
|
| 36 |
# Load PDF document and create doc splits
|
| 37 |
def load_doc(list_file_path, chunk_size, chunk_overlap):
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
|
| 41 |
loaders = [PyPDFLoader(x) for x in list_file_path]
|
| 42 |
pages = []
|
| 43 |
for loader in loaders:
|
| 44 |
pages.extend(loader.load())
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
text_splitter = RecursiveCharacterTextSplitter(
|
| 47 |
+
chunk_size=chunk_size,
|
| 48 |
+
chunk_overlap=chunk_overlap
|
| 49 |
+
)
|
| 50 |
doc_splits = text_splitter.split_documents(pages)
|
| 51 |
return doc_splits
|
| 52 |
|
|
|
|
| 57 |
embedding=embedding,
|
| 58 |
client=new_client,
|
| 59 |
collection_name=collection_name,
|
| 60 |
+
|
| 61 |
)
|
| 62 |
return vectordb
|
| 63 |
|
| 64 |
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
|
| 72 |
|
| 73 |
|
| 74 |
# Initialize langchain LLM chain
|
| 75 |
def initialize_llmchain(llm_model, temperature, max_tokens, top_k, vector_db, progress=gr.Progress()):
|
| 76 |
+
llm = HuggingFaceEndpoint(
|
| 77 |
+
repo_id=llm_model,
|
| 78 |
+
temperature=temperature,
|
| 79 |
+
max_new_tokens=max_tokens,
|
| 80 |
+
top_k=top_k,
|
| 81 |
+
load_in_8bit=True,
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
|
| 156 |
|
| 157 |
+
|
| 158 |
memory = ConversationBufferMemory(
|
| 159 |
memory_key="chat_history",
|
| 160 |
output_key='answer',
|
| 161 |
return_messages=True
|
| 162 |
)
|
| 163 |
+
retriever = vector_db.as_retriever()
|
| 164 |
+
|
| 165 |
+
|
| 166 |
qa_chain = ConversationalRetrievalChain.from_llm(
|
| 167 |
llm,
|
| 168 |
retriever=retriever,
|
| 169 |
chain_type="stuff",
|
| 170 |
memory=memory,
|
| 171 |
+
|
| 172 |
return_source_documents=True,
|
| 173 |
+
|
| 174 |
verbose=False,
|
| 175 |
)
|
| 176 |
+
|
| 177 |
return qa_chain
|
| 178 |
|
| 179 |
|
| 180 |
# Generate collection name for vector database
|
| 181 |
+
|
| 182 |
def create_collection_name(filepath):
|
| 183 |
+
|
| 184 |
collection_name = Path(filepath).stem
|
| 185 |
+
collection_name = unidecode(collection_name).replace(" ", "-")
|
| 186 |
+
collection_name = re.sub('[^A-Za-z0-9]+', '-', collection_name)[:50]
|
| 187 |
+
|
| 188 |
+
|
| 189 |
+
|
| 190 |
+
|
| 191 |
+
|
| 192 |
+
|
| 193 |
+
|
| 194 |
+
|
| 195 |
+
|
| 196 |
if len(collection_name) < 3:
|
| 197 |
collection_name = collection_name + 'xyz'
|
| 198 |
+
|
| 199 |
if not collection_name[0].isalnum():
|
| 200 |
collection_name = 'A' + collection_name[1:]
|
| 201 |
if not collection_name[-1].isalnum():
|
| 202 |
collection_name = collection_name[:-1] + 'Z'
|
| 203 |
+
|
| 204 |
+
|
| 205 |
return collection_name
|
| 206 |
|
| 207 |
|
| 208 |
# Initialize database
|
| 209 |
def initialize_database(list_file_obj, chunk_size, chunk_overlap, progress=gr.Progress()):
|
| 210 |
+
|
| 211 |
list_file_path = [x.name for x in list_file_obj if x is not None]
|
| 212 |
+
|
| 213 |
+
|
| 214 |
collection_name = create_collection_name(list_file_path[0])
|
| 215 |
+
|
| 216 |
+
|
| 217 |
doc_splits = load_doc(list_file_path, chunk_size, chunk_overlap)
|
| 218 |
+
|
| 219 |
+
|
| 220 |
+
|
| 221 |
vector_db = create_db(doc_splits, collection_name)
|
|
|
|
|
|
|
| 222 |
|
| 223 |
+
return vector_db, collection_name, "Complete!"
|
| 224 |
|
| 225 |
+
# Initialize LLM
|
| 226 |
def initialize_LLM(llm_option, llm_temperature, max_tokens, top_k, vector_db, progress=gr.Progress()):
|
| 227 |
+
llm_name = allowed_llms[llm_option]
|
| 228 |
+
|
| 229 |
+
|
| 230 |
qa_chain = initialize_llmchain(llm_name, llm_temperature, max_tokens, top_k, vector_db, progress)
|
| 231 |
return qa_chain, "Complete!"
|
| 232 |
|
| 233 |
+
# Format chat history
|
| 234 |
def format_chat_history(message, chat_history):
|
| 235 |
formatted_chat_history = []
|
| 236 |
for user_message, bot_message in chat_history:
|
| 237 |
formatted_chat_history.append(f"User: {user_message}")
|
| 238 |
formatted_chat_history.append(f"Assistant: {bot_message}")
|
| 239 |
return formatted_chat_history
|
|
|
|
| 240 |
|
| 241 |
|
| 242 |
+
# Conversation handling
|
| 243 |
def conversation(qa_chain, message, history):
|
| 244 |
formatted_chat_history = format_chat_history(message, history)
|
| 245 |
+
|
| 246 |
+
|
| 247 |
+
|
| 248 |
response = qa_chain({"question": message, "chat_history": formatted_chat_history})
|
| 249 |
+
response_answer = response["answer"].split("Helpful Answer:")[-1]
|
| 250 |
+
|
| 251 |
+
|
| 252 |
response_sources = response["source_documents"]
|
| 253 |
+
|
| 254 |
+
|
| 255 |
+
|
| 256 |
+
|
| 257 |
+
|
| 258 |
+
|
| 259 |
+
|
| 260 |
+
|
| 261 |
+
|
| 262 |
+
|
| 263 |
+
|
| 264 |
new_history = history + [(message, response_answer)]
|
| 265 |
+
response_details = [(src.page_content.strip(), src.metadata["page"] + 1) for src in response_sources[:3]]
|
| 266 |
+
return qa_chain, gr.update(value=""), new_history, *sum(response_details, ())
|
| 267 |
+
|
| 268 |
+
|
| 269 |
+
|
| 270 |
+
|
| 271 |
+
|
| 272 |
+
|
| 273 |
+
|
| 274 |
+
|
| 275 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
+
# Gradio Interface
|
| 280 |
def demo():
|
| 281 |
+
with gr.Blocks(theme="default") as demo:
|
| 282 |
vector_db = gr.State()
|
| 283 |
qa_chain = gr.State()
|
| 284 |
collection_name = gr.State()
|
| 285 |
|
| 286 |
gr.Markdown(
|
| 287 |
+
"""<center><h2>PDF-based Chatbot</h2></center>
|
| 288 |
<h3>Ask any questions about your PDF documents</h3>""")
|
| 289 |
+
|
| 290 |
+
|
| 291 |
+
|
| 292 |
+
|
| 293 |
+
|
| 294 |
+
|
| 295 |
|
| 296 |
+
with gr.Tab("Upload PDF"):
|
| 297 |
+
document = gr.Files(height=100, file_count="multiple", file_types=["pdf"], interactive=True, label="Upload PDF Documents")
|
| 298 |
+
|
| 299 |
+
|
| 300 |
|
| 301 |
+
with gr.Tab("Process Document"):
|
| 302 |
+
db_btn = gr.Radio(["ChromaDB"], label="Vector Database", value="ChromaDB", type="index")
|
| 303 |
+
with gr.Accordion("Advanced Options", open=False):
|
| 304 |
+
slider_chunk_size = gr.Slider(100, 1000, 600, 20, label="Chunk Size", interactive=True)
|
| 305 |
+
slider_chunk_overlap = gr.Slider(10, 200, 40, 10, label="Chunk Overlap", interactive=True)
|
| 306 |
+
db_progress = gr.Textbox(label="Database Initialization Status", value="None")
|
| 307 |
+
db_btn = gr.Button("Generate Database")
|
| 308 |
+
|
| 309 |
+
|
| 310 |
+
|
| 311 |
+
|
| 312 |
+
|
| 313 |
|
| 314 |
+
with gr.Tab("Initialize QA Chain"):
|
| 315 |
+
llm_btn = gr.Radio(list_llm_simple, label="LLM Models", value=list_llm_simple[0], type="index")
|
| 316 |
+
with gr.Accordion("Advanced Options", open=False):
|
| 317 |
+
slider_temperature = gr.Slider(0.01, 1.0, 0.7, 0.1, label="Temperature", interactive=True)
|
| 318 |
+
slider_maxtokens = gr.Slider(224, 4096, 1024, 32, label="Max Tokens", interactive=True)
|
| 319 |
+
slider_topk = gr.Slider(1, 10, 3, 1, label="Top-k Samples", interactive=True)
|
| 320 |
+
llm_progress = gr.Textbox(value="None", label="QA Chain Initialization Status")
|
| 321 |
+
qachain_btn = gr.Button("Initialize QA Chain")
|
| 322 |
+
|
| 323 |
+
with gr.Tab("Chatbot"):
|
| 324 |
+
|
| 325 |
+
|
| 326 |
+
|
| 327 |
+
|
| 328 |
+
|
| 329 |
+
|
| 330 |
+
|
| 331 |
chatbot = gr.Chatbot(height=300)
|
| 332 |
+
with gr.Accordion("Document References", open=False):
|
| 333 |
+
for i in range(1, 4):
|
| 334 |
+
gr.Row([gr.Textbox(label=f"Reference {i}", lines=2, container=True, scale=20), gr.Number(label="Page", scale=1)])
|
| 335 |
+
msg = gr.Textbox(placeholder="Type message here...", container=True)
|
| 336 |
+
gr.Row([gr.Button("Submit"), gr.Button("Clear Conversation")])
|
| 337 |
+
|
| 338 |
+
|
| 339 |
+
|
| 340 |
+
|
| 341 |
+
|
| 342 |
+
|
| 343 |
+
|
| 344 |
+
|
| 345 |
+
|
| 346 |
+
|
| 347 |
|
| 348 |
+
# Define Interactions
|
| 349 |
+
db_btn.click(initialize_database, inputs=[document, slider_chunk_size, slider_chunk_overlap], outputs=[vector_db, collection_name, db_progress])
|
| 350 |
+
qachain_btn.click(initialize_LLM, inputs=[llm_btn, slider_temperature, slider_maxtokens, slider_topk, vector_db], outputs=[qa_chain, llm_progress])
|
| 351 |
+
msg.submit(conversation, inputs=[qa_chain, msg, chatbot], outputs=[qa_chain, msg, chatbot] + [None] * 6)
|
| 352 |
+
|
| 353 |
+
|
| 354 |
+
|
| 355 |
+
|
| 356 |
+
|
| 357 |
+
|
| 358 |
+
|
| 359 |
+
|
| 360 |
+
|
| 361 |
+
|
| 362 |
+
|
| 363 |
+
|
| 364 |
+
|
| 365 |
+
|
| 366 |
+
|
| 367 |
+
|
| 368 |
+
|
| 369 |
+
|
| 370 |
+
|
| 371 |
+
|
| 372 |
+
|
|
|
|
| 373 |
|
| 374 |
|
| 375 |
+
demo.launch(debug=True)
|
| 376 |
|
| 377 |
if __name__ == "__main__":
|
| 378 |
demo()
|