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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import time
|
| 4 |
+
from langchain.document_loaders import PDFMinerLoader,CSVLoader ,UnstructuredWordDocumentLoader,TextLoader,OnlinePDFLoader
|
| 5 |
+
from langchain.text_splitter import CharacterTextSplitter
|
| 6 |
+
from langchain.embeddings import SentenceTransformerEmbeddings
|
| 7 |
+
from langchain.vectorstores import FAISS
|
| 8 |
+
from langchain import HuggingFaceHub
|
| 9 |
+
from langchain.chains import RetrievalQA
|
| 10 |
+
from langchain.prompts import PromptTemplate
|
| 11 |
+
from langchain.llms.huggingface_pipeline import HuggingFacePipeline
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
DEVICE = 'cpu'
|
| 15 |
+
FILE_EXT = ['pdf','text','csv','word']
|
| 16 |
+
DEFAULT_SYSTEM_PROMPT = "As a chatbot you are answering set of questions being requested ."
|
| 17 |
+
MAX_NEW_TOKENS = 4096
|
| 18 |
+
DEFAULT_TEMPERATURE = 0.1
|
| 19 |
+
DEFAULT_MAX_NEW_TOKENS = 2048
|
| 20 |
+
MAX_INPUT_TOKEN_LENGTH = 4000
|
| 21 |
+
|
| 22 |
+
def loading_file():
|
| 23 |
+
return "Loading..."
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def process_documents(documents,data_chunk=1500,chunk_overlap=100):
|
| 27 |
+
text_splitter = CharacterTextSplitter(chunk_size=data_chunk, chunk_overlap=chunk_overlap,separator='\n')
|
| 28 |
+
texts = text_splitter.split_documents(documents)
|
| 29 |
+
return texts
|
| 30 |
+
|
| 31 |
+
def get_hugging_face_model(model_id,temperature=0.1,max_tokens=4096,API_key=None):
|
| 32 |
+
chat_llm = HuggingFacePipeline.from_model_id(
|
| 33 |
+
model_id=model_id,
|
| 34 |
+
task="text-generation",
|
| 35 |
+
pipeline_kwargs={"max_new_tokens": max_tokens,"temperature": temperature,},
|
| 36 |
+
)
|
| 37 |
+
# chat_llm = HuggingFaceHub(huggingfacehub_api_token=API_key,
|
| 38 |
+
# repo_id=model_id,
|
| 39 |
+
# model_kwargs={"temperature": temperature, "max_new_tokens": max_tokens})
|
| 40 |
+
return chat_llm
|
| 41 |
+
|
| 42 |
+
def chat_application(temperature=0.1, max_tokens=1024):
|
| 43 |
+
|
| 44 |
+
llm = get_hugging_face_model(model_id='tiiuae/falcon-7b-instruct',temperature=temperature, max_tokens=max_tokens)
|
| 45 |
+
return llm
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def document_loader(file_path,doc_type='pdf',temperature=0.1,max_tokens=2048):
|
| 49 |
+
document = None
|
| 50 |
+
if doc_type == 'pdf':
|
| 51 |
+
document = process_pdf_document(document_file=file_path)
|
| 52 |
+
elif doc_type == 'text':
|
| 53 |
+
document = process_text_document(document_file=file_path)
|
| 54 |
+
elif doc_type == 'csv':
|
| 55 |
+
document = process_csv_document(document_file=file_path)
|
| 56 |
+
elif doc_type == 'word':
|
| 57 |
+
document = process_word_document(document_file=file_path)
|
| 58 |
+
|
| 59 |
+
embedding_model = SentenceTransformerEmbeddings(model_name='thenlper/gte-base',model_kwargs={"device": DEVICE})
|
| 60 |
+
|
| 61 |
+
texts = process_documents(documents=document)
|
| 62 |
+
global vector_db
|
| 63 |
+
vector_db = FAISS.from_documents(documents=texts, embedding= embedding_model)
|
| 64 |
+
global qa
|
| 65 |
+
qa = RetrievalQA.from_chain_type(llm=chat_application(temperature=temperature,
|
| 66 |
+
max_tokens=max_tokens
|
| 67 |
+
),
|
| 68 |
+
chain_type='stuff',
|
| 69 |
+
retriever=vector_db.as_retriever(),
|
| 70 |
+
# chain_type_kwargs=chain_type_kwargs,
|
| 71 |
+
return_source_documents=True
|
| 72 |
+
)
|
| 73 |
+
return "Document Processing completed ..."
|
| 74 |
+
|
| 75 |
+
def process_text_document(document_file):
|
| 76 |
+
loader = TextLoader(document_file.name)
|
| 77 |
+
document = loader.load()
|
| 78 |
+
return document
|
| 79 |
+
|
| 80 |
+
def process_csv_document(document_file):
|
| 81 |
+
loader = CSVLoader(file_path=document_file.name)
|
| 82 |
+
document = loader.load()
|
| 83 |
+
return document
|
| 84 |
+
|
| 85 |
+
def process_word_document(document_file):
|
| 86 |
+
loader = UnstructuredWordDocumentLoader(file_path=document_file.name)
|
| 87 |
+
document = loader.load()
|
| 88 |
+
return document
|
| 89 |
+
|
| 90 |
+
def process_pdf_document(document_file):
|
| 91 |
+
print("Document File Name :",document_file.name)
|
| 92 |
+
loader = PDFMinerLoader(document_file.name)
|
| 93 |
+
document = loader.load()
|
| 94 |
+
return document
|
| 95 |
+
|
| 96 |
+
def clear_chat():
|
| 97 |
+
return []
|
| 98 |
+
|
| 99 |
+
def infer(question, history):
|
| 100 |
+
# res = []
|
| 101 |
+
# # for human, ai in history[:-1]:
|
| 102 |
+
# # pair = (human, ai)
|
| 103 |
+
# # res.append(pair)
|
| 104 |
+
|
| 105 |
+
# chat_history = res
|
| 106 |
+
print("Question in infer :",question)
|
| 107 |
+
result = qa({"query": question})
|
| 108 |
+
matching_docs_score = vector_db.similarity_search_with_score(question)
|
| 109 |
+
|
| 110 |
+
print(" Matching_doc ",matching_docs_score)
|
| 111 |
+
|
| 112 |
+
return result["result"]
|
| 113 |
+
|
| 114 |
+
def bot(history):
|
| 115 |
+
|
| 116 |
+
response = infer(history[-1][0], history)
|
| 117 |
+
history[-1][1] = ""
|
| 118 |
+
|
| 119 |
+
for character in response:
|
| 120 |
+
history[-1][1] += character
|
| 121 |
+
time.sleep(0.05)
|
| 122 |
+
yield history
|
| 123 |
+
|
| 124 |
+
def add_text(history, text):
|
| 125 |
+
history = history + [(text, None)]
|
| 126 |
+
return history, ""
|
| 127 |
+
|
| 128 |
+
css="""
|
| 129 |
+
#col-container {max-width: 700px; margin-left: auto; margin-right: auto;}
|
| 130 |
+
"""
|
| 131 |
+
|
| 132 |
+
title = """
|
| 133 |
+
<div style="text-align: center;max-width: 700px;">
|
| 134 |
+
<h1>Chat with Data • OpenAI/HuggingFace</h1>
|
| 135 |
+
<p style="text-align: center;">Upload a file from system,UpLoad file and generate embeddings, <br />
|
| 136 |
+
once status is ready, you can start asking questions about the data you uploaded without chat history <br />
|
| 137 |
+
and gives you option to use HuggingFace/OpenAI as LLM's, make sure to add your key.
|
| 138 |
+
</p>
|
| 139 |
+
</div>
|
| 140 |
+
"""
|
| 141 |
+
|
| 142 |
+
with gr.Blocks(css=css) as demo:
|
| 143 |
+
with gr.Column(elem_id="col-container"):
|
| 144 |
+
gr.HTML(title)
|
| 145 |
+
|
| 146 |
+
with gr.Group():
|
| 147 |
+
chatbot = gr.Chatbot(height=300)
|
| 148 |
+
with gr.Row():
|
| 149 |
+
question = gr.Textbox(label="Type your question !",lines=1)
|
| 150 |
+
submit_btn = gr.Button(value="Send message", variant="primary", scale = 1)
|
| 151 |
+
clean_chat_btn = gr.Button("Delete Chat")
|
| 152 |
+
|
| 153 |
+
with gr.Column():
|
| 154 |
+
with gr.Box():
|
| 155 |
+
LLM_option = gr.Dropdown(['tiiuae/falcon-7b-instruct'],label='Large Language Model Selection',info='LLM Service')
|
| 156 |
+
|
| 157 |
+
with gr.Column():
|
| 158 |
+
with gr.Box():
|
| 159 |
+
file_extension = gr.Dropdown(FILE_EXT, label="File Extensions", info="Select type of file to upload !")
|
| 160 |
+
pdf_doc = gr.File(label="Upload File to start QA", file_types=FILE_EXT, type="file")
|
| 161 |
+
with gr.Accordion(label='Advanced options', open=False):
|
| 162 |
+
max_new_tokens = gr.Slider(
|
| 163 |
+
label='Max new tokens',
|
| 164 |
+
minimum=2048,
|
| 165 |
+
maximum=MAX_NEW_TOKENS,
|
| 166 |
+
step=1024,
|
| 167 |
+
value=DEFAULT_MAX_NEW_TOKENS,
|
| 168 |
+
)
|
| 169 |
+
temperature = gr.Slider(
|
| 170 |
+
label='Temperature',
|
| 171 |
+
minimum=0.1,
|
| 172 |
+
maximum=4.0,
|
| 173 |
+
step=0.1,
|
| 174 |
+
value=DEFAULT_TEMPERATURE,
|
| 175 |
+
)
|
| 176 |
+
with gr.Row():
|
| 177 |
+
langchain_status = gr.Textbox(label="Status", placeholder="", interactive = False)
|
| 178 |
+
load_pdf = gr.Button("Upload File & Generate Embeddings",).style(full_width = False)
|
| 179 |
+
|
| 180 |
+
# chatbot = gr.Chatbot()l̥
|
| 181 |
+
# question = gr.Textbox(label="Question", placeholder="Type your question and hit Enter")
|
| 182 |
+
# submit_button = gr.Button("Send Message")
|
| 183 |
+
|
| 184 |
+
if pdf_doc:
|
| 185 |
+
load_pdf.click(loading_file, None, langchain_status, queue=False)
|
| 186 |
+
load_pdf.click(document_loader, inputs=[pdf_doc,file_extension,temperature,max_new_tokens], outputs=[langchain_status], queue=False)
|
| 187 |
+
|
| 188 |
+
question.submit(add_text, inputs=[chatbot, question], outputs=[chatbot, question]).then(bot, chatbot, chatbot)
|
| 189 |
+
submit_btn.click(add_text, inputs=[chatbot, question], outputs=[chatbot, question]).then(bot, chatbot, chatbot)
|
| 190 |
+
# submit_btn.then(chatf.highlight_found_text, [chatbot, sources], [sources])
|
| 191 |
+
clean_chat_btn.click(clear_chat, [], chatbot)
|
| 192 |
+
|
| 193 |
+
|
| 194 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
openai
|
| 2 |
+
tiktoken
|
| 3 |
+
chromadb
|
| 4 |
+
langchain
|
| 5 |
+
unstructured
|
| 6 |
+
unstructured[local-inference]
|
| 7 |
+
transformers
|
| 8 |
+
torch
|
| 9 |
+
faiss-cpu
|
| 10 |
+
sentence-transformers
|
| 11 |
+
chromadb
|