Qazi-Mudassar-Ilyas
commited on
Commit
•
66e66b5
1
Parent(s):
99650b6
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,165 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
from langchain.chains import RetrievalQA
|
4 |
+
from langchain_community.document_loaders import TextLoader
|
5 |
+
from langchain.document_loaders import PyPDFLoader
|
6 |
+
from langchain.document_loaders import PyMuPDFLoader
|
7 |
+
|
8 |
+
import chromadb #==0.4.24
|
9 |
+
from langchain.memory import ConversationBufferMemory
|
10 |
+
|
11 |
+
|
12 |
+
from langchain.indexes import VectorstoreIndexCreator
|
13 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
14 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
15 |
+
from langchain_community.llms import HuggingFaceEndpoint
|
16 |
+
from langchain import HuggingFaceHub
|
17 |
+
from dotenv import find_dotenv, load_dotenv
|
18 |
+
|
19 |
+
from langchain.chains import create_retrieval_chain, RetrievalQA
|
20 |
+
from langchain.chains import ConversationalRetrievalChain
|
21 |
+
|
22 |
+
from langchain_community.vectorstores import FAISS
|
23 |
+
|
24 |
+
from langchain_community.vectorstores import LanceDB
|
25 |
+
import lancedb #==0.6.5
|
26 |
+
|
27 |
+
from langchain_community.vectorstores import Chroma
|
28 |
+
|
29 |
+
_=load_dotenv(find_dotenv())
|
30 |
+
hf_api = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
31 |
+
|
32 |
+
llms = ["Google/flan-t5-xxl", "Mistralai/Mistral-7B-Instruct-v0.2", "Mistralai/Mistral-7B-Instruct-v0.1", \
|
33 |
+
"Google/gemma-7b-it","Google/gemma-2b-it", "HuggingFaceH4/zephyr-7b-beta", \
|
34 |
+
"TinyLlama/TinyLlama-1.1B-Chat-v1.0", "Mosaicml/mpt-7b-instruct", "Tiiuae/falcon-7b-instruct", \
|
35 |
+
]
|
36 |
+
|
37 |
+
def indexdocs (docs,chunk_size,chunk_overlap,vector_store, progress=gr.Progress()):
|
38 |
+
|
39 |
+
progress(0.1,desc="Loading documents...")
|
40 |
+
|
41 |
+
loaders = [PyPDFLoader(x) for x in docs]
|
42 |
+
pages = []
|
43 |
+
for loader in loaders:
|
44 |
+
pages.extend(loader.load())
|
45 |
+
|
46 |
+
progress(0.2,desc="Splitting documents...")
|
47 |
+
|
48 |
+
text_splitter = RecursiveCharacterTextSplitter(
|
49 |
+
chunk_size = chunk_size,
|
50 |
+
chunk_overlap = chunk_overlap)
|
51 |
+
doc_splits = text_splitter.split_documents(pages)
|
52 |
+
|
53 |
+
progress(0.3,desc="Generating embeddings...")
|
54 |
+
|
55 |
+
embedding = HuggingFaceEmbeddings()
|
56 |
+
|
57 |
+
progress(0.5,desc="Generating vectorstore...")
|
58 |
+
|
59 |
+
if vector_store== 0:
|
60 |
+
new_client = chromadb.EphemeralClient()# "Chroma"
|
61 |
+
vector_store_db = Chroma.from_documents(
|
62 |
+
documents=doc_splits,
|
63 |
+
embedding=embedding,
|
64 |
+
client=new_client #,
|
65 |
+
)
|
66 |
+
elif vector_store==1: #"FAISS"
|
67 |
+
vector_store_db = FAISS.from_documents(
|
68 |
+
documents=doc_splits,
|
69 |
+
embedding=embedding
|
70 |
+
)
|
71 |
+
else: #Lance
|
72 |
+
vector_store_db = LanceDB.from_documents(
|
73 |
+
documents=doc_splits,
|
74 |
+
embedding=embedding
|
75 |
+
)
|
76 |
+
|
77 |
+
progress(0.9,desc="Vector store generated from the documents.")
|
78 |
+
|
79 |
+
return vector_store_db, gr.Column(visible=True), "Vector store generated from the documents"
|
80 |
+
|
81 |
+
def setup_llm(vector_store,llm_model,temp,max_tokens):
|
82 |
+
retriever=vector_store.as_retriever()
|
83 |
+
memory = ConversationBufferMemory(
|
84 |
+
memory_key="chat_history",
|
85 |
+
output_key='answer',
|
86 |
+
return_messages=True
|
87 |
+
)
|
88 |
+
llm = HuggingFaceEndpoint(
|
89 |
+
repo_id=llms[llm_model],
|
90 |
+
temperature = temp,
|
91 |
+
max_new_tokens = max_tokens,
|
92 |
+
top_k = 3 #top_k,
|
93 |
+
)
|
94 |
+
qa_chain = ConversationalRetrievalChain.from_llm(
|
95 |
+
llm,
|
96 |
+
retriever=retriever,
|
97 |
+
chain_type="stuff",
|
98 |
+
memory=memory,
|
99 |
+
return_source_documents=True,
|
100 |
+
verbose=False,
|
101 |
+
)
|
102 |
+
return qa_chain,gr.Column(visible=True)
|
103 |
+
|
104 |
+
def format_chat_history(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 |
+
def chat(qa_chain,msg,history):
|
112 |
+
formatted_chat_history = format_chat_history(history)
|
113 |
+
response = qa_chain.invoke({"question": msg, "chat_history": formatted_chat_history})
|
114 |
+
response_answer = response["answer"]
|
115 |
+
response_sources=response["source_documents"]
|
116 |
+
response_source1=response_sources[0].page_content.strip()
|
117 |
+
response_source_page=response_sources[0].metadata["page"]+1
|
118 |
+
new_history = history + [(msg, response_answer)]
|
119 |
+
return qa_chain, gr.update(value=""), new_history, response_source1, response_source_page
|
120 |
+
|
121 |
+
with gr.Blocks() as demo:
|
122 |
+
vector_store_db=gr.State()
|
123 |
+
qa_chain=gr.State()
|
124 |
+
|
125 |
+
gr.Markdown(
|
126 |
+
"""
|
127 |
+
# PDF Knowledge Base QA using RAG
|
128 |
+
"""
|
129 |
+
)
|
130 |
+
with gr.Accordion(label="Create Vectorstore",open=True):
|
131 |
+
with gr.Column():
|
132 |
+
file_list = gr.File(label='Upload your PDF files...', file_count='multiple', file_types=['.pdf'])
|
133 |
+
chunk_size=gr.Slider(minimum=100, maximum=1000, value=500, step=25, label="Chunk Size", interactive=True)
|
134 |
+
chunk_overlap=gr.Slider(minimum=10, maximum=200, value=30, step=10, label="Chunk Overlap", interactive=True)
|
135 |
+
vector_store=gr.Radio (["Chroma","FAISS","Lance"], value="Chroma", label="Vectorstore",type="index", interactive=True)
|
136 |
+
vectorstore_db_progress=gr.Textbox(label="Vectorstore database progress",value="Not started yet")
|
137 |
+
fileuploadbtn= gr.Button ("Generate Vectorstore and Move to LLM Setup Step")
|
138 |
+
with gr.Column(visible=False) as llm_column:
|
139 |
+
llm=gr.Radio(llms, label="Choose LLM Model", value=llms[0],type="index")
|
140 |
+
model_temp=gr.Slider(minimum=0.0, maximum=1.0,step=0.1, value=0.3, label="Temperature", interactive=True)
|
141 |
+
model_max_tokens=gr.Slider(minimum=100, maximum=1000,step=50, value=200, label="Maximum Tokens", interactive=True)
|
142 |
+
setup_llm_btn=gr.Button("Set up LLM and Start Chat")
|
143 |
+
with gr.Column(visible=False) as chat_column:
|
144 |
+
with gr.Row():
|
145 |
+
chatbot=gr.Chatbot(height=300)
|
146 |
+
with gr.Row():
|
147 |
+
source=gr.Textbox(info="Source",container=False,scale=4)
|
148 |
+
source_page=gr.Textbox(info="Page",container=False,scale=1)
|
149 |
+
with gr.Row():
|
150 |
+
prompt=gr.Textbox(container=False, scale=4, interactive=True)
|
151 |
+
promptsubmit=gr.Button("Submit", scale=1, interactive=True)
|
152 |
+
gr.Markdown(
|
153 |
+
"""
|
154 |
+
# Responsible AI Usage
|
155 |
+
Your documents uploaded to the system or interactions with the chatbot are not saved.
|
156 |
+
"""
|
157 |
+
)
|
158 |
+
|
159 |
+
fileuploadbtn.click(fn=indexdocs, inputs = [file_list,chunk_size,chunk_overlap,vector_store], outputs=[vector_store_db,llm_column,vectorstore_db_progress])# , outputs=[rep,prompt,promptsubmit])
|
160 |
+
setup_llm_btn.click(fn=setup_llm, inputs=[vector_store_db,llm,model_temp,model_max_tokens], outputs=[qa_chain,chat_column])
|
161 |
+
promptsubmit.click(fn=chat, inputs=[qa_chain,prompt,chatbot], outputs=[qa_chain,prompt,chatbot])
|
162 |
+
prompt.submit(fn=chat, inputs=[qa_chain,prompt,chatbot], outputs=[qa_chain,prompt,chatbot,source,source_page],queue=False)
|
163 |
+
|
164 |
+
if __name__ == "__main__":
|
165 |
+
demo.launch()
|