|
import gradio as gr |
|
import os |
|
|
|
from langchain.document_loaders import PyPDFLoader |
|
from langchain.text_splitter import RecursiveCharacterTextSplitter |
|
from langchain.vectorstores import Chroma |
|
from langchain.chains import ConversationalRetrievalChain |
|
from langchain.embeddings import HuggingFaceEmbeddings |
|
from langchain.llms import HuggingFaceHub |
|
|
|
from pathlib import Path |
|
import chromadb |
|
|
|
|
|
list_llm = ["mistralai/Mistral-7B-Instruct-v0.2", "mistralai/Mixtral-8x7B-Instruct-v0.1", "mistralai/Mistral-7B-Instruct-v0.1", |
|
"google/gemma-7b-it", "google/gemma-2b-it", |
|
"HuggingFaceH4/zephyr-7b-beta", "meta-llama/Llama-2-7b-chat-hf", "microsoft/phi-2", |
|
"TinyLlama/TinyLlama-1.1B-Chat-v1.0", "mosaicml/mpt-7b-instruct", "tiiuae/falcon-7b-instruct", |
|
"google/flan-t5-xxl" |
|
] |
|
list_llm_simple = [os.path.basename(llm) for llm in list_llm] |
|
|
|
|
|
def load_doc(list_file_path, chunk_size, chunk_overlap): |
|
loaders = [PyPDFLoader(x) for x in list_file_path] |
|
pages = [] |
|
for loader in loaders: |
|
pages.extend(loader.load()) |
|
text_splitter = RecursiveCharacterTextSplitter(chunk_size=chunk_size, chunk_overlap=chunk_overlap) |
|
doc_splits = text_splitter.split_documents(pages) |
|
return doc_splits |
|
|
|
|
|
def create_db(splits, collection_name): |
|
embedding = HuggingFaceEmbeddings() |
|
new_client = chromadb.EphemeralClient() |
|
vectordb = Chroma.from_documents( |
|
documents=splits, |
|
embedding=embedding, |
|
client=new_client, |
|
collection_name=collection_name |
|
) |
|
return vectordb |
|
|
|
|
|
def initialize_llmchain(llm_model, temperature, max_tokens, top_k, vector_db, progress=gr.Progress()): |
|
if llm_model == "mistralai/Mixtral-8x7B-Instruct-v0.1": |
|
model_kwargs = {"temperature": temperature, "max_new_tokens": max_tokens, "top_k": top_k, "load_in_8bit": True} |
|
elif llm_model == "microsoft/phi-2": |
|
raise gr.Error("phi-2 model requires 'trust_remote_code=True', currently not supported by langchain HuggingFaceHub...") |
|
elif llm_model == "TinyLlama/TinyLlama-1.1B-Chat-v1.0": |
|
model_kwargs = {"temperature": temperature, "max_new_tokens": 250, "top_k": top_k} |
|
else: |
|
model_kwargs = {"temperature": temperature, "max_new_tokens": max_tokens, "top_k": top_k} |
|
|
|
llm = HuggingFaceHub( |
|
repo_id=llm_model, |
|
model_kwargs=model_kwargs |
|
) |
|
|
|
memory = ConversationBufferMemory( |
|
memory_key="chat_history", |
|
output_key='answer', |
|
return_messages=True |
|
) |
|
|
|
retriever = vector_db.as_retriever() |
|
|
|
qa_chain = ConversationalRetrievalChain.from_llm( |
|
llm, |
|
retriever=retriever, |
|
chain_type="stuff", |
|
memory=memory, |
|
return_source_documents=True, |
|
verbose=False |
|
) |
|
|
|
progress(0.9, desc="Done!") |
|
return qa_chain |
|
|
|
def initialize_demo(list_file_obj, chunk_size, chunk_overlap, db_progress): |
|
list_file_path = [file.name for file in list_file_obj if file is not None] |
|
collection_name = Path(list_file_path[0]).stem.replace(" ", "-")[:50] |
|
doc_splits = load_doc(list_file_path, chunk_size, chunk_overlap) |
|
vector_db = create_db(doc_splits, collection_name) |
|
qa_chain = initialize_llmchain( |
|
list_llm[0], |
|
0.7, |
|
1024, |
|
3, |
|
vector_db, |
|
db_progress |
|
) |
|
return vector_db, collection_name, qa_chain, "Complete!" |
|
|
|
def upload_file(file_obj): |
|
list_file_path = [] |
|
for file in file_obj: |
|
if file is not None: |
|
file_path = file.name |
|
list_file_path.append(file_path) |
|
return list_file_path |
|
|
|
def demo(): |
|
with gr.Blocks(theme="base") as demo: |
|
vector_db = gr.State() |
|
collection_name = gr.State() |
|
qa_chain = gr.State() |
|
|
|
with gr.Tab("Step 1 - Document pre-processing"): |
|
document = gr.Files(height=100, file_count="multiple", file_types=["pdf"], interactive=True, label="Upload your PDF documents (single or multiple)") |
|
slider_chunk_size = gr.Slider(minimum=100, maximum=1000, value=600, step=20, label="Chunk size", info="Chunk size", interactive=True) |
|
slider_chunk_overlap = gr.Slider(minimum=10, maximum=200, value=40, step=10, label="Chunk overlap", info="Chunk overlap", interactive=True) |
|
db_progress = gr.Textbox(label="Vector database initialization", value="None") |
|
db_btn = gr.Button("Generate vector database...") |
|
|
|
with gr.Tab("Step 2 - QA chain initialization"): |
|
llm_progress = gr.Textbox(value="None", label="QA chain initialization") |
|
qachain_btn = gr.Button("Initialize question-answering chain...") |
|
|
|
with gr.Tab("Step 3 - Conversation with chatbot"): |
|
chatbot = gr.Chatbot(height=300) |
|
doc_source1 = gr.Textbox(label="Reference 1", lines=2, container=True, scale=20) |
|
source1_page = gr.Number(label="Page", scale=1) |
|
doc_source2 = gr.Textbox(label="Reference 2", lines=2, container=True, scale=20) |
|
source2_page = gr.Number(label="Page", scale=1) |
|
doc_source3 = gr.Textbox(label="Reference 3", lines=2, container=True, scale=20) |
|
source3_page = gr.Number(label="Page", scale=1) |
|
msg = gr.Textbox(placeholder="Type message", container=True) |
|
submit_btn = gr.Button("Submit") |
|
clear_btn = gr.ClearButton([msg, chatbot]) |
|
|
|
document.upload(initialize_demo, inputs=[document, slider_chunk_size, slider_chunk_overlap, db_progress], outputs=[vector_db, collection_name, qa_chain, db_progress]) |
|
qachain_btn.click(initialize_llmchain, inputs=[qa_chain, llm_progress], outputs=[qa_chain, llm_progress]) |
|
submit_btn.click(lambda: None, inputs=None, outputs=[chatbot, doc_source1, source1_page, doc_source2 |
|
|