Spaces:
Sleeping
Sleeping
File size: 5,060 Bytes
8f0aa9f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 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 160 161 162 |
import streamlit as st
from pypdf import PdfReader
import torch
from io import BytesIO
from langchain.prompts import PromptTemplate
from langchain.document_loaders import PyPDFLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.vectorstores import FAISS
from langchain.chains import RetrievalQA
import textwrap
from langchain.llms.huggingface_pipeline import HuggingFacePipeline
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline, BitsAndBytesConfig
# load the environments
from dotenv import load_dotenv
load_dotenv()
#DEFINE SOME VARIABLES
CHUNK_SIZE = 1000
# Using HuggingFaceEmbeddings with the chosen embedding model
embeddings = HuggingFaceEmbeddings(
model_name="sentence-transformers/all-mpnet-base-v2",model_kwargs = {"device": "cuda"})
# transformer model configuration
quant_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_use_double_quant=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype=torch.bfloat16
)
# CREATE A VECTOR DATABASE - FAISS
def creat_vector_db(uploaded_pdfs) -> FAISS:
"""Read multiple PDFs, split, embedd and store the embeddings on FAISS vector store"""
text = ""
for pdf in uploaded_pdfs:
pdf_reader = PdfReader(pdf)
for page in pdf_reader.pages:
text += page.extract_text()
text_splitter = RecursiveCharacterTextSplitter(chunk_size=CHUNK_SIZE,
chunk_overlap=100)
texts = text_splitter.split_text(text)
vector_db = FAISS.from_texts(texts, embeddings) # create vector db for similarity search
vector_db.save_local("faiss_index") # save the vector db to avoid repeated calls to it
return vector_db
# LOAD LLM
def load_llm():
model_id = "Deci/DeciLM-6b"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(model_id,
trust_remote_code=True,
device_map = "auto",
quantization_config=quant_config)
pipe = pipeline("text-generation",
model=model,
tokenizer=tokenizer,
temperature=0.1,
return_full_text = True,
max_new_tokens=40,
repetition_penalty = 1.1)
llm = HuggingFacePipeline(pipeline=pipe)
return llm
# RESPONSE INSTRUCTIONS
def set_custom_prompt():
"""instructions, to the llm for text response generation"""
custom_prompt_template = """You have been given the following documents to answer the user's question.
If you do not have information from the information given to answer the questions just say 'I don't know the answer" and don't try to make up an answer.
Context: {context}
Question: {question}
Give a detailed helpful answer and nothing more.
Helpful answer:
"""
prompt = PromptTemplate(template=custom_prompt_template, input_variables=[
"context", "question"])
return prompt
# QUESTION ANSWERING CHAIN
def retrieval_qa_chain(prompt, vector_db):
"""Chain to retrieve answers. the chain takes the documents and
makes a call to the DeciLM-6b llm """
llm = load_llm()
qa_chain = RetrievalQA.from_chain_type(
llm = llm,
chain_type = "stuff",
retriever = vector_db.as_retriever(),
return_source_documents=True,
chain_type_kwargs={"prompt": prompt}
)
return qa_chain
# QUESTION ANSWER BOT
def qa_bot():
vectore_db = FAISS.load_local("faiss_index", embeddings)
conversation_prompt = set_custom_prompt()
conversation = retrieval_qa_chain(conversation_prompt, vectore_db)
return conversation
# RESPONSE FROM BOT
def bot_response(query):
conversation_result = qa_bot()
response = conversation_result({"query": query})
return response["result"]
def main():
st.set_page_config(page_title="Multiple PDFs chat with DeciLM-6b and LangChain",
page_icon=":file_folder:")
# page side panel
with st.sidebar:
st.subheader("Hello, welcome!")
pdfs = st.file_uploader(label="Upload your PDFs here and click Process!",
accept_multiple_files=True)
if st.button("Process"):
with st.spinner("Processing file(s)..."):
# create a vectore store
creat_vector_db(pdfs)
st.write("Your files are Processed. You set to ask questions!")
st.header("Chat with Multiple PDFs using DeciLM-6b-instruct LLM")
# Query side
query = st.text_input(label="Type your question based on the PDFs",
placeholder="Type question...")
if query:
st.write(f"Query: {query}")
st.text(textwrap.fill(bot_response(query), width=80))
if __name__ == "__main__":
main() |