File size: 3,595 Bytes
abce492 deb7ff3 abce492 2d6e37a abce492 cd5eed1 abce492 c4b0a5b |
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 |
import os
import streamlit as st
from PyPDF2 import PdfReader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.vectorstores import FAISS
from langchain.chains.question_answering import load_qa_chain
from langchain.prompts import PromptTemplate
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
# Load a Hugging Face model (e.g., LLaMA or Falcon)
model_name = "mixedbread-ai/mxbai-embed-2d-large-v1" # Replace with your preferred model
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16)
def get_pdf_text(pdf_docs):
text = ""
for pdf in pdf_docs:
pdf_reader = PdfReader(pdf)
for page in pdf_reader.pages:
text += page.extract_text()
return text
# chuck_size = 1000, chunk_overlap = 200 (for shorted PDFs)
def get_text_chunks(text):
text_splitter= RecursiveCharacterTextSplitter(
chunk_size=10000,
chunk_overlap=1000,
# length_function=len
)
chunks=text_splitter.split_text(text)
return chunks
# Converting into Vector data/store (can also be stored)
def get_vector_store(text_chunks):
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
vector_store = FAISS.from_texts(texts=text_chunks, embedding=embeddings)
vector_store.save_local("faiss_index")
# return vector_store
def chat_with_huggingface(context, query):
prompt_template = """
Answer the query as detailed as possible from the provided context.
If the answer is not in the context, just say, "Answer is not available in the provided documents".
Context: {context}
Query: {query}
Answer:
"""
inputs = tokenizer(prompt_template, return_tensors="pt").to(model.device)
outputs = model.generate(**inputs, max_length=500, temperature=0.3)
return tokenizer.decode(outputs[0], skip_special_tokens=True)
def get_conversation_chain():
def huggingface_chain(inputs):
context = inputs["input_documents"][0].page_content # Extract context from FAISS search
query = inputs["question"]
return {"output_text": chat_with_huggingface(context, query)}
return huggingface_chain
def user_input(user_question):
# embeddings = GoogleGenerativeAIEmbeddings(model='embedding-gecko-001')
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
# Loading the embeddings
new_db = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
docs = new_db.similarity_search(user_question)
chain=get_conversation_chain()
response = chain(
{"input_documents": docs, "question": user_question})
print(response)
st.write("Reply: ", response["output_text"])
# Frontend page Processor
def main():
st.set_page_config(page_title="PDF Chatbot")
st.header("PDF Chatbot made for Pooja")
user_question = st.text_input("Puchiye kuch apne documents se:")
if user_question:
user_input(user_question)
with st.sidebar:
st.title("Menu:")
pdf_docs = st.file_uploader(
"Apne PDFs yaha pe upload karo then click on 'Process'", accept_multiple_files=True)
if st.button("Submit & Process"):
with st.spinner("Ruko Padh raha hu..."):
raw_text = get_pdf_text(pdf_docs)
text_chunks = get_text_chunks(raw_text)
get_vector_store(text_chunks)
st.success("Saare documents padh liya. Ab swaal pucho 😤")
if __name__ == '__main__':
main() |