Spaces:
Sleeping
Sleeping
yourownvibhore
commited on
Commit
β’
8495354
1
Parent(s):
bf8b4fe
Upload 2 files
Browse files- app.py +123 -0
- requirements.txt +12 -0
app.py
ADDED
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from PyPDF2 import PdfReader
|
3 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
4 |
+
from llama_index.core import Settings
|
5 |
+
from langchain_community.vectorstores import FAISS
|
6 |
+
import pickle
|
7 |
+
import os
|
8 |
+
from dotenv import load_dotenv
|
9 |
+
from langchain.chains.question_answering import load_qa_chain
|
10 |
+
from langchain.prompts import ChatPromptTemplate
|
11 |
+
from dotenv import load_dotenv
|
12 |
+
|
13 |
+
from langchain_huggingface import HuggingFaceEndpoint
|
14 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
15 |
+
from langchain.prompts import PromptTemplate
|
16 |
+
|
17 |
+
|
18 |
+
|
19 |
+
load_dotenv()
|
20 |
+
hugging_face_api = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
21 |
+
os.environ["HUGGINGFACEHUB_API_TOKEN"]=hugging_face_api
|
22 |
+
|
23 |
+
|
24 |
+
|
25 |
+
|
26 |
+
|
27 |
+
def get_pdf_text(pdf_docs):
|
28 |
+
text=""
|
29 |
+
for pdf in pdf_docs:
|
30 |
+
pdf_reader= PdfReader(pdf)
|
31 |
+
for page in pdf_reader.pages:
|
32 |
+
text+= page.extract_text()
|
33 |
+
return text
|
34 |
+
|
35 |
+
|
36 |
+
|
37 |
+
def get_text_chunks(text):
|
38 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=10000, chunk_overlap=1000)
|
39 |
+
chunks = text_splitter.split_text(text)
|
40 |
+
return chunks
|
41 |
+
|
42 |
+
def get_vector_store(text_chunks):
|
43 |
+
model_name = "sentence-transformers/all-MiniLM-L12-v2"
|
44 |
+
model_kwargs = {'device': 'cpu'}
|
45 |
+
encode_kwargs = {'normalize_embeddings': False}
|
46 |
+
embeddings = HuggingFaceEmbeddings(model_name=model_name,model_kwargs=model_kwargs,encode_kwargs=encode_kwargs)
|
47 |
+
vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
|
48 |
+
vector_store.save_local("faiss_index")
|
49 |
+
|
50 |
+
def get_conversational_chain():
|
51 |
+
|
52 |
+
prompt_template = """
|
53 |
+
Answer the question as detailed as possible from the provided context, make sure to provide all the details, if the answer is not in
|
54 |
+
provided context just say, "answer is not available in the context", don't provide the wrong answer\n\n
|
55 |
+
Context:\n {context}?\n
|
56 |
+
Question: \n{question}\n
|
57 |
+
|
58 |
+
Answer:
|
59 |
+
"""
|
60 |
+
|
61 |
+
repo_id="mistralai/Mistral-7B-Instruct-v0.3"
|
62 |
+
model = HuggingFaceEndpoint(repo_id=repo_id,max_length=128, token=hugging_face_api)
|
63 |
+
|
64 |
+
prompt = PromptTemplate(template = prompt_template, input_variables = ["context", "question"])
|
65 |
+
chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
|
66 |
+
|
67 |
+
return chain
|
68 |
+
|
69 |
+
def user_input(user_question):
|
70 |
+
model_name = "sentence-transformers/all-MiniLM-L12-v2"
|
71 |
+
model_kwargs = {'device': 'cpu'}
|
72 |
+
encode_kwargs = {'normalize_embeddings': False}
|
73 |
+
embeddings = HuggingFaceEmbeddings(model_name=model_name,model_kwargs=model_kwargs,encode_kwargs=encode_kwargs)
|
74 |
+
|
75 |
+
|
76 |
+
new_db = FAISS.load_local("faiss_index", embeddings,allow_dangerous_deserialization=True)
|
77 |
+
docs = new_db.similarity_search(user_question)
|
78 |
+
|
79 |
+
chain = get_conversational_chain()
|
80 |
+
|
81 |
+
|
82 |
+
response = chain(
|
83 |
+
{"input_documents":docs, "question": user_question}
|
84 |
+
, return_only_outputs=True)
|
85 |
+
|
86 |
+
print(response)
|
87 |
+
st.write("Reply: ", response["output_text"])
|
88 |
+
|
89 |
+
|
90 |
+
|
91 |
+
|
92 |
+
def main():
|
93 |
+
st.set_page_config("Chat PDF")
|
94 |
+
with st.sidebar:
|
95 |
+
st.title('Chat PDF π')
|
96 |
+
st.markdown('''
|
97 |
+
## About
|
98 |
+
This is a simple Streamlit app that allows you to chat with your PDF files.
|
99 |
+
## How to use
|
100 |
+
|
101 |
+
''')
|
102 |
+
st.write('Here you can upload your PDF files and ask questions from the PDF files.We will provide you the most relevant answer from the PDF files. You can also upload multiple files at once.')
|
103 |
+
st.header("Chat with your PDF π")
|
104 |
+
pdf_docs = st.file_uploader("Upload your PDF Files and Click on the Submit & Process Button. you can also upload multiple files also", accept_multiple_files=True)
|
105 |
+
if st.button("Submit & Process"):
|
106 |
+
with st.spinner("Processing..."):
|
107 |
+
raw_text = get_pdf_text(pdf_docs)
|
108 |
+
text_chunks = get_text_chunks(raw_text)
|
109 |
+
get_vector_store(text_chunks)
|
110 |
+
st.success("Done")
|
111 |
+
|
112 |
+
user_question = st.text_input("Ask a Question from the PDF Files")
|
113 |
+
|
114 |
+
if user_question:
|
115 |
+
user_input(user_question)
|
116 |
+
|
117 |
+
|
118 |
+
|
119 |
+
|
120 |
+
|
121 |
+
|
122 |
+
if __name__ == '__main__':
|
123 |
+
main()
|
requirements.txt
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
langchain-huggingface
|
2 |
+
PyPDF2
|
3 |
+
python-dotenv
|
4 |
+
streamlit
|
5 |
+
streamlit_extras
|
6 |
+
huggingface_hub
|
7 |
+
accelerate
|
8 |
+
bitsandbytes
|
9 |
+
langchain-community
|
10 |
+
faiss-cpu
|
11 |
+
InstructorEmbedding
|
12 |
+
sentence_transformers
|