Phaneendra99 commited on
Commit
bb92ce8
·
verified ·
1 Parent(s): 77b4f96

initial commit

Browse files
Files changed (2) hide show
  1. app.py +106 -0
  2. requirements.txt +7 -0
app.py ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import os
3
+ import streamlit as st
4
+ from PyPDF2 import PdfReader
5
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
6
+ from langchain.vectorstores import FAISS
7
+ from langchain.prompts import PromptTemplate
8
+ from langchain_huggingface import HuggingFaceEndpoint
9
+ from langchain_huggingface.embeddings import HuggingFaceEmbeddings
10
+ from dotenv import load_dotenv
11
+
12
+ # Getting API from environmental variables
13
+ load_dotenv()
14
+ huggingfacehub_api_token = os.getenv("HUGGINGFACE_API_KEY")
15
+
16
+
17
+ def get_pdf_text(pdf_docs):
18
+ text=""
19
+ for pdf in pdf_docs:
20
+ pdf_reader= PdfReader(pdf)
21
+ for page in pdf_reader.pages:
22
+ text+= page.extract_text()
23
+ return text
24
+
25
+
26
+
27
+ def get_text_chunks(text):
28
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=10000, chunk_overlap=1000)
29
+ chunks = text_splitter.split_text(text)
30
+ return chunks
31
+
32
+
33
+ def get_vector_store(text_chunks):
34
+ embeddings = HuggingFaceEmbeddings()
35
+ vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
36
+ vector_store.save_local("faiss_index")
37
+
38
+
39
+ def get_conversational_chain():
40
+
41
+ template = """
42
+ Answer the question as detailed as possible from the provided context, make sure to provide all the details, if the answer is not in
43
+ provided context just say, "answer is not available in the context", don't provide the wrong answer\n\n
44
+ Context:\n {context}?\n
45
+ Question: \n{question}\n
46
+
47
+ Answer:
48
+ """
49
+
50
+ prompt = PromptTemplate.from_template(template)
51
+ repo_id = "mistralai/Mistral-7B-Instruct-v0.3"
52
+
53
+ llm = HuggingFaceEndpoint(
54
+ repo_id=repo_id,
55
+ max_length=128,
56
+ temperature=0.5,
57
+ huggingfacehub_api_token=huggingfacehub_api_token,
58
+ )
59
+ llm_chain = prompt | llm
60
+
61
+ return llm_chain
62
+
63
+
64
+
65
+ def user_input(user_question):
66
+ embeddings = HuggingFaceEmbeddings()
67
+
68
+ new_db = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
69
+ docs = new_db.similarity_search(user_question)
70
+
71
+ chain = get_conversational_chain()
72
+
73
+ response = chain.invoke({"context":docs,"question": user_question})
74
+ # response = chain(
75
+ # {"input_documents":docs, "question": user_question}
76
+ # , return_only_outputs=True)
77
+
78
+ print(response)
79
+ st.write("Reply: ", response)
80
+
81
+
82
+
83
+
84
+ def main():
85
+ st.set_page_config("Chat PDF")
86
+ st.header("Chat with PDF using Mistral LLM")
87
+
88
+ user_question = st.text_input("Ask a Question from the PDF Files")
89
+
90
+ if user_question:
91
+ user_input(user_question)
92
+
93
+ with st.sidebar:
94
+ st.title("Menu:")
95
+ pdf_docs = st.file_uploader("Upload your PDF Files and Click on the Submit & Process Button", accept_multiple_files=True)
96
+ if st.button("Submit & Process"):
97
+ with st.spinner("Processing..."):
98
+ raw_text = get_pdf_text(pdf_docs)
99
+ text_chunks = get_text_chunks(raw_text)
100
+ get_vector_store(text_chunks)
101
+ st.success("Done")
102
+
103
+
104
+
105
+ if __name__ == "__main__":
106
+ main()
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ streamlit
2
+ python-dotenv
3
+ langchain
4
+ PyPDF2
5
+ langchain_community
6
+ faiss-cpu
7
+ langchain_huggingface